views:

140

answers:

2

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

Without external libraries if possible. Thanks!

+1  A: 

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
+3  A: 

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
This is ok, but really you don't need to build `edges`. If, for a given combination (V, E), E mentions any vertices not in V, you can just skip it.
Jason Orendorff
Whoa, that's right. The fixed edges will be generated in another iteration. Fixing it...
pau.estalella