Assignment 10
Assignment 10
5. Write your name, roll number, and assignment number at the beginning of your program.
7. Submit your program on Moodle before deadline. Submissions by email or any other means will
NOT be considered for evaluation.
You are free to use any code from Internet/book/friend for part I of today’s assignment. Stan-
dard penalty of plagiarism does not apply for part I. However, you must write the other parts
without any external help. Hence, needless to say that standard penalty for plagiarism applies
there. In this assignment, assume all weights are positive integers only. However, you are free to use
float/double variables if you want.
Part I
Write a function which implements the Dijkstra’s algorithm for the single-source shortest path problem
for directed graphs.
Part II
Consider an undirected graph with both vertex and edge weights. In this graph, the cost of a path is the
sum of the weights of the edges plus the product of the weights of the vertices in the path. For example,
P Qk
the cost of a path x0 , x1 , . . . , xk is k−1
i=0 w(xi , xi+1 ) +
0
i=0 w (xi ). Use your function from part I to find
a single-source shortest path lengths in this undirected graph.
Submit one single C/C++ code.
Sample Output
Write the number of vertices in the undirected graph: 5
Write -1 to indicate the end of neighbors
Write neighbors of vertex 1: 2 5 -1
1
Write neighbors of vertex 2: 1 3 4 5 -1
Write neighbors of vertex 3: 2 4 5 -1
Write neighbors of vertex 4: 2 3 -1
Write neighbors of vertex 5: 1 2 3 -1
Weight of the edge {1,2}: 6
Weight of the edge {1,5}: 2
Weight of the edge {2,3}: 5
Weight of the edge {2,4}: 9
Weight of the edge {2,5}: 3
Weight of the edge {3,4}: 2
Weight of the edge {3,5}: 6
Weight of the vertex 1: 1
Weight of the vertex 2: 2
Weight of the vertex 3: 3
Weight of the vertex 4: 1
Weight of the vertex 5: 2
Write source vertex: 1
Distance of vertex 2 from vertex 1: 8
Distance of vertex 3 from vertex 1: 14
Distance of vertex 4 from vertex 1: 17
Distance of vertex 5 from vertex 1: 4