Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved...

12
Design and Analysis of Algorithms - Chapter 9 1 Greedy algorithms Greedy algorithms Optimization problems Optimization problems solved through a sequence solved through a sequence of choices that are: of choices that are: feasible feasible locally optimal locally optimal irrevocable irrevocable Not all optimization problems can be Not all optimization problems can be approached in this manner! approached in this manner!

description

Design and Analysis of Algorithms - Chapter 93 Minimum Spanning Tree (MST) b Spanning tree of a connected graph G: a connected acyclic subgraph of G that includes all of G’s vertices. b Minimum Spanning Tree of a weighted, connected graph G: a spanning tree of G of minimum total weight. b Example:

Transcript of Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved...

Page 1: Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally.

Design and Analysis of Algorithms - Chapter 9 1

Greedy algorithmsGreedy algorithmsOptimization problemsOptimization problems solved through a sequence of choices solved through a sequence of choices

that are:that are: feasiblefeasible

locally optimallocally optimal

irrevocableirrevocable

Not all optimization problems can be approached in this Not all optimization problems can be approached in this manner!manner!

Page 2: Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally.

Design and Analysis of Algorithms - Chapter 9 2

Applications of the Greedy Applications of the Greedy StrategyStrategy Optimal solutions:Optimal solutions:

• change makingchange making• Minimum Spanning Tree (MST)Minimum Spanning Tree (MST)• Single-source shortest paths Single-source shortest paths • simple scheduling problemssimple scheduling problems• Huffman codesHuffman codes

Approximations:Approximations:• Traveling Salesman Problem (TSP)Traveling Salesman Problem (TSP)• Knapsack problemKnapsack problem• other combinatorial optimization problemsother combinatorial optimization problems

Page 3: Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally.

Design and Analysis of Algorithms - Chapter 9 3

Minimum Spanning Tree (MST)Minimum Spanning Tree (MST) Spanning treeSpanning tree of a connected graph of a connected graph GG: a connected acyclic : a connected acyclic

subgraph of subgraph of G G that includes all of that includes all of GG’s vertices.’s vertices.

Minimum Spanning TreeMinimum Spanning Tree of a weighted, connected graph of a weighted, connected graph GG: : a spanning tree of a spanning tree of GG of minimum total weight. of minimum total weight.

Example:Example: 3

42

14

26

1

3

Page 4: Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally.

Design and Analysis of Algorithms - Chapter 9 4

Prim’s MST algorithmPrim’s MST algorithm Start with tree consisting of one vertexStart with tree consisting of one vertex

““grow” tree one vertex/edge at a time to produce MSTgrow” tree one vertex/edge at a time to produce MST• Construct a series of expanding subtrees TConstruct a series of expanding subtrees T11, T, T22, …, …

at each stage at each stage construct Tconstruct Ti+1i+1 from T from Tii: add minimum weight : add minimum weight edge connecting a vertex in tree (edge connecting a vertex in tree (TTii) to one not yet in tree) to one not yet in tree• choose from “fringe” edges choose from “fringe” edges • (this is the “greedy” step!)(this is the “greedy” step!)

algorithm stops when all vertices are includedalgorithm stops when all vertices are included

Page 5: Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally.

Design and Analysis of Algorithms - Chapter 9 5

Examples:Examples:

3

42

14

26

1

3

a

edc

b1

5

24 6

3 7

Page 6: Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally.

Design and Analysis of Algorithms - Chapter 9 6

Notes about Prim’s algorithmNotes about Prim’s algorithm Need to prove that this construction actually yields MST Need to prove that this construction actually yields MST

Need priority queue for locating lowest cost fringe edge: Need priority queue for locating lowest cost fringe edge: use use min-heapmin-heap

Efficiency:Efficiency: For graph with For graph with n n vertices and vertices and mm edges: edges: ((nn – 1 + – 1 + mm) log ) log nn

ΘΘ((mm log log n)n)

