def opgD(f, s, g, u, d):
    g = Graph()

    #opretter alle hjørner
    for i in range(1, f):
        g.insert_vertex(i)

    #indsætter kanter
    myVertices = list(g.vertices())
    for i in range(1, f, u):
        g.insert_edge(i, myVertices)
    def DFS(self, g: Graph):

        for u in g.vertices():
            self._unexploredV.append(u)

        for u in g.edges():
            self._unexploredE.append(u)

        print("Unexplored edge:")
        for i in self._unexploredE:
            print(i)
        print("Unexplored vertex")
        for i in self._unexploredV:
            print(i)

        for u in self._unexploredV:
            self._DFS(g, u)
def build_graph(routes):

    G = Graph()

    for r in routes:  # r = [a, b, c], a & b: Vertices, c: int edge label.
        a1 = r[0]
        a2 = r[1]
        c = r[2]

        v1 = Vertex(a1)
        v2 = Vertex(a2)

        vertices = list(G.vertices())

        if vertices:
            found_v1 = False
            found_v2 = False
            v1_str = v1.__str__()
            v2_str = v2.__str__()

            for v in vertices:
                v_str = v.__str__()

                if v_str == v1_str:
                    found_v1 = True
                    va = v
                elif v_str == v2_str:
                    found_v2 = True
                    vb = v
                else:
                    continue
            if not found_v1:
                va = G.insert_vertex(a1)
            if not found_v2:
                vb = G.insert_vertex(a2)
        else:
            va = G.insert_vertex(a1)
            vb = G.insert_vertex(a2)

        G.insert_edge(c, va, vb)

    return G
Beispiel #4
0
def kruskal(g: Graph):
    q = HeapPriorityQueue()
    clusters = []
    T = []
    for v in g.vertices():
        clusters.append([v])
        v.pos = len(clusters) - 1

    for e in g.edges():
        q.add(e._element, e)

    while len(T) < g.num_vertices() - 1:
        (w, e) = q.remove_min()
        v1 = g.end_vertices(e)[0]
        v2 = g.end_vertices(e)[1]

        A = clusters[v1.pos]
        B = clusters[v2.pos]

        if A != B:
            _merege(A, B)
            T.append(e)
    return T
Beispiel #5
0
def kruskal(g: Graph):
    q = HeapPriorityQueue()
    T = []  #Holder listen af edges med minimal vægt
    trees = {}
    union = unionFind()

    for v in g.vertices():
        trees[v] = union.makeset(v)

    for e in g.edges():
        q.add(e._element, e)

    while len(T) < g.num_vertices() - 1:
        (u, e) = q.remove_min()
        v1 = g.end_vertices(e)[0]
        v2 = g.end_vertices(e)[1]

        leaderv1 = union.quickFind(trees[v1])
        leaderv2 = union.quickFind(trees[v2])

        if leaderv1._element != leaderv2._element:
            union.quickUnion(leaderv1, leaderv2)
            T.append(e)
    return T