Docsity

Dijkstra's Algorithm for Single-Source Shortest Path Problem | CIS 4900, Study notes of Computer Science

Partial preview of the text

Download Dijkstra's Algorithm for Single-Source Shortest Path Problem | CIS 4900 and more Study notes Computer Science in PDF only on Docsity!

Dijkstra’s Algorithm forSingle-Source Shortest Path

Problem

Dijkstra’Dijkstra

’ss Algorithm forAlgorithm for

Single-Single

-Source Shortest PathSource Shortest Path

ProblemProblem

Programming Puzzles and CompetitionsProgramming Puzzles and Competitions

CIS 4900 / 5920CIS 4900 / 5920Spring 2009Spring 2009

Outline

• Dijkstra’s algorithm• How to code it in Java• An application to a problem on theFSU ACM spring 2009 programmingcontest

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

  • x mod n = 1

→^ may move NE or SW

  • x mod n = 2

→^ may move E or W

  • x mod n = 3

→^ may move NW or SE

Example for n=4 :

Movement rules

x mod n = 0

→^ may move N or S

  • x mod n = 1

→^ may move NE or SW

  • x mod n = 2

→^ may move E or W

  • x mod n = 3

→^ may move NW or SE

x-n x x+n x-n+1x x+n-1x^

x+ x-1 x-n-1x x+n+

Designing a Java implementation• How to represent nodes?class? – too cumbersome for time limitso, use integers 0 .. V-1, for V = n * n• How to represent edges?• How to represent distance?• How to implement Q?

Edge representation

  • Adjacency list is most efficient• Avoids looking at non-edges• Reduces from O(V

2 logV) to O(ElogV)

  • How to implement an adjacency list?

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

  • array of arrays– saves -1 check, but need code to createsub-array of correct length• implicit representation, using afunction (or iterator)– e.g. int neighbor(x,i){ ...}– maybe a good idea, but estimate ofcoding time seems greater

How to represent distances?• int d[] = int[V];• How to represent

for (i=0; i < V; i++)d[i] = Integer.MAX_VALUE– watch out for overflow later!

How to represent Q?

A. Roll your own priority queue?B.^ Use Java utility library?–^

takes less time to code– no debugging time– if you know how to use it!

http://java.sun.com/javase/6/docs/api/java/util/PriorityQueue.html