views:

77

answers:

2

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 EdgeRenderingFunction->g specifies that each edge should be rendered with the graphics primitives given by g[{Subscript[r, i],[Ellipsis],Subscript[r, j]},{Subscript[v, i],Subscript[v, j]},Subscript[lbl, ij]], where Subscript[r, i], Subscript[r, j] are the beginning and ending points of the edge, Subscript[v, i], Subscript[v, j] are the beginning and ending vertices, the Subscript[lbl, ij] is any label specified for the edge. http://reference.wolfram.com/mathematica/ref/EdgeRenderingFunction.html 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.

+1  A: 
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]}
  &)
]

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,...] &).

KennyTM
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?
dbjohn
@dbjohn: See [MemberQ](http://reference.wolfram.com/mathematica/ref/MemberQ.html).
KennyTM
@KennyTM: 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.
dbjohn
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!
Michael Pilat
+3  A: 

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]

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]
Mark McClure
simple, elegant and general +1
belisarius
@ 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?
dbjohn
@dbjohn 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.
Mark McClure
dbjohn