I'm trying to access information within a Graph
object in Mathematica 8. For some reason, the Part
command does not seem to work.
myGraph
is the object I want to gain access to.
The first line below displays myGraph. The others serve to inspect it.
myGraph
myGraph // FullForm
myGraph // InputForm
myGraph // OutputForm
myGraph[[1]]
myGraph[[2]]
Why doesn't myGraph[[1]]
return List[1,3,4,2,5]
?
[I checked to level 2 just in case Graph
were wrapped by some invisible wrapper. Level[myGraph,1]
, simply returns {}
. And FullForm[myGraph][[1]]
returns a picture of the graph itself.
I must be overlooking something obvious.
Edit
Here's the code I used to produce the graph. Most of it is irrelevant to the issue at hand.
But at least you'll be working with the same code I am using.
ClearAll[edges, compatibleQ, adjacentCourses, g];
edges[w_, b_] :=
Most /@ Accumulate /@
Flatten[Permutations[#] & /@ IntegerPartitions[w, All, b], 1]
compatibleQ[j_, k_, edg_] :=
If[Intersection[edg[[j]], edg[[k]]] == {}, {j, k}, False]
adjacentCourses[edg_] :=
Module[{len = Length[edg]},
Cases[Flatten[Table[compatibleQ[j, k, edg], {j, len}, {k, j, len}],
1], {v_, w_} :> v \[UndirectedEdge] w]]
myGraph = Graph[adjacentCourses[edges[9, {2, 3}]], VertexLabels -> "Name",
ImagePadding -> 10]
Despite appearances, the graph objects introduced in Mathematica 8 are not "normal" symbolic expressions. The following SO question discusses this and other such problems in detail, including ways to extract parts of the graph definition:
new Graph in Mathematica 8.0
This might be useful to you, to answer the question
"How can I programmatically access information about a 'Graph` object in Mathematica 8?"
There seems to be a number of new functions for getting bits of information about graphs as listed here
http://reference.wolfram.com/mathematica/guide/GraphRepresentation.html.
In your example you seem to want the list of vertices of the graph in the correct order. The function VertixList seems to do this.
Here is a screenshot from Properties & Relations section in doc:
Turns out that there were some straightforward answers to my question.
The documentation for Graph
contains several ways of retrieving information from a Graph
object. (Shame on me for not checking.) The most useful commands, in my view, are:
VertexList[]
VertexCount[]
EdgeList[]
EdgeCount[]
EdgeRules[]
VertexIndex[]
EdgeIndex[]
PropertyValue[]
We need to get information OUT of the graph object typically after we've manipulated it.
I can easily find what information went into a Graph I build but if a derivative Graph is output, e.g. from NeighborhoodGraph, I won't know its properties without probing.
Thanks to @dbJohn for the link to the Wolfram documentation.
Special thanks to @WReach for the link to his comments in a prior SO discussion about the Graph
object.