Is there a neat way to generate the odd set constraints matrix?
Although documentation does not show this particular usage pattern, odd-length subsets can be obtained using the second argument of Subsets
:
oddsubsets = Subsets[VertexList[#], {3, ∞, 2}] &;
(* excluded subsets of length 1; change 3 to 1 if you need all odd-sized subsets *)
eVsubsetsM = Function[{g}, Outer[Boole[MemberQ[EdgeList[Subgraph[g, #1]], #2]]&,
oddsubsets@g, EdgeList@g, 1]];
Usage example:
g = RandomGraph[{7, 10}, ImageSize -> 400];
ap = eVsubsetsM[g] // ArrayPlot[#, ImageSize -> 50] &;
Row[{g, ap}]
eVsubsetsM[g]
Here is a version that is slightly faster then all the other answers so far:
g = RandomGraph[{9, 12}]
nZ = Flatten[
Position[EdgeList[g], #] & /@ EdgeList[Subgraph[g, #]]] & /@
Select[Subsets[VertexList[g]], Length@# > 1 && OddQ@Length@# &];
nZ = Flatten@Table[{n, #} -> 1 & /@ nZ[[n]], {n, 1, Length@nZ}];
MatrixForm@SparseArray[nZ]
AbsoluteTiming
:
(*paw:*) 0.028285
(*Han Xiao:*) 0.034661
(*ubpdqn:*) 0.083890
(*kguler:*) 0.164534
This is ugly cf @kguler and other answers
fun[gr_] :=
Module[{el, vl, sub,
f = Function[{x, y},
1 - Unitize[Norm[# - Sort@x] & /@ (Sort /@ List @@@ y)]], subg,
res},
el = EdgeList[gr];
vl = VertexList[gr];
sub = Subsets[vl, {1,Infinity,2}];
subg = List @@@ EdgeList[Subgraph[gr, #]] & /@ sub;
res = If[# == {}, ConstantArray[0, {Length@el}],
Total@Map[Function[x, f[x, el]], #]] & /@ subg;
TableForm[res, TableHeadings -> {sub, el}]
]
e.g g=
fun[g]
yields: