In the above graph S is the source node, Now let's implement Dijkstra's algorithm to find the shortest path.

Let's go through the order of implementation :

1. Initialize-Single-Source(G,s) is executed and all vertices are given initial d and pi values.

2. Queue Q now contains all vertices, S is assigned empty set.

3. Enters while loop

4. Minimum Vertex from the available vertices in Q is assigned to u.

5. d value is updated in Relax function. For d value to be updated the current d value must be greater than the new d value. New d value will be d[u] + w[u,v].

6. Repeat statements 4,5 until Q becomes empty.

The below table represents initial values of all vertices after step 1 and step 2 :

Let's go through the order of implementation :

1. Initialize-Single-Source(G,s) is executed and all vertices are given initial d and pi values.

2. Queue Q now contains all vertices, S is assigned empty set.

3. Enters while loop

4. Minimum Vertex from the available vertices in Q is assigned to u.

5. d value is updated in Relax function. For d value to be updated the current d value must be greater than the new d value. New d value will be d[u] + w[u,v].

6. Repeat statements 4,5 until Q becomes empty.

The below table represents initial values of all vertices after step 1 and step 2 :

The above table represents the values of the graph after the execution of the function Initialize-Single-Source(G,s). And the Queue determines the set Q ( Line 3 ) which is assigned all the vertices in the graph. Also s is added to the empty set **S.** Now in the while loop, u is assigned to the minimum value among all vertices in Q, s is the vertex with value 0 and all other are infinity, so u is assigned to vertex s.

Now from vertex s there are two outgoing edges ( A and C )

Now from vertex s there are two outgoing edges ( A and C )

For Vertex A Current Value = Infinity New Value = d[u] + w[u,v] = 0 + 10 = 10. Current Value is greater than New value. d[a] is updated to 10. Pi[a] will now be S | For Vertex C Current Value = Infinity New Value = d[u] + w[u,v] = 0 + 5 = 5. Current Value is greater than New value. d[c] is updated to 5. Pi[c] will now be S |

Since Dijkstra's follows the greedy approach we proceed to node with minimum distance and in our case vertex C has the minimum distance ( among remaining vertices A,B,C,D) hence now u will be assigned to vertex C. Vertex C is added to the set **S. **

Now from C we have 3 outgoing edges ( A, B & D ).

Now from C we have 3 outgoing edges ( A, B & D ).

For Vertex A Current Value = 10 New Value = d[u] + w[u,v] = 5 + 3 = 8. Current Value is greater than New value. d[a] is updated to 8. Pi[a] will now be C | For Vertex B Current Value = infinity New Value = d[u] + w[u,v] = 5 + 9 = 14. Current Value is greater than New value. d[b] is updated to 14. Pi[b] will now be C | For Vertex D Current Value = infinity New Value = d[u] + w[u,v] = 5 + 2 = 7. Current Value is greater than New value. d[d] is updated to 7. Pi[d] will now be C |

We now proceed to node with minimum distance, which right now is vertex D with distance 7 ( among vertices A,B & D). Now u will be assigned to vertex D and D is also added to the set **S. **

Now from vertex D we have two outgoing edges ( S and B ).

Now from vertex D we have two outgoing edges ( S and B ).

For Vertex S Current Value = 0 New Value = d[u] + w[u,v] = 7 + 7 = 14. Current Value is not greater than New value. d[s] will remain the same. | For Vertex B Current Value = 14 New Value = d[u] + w[u,v] = 7 + 6 = 13. Current Value is greater than New value. d[b] is updated to 13. Pi[b] will now be D |

Now we proceed to vertex A ( least among A and B ). Vertex A is now assigned to u and is also added to set S.

From vertex A we have two outgoing edges ( C and B ). d[c] cannot be updated because it is currently 5 and the new value will be ( 8 + 3 ) 11. Since 5 is less than 11 we cannot update d[c].

d[b] will now be ( 8 + 1 ) 9. Since 13 is greater than 9 it will be updated to 9. Similarly Pi[b] will now be updated to D. The new table will look like :

From vertex A we have two outgoing edges ( C and B ). d[c] cannot be updated because it is currently 5 and the new value will be ( 8 + 3 ) 11. Since 5 is less than 11 we cannot update d[c].

d[b] will now be ( 8 + 1 ) 9. Since 13 is greater than 9 it will be updated to 9. Similarly Pi[b] will now be updated to D. The new table will look like :

For Vertex C Current Value = 5 New Value = d[u] + w[u,v] = 8 + 3 = 11. Current Value is not greater than New value. d[c] will remain the same. | For Vertex B Current Value = 13 New Value = d[u] + w[u,v] = 8 + 1 = 14. Current Value is greater than New value. d[b] will be updated to 9. Pi[b] will be updated to A. |

