G.addVertex('d') G.addVertex('e') G.addEdge('a', 'b', 1) G.addEdge('a', 'c', 1) G.addEdge('b', 'd', 1) G.addEdge('b', 'e', 1) G.addEdge('c', 'd', 1) G.addEdge('c', 'e', 1) G.addEdge('d', 'e', 1) G.addEdge('e', 'a', 1) print has_cycle(G,G.getVertex('a')) #existence of a cycle in a directed graph DG = DirectedGraph() DG.addVertex('a') DG.addVertex('b') DG.addVertex('c') DG.addVertex('d') DG.addVertex('e') DG.addEdge('a', 'b', 1) DG.addEdge('b', 'c', 1) DG.addEdge('c', 'd', 1) DG.addEdge('e', 'a', 1) DG.addEdge('e', 'c', 1) print has_cycle(DG,DG.getVertex('a'))
G.addEdge('a', 'b', 1) G.addEdge('a', 'c', 1) G.addEdge('b', 'd', 1) G.addEdge('b', 'e', 1) G.addEdge('c', 'd', 1) G.addEdge('c', 'e', 1) G.addEdge('d', 'e', 1) G.addEdge('e', 'a', 1) topologicalSort(G) # topologicalSortedNodes = topologicalSort(G,G.getVertex('a')) DG = DirectedGraph() DG.addVertex('a') DG.addVertex('b') DG.addVertex('c') DG.addVertex('d') DG.addVertex('e') DG.addVertex('f') DG.addVertex('g') DG.addVertex('h') DG.addVertex('i') DG.addEdge('a', 'd', 1) DG.addEdge('b', 'd', 1) DG.addEdge('c', 'd', 1) DG.addEdge('d', 'g', 1) DG.addEdge('d', 'e', 1) DG.addEdge('g', 'h', 1)
G.addVertex('e') G.addEdge('a', 'b', 1) G.addEdge('a', 'c', 1) G.addEdge('b', 'd', 1) G.addEdge('b', 'e', 1) G.addEdge('c', 'd', 1) G.addEdge('c', 'e', 1) G.addEdge('d', 'e', 1) G.addEdge('e', 'a', 1) topologicalSort(G) # topologicalSortedNodes = topologicalSort(G,G.getVertex('a')) DG = DirectedGraph() DG.addVertex('a') DG.addVertex('b') DG.addVertex('c') DG.addVertex('d') DG.addVertex('e') DG.addVertex('f') DG.addVertex('g') DG.addVertex('h') DG.addVertex('i') DG.addEdge('a', 'd', 1) DG.addEdge('b', 'd', 1) DG.addEdge('c', 'd', 1) DG.addEdge('d', 'g', 1) DG.addEdge('d', 'e', 1) DG.addEdge('g', 'h', 1)