Dr. Naveed Ahmad Assistant Professor Department of Computer Science University of Peshawar.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
Advertisements

Introduction to Algorithms Greedy Algorithms
The Greedy Method1. 2 Outline and Reading The Greedy Method Technique (§5.1) Fractional Knapsack Problem (§5.1.1) Task Scheduling (§5.1.2) Minimum Spanning.
Types of Algorithms.
Chapter 5 Fundamental Algorithm Design Techniques.
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
Merge Sort 4/15/2017 6:09 PM The Greedy Method The Greedy Method.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Greedy Algorithms. 2 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2006 Lecture 2 Monday, 9/13/06 Design Patterns for Optimization Problems.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2002 Monday, 12/2/02 Design Patterns for Optimization Problems Greedy.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2008 Lecture 2 Tuesday, 9/16/08 Design Patterns for Optimization.
ASC Program Example Part 3 of Associative Computing Examining the MST code in ASC Primer.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
1 The Greedy Method CSC401 – Analysis of Algorithms Lecture Notes 10 The Greedy Method Objectives Introduce the Greedy Method Use the greedy method to.
The Shortest Path Problem
D IJKSTRA ' S ALGORITHM By Laksman Veeravagu and Luis Barrera.
Dijkstra's algorithm.
Lecture 23. Greedy Algorithms
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
Lecture 1: The Greedy Method 主講人 : 虞台文. Content What is it? Activity Selection Problem Fractional Knapsack Problem Minimum Spanning Tree – Kruskal’s Algorithm.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
SPANNING TREES Lecture 21 CS2110 – Spring
Fundamentals of Algorithms MCS - 2 Lecture # 7
CPSC 320: Intermediate Algorithm Design & Analysis Greedy Algorithms and Graphs Steve Wolfman 1.
5-1-1 CSC401 – Analysis of Algorithms Chapter 5--1 The Greedy Method Objectives Introduce the Brute Force method and the Greedy Method Compare the solutions.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Shortest Path in Weighted Graph : Dijkstra’s Algorithm.
D IJKSTRA ' S ALGORITHM. S INGLE -S OURCE S HORTEST P ATH P ROBLEM Single-Source Shortest Path Problem - The problem of finding shortest paths from a.
The Greedy Method. The Greedy Method Technique The greedy method is a general algorithm design paradigm, built on the following elements: configurations:
Greedy Algorithms. What is “Greedy Algorithm” Optimization problem usually goes through a sequence of steps A greedy algorithm makes the choice looks.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Spring 2008The Greedy Method1. Spring 2008The Greedy Method2 Outline and Reading The Greedy Method Technique (§5.1) Fractional Knapsack Problem (§5.1.1)
A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding.
SPANNING TREES Lecture 21 CS2110 – Spring
Greedy Algorithms.
By Laksman Veeravagu and Luis Barrera
Shortest Path Problems
Problem solving sequence
Greedy Algorithms.
Department of Computer Science
Merge Sort 7/29/ :21 PM The Greedy Method The Greedy Method.
Types of Algorithms.
Types of Algorithms.
Merge Sort 11/28/2018 2:18 AM The Greedy Method The Greedy Method.
The Greedy Method Spring 2007 The Greedy Method Merge Sort
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
Merge Sort 11/28/2018 8:16 AM The Greedy Method The Greedy Method.
Analysis and design of algorithm
Chapter 2: Business Efficiency Business Efficiency
CSE 373: Data Structures and Algorithms
Advanced Analysis of Algorithms
Merge Sort 1/17/2019 3:11 AM The Greedy Method The Greedy Method.
Graph Searching.
Slide Courtesy: Uwash, UT
CSC 380: Design and Analysis of Algorithms
Shortest Path Algorithm for Weighted Non-negative Undirected Graphs
Types of Algorithms.
CSC 380: Design and Analysis of Algorithms
Slide Courtesy: Uwash, UT
Merge Sort 5/2/2019 7:53 PM The Greedy Method The Greedy Method.
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Graphs: Shortest path and mst
Presentation transcript:

