Docsity

Analog and digital C - Greedy Algorithm - Single source shortest path, Study notes of Digital & Analog Electronics

Partial preview of the text

Download Analog and digital C - Greedy Algorithm - Single source shortest path and more Study notes Digital & Analog Electronics in PDF only on Docsity!

Greedy Algorithm

Single Source Shortest Path

Single-Source Single-Destination

Shorted Path

  • (^) Possible greedy algorithm

 Leave the source vertex using the cheapest edge

 Leave the current vertex using the cheapest edge to the

next vertex

 Continue until destination is reached

 Try Shortest 1 to 7 Path by this Greedy Algorithm

 the algorithm does not guarantee the optimal solution

1

2

3

4

5

6

7

2

6

16

7

10

3

4

4

5 3

1

Single-Source Single-Destination

Shorted Path

4

1

2

3

4

5

6 2

6

16

7

8

10

3

14

4

4

5 3

1

Lengt

h

1

0

1 3 2

1 3 5

5

1 2

6

1 3 9

5 4

1 3 1

0

6

1 3 1

1

6 7

 Each path (other than first) is a one

edge extension of a previous path

 Next shortest path is the shortest

one edge extension of an already

generated shortest path

Increasing

order

Greedy Single-Source All-Destinations Shortest

Path (3)

  • Data Structures - (^) Let d(i) (distanceFromSource(i)) be the length of a shortest

one edge extension of an already generated shortest path,

the one edge extension ends at vertex i

  • (^) The next shortest path is to an as yet unreached vertex for

which the d() value is least

  • Let p(i) (predecessor(i)) be the vertex just before vertex i on

the shortest one edge extension to i

5

Greedy Single Source All Destinations :

Example (2)

1

2

3

4

5

6

7

[1] [2] [3] [4] [5] [6] [7]

d

p

1

1 3

2

5

6

Greedy Single Source All Destinations :

Example (3)

1

2

3

4

5

6

7

[1] [2] [3] [4] [5] [6] [7]

d

p

1

1 3

1 3 5

4 7

Greedy Single Source All Destinations :

Example (5)

1

2

3

4

5

6

7

[1] [2] [3] [4] [5] [6] [7]

d

p

1

1 3

1 3 5

1 2

1 3 5 4

7

Greedy Single Source All Destinations :

Example (6)

1

2

3

4

5

6

7

[1] [2] [3] [4] [5] [6] [7]

d

p

1 3 6

7