How can I programmatically access information about a 'Graph` object in Mathematica 8?
Asked Answered
W

3

7

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]]  

myGraph

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]
Winnifredwinning answered 11/6, 2011 at 12:22 Comment(1)
David, I think it is appropriate to post your "Epilog" as an answer, rather than in the body of your question (especially with it at the top).Echopraxia
S
8

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

Salangi answered 11/6, 2011 at 14:36 Comment(4)
Thanks. I am ashamed to say that I had read about the out-of-the-box features before, but entirely forgotten about them and about the new way that Graph works. Your answer in SO link you posted raised some additional issues worth knowing about.Winnifredwinning
+1. The decision to make Graphs atomic and their structure opaque and hard to symbolically manipulate seems very questionable to me. This seems to undermine the language (informal) principles. Some of the things that I like the most about mma is that it is hackable and consistent, and lots of its internals are exposed. And I don't consider this graph decision as a good way to gain efficiency (if it was motivated by efficiency gains). I'd rather like to see extensions for Mathematica compiler to compile a larger subset of symbolic code, plus Graphs implemented using that. My two cents.Essam
@Leonid I agree. Do you think WR may open it up as they advance it some more and gradually replace Combinatorica?Winnifredwinning
@David I don't think the general ideology behind graphs will change any time soon (they will remain atomic etc). May be, they will still become more open, I don't know. Also, I actually did not use the new graph functionality enough to have a very definite opinion on the matter. What I expressed in the previous comments was based on a first few impressions, not on an extensive experiences.Essam
C
5

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: https://reference.wolfram.com/language/guide/GraphConstructionAndRepresentation.html.

In your example you seem to want the list of vertices of the graph in the correct order. The function VertexList seems to do this.
Here is a screenshot from Properties & Relations section in doc:

enter image description here

Cloche answered 11/6, 2011 at 13:34 Comment(1)
Thanks. I had completely overlooked VertexList.Winnifredwinning
W
5

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.

Winnifredwinning answered 12/6, 2011 at 5:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.