Search code examples
wolfram-mathematicagraph-theory

Trying to use Edge Count Mathematica command on a list of subgraphs but Mathematica doesn't recognise the objects as graphs


I am trying to count the edges of subgraphs generated by a random graph in Mathematica. I have written code to generate a random graph and to extract and save the different subgraphs of a specific size in a list. I am now attempting to use EdgeCount on the elements of the list but get an error EdgeCount::A graph object is expected at position 1 in EdgeCount.

Code to generate random graph

G[n_, p_] := 
Module[{A, M}, A = Table[If[i < j, If[RandomReal[] < p, 1, 0], 0], {i, 1, n}, {j, 1, n}];
M = A + Transpose[A];Return[AdjacencyGraph[M]]; ]

Code to generate list of subgraphs

Subcount[n_, p_, d_] := Module[{i, ex, comb, sub1}, 
ex = G[n, p]; comb = Subsets[Range[n], {d}];
Table[{Subgraph[ex, Part[comb, i]]}, {i, Length[comb]}]]

Code to count edges

mylist2 = Subcount2[5, 0.4, 4]
ab = mylist2[[3]]
EdgeCount[ab]

Follow up, after this I want to search for complete bipartite graphs by colouring the vertices, can this be done the way the data is currently stored or is there a more efficient method?


Solution

  • There is a minor detail of defining Subcount but then using Subcount2

    Subcount returns a Table of {graph} and thus you are trying to do Edgecount[{graph}] instead of Edgecount[graph]

    If you change that to Edgecount[First[ab]] so that you extract the graph out of the {graph} or do anything equivalent to this, then it appears to work just fine