Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit3

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 248

Noida Institute of Engineering and Technology, Greater Noida

Divide and Conquer and Greedy Method

Unit: 3

Design & Analysis of Algorithms


ANKITA SHARMA
ACSE0501
Assitant Professor
B.TECH , CSE
CSE 4TH SEM

ANKITA SHARMA ACSE0401 UNIT 3


1
12/15/2024
Evaluation Scheme

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 2


Subject Introduction

The word Algorithm means ” A set of rules to be followed in calculations or other


problem-solving operations ” Or ” A procedure for solving a mathematical problem in a
finite number of steps that frequently by recursive operations “.

1. https://www.youtube.com/watch?v=0IAPZzGSbME

2. https://www.youtube.com/watch?v=yOl-EpY39kM

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 3


Syllabus

ANKITA SHARMA ACSE0401 UNIT 3


12/15/2024 4
Branch wise Application

• First, we will start with the internet which is very much important for our daily life and we
cannot even imagine our life without the internet and it is the outcome of clever and
creative algorithms. Numerous sites on the internet can operate and falsify this huge
number of data only with the help of these algorithms.

• The everyday electronic commerce activities are massively subject to our data, for
example, credit or debit card numbers, passwords, OTPs, and many more. The centre
technologies used incorporate public-key cryptocurrency and digital signatures which
depend on mathematical algorithms.

• Even an application that doesn't need algorithm content at the application level depends
vigorously on the algorithm as the application relies upon hardware, GUI, networking, or
object direction and all of these create a substantial use of algorithms.

• There are some other vital use cases where the algorithm has been used such as if we
watch any video on YouTube then next time we will get related-type advice as
recommended videos for us.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 5


Course Objective

• Upon completion of this course, students will be able to do the


following:
• Analyze the asymptotic performance of algorithms.
• Write rigorous correctness proofs for algorithms.
• Demonstrate a familiarity with major algorithms and data
structures.
• Apply important algorithmic design paradigms and methods of
analysis.
• Synthesize efficient algorithms in common engineering design
situations.

ANKITA SHARMA ACSE0401 UNIT 3


12/15/2024 6
Course Outcome

At the end of the semester, the student will be able:


Description Bloom’s Taxonomy
CO1 To have knowledge of basic principles of algorithm design and Knowledge, analysis
Analysis, asymptotic notations and growth of functions for time And design
and space complexity analysis and applying the same in
different sorting algorithms
CO2 To apply different problem-solving approaches for advanced Knowledge, analysis
data structures And apply
CO3 To apply divide and conquer method for solving merge sort, Knowledge, analysis and
quick sort, matrix multiplication and Greedy Algorithm for Apply
solving different Graph Problem.

CO4 To analyze and apply different optimization techniques like Knowledge, Analysis And
dynamic programming, backtracking and Branch & Bound to Apply
solve the complex problems
CO5 To understand the advanced concepts like NP Completeness and Knowledge, Analysis and
Fast Fourier Transform, to analyze and apply String Matching, Apply
Approximation and Randomized Algorithms to solve the
complex problems

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 7


Program Outcome

At the end of the semester, the student will be able:


POs Engineering Graduates will be able to
PO1 Engineering Knowledge
PO2 Problem Analysis
PO3 Design & Development of solutions
PO4 Conduct Investigation of complex problems
PO5 Modern Tool Usage
PO6 The Engineer and Society
PO7 Environment and sustainability
PO8 Ethics
PO9 Individual & Team work
PO10 Communication
PO11 Project management and Finance
PO12 Life Long Learning

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 8


CO-PO and PSO Mapping

Design and Analysis of Algorithm (kCS-502)

PO1
CO.K PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO12
1

ACSE0401.1 3 3 3 3 2 - - - 2 2 - 3

ACSE0401.2 3 3 3 3 2 2 - 1 1 1 - 3

ACSE0401.3 3 3 2 3 3 2 - 2 1 1 2 3

ACSE0401.4 3 3 3 3 2 2 - 2 2 1 3 3

ACSE0401.5 2 2 2 2 2 2 - 2 1 1 1 2

Average 2.8 2.8 2.6 2.8 2.2 1.6 - 1.8 1.4 1.2 1.2 2.8

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 9


Program Educational Objectives(PEOs)

PEO1: To have an excellent scientific and engineering breadth so as to


comprehend, analyze, design and provide sustainable solutions for
real-life problems using state-of-the-art technologies.
PEO2:To have a successful career in industries, to pursue higher studies or
to support enterpreneurial endeavors and to face global challenges.
PEO3:To have an effective communication skills, professional attitude,
ethical values and a desire to learn specific knowledge in emerging
trends, technologies for research, innovation and product
development and contribution to society.
PEO4: To have life-long learning for up-skilling and re-skilling for
successful professional career as engineer, scientist, enterpreneur
and bureaucrat for betterment of society

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 10


End Semester Question Paper Template

B TECH
(SEM-V) THEORY EXAMINATION 20__-20__
Design and Analysis of Algorithms
Time: 3 Hours Total Marks:
100
Note: 1. Attempt all Sections. If require any missing data; then choose
suitably.
SECTION A
1.Q.No.
Attempt all questions in brief.
Question 2 x 10 =CO
Marks 20
1 2
2 2
. .
10 2

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 11


End Semester Question Paper Templates

SECTION B
2. Attempt any three of the following: 3 x 10 = 30

Q.No. Question Marks CO


1 10
2 10
. .
5 10

SECTION C
3. Attempt any one part of the following: 1 x 10 = 10
Q.No. Question Marks CO

1 10
2 10
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 12
End Semester Question Paper Templates
4. Attempt any one part of the following: 1 x 10 = 10
Q.No. Question Marks CO

1 10
2 10
5. Attempt any one part of the following: 1 x 10 = 10
Q.No. Question Marks CO
1 10
2 10

6. Attempt any one part of the following: 1 x 10 = 10


Q.No. Question Marks CO

1 10
2 10

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 13


Prerequisite and Recap

• Prerequisite
• Basic concept of c programming language.
• Concept of stack, queue and link list.

• Recap
• Flow Chart
• Algorithm

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 14


Topic Objective- Divide and Conquer

• The divide-and-conquer paradigm is often used to find an optimal solution of a


problem.

• Its basic idea is to decompose a given problem into two or more similar, but
simpler, subproblems, to solve them in turn, and to compose their solutions to
solve the given problem.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 15


Divide And Conquer(CO3)

The topic will cover following contents


• Sorting: merge sort and quick sort
• Binary search
• Convex hull – Quick Hull algorithm
• Matrix multiplication - Strassen’s algorithm

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 16


Divide and Conquer(CO3)

• Divide the problem into a number of sub-problems


– Similar sub-problems of smaller size

• Conquer the sub-problems


– Solve the sub-problems recursively
– Sub-problem size small enough  solve the problems in
straightforward manner

• Combine the solutions of the sub-problems


– Obtain the solution for the original problem

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 17


Divide-and-conquer technique(CO3)

a problem of size n

subproblem 1 subproblem 2
of size n/2 of size n/2

a solution to a solution to
subproblem 1 subproblem 2

a solution to
the original problem

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 18


Divide and Conquer(CO3)

• Merge Sort
• To sort an array A[p . . r]:
• Divide
– Divide the n-element sequence to be sorted into two
subsequences of n/2 elements each
• Conquer
– Sort the subsequences recursively using merge sort
– When the size of the sequences is 1 there is nothing more to do
• Combine
– Merge the two sorted subsequences

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 19


Divide and Conquer(CO3)

p q r
1 2 3 4 5 6 7 8

Algorithm.: MERGE-SORT(A, p, r) 5 2 4 7 1 3 2 6

if p < r Check for base case

then q ← (p + r)/2 Divide


MERGE-SORT(A, p, q) Conquer
MERGE-SORT(A, q + 1, r) Conquer
MERGE(A, p, q, r) Combine

• Initial call: MERGE-SORT(A, 1, n)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 20


Divide and Conquer(CO3)

1 2 3 4 5 6 7 8

Divide 5 2 4 7 1 3 2 6 q=4

1 2 3 4 5 6 7 8

5 2 4 7 1 3 2 6

1 2 3 4 5 6 7 8

5 2 4 7 1 3 2 6

1 2 3 4 5 6 7 8

5 2 4 7 1 3 2 6

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 21


Divide and Conquer(CO3)

1 2 3 4 5 6 7 8

Conquer 1 2 2 3 4 5 6 7
and
Merge 1 2 3 4 5 6 7 8

2 4 5 7 1 2 3 6

1 2 3 4 5 6 7 8

2 5 4 7 1 3 2 6

1 2 3 4 5 6 7 8

5 2 4 7 1 3 2 6

ANKITA SHARMA ACSE0401 UNIT 3


12/15/2024 22
Divide and Conquer(CO3)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 23


Divide and Conquer(CO3)
Merge Sort

• Divide:
– compute q as the average of p and r: D(n) = (1)
• Conquer:
– recursively solve 2 subproblems, each of size n/2  2T
(n/2)
• Combine:
– MERGE on an n-element subarray takes (n) time 
C(n) = (n)
(1) if n =1
T(n) = 2T(n/2) + (n) if n > 1

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 24


Divide and Conquer(CO3)

• MergeSort Algorithm
• The MergeSort function repeatedly divides
the array into two halves until we reach a
stage where we try to perform MergeSort on
a subarray of size 1 i.e. p == r.
• After that, the merge function comes into
play and combines the sorted arrays into
larger arrays until the whole array is merged.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 25


Divide and Conquer(CO3)

To sort an entire array , we need to call MergeSort (A , 0, length(A) - 1

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 26


Divide and Conquer(CO3)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 27


Quick Sort (CO3)
Quicksort, like merge sort, applies the divide-and-conquer
method.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 28


Quick Sort (CO3)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 29


Quick Sort (CO3)
Worst-case running time

When we total up the partitioning times for each level, we get :


In big-Θ notation, quicksort's worst-case running time is big Theta(n^2)

Best-case running time


Quicksort's best case occurs when the partitions are as evenly balanced as possible:
their sizes either are equal or are within 1 of each other.

Average Case :

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 30


Divide and Conquer(CO3)

• Select a pivot (partitioning element)


• Rearrange the list so that all the elements in the positions before the
pivot are smaller than or equal to the pivot and those after the pivot
are larger than the pivot (See algorithm Partition in section 4.2)
• Exchange the pivot with the last element in the first (i.e., ≤ sublist) –
the pivot is now in its final position
• Sort the two sublists

A[i]≤p A[i]>p

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 31


Divide and Conquer(CO3)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 32


Divide and Conquer(CO3)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 33


Strassen Multiplication Method
(CO3)
• Strassen's algorithm, developed by Volker
Strassen in 1969, reduces the number of
necessary multiplication operations from 8 to 7
compared to the standard method. It uses the
Divide and Conquer technique, breaking the
input matrices into smaller matrices and
recursively performing the operation.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 34


Divide and Conquer(CO3)

Strassen Multiplication Method

• Strassen observed [1969] that the product of two matrices


A and B (of size 2nx2n) can be computed as follows:

C00 C01 A00 A01 B00 B01


= *
C10 C11 A10 A11 B10 B11

M1 + M4 - M 5 + M7 M 3 + M5
=
M2 + M4 M1 + M3 - M 2 + M6

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 35


Divide and Conquer(CO3)
• Strassen Multiplication Method
Sub Matrices
• M1 = (A00 + A11) * (B00 + B11)
• M2 = (A10 + A11) * B00
• M3 = A00 * (B01 - B11)
• M4 = A11 * (B10 - B00)
• M5 = (A00 + A01) * B11
• M6 = (A10 - A00) * (B00 + B01)
• M7 = (A01 - A11) * (B10 + B11)
Time Complexity of Strassen’s Method
• Addition and Subtraction of two matrices takes O(N2) time.
recurrence
T(N) = relation can be
7T(N/2) + written
O(N2) as :
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 36
Practice Questions

1. Compute the product matrix using Strassen's matrix


multiplication algorithm.
Given a11=1; a12=3; a21=5; a22=7
b11=8; b12=4; b21=6; b22=2
2. Describe Best and Worst Case complexities of Quick Sort in
detail
3. Describe in detail the Strassen Matrix Multiplication
algorithms based on divide and conquer strategies using suitable
example .

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 37


Divide and Conquer(CO3)

• Time Complexity of Strassen’s Method


Addition and Subtraction of two matrices takes O(N2) time. So time
complexity can be written as
• T(N) = 7T(N/2) + O(N2)
• From Master's Theorem, time complexity of above method is
O(Nlog7) which is approximately O(N2.8074)

• For more examples refer the link


• https://www.youtube.com/watch?v=0oJyNmEbS4w

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 38


Convex Hull(CO3)
• What is convex hull problem?
A convex hull is the smallest convex polygon that completely
encloses a set of points in a two-dimensional or three-
dimensional space.

• The first step is to find out the farthest two points in the plane:

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 39


Convex Hull(CO3)
• Then, in the two spaces S1 and S2, we will find
out the farthest point:

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 40


Convex Hull(CO3)
We will continue to do operations.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 41


Convex Hull(CO3)
Finally, Our resultant polygon would look
something like this:

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 42


Topic Objective- Greedy Algorithm

This objective of this topic is to make the students aware


about the following concepts
• Graph
• Dijkstra Algorithm
• Kruskals Algorithm
• Prim Algorithm
• Huffman Coding
• Activity Selection Problem
• Knapsack problem

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 43


Convex Hull(CO3)
• What are the applications of the convex hull
problem?
• The problem has various applications, which
include:
• Geographic Information Systems
• Image Processing
• Pattern Recognition
• Game Theory

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 44


Quiz
1. Given the recurrence f(n) = 4 f(n/2) + 1, how many sub-problems will a divide-and-conquer algorithm
divide the original problem into, and what will be the size of those sub-problems?

a) 4 sub-problems, each of size 2

b) 4 sub-problems, each of size n/2

c) 2 sub-problems, each of size 4

d) 2 sub-problems, each of size n/4

2. A SORTING TECHNIQUE THAT GUARANTEES, THAT RECORDS WITH THE SAME PRIMARY KEY OCCURS, IN
THE SAME ORDER IN THE SORTED LIST AS IN THE ORIGINAL UNSORTED LIST IS SAID TO BE:

a) Stable

b) Consistent

c) External

d) Linear

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 45


Greedy Algorithm (CO3)

Greedy Algorithm
• To begin with, the solution set (containing answers) is
empty.
• At each step, an item is added to the solution set until
a solution is reached.
• If the solution set is feasible(which is satisfying the
condition given in question ), the current item is kept.
• Else, the item is rejected and never considered again.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 46


Greedy Algorithm (CO3)

Algorithm Greedy (a,n ) a1 a2 a3 a4 a5


{ for i =1 to n do
{ x=select (a);
if Feasible(x) then
{ solution = solution + x ;
}
}}

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 47


Greedy Algorithm : Knapsack (CO3)
The fractional knapsack problem is also one of the
techniques which are used to solve the knapsack
problem.

In fractional knapsack, the items can be broken in order


to maximize the profit. ie ; can even put the fraction of
any item into the knapsack if taking the complete item
is not possible.

The problem in which we break the item is known as a


Fractional knapsack problem.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 48


Greedy Algorithm : Knapsack (CO3)
The problem states-
• Which items should be placed into the
knapsack such that-
• The value or profit obtained by putting the
items into the knapsack is maximum.
• And the weight limit of the knapsack does not
exceed.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 49


Greedy Algorithm : Knapsack (CO3)
Fractional knapsack problem is solved using greedy
method in the following steps-
Step-01:
For each item, compute its value / weight ratio.
Step-02:
Arrange all the items in decreasing order of their
value / weight ratio.
Step-03:
Start putting the items into the knapsack beginning
from the item with the highest ratio.
Put as many items as you can into the knapsack.
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 50
Greedy Algorithm : Knapsack (CO3)
Example : For the given set of items and
knapsack capacity = 60 kg, find the optimal
solution for the fractional knapsack problem
making use of greedy approach.

