Graph II - Shortest Paths, MST
Graph II - Shortest Paths, MST
Assume that you are given a function called 𝑑𝑖𝑗𝑘𝑠𝑡𝑟𝑎(𝐺, 𝑎) which takes a graph 𝐺, and a source
vertex 𝑎 as input, then finds the shortest path (distances, 𝑑𝑖𝑠𝑡 and parents, 𝑝𝑎𝑟) from 𝑎 to every
other vertex in the graph. Use this function to solve problems #1 and #2. Present your solution
idea with a pseudocode, accompanying necessary explanation.
You, and your friend Benji, live in a city which has 50 areas numbered from 1 to 50. Some of the
areas are connected with bi-directional roads of various lengths. Your house is in 1 and Benji’s
is in 50. Both of you want to meet. Now propose an algorithm to choose the meeting point that
minimizes the total travel time. In other words, if the meeting area you chose is 𝑋, it has the
minimum 𝑑𝑖𝑠𝑡(1, 𝑋) + 𝑑𝑖𝑠𝑡(50, 𝑋) among all possible area choices.
Expected time complexity: 𝑂(𝐸 * 𝑙𝑜𝑔2(𝑉))
Benji now has asked you to visit his house in area 50. He likes KFC, so you are thinking about
getting a bucket of fried chicken for him. The are KFC outlets available in 𝐾 different areas in the
city. You have to make a stop at one of them. Now propose an algorithm to choose the area
from the available 𝐾 options that minimizes your total travel time. In other words, if the area you
chose is 𝑋, it has the minimum 𝑑𝑖𝑠𝑡(1, 𝑋) + 𝑑𝑖𝑠𝑡(𝑋, 50) among all possible choices.
Expected time complexity: 𝑂(𝐸 * 𝑙𝑜𝑔2(𝑉))
Note: If you have to run Dijkstra’s algorithm from 𝐾 different sources, the complexity is
𝑂(𝐾 * 𝐸 * 𝑙𝑜𝑔2(𝑉)), which exceeds the expected complexity for this problem.
Problem #3: Dijkstra’s algorithm in a ‘negative’ weight graph!!
Draw a directed graph as described below.
b. Explain why the algorithm could not find the correct shortest path costs for some of the
vertices.
c. Imagine you have a modified version of Dijkstra’s algorithm with the following changes:
i. At the beginning, it inserts only the source vertex in the priority-queue.
ii. Whenever the distance of a vertex is updated, it inserts that vertex into the
priority-queue.
iii. It does not assume that extracting a vertex from the queue means that its
shortest path cost is finalized.
Will this modified Dijkstra’s algorithm be able to find the correct shortest path costs in the
graph given above? Explain with reasoning or a simulation.
d. Analyze the time-complexity of this modified algorithm.
Problem #4: A divide-and-conquer MST algorithm?!
Your friend Mriaslun loves the divide and conquer strategy! He recently invented a
divide-and-conquer algorithm to find the Minimum Spanning Tree of a given undirected
weighted graph. The algorithm is quite simple, just having 4 steps in it:
1. Divide the set of vertices into two equal halves (one side can have an extra, like we do in
merge sort). Assume that this divides the graph in three parts:
a. 𝐺𝑙𝑒𝑓𝑡 which contains all the vertices in the left half and the edges between them
b. 𝐺𝑟𝑖𝑔ℎ𝑡 containing the vertices in the right half, and the corresponding edges
c. 𝐺𝑟𝑒𝑚𝑎𝑖𝑛𝑠 containing the edges where one vertex is from the left half, and another one is
from the right half
2. Use a recursive approach (repeating steps 1 to 4) to find the MST of 𝐺𝑙𝑒𝑓𝑡, let’s call it 𝑀𝑆𝑇𝑙𝑒𝑓𝑡
3. Do the same to find 𝑀𝑆𝑇𝑟𝑖𝑔ℎ𝑡
4. Now you only need to select one edge from 𝐺𝑟𝑒𝑚𝑎𝑖𝑛𝑠 to connect 𝑀𝑆𝑇𝑙𝑒𝑓𝑡 to 𝑀𝑆𝑇𝑟𝑖𝑔ℎ𝑡. So you
pick the one with the lowest weight value and you have the MST of the original graph!
In 𝐺𝑙𝑒𝑓𝑡, we have three vertices {1, 2, 3} and three edges {(1, 2), (1, 3), (2, 3)}
In 𝐺𝑟𝑖𝑔ℎ𝑡, we have vertices {4, 5, 6} and edges {(4, 5), (4, 6), (5, 6)}
And 𝐺𝑟𝑒𝑚𝑎𝑖𝑛𝑠 is defined by the edges {(2, 4), (2, 5), (3, 4), (3, 5)}
If we can find 𝑀𝑆𝑇𝑙𝑒𝑓𝑡 and 𝑀𝑆𝑇𝑟𝑖𝑔ℎ𝑡 separately, then we can connect them with the minimum
weight edge from 𝐺𝑟𝑒𝑚𝑎𝑖𝑛𝑠, which is (2, 4). And we can use the same divide and conquer
approach recursively to know that 𝑀𝑆𝑇𝑙𝑒𝑓𝑡 consists of the edges {(1, 2), (2, 3)} and 𝑀𝑆𝑇𝑟𝑖𝑔ℎ𝑡
contains the edges {(4, 5), (5, 6)}.
So, the MST of the original graph is {(1, 2), (2, 3), (2, 4), (4, 5), (5, 6)} which is the correct
answer!
Now your job is to prove that this algorithm will not always give a correct answer. Provide
necessary reasonings to do that. You can also create an input graph and show that the
algorithm fails or provides a wrong answer for this input.
Examples:
Input Output
Notes: 3, 5, 8 Impossible
Amount to make: 7
END!