Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a simple graph like

g=Graph[{b -> c, a -> b}]

I would like to have f[g] resulting in {a,b,c} but for instance VertexList[g] results {b,c,a} which's not wanted. Is there any simple way to do this?

share|improve this question
perhaps you can be more specific on f[]? Or is it a Sort of the vertexes? – FredrikD Dec 2 '12 at 4:47
Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the FAQs! 3) When you see good Q&A, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. ALSO, remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign` – Vitaliy Kaurov Dec 2 '12 at 5:47
up vote 1 down vote accepted

If you have disordered graph

g = Graph[{b -> c, a -> b}]; VertexList[g]

{b, c, a}

Use sort to find the order

f[g_] := Graph[Sort[VertexList[g]], EdgeList[g]];

{a, b, c}

If your graph is directed and acyclic, then you have a built in function to sort based on topological order:


{a, b, c}

share|improve this answer
Sorry, forgot to say the input data only contains edges. And the vertex doesn't limited to letters or numbers, the goal is to fine vertex ordering based on edges. – jamtype7 Dec 2 '12 at 4:23
@jamtype7 See update. – Vitaliy Kaurov Dec 2 '12 at 5:20

Your Answer


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.