Creating a graph with edges of different colors in Mathematica

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

Here are a few examples that have different colored edges http://demonstrations.wolfram.com/AGraphTheoryInterpretationOfTheSumOfTheFirstNIntegers/ and http://demonstrations.wolfram.com/Ramsey336/ . I looked at the source code for them, but these solutions seem complicated. I need a simple example to work with. I believe that I need to use EdgeRenderingFunction as one of the options for GraphPlot .

Additionally, in the EdgeRenderingFunction section , the documentation in the "Details Information" section says:

Mathematica graphics

This seems useful, but unfortunately there are no coded examples.

Taking it 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 will not work. It takes something smarter than that.

+5
source share
2 answers

Here is an example that illustrates how to automate the allocation of a specific path through a graph.

Here's a dumb graph given by a list of edge rules:

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

Mathematica graphics

Here is the schedule path we would like to highlight.

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

Let me divide the path into edges, given the fact that we want to highlight an edge that does not depend on its orientation.

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

We are writing an EdgeRenderingFunction that displays an edge in one of two styles, depending on whether it is on our list or not.

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

Finally, we show the result.

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

Mathematica graphics

+6
source
 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 render function is a callback function that takes 3 arguments. The first is a list of line coordinates, the second is the top of the edge, and the third is the edge label.

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

+1
source

Source: https://habr.com/ru/post/898230/


All Articles