Item or object : 1 2 3 4 5
Weight : 5 10 15 22 25
Value or profit: 30 40 45 77 90

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 51


Greedy Algorithm : Knapsack (CO3)
Step 1 : Compute the (value / weight) ratio for
each item- Value
Items Weight or Ratio
Profit
1 5 30 6
2 10 40 4
3 15 45 3
4 22 77 3.5

5 25 90 3.6

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 52


Greedy Algorithm (CO3)
Step-02:
Sort all the items in decreasing order of their
(value / weight) ratio-
Value/weight : 6 4 3.6 3.5 3
Item : 1 2 5 4 3

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 53


Greedy Algorithm : Knapsack (CO3)
Step-03:
Start filling the knapsack by putting the items
into it one by one.
Knapsack Items
profit
Weight in Knapsack

60 Ø 0

//Reduced wt.5 of 60-5=55 1 30


item 1 and so on // 55-10=45 1, 2 70

45-25=20 1, 2, 5 160
// wt.20 can be
added out of 22
from item 4 .//
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 54
Greedy Algorithm : Knapsack (CO3)
Now,
• Knapsack weight left to be filled is 20 kg but item-4
has a weight of 22 kg.
• Since in fractional knapsack problem, even the fraction
of any item can be taken.
• So, knapsack will contain the following items-
• < 1 , 2 , 5 , (20 out of 22) 4 >

Total cost of the knapsack


= 160 + (20/22) x 77 //here summation of all (item* profit))//
= 160 + 70
= 230 units
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 55
Greedy Algorithm : Knapsack (CO3)
Time Complexity-

Time Complexity: O( N*LogN), where N is the


number of items given. Finding the ratio and
sorting them in the descending order using
merge sort, has the overall time complexity of
O(N* Log N).
Space Compleity: O(N), where N is the number
of items given.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 56


Greedy Algorithm (CO3)

Example-

Let us consider that the capacity of the knapsack W = 60 and the list of
provided items are shown in the following table −

Item A B C D

Profit 280 100 120 120

Weight 40 10 20 24

Ratio (piwi)
(piwi) 7 10 6 5

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 57


Greedy Algorithm (CO3)

he provided items are not sorted based on pi/wi. After sorting, the items are as shown in the following t

Item B A C D

Profit 100 280 120 120

Weight 10 40 20 24

Ratio (piwi)
(piwi) 10 7 6 5

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 58


Greedy Algorithm (CO3)

Solution
After sorting all the items according to pi/wi. First all of B is chosen as weight of B is less
than the capacity of the knapsack. Next, item A is chosen, as the available capacity of
the knapsack is greater than the weight of A. Now, C is chosen as the next item.
However, the whole item cannot be chosen as the remaining capacity of the knapsack is
less than the weight of C.

Hence, fraction of C (i.e. (60 − 50)/20) is chosen.

Now, the capacity of the Knapsack is equal to the selected items. Hence, no more item
can be selected.

The total weight of the selected items is 10 + 40 + 20 * (10/20) = 60

And the total profit is 100 + 280 + 120 * (10/20) = 380 + 60 = 440

This is the optimal solution. We cannot gain more profit selecting any different
combination of items.
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 59
Greedy Algorithm (CO3)

Algorithm:

Greedy-Fractional-Knapsack (w[1..n], p[1..n], W)


for i = 1 to n
do x[i] = 0
weight = 0
for i = 1 to n
if weight + w[i] ≤ W then
x[i] = 1
weight = weight + w[i]
else
x[i] = (W - weight) / w[i]
weight = W
break
return x

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 60


Greedy Algorithm (CO3)

Activity Selection
Greedy Activity Selector (s, f)

1. n ←length[s]
2. A ← {a1}
3. i←1
4. For m ← 2 to n
5. do if sm >= fi
6. then A ← A U {am}
7. i←m
8. Return A

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 61


Greedy Algorithm (CO3)

Example 1 : Consider the following 3 activities sorted by


by finish time.

start[] = {10, 12, 20};


finish[] = {20, 25, 30};

A person can perform at most two activities. The maximum set of activities that can be
executed is {0, 2} [ These are indexes in start[] and finish[].

Example 2 : Consider the following 6 activities


sorted by by finish time.

start[] = {1, 3, 0, 5, 8, 5};


finish[] = {2, 4, 6, 7, 9, 9};

A person can perform at most four activities. The maximum set of activities that can be
executed is {0, 1, 3, 4} [ These are indexes in start[] and finish[].

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 62


Greedy Algorithm : Activity Selection (CO3)
The Activity Selection Problem is an
optimization problem which deals with the
selection of non-conflicting activities that needs
to be executed by a single person or machine in a
given time frame.

Each activity is marked by a start and finish time.


Greedy technique is used for finding the solution
since this is an optimization problem.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 63


Greedy Algorithm : Activity Selection (CO3)
Problem Statement : given n activities with their
start and finish times. Select the maximum
number of activities that can be performed by a
single person, assuming that a person can only
work on single activity at a time .
Example :

Here A1 starts after 12 unit time and ends at 25 ,


similarly A2 starts after 10 unit time end at 20 ,
same for A3
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 64
Greedy Algorithm : Activity Selection (CO3)
Now , there is a person who has to perform
maximum number of activity out of these three.
Lets see how many he can ,

He starts with A2 it starts at 10 and end at 20 .


Now left with A1 and A3 but can’t start A1 becoz
it’s start time has already finish so next he takes
A3 as start time is 20 that is still achievable.

He would be able to make 2 out of 3 activity.


12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 65
Greedy Algorithm : Activity Selection (CO3)

Greedy approach can be used to find the


solution since we want to maximize the count of
activities that can be executed. This approach
will greedily choose an activity with earliest
finish time at every step, thus yielding an
optimal solution.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 66


Greedy Algorithm : Activity Selection (CO3)

Input Data for the Algorithm:


•act[] array containing all the activities.
•s[] array containing the starting time of all the activities.
•f[] array containing the finishing time of all the
activities.

Output Data for Algorithm :


• Sol[] array referring to the solution set containing the
maximum number of non- conflicting activities

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 67


Greedy Algorithm : Activity Selection (CO3)
Steps for Activity Selection Problem
Following are the steps we will be following to
solve the activity selection problem,

Step 1: Sort the given activities in ascending


order according to their finishing time.

Step 2: Select the first activity from sorted array


act[] and add it to sol[] array .

Step 3: Repeat step 4 and 5 for remaining


activities in act [].
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 68
Greedy Algorithm : Activity Selection (CO3)

Step 4 : if the start t time of currently selected


activity is greater than or equal to the finish time
of previously selected activity , then add it to the
sol[] array.

Step 5 : select the next activity in act[] array.

Step 6 : Print the sol[] array.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 69


Greedy Algorithm : Activity Selection (CO3)

Example : Start Finish Activity


Time (s) Time (f) Name

5 9 a1
1 2 a2
3 4 a3
0 6 a4
5 7 a5
8 9 a6

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 70


Greedy Algorithm : Activity Selection (CO3)

Solution :
Step 1: Sort the given activities in ascending order
according to their finishing time.
The table after we have sorted it:
Start Finish Activity
Time (s) Time (f) Name
1 2 a2
3 4 a3
0 6 a4
5 7 a5
5 9 a1
12/15/2024
8 9
ANKITA SHARMA ACSE0401
a6
UNIT 3 71
Greedy Algorithm : Activity Selection (CO3)

Step 2 : Select the first activity from sorted array act[]


and add it to sol[] array , thus sol = {a2} .

Step 3 : Repeat the steps 4 and 5 for the remaining


activities in act [].

Step 4 :if start time of current activity is greater than


equal to finish time of previously selected activity then
add it to sol[].

Step 5: Select the next activity in act []

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 72


Greedy Algorithm : Activity Selection (CO3)
For the data given in the above table,
A.Select activity a3. Since the start time of a3 is
greater than the finish time of a2 (i.e. s(a3) > f(a2)),
we add a3 to the solution set. Thus sol = {a2, a3}.
B.Select a4. Since s(a4) < f(a3), it is not added to the
solution set.
C.Select a5. Since s(a5) > f(a3), a5 gets added to
solution set. Thus sol = {a2, a3, a5}
D.Select a1. Since s(a1) < f(a5), a1 is not added to the
solution set.
E.Select a6. a6 is added to the solution set since s(a6)
> f(a5). Thus sol = {a2, a3, a5, a6}.
Step 6: At last, print the array sol[]
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 73
Greedy Algorithm : Activity Selection (CO3)

Hence, the execution schedule of maximum


number of non-conflicting activities will be:
Output
(1,2)
(3,4)
(5,7)
(8,9)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 74


Greedy Algorithm : Task Scheduling(CO3)

//Execution time of jobs is 1 unit.


Have to make schedule which tells us , that which JOB will be executed by CPU first.
Here we have N activities available and every activity is associated with deadline.
If activity deadline passed then penalty.
So we have to make schedule in such a ANKITA
12/15/2024 way that minimum
SHARMA ACSE0401 penalty
UNIT 3 will occur. 75
//
Greedy Algorithm (CO3)

Following is the algorithm.

1) Sort all jobs in decreasing order of profit.

2) Iterate on jobs in decreasing order of profit.For each job , do the following :

a)Find a time slot i, such that slot is empty and i < deadline and i is greatest.Put the
job in this slot and mark this slot filled.

b)If no such i exists, then ignore the job.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 76


Example: Find the optimal schedule for the following task with given weight (penalties)
and deadlines.

1 2 3 4 5 6 7
di 4 2 4 3 1 4 6
wi 70 60 50 40 30 20 10

Solution: According to the Greedy algorithm we sort the jobs in decreasing order of their
penalties so that minimum of penalties will be charged.

In this problem, we can see that the maximum time for which uniprocessor machine will
run in 6 units because it is the maximum deadline.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 77


Let Ti represents the tasks where i = 1 to 7

T5 and T6 cannot be accepted after T7 so penalty is

w5 + w6 = 30 + 20 = 50 (2 3 4 1 7 5 6)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 78


Other schedule is

(2 4 1 3 7 5 6)
There can be many other schedules but (2 4 1 3 7 5 6) is optimal.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 79


Task Scheduling-Greedy Algorithm

Have to make choices :which activity to


schedule (on the basis of penalty and
deadline) .Greedy Approach is perfect here
bcz we make optimal choice .

Example :- number of activity =5

A B C D E
Deadline:- 2 1 2 1 3
Penalty :- 100 19 27 25 15
(**have to make least penalty)
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 80
Task Scheduling-Greedy Algorithm
Solution: According to the Greedy algorithm we
sort the jobs in decreasing order of their penalties so
that minimum of penalties will be charged.Let’s
Schedule A first , because if it gets miss Penalty of
100 will occur.
TASK : A C D B E
DEADLINE: 2 2 1 1 3
PENALTY : 100 27 25 19 15

Lets make empty schedule for unit jobs

0 1 2 3 4 5
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 81
Task Scheduling-Greedy Algorithm

• Lets start with highest Penalty ie; A , its


deadline is 2
 Look at Gantt chart and search for 2 , also look
for empty box in backward direction of
deadline.
0 1 2 3 4 5
 A is placed in nearest empty box of deadline
and next is C its deadline is also 2.
C A
0
12/15/2024 1 2 3 4
ANKITA SHARMA ACSE0401 5
UNIT 3 82
Task Scheduling-Greedy Algorithm

 Next check for D but its Deadline is 1 and we don’t


have Empty box iif we check in backward direction.,
similarly for B can’t schedule it.
 Check for next ie; B its deadline is 3 it can be
scheduled.
C A E D B

0 1 2 3 4 5
 C , A , E were the part of schedule but we get the
penalty for D , B
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 83
Task Scheduling-Greedy Algorithm

Total Penalty will be equal to the Sum of Penalties of


Job D and B = 25 +19 =34

Total Profit will be equal to sum of penalties of job A


,C and E = 100+ 27 +15 =142

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 84


Task Scheduling-Greedy Algorithm

Time Complexity is (NlogN)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 85


Spanning Tree -Greedy Algorithm (CO3)

• A spanning tree is a subset of Graph G, which has


all the vertices covered with minimum possible
number of edges.

• A spanning tree does not have cycles and it cannot


be disconnected.

• The only difference between a spanning tree and a


graph is that a spanning tree does not have a cycle
and a minimum number of edges possible.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 86


Spanning Tree -Greedy Algorithm (CO3)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 87


Spanning Tree -Greedy Algorithm (CO3)

The following Spanning trees are possible from the


above graph:

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 88


Spanning Tree -Greedy Algorithm (CO3)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 89


Spanning Tree -Greedy Algorithm (CO3)

Properties of Spanning Tree


• A connected graph can have multiple spanning
trees. A graph with n vertices can have an n^(n-2)
number of spanning trees.
• Spanning trees does not have any loop or cycle.
• Spanning trees have n vertices and n-1 number of
edges.
• All spanning tree of a graph has equivalent
vertices(vertices equal to graph).
• Removing a single edge from the spanning tree
will make the graph disconnected as the spanning
tree is minimal connected.
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 90
Spanning Tree -Greedy Algorithm (CO3)

• Adding any edge can create a loop or cycle in the


spanning tree.
• Spanning trees can be formed on connected graphs
only, disconnected graphs cannot form spanning
trees.

Application of Spanning Tree:

Civil Network Planning


Computer Network Routing Protocol
Cluster Analysis

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 91


Minimum Spanning Tree -Greedy Algorithm

A minimum spanning tree or minimum cost


spanning tree is that spanning tree, which covers all
the vertices of the graph with minimum edges and
the sum of the weight of those edges is minimum
among other spanning trees of that graph.

The minimum spanning trees are mainly of two


types:
• Prim’s MST
• Kruskal’s MST
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 92
Greedy Algorithm (CO3)

Minimum Spanning Tree


A spanning tree of a connected graph G contains all the nodes and has
edges, which connects all the nodes. So number of edges will be 1 less
than the number of nodes.

Spanning Trees: A sub graph T of a undirected graph G=(V,E) is a


spanning tree of G if it is a tree and contains every vertex of G.
A minimum spanning tree is a sub graph of an undirected weighted
graph G, such that

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 93


Greedy Algorithm (CO3)

Prims Algorithm
The tree starts from an arbitrary root vertex r and grows until the tree
spans all the vertices in V.

● Select any vertex

● Choose the minimum cost adjacent edge of selected vertex

● Repeat step2 until all node are not explored.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 94


Greedy Algorithm (CO3)

Step 1: Select a starting vertex.

Step 2: Repeat Steps 3 and 4 until there are fringe(unvisited) vertices.

Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that
has minimum weight.

Step 4: Add the selected edge and the vertex to the minimum spanning
tree T.
[END OF LOOP]

Step 5: EXIT

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 95


Greedy Algorithm (CO3)

Prims Algorithm Example


Select any vertex A
B 5 C
Select the shortest
edge connected to that
3 vertex
6 4 AB 3
8
8
A F D
7
5
4 2

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 96


Greedy Algorithm (CO3)

Prims Algorithm Example


B 5 Select the shortest
C edge connected to
3 any vertex already
6 4 connected.
8
8 AE 4
A F D
7
5
4 2

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 97


Greedy Algorithm (CO3)

Prims Algorithm Example


Select the shortest
B 5 C edge connected to
any vertex already
3 connected.
6 4
8
8 ED 2
A F D
7
5
4 2

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 98


Greedy Algorithm (CO3)

Prims Algorithm Example

B 5 C
Select the shortest
edge connected to
3 any vertex already
6 4 connected.
8
8 DC 4
A F D
7
5
4 2

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 99


Greedy Algorithm (CO3)

Prims Algorithm Example

B 5 C
3 Select the shortest
6 4 edge connected to
8 any vertex already
8 connected.
A F D
7 EF 5
5
4 2

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 100


Greedy Algorithm (CO3)

Prims Algorithm Example All vertices have been


