Unit 3

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

Noida Institute of Engineering and Technology, Greater Noida

Divide and Conquer and Greedy Method

Unit: 1

Design & Analysis of Algorithms


Sachin Singh
Assistant Professor
CS(AI)

Sachin Singh DAA Unit III


1
12/22/2024
Brief Introduction About Me

Name : Sachin Singh

Designation :Assistant Professor CS(AI) Department


NIET Greater Noida

Qualification:

 B.Tech AKGEC Ghaziabad in 2017.


 M.Tech Delhi Technological University(formely DCE) in 2020.

12/22/2024 Sachin Singh DAA Unit III 2


Evaluation Scheme

12/22/2024 Sachin Singh DAA Unit III 3


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/22/2024 Sachin Singh DAA Unit III 4


Syllabus

Sachin Singh DAA Unit III


12/22/2024 5
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/22/2024 Sachin Singh DAA Unit III 6


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.

Sachin Singh DAA Unit III


12/22/2024 7
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/22/2024 Sachin Singh DAA Unit III 8


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/22/2024 Sachin Singh DAA Unit III 9


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/22/2024 Sachin Singh DAA Unit III 10


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/22/2024 Sachin Singh DAA Unit III 11


Result Analysis

12/22/2024 Sachin Singh DAA Unit III 12


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/22/2024 Sachin Singh DAA Unit III 13


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/22/2024 Sachin Singh DAA Unit III 14
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/22/2024 Sachin Singh DAA Unit III 15


Prerequisite and Recap

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

• Recap
• Flow Chart
• Algorithm

12/22/2024 Sachin Singh DAA Unit III 16


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/22/2024 Sachin Singh DAA Unit III 17


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/22/2024 Sachin Singh DAA Unit III 18


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/22/2024 Sachin Singh DAA Unit III 19


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/22/2024 Sachin Singh DAA Unit III 20


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/22/2024 Sachin Singh DAA Unit III 21


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/22/2024 Sachin Singh DAA Unit III 22


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/22/2024 Dr. Shilpa Srivastava RCS-502 DAA Unit Number: 3 23


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

Dr. Shilpa Srivastava RCS-502 DAA Unit Number: 3


12/22/2024 24
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/22/2024 Sachin Singh DAA Unit III 25


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/22/2024 Sachin Singh DAA Unit III 26


Divide and Conquer(CO3)

12/22/2024 Sachin Singh DAA Unit III 27


Divide and Conquer(CO3)

12/22/2024 Sachin Singh DAA Unit III 28


Divide and Conquer(CO3)

• Best case: split in the middle — Θ( n log n)


• Worst case: sorted array! — Θ( n2)
• Average case: random arrays — Θ( n log n)
• Improvements:
– better pivot selection: median of three partitioning avoids
worst case in sorted files
– switch to insertion sort on small subfiles
– elimination of recursion
-these combine to 20-25% improvement
• Considered the method of choice for internal sorting for large
files (n ≥ 10000)

12/22/2024 Sachin Singh DAA Unit III 29


Divide and Conquer(CO3)

Binary Search
Binary search is the most popular Search algorithm.It is efficient
and also one of the most commonly used techniques that is used to
solve problems.

RULES FOR BINARY SEARCH:


1.Find the middle element of array( A[MID] ).
2.Compare the middle element with the data to be searched , then
there are three cases:-
( a ) If middle element is a desired element then search is successful.
( b ) If middle element is less than desired element then search only
the
first half of the array.
( c ) If middle element is greater than desired element then search
only the second half of the array.
12/22/2024 Sachin Singh DAA Unit III 30
Divide and Conquer(CO3)
ALGORITHM OF BINARY SEARCHING
binarySearch(arr, size)
loop until beg is not equal to end
midIndex = (beg + end)/2
if (item == arr[midIndex] )
return midIndex
else if (item > arr[midIndex] )
beg = midIndex + 1
else
end = midIndex - 1

12/22/2024 Sachin Singh DAA Unit III 31


Divide and Conquer(CO3)
Search element 40 in the following array elements:-
Beg End
9 10 25 30 40

A[0] A[2] A[3] A[4]