Now we are only left with vertex B in the queue Q. we proceed to B, from B we have only one outgoing edge (D). D cannot be updated since its current value is not greater than the new value. The final tables will be :

**Q is now empty, so the loop terminates.**

Now the shortest path is Starting from S to C then to D via C and then to A via C and then to B via A.

Apply the Dijkstra's approach and find shortest path for the below graph.

Dijkstra's follows a greedy approach in order to calculate the shortest path. It is more efficient when compared with Bellman-Ford Algorithm. We shall assume that there are no negative weights in the graph. Dijkstra's algorithm maintains a set

This is how the algorithm works :

1. Algorithm selects a vertex

2. Adds the vertex

The above steps are executed repeatedly until it finds the shortest path.

Algorithm : First we will look at two helper functions and then the Dijkstra's algorithm itself.

This function is the first step of the Dijkstra’s algorithm, here for each vertex we will make shortest path ( Step 2 ) Infinity since we are yet to determine that. And since we do not know the path which we will take to reach target node, we will assign Parent node of each vertex as NIL ( Step 3).

The process of relaxing an edge (u,v) consists of testing whether we can improve the shortest path to **v** found so far by going through **u** and, if so, updating d[v] and Pi[v].

Line 1 initializes the values in the usual way as described earlier, and line 2 initializes the set S to empty. Line 3 initializes queue Q to all the vertices of the graph. Each time through the while loop lines 4 - 8, Line 5 extracts a vertex u from the Q and Line 6 adds it to the set S. ( The first time through this loop, u = s) Vertex u, therefore, has the smallest shortest-path estimate of any vertex in V - S. Then, lines 7–8 relax each edge (u,v) leaving u, thus updating the estimate d[v] and the predecessor pi[v] if we can improve the shortest path to v found so far by going through u. Observe that the algorithm never inserts vertices into Q after line 3 and that each vertex is extracted from Q and added to S exactly once, so that the while loop of lines 4–8 iterates exactly |V| times.

]]>Basic idea of DP is to :

1. Divide a problem into sub-problems

2. Solve those sub problems (save the solution for future reference)

3. Re-use those solutions.

This approach can be put to use when a given problem:

1. Holds the optimal substructure property

2. Can be divided into overlapping sub-problems ( sub-problems which tend to repeat ).

Since the solutions of sub-problems are saved, we can refer them instead of computing over and over again.

Let's better understand the concept by using Fibonacci program as an example. Given below is the algorithm:

Now let's draw a recursion tree for the above algorithm :

The highlighted sub-problems in the above tree overlap. Our algorithm recursively solves these sub problems, as in the above case F(n-3) and F(n-2) will be computed twice which is a waste of time. Applying DP approach to this problem would eliminate such unnecessary processing. When we encounter F(n-3) the second time we can simply refer to its solution computed earlier.

Dynamic Programming is very effective on such problems, as in the above case the running time of the algorithm is reduced from Exponential time to Linear time.

Dynamic Programming is very effective on such problems, as in the above case the running time of the algorithm is reduced from Exponential time to Linear time.

Modify the given Fibonacci algorithm by applying dynamic programming approach.

Hint: Use an array to save the solutions of all the sub-problems.

]]>Hint: Use an array to save the solutions of all the sub-problems.

(b) Imagine you were lost in a maze and had access to a graph traversal algorithm to help you get out of it. Which algorithm would be more useful, DFS or BFS? Justify your answer. (Note that the maze could be very large and the corresponding graph could have a very large number of nodes.)

The squares in the above diagram represent the junction points.

Now let us construct a graph out of the above diagram. For convenience I will be naming each junction.

Here ‘S’ stands for Start and ‘F’ stands for Finish. X,Y,Z,W,M,S,T,U,V represent vertices of dead ends.

**(B). **First let us have a look at how BFS and DFS solve the maze problem.

DFS :

➔ Depth First Search is a pretty good recursive search algorithm, it always takes the rightmost approach and if it encounters a dead end then it will backtrack until the first place it can go left or straight.

➔ The problem with DFS is that it takes a lot of space while solving the maze, the reason behind this is that for DFS you may have to keep the complete track in memory. When the maze is very large this can be an issue for using DFS to solve the maze.

➔ DFS will always find a solution in case of closed mazes, whereas in the case of open mazes DFS may go into infinite loop.

➔ For mazes with more than one solutions DFS does not always return the shortest path.

BFS :

➔ By definition Breadth first search will first check for all possible paths at a particular depth before moving to lower depth. So if a solution exists with length 10, BFS will find it before trying any solutions of depth 11.

➔ Contrary to DFS BFS does not have the issue with space since it does not need the complete track to be in memory. Also BFS can solve open mazes without going into a infinite loop.

