Editing Graph search

Jump to: navigation, search

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.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 117: Line 117:
 
               add (v,w) to F
 
               add (v,w) to F
 
</pre>
 
</pre>
Here, instead of simply pushing a vertex's neighbour onto the fringe ''F'', we push a pair containing the original vertex and the neighbour. That way, when this pair is removed from ''F'', we know both which vertex we wish to visit ''and'' where we "came from" (the ''predecessor''). We record the predecessor in the array ''pred[]''. After this algorithm has terminated, if ''t'' is in the same connected component as ''s'', then at some point ''t'' will have been visited, and at that time ''pred[t]'' will have been set to some vertex which has ''t'' as its neighbour. That vertex, in turn, must have been visited, so its predecessor was recorded also, and we can ultimately trace back the path to ''s''. (In this implementation, ''s'' is its own predecessor; this is not necessary however.) In other words, in the general case, ''s'', ''pred[pred[pred[...t...]]]'', ...,  ''pred[pred[pred[t]]]'', ''pred[pred[t]]'', ''pred[t]'', ''t'' will be a path from ''s'' to ''t'' after the algorithm has terminated.
+
Here, instead of simply pushing a vertex's neighbour onto the fringe ''F'', we push a pair containing the original vertex and the neighbour. That way, when this pair is removed from ''F'', we know both which vertex we wish to visit ''and'' where we "came from" (the ''predecessor''). We record the predecessor in the array ''visited[]''. After this algorithm has terminated, if ''t'' is in the same connected component as ''s'', then at some point ''t'' will have been visited, and at that time ''pred[t]'' will have been set to some vertex which has ''t'' as its neighbour. That vertex, in turn, must have been visited, so its predecessor was recorded also, and we can ultimately trace back the path to ''s''. (In this implementation, ''s'' is its own predecessor; this is not necessary however.) In other words, in the general case, ''s'', ''pred[pred[pred[...t...]]]'', ...,  ''pred[pred[pred[t]]]'', ''pred[pred[t]]'', ''pred[t]'', ''t'' will be a path from ''s'' to ''t'' after the algorithm has terminated.
  
 
As a matter of fact, this algorithm finds paths from ''s'' to ''all'' other vertices reachable from ''s''. We can replace ''t'' by any other vertex and use the ''pred[]'' array to find a path from ''s'' to this vertex. Since every vertex except for ''s'' has one unique ''parent'' (predecessor), the vertices form a [[tree]], with an edge between two vertices if and only if one is the other's predecessor when this algorithm is run. The root of the tree is the only vertex lacking a parent, or ''s''. The algorithm is said to produce a ''spanning tree'' of the connected component containing ''s'': a subgraph of that connected component which is also a tree.
 
As a matter of fact, this algorithm finds paths from ''s'' to ''all'' other vertices reachable from ''s''. We can replace ''t'' by any other vertex and use the ''pred[]'' array to find a path from ''s'' to this vertex. Since every vertex except for ''s'' has one unique ''parent'' (predecessor), the vertices form a [[tree]], with an edge between two vertices if and only if one is the other's predecessor when this algorithm is run. The root of the tree is the only vertex lacking a parent, or ''s''. The algorithm is said to produce a ''spanning tree'' of the connected component containing ''s'': a subgraph of that connected component which is also a tree.

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)