我试图找出它有一种在adj矩阵图中找到最大连通分量的方法。比如这个:
0000000110
0001100000
0000000000
0100000010
0100000100
0000000100
0000000000
1000110000
1001000000
0000000000
我已经谷歌解决了这个问题并且正在努力寻找任何东西,我也读过关于图论的维基文章而且没有快乐。我认为他们必须是一个算法来解决这个问题。任何人都可以指出我正确的方向并给我一些关于我自己应该做些什么的指示吗?
答案 0 :(得分:6)
应用连接组件算法。
对于无向图,只需选择一个节点并进行广度优先搜索。如果在第一个BFS之后剩下任何节点,请选择其中一个剩余部分并执行另一个BFS。 (每个BFS可以获得一个连接组件。)
请注意,有向图需要稍微强一些的算法才能找到强连接的组件。脑海中浮现着Kosaraju的算法。
从(1)计算每个连接组件中的节点数。选择最大的一个。
答案 1 :(得分:4)
选择一个起点并开始“行走”到其他节点,直到你筋疲力尽。在找到所有组件之前执行此操作。这将在O(n)
中运行,其中n
是图表的大小。
Python中解决方案的框架:
class Node(object):
def __init__(self, index, neighbors):
self.index = index
# A set of indices (integers, not Nodes)
self.neighbors = set(neighbors)
def add_neighbor(self, neighbor):
self.neighbors.add(neighbor)
class Component(object):
def __init__(self, nodes):
self.nodes = nodes
self.adjacent_nodes = set()
for node in nodes:
self.adjacent_nodes.add(node.index)
self.adjacent_nodes.update(node.neighbors)
@property
def size(self):
return len(self.nodes)
@property
def node_indices(self):
return set(node.index for node in self.nodes)
@property
def is_saturated(self):
return self.node_indices == self.adjacent_nodes
def add_node(self, node):
self.nodes.append(node)
self.adjacent_nodes.update(node.neighbors)
adj_matrix = [[0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
matrix_size = len(adj_matrix)
nodes = {}
for index in range(matrix_size):
neighbors = [neighbor for neighbor, value in enumerate(adj_matrix[index])
if value == 1]
nodes[index] = Node(index, neighbors)
components = []
index, node = nodes.popitem()
component = Component([node])
while nodes:
if not component.is_saturated:
missing_node_index = component.adjacent_nodes.difference(component.node_indices).pop()
missing_node = nodes.pop(missing_node_index)
component.add_node(missing_node)
else:
components.append(component)
index, node = nodes.popitem()
component = Component([node])
# Final component needs to be appended as well
components.append(component)
print max([component.size for component in components])
答案 2 :(得分:1)
#include<iostream>
#include<cstdlib>
#include<list>
using namespace std;
class GraphDfs
{
private:
int v;
list<int> *adj;
int *label;
int DFS(int v,int size_connected)
{
size_connected++;
cout<<(v+1)<<" ";
label[v]=1;
list<int>::iterator i;
for(i=adj[v].begin();i!=adj[v].end();++i)
{
if(label[*i]==0)
{
size_connected=DFS(*i,size_connected);
}
}
return size_connected;
}
public:
GraphDfs(int k)
{
v=k;
adj=new list<int>[v];
label=new int[v];
for(int i=0;i<v;i++)
{
label[i]=0;
}
}
void DFS()
{
int flag=0;
int size_connected=0;
int max=0;
for(int i=0;i<v;i++)
{
size_connected=0;
if(label[i]==0)
{
size_connected=DFS(i,size_connected);
max=size_connected>max?size_connected:max;
cout<<size_connected<<endl;
flag++;
}
}
cout<<endl<<"The number of connected compnenets are "<<flag<<endl;
cout<<endl<<"The size of largest connected component is "<<max<<endl;
//cout<<cycle;
}
void insert()
{
int u=0;int a=0;int flag=1;
while(flag==1)
{ cout<<"Enter the edges in (u,v) form"<<endl;
cin>>u>>a;
adj[a-1].push_back(u-1);
adj[u-1].push_back(a-1);
cout<<"Do you want to enter more??1/0 "<<endl;
cin>>flag;
}
}
};
int main()
{
cout<<"Enter the number of vertices"<<endl;
int v=0;
cin>>v;
GraphDfs g(v);
g.insert();
g.DFS();
system("Pause");
}