计算无向未加权图的每个连接部分中的节点数
Count number of nodes in each connected part of an undirected unweighted graph
我是 C++ STL 的新手,最近开始学习图论。
参考https://www.geeksforgeeks.org/connected-components-in-an-undirected-graph/后,我可以使用DFS计算一个无向、未加权图中的连通分量数为:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int connected=0, temp1, temp2,n, p;
void DFS(int start, vector<int> v[],vector<int> &visited) {
visited[start] = 1;
for(int i= 0; i<v[start].size(); ++i) {
if(visited[v[start][i]] == 0)
DFS(v[start][i], v, visited);
}
}
int main() {
cin>>n>>p; // number of vertices and edges
vector<int> v[n+1], visited(n+1,0);
for(int i=0; i<p; ++i) {
cin>>temp1>>temp2;
v[temp1].push_back(temp2);
v[temp2].push_back(temp1);
}
connected = 0;
for(int i=1;i<=n;++i) {
if(visited[i] == 0 ) {
connected++;
DFS(i,v,visited);
}
}
cout<<connected<<endl;
return 0;
}
但是我们如何统计每个组件中的节点总数呢?
For example: In this graph, see image there are 3 connected
components, with no. of nodes being 3, 2 , and 1 respectively.
您可以在每次从 main()
调用 DFS
时维护一个虚拟变量 count
void DFS(int start, vector<int> v[],vector<int> &visited, int &count)
{
visited[start] = 1;
count++;
for(int i= 0; i<v[start].size(); ++i)
{
if(visited[v[start][i]] == 0)
DFS(v[start][i], v, visited);
}
}
和
for(int i=1;i<=n;++i)
{
if(visited[i] == 0 )
{
connected++;
int count=0;
DFS(i,v,visited,count);
cout<<"This component has "<<count<<" nodes"<<"\n";
}
}
或者您可以在每次从 main()
调用 DFS()
后参考 visited
向量的变化(其中新 1 的数量)
您可以维护一个全局变量no_of_nodes
,它会在每个组件的 dfs 开始时设置为零,并在您访问该组件中的每个节点时递增 1。
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int connected=0, temp1, temp2,n, p;
int no_of_nodes=0;
void DFS(int start, vector<int> v[],vector<int> &visited) {
visited[start] = 1;
no_of_nodes++;
for(int i= 0; i<v[start].size(); ++i) {
if(visited[v[start][i]] == 0)
DFS(v[start][i], v, visited);
}
}
int main() {
cin>>n>>p; // number of vertices and edges
vector<int> v[n+1], visited(n+1,0);
for(int i=0; i<p; ++i) {
cin>>temp1>>temp2;
v[temp1].push_back(temp2);
v[temp2].push_back(temp1);
}
connected = 0;
vector<int>nodes;
for(int i=1;i<=n;++i) {
if(visited[i] == 0 ) {
connected++;
no_of_nodes=0;
DFS(i,v,visited);
nodes.push_back(no_of_nodes);
}
}
cout<<connected<<endl;
return 0;
}
我是 C++ STL 的新手,最近开始学习图论。 参考https://www.geeksforgeeks.org/connected-components-in-an-undirected-graph/后,我可以使用DFS计算一个无向、未加权图中的连通分量数为:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int connected=0, temp1, temp2,n, p;
void DFS(int start, vector<int> v[],vector<int> &visited) {
visited[start] = 1;
for(int i= 0; i<v[start].size(); ++i) {
if(visited[v[start][i]] == 0)
DFS(v[start][i], v, visited);
}
}
int main() {
cin>>n>>p; // number of vertices and edges
vector<int> v[n+1], visited(n+1,0);
for(int i=0; i<p; ++i) {
cin>>temp1>>temp2;
v[temp1].push_back(temp2);
v[temp2].push_back(temp1);
}
connected = 0;
for(int i=1;i<=n;++i) {
if(visited[i] == 0 ) {
connected++;
DFS(i,v,visited);
}
}
cout<<connected<<endl;
return 0;
}
但是我们如何统计每个组件中的节点总数呢?
For example: In this graph, see image there are 3 connected components, with no. of nodes being 3, 2 , and 1 respectively.
您可以在每次从 main()
DFS
时维护一个虚拟变量 count
void DFS(int start, vector<int> v[],vector<int> &visited, int &count)
{
visited[start] = 1;
count++;
for(int i= 0; i<v[start].size(); ++i)
{
if(visited[v[start][i]] == 0)
DFS(v[start][i], v, visited);
}
}
和
for(int i=1;i<=n;++i)
{
if(visited[i] == 0 )
{
connected++;
int count=0;
DFS(i,v,visited,count);
cout<<"This component has "<<count<<" nodes"<<"\n";
}
}
或者您可以在每次从 main()
DFS()
后参考 visited
向量的变化(其中新 1 的数量)
您可以维护一个全局变量no_of_nodes
,它会在每个组件的 dfs 开始时设置为零,并在您访问该组件中的每个节点时递增 1。
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int connected=0, temp1, temp2,n, p;
int no_of_nodes=0;
void DFS(int start, vector<int> v[],vector<int> &visited) {
visited[start] = 1;
no_of_nodes++;
for(int i= 0; i<v[start].size(); ++i) {
if(visited[v[start][i]] == 0)
DFS(v[start][i], v, visited);
}
}
int main() {
cin>>n>>p; // number of vertices and edges
vector<int> v[n+1], visited(n+1,0);
for(int i=0; i<p; ++i) {
cin>>temp1>>temp2;
v[temp1].push_back(temp2);
v[temp2].push_back(temp1);
}
connected = 0;
vector<int>nodes;
for(int i=1;i<=n;++i) {
if(visited[i] == 0 ) {
connected++;
no_of_nodes=0;
DFS(i,v,visited);
nodes.push_back(no_of_nodes);
}
}
cout<<connected<<endl;
return 0;
}