给出具有 n 个顶点的
的完全图.
CompleteGraph[{n1,n2,…,nk}]
给出具有 n1+n2+⋯+nk 个顶点的
的完全 k 分图.
CompleteGraph
给出具有 n 个顶点的
的完全图.
CompleteGraph[{n1,n2,…,nk}]
给出具有 n1+n2+⋯+nk 个顶点的
的完全 k 分图.
更多信息和选项
- CompleteGraph 也称为通用图或完整图.
- CompleteGraph 生成一个 Graph 对象.
- CompleteGraph[n] 给出具有 n 个顶点,以及在每对顶点之间存在一条边的一个图.
- CompleteGraph[{n1,n2,…,nk}] 给出由 n1+⋯+nk 个顶点组成的一个图,其中这些顶点被分成若干个不相交集合 Vi,每个集合有 ni 个顶点,并且在不同集合 Vi 和 Vj 的所有顶点之间存在边,但是在同一个集合 Vi 的所有顶点之间不存在边.
- CompleteGraph[…,DirectedEdges->True] 给出一个有向完全图.
- CompleteGraph 采用和 Graph 相同的选项.
所有选项的列表
范例
打开所有单元 关闭所有单元基本范例 (4)
Table[CompleteGraph[i, PlotLabel -> Subscript[K, ``i``]], {i, 3, 6}]Table[CompleteGraph[{i, i}, PlotLabel -> Subscript[K, i, i]], {i, 2, 4}]Table[CompleteGraph[n, DirectedEdges -> True, PlotLabel -> Subscript[K, n]], {n, 3, 5}]Table[CompleteGraph[{i, i, i}, DirectedEdges -> True, PlotLabel -> Subscript[K, i, i, i]], {i, 2, 4}]选项 (81)
AnnotationRules (2)
DirectedEdges (2)
CompleteGraph[4]使用 DirectedEdges->True 来生成一个有向图:
CompleteGraph[4, DirectedEdges -> True]Row[Table[CompleteGraph[Range[n], DirectedEdges -> True], {n, 2, 4}]]EdgeLabels (7)
CompleteGraph[4, EdgeLabels -> {12 -> "Hello"}]el = EdgeList[CompleteGraph[4]]CompleteGraph[4, EdgeLabels -> Table[el[[i]] -> Subscript["e", i], {i, Length[el]}]]CompleteGraph[4, EdgeLabels -> {24 -> [image], 34 -> [image], 13 -> [image]}]使用符号位置信息的 Placed 来控制沿着一条边的标签位置:
Table[CompleteGraph[4, EdgeLabels -> {34 -> Placed["■■■", p]}, PlotLabel -> p], {p, {"Start", "Middle", "End"}}]Table[CompleteGraph[4, EdgeLabels -> {23 -> Placed["■■■", p]}, PlotLabel -> p, BaselinePosition -> Bottom], {p, {0, 1 / 4, 1 / 3}}]Table[CompleteGraph[4, EdgeLabels -> {23 -> Placed["■■■", {1 / 2, p}]}, PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]CompleteGraph[5, EdgeLabels -> {31 -> Placed[{"lbl1", "lbl2"}, {"Start", "End"}]}]CompleteGraph[5, EdgeLabels -> {31 -> Placed[{"lbl1", "lbl2", "lbl3"}, {"Start", "Middle", "End"}]}]通过 Tooltip 和 StatusArea 的值,使用自动标签:
CompleteGraph[5, EdgeLabels -> Placed["Name", Tooltip]]CompleteGraph[5, EdgeLabels -> Placed["Name", StatusArea]]EdgeShapeFunction (6)
获取 EdgeShapeFunction 的内置设置列表:
ResourceData["EdgeShapeFunction"]CompleteGraph[4, EdgeShapeFunction -> "Line"]Table[CompleteGraph[4, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, {"BoxLine", "DiamondLine", "DotLine"}}]Table[CompleteGraph[4, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "FilledArrow"]}]Table[CompleteGraph[4, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "UnfilledArrow"]}]Table[CompleteGraph[4, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "CarvedArrow"]}]CompleteGraph[4, EdgeShapeFunction -> {12 -> "DotLine"}]CompleteGraph[5, EdgeShapeFunction -> {12 -> "BoxLine", "DotLine"}]ef[pts_List, e_] :=
Block[{s = 0.015, g = [image]}, {Arrowheads[{{s, 0.33, g}, {s, 0.67, g}}], Arrow[pts]}]CompleteGraph[4, EdgeShapeFunction -> ef]EdgeShapeFunction 可以与 EdgeStyle 合起来使用:
CompleteGraph[4, EdgeStyle -> Blue, EdgeShapeFunction -> (Line[#1]&)]EdgeShapeFunction 具有比 EdgeStyle 更高的优先级:
CompleteGraph[4, EdgeStyle -> Blue, EdgeShapeFunction -> ({Red, Line[#1]}&)]EdgeStyle (2)
EdgeWeight (2)
GraphHighlight (3)
GraphHighlightStyle (2)
获取 GraphHighlightStyle 的默认设置列表:
ResourceData["GraphHighlightStyle"]使用 GraphHighlightStyle 的内置设置:
CompleteGraph[3, GraphHighlight -> {1, 23}, VertexSize -> Small, GraphHighlightStyle -> #, PlotLabel -> #]& /@ Select[ResourceData["GraphHighlightStyle"], # =!= Automatic&]GraphLayout (5)
CompleteGraph[5, GraphLayout -> Automatic]Table[CompleteGraph[20, GraphLayout -> l, PlotLabel -> l], {l, {"CircularEmbedding", "SpiralEmbedding"}}]Table[CompleteGraph[20, GraphLayout -> l, PlotLabel -> l], {l, {"SpringEmbedding", "SpringElectricalEmbedding", "HighDimensionalEmbedding"}}]VertexCoordinates 会覆盖 GraphLayout 的坐标:
{CompleteGraph[5, GraphLayout -> "SpringElectricalEmbedding"],
CompleteGraph[5, GraphLayout -> "SpringElectricalEmbedding", VertexCoordinates -> Table[{i, i}, {i, 0, 4}]]}通过一个布局算法,利用 AbsoluteOptions 提取计算所得的 VertexCoordinates:
CompleteGraph[5]AbsoluteOptions[%, VertexCoordinates]PlotTheme (4)
基本主题 (2)
VertexCoordinates (3)
CompleteGraph[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[10, {2, 1}]]]CompleteGraph[10, VertexCoordinates -> ellipseLayout[10, {2, 1}]]VertexCoordinates 具有比 GraphLayout 更高的优先级:
CompleteGraph[4, VertexCoordinates -> Table[{i, i}, {i, 4}], GraphLayout -> "CircularEmbedding"]VertexLabels (13)
CompleteGraph[3, VertexLabels -> "Name"]CompleteGraph[3, VertexLabels -> {1 -> "one"}]CompleteGraph[3, VertexLabels -> Table[i -> Subscript[v, i], {i, 3}]]CompleteGraph[3, VertexLabels -> {1 -> [image], 2 -> [image], 3 -> [image]}, ImagePadding -> 20]使用具有符号位置信息的 Placed 来控制标签位置,包括外部位置:
Table[CompleteGraph[3, VertexSize -> 0.1, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■■■", p], {i, 3}], PlotLabel -> p, ImagePadding -> 20], {p, {Before, After, Below, Above}}]pl = {{Before, Below}, {After, Below}, {Before, Above}, {After, Above}};Table[CompleteGraph[3, VertexSize -> 0.1, VertexShapeFunction -> "Square", ImagePadding -> 20, VertexLabels -> Table[i -> Placed["■■■", p], {i, 3}], PlotLabel -> p], {p, pl}]Table[CompleteGraph[3, VertexSize -> 0.25, 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[CompleteGraph[3, VertexSize -> 0.25, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■■■", p], {i, 3}], PlotLabel -> p], {p, pl}]Table[CompleteGraph[3, VertexSize -> 0.25, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed[[image], p], {i, 3}], PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]Table[CompleteGraph[3, VertexSize -> 0.35, 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}}}]CompleteGraph[3, VertexLabels -> {1 -> Placed[{"lbl1", "lbl2"}, {Above, Below}]}]CompleteGraph[3, VertexLabels -> {1 -> Placed[{"lbl1", "lbl2", "lbl3", "lbl4"}, {Above, After, Below, Before}]}]使用 Placed 的变量来控制格式,包括 Tooltip:
CompleteGraph[3, VertexLabels -> Placed["Name", Tooltip]]或者 StatusArea:
CompleteGraph[3, VertexLabels -> Placed["Name", StatusArea]]rotateLabel[lab_] := Rotate[lab, 45Degree]CompleteGraph[3, VertexLabels -> Table[i -> Placed["xxx", Below, rotateLabel], {i, 3}]]panelLabel[lab_] := Panel[lab, FrameMargins -> 0, Background -> Lighter[Yellow, 0.7]]CompleteGraph[3, VertexLabels -> Table[i -> Placed["xxx", Center, panelLabel], {i, 3}]]hyperlinkLabel[lab_] := Hyperlink[lab, "http://www.wolfram.com"]CompleteGraph[3, VertexLabels -> Table[i -> Placed["xxx", Center, hyperlinkLabel], {i, 3}]]VertexShape (5)
将任意 Graphics、Image 或者 Graphics3D 作为顶点形状使用:
Table[CompleteGraph[5, VertexShape -> s, VertexSize -> Medium], {s, {[image], [image], [image]}}]CompleteGraph[5, VertexShape -> {2 -> [image]}, VertexSize -> Medium]VertexShape 可以与 VertexSize 合起来使用:
Table[CompleteGraph[5, VertexSize -> s, VertexShape -> [image], PlotLabel -> s], {s, {Small, Large}}]VertexShape 不受 VertexStyle 影响:
CompleteGraph[5, VertexSize -> 0.2, VertexShape -> [image], VertexStyle -> Blue]VertexShapeFunction 具有比 VertexShape 更高的优先级:
CompleteGraph[5, VertexSize -> 0.1, VertexShapeFunction -> "Square", VertexShape -> [image]]VertexShapeFunction (10)
获取 VertexShapeFunction 的内置设置列表:
ResourceData["VertexShapeFunction"]利用 VertexShapeFunction 在 "Basic" 集合中的内置设置:
ResourceData["VertexShapeFunction", "Basic"]Table[CompleteGraph[4, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, {"Triangle", "Square", "Rectangle", "Pentagon", "Hexagon", "Octagon"}}]Table[CompleteGraph[4, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, {"DownTrapezoid", "UpTrapezoid", "Parallelogram", "FiveDown", "Circle", "Diamond", "Star", "Capsule"}}]使用 VertexShapeFunction 在 "Rounded" 集合中的内置设置:
ResourceData["VertexShapeFunction", "Rounded"]Table[CompleteGraph[4, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, ResourceData["VertexShapeFunction", "Rounded"]}]使用 VertexShapeFunction 在 "Concave" 集合中的内置设置:
ResourceData["VertexShapeFunction", "Concave"]Table[CompleteGraph[4, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, ResourceData["VertexShapeFunction", "Concave"]}]CompleteGraph[4, VertexShapeFunction -> { 1 -> "Square"}, VertexSize -> 0.2]CompleteGraph[4, VertexShapeFunction -> { 1 -> "Square", "Triangle"}, VertexSize -> 0.2]CompleteGraph[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}}]
];CompleteGraph[4, VertexShapeFunction -> vf, VertexSize -> 0.2]VertexShapeFunction 可以与 VertexStyle 合起来使用:
vf1[{xc_, yc_}, name_, {w_, h_}] := Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]CompleteGraph[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}]}CompleteGraph[4, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf2]VertexShapeFunction 可以与 VertexSize 合起来使用:
CompleteGraph[4, VertexShapeFunction -> "Star", VertexSize -> {1 -> Small, Medium}]VertexShapeFunction 具有比 VertexShape 更高的优先级:
CompleteGraph[4, VertexSize -> 0.3, VertexShapeFunction -> "Star", VertexShape -> [image]]VertexSize (8)
CompleteGraph[5, VertexSize -> Automatic]Table[CompleteGraph[5, VertexSize -> s, PlotLabel -> s], {s, {Tiny, Small, Medium, Large}}]Table[CompleteGraph[5, VertexSize -> s, PlotLabel -> s], {s, 0.1, 1, 0.3}]Table[CompleteGraph[5, VertexSize -> {"Scaled", s}, PlotLabel -> {"Scaled", s}], {s, 0.1, 1, 0.3}]Table[CompleteGraph[5, VertexSize -> s, PlotLabel -> s], {s, {{0.1, 0.2}, {0.2, 0.1}}}]CompleteGraph[4, VertexSize -> {1 -> 0.2, 2 -> 0.3}]VertexSize 可以与 VertexShapeFunction 合起来使用:
Table[CompleteGraph[5, VertexSize -> s, VertexShapeFunction -> "Square", PlotLabel -> s], {s, {0.05, 0.1, 0.2}}]VertexSize 可以与 VertexShape 合起来使用:
Table[CompleteGraph[5, VertexSize -> s, VertexShape -> [image], PlotLabel -> s], {s, {0.1, 0.2, 0.4}}]VertexStyle (5)
Table[CompleteGraph[4, VertexStyle -> style, VertexSize -> 0.3, PlotLabel -> style], {style, {Yellow, EdgeForm[Dashed]}}]CompleteGraph[4, 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}]CompleteGraph[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}]}CompleteGraph[4, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf2]VertexStyle 可以与 BaseStyle 合起来使用:
CompleteGraph[4, VertexStyle -> LightBlue, BaseStyle -> EdgeForm[Dotted], VertexSize -> 0.2]VertexStyle 具有比 BaseStyle 更高的优先级:
CompleteGraph[4, VertexStyle -> LightBlue, BaseStyle -> Gray, VertexSize -> 0.2]VertexShape 不受 VertexStyle 影响:
CompleteGraph[5, VertexSize -> 0.2, VertexShape -> [image], VertexStyle -> Blue]应用 (7)
一个完全图的 GraphCenter 包含所有顶点:
Table[HighlightGraph[#, GraphCenter[#]]&[CompleteGraph[i, VertexSize -> Small]], {i, 3, 6}]GraphPeriphery 包含所有顶点:
Table[HighlightGraph[#, GraphPeriphery[#]]&[CompleteGraph[i, VertexSize -> Small]], {i, 3, 6}]所有顶点的 VertexEccentricity 是 1:
VertexEccentricity[CompleteGraph[5], #]& /@ VertexList[CompleteGraph[5]]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 = CompleteGraph[4], FindVertexEccentricityPath[g, u]], {u, Range[4]}]GraphRadius 是 1:
Table[GraphRadius[CompleteGraph[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[#]]&[CompleteGraph[i, VertexSize -> Small]], {i, 3, 6}]GraphDiameter 是 1:
Table[GraphDiameter[CompleteGraph[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[#]]&[CompleteGraph[i, VertexSize -> Small]], {i, 3, 6}]g = CompleteGraph[4, VertexLabels -> "Name"]{HighlightGraph[g, 12],
HighlightGraph[g, PathGraph[{1, 3, 2}]],
HighlightGraph[g, PathGraph[{1, 4, 2}]]}CompleteGraph[n] 的顶点连通度是
:
g = CompleteGraph[5, VertexLabels -> "Name"];{HighlightGraph[g, PathGraph[{1, 2}]],
HighlightGraph[g, PathGraph[{1, 3, 2}]],
HighlightGraph[g, PathGraph[{1, 4, 2}]],
HighlightGraph[g, PathGraph[{1, 5, 2}]]
}突出显示 CompleteGraph 的顶点度:
HighlightCentrality[g_, cc_] := HighlightGraph[g, Table[Style[VertexList[g][[i]], ColorData["TemperatureMap"][cc[[i]] / Max[cc]]], {i, VertexCount[g]}]];g = CompleteGraph[8, VertexSize -> Large];HighlightCentrality[g, VertexDegree[g]]HighlightCentrality[g, ClosenessCentrality[g]]HighlightCentrality[g, EigenvectorCentrality[g]]属性和关系 (12)
CompleteGraph[n] 的顶点数:
VertexCount[CompleteGraph[n]]CompleteGraph[n] 的边数:
EdgeCount[CompleteGraph[n]]CompleteGraph[6]VertexDegree[%]g = CompleteGraph[8]HighlightGraph[g, h = Subgraph[g, Range[5]]]CompleteGraphQ[h]CompleteGraph[8, VertexSize -> {1 -> 0.2}]HighlightGraph[%, NeighborhoodGraph[%, 1]]CompleteGraph[5]Subgraph[%, First[FindClique[%]]]完全图的 GraphComplement 不具有边:
GraphComplement[CompleteGraph[5]]对于一个完全图,AdjacencyMatrix 中对角线外的元素都是1:
AdjacencyMatrix[CompleteGraph[25]]//MatrixPlotAdjacencyMatrix[CompleteGraph[{1, 2, 3, 4, 5}]]//MatrixPlot{CompleteGraph[3], CycleGraph[3]}{CompleteGraph[4], WheelGraph[4]}{CompleteGraph[6], LineGraph[StarGraph[7]]}巧妙范例 (2)
Graphics[Table[Inset[CompleteGraph[RandomInteger[{4, 8}]], RandomReal[{0, 10}, 2], Automatic, 1], {75}]]ColoringCycles[n_] := Block[{i, e},
e = Table[Mod[#, n, 1]Mod[# + i, n, 1]& /@ Range[n], {i, (n - 1) / 2}];
HighlightGraph[CompleteGraph[n], Table[Style[e[[i]], ColorData[1][i]], {i, Length[e]}]]
]Table[ColoringCycles[i], {i, {3, 5, 7, 9, 11, 13}}]相关链接
文本
Wolfram Research (2010),CompleteGraph,Wolfram 语言函数,https://reference.wolfram.com/language/ref/CompleteGraph.html (更新于 2020 年).
CMS
Wolfram 语言. 2010. "CompleteGraph." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2020. https://reference.wolfram.com/language/ref/CompleteGraph.html.
APA
Wolfram 语言. (2010). CompleteGraph. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/CompleteGraph.html 年
BibTeX
@misc{reference.wolfram_2026_completegraph, author="Wolfram Research", title="{CompleteGraph}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/CompleteGraph.html}", note=[Accessed: 20-July-2026]}
BibLaTeX
@online{reference.wolfram_2026_completegraph, organization={Wolfram Research}, title={CompleteGraph}, year={2020}, url={https://reference.wolfram.com/language/ref/CompleteGraph.html}, note=[Accessed: 20-July-2026]}