graph.add_edge(0, 1, 4)
    graph.add_edge(0, 7, 8)
    graph.add_edge(1, 2, 8)
    graph.add_edge(1, 7, 11)
    graph.add_edge(2, 3, 7)
    graph.add_edge(2, 8, 2)
    graph.add_edge(2, 5, 4)
    graph.add_edge(3, 4, 9)
    graph.add_edge(3, 5, 14)
    graph.add_edge(4, 5, 10)
    graph.add_edge(5, 6, 2)
    graph.add_edge(6, 7, 1)
    graph.add_edge(6, 8, 6)
    graph.add_edge(7, 8, 7)

    path, distance = dijkastra_algo(graph.get_graph(), 0)

    for key, value in distance.items():
        print "{0} to {1} distance = {2}".format(0, key, value)

    print "*" * 50

    for vertex in graph.get_vertex():
        x = vertex
        path_list = []
        while x != 0:
            path_list.append(x)
            x = path[x]
        path_list.append(0)
        print "Shortest Path from 0 to {0} is {1}".format(vertex, path_list[::-1])
Beispiel #2
0
        for vertex in graph[node]:
            if vertex in min_heap_dic and graph[node][vertex] < min_heap_dic[vertex]:
                min_heap_dic[vertex] = graph[node][vertex]
                edge_dic[vertex] = "{0}{1}".format(node, vertex)

    return result


if __name__ == "__main__":
    graph = WeightedGraph(directed=False)
    # graph = {'A': {'A': INF, 'B': 3, 'C': INF, 'D': 1, 'E': INF, 'F': INF},
    #          'B': {'A': 3, 'B': INF, 'C': 1, 'D': 3, 'E': INF, 'F': INF},
    #          'C': {'A': INF, 'B': 1, 'C': INF, 'D': 1, 'E': 5, 'F': 4},
    #          'D': {'A': 1, 'B': 3, 'C': 1, 'D': INF, 'E': 6, 'F': INF},
    #          'E': {'A': INF, 'B': INF, 'C': 5, 'D': 6, 'E': INF, 'F': INF},
    #          'F': {'A': INF, 'B': INF, 'C': 4, 'D': INF, 'E': 2, 'F': INF}
    #          }
    graph.add_edge('A', 'B', 3)
    graph.add_edge('A', 'D', 1)
    graph.add_edge('B', 'C', 1)
    graph.add_edge('B', 'D', 3)
    graph.add_edge('C', 'D', 1)
    graph.add_edge('C', 'E', 5)
    graph.add_edge('C', 'F', 4)
    graph.add_edge('D', 'E', 6)
    graph.add_edge('F', 'E', 2)
    result = prims_algo(graph.get_graph())
    print result


# ['AD', 'DC', 'CB', 'CF', 'FE']