Professional Documents
Culture Documents
blog@ anilkumarprathipati.wordpress.com
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.
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
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
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.
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
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>.
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
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
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
blog@anilkumarprathipati.wordpress.com 14
Example
mississippi
blog@anilkumarprathipati.wordpress.com 15