Message Boards Message Boards

ConnectedComponents requiring graph image as input

Posted 10 years ago
I'm trying to find the connected components of a very large directed graph with Mathematica 9 and have been having trouble with the ConnectedComponents function. I have been working with smaller components of the graph, but the ConnectedComponents function has not been recognizing the smaller graph images that the GraphPlot command produces. Here is an example of the code I've been using:

in[1]:=    g = GraphPlot[{file1 -> file6, file1 -> file2, file1 -> file3, file2 -> file7, file3 -> file9}, DirectedEdges -> True]

out[1]:= (A small directed graph, the GraphPlot command works fine)

in[2]:= ConnectedComponents

ConnectedComponents::graph: A graph object is expected at position 1 in ConnectedComponents

out[2]:= (same as in[2])

If the first line properly outputs a graph, why doesn't the next like properly read it? I'll attach a screenshot of my code to show what's going wrong.
Attachments:
POSTED BY: Pat Dillon
It's slightly subtle (and confusing, I admit).

GraphPlot creates a Graphics version of the Graph.  Hence the Head of your GraphPlot is Graphics and the error message from ConnectedComponents is complaining about this.  The argument of ConnectedComponents must havve the Head Graph.  To get this you need to create your graph using the Graph function as in (I've simplified your first argument just for my ease of reading):
g = Graph[{1 -> 6, 1 -> 2, 1 -> 3, 2 -> 7, 3 -> 9},DirectedEdges -> True]

Then ConnectedComponents works:
In[6]:= ConnectedComponents[g]
Out[6]= {{1}, {6}, {2}, {3}, {7}, {9}}


In[4]:= Head[g]
Out[4]= Graph
POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract