You are on page 1of 15

UNIT- IV SYLLABUS What is Greedy Approach?

Greedy : Interval scheduling, Minimum cost


spanning trees: Prim’s algorithm, Kruskal’s • Suppose that a problem can be solved by a sequence of
Algorithm, Shortest paths: unweighted and decisions. The greedy method has that each decision is
locally optimal. These locally optimal solutions will
weighted, Single source shortest paths:Dijkstra,
finally add up to a globally optimal solution.
Huffman coding.
• Only a few optimization problems can be solved by the
greedy method.
Reference:
1. “Fundamentals of Computer Algorithms” by Horowitz, Sahni and
Rajasekaran.
2. “Introduction to Algorithms” by Thomas H. Cormen, Charles E.
Leiserson, Ronald L. Rivest, and Clifford Stein.

blog@ anilkumarprathipati.wordpress.com

Control abstraction for Greedy Method


Three Important Activities
Algorithm GreedyMethod (a, n)
{ 1. A selection of solution from the given input domain is
// a is an array of n inputs
Solution: =Ø;
performed, i.e. s:= select(a).
for i: =0 to n do 2. The feasibility of the solution is performed, by using
{ feasible ‘(solution, s)’ and then all feasible solutions
s: = select (a);
if (feasible (Solution, s)) then
are obtained.
{ 3. From the set of feasible solutions, the particular
Solution: = union (Solution, s); solution that minimizes or maximizes the given
}
else objection function is obtained. Such a solution is
reject (); // if solution is not feasible reject it. called optimal solution.
}
return solution;
}

blog@anilkumarprathipati.wordpress.com 1
Differentiate Greedy and Divide-and-Conquer
Application - KNAPSACK PROBLEM
GREEDY APPROACH DIVIDE AND CONQUER
1.Many decisions and sequences are 1.Divide the given problem into many sub- • In this problem we have a Knapsack that has a weight
guaranteed and all the overlapping sub- problems. Find the individual solutions and
instances are considered. combine them to get the solution for the
limit M
main problem • There are items i1, i2, ..., in each having weight w1, w2,
2. Follows Bottom-up technique 2. Follows top down technique … wn and some benefit (value or profit) associated with it
3.Split the input at every possible points 3.Split the input only at specific points (mid
rather than at a particular point point), each problem is independent.
p1, p2, ..., pn
• Our objective is to maximise the benefit such that the
4. Sub problems are dependent 4. Sub problems are independent
total weight inside the knapsack is at most M, and we are
on the main Problem on the main Problem
also allowed to take an item in fractional part.
5. Time taken by this approach is not that 5. Time taken by this approach efficient
much efficient when compared with DAC. when compared with GA.

6.Space requirement is less when 6.Space requirement is very much high


compared DAC approach. when compared GA approach.

Example Algorithm
ITEM WEIGHT VALUE
i1 6 6 M=16
i2 10 2
i3 3 1
i4 5 8
i5 1 3
i6 3 5
• Maximum Profit (20)
• Minimum Weight (17)
• Maximum Profit – Weight ratio (22.333336)

blog@anilkumarprathipati.wordpress.com 2
Interval Scheduling
• Interval scheduling.
Possible Solutions
– Job j starts at sj and finishes at fj.
• Greedy template. Consider jobs in some natural
– Two jobs compatible if they don't overlap.
order. Take each job provided it's compatible with the
– Goal: find maximum subset of mutually compatible jobs.
ones already taken.
a – [Earliest start time] Consider jobs in ascending order of sj.
b – [Earliest finish time] Consider jobs in ascending order of
c fj.
d – [Shortest interval] Consider jobs in ascending order of fj -
e sj.
f – [Fewest conflicts] For each job j, count the number of
g conflicting jobs cj. Schedule in ascending order of cj.
h
Time
0 1 2 3 4 5 6 7 8 9 10 11
13 14

Greedy Algorithm Procedure


