Daaco 2
Daaco 2
Greedy Method
General Method
Basic Notations
• A feasible solution is any subset of the original input that satisfies a given set
of constraints.
• Global Optimal Solutions will be made with local optimal (Greedy) choices
• In the Greedy algorithm, the best choice will be selected at the moment to solve
the sub-problem that remains
• The choice of the greedy may depend on the previous choices made but it
cannot depend on future choices or the solutions to the sub-problems
Types of Greedy Problems
• Subset Paradigm: To solve a problem (or possibly find the optimal/best solution), a
greedy approach generates a subset by selecting one or more available choices.
Example:
• Knapsack problem
• Job sequencing with deadlines
Control abstraction for subset paradigm
• Knapsack Problem
• Huffman Codes
10
Limitations
DC approach divides the problem into small sub- In greedy approach, the optimal solution is obtained
problems, each sub-problem is solved independently from a set of feasible solutions.
and solutions of the smaller problems are combined
to find the solution to the large problem.
Sub problems are independent, so DC might solve Greedy algorithm does not consider the previously solved
same sub problem multiple time. instance thus it avoids the re-computation.
DC approach is recursive in nature, so it is slower and Greedy algorithms are iterative in nature and hence
inefficient. faster.
Divide and conquer algorithms mostly runs in Greedy algorithms also run in polynomial time but takes
polynomial time less time than Divide and conquer
Example: Example:
Merge sort, Knapsack problem,
Quick sort Job scheduling problem 12
Department of CSE
Greedy Method
Introduction
The problem states- “How can the total profit be maximized if only one job can
be completed at a time?”
Algorithm
Greedy Algorithm is adopted to determine how the next job is selected for an optimal
solution.
The greedy algorithm described below always gives an optimal solution to the job
sequencing problem-
Step-1: Sort all the given jobs in decreasing order of their profit.
Draw a Gantt chart where maximum time on Gantt chart is the value of
maximum deadline.
Put the job on Gantt chart as far as possible from 0 ensuring that the job gets
completed before its deadline.
Example
Step-1: Sort all the given jobs in decreasing order of their profit
Step-2: Value of maximum deadline = 5.
So, draw a Gantt chart with maximum time on Gantt chart = 5 units as shown-
Now,
• We take each job one by one in the order they appear in Step-01.
Step-4: We take job J1. Since its deadline is 5, so we place it in the first empty cell
before deadline 5 as-
Step-5: We take job J3. Since its deadline is 3, so we place it in the first empty cell
before deadline 3 as-
Step-6: We take job J2. Since its deadline is 3, so we place it in the first empty cell
before deadline 3. Since the second and third cells are already filled, so we place
job J2 in the first cell as-
Step-7: Now, we take job J5. Since its deadline is 4, so we place it in the first
empty cell before deadline 4 as-
The only job left is job J6 whose deadline is 2. All the slots before deadline 2 are
already occupied. Thus, job J6 cannot be completed.
This is the required order in which the jobs must be completed in order to obtain
the maximum profit.
Maximum earned profit = Sum of profit of all the jobs in optimal schedule
= Profit of job J2 + Profit of job J4 + Profit of job J3 + Profit of job J5 + Profit of job J1
= 990 units
Job Sequencing with Deadlines
We are given a set of n jobs.
Deadline di >= 0 and a profit pi >0 are associated with each job i.
For any job profit is earned if and only if the job is completed by its deadline.
To complete a job, a job has to be processed by a machine for one unit of time.
Only one machine is available for processing jobs.
A feasible solution to this problem is a subset of jobs such that each job in this
subset can be completed by its deadline
The value of feasible solution J is the sum of the profits of the jobs in J , or
The optimal solution is a feasible solution that will maximize the total profit.
The objective is to find an order of processing of jobs that will maximize the total
profit.
Example 1: n = 4, (p1, p2, p3, p4) = (100,10,15,27)
(d1, d2, d3, d4) = (2, 1, 2, 1)
The maximum deadline
Feasible is 2 units, hence
solution the feasible
Processing solution set must have <=2
sequence jobs.
value
1 (1,2) 2,1 110
2 (1,3) 1,3 or 3, 1 115
3 (1,4) 4, 1 127
4 (2,3) 2, 3 25
5 (3,4) 4,3 42
6 (1) 1 100
7 (2) 2 10
8 (3) 3 15
9 (4) 4 27
Solution 3 is optimal.
Example 2:
Let n =5 , (P1,P2,P3,P4,P5)= (20,15,10,5,1) and (d1,d2,d3,d4,d5) = (2,2,1,3,3).
Solution:
J Assigned Jobs Considered Action Profit
Slots
Ø None
Algorithm GreedyJob(d, j, n)
// J is a set of jobs that their deadlines can complete
{
j : = {1};
for i := 2 to n do
{
if (all jobs in J U {i} can be completed by their deadlines) then
j := j U {i};
}
}
Algorithm JS(d, j, n)
// d[i] ≥ 1, 1 ≤ i ≤ n are the deadlines, n ≥ 1.
// The jobs are ordered such that p[1] ≥ p[2] …… ≥ p[n]
// j[i] is the ith job in the optimal solution, 1 ≤ i ≤ k , at
//termination d [ j[i]] ≤ d[j[i+1]], 1 ≤ i ≤ k
{
d[0] := j[0] := 0; // Initialize
j[1] := 1; // Include job 1
k := 1;
for i := 2 to n do
{ //Consider jobs in Descending order of p[i].
// Find position for i and check feasibility of insertion.
r := k;
while( ( d[ j[r]]> d[i] and ( d[j[r]] ≠ r )) do
r := r - 1;
if( d[i] > r )) then
{
// Insert i into j[].
for q = k to (r+1) step -1 do j[q+1] = j[q];
j[r+1] := i;
k:=k+1;
}
}
return k;
}
Time taken by this algorithm is o(n2)
Department of CSE
Knapsack Problem
Greedy
GreedyMethod
Method
Knapsack
KnapsackProblem
Problem
• The knapsack problem or rucksack problem , problem Given a set of items, each with a
weight and a value, determine the number of each item to include in a collection so
that the total weight is less than or equal to a given limit and the total value is as large
as possible.
• We are give n objects and a knapsack or bag .
• If a fraction xi , 0 <xi < 1, of object i is placed into the knapsack, then a profit of pi xi is
earned.
• The objective is to obtain a filling of the knapsack that Maximizes the total profit
earned.
Knapsack Problem Variants: Knapsack problem has the following two
variants
1. 0/1 Knapsack Problem
➢ Items are indivisible i.e. we can not take a fraction of any item.
➢ We have to either take an item completely or leave it completely.
➢ It is solved using a dynamic programming approach.
Algorithm GreedyKnapsack(m, n)
//P[1:n] and w[1:n] contain the profits and weights respectively of the n objects
ordered such that p[i]/w[i] >= p[i+1]/w[i+1].
//m is the knapsack size and x[1:n] is the solution vector.
{
for i :=1 to n do x[i] := 0.0; // Initialize x.
U := m;
for i := 1 to n do
{
if ( w[i] > U ) then break;
if x[i] := 1; U := U - w[i];
}
if ( i <= n) then x[i] := U/w[i];
}
Time
TimeComplexity
Complexity
• The main time-consuming step is sorting all items in the decreasing order of their
value/weight ratios.
• If the items are already arranged in the required order, the while loop takes O(n)
time.
Greedy Method
Spanning Tree
• All possible spanning trees of graph G, have the same number of edges and
vertices.
• Removing one edge from the spanning tree will make the graph disconnected, i.e.
the spanning tree is minimally connected.
• Adding one edge to the spanning tree will create a circuit or loop, i.e. the spanning
tree is maximally acyclic.
Example:
1 1
A B A B A B
5 2
4 2 4 4
6
D C D C D C
3 3 3
1 1
Undirected Graph A B A B
2 2
4
D C D C
3
• A minimum spanning tree is the one among all the spanning trees with the
smallest total cost or A Spanning tree with minimum weight
1 1
A B A B
4
4 2 2
5
D C D C
3 3
• Computer Networks
• How to connect a set of computers using the minimum amount of wire..
• Civil Network Planning
• Computer Network Routing Protocol
• Cluster Analysis
Kruskal’s Algorithm
8 7
2 3 4
1 9
2
1 11 9 4 14 5
8
7 16
10
8 7 2
6
4
2 3 4
1 9 5
8 7 6
Time Complexity
• With an efficient Find-set and union algorithms, the running time of kruskal’s
algorithm will be dominated by the time needed for sorting the edge costs of
a given graph.
• Prim’s Algorithm is another greedy algorithm used for finding the Minimum Spanning
Tree (MST) of a given graph.
• The graph must be weighted, connected and undirected
• Start with minimum cost edge.
• For rest of the procedure, always select a minimum cost edge from graph make sure
that already connected to the selected vertices.
• Continue this process until the tree has n - 1 edges.
Differences
Department of CSE
Greedy
GreedyMethod
Method
Shortest
ShortestPath
PathProblem
Problem
• Shortest path problem is a problem of finding the shortest path(s) between
vertices of a given graph.
• Shortest path between two vertices is a path that has the least cost as
compared to all other existing paths.
Applications-
• Google Maps
• Road Networks
• Logistics Research
Types
Typesof
ofShortest
ShortestPath
Path
Shortest
ShortestPath
Path
Step-01:
In the first step. two sets are defined-
•One set contains all those vertices included in the shortest path tree.
In the beginning, this set is empty.
•Another set contains all those vertices left to be included in the
shortest path tree.
In the beginning, this set contains all the vertices of the given graph.
Step-02:
For each vertex of the given graph, two variables are defined as
•Π[v] which denotes the predecessor of vertex ‘v’
•d[v] which denotes the shortest path estimate of vertex ‘v’ from the
source vertex.
Initially, the value of these variables is set as
•The value of variable ‘Π’ for each vertex is set to NIL i.e. Π[v] = NIL
•The value of variable ‘d’ for source vertex is set to 0 i.e. d[S] = 0
•The value of variable ‘d’ for remaining vertices is set to ∞ i.e. d[v] =
∞
Step-03:
The following procedure is repeated until all the vertices of the graph
are processed-
•Among unprocessed vertices, a vertex with a minimum value of
variable ‘d’ is chosen.
•Its outgoing edges are relaxed.
•After relaxing the edges for that vertex, the sets created in step-01
are updated.
Edge
Edgerelaxation
relaxation
• Here, d[a] and d[b] denotes the shortest path estimate for vertices a
and b respectively from the source vertex ‘S’.
• Now, If d[a] + w < d[b]
then d[b] = d[a] + w and Π[b] = a
• This is called as edge relaxation.
Time
Timecomplexity
complexity
Step-01:
The following two sets are created
•Unvisited set : {S , a , b , c , d , e}
•Visited set : { }
Step-02:
The two variables Π and d are created for each vertex and initialized as
•Π[S] = Π[a] = Π[b] = Π[c] = Π[d] = Π[e] = NIL
•d[S] = 0
•d[a] = d[b] = d[c] = d[d] = d[e] = ∞
Step-03:
•Vertex ‘S’ is chosen.
•This is because shortest path estimate for vertex ‘S’ is least.
•The outgoing edges of vertex ‘S’ are relaxed.
Before Edge Relaxation
Now,
•d[S] + 1 = 0 + 1 = 1 < ∞
∴ d[a] = 1 and Π[a] = S
•d[S] + 5 = 0 + 5 = 5 < ∞
∴ d[b] = 5 and Π[b] = S
• After edge relaxation, our shortest path tree is
v V1 V2 V5
V1 V 3
V 1 V3 V 4
V 1 V 3 V4 V 2
V1 V 5
V3 V4 V6 5) V 1V3 V 4V6 28
After edge relaxation, our shortest path tree remains the same as in
Step-05.
Now, the sets are updated as-
•Unvisited set : {c , e}
•Visited set : {S , a , d , b}
Step-07:
•Vertex ‘c’ is chosen.
•This is because shortest path estimate for vertex ‘c’ is least.
•The outgoing edges of vertex ‘c’ are relaxed.
Before Edge Relaxation-
Now,
•d[c] + 1 = 3 + 1 = 4 = 4
•∴ No change
SSSP
SSSPDijkstra’s
Dijkstra’salgorithm
algorithm
Huffman Coding
Greedy
GreedyMethod
Method
Encoding
Encodingand
andCompression
Compressionof
ofData
Data
• Compression:
• Data Compression, shrinks down a String so that it takes up less space.
This is desirable for data storage and data communication.
• Encoding means converting the String into Binary codes
• Decompression:
In the decompression we convert the Binary codes into the Original string.
Fixed
Fixedand
andVariable
VariableLength
LengthCode
Code
Advantage Disadvantage
Access is fast because the Using Fixed length records, the records
computer knows where each are usually larger and therefore need
Word starts more storage space and are slower to
transfer
Fixed
Fixedand
andVariable
VariableLength
LengthCode
Code
Advantage Disadvantages
Variable-length codes over fixed Where a character ends
length is short codes that can be and another begins,
given to characters that occur difficult to identify.
frequently.
Prefix
PrefixProperty
Property
• A code has the prefix property, code assigned to one character is not the prefix
of code assigned to any other character.
• Example:
Step-04:
•Keep repeating Step-02 and Step-03 until all the nodes form a single tree.
•The tree finally obtained is the desired Huffman Tree.
Sample
Sample
Message: aabacb
Formulae
Formulae
The following 2 formulas are important to solve the problems based on Huffman
Coding-
Formula-01:
Formula-02:
•Total number of bits in Huffman encoded message
= Total number of characters in the message x Average code length per character
= ∑ ( frequencyi x Code lengthi )
Example
Example
Problem-
A file contains the following characters with the frequencies as
shown. If Huffman Coding is used for data compression,
determine-
[Link] Code for each character
[Link] code length
[Link] of Huffman encoded message (in bits)
First let us construct the Huffman Tree.
•Huffman Tree is constructed in the following steps-
Step-01:
Step-02:
Step-03:
Step-04:
Step-05:
Step-06:
Step-07:
Now,
•We assign weight to all the edges of the constructed Huffman Tree.
•Let us assign weight ‘0’ to the left edges and weight ‘1’ to the right edges
Rule
•If you assign weight ‘0’ to the left edges, then assign weight ‘1’ to the right edges.
•If you assign weight ‘1’ to the left edges, then assign weight ‘0’ to the right edges.
•Any of the above two conventions may be followed.
•But follow the same convention at the time of decoding that is adopted at the time
of encoding.
Huffman Code fo each Character
To write Huffman Code for any character, traverse the Huffman Tree from root
node to the leaf node of that character.
Following this rule, the Huffman Code for each character is-
•a = 111
From here, we can observe-
•e = 10 •Characters occurring less frequently in the text are assigned the
•i = 00 larger code.
•o = 11001 •Characters occurring more frequently in the text are assigned
the smaller code.
•u = 1101
•s = 01
•t = 11000
Average Code Length
Using formula-01
Average code length
= ∑ ( frequencyi x code lengthi ) / ∑ ( frequencyi )
= { (10 x 3) + (15 x 2) + (12 x 2) + (3 x 5) + (4 x 4) + (13 x 2) + (1 x 5) } / (10 +
15 + 12 + 3 + 4 + 13 + 1)
= 2.52
Length of Huffman Encoded Message
Using formula-02
Total number of bits in Huffman encoded message
= Total number of characters in the message x Average code length per
character
= 58 x 2.52
= 146.16
≅ 147 bits
Applications
Applications
• Huffman coding is a technique used to compress files for transmission
• Uses statistical coding
• more frequently used symbols have shorter code words
• Works well for text and fax transmissions
• An application that uses several data structures
Home
HomeWork
Work
Suppose a data file has the following characters and the frequencies. If huffman coding
is used, calculate:
•Huffman Code of each character
•Average code length Characters Frequencies
•Length of Huffman encoded data
A 12
B 15
C 7
D 13
E 9
Home
HomeWork
Work
Suppose a data file has the following characters and the frequencies. If huffman coding
is used, calculate:
•Huffman Code of each character
•Average code length
•Length of Huffman encoded data
Letter Z K M C U D L E
Frequency 2 7 24 32 37 42 42 120
Department of CSE
Merge Sort
Merge Sort
i m j
0 1 2 3 4 5 6 7 8
temp 2
k
0 1 2 3 4 5 6 7 8
arr 2 4 6 8 9 3 5 7 10
i m j
0 1 2 3 4 5 6 7 8
temp 2 3
k
0 1 2 3 4 5 6 7 8
arr 2 4 6 8 9 3 5 7 10
i m j
0 1 2 3 4 5 6 7 8
temp 2 3 4
k
0 1 2 3 4 5 6 7 8
arr 2 4 6 8 9 3 5 7 10
i m j
0 1 2 3 4 5 6 7 8
temp 2 3 4 5
k
0 1 2 3 4 5 6 7 8
arr 2 4 6 8 9 3 5 7 10
i m j
0 1 2 3 4 5 6 7 8
temp 2 3 4 5 6
k
0 1 2 3 4 5 6 7 8
arr 2 4 6 8 9 3 5 7 10
i m j
0 1 2 3 4 5 6 7 8
temp 2 3 4 5 6 7
k
0 1 2 3 4 5 6 7 8
arr 2 4 6 8 9 3 5 7 10
i m j
0 1 2 3 4 5 6 7 8
temp 2 3 4 5 6 7 8
k
0 1 2 3 4 5 6 7 8
arr 2 4 6 8 9 3 5 7 10
mi j
0 1 2 3 4 5 6 7 8
temp 2 3 4 5 6 7 8 9
k
0 1 2 3 4 5 6 7 8
arr 2 4 6 8 9 3 5 7 10
m i j
0 1 2 3 4 5 6 7 8
temp 2 3 4 5 6 7 8 9 10
k
The final sorted list
Algorithm
Quick
QuickSort
Sort
Quick sort
Quick sort
• Quick Sort is a Divide and Conquer algorithm.
• It picks an element as pivot and partitions the given array around the picked pivot.
• There are many different versions of quick sort that pick pivot in different ways.
• Always pick first element as pivot.
• Always pick last element as pivot
• Pick a random element as pivot.
• Pick median as pivot.
Best case O(nlogn)
Average case O(nlogn)
Worst case O(n^2)
Memory Average: logn
Worst: n
Stable NO
Inplace Yes
Quick sort algorithm
Step 1 − Choose the highest index value has pivot
Step 2 − Take two variables to point left and right of the list excluding pivot
Step 3 − left points to the low index -1
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot
EXAMPLE
List partition
5 3 8 1 4 6 2 7
P L R
5 3 8 1 4 6 2 7
P L R
5 3 8 1 4 6 2 7
P L R
5 3 8 1 4 6 2 7
P L R
5 3 2 1 4 6 8 7
P L R
5 3 2 1 4 6 8 7
P L R
5 3 2 1 4 6 8 7
P L R
5 3 2 1 4 6 8 7
P LR
5 3 2 1 4 6 8 7
R crossed L
P SWAP ARR[R] = ARR[P]
R L
4 3 2 1 5 6 8 7
P L R P L R
• Best case: The pivot chosen always divides the array into two equal halves.
• Average case: The pivot divides the array into two subarrays that are not necessarily
equal but reasonably balanced.
• Worst case: The pivot chosen is always the smallest or largest element, leading to highly
unbalanced partitions.
Best case
Average case
Worst case
Examples
1. Sort the following elements using Quick sort technique using first element as
pivot: 54, 26, 93, 17, 77, 31, 44, 55, 20
2. Sort the following elements using Quick sort technique using last element as
pivot: 10, 80, 30, 90, 40, 50, 70, 60
Strassen’s
Strassen’sMatrix
MatrixMultiplication
Multiplication
Matrix
MatrixMultiplication
Multiplication
c11 c12 1 1 2 2 5 5 6 6
Then, C11 C12 1 1 2 2 5 5 6 6
C
C11=A11B11+A12B21 C 21 C 22 3 3 4 4 7 7 8 8
c21 c22 3 3 4 4 7 7 8 8
C12=A11B12+A12B22 A21 A 22B 21 B 22
C21=A21B11+A22B21
C22=A21B12+A22B22
Each of these four equations specifies two multiplications of n/2×n/2 matrices and the
addition of their n/2×n/2 products.
We can derive the following recurrence relation:
T(n)= 1 if n=1
8T(n/2)+ 4n2 if n>1
Master
MasterTheorem
Theorem
Strassen’s
Strassen’smethod
method
T(n)= 1 if n=1
7T(n/2)+ 18n2 if n>1
Conclusion
Conclusion
• The number 2.81 may not seem much smaller than 3, but because the difference is
in the exponent, the impact on running time is significant.
• In fact, Strassen’s algorithm beats the ordinary algorithm on today’s machines.
Convex Hull
Convex vs Concave
• A polygon P is convex if, for every pair of points x and y in P, the line xy
is also in P; otherwise, it is called concave.
x
P y x
P y
concave convex
Convex Hull Problem
Steps:
1. Sort the Points:
1. Sort the given points based on their x-coordinates (if x-coordinates are the same, use y-
coordinates).
2. Sorting takes O(nlogn)
2. Divide the Points:
1. Split the sorted points into two halves (left and right).
3. Recursive Hull Computation:
1. Recursively find the convex hulls for both halves.
4. Merge the Hulls:
1. Find the upper and lower tangents to combine the two convex hulls.
2. Remove the points that are not part of the final convex hull.
Divide & Conquer
•
Ex: Draw the convex hull for the following points
(1,1),(2,3),(3,5),(5,3),(3,2),(2,2),(6,1),(7,4)
(0, 0), (0, 4), (-4, 0), (5, 0), (0, -6), (1, 0)
Department of CSE
Time Complexity
Time Complexity
Recurrence Relations
Substitution Method
Example
Master Theorem
Examples
Types of recursive calls
Linear Recursion:
Example: T(n) = T(n-1) + O(1)
Time Complexity: O(n)
Logarithmic Reduction:
Example: T(n) = T(n/2) + O(1)
Time Complexity: O(log n)