Esempio n. 1
0
    def test_components(self):
        g = SimpleGraph()
        g.from_graph_sequence([4, 3, 3, 2, 2, 1, 1])
        comps = g.components()

        assert comps[1] == comps[2] == comps[3] == comps[4] == comps[5]
        assert comps[6] == comps[7]
        assert comps[1] != comps[6]
Esempio n. 2
0
def get_minimum_spanning_tree_kruskal(g: SimpleGraph) -> SimpleGraph:
    """
    Przyjmuje graf
    Zwraca jego minimalne drzewo rozpinające
    Korzysta z algorytmu kruskala
    """
    # minimum spanning tree
    mst = SimpleGraph(len(g))
    Q = []
    for edge in g.edges:
        Q.append(edge)

    while Q and not mst.is_connected_graph():
        Q.sort(key=lambda e: e.weight)
        current_edge = Q.pop(0)
        comps = mst.components()
        if comps[current_edge.begin] != comps[current_edge.end]:
            mst.edges.add(current_edge)
    return mst