Given an undirected graph G={E, V} with positive edge costs C (i,j) E E, ie V, j
ID: 3874654 • Letter: G
Question
Given an undirected graph G={E, V} with positive edge costs C (i,j) E E, ie V, je V) and given a vertex V', v' e V, describe an efficient algorithm to determine if v' is NOT on any shortest path for which it is not an endpoint (obviously v' is on the shortest path if it is the destination). That is, v, i,j e V and izv' and j#V', determine v' ¢ shortest_path(i, j). Note, if your algorithm is not the most efficient, but is still correct, you will still get partial credit. What is the worst-case runtime of your algorithm in terms of |E| and |?Explanation / Answer
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.
TIME COMPLEXITY:-
Notes:
1) The code calculates shortest distance, but doesn’t calculate the path information. We can create a parent array, update the parent array when distance is updated (like prim’s implementation) and use it show the shortest path from source to different vertices.
2) The code is for undirected graph, same dijkstra function can be used for directed graphs also.
3) The code finds shortest distances from source to all vertices. If we are interested only in shortest distance from source to a single target, we can break the for loop when the picked minimum distance vertex is equal to target (Step 3.a of algorithm).
4) Time Complexity of the implementation is O(V^2). If the input graph is represented using adjacency list, it can be reduced to O(E log V) with the help of binary heap.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.