0
3.7kviews
Write single source shortest path algorithm and apply the same for following.
1 Answer
0
28views

enter image description here

Ans:

Algorithm:

Algorithm

1) Create a set sptSet (shortest path tree set) that keeps track of vertices included in shortest path tree, i.e., whose minimum distance from source is calculated and finalized. Initially, this set is empty.

2) Assign a distance value to all vertices in the input graph. Initialize all distance values as INFINITE. Assign distance value as 0 for the source vertex so that it is picked first.

3) While sptSet doesn’t include all vertices

a) Pick a vertex u which is not there in sptSetand has minimum distance value.

b) Include u to sptSet.

c) Update distance value of all adjacent vertices of u. To update the distance values, iterate through all adjacent vertices. For every adjacent vertex v, if sum of distance value of u (from source) and weight of edge u-v, is less than the distance value of v, then update the distance value of v.

Problem:

Consider a graph as shown in the problem

We will start from source vertex a. Hence set

s[a]=1

now shortest distance from vertex a is 2. i.e. a->b=2. Hence {a,b} and min=2.

From vertex b next vertex chosen is e.

{a,b}=2

{a,c}=6

{a,d}=12

{a,e}=15

Now,

{a,b,c}=9

{a,b,d}=∞

{a,b,e}=5

Hence select e.

S[e]=1

Hence vertex e will be selected. In single source shortest path if destination vertex is e then we have achieved shortest path a-b-e with path length 5. The single source shortest path from each vertex is summarized below-

a,b 2
a,b,e 5
a,b,c 9
a,b,c,d 14
a,b,c,d,e 17
a,c 6
a,c,d 11
a,c,d,e 14
a,d 12
a,d,e 15
Please log in to add an answer.