Shortest paths & Weighted graphs Based on Data Structures, Algorithms & Software Principles in C T. A. Standish, A-W (Pearson), 1998
Terms Weighted graph Weighted adjacency matrix Numerical values attached to edges are called weights Represent: costs, distances, times, etc. Weighted adjacency matrix A row/col intersection (edge exists from A-> B) contains the weight of the edge (vs a 1/0 of a non-weighted matrix) A row/col intersection for a non-existent edge contains a value ≡ ∞
Usage Distance on a map (GPS implications) Cost to fly a plane Computer network shortest # hops or delay time Social media: degree of separation Arbitrage (currency exchange) convert (USD->EUR->CNY->USD) 1000 US$ to ₠ 810 (*.81) ₠ 810 to 6261.3 CNY (*7.73, 4/22/'18) 6261.3 CNY back to 1001.808 US$ (rate=.16) Profit of 1.80 USD (known as) in 1 msec (find a negative cycle)
Weighted Di-Graph & it's Adj1 Shortest path A→D, is: ACEFD To 3 B A B C D E F ∞ 9 3 5 4 7 8 1 6 9 8 4 D 5 C 6 From 7 F 1 E
Finding the shortest path Easy if all weights are equal (simple BFS) Dijkstra's algorithm - shortest path for ONE pair See pg 426 for example and proof of correctness O(V2), but O(V+ E* log V) if a min-priority queue is used Floyd-Warshall algorithm - shortest path for ALL pairs O(V3) See https://www.hackerearth.com/practice/algorithms/graphs/shortest-path-algorithms/tutorial/