Design & Analysis of Algorithm Notes For BCA Purvanchal 4th Sem PDF
Design & Analysis of Algorithm Notes For BCA Purvanchal 4th Sem PDF
Design & Analysis of Algorithm Notes For BCA Purvanchal 4th Sem PDF
Semester-4
BCA 401
DAA
(Design & Analysis of Algorithm)
Created By :d.P.Mishra
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
22
Keep Learning with Us VBSPU 4th sem. DAA Notes
Unit – 1
Introduction
Algorithm–
An algorithm is the step by step problem solving technique.
In an algorithm we can use general English for write the code this code is
known as sedo code.
Characteristics of Algorithms
The main characteristics of algorithms are as follows −
Algorithms must have a unique name
Algorithms should have explicitly defined set of inputs and outputs
Algorithms are well-ordered with unambiguous operations
Algorithms halt in a finite amount of time. Algorithms should not run for
infinity, i.e., an algorithm must end at some point
Pseudocode
Pseudocode gives a high-level description of an algorithm without the
ambiguity associated with plain text but also without the need to know the
syntax of a particular programming language.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
33
Keep Learning with Us VBSPU 4th sem. DAA Notes
Analysis of Algorithm–
Algorithm analysis is an important part of computational complexity theory
which provide theoretical estimation for the require resource of algorithm
to solve a specific computational problem.
Most algorithm are design to work with inputs of orbitery length.
Analysis of algorithm is determination of the amount of time and space
resource require to execute.
Designing Algorithm–
An algorithm is the series of instruction often refer to as a process, which is
to be follow when solving a particular problem.
Since modern computing uses algorithm much more frequently then at the
other point in human history. The field of algorithm design require a strong
mathematical background.
The key to algorithm design to be process by asking to yourself a sequence
of questions to guid your thought process.
There is a various techniques for designing an algorithm-
1. Brute Force.
2. Greedy algorithm.
3. Divide and conquer.
4. Dynamic programming.
5. Backtracking and branch and bound.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
44
Keep Learning with Us VBSPU 4th sem. DAA Notes
Growth of functions–
functions
The growth of functions is directly related to the complexity of algorithms.
... In order to get a handle on its complexity, we first look for
a function that gives the number of operations in terms of the size of the
problem, usually measured by a positive integer n, to which
the algorithm is applied.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit:
Visit www.dpmishra.com
55
Keep Learning with Us VBSPU 4th sem. DAA Notes
Summation–
Summation (Σ) just means to “add up.” For example, let’s say you had 5
items in a data set: 1,2,5,7,9; you can think of these as xx-values.
values. If you were
asked to add all of the items up in summation notation, you would see:
Σ(x) which equals 1 + 2 + 5 + 7 + 9 = 24.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit:
Visit www.dpmishra.com
66
Keep Learning with Us VBSPU 4th sem. DAA Notes
Recurrence–
A recurrence is an equation or inequality that describes a function in terms of its
value on smaller inputs. Recurrences are generally used in divide
divide-and-conquer
paradigm.
Let us consider T(n) to be the running time on a problem of size n.
If the problem size is small enough, say n < c where c is a constant, the
straightforward solution takes constant time, which is written as θ(1). If the
sub problems with size nbnb.
division of the problem yields a number of sub-problems
To solve the problem, the required time is a.T(n/b).. If we consider the time
required for division is D(n) and the time required for combining
bining the results of
sub-problems is C(n),, the recurrence relation can be represented as −
Sets–
A set is defined as a collection of distinct objects of the same type or class
of objects. The purposes of a set are called elements or members of the set.
An object can be numbers, alphabets, names, etc.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit:
Visit www.dpmishra.com
77
Keep Learning with Us VBSPU 4th sem. DAA Notes
We broadly denote a set by the capital letter A, B, C, etc. while the fundamentals
of the set by small letter a, b, x, y, etc.
Sets Representation:
a) Roster or tabular form: In this form of representation we list all the elements
of the set within braces { } and separate them by commas.
Example: If A= set of all odd numbers less then 10 then in the roster from it can
be expressed as A={ 1,3,5,7,9}.
b) Set Builder form: In this form of representation we list the properties fulfilled
by all the elements of the set. We note as {x: x satisfies properties P}. and read as
'the set of those entire x such that each x has properties P.'
Example: If B= {2, 4, 8, 16, 32}, then the set builder representation will be: B={x:
x=2n, where n ∈ N and 1≤ n ≥5}
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
88
Keep Learning with Us VBSPU 4th sem. DAA Notes
Thus, if an event can happen in m ways and fails to occur in n ways and m+n ways
is equally likely to occur then the probability of happening of the event A is given
by
Note:
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit:
Visit www.dpmishra.com
99
Keep Learning with Us VBSPU 4th sem. DAA Notes
Unit – 2
Divide and conquer
Divide and Conquer is an algorithmic pattern. In algorithmic methods, the
design is to take a dispute on a huge input, break the input into minor
pieces, decide the problem on each of the small pieces, and then merge the
piecewise solutions into a global solution. This mechanism of solving the
problem is called the Divide & Conquer Strategy.
Divide and Conquer algorithm consists of a dispute using the following three
steps.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
10
10
Keep Learning with Us VBSPU 4th sem. DAA Notes
Searching:
Binary Search-
1. In Binary Search technique, we search an element in a sorted array by
recursively dividing the interval in half.
3. If the Pivot Element (the item to be searched) is less than the item in the
middle of the interval, We discard the second half of the list and recursively
repeat the process for the first half of the list by calculating the new middle and
last element.
4. If the Pivot Element (the item to be searched) is greater than the item in the
middle of the interval, we discard the first half of the list and work recursively on
the second half by calculating the new beginning and middle element.
Example
In this example, we are going to search element 63.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
11
11
Keep Learning with Us VBSPU 4th sem. DAA Notes
Sorting:
Counting sort-
It is a linear time sorting algorithm which works faster by not making a
comparison. It assumes that the number to be sorted is in range 1 to k
where k is small.
Basic idea is to determine the "rank" of each number in the final sorted
array.
Points to be noted:
1. Counting sort is efficient if the range of input data is not significantly greater
than the number of objects to be sorted. Consider the situation where the input
sequence is between range 1 to 10K and the data is 10, 5, 10K, 5K.
2. It is not a comparison based sorting. It running time complexity is O(n) with
space proportional to the range of data.
3. It is often used as a sub-routine to another sorting algorithm like radix sort.
4. Counting sort uses a partial hashing to count the occurrence of the data object
in O(1).
5. Counting sort can be extended to work for negative inputs also.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
12
12
Keep Learning with Us VBSPU 4th sem. DAA Notes
Radix sort-
The idea of Radix Sort is to do digit by digit sort starting from least
significant digit to most significant digit. Radix sort uses counting sort as a
subroutine to sort.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
13
13
Keep Learning with Us VBSPU 4th sem. DAA Notes
bucket algorithm.
bucketSort(arr[], n)
1) Create n empty buckets (Or lists).
2) Do following for every array element arr[i].
.......a) Insert arr[i] into bucket[n*array[i]]
3) Sort individual buckets using insertion sort.
4) Concatenate all sorted buckets.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
14
14
Keep Learning with Us VBSPU 4th sem. DAA Notes
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
15
15
Keep Learning with Us VBSPU 4th sem. DAA Notes
Selection sort-
The selection sort algorithm sorts an array by repeatedly finding the minimum
element (considering ascending order) from unsorted part and putting it at the
beginning. The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering ascending
order) from the unsorted subarray is picked and moved to the sorted subarray.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
16
16
Keep Learning with Us VBSPU 4th sem. DAA Notes
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
17
17
Keep Learning with Us VBSPU 4th sem. DAA Notes
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
18
18
Keep Learning with Us VBSPU 4th sem. DAA Notes
Heap sort-
Heap sort is a comparison based sorting technique based on Binary Heap
data structure. It is similar to selection sort where we first find the
maximum element and place the maximum element at the end. We repeat
the same process for remaining element.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
19
19
Keep Learning with Us VBSPU 4th sem. DAA Notes
Merge sort-
Like QuickSort, Merge Sort is a Divide and Conquer algorithm. It divides input
array in two halves, calls itself for the two halves and then merges the two sorted
halves. The merge() function is used for merging two halves.
The following diagram shows the complete merge sort process for an example
array {38, 27, 43, 3, 9, 82, 10}. If we take a closer look at the diagram, we can see
that the array is recursively divided in two halves till the size becomes 1. Once the
size becomes 1, the merge processes comes into action and starts merging arrays
back till the complete array is merged.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
20
20
Keep Learning with Us VBSPU 4th sem. DAA Notes
Quick sort-
It picks an element as pivot and partitions the given array around the picked
pivot. There are many different versions of quickSort that pick pivot in different
ways.
1. Always pick first element as pivot.
2. Always pick last element as pivot (implemented below)
3. Pick a random element as pivot.
4. Pick median as pivot.
The key process in quickSort is partition(). Target of partitions is,
is, given an array
and an element x of array as pivot, put x at its correct position in sorted array and
put all smaller elements (smaller than x) before x, and put all greater elements
(greater than x) after x. All this should be done in linear time.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit:
Visit www.dpmishra.com
21
21
Keep Learning with Us VBSPU 4th sem. DAA Notes
Greedy method:
Among all the algorithmic approaches, the simplest and straightforward
approach is the Greedy method. In this approach, the decision is taken on
the basis of current available information without worrying about the effect
of the current decision
decisio in future.
This approach is mainly used to solve optimization problems.
Greedy method is easy to implement and quite efficient in most of the
cases. Hence, we can say that Greedy algorithm is an algorithmic paradigm
based on heuristic that follows local optimal choice at each step with the
hope of finding global optimal solution.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit:
Visit www.dpmishra.com
22
22
Keep Learning with Us VBSPU 4th sem. DAA Notes
In the above graph, we have shown a spanning tree though it’s not the minimum
spanning tree. The cost of this spanning tree is (5 + 7 + 3 + 3 + 5 + 8 + 3 + 4) = 38.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
23
23
Keep Learning with Us VBSPU 4th sem. DAA Notes
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
24
24
Keep Learning with Us VBSPU 4th sem. DAA Notes
1 0 0 0 0 0 0 0 0 0
2 ∞ 5 4 4 4 4 4 4 4
3 ∞ 2 2 2 2 2 2 2 2
4 ∞ ∞ ∞ 7 7 7 7 7 7
5 ∞ ∞ ∞ 11 9 9 9 9 9
6 ∞ ∞ ∞ ∞ ∞ 17 17 16 16
7 ∞ ∞ 11 11 11 11 11 11 11
8 ∞ ∞ ∞ ∞ ∞ 16 13 13 13
9 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 20
Hence, the minimum distance of vertex 9 from vertex 1 is 20. And the path is
1→ 3→ 7→ 8→ 6→ 9
This path is determined based on predecessor information.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
25
25
Keep Learning with Us VBSPU 4th sem. DAA Notes
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
26
26
Keep Learning with Us VBSPU 4th sem. DAA Notes
In this context, first we need to sort those items according to the value
of piwipiwi, so that pi+1wi+1pi+1wi+1 ≤ piwipiwi . Here, x is an array to store the
fraction of items.
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
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
Weight 40 10 20 24
Ratio (piwi)(piwi) 7 10 6 5
As the provided items are not sorted based on piwipiwi. After sorting, the items
are as shown in the following table.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
27
27
Keep Learning with Us VBSPU 4th sem. DAA Notes
Item B A C D
Weight 10 40 20 24
Ratio (piwi)(piwi) 10 7 6 5
Solution
After sorting all the items according to piwipiwi. 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.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
28
28
Keep Learning with Us VBSPU 4th sem. DAA Notes
Example:
Input : n = 3
L[] = { 5, 3, 10 }
Output : Order should be { 3, 5, 10 } with MRT = 29/3
Let us first break down the problem and understand what needs to be done.
A magnetic tape provides only sequential access of data. In an audio
tape/cassette, unlike a CD, a fifth song from the tape can’t be just directly
played. The length of the first four songs must be traversed to play the fifth
song. So in order to access certain data, head of the tape should be
positioned accordingly.
Now suppose there are 4 songs in a tape of audio lengths 5, 7, 3 and 2 mins
respectively. In order to play the fourth song, we need to traverse an audio
length of 5 + 7 + 3 = 15 mins and then position the tape head.
Retrieval time of the data is the time taken to retrieve/access that data in
its entirety. Hence retrieval time of the fourth song is 15 + 2 = 17 mins.
Now, considering that all programs in a magnetic tape are retrieved equally
often and the tape head points to the front of the tape every time, a new
term can be defined called the Mean Retrieval Time (MRT).
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
29
29
Keep Learning with Us VBSPU 4th sem. DAA Notes
Unit – 3
Dynamic Programming
0-1 knapsack problem-
In 0-1 Knapsack, items cannot be broken which means the thief should take the
item as a whole or should leave it. This is reason behind calling it as 0-1
Knapsack.
Hence, in case of 0-1 Knapsack, the value of xi can be either 0 or 1, where other
constraints remain the same.
0-1 Knapsack cannot be solved by Greedy approach. Greedy approach does not
ensure an optimal solution. In many instances, Greedy approach may give an
optimal solution.
The following examples will establish our statement.
Example
Let us consider that the capacity of the knapsack is W = 25 and the items are as
shown in the following table.
Item A B C D
Profit 24 18 18 10
Weight 24 10 10 7
Without considering the profit per unit weight (pi/wi), if we apply Greedy
approach to solve this problem, first item A will be selected as it will contribute
maximum profit among all the elements.
After selecting item A, no more item will be selected. Hence, for this given set of
items total profit is 24. Whereas, the optimal solution can be achieved by
selecting items, B and C, where the total profit is 18 + 18 = 36.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
30
30
Keep Learning with Us VBSPU 4th sem. DAA Notes
Here, Chain means one matrix's column is equal to the second matrix's row
[always].
In general:
If A = ⌊aij⌋ is a p x q matrix
B = ⌊bij⌋ is a q x r matrix
C = ⌊cij⌋ is a p x r matrix
Then
It can be observed that the total entries in matrix 'C' is 'pr' as the matrix is of
dimension p x r Also each entry takes O (q) times to compute, thus the total time
to compute all possible entries for the matrix 'C' which is a multiplication of 'A'
and 'B' is proportional to the product of the dimension p q r.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
31
31
Keep Learning with Us VBSPU 4th sem. DAA Notes
It is also noticed that we can save the number of operations by reordering the
parenthesis.
Example :
Input: keys[] = {10, 12}, freq[] = {34, 50}
There can be following two possible BSTs
10 12
\ /
12 10
I II
Frequency of searches of 10 and 12 are 34 and 50 respectively.
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
32
32
Keep Learning with Us VBSPU 4th sem. DAA Notes
Unit – 4
Back Tracking
The Backtracking is an algorithmic method to solve a problem with an
algorithmic-method
additional way. It uses a rrecursive
ecursive approach to explain the problems. We
can say that the backtracking is needed to find all possible combination to
solve an optimization problem.
Backtracking is a systematic way of trying out different sequences of
decisions until we find one that "works."
o Each non-leaf
leaf node in a tree is a parent of one or more other nodes (its
children)
o Each node in the tree, other than the root, has exactly one parent
8 Queen problem-
problem
The eight queens problem is the problem of placing eight queens on an 8×8
chessboard such that none of them attack one another (no two are in the
same row, column, or diagonal). More generally, the n queens problem
places n queens on an n×n chessboard.
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit:
Visit www.dpmishra.com
33
33
Keep Learning with Us VBSPU 4th sem. DAA Notes
if (row<8)
putQueen(row+1);
else success;
Chromatic number-
The minimum number of colors required for vertex coloring of graph ‘G’ is
called as the chromatic number of G, denoted by X(G).
χ(G) = 1 if and only if 'G ' is a null graph. If 'G ' is not a null graph,
then χ(G) ≥ 2
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
34
34
Keep Learning with Us VBSPU 4th sem. DAA Notes
Example:
Note − A graph ‘G’ is said to be n-coverable if there is a vertex coloring that uses
at most n colors, i.e., X(G) ≤ n.
Graph coloring-
Graph coloring is nothing but a simple way of labelling graph components
such as vertices, edges, and regions under some constraints. In a graph, no
two adjacent vertices, adjacent edges, or adjacent regions are colored with
minimum number of colors. This number is called the chromatic
number and the graph is called a properly colored graph.
Clustering
Data mining
Image capturing
Image segmentation
Networking
Resource allocation
Processes scheduling
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit: www.dpmishra.com
35
35
Keep Learning with Us VBSPU 4th sem. DAA Notes
Unit – 5
Branch and Bound
Branch and bound (BB, B&B, or BnB) is an algorithm design paradigm for
discrete and combinatorial optimization problems, as well as mathematical
optimization. ... The algorithm explores branches of this tree, which
represent subsets of the solution set.
Example
In the following example, we will illustrate the steps to solve the travelling
salesman problem.
1 2 3 4
1 0 10 15 20
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit:
Visit www.dpmishra.com
36
36
Keep Learning with Us VBSPU 4th sem. DAA Notes
2 5 0 9 10
3 6 13 0 12
4 8 8 9 0
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit:
Visit www.dpmishra.com
37
37
Keep Learning with Us VBSPU 4th sem. DAA Notes
Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit:
Visit www.dpmishra.com