• Greedy algorithm. Consider jobs in increasing order
of finish time. Take each job provided it's compatible 1) Sort the activities according to their finishing
with the ones already taken. time.
Sort jobs by finish times so that f1  f2  ...  2) Select the first activity from the sorted array
fn. and print it.
set of jobs selected
A   3) Do following for remaining activities in the
for j = 1 to n {
if (job j compatible with A) sorted array.
A  A  {j}
}
a) If the start time of this activity is greater
return A than or equal to the finish time of previously
selected activity then select this activity and print
• Implementation. O(n log n)  sorting time of it.
finishing time of n jobs.
15

blog@anilkumarprathipati.wordpress.com 3
#include<stdio.h> Application – Minimum Spanning Tree
// Prints a maximum set of activities that can be done by a single person, one at a time.
n --> Total number of activities, s[ ] --> An array that contains start time of all • A spanning tree is a subset of Graph G, which has all the
activities, f[ ] --> An array that contains finish time of all activities.
void printMaxActivities(int s[ ], int f[ ], int n)
vertices covered with minimum possible number of
{ int main()
edges. Hence, a spanning tree does not have cycles and
int i, j,count=0; { it cannot be disconnected.
printf ("Following activities are selected \n"); int s[ ] = {1, 3, 0, 5, 8, 5};
int f[ ] = {2, 4, 6, 7, 9, 9};
Note 1: Every connected and undirected Graph G has at
i = 0;
printf("%d ", i);
int n = sizeof(s)/sizeof(s[0]); least one spanning tree.
printMaxActivities(s, f, n);
count++; return 0; Note 2: A disconnected graph does not have any
for (j = 1; j < n; j++) } spanning tree.
{
if (s[j] >= f[i])
• A complete undirected graph can have maximum
{ nn-2 number of spanning trees, where n is the number of
printf ("%d ", j); i = j; count++; nodes.
}
}printf (“Selected activities are: %d\n“,count);
}

i. Kruskal’s Algorithm
Step 1 - Remove all loops and Parallel Edges.
Step 2 - Arrange all edges in their increasing order of
weight.
Step 3 - Add the edge which has the least weightage iff it
does not form cycle.

Ex:

blog@anilkumarprathipati.wordpress.com 4
Algorithm

blog@anilkumarprathipati.wordpress.com 5
ii. Prim’s Algorithm
• Prim's algorithm, in contrast with Kruskal's algorithm,
treats the nodes as a single tree and keeps on adding new
nodes to the spanning tree from the given graph.
• Step 1 - Remove all loops and parallel edges.
• Step 2 - Choose any arbitrary node as root node.
• Step 3 - Check outgoing edges and select the one with
less cost.

Kruskal’s vs Prim’s
• Prim’s algorithm initializes with a node, whereas
Kruskal’s algorithm initiates with an edge.
Algorithm • Prim’s algorithms span from one node to another while
Kruskal’s algorithm select the edges in a way that the
position of the edge is not based on the last step, it is
based on the minimum weight.
• In Prim’s each vertex traverse more than once, where as
in Kruskal’s each edge traverse at most once.
• In prim’s algorithm, graph must be a connected while the
Kruskal’s can function on disconnected graphs too.
• Prim’s algorithm has a time complexity of O(V2), and
Kruskal’s time complexity is O(ElogV).

blog@anilkumarprathipati.wordpress.com 6
Application – Single source shortest path
• For a given source node in the graph, the algorithm finds
the shortest path between that node and every other. It
also used for finding the shortest paths from a single
node to a single destination node by stopping the
algorithm once the shortest path to the destination node
has been determined.

Algorithm

blog@anilkumarprathipati.wordpress.com 7
Application: Huffman Code
Computer Data Compression:
How do we compressed? And how do we
represent data in binary?

Historical Solution:
Fixed length codes.
Encode every symbol by a unique binary string of
a fixed length.
Examples:
ASCII (7 bit code),
American Standard Code for Information Interchange

EBCDIC (8 bit code), …


Extended Binary Coded Decimal Interchange Code

American Standard Code for ASCII Example:


Information Interchange

BCCABBDDAECCBBAEDDCC

A B C D E
1000001 1000010 1000011 1000100 1000101

blog@anilkumarprathipati.wordpress.com 8
Total space usage in bits Fixed Length codes
Idea: In order to save space, use less bits for
frequent characters and more bits for rare
characters.
Assume an ℓ bit fixed length code.
Example: suppose alphabet of 3 symbols:
For a file of n characters { A, B, C }.
suppose in file: 1,000,000
characters.
Need nℓ bits. Need 2 bits for a fixed length
code for a total of
2,000,000 bits.

