Exemplo n.º 1
0
    def is_connected(self, g: Graph):
        counter = 0
        for i in self.iterator_dfs(g, 15):
            counter += 1

        if counter == g.num_vertices():
            return True
        return False
Exemplo n.º 2
0
def max_value(graf : Graph, vertex):
    global mx

    if(mySet.__len__() == graf.num_vertices()):
        return mx
    if(vertex > mx):
        mx = vertex
    if not mySet.__contains__(vertex):
        mySet.add(vertex)
        LastVisitet = vertex
        for i in graf.adjacent_vertices(vertex):
            if not mySet.__contains__(i):
                return max_value(graf, i)
    
    return max_value(graf, LastVisitet)
Exemplo n.º 3
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
Exemplo n.º 4
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