Dr. Naveed Ahmad Assistant Professor Department of Computer Science University of Peshawar

 There is no silver bullet  Divide and conquer (merge Sort)  Greedy algorithm (dijesktra algorithm)  Randomized algorithm (hash functions)  Dynamic Programming (shortest path and sequence alignment)

 The greedy method is a general algorithm design paradigm, built on the following elements:  configurations: different choices, collections, or values to find  objective function: a score assigned to configurations, which we want to either maximize or minimize  It works best when applied to problems with the greedy-choice property:  a globally-optimal solution can always be found by a series of local improvements from a starting configuration.

 A greedy algorithm works in phases. At each phase:  You take the best you can get right now, without regard for future consequences  You hope that by choosing a local optimum at each step, you will end up at a global optimum

 Problem: Find a shortest path from v 0 to v 3.  The greedy method can solve this problem.  The shortest path: = 7.

 Problem: Find a shortest path from v0 to v3 in the multi-stage graph.  Greedy method: v 0 v 1,2 v 2,1 v 3 = 23  Optimal: v 0 v 1,1 v 2,2 v 3 = 7  The greedy method does not work.

 d min (i,j): minimum distance between i and j.

 An optimization problem is one in which you want to find, not just a solution, but the best solution  A “greedy algorithm” sometimes works well for optimization problems

 Suppose you want to count out a certain amount of money, using the fewest possible bills and coins  A greedy algorithm would do this would be: At each step, take the largest possible bill or coin that does not overshoot  Example: To make $6.39, you can choose:  a $5 bill  a $1 bill, to make $6  a 25¢ coin, to make $6.25  A 10¢ coin, to make $6.35  four 1¢ coins, to make $6.39  For US money, the greedy algorithm always gives the optimum solution

 In some (fictional) monetary system, “krons” come in 1 kron, 7 kron, and 10 kron coins  Using a greedy algorithm to count out 15 krons, you would get  A 10 kron piece  Five 1 kron pieces, for a total of 15 krons  This requires six coins  A better solution would be to use two 7 kron pieces and one 1 kron piece  This only requires three coins  The greedy algorithm results in a solution, but not in an optimal solution

 A salesman must visit every city (starting from city A), and wants to cover the least possible distance  He can revisit a city (and reuse a road) if necessary  He does this by using a greedy algorithm: He goes to the next nearest city from wherever he is  From A he goes to B  From B he goes to D  This is not going to result in a shortest path!  The best result he can get now will be ABDBCE, at a cost of 16  An actual least-cost path from A is ADBCE, at a cost of 14 E ABC D

 You have to run nine jobs, with running times of 3, 5, 6, 10, 11, 14, 15, 18, and 20 minutes  You have three processors on which you can run these jobs  You decide to do the longest-running jobs first, on whatever processor is available P1 P2 P3  Time to completion: = 35 minutes  This solution isn’t bad, but we might be able to do better

 What would be the result if you ran the shortest job first?  Again, the running times are 3, 5, 6, 10, 11, 14, 15, 18, and 20 minutes  That wasn’t such a good idea; time to completion is now = 40 minutes  Note, however, that the greedy algorithm itself is fast  All we had to do at each stage was pick the minimum or maximum P1 P2 P3

 This solution is clearly optimal (why?)  How do we find such a solution?  One way: Try all possible assignments of jobs to processors  Unfortunately, this approach can take exponential time  Better solutions do exist: P1 P2 P3

 Is there other optimal solutions?

 Un weighted  Every edges has one unit

Dijkstra's algorithm - is a solution to the single-source shortest path problem in graph theory. Works on both directed and undirected graphs. However, all edges must have nonnegative weights. Approach: Greedy Input: Weighted graph G={E,V} and source vertex v ∈ V, such that all edge weights are nonnegative Output: Lengths of shortest paths (or the shortest paths themselves) from a given source vertex v ∈ V to all other vertices

 dist[s] ← 0 (distance to source vertex is zero) for all v ∈ V–{s} do dist[v] ← ∞ (set all other distances to infinity) S ← ∅ (S, the set of visited vertices is initially empty) Q ← V (Q, the queue initially contains all vertices) while Q ≠ ∅ (while the queue is not empty) do u ← mindistance(Q,dist)n( select the element of Q with the min. distance) S ← S ∪ {u} (add u to list of visited vertices) for all v ∈ neighbors[u] do if dist[v] > dist[u] + w(u, v) (if new shortest path found) then d[v] ← d[u] + w(u, v)(set new value of shortest path) (if desired, add traceback code) return dist