Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Advertisements

Greedy Algorithms Greed is good. (Some of the time)
Greed is good. (Some of the time)
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Minimum Spanning Tree (MST) form a tree that connects all the vertices (spanning tree). minimize the total edge weight of the spanning tree. Problem Select.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Introduction to Algorithms 6.046J/18.401J/SMA5503
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
Chapter 23 Minimum Spanning Trees
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Minimum Spanning Tree Algorithms
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Shortest Path Problems
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
Analysis of Algorithms
Lecture 1: The Greedy Method 主講人 : 虞台文. Content What is it? Activity Selection Problem Fractional Knapsack Problem Minimum Spanning Tree – Kruskal’s Algorithm.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
 2004 SDU Lecture 7- Minimum Spanning Tree-- Extension 1.Properties of Minimum Spanning Tree 2.Secondary Minimum Spanning Tree 3.Bottleneck.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
1 Greedy Algorithms and MST Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Introduction to Algorithms Jiafen Liu Sept
November 13, Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
Finding Minimum Spanning Trees Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Chap 23 – Minimum.
Lecture 13 Algorithm Analysis
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
 2004 SDU Lecture 6- Minimum Spanning Tree 1.The Minimum Spanning Tree Problem 2.Greedy algorithms 3.A Generic Algorithm 4.Kruskal’s Algorithm.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Greedy Algorithms General principle of greedy algorithm
Lecture ? The Algorithms of Kruskal and Prim
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Algorithms and Data Structures Lecture XIII
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Data Structures – LECTURE 13 Minumum spanning trees
CS 583 Analysis of Algorithms
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Lecture 13 Algorithm Analysis
Minimum Spanning Trees
Greedy Algorithms Comp 122, Spring 2004.
Introduction to Algorithms: Greedy Algorithms (and Graphs)
Lecture 12 Algorithm Analysis
Minimum Spanning Trees
Presentation transcript:

Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1

Lecture 3: Greedy Algorithms 0. Introduction 1. Minimum spanning trees 2. Single-source shortest paths 3* Huffman codes

0. Introduction Algorithms for optimization problems typically go through a sequence of steps, with a set of choices at each step. For many optimization problems, using dynamic programming to determine the best choices is overkill; simpler, more efficient algorithms will do. A greedy algorithm always makes the choice that looks best at the moment. That is, it makes a locally optimal choice in the hope that this choice will lead to a globally optimal solution.

Strategy Design greedy algorithms according to the following sequence of steps: 1.Cast the optimization problem as one in which we make a choice and are left with one sub problem to solve. 2.Prove that there is always an optimal solution to the original problem that makes the greedy choice, so that the greedy choice is always safe. 3.Demonstrate that, having made the greedy choice, what remains is a sub problem with the property that if we combine an optimal solution to the sub problem with the greedy choice we have made, we arrive at an optimal solution to the original problem.

A very simple example 1.Problem: Given an array of positive integers. Find the greatest element of this array. 2. Solution: - At the beginning, the max = 0 -At each step i, we consider max and A(i), if A(i) > max then Max = A(i). -After n steps, Max is the greatest element of the array. 3. Greedy idea: At each step, we take the best solution for Max, After step I, Max is the greatest element of I first elements. The local solution implies a global solution for the problem.

1. Minimum spanning trees Overview: Problem: In the design of electronic circuitry, we wish to make the pins of several components electrically equivalent by wiring them together. Of all such arrangements, the one that uses the least amount of wire is usually the most desirable. Model: undirected graph G = (V, E), V: the set of pins, E: the set of interconnections between pairs of pins, weight w(u, v): the cost to connect u and v. We wish to find an acyclic subset T of E that connects all of the vertices and whose total weight is minimized.

minimum-spanning-tree problem T is acyclic and connects all of the vertices, it must form a tree, which we call a spanning tree since it "spans" the graph G. We call the problem of determining the tree T the minimum-spanning-tree problem. Problem: given an weighted graph. Find a spanning tree with smallest weight.

1. 1. Greedy generic algorithm GENERIC-MST(G, w) 1 A←Ø 2while A does not form a spanning tree 3do find an edge (u, v) that is safe for A 4A ← A U {(u, v)} 5return A We use the loop invariant as follows: Initialization: After line 1, the set A trivially satisfies the loop invariant. Maintenance: The loop in lines 2-4 maintains the invariant by adding only safe edges. Termination: All edges added to A are in a minimum spanning tree, and so the set A is returned in line 5 must be a minimum spanning tree.

1. 2. Kruskal’s algorithm Each time, we have a forest. At the beginning, it is a forest of n trees, each tree is a vertex. At each step, we chose a smallest edge (u,v) that connect two different trees. At the end, every trees are connected, we obtain an unique (spanning) tree.