number of stages(min-heap deletions)

number of edges considered(min-heap insertions)

insertion/deletion from min-heap

Page 7: Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally.

Design and Analysis of Algorithms - Chapter 9 7

Another Greedy algorithm for MST: Another Greedy algorithm for MST: KruskalKruskal Start with empty forest of treesStart with empty forest of trees ““grow” MST one edge at a timegrow” MST one edge at a time

• intermediate stages usually have forest of trees (not connected) intermediate stages usually have forest of trees (not connected) at each stage add minimum weight edge among those not at each stage add minimum weight edge among those not

yet used that does not create a cycle yet used that does not create a cycle • edges are initially sorted by increasing weightedges are initially sorted by increasing weight• at each stage the edge may:at each stage the edge may:

– expand an existing treeexpand an existing tree– combine two existing trees into a single treecombine two existing trees into a single tree– create a new treecreate a new tree

• need efficient way of detecting/avoiding cyclesneed efficient way of detecting/avoiding cycles algorithm stops when all vertices are includedalgorithm stops when all vertices are included

Page 8: Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally.

Design and Analysis of Algorithms - Chapter 9 8

Examples:Examples:

3

42

14

26

1

3

a

edc

b1

5

24 6

3 7

Page 9: Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally.

Design and Analysis of Algorithms - Chapter 9 9

Notes about Kruskal’s algorithmNotes about Kruskal’s algorithm Algorithm looks easier than Prim’s but is Algorithm looks easier than Prim’s but is

• harder to implement (checking for cycles!)harder to implement (checking for cycles!)• less efficient less efficient ΘΘ((mm log log mm))

Cycle checking:Cycle checking: a cycle exists iff edge connects vertices in a cycle exists iff edge connects vertices in the same component. the same component.

Union-find Union-find algorithms – see section 9.2algorithms – see section 9.2

Page 10: Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally.

Design and Analysis of Algorithms - Chapter 9 10

Shortest paths-Dijkstra’s Shortest paths-Dijkstra’s algorithmalgorithm Single Source Shotest Paths Problem:Single Source Shotest Paths Problem: Given a weighted graph G, find the Given a weighted graph G, find the

shortest paths from a source vertex s to each of the other vertices.shortest paths from a source vertex s to each of the other vertices.

Dijkstra’s algorithm: Similar to Prim’s MST algorithm, with the following Dijkstra’s algorithm: Similar to Prim’s MST algorithm, with the following difference:difference:• Start with tree consisting of one vertexStart with tree consisting of one vertex

• ““grow” tree one vertex/edge at a time to produce MSTgrow” tree one vertex/edge at a time to produce MST– Construct a series of expanding subtrees TConstruct a series of expanding subtrees T11, T, T22, …, …

• Keep track of shortest path from source to each of the vertices in TKeep track of shortest path from source to each of the vertices in T ii

• at each stage at each stage construct Tconstruct Ti+1i+1 from T from Tii: add minimum weight edge connecting a : add minimum weight edge connecting a vertex in tree (vertex in tree (TTii) to one not yet in tree) to one not yet in tree– choose from “fringe” edges choose from “fringe” edges – (this is the “greedy” step!)(this is the “greedy” step!)

• algorithm stops when all vertices are includedalgorithm stops when all vertices are included

edge (edge (v,wv,w) with lowest ) with lowest dd((s,vs,v) + ) + dd((v,wv,w))

Page 11: Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally.

Design and Analysis of Algorithms - Chapter 9 11

Example:Example:

a

edc

b1

5

24 6

3 7

Page 12: Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally.

Design and Analysis of Algorithms - Chapter 9 12

Notes on Dijkstra’s algorithmNotes on Dijkstra’s algorithm Doesn’t work with negative weightsDoesn’t work with negative weights

Applicable to both undirected and directed graphsApplicable to both undirected and directed graphs

Efficiency:Efficiency: