在Mathematica中创建具有不同颜色边缘的图形
-
29-09-2019 - |
题
我想创建一个图形(图理论),其中某些边缘与其他边缘具有不同的颜色,该颜色将用于突出一个从一个顶点到另一个顶点的图路径。
这是一些具有不同彩色边缘的示例 http://demnstration.wolfram.com/agraphtheoryternoryternationofthesumofthefirstnintegers/ 和 http://demstrations.wolfram.com/ramsey336/. 。我查看了这些源代码,但这些解决方案似乎很复杂。我需要一个简单的示例才能使用。我认为我需要使用EdgerenderingFunction作为一种选项之一 GraphPlot
.
另外 EdgerenderingFunction 它说的“更多信息”部分中的文档:
这看起来很有用,但不幸的是没有编码示例可以尝试。
从字面上看,我尝试了
GraphPlot [{1-> 2,2-> 3,3-> 4,4-> 1,2-> 4,4-> 5,4-> 6},vertexlabeling-> true,true,true,
EdgerenderingFunction-> g [{1,2},{1,2},red]
但这是行不通的。这将需要比这更聪明。
解决方案
这是一个示例,该示例说明了如何通过图形自动化特定路径的突出显示。
这是一个愚蠢的图,由边缘规则列表指定:
edges = Table[i -> Mod[1 + i^2, 10], {i, 0, 9}];
GraphPlot[edges, VertexLabeling -> True]
这是我们要突出的图形的路径。
path = {0, 1, 2, 5, 6, 7, 0};
让我们将路径分为边缘,这是一个事实,即我们要突出边缘独立于其方向。
edgesToHighlight = Partition[path, 2, 1];
edgesToHighlight = Join[edgesToHighlight,
Reverse /@ edgesToHighlight];
我们写一个 EdgeRenderingFunction
这在两种样式之一中具有优势,具体取决于我们是否在我们的列表中。
erf[pts_, edge_, ___] := If[MemberQ[edgesToHighlight, edge],
{Thick, Black, Arrow[pts, 0.1]}, {Darker[Red], Line[pts]}];
最后,我们显示结果。
GraphPlot[edges, EdgeRenderingFunction -> erf,
VertexLabeling -> True]
其他提示
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]}
&)
]
渲染函数是一个回调函数,该函数需要3个参数。第一是线的坐标列表,第二是边缘的顶点,第三是边缘的标签。
在Mathematica中,您可以使用 (f[#1,#2,#3,...] &)
.