• Tidak ada hasil yang ditemukan

Finding the Fastest Route in Graphs

N/A
N/A
Sohuturon Fernando

Academic year: 2024

Membagikan " Finding the Fastest Route in Graphs"

Copied!
19
0
0

Teks penuh

(1)

DIJKSTRA’S

SINGLE SOURCE SHORTEST PATH

Informatics Department

Parahyangan Catholic University

(2)

SHORTEST PATH

Given a graph G, a source vertex s, and a destination vertex d. Find a shortest path between s and d.

Unweighted Graph

Can be solved using simple BFS from s

Weighted Graph

Bellman-Ford Algorithm (single source)

Dijkstra’s Algorithm (no negative cycle)

Floyd-Warshall Algorithm (all-pairs shortest path)

etc.

(3)

SINGLE SOURCE SHORTEST PATH

Given a graph G and a source vertex s. Find the shortest path from s to all other vertices.

Can solve one-pair shortest path problem.

Very similar to Prim’s MST Algorithm

(4)

RECALL, PRIM’S MST

At each step, a light edge is added to the tree A that connects A to an isolated vertex

A

b

d a

c 14

8 5

22

Light edge ≡ edge with smallest weight that connects A

to an isolated vertex

(5)

DIJKSTRA’S ALGORITHM

Maintain a set A of vertices whose shortest path (from source vertex s) is already known

a

b

d c

s

4 8

2 3

(6)

DIJKSTRA’S ALGORITHM

On each iteration, pick a vertex u  S with the smallest shortest path estimate

b

d

a

c

14

8 22

5 a

b

d c

s

4 8

2 3

sse = 2+22 = 24

sse = 3+8 = 11

sse = 8+5 = 13 sse = 4+14 = 18

(7)

SHORTEST PATH ESTIMATE

d

8

5 a

b c

s

8

2 3

4

sse is calculated as minimum of

•2+4 = 6

3+8 = 11

8+5 = 13

= 6

sse is calculated as minimum of

•2+4 = 6

3+8 = 11

8+5 = 13

= 6

(8)

EXAMPLE

a 0

b c

i

h g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

(9)

EXAMPLE

a 0

b

4 c

i

h

8 g f

d

e 8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

(10)

EXAMPLE

a 0

b 4

c 12

i

h

8 g f

d

e

8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

distance(a,h)via b is 4+11, which is worse than directly a-h (8)

(11)

EXAMPLE

a 0

b 4

c 12

i 15

h 8

g

9 f

d

e

8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

(12)

EXAMPLE

a 0

b 4

c 12

i 15

h 8

g 9

f 11 d

e

8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

distance(a,i) via g is 9+6, which is equally good as via h (8+7)

(13)

EXAMPLE

a 0

b 4

c 12

i 15

h 8

g 9

f 11 d 25

e 21

8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

(14)

EXAMPLE

a 0

b 4

c 12

i 14

h 8

g 9

f 11 d 19

e 21

8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

distance(a,i) via c is 12+2, which is better than via h (15) distance(a,d) via c is 12+7, which is better than via f (25)

(15)

EXAMPLE

a 0

b 4

c 12

i 14

h 8

g 9

f 11 d 19

e 21

8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

(16)

EXAMPLE

a 0

b 4

c 12

i 14

h 8

g 9

f 11 d 19

e 21

8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

distance(a,e) via d is 19+9, which is worse than via f (21)

(17)

EXAMPLE

a 0

b 4

c 12

i 14

h 8

g 9

f 11 d 19

e 21

8

8

11 4

7

4 2

7 6

1 2

14

9

10

A

(18)

PSEUDOCODE

1. Dijkstra-SSSP(G, w, s)

2. for each u V do

3. dist[u] =

4. parent[u] = NIL

5. dist[s] = 0

6. Q = all vertices of G

7. While Q do

8. u = EXTRACT-MIN(Q)

9. for each v adjacent to u do

10. if vQ and dist[u]+w(u,v)<dist[v] then

11. parent[v] = u

12. dist[v] = dist[u]+w(u,v)

Initialization :

Set all vertices’ dist to 

except the source, so that it will be the first vertex processed.

•Parent of each vertex is set to NIL.

Min-priority queue Q contains all vertices.

(19)

IMPLEMENTATION

The performance of Dijkstra’s Algorithm depends on how we implement the min- priority queue Q (very similar analysis to Prim’s MST)

Suppose we implement Q using a min-heap:

Store vertices’ id on the heap’s array

Store vertices’ dist on a separate array

Store vertices’ location on a separate array

Additionally, we need an array parent to store the resulting tree

Referensi

Dokumen terkait

These are finally used to reformulate the solutions in terms of partition functions for weighted paths on graphs, the weights being entirely expressed in terms of the initial

Finally, combined with the recent black-box reduction of Stubbs and Vassilevska Williams [51] from the weighted to the unweighted matching problem, our algorithms also yield the

Omoomi, “On the locating chromatic number of Kneser graphs,” Discrete Applied Mathematics: The Journal of Combinatorial Algorithms, Informatics and Computational Sciences,

Little bit History: Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph , which may represent, for example, road networks.. It was conceived by

This phase of our simulation includes three different path finding algorithms for solving the MUPFP: • A constraint satisfaction problem CSP formulation with a branch and bound

Cite this Article: Achmad Fitro, Otong Saeful Bachri, Arif Ilham Sulistio Purnomo and Indra Frendianata, Shortest Path Finding in Geographical Information Systems using Node Combination

The document discusses two sorting algorithms, Bubble Sort and Maximum Sort, and demonstrates their implementation in Pascal programming

The document discusses the design and analysis of algorithms for priority queues, a fundamental data structure in computer