


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























Point-to-point Shortest Path Problem^ t^
s
3
2
Dijkstra’s Idea
s^
x^
y tentative d[y]
length(x,y) settled d[x]
Q priority queue
settled nearest unsettled neighbor of x
1.^ Shortest distance from s to all nodes initially “unsettled”.2.^ Shortest distance to s is zero. Tentative distance to others is
∞.
3.^ Put all nodes in queue ordered by tentative distance from s.4.^ Take out nearest unsettled node, x. Settle its distance from s.5.^ For each unsettled immediate neighbor y of x6.^ If going from s to y through x is shorter than shortest path throughsettled nodes, update tentative distance to y.7.^ Repeat from step 4, until distance to destination is settled.
To extract path
1.^ trace back-links from destination tosource, reversing them as we go2.^ traverse reversed links from source todestination, to obtain a shortest paths^
t
back
back
back
s^
t
back
back
back
To get minimal spanning tree• Run until all nodes are settled• Reverse all links
Movement Rules
From block x:• x mod n = 0
→^ may move N or S
→^ may move NE or SW
→^ may move E or W
→^ may move NW or SE
Example for n=4 :
Movement rules
x mod n = 0
→^ may move N or S
→^ may move NE or SW
→^ may move E or W
→^ may move NW or SE
x-n x x+n x-n+1x x+n-1x^
x+ x-1 x-n-1x x+n+
Edge representation
for (int x = 0; x < V; x++) {switch (x % n) {case 0:if (x-n >= 0) neighbor[x][0] = x-n; // Nif (x+n < V) neighbor[x][1] = x+n;
// S
break;case 1:if ((x-n >= 0) && (x % n < n-1))neighbor[x][0] = x-n+1; // NEif ((x+n < N) && (x % n > 0))neighbor[x][1] = x+n-1; // SW...etc.}}
Setting upneighborarray.
Alternatives
How to represent distances?• int d[] = int[V];• How to represent
How to represent Q?
takes less time to code– no debugging time– if you know how to use it!