Ex.
No:10                           IMPLEMENTATION OF DIJKSTRA ALGORITHM AND DYNAMIC
Date:                                                         PROGRAMMING
        Aim:
                (a) To write a program to implement Dijkstra’s algorithm for finding the
                      shortest path.
                (b) To write a program to find the Longest Common Subsequence using
                      dynamic programming
        Algorithm:
            Dijkstra’s Algorithm:
                1. Get the input graph using adjacency matrix
                2. Select the source and destination vertex
                3. Apply Dijkstra algorithm and finalize the vertices with minimum cost.
                4. Output the path and its minimum distance
             Dynamic Programming:
                 i.   Initialize a 2D array dp of size (m+1) x (n+1) with all values as 0, where m =
                      len(X) and n = len(Y).
                ii.   Fill the table:
               iii.   If X[i-1] == Y[j-1], then dp[i][j] = dp[i-1][j-1] + 1
               iv.    Else, dp[i][j] = max(dp[i-1][j], dp[i][j-1])
                v.    The final answer will be dp[m][n], i.e., the bottom-right cell.
        PSEUDOCODE:
            Dijkstra’s Algorithm:
        function dijkstra(G, S)
          for each vertex V in G
            distance[V] <- infinite
            previous[V] <- NULL
            If V != S, add V to Priority Queue Q
          distance[S] <- 0
          while Q IS NOT EMPTY
            U <- Extract MIN from Q
   for each unvisited neighbour V of U
     tempDistance <- distance[U] + edge_weight(U, V)
     if tempDistance < distance[V]
       distance[V] <- tempDistance
       previous[V] <- U
 return distance[], previous[]
PROGRAM:
OUTPUT
Result:
Thus the program to implement Dijkstra’s algorithm to find the shortest path of a graph
and to implement Dynamic programming to determine the Longest Common
Subsequence are executed and the outputs are verified.