コード例 #1
0
    def start(self):
        prim_graph = Graph("n")
        nodes_visited = list()
        nodes_to_visit = self.graph.nodes
        prim_graph.addNodes(nodes_to_visit)
        print(nodes_to_visit)
        nodes_to_visit["A"].data["key"] = 0
        while nodes_to_visit:
            min = self.return_min_value(nodes_to_visit)
            nodes_to_visit.pop(min.name)
            print(nodes_to_visit)
            if min.data["parent"] is not None:
                # self.lista[min.name] = min.data["parent"]

                prim_graph.addEdge(min.name, min.data["parent"].name,
                                   min.data["key"])

            nodes_adj = self.return_all_vertices(min)

            for node in nodes_adj:
                if node[1].weight < node[0].data["key"]:
                    node[0].data["parent"] = min
                    node[0].data["key"] = node[1].weight

            # break

        self.graph = prim_graph
コード例 #2
0
def graphThree():
    grafo = Graph("n")
    grafo.addNodes(["A", "B", "C", "D", "E", "F", "G"])
    grafo.addEdge("A", "B", 2)
    grafo.addEdge("A", "D", 3)
    grafo.addEdge("A", "C", 3)
    grafo.addEdge("B", "C", 4)
    grafo.addEdge("B", "E", 3)
    grafo.addEdge("C", "D", 5)
    grafo.addEdge("C", "F", 6)
    grafo.addEdge("C", "E", 1)
    grafo.addEdge("D", "F", 7)
    grafo.addEdge("E", "F", 8)
    grafo.addEdge("F", "G", 9)

    return grafo