connected.
B 5 C
The solution is
3
6 4 AB 3
8 AE 4
8 ED 2
A D DC 4
7 F EF 5
5
4 2 Total weight of tree: 18

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 101


Prim’s MST - Greedy Algorithm

Example : Construct the minimum spanning tree (MST)


for the given graph using Prim’s Algorithm-

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 102


Prim’s MST - Greedy Algorithm

Step 1 :

Step 2:

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 103


Prim’s MST - Greedy Algorithm

Step 3 :

Step 4 :

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 104


Prim’s MST - Greedy Algorithm

Step 5 :

Step 6 :

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 105


Prim’s MST - Greedy Algorithm

Since all the vertices have been included in the MST,


so we stop.

Now, Cost of Minimum Spanning Tree


= Sum of all edge weights
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 106


Practise Questions

Q.1 Explain “greedy algorithm” Write its pseudo


code to prove that fractional Knapsack problem has a
greedy-choice property.
Q.2 Explain Greedy programming in brief.
Q.3 What is Knapsack problem? Solve Fractional
knapsack problem using greedy programming for the
following four items with their weights w = {3, 5, 9, 5}
and values P = {45, 30, 45, 10} with knapsack capacity
is 16.
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 107
Practise Questions

Q.4 Given the six items in the table below and a


Knapsack with Weight 100, what is the solution to the
Knapsack problem in all concepts. I.e. explain greedy
all approaches and find the optimal solution

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 108


Topic Objective- Greedy Algorithm

Q.5 Using Prim’s Algorithm, find the cost of minimum


spanning tree (MST) of the given graph-

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 109


Topic Objective- Greedy Algorithm

The minimum spanning tree obtained by the application


of Prim’s Algorithm on the given graph is as shown
below-

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 110


Topic Objective- Greedy Algorithm

Now, Cost of Minimum Spanning Tree


= Sum of all edge weights
= 1 + 4 + 2 + 6 + 3 + 10
= 26 units

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 111


Greedy Algorithm (CO3)

Algorithm of Kruskal's

For any graph, G=(V,E), finding an MST using Kruskal's algorithm involves
the following steps –

• Sort all the edges of the graph in ascending order of their weights.

• Check the edge with minimum weight, if including it in the answer forms
a cycle discard it, otherwise include it in the answer.

• Repeat the above step until we have chosen V−1 edges.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 112


Greedy Algorithm (CO3)

Kruskal’s working Rule

● Select minimum weighted edge in graph

● Choose the next minimum weighted edge

● Repeat step2 until all edges are not explored

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 113


Greedy Algorithm (CO3)

Kruskal’s Algorithm Example List the edges in


order of size:
B 5 C ED 2
3 AB 3
6 4 AE 4
8
CD 4
8 BC 5
A F D EF 5
7
CF 6
5
4 AF 7
2 BF 8
DF 8
E

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 114


Greedy Algorithm (CO3)

Kruskal’s Algorithm Example

B 5 C Select the shortest


3 edge in the network
6 4
8
ED 2
8
A F D
7
5
4 2

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 115


Greedy Algorithm (CO3)

Kruskal’s Algorithm Example


Select the shortest
B 5 C edge in the network
3
6 4 ED 2
8
AB 3
8
A F D
7
5
4 2

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 116


Greedy Algorithm (CO3)

Kruskal’s Algorithm Example


Select the shortest
B 5 C edge in the network
3
6 4 ED 2
8
AB 3
8 CD 4( or AE 4)
A F D
7
5
4 2

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 117


Greedy Algorithm (CO3)

Kruskal’s Algorithm Example Select the shortest


edge in the network
B 5 C
ED 2
3 AB 3
6 4
8 CD 4( or AE 4)
8 AE 4
A F D
7
5
4 2

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 118


Greedy Algorithm (CO3)

Kruskal’s Algorithm Example Select the shortest


edge in the network
B 5 C
ED 2
3 AB 3
6 4
8 CD 4( or AE 4)
8 AE 4
A
7 F D BC FORMS A CYCLE
EF 5
5
4 2

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 119


Greedy Algorithm (CO3)

Kruskal’s Algorithm Example


Select the shortest
B 5 edge in the network
C
3 ED 2
6 4 AB 3
8
CD 4( or AE 4)
8 AE 4
A F D BC FORMS A CYCLE
7
EF 5
5
4 Total Weight of Tree
2 = 18

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 120


Topic Objective- Greedy Algorithm

Construct the minimum spanning tree (MST)


for the given graph using Kruskal’s
Algorithm-

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 121


Topic Objective- Greedy Algorithm

To construct MST using Kruskal’s Algorithm,


•Simply draw all the vertices on the paper.
•Connect these vertices using edges with
minimum weights such that no cycle gets formed.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 122


Topic Objective- Greedy Algorithm

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 123


Topic Objective- Greedy Algorithm

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 124


Topic Objective- Greedy Algorithm

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 125


Topic Objective- Greedy Algorithm

Since all the vertices have been connected /


included in the MST, so we stop.
Weight of the MST
= Sum of all edge weights
= 10 + 25 + 22 + 12 + 16 + 14= 99 units
Time TAKEN BY Kruskal algorithm :
O(|V| |E|) which can also be written as O (n.e)
that is O( n^2)
But Kruskal can be improved wen we need
value than min heap data structures is one which
always give minimum value .
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 126
Topic Objective- Greedy Algorithm

So Time taken to find minimum cost edge is


log n and n number of times we have to do this
so , Time Complexity of Kruskal algorithm is
O (n log n)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 127


Greedy Algorithm (CO3)
Difference Between Kruskal’s and Prim’s Algorithm.
Kruskal’s algorithm Prims algorithm

1. Select the shortest edge in a 1. Select any vertex


network
2. Select the shortest edge
2. Select the next shortest connected to that vertex
edge which does not create
a cycle 3. Select the shortest edge
connected to any vertex already
3. Repeat step 2 until all connected
vertices have been
connected 4. Repeat step 3 until all vertices
have been connected

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 128


Topic Objective- Greedy Algorithm

Q. Find Minimum cost Spanning tree using Kruskal


method

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 129


Previous year question -Greedy Algorithm

Q.1 Prove that if the weights on the edge of the


connected undirected graph are distinct then there
is a unique Minimum Spanning Tree. Give an
example in this regard. Also discuss Kruskal’s
Minimum Spanning Tree in detail.

Q.2 Write and explain the Kruskal algorithm to


find the Minimum Spanning Tree of a graph with
suitable example.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 130


Previous year question -Greedy Algorithm

Q.3 What is Minimum Cost Spanning Tree?


Explain Kruskal’s Algorithm and Find MST of the
Graph. Also write its Time-Complexity

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 131


Shortest Path Problem

In data structures,
•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.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 132


Greedy Algorithm (CO3)

Single-Source Shortest Paths


Problem: given a weighted directed graph G, find the minimum weight
path from a given source vertex s to another vertex v

● “Shortest-path” = minimum weight

● Weight of path is sum of edges

● E.g., a road map: what is the shortest path from Chapel Hill to
Charlottesville?

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 133


Dijkstra Algo Pseudocode(CO3)
RELAX(u, v, w)

1. If d[v] > d[u] +w(u, v)


2. then d[v] ← d[u] +w(u, v)
3. π[v] ← u

Initialize Single Source(G, S)

1.For each vertex v ∈ V[G]


2. do d[v] ←∞
3. π[v] ← NIL
4.d[s] ←0 // dist. to source vertex is set to 0

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 134


Dijkstra Algo Pseudocode (CO3)
Dijkstra's (G, W, S)

1. Initialize Single Source(G, S)


2. S ←∅
3. Q ←V[G]
4. while Q ≠∅
5. do u ← EXTRACT-MIN(Q)
6. S←S∪{u}
7. for each vertex all v ∈ adjacent[u]
8. do relax(u, v, w)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 135


Dijkstra Algorithm (CO3)

1. dist[S] ← 0 // The distance to source vertex is set to 0


