Download presentation
Presentation is loading. Please wait.
Published byBerniece Parrish Modified over 9 years ago
1
Dr. Naveed Ahmad Assistant Professor Department of Computer Science University of Peshawar
2
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)
3
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.
4
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
5
Problem: Find a shortest path from v 0 to v 3. The greedy method can solve this problem. The shortest path: 1 + 2 + 4 = 7.
6
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.
7
d min (i,j): minimum distance between i and j.
8
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
9
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
10
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
11
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 2 33 4 44
12
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 201815141110653 P1 P2 P3 Time to completion: 18 + 11 + 6 = 35 minutes This solution isn’t bad, but we might be able to do better
13
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 6 + 14 + 20 = 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 201815141110653 P1 P2 P3
14
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: 20 18 15 14 11 106 5 3 P1 P2 P3
15
Is there other optimal solutions?
17
Un weighted Every edges has one unit
20
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
21
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.