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 GraphUtilities`VertexList 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[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`

  • $\begingroup$ Please see the edit to my answer. $\endgroup$
    – rm -rf
    Commented May 6, 2012 at 0:15
  • $\begingroup$ Thanks. This problem was costing my student and I minutes to hours each time it happened. $\endgroup$ Commented May 6, 2012 at 0:31
  • $\begingroup$ You're welcome :) If it answered your question, you can mark it with the check mark (tick mark) to "accept" the answer. $\endgroup$
    – rm -rf
    Commented May 6, 2012 at 0:33

2 Answers 2


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.

  • $\begingroup$ 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"}} $\endgroup$ Commented May 5, 2012 at 23:25
  • $\begingroup$ 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} $\endgroup$
    – rm -rf
    Commented May 5, 2012 at 23:31
  • $\begingroup$ 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. $\endgroup$ Commented May 5, 2012 at 23:38
  • $\begingroup$ @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. $\endgroup$
    – rm -rf
    Commented May 5, 2012 at 23:55
  • $\begingroup$ 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. $\endgroup$ Commented May 6, 2012 at 0:14

In versions 10.3+ VertexList accepts a set of rules as input.

Documentation >> VertexList:

enter image description here

VertexList[{a -> b, c -> d}]
{a, b, c, d}
VertexList[{a -> b, c <-> d, a -> c}]

 {a, b, c, d}


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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