2. Π[S] ← NIL // The predecessor of source vertex is set as NIL
3. for all v ∈ V - {S} // For all other vertices
4. do dist[v] ← ∞ // All other distances are set to ∞
5. Π[v] ← NIL // The predecessor of all other vertices is set as NIL
6. S ← ∅ // The set of vertices that have been visited 'S' is initially empty
7. Q ← V // The queue 'Q' initially contains all the vertices
8. while Q ≠ ∅ // While loop executes till the queue is not empty
9. do u ← mindistance (Q, dist) // A vertex from Q with the least distance is selected
10. S ← S ∪ {u} // Vertex 'u' is added to 'S' list of vertices that have been visited
11. for all v ∈ neighbors[u] // For all the neighboring vertices of vertex 'u'
12. do if dist[v] > dist[u] + w(u,v) // if any new shortest path is discovered
13. then dist[v] ← dist[u] + w(u,v) // The new value of the shortest path is selected
14. return dist

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 136


Shortest Path Problem

Applications-

Shortest path algorithms have a wide range of


applications such as in-
• Google Maps
• Road Networks
• Logistics Research

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 137


Shortest Path Problem

Single Source shortest path problem is one of the


shortest path problem.
•It is a shortest path problem where the shortest
path from a given source vertex to all other
remaining vertices is computed.
•Dijkstra’s Algorithm and Bellman Ford
Algorithm are the famous algorithms used for
solving single-source shortest path problem.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 138


Dijkstra Algorithm-Greedy Algorithm

•Dijkstra Algorithm is a very famous greedy


algorithm.
•It is used for solving the single source shortest
path problem.
•It computes the shortest path from one particular
source node to all other remaining nodes of the
graph.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 139


Dijkstra Algorithm Time Complexity

The time complexity of Dijkstra’s Algorithm is


typically O(V2) when using a simple array
implementation or O((V + E) log V) with a
priority queue, where V represents the number of
vertices and E represents the number of edges in the
graph. The space complexity of the algorithm
is O(V) for storing the distances and predecessors for
each node, along with additional space for data structures
like priority queues or arrays.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 140


Greedy Algorithm (CO3)
Dijkstra's Example

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 141


Greedy Algorithm (CO3)
Dijkstra's Example

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 142


Greedy Algorithm (CO3)
Dijkstra's Example

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 143


Greedy Algorithm (CO3)
Dijkstra's Example

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 144


Greedy Algorithm (CO3)
Dijkstra's Example

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 145


Greedy Algorithm (CO3)
Dijkstra's Example

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 146


Greedy Algorithm (CO3)
Dijkstra's Example

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 147


Greedy Algorithm (CO3)
Dijkstra's Example

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 148


Greedy Algorithm (CO3)
Dijkstra's Example

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 149


Greedy Algorithm (CO3)
Dijkstra's Example

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 150


Bellman Ford Algorithm
• Bellman ford algorithm is a single-source shortest path algorithm.
This algorithm is used to find the shortest distance from the single
vertex to all the other vertices of a weighted graph.
• There are various other algorithms used to find the shortest path like
Dijkstra algorithm, etc. If the weighted graph contains the negative
weight values, then the Dijkstra algorithm does not confirm whether
it produces the correct answer or not.
• In contrast to Dijkstra algorithm, bellman ford algorithm guarantees
the correct answer even if the weighted graph contains the negative
weight values.
• It follows Dynamic Programming strategy. Dynamic Programming
strategy states that you try out all possible solution and pick up the
best solution

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 151


Bellman Ford Algorithm
• This algo says that u go on relax all adjacent
Ie; (n-1) times.

// n is num of vertices.
• If we relax all it will cover all possible paths.
If d[v] > d[u] +w(u, v)
then d[v] ← d[u] +w(u, v)
• The Bellman-Ford algorithm uses the bottom-
up approach.
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 152
Bellman Ford (CO3)
Bellman Ford (G, W, S)

1. Initialize Single Source(G, S)


2. For i ←1 to [V[G]] -1
3. do for each edge (u, v)∈ E[G]
4. do relax(u, v, w)
5. for each edge (u, v)∈ E[G]
6. do if d[v] > d[u] +w(u, v) //if –ve weight cycle does not exist
7. then return false // then false
8. return true

Time complexity of Bellman-Ford is O(VE), which is more than


Dijkstra.
where V is a number of vertices and E is a number of edges. For
a complete graph with n vertices, V = n, E = O(n2). So overall
time complexity becomes O(n3).
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 153
Bellman Ford (CO3)

Q.Consider the following graph, and find the


shortest path using the bellman ford algorithm.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 154


Bellman Ford (CO3)

Solution :- Initially, the distance of each vertex will


be infinity from the source vertex and source-to-
source distance value would be zero. So,
considering A to be source vertex.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 155


Bellman Ford (CO3)

Relax all the edges n-1 times, where n is the number


of vertices
If, [(d(u) + w(u , v) < d(v)) ]
d(v) = d(u) + w(u , v)
So, for this problem, there will be total 5 iteration as
(6-1).
List out all the edges in the graph.
(A, B), (A, C), (A, D), (B, E), (D, C), (D, F) (C, E),
(C, B), (E, F)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 156


Bellman Ford (CO3)

• First iteration:
Let’s take (A, B), here d(u) is 0, d(v) is ∞ and w(u,
v) is 6.
putting, these value in “Principle of relaxation”
If, [0 + 6 < ∞]…… True
So, [d(B) = 6]
Now, let’s take (A, C)
On putting “Principle of relaxation”
[ d(C) = 4 ]

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 157


Bellman Ford (CO3)

Now, let’s take (A, D)


On putting “Principle of relaxation”
[ d(D) = 5 ]
Now, let’s take (B, E)
On putting “Principle of relaxation”
If, [6 + (-1) < ∞]…… True
So, [ d(E) = 5 ]
Now, let’s take (C, E)
On putting “Principle of relaxation”
If, [4 + 3 < 5]…… False
So, Don’t update.
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 158
Bellman Ford (CO3)
Now, let’s take (D, C)
On putting “Principle of relaxation”
If, [5+ (-2) < 4]…… True
So, [ d(C) = 3 ]
Now, let’s take (D, F)
On putting “Principle of relaxation”
If, [5+ (-1) < ∞]…… True
So, [ d(F) = 4 ]
Now, let’s take (E, F)
On putting “Principle of relaxation”
If, [5+ 3 < 4]…… False
So, Don’t Update
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 159
Bellman Ford (CO3)

Now, let’s take (C, B)


On putting “Principle of relaxation”
If, [3+ (-2) < 6]…… True
So, [ d(B) = 1 ]
The graph after first iteration looks as follows:

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 160


Bellman Ford (CO3)
Second Iteration:
Let’s take (A, B),
putting, these value in “Principle of relaxation”
If, [0 + 6 < 1]…… False
So, Don’t update
Now, let’s take (A, C)
On putting “Principle of relaxation”
If, [0 + 4 < 3]…… False
So, Don’t update

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 161


Bellman Ford (CO3)

Now, let’s take (A, D)


On putting “Principle of relaxation”
If, [0 + 5 < 5]…… False
So, Don’t update
Now, let’s take (B, E)
On putting “Principle of relaxation”
If, [1 + (-1) < 5]…… True
So, [ d(E) = 0 ]
Now, let’s take (C, E)
On putting “Principle of relaxation”
If, [3 + 3 < 0]…… False
So, Don’t update.
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 162
Bellman Ford (CO3)
Now, let’s take (D, C)
On putting “Principle of relaxation”
If, [5+ (-2) < 3]…… False
So, Don’t Update
Now, let’s take (D, F)
On putting “Principle of relaxation”
If, [5+ (-1) < 4]…… False
So, Don’t Update
Now, let’s take (E, F)
On putting “Principle of relaxation”
If, [0+ 3 < 4]…… True
So, [d(F) = 3]
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 163
Bellman Ford (CO3)
Now, let’s take (C, B)
On putting “Principle of relaxation”
If, [3+ (-2) < 6]…… False
So, Don’t Update
The Graph after second iteration

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 164


