# Greedy Algorithms - PowerPoint PPT Presentation

View by Category
Title:

## Greedy Algorithms

Description:

### Correctness of Dijkstra's Algorithm ... As with Dijkstra's algorithm, the key issue is determining which vertex is next closest. ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 25
Provided by: wasfiga
Category:
Tags:
Transcript and Presenter's Notes

Title: Greedy Algorithms

1
Greedy Algorithms
• Like dynamic programming algorithms, greedy
algorithms are usually designed to solve
optimization problems
• Unlike dynamic programming algorithms,
• greedy algorithms are iterative in nature.
• An optimal solution is reached from local optimal
solutions.
• This approach does not work all the time.
• A proof that the algorithm does what it claims is
needed, and usually not easy to get.

2
Shortest Paths Problems
• Input A graph with non-negative weights or costs
associated with each edge.
• Output The list of edges forming the shortest
path.
• Sample problems
• Find shortest path between two named vertices
• Find shortest path from S to all other vertices
• Find shortest path between all pairs of vertices
• Will actually calculate only distances, not paths.

3
Shortest Paths Definitions
• ?(A, B) is the shortest distance from vertex A to
B.
• length(A, B) is the weight of the edge connecting
A to B.
• If there is no such edge, then length(A, B) ?.

8
A
C
1
10
5
B
D
7
4
Single-Source Shortest Paths
• Problem Given G(V,E), start vertex s, find the
shortest path from s to all other vertices.
• Assume V1, 2, , n and s1
• Solution A greedy algorithm called Dijkstras
Algorithm

5
Dijkstras Algorithm Outline
• Partition V into two sets
• X1 and Y 2, 3, , n
• Initialize ?i for 1 ? i ? n as follows
• Select y?Y such that ?y is minimum
• ?y is the length of the shortest path from 1 to
y that uses only vertices in set X.
• Remove y from Y, add it to X, and update ?w for
each w?Y where (y,w) ? E if the path through y is
shorter.

6
Example
5
6
1
2
11
2
3
15
7
Dijkstras Algorithm

A B C D E
Initial 0 ? ? ? ?
Process A
Process
Process
Process
Process
8
Dijkstras Algorithm
• Input A weighted directed graph G (V,E), where
V 1,2,,n
• Output The distance from vertex 1 to every other
vertex in G.
• X 1 Y 2,3,,n ?10
• for y 2 to n do
• if y is adjacent to 1 then ?ylength1,y
• else ?y ? end if
• end for
• for j 1 to n 1 do
• Let y ? Y be such that ?y is minimum
• X X ?? y // add vertex y to X
• Y Y ?- y // delete vertex y from Y
• for each edge (y,w) do
• if w ? Y and ?y lengthy,w lt ?w then
• ?w ?y lengthy,w
• end for
• end for

9
Correctness of Dijkstras Algorithm
• Lemma In Dijkstras algorithm, when a vertex y
is chosen in Step 7, if its label ?y is finite
then ?y ?? y.
• Proof

10
Time Complexity
• Mainly depends on how we implement step 7, i.e.,
finding y s.t. ?y is minimum.
• Approach 1 Scan through the vector representing
current distances of vertices in Y
• Approach 2 Use a min-heap to maintain vertices
in the set Y

11
Minimum Cost Spanning Trees
• Minimum Cost Spanning Tree (MST) Problem
• Input An undirected weighted connected graph G.
• Output The subgraph of G that
• 1) has minimum total cost as measured by
summing the weights of all the edges in the
subset, and
• 2) keeps the vertices connected.
• What does such subgraph look like?

12
MST Example
5
2
1
4
11
2
3
2
13
Kruskals Algorithm
• Initially, each vertex is in its own MST.
• Merge two MSTs that have the shortest edge
between them.
• Use a priority queue to order the unprocessed
edges. Grab next one at each step.
• How to tell if an edge connects two vertices that
are already in the same MST?
• Use the UNION/FIND algorithm with parent-pointer
representation.

14
Example
5
2
1
3
11
2
3
2
15
Kruskals MST Algorithm
• Sort the edges of E(G) by weight in
non-decreasing order
• For each vertex v ? V(G) do
• New_Tree(v) // creating tree with one root
node v
• T? // MST initialized to empty
• While T lt n - 1 do
• Let (u,v) be the next edge in E(G)
• if FIND(u) ? FIND(v) then
• T T ? (u,v)
• UNION(u,v)
• Return T

16
Asymptotic Analysis of Kruskals Algorithm
17
Correctness of Kruskals Algorithm
• Lemma Algorithm Kruskal correctly finds a
minimum cost spanning tree in a weighted
undirected graph.
• Proof
• Theorem Algorithm Kruskals finds a minimum cost
spanning tree in a weighted undirected graph in
O(m log m) time, where m is the number of edges
in G.

18
Prims Algorithm
• Very similar to Dijkstras algorithm
• Grows a minimum spanning tree from an arbitrary
vertex u ? V

19
Idea of Prims Algorithm
8
4
9
6
7
Y
X
20
Idea of Prims Algorithm (Cont.)
8
4
9
6
7
Y
X
21
Prims Algorithm
1. T ?? X1 Y V(G) 1
2. while Y ? ? do
3. Let (x,y) be of minimum weight such that x
? X and y ? Y.
4. X X ? y
5. Y Y y
6. T T ? (x,y)
7. end while

22
Example
5
2
1
3
11
2
3
2
23
Prims MST Implementation
• As with Dijkstras algorithm, the key issue is
determining which vertex is next closest.
• As with Dijkstras algorithm, the alternative is
to use a priority queue (min-heap).
• Running times for the two implementations are
identical to the corresponding Dijkstras
algorithm implementations.
• For implementation details of the corresponding
algorithms, check the book pages 245 and 247.

24
Correctness of Prims Algorithm
• Lemma Algorithm Prim correctly finds a minimum
cost spanning tree in a connected undirected
graph.
• Proof