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.

If I have a directed graph, how can I draw it with arrows pointing vertically from bottom to top, like showing a class inheritance pattern in OOP(object oriented programming)?

share|improve this question
    
Does LayeredGraphPlot do what you want? –  celtschk May 7 '12 at 21:58
add comment

3 Answers

As mentioned by others, you could use LayeredGraphPlot for this. However, LayeredGraphPlot orders vertices in such a way that directed edges are generally pointing down. To flip the graph over, you could reverse the edges and supply a custom EdgeRenderingFunction, e.g.

edges = {2 -> 1, 3 -> 1, 4 -> 1, 5 -> 2, 6 -> 1, 7 -> 3, 8 -> 7, 9 -> 8};
LayeredGraphPlot[Reverse /@ edges, DirectedEdges -> True,
 EdgeRenderingFunction -> (Arrow[Reverse[#1], .05] &)]

Mathematica graphics

share|improve this answer
    
when I havve a larger data set, the graph is messy, for example, edges = {18 -> 40, 18 -> 25, 19 -> 0, 20 -> 0, 21 -> 2, 21 -> 2, 21 -> 8, 22 -> 1, 22 -> 36, 23 -> 3, 23 -> 31, 23 -> 21, 23 -> 22, 24 -> 3, 24 -> 34, 24 -> 27, 24 -> 40, 25 -> 0, 26 -> 1, 26 -> 8, 27 -> 0, 28 -> 3, 28 -> 35, 28 -> 41, 28 -> 4, 29 -> 4, 29 -> 26, 29 -> 40, 29 -> 41, 29 -> 25, 30 -> 1, 30 -> 35, 31 -> 0, 32 -> 0, 33 -> 1, 33 -> 32, 34 -> 1, 34 -> 42, 35 -> 1, 35 -> 37, 36 -> 0, 37 -> 0, 38 -> 1, 38 -> 40, 39 -> 3, 39 -> 41, 39 -> 31, 39 -> 12, 40 -> 0, 41 -> 0, 42 -> 1, 42 -> 17}. How to make it nicer here? –  Qiang Li May 8 '12 at 0:03
1  
@QiangLi Try changing the AspectRatio and ImageSize –  rm -rf May 8 '12 at 0:59
add comment

Consider the TreePlot function. This page in the documentation should be useful.

One difference between TreePlot and LayeredGraphPlot (as suggested by R.M.) seems to be that TreePlot can handle multiple ''trees'' within the one graph, which might be what you need to trace inheritance in OOP for a large group of functions.

Here is an example lifted directly from the documentation:

TreePlot[Table[i -> Mod[i^3, 221], {i, 0, 221}]]

enter image description here

The other difference between TreePlot and LayeredGraphPlot is that TreePlot will lay out the graph without reference to the direction of the edges, if necessary, but LayeredGraphPlot is specifically for drawing hierarchical directed graphs.

share|improve this answer
add comment

You'll need to use LayeredGraphPlot. Taking an example from the documentation:

LayeredGraphPlot[{"Total" -> "TotalDispatch", 
  "TotalList" -> "CheckThreading", "TotalList" -> "TotalDispatch", 
  "TotalSparse" -> "TotalDispatch", "TotalSparse" -> "TotalDispatch", 
  "TotalDispatch" -> "TotalDispatch", "TotalDispatch" -> "TotalList", 
  "TotalDispatch" -> "TotalPacked", "TotalDispatch" -> "TotalSparse"},
  VertexLabeling -> True]

enter image description here

share|improve this answer
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.