GraphUtilities`
GraphUtilities`

GraphDistanceMatrix

As of Version 10, all the functionality of the GraphUtilities package is built into the Wolfram System. »

GraphDistanceMatrix[g]

gives a matrix in which the (i,j)^(th) entry is the length of a shortest path in g between vertices i and j.

GraphDistanceMatrix[g,Parent]

returns a three-dimensional matrix in which the (1,i,j)^(th) entry is the length of a shortest path from i to j and the (2,i,j)^(th) entry is the predecessor of j in a shortest path from i to j.

更多信息和选项

范例

打开所有单元关闭所有单元

基本范例  (2)

This defines a simple directed graph:

This calculates the distance between the vertices:

This function has been superseded by GraphDistanceMatrix in the Wolfram System:

Scope  (1)

This defines a simple directed graph:

This calculates the distance between the vertices:

This shows also the predecessors in the shortest path:

The path from 1 to 3 is {1,5,4,3}:

This confirms the shortest path:

Options  (2)

Method  (2)

This defines a small graph:

Because of the negative edge weight, the Dijkstra algorithm cannot be applied:

Both the FloydWarshall and Johnson algorithms work:

This defines a small graph with a negative cycle:

The Dijkstra algorithm does not work for negative edge weights:

The FloydWarshall algorithm does not detect any negative weight cycle, and gives the wrong answer:

The Johnson algorithm detects a negative weight cycle:

The default algorithm for graphs with negative edge weights is Johnson:

Wolfram Research (2007),GraphDistanceMatrix,Wolfram 语言函数,https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html.

文本

Wolfram Research (2007),GraphDistanceMatrix,Wolfram 语言函数,https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html.

CMS

Wolfram 语言. 2007. "GraphDistanceMatrix." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html.

APA

Wolfram 语言. (2007). GraphDistanceMatrix. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html 年

BibTeX

@misc{reference.wolfram_2024_graphdistancematrix, author="Wolfram Research", title="{GraphDistanceMatrix}", year="2007", howpublished="\url{https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html}", note=[Accessed: 18-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_graphdistancematrix, organization={Wolfram Research}, title={GraphDistanceMatrix}, year={2007}, url={https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html}, note=[Accessed: 18-November-2024 ]}