Step 1:
A[1]
Beg=0; End=4
Mid =int((0+4)/2)=
A[2]!=40, 2 Reinitialize the variable
40<25 i. e.
Step 2: A[Mid]=A[2]=25 End=4
Beg=2+1=3,
Mid=int((3+4)/2)=3 Step 5:
A[Mid]=A[3]=30 Since (A[4]==data),
Step 3: 40==40
A[3]!=40, 40<30, i.e. Reinitialize the variable A[4]=40,
Beg=3+1=4, End=4
Mid=int((4+4)/2)=4 40=40.
Searching is successful . Element present on 5th
position
12/22/2024 Sachin Singh DAA Unit III 32
Divide and Conquer(CO3)
Binary Search
The worst case time complexity of binary search is O(log2n)
Advantage :
-
• The binary search is fast as compared to linear search.
• Most suitable for sorted array.
Disadvantage :-
• The list must be sorted.
• Direct access to the middle element in any sub list.
• Time complexity is high in both average and worst case.

12/22/2024 Sachin Singh DAA Unit III 33


Divide and Conquer(CO3)

Closest Pair Problem

• Problem: find closest points among n ones in k-dimensional space

• Assume points are sorted by x-coordinate values.

• Divide the points in two subsets S1 and S2 of n/2 points by drawing a vertical
line at x=c.

• Select the closest pair among the closest pair of the left subset (distance d 1),
of the closest pair of the right subset (distance d 2), and the closest pair with
points from both sides.

• We need to examine a stripe of width 2d, where d=min[d 1,d2]

12/22/2024 Sachin Singh DAA Unit III 34


Divide and Conquer(CO3)
Convex Hull
• A convex hull of a set of 2D points is the shape taken by a
rubber band stretched around nails pounded into the plane at
each point.
• A convex hull of a set S of points is the smallest polygon P for
which each point of S is either on the boundary or in the
interior of P.
• A convex hull of a set S of points is the smallest set   S | if
x1, x2   and   [0,1] then x = x1 + (1 - )x2  
Applications:
• collision avoidance in robotics;
• mapping the surface of an object (like an asteroid);
• analyzing images of soil particles;
• estimating percentage of lung volume occupied by pneumonia

12/22/2024 Sachin Singh DAA Unit III 35


Divide and Conquer(CO3)

12/22/2024 Sachin Singh DAA Unit III 36


Divide and Conquer(CO3)

Convex Hull
• This is actually a half-space hull finder
– You start with the leftmost (A) and rightmost
(B) points and all the points above them
– It finds the half hull to the top side
• One can then find the entire hull by running the same
algorithm on the bottom points

12/22/2024 Sachin Singh DAA Unit III 37


Divide and Conquer(CO3)
Convex Hull
• Pick the point C that is furthest from the line AB

• Form two lines – AC and CB and place all points above


AC into one list and all points above CB into another list.
Ignore the inner
points

12/22/2024 Sachin Singh DAA Unit III 38


Divide and Conquer(CO3)

Convex Hull
• Recursively conquer each list of points.

• Return the line AB for each eventual empty list of points


• Combine resulting AB lines into polygon

12/22/2024 Sachin Singh DAA Unit III 39


Divide and Conquer(CO3)

Convex Hull

• Finding point farthest away from line P1P2 can be done in linear time
• This gives same efficiency as quicksort:
– Worst case: Θ(n2)
– Average case: Θ(n log n)

• If points are not initially sorted by x-coordinate value, this can be


accomplished in Θ(n log n) —
no increase in asymptotic efficiency class

• Other algorithms for convex hull:


– Graham’s scan
– DCHull

12/22/2024 Sachin Singh DAA Unit III 40


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/22/2024 Sachin Singh DAA Unit III 41


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)

12/22/2024 Sachin Singh DAA Unit III 42


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/22/2024 Sachin Singh DAA Unit III 43


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/22/2024 Sachin Singh DAA Unit III 44


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/22/2024 Sachin Singh DAA Unit III 45


Prerequisite and Recap

• Prerequisite
• Graph
• Different operations
• Notations
• Recap
– Algorithm analysis
– Analysis of different sorting and searching algorithms using
divide and conquer.

