Consider each node with a tag associated as (flag, previous,
distance); where flag is true if the node is made permanent else false,
previous indicates the name of the previous node and distance is the distance
of the current node from the start node.
Algorithm 1 (To set the
traversal path and find minimum distance)
- Set the
tags of all the nodes as (F, -, infinity) i.e.
F -> permanent false(dash - ) -> No previous node anddistance is infinity
- Accept the
start node and set the tag for the start node as (T, -, 0) i.e.
permanentNo previous node anddistance is 0
- Set the tags of all reachable nodes from start node; previous node is start node and distance from the start node
- Find a node with minimum distance and permanent = false; Note: don’t compare with nodes having distance infinity
- If no such node exists then go to step 9
- Mark this node permanent i.e. set the permanent flag true
- Find all
reachable nodes from this node
Dist = this.distance + distance of reachable node from this nodeIf distance of the new node is infinity thenSet the distance = DistSet the previous node = thisElseIf (reachable.distance>Dist) thenSet the distance = DistSet the previous node = thisEnd IfEnd If
- Go to Step 4
- Print traversal path and distance of each node from start node
- Stop
Algorithm 2 (Print
traversal path and distance of each node from start node)
- Take a node from the tree
- Print the distance value from tag
- Print current nod name
- If distance=0 then go to step 7
- Current = Current.previous
- Go to step 3
- Take next node
- If node exists then go to step 2
- Stop
No comments:
Post a Comment
Your comments are very much valuable for us. Thanks for giving your precious time.