Bellman Ford (CO3)
Third Iteration:
Let’s take (A, B),
putting, these values in the “Principle of relaxation”
If, [0 + 6 < 1]…… False
So, Don’t update
Now, let’s take (A, C)
On putting “Principle of relaxation”
If, [0 + 4 < 3]…… False
So, Don’t update
Now, let’s take (A, D)
On putting “Principle of relaxation”
If, [0 + 5 < 5]…… False
So, Don’t update
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 165
Bellman Ford (CO3)
Now, let’s take (B, E)
On putting “Principle of relaxation”
If, [1 + (-1) < 0……. False
So, Don’t update
Now, let’s take (C, E)
On putting “Principle of relaxation”
If, [3 + 3 < 0]…… False
So, Don’t update.
Now, let’s take (D, C)
On putting “Principle of relaxation”
If, [5+ (-2) < 3]…… False
So, Don’t Update
Now, let’s take (D, F)
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 166
Bellman Ford (CO3)
On putting “Principle of
relaxation”
If, [5+ (-1) < 3]…… False
So, Don’t Update
Now, let’s take (E, F)
On putting “Principle of
relaxation”
If, [0+ 3 < 3]…… False
So, Don’t Update
Now, let’s take (C, B)
On putting “Principle of
relaxation”
If, [3+ (-2) < 6]…… False
So, Don’t Update ANKITA SHARMA ACSE0401
12/15/2024 UNIT 3 167
Bellman Ford (CO3)

The graph after the third iteration is

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 168


Bellman Ford (CO3)
• According to the relaxation condition, we need at most
five iterations, as we can see there are no updations or
changes in the third iteration itself. So, we will stop
here and write the shortest path of each vertex from A.
• So, the cost for each vertex from A is
• A- A: 0
• A- B: 1
• A- C: 3
• A- D: 5
• A- E: 0
• A- F: 3
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 169
Greedy Algorithm (CO3)

Example
Let us understand the algorithm with following example graph.

Let the given source vertex be 0. Initialize all distances as infinite, except the
distance to the source itself. Total number of vertices in the graph is 5, so all edges
must be processed 4 times.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 170


Greedy Algorithm (CO3)

Let all edges are processed in the following order: (B, E), (D, B), (B, D), (A, B), (A, C),
(D, C), (B, C), (E, D). We get the following distances when all edges are processed the
first time. The first row shows initial distances. The second row shows distances
when edges (B, E), (D, B), (B, D) and (A, B) are processed. The third row shows
distances when (A, C) is processed. The fourth row shows when (D, C), (B, C) and (E,
D) are processed.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 171


Greedy Algorithm (CO3)

The first iteration guarantees to give all shortest paths which are at most 1 edge long.
We get the following distances when all edges are processed second time (The last
row shows final values).

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 172


Greedy Algorithm (CO3)

The second iteration guarantees to give all shortest paths which are at most 2
edges long. The algorithm processes all edges 2 more times. The distances are
minimized after the second iteration, so third and fourth iterations don’t update
the distances.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 173


Bellman Ford Algorithm
• Negative weight edges can create negative weight cycles i.e. a
cycle that will reduce the total path distance by coming back to the
same point.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 174


Bellman Ford Algorithm

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 175


Bellman Ford Algorithm

Initially mark the distance for all vertices

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 176


Bellman Ford Algorithm

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 177


Bellman Ford Algorithm

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 178


Bellman Ford Algorithm

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 179


Bellman Ford Algorithm

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 180


Practise Question
Q.1) What is difference between Dijkstra
Algorithm and Bellmand Ford Algorithm explain
using an example .

• **Bellman Ford's algorithm and Dijkstra's


algorithm are very similar in structure. While
Dijkstra looks only to the immediate neighbors
of a vertex, Bellman goes through each edge
in every iteration.***
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 181
Practise Question
Q :2 solve using Bellman Ford

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 182


Huffman Coding -Greedy Algorithm (CO3)
Huffman Coding is a compression technique its is used
for reducing the size of data or message ,when data is
sent over a network data can be compress to reduce the
cost of the transmission.

Eg: Lets understand a Problem where message is


BCCABBDDAECCBBAEDDC
This Message we want to store in a file or sent it over a
network, so what will be the cost of message.

size of the message is measured in bits., size of message


here is 20.
This msg is sent using ASCII code , these are of 8 bits
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 183
Huffman Coding -Greedy Algorithm (CO3)
A 65 01000001
B 66 01000010
C 67
D 68
E 69
With this we can understand that our message willb take
8 * 20 = 160 bits (***8 bits for each alphabet)

Here message can be reprented with reduced size as well.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 184


There are mainly two major parts
in Huffman Coding
1.Build a Huffman Tree from input
characters.
2.Traverse the Huffman Tree and
assign codes to characters.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 185


Huffman Coding -Greedy Algorithm (CO3)

Problem-

A file contains the following characters with the


frequencies as shown. If Huffman Coding is used for
data compression, determine-
1. Huffman Code for each character
2. Length of Huffman encoded message (in bits)
3. Average code length

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 186


Huffman Coding -Greedy Algorithm (CO3)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 187


Huffman Coding -Greedy Algorithm (CO3)

Solution : Step-01:

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 188


Huffman Coding -Greedy Algorithm (CO3)

Solution : Step-02:

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 189


Huffman Coding -Greedy Algorithm (CO3)

Step-03:

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 190


Huffman Coding -Greedy Algorithm (CO3)

Step-04:

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 191


Huffman Coding -Greedy Algorithm (CO3)

Step-05:

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 192


Huffman Coding -Greedy Algorithm (CO3)

Step-06:

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 193


Huffman Coding -Greedy Algorithm (CO3)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 194


Huffman Coding -Greedy Algorithm (CO3)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 195


Huffman Coding -Greedy Algorithm (CO3)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 196


Huffman Coding -Greedy Algorithm (CO3)
Let us assign weight ‘0’ to the left edges and weight ‘1’
to the right edges.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 197


Huffman Coding -Greedy Algorithm (CO3)
1. Huffman code for 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
• e = 10
• i = 00
• o = 11001
• u = 1101
• s = 01
• t = 1100
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 198
Huffman Coding -Greedy Algorithm (CO3)

• From here, we can observe-


• Characters occurring less frequently in the text
are assigned the larger code.
• Characters occurring more frequently in the
text are assigned the smaller code.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 199


Huffman Coding -Greedy Algorithm (CO3)

2. Average Code Length-

Using formula-01, we have-


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
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 200
Huffman Coding -Greedy Algorithm (CO3)

3 .Length of Huffman encoded message


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

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 201


Algorithm
• The method which is used to construct
optimal prefix code is called Huffman
coding.
• This algorithm builds a tree in bottom up
manner. We can denote this tree by T
• Let, |c| be number of leaves
• |c| -1 are number of operations required
to merge the nodes.
• Q be the priority queue which can be
used while constructing binary heap.
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 202
Steps to build Huffman Tree
• Input is an array of unique characters along with their frequency of
occurrences and output is Huffman Tree.
1. Create a leaf node for each unique character and build a min heap of
all leaf nodes (Min Heap is used as a priority queue. The value of
frequency field is used to compare two nodes in min heap. Initially,
the least frequent character is at root)
2. Extract two nodes with the minimum frequency from the min heap.

3. Create a new internal node with a frequency equal to the sum of the
two nodes frequencies. Make the first extracted node as its left child
and the other extracted node as its right child. Add this node to the
min heap.
4. Repeat steps#2 and #3 until the heap contains only one node. The
remaining node is the root node and the tree is complete.
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 203
Huffman Coding -Greedy Algorithm (CO3)
)
Huffman (C)

1. n ←[C]
2. Q ←C
3. For i ← 1 to n-1
4. do allocate a new node Z
5. left[z] ← x ← EXTRACT-MIN(Q)
6. Right[z] ← y ← EXTRACT-MIN(Q)
7. F[z] ← F[x] + F[y]
8. Insert (Q, Z)
9. Return EXTRACT-MIN(Q)

The time complexity for encoding each unique character


based on its frequency is O(nlog n) .
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 204
Step 1. Build a min heap that contains 6 nodes where each
node represents root of a tree with single node.

Step 2 Extract two minimum frequency nodes from min heap.


