educative.io

The input graph is a directed graph and not an undirected graph in is_tree

Although the question claims that the input should be an undirected graph but the very first thing that I do in is_tree function is print the graph and here is the output:

Adjacency List of Directed Graph<<
| 0 | => [ 3 ] → [ 2 ] → [ 1 ] → None
| 1 | => None
| 2 | => None
| 3 | => [ 4 ] → None
| 4 | => None
It clearly shows that the input is a directed graph and hence when you start from 1 you cannot reach other graphs.
Are we supposed to create the directed graph by ourself taking the input. I looked into the graph class and it is only creating the directed graph. Here is the graph class add_edge method:

Function to add an edge from source to destination

def add_edge(self, source, destination):
    if source < self.vertices and destination < self.vertices:

    # As we are implementing a directed graph, (1,0) is not equal to (0,1)
        self.array[source].insert_at_head(destination)

Course: Data Structures for Coding Interviews in Python - AI-Powered Learning for Developers
Lesson: Challenge: Check if a Given Undirected Graph is Tree or not? - Data Structures for Coding Interviews in Python