## Sunday, August 25, 2013

### Dijkstra's algorithm for equal weight edges using BFS

This is the application of BFS (breadth first search)

dist(v) = fewest number of edges on path s to v.

Here is the pseudocode:
Initialize dist(v) = {0 if v=s, infinity if v != s}
```foreach vertex v in all vertices
{
if(v==s)
dist(v) = 0;
else
dist(v) = infinity;
}
```

Now apply BFS, and hen considering edge (v, w), if w is unexplored, then set dist(w) = dist(v)+1, keeping rest of the BFS same.

Note - BFS computes the shortest path, only when the length of all the edge is same (say 1)

Consider the graph:
Initially, dist(s) = 0 and rest of the nodes have dist = infinity.

So, applying bfs now, where we find a , we add to queue, mark dist(a) = dist(s)+1 = 0+1 = 1.
So, dist(v)=1. Now we add b, and do the same. So dist(b) = 1.

Now we deque a, and add c. While adding c, we will find dist(c) = dist(a) +1, so dist (c) and so on.

In order to retrieve the shortest path from the origin to a node, you need to maintain two items for each node in the graph: it's current shortest distance, and the preceding node in the shortest path. Initially all distances are set to infinity, and all predecessors are set to empty. Consider following example:
A - B - C
|           |
D          E

In this example, we set A's distance to zero, and then proceed with the BFS. On each step you check if you can improve the distance of a descendant, i.e. the distance from the origin to the predecessor plus the length of the edge that you are exploring is less than the current best distance for the node in question. If you can improve the distance, set the new shortest path, and remember the predecessor through which that path has been acquired. When the BFS queue is empty, pick a node (in your example, it's E) and traverse its predecessors back to the origin. This would give you the shortest path.

Here is the pseudo code of what we talked about:
```function Dijkstra(Graph, source):
for each vertex v in Graph:                                // Initializations
dist[v] := infinity ;                                  // Unknown distance function from
// source to v
previous[v] := undefined ;                             // Previous node in optimal path
end for                                                    // from source

dist[source] := 0 ;                                        // Distance from source to source
Q := the set of all nodes in Graph ;                       // All nodes in the graph are
// unoptimized – thus are in Q
while Q is not empty:                                      // The main loop
u := vertex in Q with smallest distance in dist[] ;    // Source node in first case
remove u from Q ;
if dist[u] = infinity:
break ;                                            // all remaining vertices are
end if                                                 // inaccessible from source

for each neighbor v of u:                              // where v has not yet been
// removed from Q.
alt := dist[u] + dist_between(u, v) ;
if alt < dist[v]:                                  // Relax (u,v,a)
dist[v] := alt ;
previous[v] := u ;
decrease-key v in Q;                           // Reorder v in the Queue
end if
end for
end while
return dist;
endfunction
```

For detailed algorithm of dijkstra's algorithm, please refer here.