1

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

Without external libraries if possible. Thanks!

Milan Babuškov
  • 59,775
  • 49
  • 126
  • 179
Cristo
  • 21
  • 5
  • 1
    Homework? If so, use the homework tag. – Mark Byers Jan 01 '10 at 19:42
  • 1
    That would be the cartesian product of the set of all subsets of vertices and the set of all subsets of edges, right? – pau.estalella Jan 01 '10 at 19:45
  • Crisco: you've just asked a questions about a clique algorithm. I seriously suggest you try to do your homework yourself, we wont be there during your end of term finals – Alon Jan 01 '10 at 19:46
  • If it was homework, believe me I wouldn't be here... Thanks Pau! – Cristo Jan 01 '10 at 19:49
  • 2
    I don't think Pau's answer is totally correct, as it suggests that the number of edges and number of vertices are independent of each other. Removing a vertex means that certain edges will no longer exist, so a simple Cartesian product of subsets won't do. – avpx Jan 01 '10 at 19:56
  • Obviously the resulting graphs should have the edges connecting vertices not in the graph removed. – pau.estalella Jan 01 '10 at 19:58
  • Can you give me some pseudocode example? – Cristo Jan 01 '10 at 20:10

3 Answers3

4

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

pau.estalella
  • 2,197
  • 1
  • 15
  • 20
1

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.

avpx
  • 1,892
  • 15
  • 13
0

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