Exemple #1
0
def BFTIterLinkedList() -> List[Node]:
    graph = createLinkedList(10000)
    return GraphSearch.BFTIter(graph)
Exemple #2
0
    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")
Exemple #3
0
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 BFTIterLinkedList(graph):
     from graphSearch import GraphSearch
     return GraphSearch.BFTIter(graph)
def BFTIterLinkedList(graph):
    iterativePath = GraphSearch()
    iterPath = iterativePath.BFTIter(graph)
    return iterPath
    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: ")
    printTraversal(recursiveReturn)
Exemple #7
0
def BFTIterLinkedList(node_count=10000):
    self = createLinkedList(node_count)
    print(self)
    return GraphSearch.BFTIter(self, 0)
Exemple #8
0
    # Weighted Linked List:
    wll = createWeightedLinkedList(10, 2)
    print(wll)

    # Random unweighted graph:
    print("Random Unwighted graph:")
    graph = createRandomUnweightedGraphIter(200)
    print(graph)

    # Gridgraph:
    print("Grid Graph:")
    gGraph = createRandomGridGraph(8)
    print(gGraph)

    # Graph Traversals:
    print("Graph Searches:")
    GraphSearch.printResult(GraphSearch(), GraphSearch.BFTIter(graph, 0))
    GraphSearch.printResult(
        GraphSearch(),
        GraphSearch.DFTIter(createRandomUnweightedGraphIter(200), 0))
    # print(BFTRecLinkedList()) # Exceeds max recursion depth!
    GraphSearch.printResult(GraphSearch(),
                            BFTIterLinkedList(100))  # BFT on a linked list
    GraphSearch.printResult(GraphSearch(), BFTRecLinkedList(
        100))  # Exceeds recursion limit if node_count is too high!
    # GraphSearch.printResult(GraphSearch(), BFTIterLinkedList()) # Works great with default (10,000 nodes), spams the console!

    print("Main took:", f"{time.perf_counter()-start:.3}",
          "seconds")  # Prints the time taken for main to run.