➔ For mazes with more than one solutions BFS always returns the shortest path.

Conclusion:

When it comes to time complexity both BFS and DFS are pretty similar, I would go with BFS for solving mazes because it is better in terms of space complexity when compared with DFS. Also BFS will definitely return a solution in case of open maze whereas DFS may go into a infinite loop.

]]>DFS :

➔ Depth First Search is a pretty good recursive search algorithm, it always takes the rightmost approach and if it encounters a dead end then it will backtrack until the first place it can go left or straight.

➔ The problem with DFS is that it takes a lot of space while solving the maze, the reason behind this is that for DFS you may have to keep the complete track in memory. When the maze is very large this can be an issue for using DFS to solve the maze.

➔ DFS will always find a solution in case of closed mazes, whereas in the case of open mazes DFS may go into infinite loop.

➔ For mazes with more than one solutions DFS does not always return the shortest path.

BFS :

➔ By definition Breadth first search will first check for all possible paths at a particular depth before moving to lower depth. So if a solution exists with length 10, BFS will find it before trying any solutions of depth 11.

➔ Contrary to DFS BFS does not have the issue with space since it does not need the complete track to be in memory. Also BFS can solve open mazes without going into a infinite loop.

➔ For mazes with more than one solutions BFS always returns the shortest path.

Conclusion:

When it comes to time complexity both BFS and DFS are pretty similar, I would go with BFS for solving mazes because it is better in terms of space complexity when compared with DFS. Also BFS will definitely return a solution in case of open maze whereas DFS may go into a infinite loop.

corns

cores

cones

coney

money

Lets try to figure out a efficient algorithm to solve the above given algorithm. As per the given problem we have n words of length 5 each, and we would like to find a minimum number of one character changes that allow you to go from a "source word" to a "target word". In order to solve such a problem we can follow the below approach:

- Creating a graph would help us in solving this kind of a problem. Lets consider all n words and each word be a vertex and use these vertices to plot a graph.
- Once we have all the vertices, next we must join all the words that differ by just one character, for example lets consider the words “coins” and “corns” , “dizzy” and “Fizzy” such words differ from each other by a single character. These two words can be joined.
- Given source word S and a target word T we must now identify the vertices between both these words in the graph and now apply a shortest path algorithm to obtain minimum number of one character changes to go from source word to target word.

Below is a part of the graph that can be made out of the n words given.

In the above example we see the words which differ by single character are joined, Now if we consider the source word “COINS” and the target word “MONEY” then the solid line in the example determines the minimum number of one character changes required to go from source word to target word.

We can use the BFS algorithm to calculate the shortest path between source word and target word. This way we will always get an optimal solution as first we are joining words which differ by a single character and then we are calculating the shortest path between two words (Source & Target Words).

Time Complexity Analysis:

Total Time taken = Time to Compare & Join words differing by single character + time taken by BFS algorithm to calculate the shortest path.

= O(N^2 ) + O(|V| + |E|)

~= O(N^2 )

Thereby the algorithm takes O(N^2 ) time to execute.

]]>We can use the BFS algorithm to calculate the shortest path between source word and target word. This way we will always get an optimal solution as first we are joining words which differ by a single character and then we are calculating the shortest path between two words (Source & Target Words).

Time Complexity Analysis:

- Let us know analyse the running time of the above specified algorithm. Here we have considered each of the n words as a separate vertex.
- Now for joining words which differ by only one single character the algorithm will have to compare each word with other, this will take O(N^2 ) time.
- Once the vertices are joined we will now have to find the shortest path between source word and target word using BFS algorithm, The worst case time complexity of BFS algorithm is O(|V| + |E|).

Total Time taken = Time to Compare & Join words differing by single character + time taken by BFS algorithm to calculate the shortest path.

= O(N^2 ) + O(|V| + |E|)

~= O(N^2 )

Thereby the algorithm takes O(N^2 ) time to execute.

Example : input 345 , output must be 346.

Today I came across a problem wherein I was asked to increment any integer without using any arithmetic operators. Below is the explanation and java code to implement such a problem.

Here we will be utilizing the XOR and & bit wise operators. First lets look at XOR and & operators truth tables given below:

Below is the java code to solve the problem.

Lets go through the code using a simple example shown below. Our core implementation starts with the invocation of method calc. Suppose lets take the input value to be 7.

7 is represented as 0000 0111. Output must be 8 which is represented as 0000 1000.

Please follow the below images to understand the working or feel free to comment and i will try to answer your query.

]]>7 is represented as 0000 0111. Output must be 8 which is represented as 0000 1000.

Please follow the below images to understand the working or feel free to comment and i will try to answer your query.