12/22/2024 Sachin Singh DAA Unit III 46


Greedy Algorithm (CO3)

• Graph
• Directed Graph
• Undirected Graph
• Degree of Graph
• Graph Representation
• Spanning Tree
• Kruskal’s Method
• Prims Method
• Single-Source Shortest Paths
• Dijkstra's Algorithm
• Knapsack Problem

12/22/2024 Sachin Singh DAA Unit III 47


Greedy Algorithm (CO3)

Graph A graph G = (V, E) as a collection of adjacency lists or as an


adjacency matrix. Where V is set of vertex (node) and E is a set of edges
(link). There are two types of graph: directed and undirected graphs.

Directed graph (or digraph) It is a graph with directed edges. Edges have
directions so they are represented by arrows. Each edge leaves a vertex
and enters a vertex.

Undirected graph It is a graph with undirected edges. Edges have no


directions so they are represented by lines.

12/22/2024 Sachin Singh DAA Unit III 48


Greedy Algorithm (CO3)

Degree (Directed Graph)

The out-degree of a vertex is the


number of edges leaving it. The
out-degree of vertex 2 is 3.

The in-degree of a vertex is the


number of edges entering it. The
in-degree of vertex 2 is 2.
degree=out-degree+ in-
degree

12/22/2024 Sachin Singh DAA Unit III 49


Greedy Algorithm (CO3)

Degree (Undirected Graph)

In an undirected graph,
The out-degree and the in-
degree are not defined.
Only the degree of a vertex is
defined. The degree of vertex 2
is 3.

12/22/2024 Sachin Singh DAA Unit III 50


Greedy Algorithm (CO3)

Representation of Graph Given graph G = (V, E). May be either


directed or undirected. Two common ways to represent for
algorithms:

1. Adjacency-list representation

2. Adjacency-matrix representation

12/22/2024 Sachin Singh DAA Unit III 51


Greedy Algorithm (CO3)
Adjacency-list representation

An array of |V| lists, one for each vertex. For vertex u, its adjacency
list contains all vertices adjacent to u. The adjacency-list
representation is usually preferred, because it provides a compact
way to represent sparse graph- those for which |E| is much less
than |V|2 .

12/22/2024 Sachin Singh DAA Unit III 52


Greedy Algorithm (CO3)

Adjacency-matrix representation
An Adjacency-matrix representation may be preferred, when the
graph is dense -|E| is close to |V|2 or when we need to be able to tell
quickly if there is an edge connecting two given vertices.

12/22/2024 Sachin Singh DAA Unit III 53


Greedy Algorithm (CO3)
Adjacency-matrix representation of Directed Graph

Graph Adjacency list Adjacency matrix

12/22/2024 Sachin Singh DAA Unit III 54


Greedy Algorithm (CO3)

Adjacency-matrix representation of Undirected Graph

Graph Adjacency list Adjacency matrix

12/22/2024 Sachin Singh DAA Unit III 55


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/22/2024 Sachin Singh DAA Unit III 56


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/22/2024 Sachin Singh DAA Unit III 57


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/22/2024 Sachin Singh DAA Unit III 58


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/22/2024 Sachin Singh DAA Unit III 59


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/22/2024 Sachin Singh DAA Unit III 60


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/22/2024 Sachin Singh DAA Unit III 61


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/22/2024 Sachin Singh DAA Unit III 62


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/22/2024 Sachin Singh DAA Unit III 63


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/22/2024 Sachin Singh DAA Unit III 64


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/22/2024 Sachin Singh DAA Unit III 65


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/22/2024 Sachin Singh DAA Unit III 66


Greedy Algorithm (CO3)

Step 1: Select a starting vertex.

Step 2: Repeat Steps 3 and 4 until there are fringe 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/22/2024 Sachin Singh DAA Unit III 67


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/22/2024 Sachin Singh DAA Unit III 68


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/22/2024 Sachin Singh DAA Unit III 69


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/22/2024 Sachin Singh DAA Unit III 70


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/22/2024 Sachin Singh DAA Unit III 71


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/22/2024 Sachin Singh DAA Unit III 72


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/22/2024 Sachin Singh DAA Unit III 73


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/22/2024 Sachin Singh DAA Unit III 74


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/22/2024 Sachin Singh DAA Unit III 75


