1 greedy 2 jose rolim university of geneva. algorithmique greedy 2jose rolim2 examples greedy ...

53
1 Greedy 2 Jose Rolim University of Geneva

Post on 18-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

1

Greedy 2

Jose RolimUniversity of Geneva

Algorithmique Greedy 2

Jose Rolim 2

Examples Greedy

Minimum Spanning Trees Shortest Paths Dijkstra

Algorithmique Greedy 2

Jose Rolim 3

Definition MST

Given a connected graph G = (V, E), with weight function w : E --> R

Min-weight connected subgraph Spanning tree T:

A tree that includes all nodes from V T = (V, E’), where E’ E Weight of T: W( T ) = w(e)

Minimum spanning tree (MST): A tree with minimum weight among all

spanning trees

Algorithmique Greedy 2

Jose Rolim 4

Example:

Algorithmique Greedy 2

Jose Rolim 5

MST:

MST for given G may not be unique

Since MST is a spanning tree: # edges : |V| - 1

If the graph is unweighted: All spanning trees have same weight

Algorithmique Greedy 2

Jose Rolim 6

Kruskal’s algorithm (Greedy)

Start with A empty, and each vertex being its own connected component

Repeatedly merge two components by connecting them with a light edge crossing them

Two issues: Maintain sets of components

Choose light edges

Algorithmique Greedy 2

Jose Rolim 7

Pseudo-code

Algorithmique Greedy 2

Jose Rolim 8

The Greedy Algorithm in Action

1

2

3

4

5

6

7

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

7

Algorithmique Greedy 2

Jose Rolim 9

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

7

Algorithmique Greedy 2

Jose Rolim 10

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

7

Algorithmique Greedy 2

Jose Rolim 11

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

7

Algorithmique Greedy 2

Jose Rolim 12

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

7

Algorithmique Greedy 2

Jose Rolim 13

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

7

Algorithmique Greedy 2

Jose Rolim 14

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

7

Algorithmique Greedy 2

Jose Rolim 15

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

7

Algorithmique Greedy 2

Jose Rolim 16

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

7

Algorithmique Greedy 2

Jose Rolim 17

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

7

Algorithmique Greedy 2

Jose Rolim 18

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

7

Algorithmique Greedy 2

Jose Rolim 19

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

7

Algorithmique Greedy 2

Jose Rolim 20

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

7

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

Node 1 2 3 4 5 6 7

First 1 2 3 4 5 4 7

35

10

30

15

25

40

20 17

8

15

11

211

2

3 5 7

root node

4 66

21

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

Node 1 2 3 4 5 6 7

First 1 4 3 4 5 4 7

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

7

2

22

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

Node 1 2 3 4 5 6 7

First 1 4 3 4 5 4 5

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

77

23

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

Node 1 2 3 4 5 6 7

First 1 4 5 4 5 4 5

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

773

24

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

Node 1 2 3 4 5 6 7

First 1 4 4 4 4 4 4

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

75 73 5 73

25

The Greedy Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

Node 1 2 3 4 5 6 7

First 4 4 4 4 4 4 4

35

10

30

15

25

40

20 17

8

15

11

211

2

3

4

5

6

75 73

1

26

Algorithmique Greedy 2

Jose Rolim 27

Complexity

Time complexity: #make-set, find-set and union

operations: O(|V| + |E|) • O( (|V| + |E|) (|V| + |E|))

Sorting:• O(|E| log |E|) = O(|E| log |V|)

Total: • O(|E| log |V|)

Algorithmique Greedy 2

Jose Rolim 28

Prim’s algorithm (also Greedy)

Start with an arbitrary node from V

Instead of maintaining a forest, grow a MST At any time, maintain a MST for V’ V

At any moment, find a light edge connecting V’ with (V-V’) I.e., the edge with smallest weight connecting

some vertex in V’ with some vertex in V-V’ !

Algorithmique Greedy 2

Jose Rolim 29

Issues:

Again two issues: Maintain the tree already build at any

moment• Easy: simply a tree rooted at r : the starting

node

Find the next light edge efficiently• For v V - V’, define key(v) = the min

distance between v and some node from V’• At any moment, find the node with min key.

Algorithmique Greedy 2

Jose Rolim 30

Pseudo-code

Algorithmique Greedy 2

Jose Rolim 31

Prim’s Algorithm in Action

1

2

3

35

104

5

30

15

25

40

20

6

7

17

8

15

1121

4

5

6

7

1

2

3

The minimum cost arc from yellow nodes to green nodes can be found by placing arc values in a priority queue.

Algorithmique Greedy 2

Jose Rolim 32

Prim’s Algorithm in Action

1

3

354

5

30

15

25

40

20

6

7

17

8

15

1121

4

5

6

7

1

3522

10

25

102

3

Algorithmique Greedy 2

Jose Rolim 33

20

Prim’s Algorithm in Action

1

3

354

515

25

40

6

7

17

15

11

1

3522

10

25

102 4

10 8

2130

8

20 3021

5

6

73

4

Algorithmique Greedy 2

Jose Rolim 34

20

Prim’s Algorithm in Action

1

3

354

515

25

40

6

7

17

15

11

1

3522

10

25

102 4

10 8

2130

8

20 3021

