def main(): graph = createRandomUnweightedGraphIter(10) nodes = graph.getAllNodes() listOfNodes = list(nodes) listOfNodes.sort(key=lambda x: x.nodeVal) dfs_rec_arr = GraphSearch.DFSRec(listOfNodes[3], listOfNodes[9]) dfs_iter_arr = GraphSearch.DFSIter(listOfNodes[3], listOfNodes[9]) bft_rec_arr = GraphSearch.BFTRec(graph) bft_iter_arr = GraphSearch.BFTIter(graph) print("--- Random Unweighted Graph ---") printGraph(graph) print("--- DFS-Rec ---") printList(dfs_rec_arr) print("\n--- DFS-Iter ---") printList(dfs_iter_arr) print("\n--- BFT-Rec ---") printList(bft_rec_arr) print("\n--- BFT-Iter ---") printList(bft_iter_arr) _graph = createLinkedList(10) _nodes = _graph.getAllNodes() _listOfNodes = list(_nodes) _listOfNodes.sort(key=lambda x: x.nodeVal) _dfs_rec_arr = GraphSearch.DFSRec(_listOfNodes[3], _listOfNodes[9]) _dfs_iter_arr = GraphSearch.DFSIter(_listOfNodes[3], _listOfNodes[9]) _bft_rec_arr = GraphSearch.BFTRec(_graph) _bft_iter_arr = GraphSearch.BFTIter(_graph) print("\n--- Linked List ---") printGraph(_graph) print("--- DFS-Rec ---") printList(_dfs_rec_arr) print("\n--- DFS-Iter ---") printList(_dfs_iter_arr) print("\n--- BFT-Rec ---") printList(_bft_rec_arr) print("\n--- BFT-Iter ---") printList(_bft_iter_arr) topSort_graph = createRandomDAGIter(1000) print("\n--- Random Directed Acyclic Graph ---") printGraph(topSort_graph) topSort_kahns = TopSort.Kahns(topSort_graph) print("--- Kahns Output---") printList(topSort_kahns) topSort_mDFS = TopSort.mDFS(topSort_graph) print("\n--- mDFS Output ---") printList(topSort_mDFS) dijkstras_graph = createRandomCompleteWeightedGraph(1000) print("\n--- Random Complete Weighted Graph ---") printGraph(dijkstras_graph) dijkstras_result = runDijkstras(dijkstras_graph, 2) print("--- Dijkstras Output ---") printDijkstras(dijkstras_result) astar_graph = createRandomGridGraph(100) print("--- Random Grid Graph ---") printGraph(astar_graph) astar_result = runAstar(astar_graph, 0, 9999) print("--- A* Output ---") printList(astar_result)
def BFTRecLinkedList() -> List[Node]: graph = createLinkedList(100) return GraphSearch.BFTRec(graph)
def BFTIterLinkedList(graph): return graphSearch.BFTIter(graph) if __name__ == "__main__": main = Main() graphSearch = GraphSearch() graph1 = main.createRandomUnweightedGraphIter(10) graph2 = main.createLinkedList(10) graphRec = main.createLinkedList(100) graphIter = main.createLinkedList(10000) graph3 = main.BFTRecLinkedList(graphRec) graph4 = main.BFTIterLinkedList(graphIter) print("-------------Unweighted Graph-------------" + "\n" "DFS-Rec: " + str(graphSearch.DFSRec(graph1, 4, 8)) + "\n" "DFS-Iter: " + str(graphSearch.DFSIter(graph1, 4, 8)) + "\n" "BFT-Rec: " + str(graphSearch.BFTRec(graph1)) + "\n" "BFT-Iter: " + str(graphSearch.BFTIter(graph1)) + "\n") print("---------------Linked List----------------" + "\n" "DFS-Rec: " + str(graphSearch.DFSRec(graph2, 4, 8)) + "\n" "DFS-Iter: " + str(graphSearch.DFSIter(graph2, 4, 8)) + "\n" "BFT-Rec: " + str(graphSearch.BFTRec(graph2)) + "\n" "BFT-Iter: " + str(graphSearch.BFTIter(graph2)) + "\n") print("----------------BFT-Rec Linked List-------------" + "\n" + str(graph3) + "\n" + "----------------BFT-Iter Linked List-------------" + "\n" + str(graph4) + "\n")
def BFTRecLinkedList(graph): from graphSearch import GraphSearch return GraphSearch.BFTRec(graph)
def BFTRecLinkedList(graph): recursivePath = GraphSearch() recPath = recursivePath.BFTRec(graph) return recPath
graph.addNode(h.val) i = GraphNode('I') graph.addNode(i.val) j = GraphNode('J') graph.addNode(j.val) k = GraphNode('K') graph.addNode(k.val) l = GraphNode('L') graph.addNode(l.val) graph.addUndirectedEdge(g, h) graph.addUndirectedEdge(g, i) graph.addUndirectedEdge(h, l) graph.addUndirectedEdge(i, j) graph.addUndirectedEdge(i, k) path = GraphSearch() bftRec = path.BFTRec(graph) # Recursive BFT bftIter = path.BFTIter(graph) # Iterative BFT print("Recursive BFT Traversal is: ") printTraversal(bftRec) print("Iterative BFT Traversal is: ") printTraversal(bftIter) print("######################## START OF TEST FOR 3H, 3I ########################") # My computer does not crash due to stack overflow for 100 elements. Anything above it will cause stack overflow. bigLinkedList = createLinkedList(100) print("Created big Linked List...") recursiveReturn = BFTRecLinkedList(bigLinkedList) print("Finished recursive BFT...") iterativeReturn = BFTIterLinkedList(bigLinkedList) print("Finished iterative BFT...") print("Recursive Linked-List BFT Traversal is: ")
def BFTRecLinkedList(node_count=10000): self = createLinkedList(node_count) return GraphSearch.BFTRec(self, 0)