The graph has 7 vertices namely v1, v2, v3, v4, v5, v6, v7. v1 has an outgoing edge to…
- User
The graph has 7 vertices namely v1, v2, v3, v4, v5, v6, v7. v1 has an outgoing edge to v2 at a distance of 4 and outgoing edge to v6 at a distance of 10. v2 has an outgoing edge to v1 at a distance of 3 and outgoing edge to v4 at a distance of 18. v3 has an outgoing edge to v2 at a distance of 6. v4 has an outgoing edge to v2 at a distance of 5, outgoing edge to 3 at a distance of 15, outgoing edge to v5 at a distance of 2, outgoing edge to v6 at a distance of 19 and outgoing edge to v7 at a distance of 5. v5 has an outgoing edge to v3 at a distance of 12 and outgoing edge to v4 at a distance of 1. v6 has an outgoing edge to v7 at a distance of 10. v7 has an outgoing edge to v4 at a distance of 8.
Analyze the Print Shortest Path algorithm: path(index q, r) if (P[ q, r ]!=0) path(q, P[q, r]) println( “v”+ P[q, r]) path(P[q, r], r) return;
//no intermediate nodes else return
• If D[q, r] < ∞, print node q and call path(…) • After returning from path(…), print node r
Input size is the number of vertices in the graph i.e. 7.
D matrix = [ 0, 4, 36, 22, 24, 10, 20] [ 3, 0, 32, 18, 20, 13, 23] [ 9, 6, 0, 24, 26, 19, 29] [ 8, 5, 14, 0, 2, 18, 5] [ 9, 6, 12, 1, 0, 19, 6] [ 26, 23, 32, 18, 20, 0, 10] [ 16, 13, 22, 8, 10, 26, 0]
P matrix = [ 0, 0, 5, 2, 4, 0, 6] [ 0, 0, 5, 0, 4, 1, 4] [ 2, 0, 0, 2, 4, 1, 4] [ 2, 0, 5, 0, 0, 1, 0] [ 2, 4, 0, 0, 0, 1, 4] [ 2, 4, 5, 7, 4, 0, 0] [ 2, 4, 5, 0, 4, 1, 0]
edges = [ (0, 1, 4), (0, 5, 10), (1, 0, 3), (1, 3, 18), (2, 1, 6), (3, 1, 5), (3, 2, 15), (3, 4, 2), (3, 5, 19), (3, 6, 5), (4, 2, 12), (4, 3, 1), (5, 6, 10), (6, 3, 8) ]
Show that the algorithm has a linear-time complexity.
Hint: You can consider each array access to P[i][j] as a basic operation.
- Assistant