Greedy Algorithm (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

12/22/2024 Sachin Singh DAA Unit III 76


Greedy Algorithm (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/22/2024 Sachin Singh DAA Unit III 77


Greedy Algorithm (CO3)
Dijkstra's Example

12/22/2024 Sachin Singh DAA Unit III 78


Greedy Algorithm (CO3)
Dijkstra's Example

12/22/2024 Sachin Singh DAA Unit III 79


Greedy Algorithm (CO3)
Dijkstra's Example

12/22/2024 Sachin Singh DAA Unit III 80


Greedy Algorithm (CO3)
Dijkstra's Example

12/22/2024 Sachin Singh DAA Unit III 81


Greedy Algorithm (CO3)
Dijkstra's Example

12/22/2024 Sachin Singh DAA Unit III 82


Greedy Algorithm (CO3)
Dijkstra's Example

12/22/2024 Sachin Singh DAA Unit III 83


Greedy Algorithm (CO3)
Dijkstra's Example

12/22/2024 Sachin Singh DAA Unit III 84


Greedy Algorithm (CO3)
Dijkstra's Example

12/22/2024 Sachin Singh DAA Unit III 85


Greedy Algorithm (CO3)
Dijkstra's Example

12/22/2024 Sachin Singh DAA Unit III 86


Greedy Algorithm (CO3)
Dijkstra's Example

12/22/2024 Sachin Singh DAA Unit III 87


Greedy Algorithm (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)
7. then return false
8. return true

12/22/2024 Sachin Singh DAA Unit III 88


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/22/2024 Sachin Singh DAA Unit III 89


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/22/2024 Sachin Singh DAA Unit III 90


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/22/2024 Sachin Singh DAA Unit III 91


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/22/2024 Sachin Singh DAA Unit III 92


Greedy Algorithm (CO3)

Knapsack Problem
There are two versions of the problem:

1. “0-1 knapsack problem”


• Items are indivisible,
• you either take an item or not.

2. “Fractional knapsack problem”


 Items are divisible
 you can take any fraction of an item

12/22/2024 Sachin Singh DAA Unit III 93


Greedy Algorithm (CO3)
Knapsack Problem

•Given a knapsack with maximum capacity W, and a set S consisting of n


items

•Each item i has some weight wi and benefit value bi (all wi and W are
integer values)

•Problem: How to pack the knapsack to achieve maximum total value of


packed items?

12/22/2024 Sachin Singh DAA Unit III 94


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/22/2024 Sachin Singh DAA Unit III 95


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/22/2024 Sachin Singh DAA Unit III 96


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/22/2024 Sachin Singh DAA Unit III 97


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/22/2024 Sachin Singh DAA Unit III 98
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/22/2024 Sachin Singh DAA Unit III 99


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)

12/22/2024 Sachin Singh DAA Unit III 100


Greedy Algorithm (CO3)

Huffman Coding

12/22/2024 Sachin Singh DAA Unit III 101


s.

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/22/2024 Sachin Singh DAA Unit III 102


s.

Greedy Algorithm (CO3)


Huffman Coding

12/22/2024 Sachin Singh DAA Unit III 103


s.

Greedy Algorithm (CO3)


Huffman Coding

12/22/2024 Sachin Singh DAA Unit III 104


s.

Greedy Algorithm (CO3)


Huffman Coding

12/22/2024 Sachin Singh DAA Unit III 105


Greedy Algorithm (CO3)

Task Scheduling Problem-

A unit-time task is a job, such as a program to be rush on a computer that needed


precisely one unit of time to complete. Given a finite set S of unit-time tasks, a schedule
for S is a permutation of S specifying the order in which to perform these tasks. The first
task in the schedule starts at time 0 and ends at time 1; the second task begins at time 1
and finishes at time 2, and so on.
The dispute of scheduling unit-time tasks with deadlines and penalties for each processor
has the following inputs:

• a set S = {1, 2, 3.....n} of n unit-time tasks.


• a set of n integer deadlines d1 d2 d3...dn such that di satisfies 1≤ di ≤ n and task i is
supposed to finish by time di and
• a set of n non-negative weights or penalties w1 w2....wn such that we incur a penalty of
wi if task i is not finished by time di, and we incurred no penalty if a task finishes by its
deadline.

12/22/2024 Sachin Singh DAA Unit III 106


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/22/2024 Sachin Singh DAA Unit III 107


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/22/2024 Sachin Singh DAA Unit III 108


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/22/2024 Sachin Singh DAA Unit III 109


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/22/2024 Sachin Singh DAA Unit III 110


Greedy Algorithm (CO3)
Activity Selection
To select the activities from a pool of activities so that maximum
resources can be utilized without any conflict.

Recursive Activity Selector (s, f, i, j)

1. m ←i +1
2. While m < j and sm < fi
3. do m ← m + 1
4. if m < j
5. then return {am} U Recursive Activity Selector
(s, f, m, j)
6. else Return ∅

12/22/2024 Sachin Singh DAA Unit III 111


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/22/2024 Sachin Singh DAA Unit III 112


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/22/2024 Sachin Singh DAA Unit III 113


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/22/2024 Sachin Singh DAA Unit III 114
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/22/2024 Sachin Singh DAA Unit III 115


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/22/2024 Sachin Singh DAA Unit III 116


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/22/2024 Sachin Singh DAA Unit III 117
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/22/2024 Sachin Singh DAA Unit III 118


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/22/2024 Sachin Singh DAA Unit III 119


Old Question Papers(2021-2022)

12/22/2024 Sachin Singh DAA Unit III 120


Old Question Papers(2021-2022)

12/22/2024 Sachin Singh DAA Unit III 121


Old Question Papers(2021-2022)

12/22/2024 Sachin Singh DAA Unit III 122


Old Question Papers(2020-2021)

12/22/2024 Sachin Singh DAA Unit III 123


Old Question Papers(2020-2021)

12/22/2024 Sachin Singh DAA Unit III 124


Old Question Papers(2020-2021)

12/22/2024 Sachin Singh DAA Unit III 125


Old Question Papers(2020-2021)

12/22/2024 Sachin Singh DAA Unit III 126


Old Question Papers(2020-2021)

12/22/2024 Sachin Singh DAA Unit III 127


Old Question Papers(2019-2020)

12/22/2024 Sachin Singh DAA Unit III 128


Old Question Papers(2019-2020)

12/22/2024 Sachin Singh DAA Unit III 129


Old Question Papers(2019-2020)

12/22/2024 Sachin Singh DAA Unit III 130


Old Question Papers(2019-2020)

12/22/2024 Sachin Singh DAA Unit III 131


Old Question Papers(2019-2020)

12/22/2024 Sachin Singh DAA Unit III 132


Old Question Papers(2018-2019)

12/22/2024 Sachin Singh DAA Unit III 133


Old Question Papers(2018-2019)

12/22/2024 Sachin Singh DAA Unit III 134


Old Question Papers(2018-2019)

12/22/2024 Sachin Singh DAA Unit III 135


Old Question Papers(2018-2019)

12/22/2024 Sachin Singh DAA Unit III 136


Old Question Papers(2018-2019)

12/22/2024 Sachin Singh DAA Unit III 137


Old Question Papers(2017-2018)

12/22/2024 Sachin Singh DAA Unit III 138


Old Question Papers(2017-2018)

12/22/2024 Sachin Singh DAA Unit III 139


Old Question Papers(2017-2018)

12/22/2024 Sachin Singh DAA Unit III 140


Old Question Papers(2017-2018)

12/22/2024 Sachin Singh DAA Unit III 141


Old Question Papers(2017-2018)

12/22/2024 Sachin Singh DAA Unit III 142


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/22/2024 Sachin Singh DAA Unit III 143


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/22/2024 Sachin Singh DAA Unit III 144


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/22/2024 Sachin Singh DAA Unit III 145


Unit 3

Thank You

12/22/2024 Sachin Singh DAA Unit III 146

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy