PathGraph
更多信息和选项
- PathGraph 生成一个 Graph 对象.
- PathGraph 支持与 Graph 同样的点、边、封装和选项.
- 一个无向路径图是一个连通图,其中每个顶点最多具有度数2.
- 一个有向路径图是一个连通图,其中每个顶点最多具有入度数1和出度数1.
- PathGraph 只能表示自回避路径,除了圈.
所有选项的列表
范例
打开所有单元 关闭所有单元基本范例 (2)
范围 (6)
连通性 (6)
PathGraph[{12, 23, 34}]PathGraph[{12, 23, 34}, EdgeStyle -> Arrowheads[Medium]]PathGraph[{1 -> 2, 2 -> 3, 3 -> 4}, EdgeStyle -> Arrowheads[Medium]]PathGraph[{1 -> 2, 2 -> 3, 3 -> 4}, DirectedEdges -> False]使用 VertexList 和 EdgeList 获得顶点和边:
{g1, g2} = {PathGraph[{12, 23, 34}], PathGraph[{23, 12, 34}]}EdgeList /@ {g1, g2}VertexList /@ {g1, g2}使用显式顶点列表控制由 VertexList 使用的顺序:
{g1, g2} = {PathGraph[{1, 2, 3, 4}, {23, 12, 34}], PathGraph[{3, 2, 1, 4}, {23, 12, 34}]}VertexList /@ {g1, g2}{PathGraph[{ab, bc}], PathGraph[{"foo""bar", "bar""gnu"}],
PathGraph[{[image][image], [image][image]}]}VertexList /@ %选项 (82)
AnnotationRules (3)
PathGraph[Range[5], AnnotationRules -> {1 -> {VertexLabels -> "hello"}}]PathGraph[Range[5], AnnotationRules -> {12 -> {EdgeLabels -> "hello"}}]PathGraph[Range[5], AnnotationRules -> {"GraphProperties" -> {"Message" -> "hello"}}]AnnotationValue[%, "Message"]DirectedEdges (2)
PathGraph[{1 -> 2, 2 -> 3, 3 -> 4}, EdgeStyle -> Arrowheads[Medium]]使用 DirectedEdges->False 将规则解释为无向边:
PathGraph[{1 -> 2, 2 -> 3, 3 -> 4}, DirectedEdges -> False]使用 DirectedEdge 或者 UndirectedEdge 直接指定一个图是有向图或者无向图:
{PathGraph[{12, 23, 34}, EdgeStyle -> Arrowheads[Medium]], PathGraph[{12, 23, 34}]}EdgeLabels (7)
PathGraph[Range[4], EdgeLabels -> {12 -> "Hello"}]el = {12, 23, 34};PathGraph[el, EdgeLabels -> Table[el[[i]] -> Subscript["e", i], {i, Length[el]}]]PathGraph[Range[4], EdgeLabels -> {12 -> [image], 23 -> [image], 34 -> [image]}]使用具有符号位置信息的 Placed 来控制边上的标签位置:
Table[PathGraph[Range[4], EdgeLabels -> {23 -> Placed["■■■", p]}, PlotLabel -> p], {p, {"Start", "Middle", "End"}}]Table[PathGraph[Range[4], EdgeLabels -> {23 -> Placed["■■■", p]}, PlotLabel -> p, BaselinePosition -> Bottom], {p, {0, 1 / 2, 1}}]Table[PathGraph[Range[4], EdgeLabels -> {23 -> Placed["■■■", {1 / 2, p}]}, PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]使用 Placed 在一个封装中放置多个标签:
PathGraph[{12, 23, Labeled[34, Placed[{"lbl1", "lbl2"}, {"Start", "End"}]]}]PathGraph[{12, 23, Labeled[34, Placed[{"lbl1", "lbl2", "lbl3"}, {"Start", "Middle", "End"}]]}]使用 EdgeLabels 放置多个标签:
PathGraph[{12, 23, 34}, EdgeLabels -> {34 -> Placed[{"lbl1", "lbl2"}, {"Start", "End"}]}]通过 Tooltip 和 StatusArea 的值使用多个标签:
PathGraph[Range[4], EdgeLabels -> Placed["Name", Tooltip]]PathGraph[Range[4], EdgeLabels -> Placed["Name", StatusArea]]EdgeShapeFunction (6)
获得 EdgeShapeFunction 的内置设置列表:
ResourceData["EdgeShapeFunction"]PathGraph[{12, 23, 34}, EdgeShapeFunction -> "Line"]Table[PathGraph[{12, 23, 34}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, {"BoxLine", "DiamondLine", "DotLine"}}]Table[PathGraph[{12, 23, 34}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "FilledArrow"]}]Table[PathGraph[{12, 23, 34}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "UnfilledArrow"]}]Table[PathGraph[{12, 23, 34}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "CarvedArrow"]}]PathGraph[{12, 23, 34}, EdgeShapeFunction -> {12 -> "FilledArrow"}]PathGraph[{12, 23, 34}, EdgeShapeFunction -> {12 -> "FilledArcArrow", "CarvedArrow"}]ef[pts_List, e_] :=
Block[{s = 0.015, g = [image]}, {Arrowheads[{{s, 0.33, g}, {s, 0.67, g}}], Arrow[pts]}]PathGraph[Range[4], EdgeShapeFunction -> ef]EdgeShapeFunction 可以与 EdgeStyle 组合使用:
PathGraph[Range[4], EdgeStyle -> Blue, EdgeShapeFunction -> (Line[#1]&)]EdgeShapeFunction 比 EdgeStyle 具有更高的优先级:
PathGraph[Range[4], EdgeStyle -> Blue, EdgeShapeFunction -> ({Red, Line[#1]}&)]EdgeStyle (2)
EdgeWeight (2)
GraphHighlight (3)
PathGraph[Range[5], VertexSize -> Medium, GraphHighlight -> {1}]PathGraph[Range[5], VertexSize -> Tiny, GraphHighlight -> {23}]PathGraph[Range[5], VertexSize -> Tiny, GraphHighlight -> {1, 2, 12, 34}]GraphHighlightStyle (2)
获得 GraphHighlightStyle 的内置设置列表:
ResourceData["GraphHighlightStyle"]使用 GraphHighlightStyle 的内置设置:
PathGraph[Range[20], GraphHighlight -> {1, 23}, VertexSize -> Small, GraphHighlightStyle -> #, PlotLabel -> #]& /@ Select[ResourceData["GraphHighlightStyle"], # =!= Automatic&]GraphLayout (5)
PathGraph[Range[10], GraphLayout -> Automatic]Table[PathGraph[Range[20], GraphLayout -> l, PlotLabel -> l], {l, {"CircularEmbedding", "SpiralEmbedding"}}]Table[PathGraph[Range[20], GraphLayout -> l, PlotLabel -> l], {l, {"SpringEmbedding", "SpringElectricalEmbedding", "HighDimensionalEmbedding"}}]VertexCoordinates 覆盖 GraphLayout 坐标:
{PathGraph[{12, 23, 34}, GraphLayout -> "SpringElectricalEmbedding"],
PathGraph[{12, 23, 34}, GraphLayout -> "SpringElectricalEmbedding", VertexCoordinates -> Table[{i, i}, {i, 0, 3}]]}使用 AbsoluteOptions 来提取通过布局算法计算得到的 VertexCoordinates:
PathGraph[{12, 23, 34}]AbsoluteOptions[%, VertexCoordinates]PlotTheme (4)
基本主题 (2)
VertexCoordinates (3)
PathGraph[Range[4]]使用 AbsoluteOptions 提取所得的顶点坐标:
AbsoluteOptions[%, VertexCoordinates]ellipseLayout[n_, {a_, b_}] := Table[{a Cos[2Pi / n u], b Sin[2Pi / n u]}, {u, 1, n}]Graphics[Point[ellipseLayout[20, {2, 1}]]]PathGraph[Range[20], VertexCoordinates -> ellipseLayout[20, {2, 1}]]VertexCoordinates 具有比 GraphLayout 更高的优先级:
PathGraph[Range[4], VertexCoordinates -> Table[{i, i}, {i, 4}], GraphLayout -> "CircularEmbedding"]VertexLabels (13)
PathGraph[Range[4], VertexLabels -> "Name"]PathGraph[Range[4], VertexLabels -> {2 -> "one"}]PathGraph[Range[4], VertexLabels -> Table[i -> Subscript[v, i], {i, 4}]]PathGraph[Range[4], VertexLabels -> {1 -> [image], 2 -> [image], 3 -> [image]}, ImagePadding -> 20]使用具有符号位置信息的 Placed 来控制标签位置,包括外部位置:
Table[PathGraph[Range[4], VertexSize -> 0.1, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■■■", p], {i, 4}], PlotLabel -> p], {p, {Before, After, Below, Above}}]pl = {{Before, Below}, {After, Below}, {Before, Above}, {After, Above}};Table[PathGraph[Range[4], VertexSize -> 0.1, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■■■", p], {i, 4}], PlotLabel -> p], {p, pl}]Table[PathGraph[Range[3], VertexSize -> 0.5, VertexLabels -> Table[i -> Placed["■■■", p], {i, 3}], VertexShapeFunction -> "Square", PlotLabel -> p], {p, {Left, Top, Right, Bottom}}]pl = {{Left, Bottom}, {Right, Bottom}, {Left, Top}, {Right, Top}};Table[PathGraph[Range[3], VertexSize -> 0.5, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■■■", p], {i, 3}], PlotLabel -> p], {p, pl}]Table[PathGraph[Range[3], VertexSize -> 0.5, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed[[image], p], {i, 3}], PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]Table[PathGraph[Range[3], VertexSize -> 0.5, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed[[image], {{1, 1}, p}], {i, 3}], PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]使用 Placed 在一个封装中放置多个标签:
PathGraph[{1, 2, 3, Labeled[4, Placed[{"lbl1", "lbl2"}, {Above, Below}]]}, {12, 23, 34}]PathGraph[{1, 2, 3, Labeled[4, Placed[{"lbl1", "lbl2", "lbl3", "lbl4"}, {Above, After, Below, Before}]]}, {12, 23, 34}]使用 VertexLabels 放置多个标签:
PathGraph[{12, 23, 34}, VertexLabels -> {4 -> Placed[{"lbl1", "lbl2"}, {Above, Below}]}]使用 Placed 的参数来控制包括 Tooltip 在内的格式:
PathGraph[Range[4], VertexLabels -> Placed["Name", Tooltip]]或者 StatusArea:
PathGraph[Range[4], VertexLabels -> Placed["Name", StatusArea]]rotateLabel[lab_] := Rotate[lab, 45Degree]PathGraph[Range[4], VertexLabels -> Table[i -> Placed["xxx", Below, rotateLabel], {i, 4}]]panelLabel[lab_] := Panel[lab, FrameMargins -> 0, Background -> Lighter[Yellow, 0.7]]PathGraph[Range[4], VertexLabels -> Table[i -> Placed["xxx", Center, panelLabel], {i, 4}]]hyperlinkLabel[lab_] := Hyperlink[lab, "http://www.wolfram.com"]PathGraph[Range[4], VertexLabels -> Table[i -> Placed["xxx", Center, hyperlinkLabel], {i, 4}]]VertexShape (5)
使用任意 Graphics、Image 或者 Graphics3D 作为顶点形状:
Table[PathGraph[Range[4], VertexShape -> s, VertexSize -> Large], {s, {[image], [image], [image]}}]PathGraph[Range[4], VertexShape -> {2 -> [image]}, VertexSize -> Large]VertexShape 可以与 VertexSize 合起来使用:
Table[PathGraph[Range[4], VertexSize -> s, VertexShape -> [image], PlotLabel -> s], {s, {Small, Large}}]VertexShape 不受 VertexStyle 影响:
PathGraph[Range[4], VertexSize -> 0.2, VertexShape -> [image], VertexStyle -> Blue]VertexShapeFunction 具有比 VertexShape 更高的优先级:
PathGraph[Range[4], VertexSize -> 0.1, VertexShapeFunction -> "Square", VertexShape -> [image]]VertexShapeFunction (10)
获得 VertexShapeFunction 内置集合列表:
ResourceData["VertexShapeFunction"]使用 "Basic" 集合中 VertexShapeFunction 的内置设置:
ResourceData["VertexShapeFunction", "Basic"]Table[PathGraph[{12, 23, 34}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, {"Triangle", "Square", "Rectangle", "Pentagon", "Hexagon", "Octagon"}}]Table[PathGraph[{12, 23, 34}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, {"DownTrapezoid", "UpTrapezoid", "Parallelogram", "FiveDown", "Circle", "Diamond", "Star", "Capsule"}}]使用 "Rounded" 集合中的 VertexShapeFunction 内置设定:
ResourceData["VertexShapeFunction", "Rounded"]Table[PathGraph[{12, 23, 34}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, ResourceData["VertexShapeFunction", "Rounded"]}]使用 "Concave" 集合中的 VertexShapeFunction 内置设定:
ResourceData["VertexShapeFunction", "Concave"]Table[PathGraph[{12, 23, 34}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, ResourceData["VertexShapeFunction", "Concave"]}]PathGraph[{12, 23, 34}, VertexShapeFunction -> { 1 -> "Square"}, VertexSize -> 0.2]PathGraph[{12, 23, 34}, VertexShapeFunction -> { 1 -> "Square", "Triangle"}, VertexSize -> 0.2]PathGraph[Range[4], VertexShapeFunction -> (Inset[[image], #]&)]vf[{xc_, yc_}, name_, {w_, h_}] :=
Block[{xmin = xc - w, xmax = xc + w, ymin = yc - h, ymax = yc + h},
Polygon[{{xmin, ymin}, {xmax, ymax}, {xmin, ymax}, {xmax, ymin}}]
];PathGraph[Range[4], VertexShapeFunction -> vf, VertexSize -> 0.2]VertexShapeFunction 可以与 VertexStyle 组合使用:
vf1[{xc_, yc_}, name_, {w_, h_}] := Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]PathGraph[Range[4], VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf1]VertexShapeFunction 比 VertexStyle 具有更高的优先级:
vf2[{xc_, yc_}, name_, {w_, h_}] := {Red, Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]}PathGraph[Range[4], VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf2]VertexShapeFunction 可以与 VertexSize 组合使用:
PathGraph[Range[4], VertexShapeFunction -> "Star", VertexSize -> {2 -> 0.3, 0.2}]VertexShapeFunction 比 VertexShape 具有更高的优先级:
PathGraph[Range[4], VertexSize -> 0.3, VertexShapeFunction -> "Star", VertexShape -> [image]]VertexSize (8)
PathGraph[Range[4], VertexSize -> Automatic]Table[PathGraph[Range[4], VertexSize -> s, PlotLabel -> s], {s, {Tiny, Small, Medium, Large}}]Table[PathGraph[Range[4], VertexSize -> s, PlotLabel -> s], {s, 0.1, 1, 0.3}]Table[PathGraph[Range[4], VertexSize -> {"Scaled", s}, PlotLabel -> {"Scaled", s}], {s, 0.1, 1, 0.3}]Table[PathGraph[Range[4], VertexSize -> s, PlotLabel -> s], {s, {{0.1, 0.2}, {0.2, 0.1}}}]PathGraph[Range[4], VertexSize -> {1 -> 0.2, 2 -> 0.3}]VertexSize 可以与 VertexShapeFunction 合起来使用:
Table[PathGraph[Range[4], VertexSize -> s, VertexShapeFunction -> "Square", PlotLabel -> s], {s, {0.05, 0.1, 0.2}}]VertexSize 可以与 VertexShape 合起来使用:
Table[PathGraph[Range[4], VertexSize -> s, VertexShape -> [image], PlotLabel -> s], {s, {0.1, 0.2, 0.4}}]VertexStyle (5)
Table[PathGraph[Range[5], VertexStyle -> style, VertexSize -> 0.4, PlotLabel -> style], {style, {Yellow, EdgeForm[Dashed]}}]PathGraph[Range[5], VertexStyle -> {1 -> Blue, 2 -> Red}, VertexSize -> 0.2]VertexShapeFunction 可以与 VertexStyle 合起来使用:
vf1[{xc_, yc_}, name_, {w_, h_}] := Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]PathGraph[Range[5], VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf1]VertexShapeFunction 具有比 VertexStyle 更高的优先级:
vf2[{xc_, yc_}, name_, {w_, h_}] := {Red, Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]}PathGraph[Range[5], VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf2]VertexStyle 可以与 BaseStyle 联合:
PathGraph[Range[5], VertexStyle -> LightBlue, BaseStyle -> EdgeForm[Dotted], VertexSize -> 0.4]VertexStyle 具有比 BaseStyle 更高的优先级:
PathGraph[Range[5], VertexStyle -> LightBlue, BaseStyle -> Gray, VertexSize -> 0.4]VertexShape 不受 VertexStyle 影响:
PathGraph[Range[5], VertexSize -> 0.4, VertexShape -> [image], VertexStyle -> Blue]VertexWeight (2)
应用 (6)
路径图的 GraphCenter:
Table[HighlightGraph[#, GraphCenter[#]]&[PathGraph[Range[i], VertexSize -> 0.2]], {i, 3, 6}]Table[HighlightGraph[#, GraphPeriphery[#]]&[PathGraph[Range[i], VertexSize -> 0.2]], {i, 3, 6}]VertexEccentricity[PathGraph[Range[20]], #]& /@ Range[4]FindVertexEccentricityPath[g_ ? UndirectedGraphQ, u_] /; MemberQ[VertexList[g], u] := Module[{d = GraphDistanceMatrix[g], posu, posv, vl = VertexList[g]}, posu = VertexIndex[g, u];
posv = First@First@Position[d[[posu]], Max[d[[posu]]]];
PathGraph[FindShortestPath[g, u, vl[[posv]]]]]Table[HighlightGraph[g = PathGraph[Range[20], VertexSize -> Medium], FindVertexEccentricityPath[g, u]], {u, Range[4]}]Table[GraphRadius[PathGraph[Range[i]]], {i, 3, 6}]FindRadiusPath[g_ ? UndirectedGraphQ] := Module[{c = First@GraphCenter[g], d, v, pos}, d = Table[GraphDistance[g, c, u], {u, VertexList[g]}];
pos = First@Position[d, Max[d]];
v = First@Part[VertexList[g], pos];
PathGraph[FindShortestPath[g, c, v]]]Table[HighlightGraph[#, FindRadiusPath[#]]&[PathGraph[Range[i], VertexSize -> Medium]], {i, 3, 6}]Table[GraphDiameter[PathGraph[Range[i]]], {i, 3, 6}]FindDiameterPath[g_ ? UndirectedGraphQ] := Module[{d = GraphDistanceMatrix[g], u, v, pos}, pos = First@Position[d, Max[d]];
{u, v} = Part[VertexList[g], pos];
PathGraph[FindShortestPath[g, u, v]]]Table[HighlightGraph[#, FindDiameterPath[#]]&[PathGraph[Range[i], VertexSize -> Medium]], {i, 3, 6}]可视化 PathGraph 不同的中心性:
HighlightCentrality[g_, cc_] := HighlightGraph[g, Table[Style[VertexList[g][[i]], ColorData["TemperatureMap"][cc[[i]] / Max[cc]]], {i, VertexCount[g]}]];g = PathGraph[Range[25], VertexSize -> Large];HighlightCentrality[g, DegreeCentrality[g]]HighlightCentrality[g, ClosenessCentrality[g]]HighlightCentrality[g, EigenvectorCentrality[g]]属性和关系 (10)
使用 VertexCount 和 EdgeCount 来计算顶点数和边数:
g = PathGraph[Range[20]]{VertexCount[g], EdgeCount[g]}使用 VertexList 和 EdgeList 以标准顺序列出顶点和边:
g = PathGraph[{12, 23, 34}];{VertexList[g], EdgeList[g]}g1 = PathGraph[{23, 12, 34}];{VertexList[g1], EdgeList[g1]}g2 = PathGraph[{34, 23, 12}];{VertexList[g2], EdgeList[g2]}邻接矩阵的行和列遵循 VertexList 给出的顺序:
g = PathGraph[{ab, bc, c -> d}];VertexList[g]TableForm[Normal@AdjacencyMatrix[g], TableHeadings -> {s = Style[#, Blue]& /@ %, s}]从一个图计算 IncidenceMatrix:
g = PathGraph[{ab, bc, c -> d}];m = IncidenceMatrix[g];行顺序由 VertexList 给出,列顺序由 EdgeList 给出:
{VertexList[g], EdgeList[g]}TableForm[Normal@m, TableHeadings -> {Style[#, Blue]& /@ VertexList[g], Style[#, Blue]& /@ EdgeList[g]}]Graph[{12, 23, 22}]PathGraphQ[%]CycleGraph[5]PathGraphQ[%]g = CycleGraph[5]{ConnectedGraphQ[g], PathGraphQ[g]}VertexDegree[g]TreeGraphQ[PathGraph[{12, 23, 34}]]PathGraph[{12, 23, 34}]AcyclicGraphQ[%]PathGraph[Range[21]]{LineGraph[%], PathGraph[Range[20]]}IsomorphicGraphQ@@%文本
Wolfram Research (2010),PathGraph,Wolfram 语言函数,https://reference.wolfram.com/language/ref/PathGraph.html (更新于 2015 年).
CMS
Wolfram 语言. 2010. "PathGraph." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2015. https://reference.wolfram.com/language/ref/PathGraph.html.
APA
Wolfram 语言. (2010). PathGraph. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/PathGraph.html 年
BibTeX
@misc{reference.wolfram_2026_pathgraph, author="Wolfram Research", title="{PathGraph}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/PathGraph.html}", note=[Accessed: 01-September-2026]}
BibLaTeX
@online{reference.wolfram_2026_pathgraph, organization={Wolfram Research}, title={PathGraph}, year={2015}, url={https://reference.wolfram.com/language/ref/PathGraph.html}, note=[Accessed: 01-September-2026]}