Add a new internal node with frequency 5 + 9 = 14.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 205


• Now min heap contains 5 nodes
where 4 nodes are roots of trees with
single element each, and one heap
node is root of tree with 3 elements.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 206


• Step 3: Extract two minimum frequency nodes from
heap. Add a new internal node with frequency 12 +
13 = 25
• Now min heap contains 4 nodes where 2 nodes are
roots of trees with single element each, and two heap
nodes are root of tree with more than one nodes

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 207


• Step 4: Extract two minimum
frequency nodes. Add a new internal
node with frequency 14 + 16 = 30

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 208


• Extract two minimum frequency
nodes. Add a new internal node with
frequency 25 + 30 = 55

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 209


Huffman Coding -Greedy Algorithm (CO3)
Huffman Coding

• we make a new node and then pick the first two nodes from the
sorted character and make the first node left child of the new
node and second node as the right child of the new node.
• The value of the new node is the summation of the values of the
children nodes.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 210


Huffman Coding -Greedy Algorithm (CO3)

Huffman Coding

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 211


s.

Huffman Coding -Greedy Algorithm (CO3)


Huffman Coding
We again sort the nodes according to the values and repeat the same
process with the first two nodes.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 212


s.

Huffman Coding -Greedy Algorithm (CO3)


Huffman Coding

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 213


s.

Huffman Coding -Greedy Algorithm (CO3)


Huffman Coding

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 214


s.

Huffman Coding -Greedy Algorithm (CO3)


Huffman Coding

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 215


Daily Quiz

• Kruskal’s algorithm is used to ________________


• What is the time complexity of Kruskal’s algorithm?
• Kruskal’s algorithm is best suited for the dense graphs than the prim’s
algorithm.(True/False).
• Worst case time complexity of Prim’s algorithm if adjacency matrix is
used?
• Prim’s algorithm is a ______
• Prim’s algorithm resembles Dijkstra’s algorithm.(True/False)
• Kruskal’s algorithm is best suited for the sparse graphs than the prim’s
algorithm.
• Dijkstra’s Algorithm is used to solve _____________ problems.
• What is the time complexity of Dijikstra’s algorithm?
• Dijkstra’s Algorithm cannot be applied on ______________
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 216
Faculty Video Links, You tube & NPTEL Video
Links and Online Courses Details

• You tube/other Video Links


• https://www.youtube.com/watch?v=XB4MIexjvY0
• https://www.youtube.com/watch?v=FtN3BYH2Zes
• https://www.youtube.com/watch?v=4ZlRH0eK-qQ
• https://www.youtube.com/watch?v=co4_ahEDCho

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 217


Weekly Assignment

Q1 Solve the 0-1 knapsack problem where you have five objects whose
weights are 1, 2, 5, 6, 7 and whose values are 1, 8, 6, 12, 18 and
maximum weight is 12. [CO3]
Q2 Solve the 0-1 knapsack problem where you have five objects whose
weights are 6, 7, 1, 2, 5 and whose values are 12, 18, 1, 8, 6 and
maximum weight is 13. [CO3]
Q3 What is the optimal Huffman Code for the following set of
frequencies? a: 05, b: 48, c: 07, d: 17, e: 10, f: 13
[CO3]
Q4 Find the optimal solution for the knapsack 0/1 problem by using the
dynamic programming approach? Consider, N = 4, W =5, (w1, w2,
w3, w4) = (2, 3, 4, 5) and (b1, b2, b3, b4) = (3, 4, 5, 6) [CO3]

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 218


MCQ
Q1 Kruskal’s algorithm is used to ______
(a) find minimum spanning tree
(b) find single source shortest path
(c) find all pair shortest path algorithm
(d) traverse the graph

Q2 Kruskal’s algorithm is a ______


(a) divide and conquer algorithm
(b) dynamic programming algorithm
(c) greedy algorithm
(d) approximation algorithm

Q3 Which of the following is true?


a) Prim’s algorithm initializes with a vertex
b) Prim’s algorithm initializes with a edge
c) Prim’s algorithm initializes with a vertex which has smallest edge
d) Prim’s algorithm initializes with a forest
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 219
MCQ
Q4 Prim’s algorithm is a ______
(a) Divide and conquer algorithm
(b) Greedy algorithm
(c) Dynamic Programming
(d) Approximation algorithm
Q5 Dijkstra’s Algorithm is used to solve _____________ problems.
(a) All pair shortest path
(b) Single source shortest path
(c) Network flow
(d) Sorting
Q6 Dijkstra’s Algorithm cannot be applied on ______________
(a) Directed and weighted graphs
(b) Graphs having negative weight function
(c) Unweighted graphs
(d) Undirected and unweighted graphs

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 220


MCQ
Q7 Which of the following methods can be used to solve the Knapsack
problem?
a) Brute force algorithm
b) Recursion
c) Dynamic programming
d) Brute force, Recursion and Dynamic Programming
Q8 The 0-1 Knapsack problem can be solved using Greedy algorithm.
a) True
b) False

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 221


Old Question Papers(2021-2022)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 222


Old Question Papers(2021-2022)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 223


Old Question Papers(2021-2022)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 224


Old Question Papers(2020-2021)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 225


Old Question Papers(2020-2021)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 226


Old Question Papers(2020-2021)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 227


Old Question Papers(2020-2021)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 228


Old Question Papers(2020-2021)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 229


Old Question Papers(2019-2020)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 230


Old Question Papers(2019-2020)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 231


Old Question Papers(2019-2020)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 232


Old Question Papers(2019-2020)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 233


Old Question Papers(2019-2020)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 234


Old Question Papers(2020-2021)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 235


Old Question Papers(2020-2021)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 236


Old Question Papers(2020-2021)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 237


Old Question Papers(2020-2021)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 238


Old Question Papers(2020-2021)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 239


Old Question Papers(2020-2021)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 240


Old Question Papers(2020-2021)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 241


Old Question Papers(2020-2021)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 242


Old Question Papers(2020-2021)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 243


Old Question Papers(2020-2021)

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 244


Expected Questions for University Exam

1.Describe activity selection problem. Following are the 9 activities


with their start and finish time as
S={1,2,4,1,5,8,9,11,13} F={3,5,7,8,9,10,11,14,16}
Find a schedule where a maximum number of activities can be
scheduled. [CO1]
2. Consider following instance for simple Knapsack Problem. Find the
solution using greedy method. N:8, Value of knapsack is 110.
V : { 30,20,100,90,160,200,180,220}
W : {5, 10, 20,22,33, 43, 45, 55} [CO1]
3. Discuss the Convex Hull Problem. [CO1]
4. Consider the Following Knapsack Problem . Find the solution
through Greedy Algorithm. N=4, M=60,
Profit={280,150,300,50}, Weight={30,12,20,24}. [CO1]
5. What is minimum spanning tree? Explain any one of minimum
spanning tree algorithm with example. [CO1]

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 245


Summary

This objective of this unit was to make the students aware


about the different techniques for solving problems. In this
unit two concepts have been discussed along with their
application to different types of problems.
Divide and conquer has been used in solving the sorting
problems. The algorithms being discussed in this regard are
Merge sort and Quick sort. Besides this Strassen algorithm for
multiplication of matrices have also been discussed.
In Greedy approach different graph algorithms like Dijskstra ,
Kruskal ,Prims, Huffman Coding and activity selection problem
have been explained.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 246


References

1. Thomas H. Coreman, Charles E. Leiserson and Ronald L. Rivest,


“Introduction to
Algorithms”, Printice Hall of India.
2. E. Horowitz & S Sahni, "Fundamentals of Computer Algorithms",
3. Aho, Hopcraft, Ullman, “The Design and Analysis of Computer
Algorithms” Pearson Education, 2008.
4. LEE “Design & Analysis of Algorithms(POD)”, McGraw Hill
5. Richard E.Neopolitan ‘Foundations of Algorithms” Jones &
Bartlett Learning.
6. Gilles Brassard and Paul Bratley, Algorithmics: Theory and
Practice, Prentice Hall, 1995.

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 247


Unit 3

Thank You

12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 248

You might also like