4
$\begingroup$

I have to make a directed graph for a maximum flow algorithm assignment. Although I have managed to make the graph for the better part, it still needs some work.

The problem is that Mathematica automatically centers EdgeLabels. This causes overlapping text at where the edges cross.

I used the following code:

Graph[{R1 \[DirectedEdge] A, R1 \[DirectedEdge] B, 
R2 \[DirectedEdge] A, R2 \[DirectedEdge] B, R2 \[DirectedEdge] C, 
R3 \[DirectedEdge] B, R3 \[DirectedEdge] C, A \[DirectedEdge] D, 
A \[DirectedEdge] E, B \[DirectedEdge] D, B \[DirectedEdge] E, 
B \[DirectedEdge] F, C \[DirectedEdge] E, C \[DirectedEdge] F, 
D \[DirectedEdge] T, E \[DirectedEdge] T, F \[DirectedEdge] T}, 
EdgeWeight -> {120, 115, 70, 90, 110, 140, 120, 110, 85, 130, 95, 85,
130, 160, 220, 330, 240}, EdgeLabels -> "EdgeWeight", 
EdgeLabelStyle -> VertexLabels -> "Name"]

And this produces the following graph.

Direct flow graph

For completeness, R1,R2,R3 are the sources and T is the sink. No need to solve the maximum flow problem.

$\endgroup$

1 Answer 1

4
$\begingroup$

Update: An alternative approach is to change the EdgeShape of selected edges to make them curved. For example, using the EdgeShapeFunction CurvedArc

Graph[edges, EdgeWeight -> ew, EdgeLabels -> "EdgeWeight", VertexLabels -> "Name", 
 ImagePadding -> 20, EdgeShapeFunction -> 
  Join[Thread[{"R2" -> "C", "R3" -> "B", "B" -> "F", "C" -> "E"} -> 
        GraphElementData[{"CurvedArc", "Curvature" -> 3/4}]], 
      Thread[{"R2" -> "A", "A" -> "E", "R1" -> "B", "B" -> "D"} -> 
        GraphElementData[{"CurvedArc", "Curvature" -> -3/4}]]]]

enter image description here

We get alternative pictures assigning Curvature values 3/4 and -3/4 to various subsets of the edges: enter image description here

Original answer:

edges = {"R1" -> "A", "R1" -> "B", "R2" -> "A", "R2" -> "B", 
   "R2" -> "C", "R3" -> "B", "R3" -> "C", "A" -> "D", "A" -> "E", 
   "B" -> "D", "B" -> "E", "B" -> "F", "C" -> "E", "C" -> "F", 
   "D" -> "T", "E" -> "T", "F" -> "T"};
ew = {120, 115, 70, 90, 110, 140, 120, 110, 85, 130, 95, 85, 130, 160,  220, 330, 240};

Graph[edges, EdgeWeight -> ew, 
 EdgeLabels ->  Thread[edges -> (Placed[#, {3/5, {1/2, 1/2}}] & /@ ew)], 
 VertexLabels -> "Name", ImagePadding -> 20]

enter image description here

$\endgroup$
1
  • $\begingroup$ @Nigel, my pleasure. Welcome to mma.se. $\endgroup$
    – kglr
    Commented Dec 15, 2014 at 13:36

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.