Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Mathematics and Algorithms > Graphs & Networks > Paths and Cycles > FindShortestPath >
Mathematica > Visualization and Graphics > Graphs & Networks > Paths and Cycles > FindShortestPath >

FindShortestPath

FindShortestPath
finds the shortest path from source vertex s to target vertex t in the graph g.
FindShortestPath[g, s, All]
generates a ShortestPathFunction[...] which can be applied repeatedly to different t.
FindShortestPath[g, All, t]
generates a ShortestPathFunction[...] which can be applied repeatedly to different s.
FindShortestPath[g, All, All]
generates a ShortestPathFunction[...] which can be applied to different s and t.
  • For an unweighted graph, edge length is assumed to be 1.
  • For a weighted graph, edge length is taken to be the weight.
  • A Method option can also be given. Possible Method settings include:
"BellmanFord"supports positive and negative weights
"Dijkstra"supports positive weights
Find a shortest path between two individual vertices in a graph:
Highlight the path:
Find a shortest path between two individual vertices in a graph:
In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
Highlight the path:
In[3]:=
Click for copyable input
Out[3]=
FindShortestPath works with undirected graphs:
Directed graphs:
Weighted graphs:
The shortest path has the smallest total edge weight:
Find the shortest paths from one vertex to all vertices:
Apply the ShortestPathFunction to every vertex of the graph:
Find the shortest paths to one vertex from all vertices:
Apply the ShortestPathFunction to every vertex of the graph:
Works with large graphs:
The method is automatically chosen depending on input:
method will use the weight 1 for every edge:
can be used for graphs with positive edge weights only:
Find the shortest path in a tree:
Find the shortest path along the seams of a football:
Create a maze by random depth-first search of a grid graph:
Restore vertex coordinates:
Solve the maze:
The distance between two vertices can be found using the shortest path:
New in 8
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team
Format:   HTML  |  CDF