给定无向图中所有顶点的连通分量中的最小顶点
给定一个由 2 N个顶点和M个边组成的无向图G(V, E) ,任务是在[1, N]范围内的所有i值的顶点i的连通分量中找到最小顶点。
例子:
Input: N = 5, edges[] = {{1, 2}, {2, 3}, {4, 5}}
Output: 1 1 1 4 4
Explanation: The given graph can be divided into a set of two connected components, i.e, {1, 2, 3} and {4, 5}.
- For i = 1, vertex 1 belongs to the component {1, 2, 3}. Therefore, the minimum vertex in the set is 1.
- For i = 2, vertex 2 belongs to the component {1, 2, 3}. Therefore, the minimum vertex in the set is 1.
- For i = 3, vertex 3 belongs to the component {1, 2, 3}. Therefore, the minimum vertex in the set is 1.
- For i = 4, vertex 4 belongs to the component {4, 5}. Therefore, the minimum vertex in the set is 4.
- For i = 5, vertex 5 belongs to the component {4, 5}. Therefore, the minimum vertex in the set is 4.
Input: N = 6, edges[] = {{1, 3}, {2, 4}}
Output: 1 2 1 2 5 6
方法:可以有效地解决给定的问题 不相交集并集。可以观察到,由一条边连接的顶点可以合并到同一个集合中,并且可以使用无序映射来跟踪每个形成的集合的最小顶点。以下是要遵循的步骤:
- 使用本文讨论的方法实现不相交集合并集的find_set和union_set函数,其中find_set(x)返回包含x的集合编号,而union_set(x, y)将包含x的集合与包含y的集合联合起来。
- 遍历给定的数组edges[]并且对于每个边(u, v) ,将包含u的集合与包含v的集合联合起来。
- 创建一个无序映射minVal ,其中minVal[x]存储包含x作为元素的集合的最小顶点。
- 使用变量i遍历所有顶点,并为每个顶点将minVal[find_set(node i)]的值设置为minVal[find_set(i)]和i的最小值。
- 完成上述步骤后,对于范围[1, N]中的每个顶点i ,打印minVal[find_set(i)]的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
const int maxn = 100;
// Stores the parent and size of the
// set of the ith element
int parent[maxn], Size[maxn];
// Function to initialize the ith set
void make_set(int v)
{
parent[v] = v;
Size[v] = 1;
}
// Function to find set of ith vertex
int find_set(int v)
{
// Base Case
if (v == parent[v]) {
return v;
}
// Recursive call to find set
return parent[v] = find_set(
parent[v]);
}
// Function to unite the set that includes
// a and the set that includes b
void union_sets(int a, int b)
{
a = find_set(a);
b = find_set(b);
// If a and b are not from same set
if (a != b) {
if (Size[a] < Size[b])
swap(a, b);
parent[b] = a;
Size[a] += Size[b];
}
}
// Function to find the smallest vertex in
// the connected component of the ith
// vertex for all i in range [1, N]
void findMinVertex(
int N, vector > edges)
{
// Loop to initialize the ith set
for (int i = 1; i <= N; i++) {
make_set(i);
}
// Loop to unite all vertices connected
// by edges into the same set
for (int i = 0; i < edges.size(); i++) {
union_sets(edges[i].first,
edges[i].second);
}
// Stores the minimum vertex value
// for ith set
unordered_map minVal;
// Loop to iterate over all vertices
for (int i = 1; i <= N; i++) {
// If current vertex does not exist
// in minVal initialize it with i
if (minVal[find_set(i)] == 0) {
minVal[find_set(i)] = i;
}
// Update the minimum value of
// the set having the ith vertex
else {
minVal[find_set(i)]
= min(minVal[find_set(i)], i);
}
}
// Loop to print required answer
for (int i = 1; i <= N; i++) {
cout << minVal[find_set(i)] << " ";
}
}
// Driver Code
int main()
{
int N = 6;
vector > edges
= { { 1, 3 }, { 2, 4 } };
findMinVertex(N, edges);
return 0;
}
Python3
# Python 3 program for the above approach
maxn = 100
# Stores the parent and size of the
# set of the ith element
parent = [0]*maxn
Size = [0]*maxn
# Function to initialize the ith set
def make_set(v):
parent[v] = v
Size[v] = 1
# Function to find set of ith vertex
def find_set(v):
# Base Case
if (v == parent[v]):
return v
# Recursive call to find set
parent[v] = find_set(
parent[v])
return parent[v]
# Function to unite the set that includes
# a and the set that includes b
def union_sets(a, b):
a = find_set(a)
b = find_set(b)
# If a and b are not from same set
if (a != b):
if (Size[a] < Size[b]):
a, b = b, a
parent[b] = a
Size[a] += Size[b]
# Function to find the smallest vertex in
# the connected component of the ith
# vertex for all i in range [1, N]
def findMinVertex(
N, edges):
# Loop to initialize the ith set
for i in range(1, N + 1):
make_set(i)
# Loop to unite all vertices connected
# by edges into the same set
for i in range(len(edges)):
union_sets(edges[i][0],
edges[i][1])
# Stores the minimum vertex value
# for ith set
minVal = {}
# Loop to iterate over all vertices
for i in range(1, N + 1):
# If current vertex does not exist
# in minVal initialize it with i
if (find_set(i) not in minVal):
minVal[find_set(i)] = i
# Update the minimum value of
# the set having the ith vertex
else:
minVal[find_set(i)] = min(minVal[find_set(i)], i)
# Loop to print required answer
for i in range(1, N + 1):
print(minVal[find_set(i)], end=" ")
# Driver Code
if __name__ == "__main__":
N = 6
edges = [[1, 3], [2, 4]]
findMinVertex(N, edges)
# This code is contributed by ukasp.
Javascript
输出:
1 2 1 2 5 6
时间复杂度: O(N)
辅助空间: O(N)