Better time complexity solution to (ninja prob) "Road Network"

#1

book-given solution is here (ver 1.2.1: prob 16.11/ ver 1.5.3: prob 22.49): https://github.com/epibook/epibook.github.io/blob/master/solutions/java/src/main/java/com/epi/RoadNetwork.java

i have my alternative solution coded up and tested using the above test code, but it’s going to be way easier to understand just explaning the algorithm, because it’s rather simple.

the book-given solution is using floyd-warshall whose time complexity (as described in book) is O(n^3).
the book on the side also mentions single-source dijkstra has time complexity of O(n lg n + m).

for alternative solution, you only need to run dijkstra twice.

  1. with source == el paso
  2. with source == corpus christi // valid because graph is undirected.

(side-note: if the graph is directed (it’s not), we can still use dijkstra’s algorithm for #2; just temporarily reverse the edges when computing #2 which would turn single-destination problem back to single-source shortest path prob)

running dijkstra twice still has time complexity of O(2(n lg n + m)) -> O(n lg n + m).

with that, for each proposed highway section ( cityA, cityB, distance),

// pseudo-code
for (HighwaySection hs : sections) {
    proposal1 = hs.cityA.minDistanceElPaso + hs.distance + hs.cityB.minDistanceCorpus;
    proposal2 = hs.cityB.minDistanceElPaso + hs.distance + hs.cityA.minDistanceCorpus;
    proposal = min(proposal1, proposal2);

    if (proposal < minProposal) {
        minProposal = proposal;
        minProposalSection = hs;
    }
}

and at the end, return minProposalSection.
that loop should have time compelexity of k (# of proposals)

so overall time complexity is,

O (n lg n + m + k); // k <= n(n-1) / 2

which should be better than book-given solution complexity of

O (n^3 + k); // == O(n^3)

let me know if there was something wrong in my time complexity analysis… at least, i implemented the entire thing, and ran the test code and it always matched the given solution.

0 Likes

#2

no, you are absolutely right. this was pointed out to us recently by another reader via email, and is in our TODO list. you are only the second person to report this, impressive, given that there’s literally tens of thousands of people who’ve see it.

could you send me a link to your solution? i’d like to compare it to what we were planning to code up,

cheers,
adnan

0 Likes

#3

Hi, i uploaded my solution here:

https://onedrive.live.com/redir?resid=B787781D45385038!19975&authkey=!AFuXOVP4xZ5j71o&ithint=file%2Cjava

i stripped off all test-related code to make it short. if you wish to test my code using the same test code as the one in this website, some conversion is needed because im using different data types (e.g. Highway vs HighwaySection); i can provide that separately too if needed to test the code.

0 Likes

#4

we thought more about the problem, here’s our rephrased version:

Write a program which takes the existing highway network
(specified as a set of highway sections between pairs of cities) and proposals for new highway
sections, and returns the proposed highway section which leads to the most improvement
in the total driving distance. The total driving distance is defined to be the sum of
the shortest path distances between all pairs of cities.
All sections, existing and proposed, allow for bi-directional traffic,
and that the original network is connected.

The reason we chose to update the problem in this way is that it really does better with a floyd-warshall approach, as compared to dijskstra. We already have a problem that uses dijkstra, and we wanted something that shows the power of floyd-warshall.

Let me know your thoughts, and thanks for your inputs, esp the sharing of the code.

bye,
adnan

0 Likes

#5

Yes, I like the rephrased version. :smile:

0 Likes

#6

The book states “Let S(u, v) be the 2D array of shortest path distances for each pair of cities. Each proposal p is a pair of cities (x,y). The best we can do by using proposal p is min(S(a,b), S(a,x) + d(x,y) + S(y,b)) where d(x,y) is the distance of the proposed highway p between x and y and a and b are El Paso and Corpus Christi, respectively”.

I am not sure why the above explanation did not mention “S(a,y) + d(x,y) + S(x,b)” though it is used in the code following the explanation.

0 Likes