Variable Length codes - I Total space usage in bits:


Suppose the frequency distribution of the
characters is: Fixed code: 1,000,000 x 2 = 2,000,000

A B C Varable code: 999,000 x 1


999,000 500 500 + 500 x 2
500 x 2
Encode: A B C
0 10 11
1,001,000

Note: that the code of A is of length 1, and the codes for B A savings of almost 50%
and C are of length 2

blog@anilkumarprathipati.wordpress.com 9
How do we decode? How do we decode?
In the fixed length, we know where every In the variable length code, we use an idea called
character starts, since they all have the Prefix code, where no code is a prefix of another.
same number of bits.
Example: A = 0
Example: A = 00 B = 10
B = 01 C = 11
C = 10
None of the above codes is a prefix of another.
000000010110101001100100001010
A A A B B C CCB C B A A C C

How do we decode? Prefix Code


Example: A = 0 Example: A = 0
B = 10 B = 10
C = 11 C = 11

Decode the string


So, for the string:
AAA B B C C C B C B AA C C the encoding: 0 0 01010111111101110 0 01111

0 0 01010111111101110 0 01111 AA AB B C C C BC BAAC C

blog@anilkumarprathipati.wordpress.com 10
Idea
Pre-requisite
Consider a binary tree, with:
Construct a variable length code for a 0 meaning a left turn
given file with the following properties: 1 meaning a right turn.

1. Prefix code. 0 1
2. Using shortest possible codes. A
0 1
3. Efficient.
4. As close to entropy as possible.
B
0 1
C D

Idea
Greedy Algorithm
Consider the paths from the root to each of the
leaves A, B, C, D: 1. Consider all pairs: <frequency, symbol>.

A: 0 2. Choose the two lowest frequencies, and


B : 10 0 1 make them brothers, with the root
C : 110 A having the combined frequency.
0 1
D : 111
3. Iterate until it forms a one tree.
B
0 1
C D

blog@anilkumarprathipati.wordpress.com 11
Greedy Algorithm Example Algorithm Run

Alphabet: A, B, C, D, E, F A 10 B 20 C 30 D 40 E 50 F 60

Frequency table:
A B C D E F
10 20 30 40 50 60

Total File Length: 210

Algorithm Run Algorithm Run

X 30 C 30 D 40 E 50 F 60 Y 60 D 40 E 50 F 60

A 10 B 20 X 30 C 30

A 10 B 20

blog@anilkumarprathipati.wordpress.com 12
Algorithm Run Algorithm Run
D 40 E 50 Y 60 F 60 Z 90 Y 60 F 60

X 30 C 30 D 40 E 50 X 30 C 30

A 10 B 20 A 10 B 20

Algorithm Run Algorithm Run


Y 60 F 60 Z 90 W 120 Z 90

X 30 C 30 D 40 E 50 Y 60 F 60 D 40 E 50

A 10 B 20 X 30 C 30

A 10 B 20

blog@anilkumarprathipati.wordpress.com 13
Algorithm Run Algorithm Run
Z 90 W 120 V 210
0 1

D 40 E 50 Y 60 F 60 Z 90 W 120
0 1 1
0
D 40 E 50 Y 60 F 60
X 30 C 30
0 1

A 10 B 20 X 30 C 30
0 1

A 10 B 20

The Huffman encoding Savings


V 210
A: 1000 0 1
B: 1001 The Huffman code:
C: 101 Z 90
1
W 120 Required 510 bits for the file.
0 0 1
D: 00
E: 01 D 40 E 50 Y 60 F 60
Fixed length code:
0 1
F: 11
X 30 C 30 Need 3 bits for 6 characters.
0 1 File has 210 characters.
A 10 B 20

File Size: 10x4 + 20x4 + 30x3 + 40x2 + 50x2 + 60x2 =


Total: 630 bits for the file.
40 + 80 + 90 + 80 + 100 + 120 = 510 bits

blog@anilkumarprathipati.wordpress.com 14
Example

mississippi

blog@anilkumarprathipati.wordpress.com 15

You might also like