0% found this document useful (0 votes)
31 views42 pages

Modified by Dr. ISSAM ALHADID 11/3/2019

The document discusses greedy algorithms and their applications. It defines greedy algorithms as making locally optimal choices at each step in the hope of finding a globally optimal solution. Specific greedy algorithms covered include Kruskal's algorithm for finding minimum spanning trees and algorithms for solving the knapsack problem. The time complexity of Kruskal's algorithm is analyzed as O(E log E) or O(E log V) where E is the number of edges and V is the number of vertices.

Uploaded by

ISSAM HAMAD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views42 pages

Modified by Dr. ISSAM ALHADID 11/3/2019

The document discusses greedy algorithms and their applications. It defines greedy algorithms as making locally optimal choices at each step in the hope of finding a globally optimal solution. Specific greedy algorithms covered include Kruskal's algorithm for finding minimum spanning trees and algorithms for solving the knapsack problem. The time complexity of Kruskal's algorithm is analyzed as O(E log E) or O(E log V) where E is the number of edges and V is the number of vertices.

Uploaded by

ISSAM HAMAD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Modified by Dr.

ISSAM ALHADID
11/3/2019
 General Method
 Minimum Spanning Tree Problem
◦ Kruskal’s Algorithm (Edge Based)
 Knapsack Problem
 Greedy method is the most straightforward
design technique, and it can be applied to a wide
variety of problems.
 We are required to find a feasible solution that
either maximizes or minimizes a given objective
function.
 A feasible solution does this is called an optimal
solution.
 There is usually an obvious way to determine a
feasible solution, but not necessarily an optimal
solution.
 Greedy algorithms don’t always yield optimal
solutions, but for many problems they do.
 What is a 'Greedy algorithm'?
 A greedy algorithm, always makes the choice that
seems to be the best at that moment.
 This means that it makes a locally-optimal choice
in the hope that this choice will lead to a globally-
optimal solution.
 How do you decide which choice is optimal?
 Assume that you have an objective function that
needs to be optimized (either maximized or
minimized) at a given point. A Greedy algorithm
makes greedy choices at each step to ensure that
the objective function is optimized.
 Greedy algorithms are used for optimization
problems.
 Key idea:
◦ Makes the choice that looks best at the moment.
 The hope:
◦ A locally optimal choice will lead to a globally optimal
solution.
 Greedy method:
◦ A solution to the problem is constructed in stages.
◦ At each stage, an obvious “greedy” selection procedure
is used to augment (build up) the solution, till the
complete solution is obtained.
 The Greedy algorithm has only one shot to
compute the optimal solution so that it never
goes back and reverses the decision.
 Scheduling:
◦ Scheduling of priority jobs on single processor.
◦ Scheduling of unit-time tasks with deadlines on
single processor.
 Graph Algorithms:
◦ Minimum Spanning Trees.
◦ Dijkstra’s (shortest path) Algorithm.
 Other Optimization Problems:
◦ Knapsack.
◦ Traveling Salesman.
◦ Optimal Prefix Codes (Huffman’s algorithm).
 A spanning tree of graph G is a subgraph
which
◦ is tree (acyclic),
◦ and connect all the vertices in V
 What is Minimum Spanning Tree?
Given a connected and undirected graph, a spanning
tree of that graph is a subgraph that is a tree and
connects all the vertices together. A single graph can
have many different spanning trees. A minimum
spanning tree (MST) or minimum weight spanning tree
for a weighted, connected and undirected graph is a
spanning tree with weight less than or equal to the
weight of every other spanning tree. The weight of a
spanning tree is the sum of weights given to each edge
of the spanning tree.
 How many edges does a minimum spanning tree has?
A minimum spanning tree has (V – 1) edges where V is
the number of vertices in the given graph.
 Input:
◦ Undirected connected graphG = (V, E) and
weight function w : E→R,
 Output:
◦ A Minimum Spanning Tree (MST): is a tree
(T) that connects all the vertices and
minimizes:
 Greedy Algorithm:
◦ Kruskal’s algorithm
 1. Sort all the edges in non-decreasing order
of their weight.
 2. From the remaining edges, select a least-
cost edge that does not result in a cycle when
added to the set of already selected edges. If
cycle is not formed, include this edge. Else,
discard it.
 3. Repeat step#2 until there are (V-1) edges
in the spanning tree.
 The graph contains 9 vertices and 14 edges.
So, the minimum spanning tree formed will
be having (9 – 1) = 8 edges.
 After sorting:
Weight Src Dest
1 7 6
2 8 2
2 6 5
4 0 1
4 2 5
6 8 6
7 2 3
7 7 8
8 0 7
8 1 2
9 3 4
10 5 4
11 1 7
14 3 5
5/17/2019 17
 Now pick all edges one by one from sorted
list of edges
1. Pick edge 7-6: No cycle is formed, include
it.

 2. Pick edge 8-2: No cycle is formed, include


it.

5/17/2019 18
 3. Pick edge 6-5: No cycle is formed, include
it.

 4. Pick edge 0-1: No cycle is formed, include


it.

5/17/2019 19
 5. Pick edge 2-5: No cycle is formed, include
it.

 6. Pick edge 8-6: Since including this edge


results in cycle, discard it.
 7. Pick edge 2-3: No cycle is formed, include
it.

5/17/2019 20
 8. Pick edge 7-8: Since including this edge
results in cycle, discard it.
 9. Pick edge 0-7: No cycle is formed, include
it.

5/17/2019 21
 10. Pick edge 1-2: Since including this edge
results in cycle, discard it.
 11. Pick edge 3-4: No cycle is formed,
include it.

 Since the number of edges included equals


(V – 1), the algorithm stops here.

5/17/2019 22
????
 Time Complexity: O(ElogE) or O(ElogV). Sorting of
edges takes O(ELogE) time. After sorting, we
iterate through all edges and apply find-union
algorithm. The find and union operations can
take at most O(LogV) time. So overall complexity
is O(ELogE + ELogV) time.
 The value of E can be atmost O(V2)

 Since |E| at most V2 so O(LogV) are O(LogE) same.


Therefore, overall time complexity is O(ElogE) or
O(ElogV)
 Exercise: what is the total space required by
Kruskal’s algorithm?
 Knapsack Problem Statement
 Knapsack Problem Formulation
 Knapsack Example
 Knapsack Algorithm
 Knapsack Running Time and Space
 Knapsack Theorem
profit

weight
 H.W.

You might also like