Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Topological sorting
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Application to shortest path finding== The topological ordering can also be used to quickly compute [[shortest path problem|shortest paths]] through a [[Weighted graph|weighted]] directed acyclic graph. Let {{mvar|V}} be the list of vertices in such a graph, in topological order. Then the following algorithm computes the shortest path from some source vertex {{mvar|s}} to all other vertices:{{r|CLRS}} <div style="margin-left: 35px; width: 600px"> {{framebox|blue}} * Let {{mvar|d}} be an array of the same length as {{mvar|V}}; this will hold the shortest-path distances from {{mvar|s}}. Set {{math|1=''d''[''s''] = 0}}, all other {{math|1=''d''[''u''] = β}}. * Let {{mvar|p}} be an array of the same length as {{mvar|V}}, with all elements initialized to {{mono|nil}}. Each {{math|''p''[''u'']}} will hold the predecessor of {{math|''u''}} in the shortest path from {{mvar|s}} to {{mvar|u}}. * Loop over the vertices {{mvar|u}} as ordered in {{mvar|V}}, starting from {{mvar|s}}: ** For each vertex {{mvar|v}} directly following {{mvar|u}} (i.e., there exists an edge from {{mvar|u}} to {{mvar|v}}): *** Let {{mvar|w}} be the weight of the edge from {{mvar|u}} to {{mvar|v}}. *** Relax the edge: if {{math|''d''[''v''] > ''d''[''u''] + ''w''}}, set **** {{math|''d''[''v''] β ''d''[''u''] + ''w''}}, **** {{math|''p''[''v''] β ''u''}}. {{frame-footer}} </div> Equivalently: <div style="margin-left: 35px; width: 600px"> {{framebox|blue}} * Let {{mvar|d}} be an array of the same length as {{mvar|V}}; this will hold the shortest-path distances from {{mvar|s}}. Set {{math|1=''d''[''s''] = 0}}, all other {{math|1=''d''[''u''] = β}}. * Let {{mvar|p}} be an array of the same length as {{mvar|V}}, with all elements initialized to {{mono|nil}}. Each {{math|''p''[''u'']}} will hold the predecessor of {{mvar|u}} in the shortest path from {{mvar|s}} to {{mvar|u}}. * Loop over the vertices {{mvar|u}} as ordered in {{mvar|V}}, starting from {{mvar|s}}: ** For each vertex {{mvar|v}} into {{mvar|u}} (i.e., there exists an edge from {{mvar|v}} to {{mvar|u}}): *** Let {{mvar|w}} be the weight of the edge from {{mvar|v}} to {{mvar|u}}. *** Relax the edge: if {{math|''d''[''u''] > ''d''[''v''] + ''w''}}, set **** {{math|''d''[''u''] β ''d''[''v''] + ''w''}}, **** {{math|''p''[''u''] β ''v''}}. {{frame-footer}} </div> On a graph of {{mvar|n}} vertices and {{mvar|m}} edges, this algorithm takes {{math|Ξ(''n'' + ''m'')}}, i.e., [[Linear time|linear]], time.{{r|CLRS}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)