2
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ perhaps you can be more specific on f[]? Or is it a Sort of the vertexes? $\endgroup$
    – FredrikD
    Dec 2, 2012 at 4:47
  • $\begingroup$ 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` $\endgroup$ Dec 2, 2012 at 5:47

1 Answer 1

1
$\begingroup$

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]];
VertexList[f[g]]

{a, b, c}

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

TopologicalSort[g]

{a, b, c}

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – jamtype7
    Dec 2, 2012 at 4:23
  • $\begingroup$ @jamtype7 See update. $\endgroup$ Dec 2, 2012 at 5:20

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.