Creating a graph with edges of different colours in Mathematica
Asked Answered
D

2

5

I want to create a graph (Graph Theory) where certain edges have a different colour to other edges, which would be used to highlight a path in the graph from one vertex to another.

Here are some examples which have different coloured edges http://demonstrations.wolfram.com/AGraphTheoryInterpretationOfTheSumOfTheFirstNIntegers/ and http://demonstrations.wolfram.com/Ramsey336/. I looked at source code for these but those solutions seem complicated. I need a simple example to work from. I reckon I need to use the EdgeRenderingFunction as one of the options for GraphPlot.

Additionally under EdgeRenderingFunction documentation in "More Information" part it says:

Mathematica graphics

This looks useful but unfortunately there is no coded examples to try.

Taking that very literally I tried things like

GraphPlot[{1 -> 2, 2 -> 3, 3 -> 4, 4 -> 1, 2 -> 4, 4 -> 5, 4 -> 6}, VertexLabeling -> True,
EdgeRenderingFunction -> g[{1, 2}, {1, 2}, Red]]

But that wouldn't work. It will take something more clever than that.

Dimenhydrinate answered 9/10, 2010 at 19:7 Comment(0)
X
6

Here's an example that illustrates how to automate the highlighting of a particular path through a graph.

Here's a silly graph, specified by a list of edge rules:

edges = Table[i -> Mod[1 + i^2, 10], {i, 0, 9}];
GraphPlot[edges, VertexLabeling -> True]

Mathematica graphics

Here's a path through the graph we'd like to highlight.

path = {0, 1, 2, 5, 6, 7, 0};

Let's partition the path into edges, accounting for the fact that we want to highlight the edge independent of its orientation.

edgesToHighlight = Partition[path, 2, 1];
edgesToHighlight = Join[edgesToHighlight,
    Reverse /@ edgesToHighlight];

We write an EdgeRenderingFunction that renders an edge in one of two styles, depending no whether it's in our list or not.

erf[pts_, edge_, ___] := If[MemberQ[edgesToHighlight, edge],
    {Thick, Black, Arrow[pts, 0.1]}, {Darker[Red], Line[pts]}];

Finally, we display the result.

GraphPlot[edges, EdgeRenderingFunction -> erf,
    VertexLabeling -> True]

Mathematica graphics

Xymenes answered 10/10, 2010 at 0:53 Comment(4)
simple, elegant and general +1Universal
@ Mark McClure: Mathematica is surprising me again. How can you call the function erf without passing any arguments to it? Even though you created 3 parameters in the function definition, I assume it automatically "finds" them in the current context?Dimenhydrinate
@Dimenhydrinate I haven't called the function; I'm simply telling GraphPlot what function to call when it draws the edges. Here's a similar example: Select[Range[9], EvenQ]. In this example, EvenQ is passed as an argument to Select. Select then selects only those integers n for which EvenQ[n] returns True.Xymenes
@Mark McClure: Ok I see, it is a kind of implicit pure function. It is the same as Select[Range[9], EvenQ[#] &] except without the more familar # and &.Dimenhydrinate
H
1
GraphPlot[
 {1 -> 2, 2 -> 3, 3 -> 4, 4 -> 1, 2 -> 4, 4 -> 5, 4 -> 6}, 
 VertexLabeling -> True, 
 EdgeRenderingFunction -> (
    {If[#2 == {1, 2}, Red, Black], 
     Line[#1]}
  &)
]

Mathematica graphics

The rendering function is a callback function, which takes 3 arguments. The 1st is the list of coordinates of the line, the 2nd is the vertices of the edge, and the 3rd is the edge's label.

In Mathematica you could create an anonymous function with (f[#1,#2,#3,...] &).

Hands answered 9/10, 2010 at 19:22 Comment(4)
That works for one edge. But suppose I want to colour a path that covers more than one edge and vertex? I tried modification: If[#2 == {1, 2, 3, 4, 5}... and If[#2 == {{1, 2}, {3, 4}}... but didn't work. Any ideas?Dimenhydrinate
@dbjohn: See MemberQ.Hands
You are suggesting I use MemberQ like: If[MemberQ[#2, {2, 3, 4, 5}], Red... that is to say if vertices x,y,z are in members of the list of all vertices colour them red? That code doesn't work, MemberQ can't take a list as its second argument. I am going to need more explicit direction.Dimenhydrinate
The second argument to the EdgeRenderingFunction is a pair, like {2,3} which represents an edge between vertices 2 and 3. You can reverse the arguments of MemberQ and do the following, for example: MemberQ[{{1, 2}, {2, 4}}, #2] That will be True for edges 1->2 and 2->4. HTH!Drynurse

© 2022 - 2024 — McMap. All rights reserved.