def buildGraph(): "build graph from state transitions" G = Graph(len(d)) for e in d.iteritems(): # e = ( key, [ (next_state, action), ..., (next_state, action) ] ) v = symTbl[e[0]] for s in e[1]: ns = s[0] action = s[1] w = symTbl[ns] G.addEdge(v, w) return G
# G = Graph(13) # G.addEdge(0, 1) # G.addEdge(0, 2) # G.addEdge(0, 6) # G.addEdge(0, 5) # G.addEdge(6, 4)has # G.addEdge(4, 3) # G.addEdge(4, 5) # G.addEdge(3, 5) # G.addEdge(7, 8) # G.addEdge(9, 10) # G.addEdge(9, 11) # G.addEdge(9, 12) # G.addEdge(11, 12) # Test a graph with no cycle Gacyc = Graph(8) Gacyc.addEdge(0, 1) Gacyc.addEdge(0, 2) Gacyc.addEdge(2, 3) Gacyc.addEdge(2, 4) Gacyc.addEdge(4, 5) Gacyc.addEdge(5, 6) Gacyc.addEdge(5, 7) G = Gacyc c = Cycle(G) c.hasCycle()
#!/bin/python3 import collections from GraphLib import Graph g = Graph.Graph() a = Graph.Vertex('A') g.add_vertex(a) g.add_vertex(Graph.Vertex('B')) for i in range(ord('A'), ord('K')): g.add_vertex(Graph.Vertex(chr(i))) edges = [ 'AB', 'AE', 'BF', 'CG', 'DE', 'DH', 'EH', 'FG', 'FI', 'FJ', 'GJ', 'HI' ] for edge in edges: g.add_edge(edge[:1], edge[1:]) g.bfs(a) #isFound = g.bfsMod(a, 'I') #isFound = g.bfsMod(a, 'X') #print ("Node found:", isFound) g.print_graph()