| 
 | 1 | +# Author: OMKAR PATHAK  | 
 | 2 | + | 
 | 3 | +# Time Complexity: O(|V| + |E|)  | 
 | 4 | + | 
 | 5 | +class Graph():  | 
 | 6 | +    def __init__(self):  | 
 | 7 | +        self.vertex = {}  | 
 | 8 | + | 
 | 9 | +    # for printing the Graph vertexes  | 
 | 10 | +    def printGraph(self):  | 
 | 11 | +        for i in self.vertex.keys():  | 
 | 12 | +            print(i,' -> ', ' -> '.join([str(j) for j in self.vertex[i]]))  | 
 | 13 | + | 
 | 14 | +    # for adding the edge beween two vertexes  | 
 | 15 | +    def addEdge(self, fromVertex, toVertex):  | 
 | 16 | +        # check if vertex is already present,  | 
 | 17 | +        if fromVertex in self.vertex.keys() and toVertex in self.vertex.keys():  | 
 | 18 | +            self.vertex[fromVertex].append(toVertex)  | 
 | 19 | +            self.vertex[toVertex].append(fromVertex)  | 
 | 20 | +        else:  | 
 | 21 | +            # else make a new vertex  | 
 | 22 | +            self.vertex[fromVertex] = [toVertex]  | 
 | 23 | +            self.vertex[toVertex] = [fromVertex]  | 
 | 24 | + | 
 | 25 | +    # This function will return True if graph is cyclic else return False  | 
 | 26 | +    def checkCyclic(self):  | 
 | 27 | +        visited = [False] * len(self.vertex)            # Marking all vertices as not visited  | 
 | 28 | +        for vertex in range(len(self.vertex)):  | 
 | 29 | +            # Call the recursive function only if not visited  | 
 | 30 | +            if visited[vertex] == False:  | 
 | 31 | +                if self.checkCyclicRec(visited, -1, vertex) == True:  | 
 | 32 | +                    return True  | 
 | 33 | +        return False  | 
 | 34 | + | 
 | 35 | +    # Recursive function for finding the cycle  | 
 | 36 | +    def checkCyclicRec(self, visited, parent, vertex):  | 
 | 37 | +        # Mark the current node in visited  | 
 | 38 | +        visited[vertex] = True  | 
 | 39 | + | 
 | 40 | +        # mark all adjacent nodes of the current node  | 
 | 41 | +        for adjacentNode in self.vertex[vertex]:  | 
 | 42 | +            if visited[adjacentNode] == False:  | 
 | 43 | +                if self.checkCyclicRec(visited, vertex, adjacentNode) == True:  | 
 | 44 | +                    return True  | 
 | 45 | +            elif parent != adjacentNode:  | 
 | 46 | +                return True  | 
 | 47 | + | 
 | 48 | +        return False  | 
 | 49 | + | 
 | 50 | +if __name__ == '__main__':  | 
 | 51 | +    graph = Graph()  | 
 | 52 | +    graph.addEdge(0, 1)  | 
 | 53 | +    graph.addEdge(0, 2)  | 
 | 54 | +    graph.addEdge(1, 2)  | 
 | 55 | +    graph.addEdge(2, 0)  | 
 | 56 | +    graph.addEdge(2, 3)  | 
 | 57 | +    graph.addEdge(3, 3)  | 
 | 58 | + | 
 | 59 | +    graph.printGraph()  | 
 | 60 | + | 
 | 61 | +    if graph.checkCyclic() == 1:  | 
 | 62 | +        print ("Graph has a cycle")  | 
 | 63 | +    else:  | 
 | 64 | +        print ("Graph has no cycle")  | 
 | 65 | + | 
 | 66 | +    g1 = Graph()  | 
 | 67 | +    g1.addEdge(0,1)  | 
 | 68 | +    g1.addEdge(1,2)  | 
 | 69 | + | 
 | 70 | +    g1.printGraph()  | 
 | 71 | + | 
 | 72 | +    if g1.checkCyclic() == 1:  | 
 | 73 | +        print ("Graph has a cycle")  | 
 | 74 | +    else:  | 
 | 75 | +        print ("Graph has no cycle")  | 
 | 76 | + | 
 | 77 | +    # OUTPUT:  | 
 | 78 | +    # 0  ->  2 -> 2  | 
 | 79 | +    # 1  ->  0 -> 2  | 
 | 80 | +    # 2  ->  3  | 
 | 81 | +    # 3  ->  2 -> 3 -> 3  | 
 | 82 | +    # Graph has a cycle  | 
 | 83 | +    # 0  ->  1  | 
 | 84 | +    # 1  ->  2  | 
 | 85 | +    # 2  ->  1  | 
 | 86 | +    # Graph has no cycle  | 
0 commit comments