如何从伪图中获取图中固定大小的所有子图? (蛮力)
如果可能,没有外部库。谢谢!
答案 0 :(得分:4)
或多或少会出现这些问题:
GenerateSubgraphs(Graph G):
powerV = powerset(G.V)
powerE = powerset(G.E)
subgraphs = {}
foreach V in powerV:
foreach E in powerE:
accept = true
foreach edge in E:
if edge.u not in V or edge.v not in V:
accept = false
if accept:
subgraphs.insert((V, E))
return subgraphs
编辑:修复'edges.insert'行
的缩进编辑:删除了重复的图表
答案 1 :(得分:1)
由于图形只是边和顶点,因此找到顶点的所有可能子集并构造它们边缘的所有可能子集。
答案 2 :(得分:0)
如果您正在使用boost子图,我有一个以下解决方案来迭代所有子图并准备其向量。
// declare the list types
vector<SubGraph*> m_vecSubgraphContainer;
vector<SubGraph*> m_vecBFSOrderedSubgraphs;
// construct container of subgraph lists in the vector
m_vecSubgraphContainer.push_back(&gInputGraph);
m_vecBFSOrderedSubgraphs.push_back(&gInputGraph);
iterateChildrenGraphs(m_vecBFSOrderedSubgraphs);
// iterating the subgraphs
for(vector<SubGraph*>::iterator itrSubgraph = m_vecSubgraphContainer.begin();
itrSubgraph != m_vecSubgraphContainer.end();
++itrSubgraph)
{
// for empty graph add dummy node to that graph
int iNumVertices = num_vertices(**itrSubgraph);
if(iNumVertices == 0)
{
VertexDescriptor vVertex = m_boostGraphWrapper.addVertex(**itrSubgraph, Enum::InvisibleNode);
// This dummy node will set the size of cluster
// set height = 80 nd width = 80;
int iDummyNodeHeight = 80;
int iDummyNodeWidth = 80;
m_boostGraphWrapper.setVertexHeight(vVertex,**itrSubgraph, iDummyNodeHeight);
m_boostGraphWrapper.setVertexWidth(vVertex, **itrSubgraph, iDummyNodeWidth);
}
}
void CircularLayoutGenerator::iterateChildrenGraphs(vector<SubGraph *> &subgraphQueue)
{
/*
we have used queue because it will contains reference of subgraphs.
adding all the subgraphs in queue to iterate one by one in circular way.
*/
// define local queue which will contains the childrens of main graph
vector<SubGraph*> SubgraphSequence;
try
{
// To iterate input queue which will contain graph reference
for( vector<SubGraph*>::iterator itrSubgraphQueue = subgraphQueue.begin();
itrSubgraphQueue != subgraphQueue.end();
itrSubgraphQueue++)
{
// Finding the children upto deep level
SubGraph::children_iterator itrSubgraph, itrSubgraphEnd;
for (boost::tie(itrSubgraph, itrSubgraphEnd) = (**itrSubgraphQueue).children();
itrSubgraph != itrSubgraphEnd;
++itrSubgraph)
{
// Add children in the global queue container
m_vecSubgraphContainer.push_back(&(*itrSubgraph));
// Add children in the local queue conatainer
SubgraphSequence.push_back(&(*itrSubgraph));
}
}
}
// To iterarte the local queue again if ant children is present
if(!SubgraphSequence.empty())
{
// Recursive call to iterate children
iterateChildrenGraphs(SubgraphSequence);
}
}