MST-KRUSKAL MST-KRUSKAL(G, w) 1 A←Ø 2 for each vertex v in V[G] 3 do MAKE-SET(v) 4 sort the edges of E into nondecreasing order by weight w 5 for each edge (u, v) in E, taken in nondecreasing order by weight 6 do if FIND-SET(u) <> FIND-SET(v) 7 thenA ← A U {(u, v)} 8 UNION(u, v) 9 return A

Analyzing of Kruskal’s algorithm Line 2-3: O(n) Line 4: O(E lg E) FIND-SET = O(lg V) Line 5-8: O(E lg V) O(lg V) = O(lg E) Total: O(E lg V)

1. 3. Prim's algorithm We construct the tree step by step by adding one safe vertex and edge. At the beginning, B contains one vertex and A has no edge. At each step, A is the MST of B. Chose a smallest edge (u,v) such that u in V-B and v in B. Add u in B and Add (u,v) in A. At the end, B = V, and A is MST of V.

Exercises Write the Prim’s algorithm. Analyze the Prim’s complexity. What is the result of Kruskal’s algorithm if the graph is not connect ? What is the result of Prim’s algorithm if the graph is not connect ?

2. Single-Source Shortest Paths Problem: Given a Graph where each edge having a length. Given a source vertex s. Find shortest path from s to each vertex of G.

Variants Single-destination shortest-paths problem: Find a shortest path to a given destination vertex t from each vertex v. By reversing the direction of each edge in the graph, we can reduce this problem to a single-source problem. Single-pair shortest-path problem: Find a shortest path from u to v for given vertices u and v. If we solve the single-source problem with source vertex u, we solve this problem also. Moreover, no algorithms for this problem are known that run asymptotically faster than the best single-source algorithms in the worst case. All-pairs shortest-paths problem: Find a shortest path from u to v for every pair of vertices u and v. Although this problem can be solved by running a single-source algorithm once from each vertex, it can usually be solved faster.

Idea of Dijkstra’s algorithm Set S contains vertices determining shortest path from s. Q = V-S. At the beginning: S=empty At each step, chose v in Q with minimum distance to s. Add v to S (delete v from Q). Update the distance to s for all neighbors of v. At the end, S=V

Lemma: a sub path of a shortest path is a shortest path. Corollary: There exists an spanning tree of V rooted at s such that for all vertex v, the unique path from s to v in the tree is a shortest path.

Parameters of the algorithm Array d where d(v) is the temporal distance from s to v. Array Adj: Adj(v) is the list of neighbor of v. Array w: w(u,v) the length of edge (u,v). Array p: p(v) is the temporal parent of v. Result: a tree rooted as s, and p(v) is the parent of v in the tree

Dijkstra’s algorithm Dijkstra (G,w,s) 1 for each v in V do d(v) = ∞; p(u) = Nil; d(s) = S= Ø; Q = V 4 while Q <> Ø 5 do chose u in Q such that d(u) min 6 S = S U {u}; Q = Q –{u} 7 for each v in Adj(u) 8 do if d(v) > d(u) + w(u,v) then d(v) = d(u)+w(u,v); p(v) = u

Proof of Dijkstra’s algorithm We use the following loop invariant: At the start of each iteration of the while loop of lines 4-8, d[v] is the distance from s to v for each vertex v in S. Initialization: S = Ø, the invariant is trivially true. Maintenance: We wish to show that in each iteration, d[u] = distance(s,u) for the vertex added to set S. Termination: At termination, Q = Ø which, along with our earlier invariant that Q = V - S, implies that S = V. Thus, d[u] = distance(s,u) for all vertices u in V.

Maintenance We prove by recurrence that: -If u in S: d(u) = distance (s,u) -If u in Q: d(u) is the length of shortest path from s to u by using only vertices of S. -At the end, S= V, then d(u) is the real distance from s to u

Analyzing the Dijkstra’s algorithm In general O(n^2) If the graph is very sparse, we can realize q as a heap, each time we take u of Q such that d(u) min, the time is O(lg V). The number of operation is O(E) The total time is O(E lg V)

Traversal Salesman Problem Problem: Given an weighted graph. Find a simple circuit passing each vertex exactly once (a Hamilton circuit) with minimum weight. Greedy algorithms ? This is a classic example such that the greedy algorithm does not work.

Counter example j = i=

Chosen edges: 12, 35, 45, 23, 46, 16 Circuit: (1,2,3,5,4,6,1), length = 58 Minimum length = 56

Conclusion Greedy algorithms do not always yield optimal solutions, but for many problems they do. In many case, the idea of a greedy algorithm is very natural and easy to realize. The greedy method is quite powerful and works well for a wide range of problems.