コード例 #1
0
ファイル: MSTPrim.py プロジェクト: snowtheghost/Algorithms
    for vertex in vertices:
        q.insert(vertex, priority[vertex])

    while not q.is_empty():
        node = q.extract_min()
        u = node.value
        u_weight = node.priority
        mst.add_vertex(u)
        if parent[u] is not None:
            mst.add_weighted_edge(u, parent[u], u_weight)

        for v in graph.adjacency_list[u]:
            if v in q and graph.weights[(u, v)] < priority[v]:
                priority[v] = graph.weights[(
                    u, v)]  # update to the correct weight
                q.update_priority(v,
                                  priority[v])  # update to the correct weight
                parent[v] = u
    return mst


if __name__ == "__main__":
    graph = WeightedGraph()
    graph.add_vertices(["A", "B", "C", "D", "E", "F"])
    graph.add_weighted_edges([("A", "B", 4), ("A", "D", 3), ("A", "E", 3),
                              ("B", "C", 10), ("B", "D", 1), ("B", "E", 2),
                              ("C", "E", 8), ("C", "F", 8), ("D", "E", 1),
                              ("E", "F", 9)])
    print(graph)
    print(MSTPrim(graph, "c"))