如何获取图中的所有子图?

发布于 2024-08-17 05:46:23 字数 58 浏览 5 评论 0原文

如何用伪代码从图中获取固定大小的所有子图? (暴力)

如果可能的话,无需外部库。谢谢!

How to obtain all the subgraphs of a fixed size from a graph, in pseudocode? (brute force)

Without external libraries if possible. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不再见 2024-08-24 05:46:23

或多或少是这样的:

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”行的缩进

编辑:删除了重复的图表

More or less that would be something along these lines:

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

EDIT: Fixed indentation of 'edges.insert' line

EDIT: Removed duplicated graphs

左秋 2024-08-24 05:46:23

由于图只有边和顶点,因此找到顶点的所有可能的子集并在它们上构造边的所有可能的子集。

Since a graph is only edges and vertices, find all possible subsets of the vertices and construct all possible subsets of the edges on them.

初心未许 2024-08-24 05:46:23

如果您使用增强子图,我有一个以下解决方案来迭代所有子图并准备其向量。

// 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);
}
}

If you are using in terms of boost subgraph i have a follwing solution to iterate all subgraphs and prepare its vector.

// 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);
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文