Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

In MMA 8, it seems that sometimes VertexList will work with rule-based graphs without needing GraphUtilities and at other times it won't. Once it starts working, it keeps working until I restart MMA. In addition, while it is working, VertexList is not colored Red to indicate shadowing.

I know that I can use GraphUtilitiesVertexList` as shown below, but that is a bit much to type and I really want to understand why it is sometimes unnecessary. I also have dozens of notebooks with VertexList and don't really want to need to change all of them.

Here is what happens:

In[1]:= VertexList[{a -> b}]
During evaluation of In[1]:= VertexList::graph: A graph object is expected at position 1 in VertexList[{a->b}]. >>
    Out[1]= VertexList[{a -> b}]

    In[2]:= GraphUtilities`VertexList[{a -> b}]
    Out[2]= GraphUtilities`VertexList[{a -> b}]

    In[3]:= Needs["GraphUtilities`"]
    In[4]:= VertexList[{a -> b}]
    Out[4]= {a, b}

    In[5]:= $ContextPath = Rest[$ContextPath]
    Out[5]= {"PacletManager`", "WebServices`", "System`", "Global`"}

    In[10]:= VertexList[{a -> b}]
    During evaluation of In[10]:= VertexList::graph: A graph object is expected at position 1 in VertexList[{a->b}]. >>
    Out[10]= VertexList[{a -> b}]

    In[7]:= GraphUtilities`VertexList[{a -> b}]
    Out[7]= {a, b}

Is there any consistent way to force MMA to get VertexList to work on older style graphs without needing to use the full name?

One more detail. When this works, I get exactly this behavior:

?VertexList

VertexList[g] gives the list of vertices for the graph g.
VertexList[g,patt] gives a list of vertices that match the pattern patt. >>

which makes it look like the symbol is coming from System`

share|improve this question
    
Please see the edit to my answer. –  rm -rf May 6 '12 at 0:15
    
Thanks. This problem was costing my student and I minutes to hours each time it happened. –  Todd Johnson May 6 '12 at 0:31
    
You're welcome :) If it answered your question, you can mark it with the check mark (tick mark) to "accept" the answer. –  rm -rf May 6 '12 at 0:33
add comment

1 Answer

up vote 4 down vote accepted

The built-in function VertexList (or System`VertexList) requires that you input a graph object (i.e., something with the head Graph), whereas {a -> b} has the head List. So if you have a list of rules denoting the edges, then just wrap it in Graph for it to work. For example:

VertexList[Graph[{a -> b}]]    
(* {a, b} *)

The weird behaviour you see only when you load the GraphUtilities` package is because the package adds a definition to System`VertexList. Note that there is no such function as GraphUtilities`VertexList as you seem to assume in the question.

If you browse $InstallationDirectory/AddOns/Packages/GraphUtilities/GraphUtilities.m you'll find the following in the code (line numbers in comments):

(* 449 *) u = Unprotect[{AdjacencyMatrix, EdgeList, VertexList}];   
(* 464 *) VertexList[x_?InternalGraphQ, r___] := With[{res = Network`GraphPlot`VertexList[x, r]},
(* 465 *)     res/;ListQ[res]];    
(* 483 *) Protect @@ u;    
(* 487 *) InternalGraphQ := Network`GraphPlot`GraphQ;

Essentially, the modification made is to simply call Network`GraphPlot`VertexList if your input is a list.

So to get a consistent behaviour for VertexList, you simply need to add the definition above and you needn't load GraphUtilities` just to get this behaviour.

share|improve this answer
    
This works for graphs without edge labels, but I've got a lot of code that was working without this approach before I restarted Mathematica and most of my graphs have edge labels, as in:{{a-> b, "on"}} –  Todd Johnson May 5 '12 at 23:25
    
Well, then post a code sample that's representative of your actual graph. This is not what you showed in the question. You could try doing something like list /. {x_, y_, _} :> {x, y} –  rm -rf May 5 '12 at 23:31
    
Sorry. The graphs I'm using have labels and are often multigraphs. And I know that I can do a number of things to get a VertexList from these kinds of graphs, but my question is really about why VertexList seems to work directly with these graphs (and yet is not colored Red) and why it sometimes doesn't. Perhaps I'm doing something strange with loading GraphUtilities or with $ContextPath. At this point, I cannot get back to where this is working again, but I have seen it happen several times in the past. –  Todd Johnson May 5 '12 at 23:38
    
@ToddJohnson I poked inside the GraphUtilities (GU) package and it looks like it adds a definition to VertexList that's the same as Network`GraphPlot`VertexList. You can try this in a fresh kernel: Network`GraphPlot`VertexList@{2 -> 3} and you'll get {2, 3}. This is the function that's being used when you load GU and pass a list to it. You don't see a red shadowing warning because Network` is not on $ContextPath. I'll add this to my answer after poking with it a little more. –  rm -rf May 5 '12 at 23:55
    
I suspect that something, somewhere is adding a global definition for VertexList that is then getting used for lists. I'm just not sure what it is, because every time I've had this problem I start to modify code, then suddenly find that the VertexList starts working with the older graphs again. –  Todd Johnson May 6 '12 at 0:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.