Assignment 4

3
CS502 - Fundamentals of Algorithms Student ID: MC120402396 Assignment 4 Answer: Dijkstra’s Algorithm The Dijkstra’s Algorithm works in very smartest way, taking an example of graph for finding the shortest path. Considering an example of there are multiple nodes drawn from one node at an end to another at distant indirectly linked position. Using Dijkstra’s Algorithm, first it visits every neighbor node & identify the distance from starting node ‘s’ to other linked nodes, & Assigns a Value or Label ‘v’ to every node that is equal to distance from starting node. The number iterations are ‘n’ i.e. unlimited. After visiting all the vertices the algorithm stops then assigns a label to visited nodes e.g. v (Node Value) = s (starting node value) + l (Distance) In the next step the algorithm chooses the node which has the smallest value at its label but it is still not final unless it reaches the targeted point. After that the algorithm will select the neighbor nodes of smallest value node. Now again for each neighbor node it will identify distance l from the starting node s again. Again it will select the smallest value up to the neighbor nodes. The system will repeat the same step ‘n’ unless the neighbor nodes are visited & then algorithm will stop.

Transcript of Assignment 4

Page 1: Assignment 4

CS502 - Fundamentals of Algorithms

Student ID: MC120402396

Assignment 4

Answer:

Dijkstra’s Algorithm

The Dijkstra’s Algorithm works in very smartest way, taking an example of graph for finding the

shortest path. Considering an example of there are multiple nodes drawn from one node at an end to

another at distant indirectly linked position. Using Dijkstra’s Algorithm, first it visits every neighbor

node & identify the distance from starting node ‘s’ to other linked nodes, & Assigns a Value or Label ‘v’

to every node that is equal to distance from starting node. The number iterations are ‘n’ i.e. unlimited.

After visiting all the vertices the algorithm stops then assigns a label to visited nodes e.g.

v (Node Value) = s (starting node value) + l (Distance)

In the next step the algorithm chooses the node which has the smallest value at its label but it is still

not final unless it reaches the targeted point. After that the algorithm will select the neighbor nodes of

smallest value node. Now again for each neighbor node it will identify distance l from the starting node

s again. Again it will select the smallest value up to the neighbor nodes. The system will repeat the

same step ‘n’ unless the neighbor nodes are visited & then algorithm will stop.

Floyd-Warshall Algorithm

Floyd-Warshall algorithm is a dynamic programming algorithm that is used to find the shortest paths

among all pairs of nodes in a graph, it does not support any negative length cycles. The main benefit of

Floyd-Warshall algorithm is its simplicity. It majorly used to compare multiple available options

Have an example of a graph, where vertices / nodes were numbered from 1 to n. Notation d ijk means

the shortest path from i to j that also passes through vertex/node k.

The Algorithm compares the two conditions:

1. If shortest path from i to j does not pass through the vertex k then value of d ijk will be equal to

dijk-1.

Page 2: Assignment 4

2. If shortest path from i to j crosses over through the vertex k. In this case the value of d ijk will be

equal to dikk-1 + dkjk-1.

The base case of this formula is the following:

Shortest Path (i, j, 0) = w( i, j) to calculate the the length of edge between vertices i and j

The recursive case of the formula is:

Shortest Path (i, j, k + 1) = min (Shortest Path (i, j, k), Shortest Path (i, k + 1, k) + Shortest Path (k + 1, j, k))

Bellman-Ford algorithm

Bellman-Ford algorithm is an algorithm designed to search the entire shortest paths in a graph from

one source to all other vertices / nodes. In this algorithm the graph does not support any cycles of

negative length, but if it does, the algorithm will detect it.

The algorithm works in a way that it generates multiple numbers of solution paths from source node to

destination node, after running every cycle it the array stores that minimal distance from starting point

to other vertices. After every run it replaces the value stored in array if found less than previous value.