68

17

15

5 73

64

Algorithmique Greedy 2

Jose Rolim 35

20

Prim’s Algorithm in Action

1

3

354

515

25

40

6

7

17

15

11

1

3522

10

25

102 4

10 8

2130

8

20 3021

68

17

1564

15

515

1173 5

Algorithmique Greedy 2

Jose Rolim 36

20

Prim’s Algorithm in Action

1

3

354

515

25

40

6

7

17

15

11

1

3522

10

25

102 4

10 8

2130

8

20 3021

68

17

1564

15

515

1173 5

Algorithmique Greedy 2

Jose Rolim 37

20

Prim’s Algorithm in Action

1

3

354

515

25

40

6

7

17

15

11

1

3522

10

25

102 4

10 8

2130

8

20 3021

68

17

1564

15

515

113 75

117

Algorithmique Greedy 2

Jose Rolim 38

20

Prim’s Algorithm in Action

1

3

354

515

25

40

6

7

17

15

11

1

3522

10

25

102 4

10 8

2130

8

20 3021

68

17

1564

15

515

117

1173 5153

Algorithmique Greedy 2

Jose Rolim 39

20

Prim’s Algorithm in Action

1

3

354

515

25

40

6

7

17

15

11

1

3522

10

25

102 4

10 8

2130

8

20 3021

68

17

1564

15

515

117

1173 5153

Algorithmique Greedy 2

Jose Rolim 40

20

Prim’s Algorithm in Action

1

3

354

515

25

40

6

7

17

15

11

1

3522

10

25

102 4

10 8

2130

8

20 3021

68

17

1564

15

515

117

1173 5153

Algorithmique Greedy 2

Jose Rolim 41

Complexity

Time complexity # insert:

• O(|V|)

# Decrease-Key: • O( |E|)

# Extract-Min• O( |V| )

Using heap for priority queue: Each operation is O ( log |V| )

Total time complexity: O (|E| log |V|)

Algorithmique Greedy 2

Jose Rolim 42

Single-Source Shortest Paths

Problem Definition Shortest paths Dijkstra’s algorithm (can be viewed

as a greedy algorithm)

Algorithmique Greedy 2

Jose Rolim 43

Problem Definition: Real problem: A motorist wishes to find the

shortest possible route from Chicago to Boston.Given a road map of the United States on which the distance between each pair of adjacent intersections is marked, how can we determine this shortest route?

Formal definition: Given a graph G=(V, E, W), where each edge has a weight, find a shortest path from s to v for some interesting vertices s and v.

s—source v—destination.

Algorithmique Greedy 2

Jose Rolim 44

Shortest path: The weight of path p=<v0,v1,…,vk > is

the sum of the weights of its constituent edges:

k

i

ii vvwpw1

1 ),()(

The cost of the shortest path from s to v is denoted as (s, v).

Algorithmique Greedy 2

Jose Rolim 45

Representing shortest paths:

we maintain for each vertex vV , a predecessor

[ v] that is the vertex in the shortest path

right before v. With the values of , a backtracking

process can give the shortest path. (We will discuss that after the algorithm is given)

Algorithmique Greedy 2

Jose Rolim 46

Dijkstra’s Algorithm:

Dijkstra’s algorithm assumes that w(e)0 for each e in the graph.

maintain a set S of vertices such that Every vertex v S, d[v]=(s, v), i.e., the shortest-path

from s to v has been found. (Intial values: S=empty, d[s]=0 and d[v]=)

(a) select the vertex uV-S such that d[u]=min {d[x]|x V-S}. Set S=S{u} (b) for each node v adjacent to u do RELAX(u,

v, w). Repeat step (a) and (b) until S=V.

Algorithmique Greedy 2

Jose Rolim 47

Implementation:

a priority queue Q stores vertices in V-S, keyed by their d[] values.

the graph G is represented by adjacency lists.

Algorithmique Greedy 2

Jose Rolim 48

0

105

2

1

3 4

2

6

97

s

u v

x y8 8

8 8

(a)

Algorithmique Greedy 2

Jose Rolim 49

0

5/s

10/s

105

2

1

3 4

2

6

97

s

u v

x y8

8

(b)(s,x) is the shortest path using one edge. It is also the shortest path from s to x.

Algorithmique Greedy 2

Jose Rolim 50

0

7/x

14/x

5/s

8/x

105

2

1

3 4

2

6

97

s

u v

x y

(c)

Algorithmique Greedy 2

Jose Rolim 51

0

7/x

13/y

5/s

8/x

105

2

1

3 4

2

6

97

s

u v

x y

(d)

Algorithmique Greedy 2

Jose Rolim 52

0

7/x

9/u

5/s

8/x

105

2

1

3 4

2

6

97

s

u v

x y

(e)

Algorithmique Greedy 2

Jose Rolim 53

Time complexity of Dijkstra’s Algorithm:

Time complexity: #Extract-min: O(|V|) #Decrease-key = #Relax: O(|E|) Use binary heap for priority heap:

• Time complexity: O((|V| + |E|) log |V|) Use Fibonacci heap for priority heap:

• Amortized time complexity: O(|V| log |V| + |E|)

Greedy algorithm: Any moment, select the lightest

vertex in V-S to add to set S