Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a directed acyclic graph $g$, and I'd like to re-order the vertices in this graph object such that when I call

VertexList[myGraph]

the vertices appear in topologically sorted order. The naive solution is to make a new graph as follows:

myGraph = Graph[TopologicalSort[g], EdgeList[g]]

but it seems that the graph object sorts its vertices. So this method is no good.

share|improve this question
Right! Thank you. – rjkaplan Jun 3 '12 at 5:00
So I still don't understand the question... The two graphs are exactly the same, except that the second has the vertices in a different order and the layout is different. Did you want the vertex order to be different but the layout to be the same? – rm -rf Jun 3 '12 at 5:03
I'm not concerned about the layout. I'd like only for the second graph to have vertices in topologically sorted order. My DAG has one source and one sink, so I would want VertexList[g][[1]] to be the source of g and VertexList[g][[-1]] to be the sink. Do you have suggestions for how I might change the question to make this more clear? – rjkaplan Jun 3 '12 at 5:07
1  
But that indeed is the case... With your code above, compare TopologicalSort[g] and VertexList[myGraph]. They're the same. The first element is the source and the last is the sink. I think you've just been looking at the values for the wrong graph. – rm -rf Jun 3 '12 at 5:08
I would recommend never to rely on VertexList or EdgeList having a specific order. You can always create your own edge/vertex list with any order you like. What kind of application do you need this for? – Szabolcs Jun 4 '12 at 8:29

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.