コード例 #1
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")
コード例 #2
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)
コード例 #3
0
    linkedList = createLinkedList(25)
    printGraph(linkedList)
    print("######################## START OF TEST FOR 3D, 3E ########################")
    a = GraphNode('A')
    b = GraphNode('B')
    c = GraphNode('C')
    d = GraphNode('D')
    e = GraphNode('E')
    f = GraphNode('F')  # Unconnected node
    a.neighbors = [b, c]
    b.neighbors = [d, e, a]
    c.neighbors = [a]
    d.neighbors = [b]
    e.neighbors = [b]
    path = GraphSearch()
    dfsRec = path.DFSRec(a, e)
    dfsIter = path.DFSIter(a, e)
    print("Recursive DFS Traversal is: ")
    printTraversal(dfsRec)
    print("Iterative DFS Traversal is: ")
    printTraversal(dfsIter)

    print("######################## START OF TEST FOR 3F, 3G ########################")
    graph = Graph()
    g = GraphNode('G')
    graph.addNode(g.val)
    h = GraphNode('H')
    graph.addNode(h.val)
    i = GraphNode('I')
    graph.addNode(i.val)
    j = GraphNode('J')