Greedy algorithms - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Greedy algorithms

Description:

... problems solved through a sequence of choices that are: feasible. locally optimal. irrevocable. Not all optimization problems can be approached in this manner! ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 16
Provided by: john1391
Category:

less

Transcript and Presenter's Notes

Title: Greedy algorithms


1
Greedy algorithms
  • Optimization problems solved through a sequence
    of choices that are
  • feasible
  • locally optimal
  • irrevocable
  • Not all optimization problems can be approached
    in this manner!

2
Applications of the Greedy Strategy
  • Optimal solutions
  • Change making
  • Minimum Spanning Tree (MST)
  • Single-source shortest paths
  • Simple scheduling problems
  • Huffman codes
  • Approximations
  • Traveling Salesman Problem (TSP)
  • Knapsack problem
  • Other combinatorial optimization problems

3
Minimum Spanning Tree (MST)
  • Spanning tree of a connected graph G a connected
    acyclic subgraph of G that includes all of Gs
    vertices.
  • Minimum Spanning Tree of a weighted, connected
    graph G a spanning tree of G of minimum total
    weight.
  • Example

4
Prims MST algorithm
  • Start with tree consisting of one vertex
  • Grow tree one vertex/edge at a time to produce
    MST
  • Construct a series of expanding subtrees T1, T2,
  • At each stage construct Ti1 from Ti add minimum
    weight edge connecting a vertex in tree (Ti) to
    one not yet in tree
  • choose from fringe edges (this is the greedy
    step)
  • Algorithm stops when all vertices are included

5
Examples

6
Prims algorithm formally
  • Algorithm Prim(G)
  • VT ? v0
  • ET ?
  • for i ? 1 to V-1 do
  • find a minimum weight edge e(v,u) among
  • all the edges (v,u) such that v ? VT and u ?
    V-VT
  • VT ? VT ? u
  • ET ? ET ? e
  • return ET

7
Notes about Prims algorithm
  • Need to prove that this construction actually
    yields MST
  • Need priority queue for locating lowest cost
    fringe edge use min-heap
  • Efficiency For graph with n vertices and m
    edges (n1m) log n
  • T(m log n)

insertion/deletion from min-heap
number of stages (min-heap deletions)
number of edges considered (min-heap insertions)
8
Another Greedy algorithm for MST Kruskal
  • Start with empty forest of trees
  • Grow MST one edge at a time
  • intermediate stages usually have forest of trees
    (not connected)
  • at each stage add minimum weight edge among those
    not yet used that does not create a cycle
  • edges are initially sorted by increasing weight
  • at each stage the edge may
  • expand an existing tree
  • combine two existing trees into a single tree
  • create a new tree
  • need efficient way of detecting/avoiding cycles
  • algorithm stops when all vertices are included

9
Examples

10
Kruskals algorithm
  • Algorithm Kruskal(G)
  • Sort E in nondecreasing order of the weights
  • ET ? k ? 0 ecounter ? 0
  • while ecounter lt V-1
  • k ? k1
  • if ET ? eik is acyclic
  • ET ? ET ? eik
  • ecounter ? ecounter 1
  • return ET

11
Notes about Kruskals algorithm
  • Algorithm looks easier than Prims but is
  • harder to implement (checking for cycles!)
  • less efficient T(m log m)
  • Cycle checking a cycle exists iff edge connects
    vertices in the same component.
  • Union-find algorithms see section 9.2

12
Shortest paths-Dijkstras algorithm
  • Single Source Shortest Paths Problem Given a
    weighted graph G, find the shortest paths from a
    source vertex s to each of the other vertices.
  • Doesnt work with negative weights
  • Applicable to both undirected and directed graphs

13
Shortest paths-Dijkstras algorithm
  • Dijkstras algorithm Similar to Prims MST
    algorithm, with the following difference
  • Start with tree consisting of one vertex
  • grow tree one vertex/edge at a time to produce
    MST
  • Construct a series of expanding subtrees T1, T2,
  • Keep track of shortest path from source to each
    of the vertices in Ti
  • at each stage construct Ti1 from Ti add minimum
    weight edge connecting a vertex in tree (Ti) to
    one not yet in tree
  • choose from fringe edges
  • (this is the greedy step!)
  • algorithm stops when all vertices are included

edge (v,w) with lowest d(s,v) d(v,w)
14
Example

15
Dijkstras algorithm
  • Algorithm Dijkstra(G,s)
  • Initialize(Q)
  • for every vertex v ? V do
  • dv?8 pv?null Insert(Q,v, dv)
  • ds?0 Decrease(Q,s,ds) VT?0
  • for i?0 to V-1 do
  • u?DeleteMin(Q) VT ? VT ? u
  • for every vertex u in V-VT that is adjacent to
    u do
  • if duw(u,u)ltdu
  • du?duw(u,u) pu?u Decrease(Q,u,du)
  • Efficiency
Write a Comment
User Comments (0)
About PowerShow.com