コード例 #1
0
def kruskal():
    es.sort()
    uf = UnionFind(V)
    res = 0
    for e in es:
        if not uf.same(e[1], e[2]):
            uf.union(e[1], e[2])
            res += e[0]
    return res
コード例 #2
0
def kruskal():
    c_f_t.sort()
    uf = UnionFind(n)
    res = 0
    for e in c_f_t:
        if not uf.same(e[1], e[2]):
            uf.union(e[1], e[2])
            res += e[0]
    return res
コード例 #3
0
    def test_union_find(self):
        """Test for union & find

        Args:
            self: TestUnionFind

        Returns:
            None

        Raises:
            None
        """
        # Given
        n = 10
        data = [i for i in range(n)]
        adjacency_list = [(0, 1), (1, 2), (0, 9), (5, 6), (6, 4), (5, 9)]
        union_find = UnionFind(data)

        # When
        for i, j in adjacency_list:
            union_find.union(i, j)

        # Then
        self.assertEqual(union_find.find(0), 9)
        self.assertEqual(union_find.find(1), 9)
        self.assertEqual(union_find.find(2), 9)
        self.assertEqual(union_find.find(3), 3)
        self.assertEqual(union_find.find(4), 9)
        self.assertEqual(union_find.find(5), 9)
        self.assertEqual(union_find.find(6), 9)
        self.assertEqual(union_find.find(7), 7)
        self.assertEqual(union_find.find(8), 8)
        self.assertEqual(union_find.find(9), 9)
コード例 #4
0
    def solve(self):
        """Solve the problem

        Note: O(E logE) solution sorts the edges in O(log E). Then iterates the edges.

        Args:

        Returns:
            list(list)

        Raises:
            None
        """
        print("Solving {} problem ...".format(self.PROBLEM_NAME))

        number_vertices = self.input_graph.get_vertices_count()

        result = []

        sorted_edges_index = 0
        edge_index = 0

        # sort the items based on the weight
        adjacency_list = self.input_graph.get_adjacency_list()
        adjacency_list = sorted(adjacency_list, key=lambda item: item[0])

        parents = [i for i in range(number_vertices)]
        union_find = UnionFind(parents)

        while edge_index < number_vertices - 1:
            weight, vertex1, vertex2 = adjacency_list[sorted_edges_index]

            vertex1_parent_index = union_find.find(vertex1)
            vertex2_parent_index = union_find.find(vertex2)

            if vertex1_parent_index != vertex2_parent_index:
                edge_index = edge_index + 1
                result.append(adjacency_list[sorted_edges_index])
                union_find.union(vertex1, vertex2)

            sorted_edges_index = sorted_edges_index + 1

        return result
    def solve(self):
        """Solve the problem

        Note:

        Args:

        Returns:
            Boolean

        Raises:
            None
        """
        print("Solving {} problem ...".format(self.PROBLEM_NAME))

        vertices = self.input_graph.get_vertices()

        parent_list = [i for i in vertices]

        union_find = UnionFind(parent_list)

        for vertex in vertices:
            for neighbor in self.input_graph.get_neighbors(vertex):
                vertex_parent = union_find.find(vertex)
                neighbor_parent = union_find.find(neighbor)

                # They both have the same parents, should be a cycle
                if vertex_parent == neighbor_parent:
                    return True
                else:
                    union_find.union(vertex, neighbor)

        return False
コード例 #6
0
    def solve(self):
        """Solve the problem

        Note:

        Args:

        Returns:
            Boolean

        Raises:
            None
        """
        print("Solving {} problem ...".format(self.PROBLEM_NAME))

        parent_list = [i for i in range(self.number_vertices)]

        union_find = UnionFind(parent_list)

        for i, j in self.adjacency_list:
            union_find.union(i, j)

        return len(set(parent_list))