Design & Analysis of Algorithm Notes For BCA Purvanchal 4th Sem PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

11

Keep Learning with Us VBSPU 4th sem. DAA Notes

Semester-4

BCA 401

DAA
(Design & Analysis of Algorithm)

(According to Purvanchal University Syllabus)

“Full Line By Line Notes”

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.

Difference between Algorithm and Pseudocode


 An algorithm is a formal definition with some specific characteristics that
describes a process, which could be executed by a Turing-complete
computer machine to perform a specific task. Generally, the word
"algorithm" can be used to describe any high level task in computer
science.
 On the other hand, pseudocode is an informal and (often rudimentary)
human readable description of an algorithm leaving many granular details
of it. Writing a pseudocode has no restriction of styles and its only

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

objective is to describe the high level steps of algorithm in a much realistic


manner in natural language.

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.

Given functions f and g, we wish to show how


how to quantify the statement:

“g grows as fast as f”.

The growth of functions is directly related to the complexity of algorithms. We are


guided by the following principles.

• We only care about the behavior for “large” problems.

• We may ignore implementation


entation details such as loop counter incrementation.

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.

When using summation notation, X1 means “the first x-value”,


value”, X2 means “the
second x-value”
value” and so on. For example, let’s say you had a list of weights: 100lb,
150lb, 153lb and 202lb. The weights and their corresponding x- x-values are:
X1: 100lb
X2: 150lb
X3: 153lb
X4: 202lb

x value”. This would be


The “i=1” at the base of Σ means “start at your first x-value”.
X1 (100lb in this example). The “n” at the top of Σ means “end at n”. In statistics, n
is the number of items in the data set. So what this summation is asking you to do
is “add up all of your x-values last.” For this set of data, that
values from the first to the last.”
would be:
100 lb + 150 lb + 153 lb + 202 lb = 605 lb.
Note: If you see a number above Σ, instead of n, it means to add up to a certain
point. For example, a “3” above the Σ means to sum up the the third item (X3) in
the set.

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 −

A recurrence relation can be solved using the following methods −


 Substitution Method − In this method, we guess a bound and using
mathematical induction we prove that our assumption was correct.
 Recursion Tree Method − In this method, a recurrence tree is formed
where each node represents the cost.
 Master’s Theorem − This is another important technique to find the
complexity of a recurrence relation.

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.

Examples of sets are:

 A set of rivers of India.


 A set of vowels.

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.

If A is a set, and a is one of the elements of A, then we denote it as a ∈ A. Here the


symbol ∈ means -"Element of."

Sets Representation:

Sets are represented in two forms:-

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}

Counting and probability–


 Counting in probability refers to the techniques used to count the set of
possible outcomes. The dependence or independence of the events is the
main factor in determining how to calculate the total possible outcomes.
Independent events don't affect each other. For example, the number of
outfits possible from two pants and four shirts is found by multiplying the
number of possibilities for each selection together, . Dependent
variables, or permutations, do affect each other. An example is ranking
three favorite items: once the first is picked, there are only two choices for
second place, with third place as the last picked. Thus, there are
possible rankings.

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

 The word 'Probability' means the chance of occurring of a particular event.


It is generally possible to predict the future of an event quantitatively with
being correct. The probability is used in such cases
a certain probability of being
where the outcome of the trial is uncertain.

The probability of happening of an event A, denoted by P(A), is defined as

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

And the probability of non-happening


non of A is

Note:

1. The probability of an event which is certain to occur is one.


2. The probability of an event which is impossible to zero.
3. If the probability of happening of an event P(A) and that of not happening is
P(A), then
P(A)+ P(A) = 1, 0 ≤ P(A) ≤ 1,0≤ P(A)≤1.

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.

1. Divide the original problem into a set of subproblems.


2. Conquer: Solve every subproblem individually, recursively.
3. Combine: Put together the solutions of the subproblems to get the solution
to the whole problem.

Generally, we can follow the divide-and-conquer approach in a three-step


process.

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.

2. Firstly, we take the whole array as an interval.

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.

5. Repeatedly, check until the value is found or interval is empty.

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.

The Radix Sort Algorithm


1) Do following for each digit i where i varies from least significant digit to the
most significant digit.
………….a) Sort input array using counting sort (or any stable sort) according to the
i’th digit.
Example:
Original, unsorted list:
170, 45, 75, 90, 802, 24, 2, 66

Sorting by least significant digit (1s place) gives:


[*Notice that we keep 802 before 2, because 802 occurred before 2 in the original
list, and similarly for pairs 170 & 90 and 45 & 75.]
170, 90, 802, 2, 24, 45, 75, 66
Sorting by next digit (10s place) gives:
[*Notice that 802 again comes before 2 as 802 comes before 2 in the previous
list.]
802, 2, 24, 45, 66, 170, 75, 90
Sorting by most significant digit (100s place) gives:
2, 24, 45, 66, 75, 90, 170, 802
Bucket sort-
Bucket sort is mainly useful when input is uniformly distributed over a range. For
example, consider the following problem.
Sort a large set of floating point numbers which are in range from 0.0 to 1.0 and
are uniformly distributed across the range. How do we sort the numbers
efficiently?

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

Following example explains the above steps:


arr[] = 64 25 12 22 11

// Find the minimum element in arr[0...4]


// and place it at beginning
11 25 12 22 64

// Find the minimum element in arr[1...4]


// and place it at beginning of arr[1...4]
11 12 25 22 64

// Find the minimum element in arr[2...4]


// and place it at beginning of arr[2...4]
11 12 22 25 64

// Find the minimum element in arr[3...4]


// and place it at beginning of arr[3...4]
11 12 22 25 64

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.

Heap Sort Algorithm for sorting in increasing order:


1. Build a max heap from the input data.
2. At this point, the largest item is stored at the root of the heap. Replace it with
the last item of the heap followed by reducing the size of heap by 1. Finally,
heapify the root of tree.
3. Repeat above steps while size of heap is greater than 1.
How to build the heap?
Heapify procedure can be applied to a node only if its children nodes are
heapified. So the heapification must be performed in the bottom up order.
Lets understand with the help of an example:

Input data: 4, 10, 3, 5, 1


4(0)
/ \
10(1) 3(2)
/ \
5(3) 1(4)

The numbers in bracket represent the indices in the array


representation of data.

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

Applying heapify procedure to index 1:


4(0)
/ \
10(1) 3(2)
/ \
5(3) 1(4)

Applying heapify procedure to index 0:


10(0)
/ \
5(1) 3(2)
/ \
4(3) 1(4)
The heapify procedure calls itself recursively to build heap
in top down manner.

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.

Components of Greedy Algorithm


Greedy algorithms have the following five components −
 A candidate set − A solu on is created from this set.
 A selection function − Used to choose the best candidate to be added to
the solution.
 A feasibility function − Used to determine whether a candidate can be
used to contribute to the solution.
 An objective function − Used to assign a value to a solu on or a par al
solution.
 A solution function − Used to indicate whether a complete solu on has
been reached.

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

Minimum spanning tree-


 A spanning tree is a subset of an undirected Graph that has all the vertices
connected by minimum number of edges.
 A Minimum Spanning Tree (MST) is a subset of edges of a connected
weighted undirected graph that connects all the vertices together with the
minimum possible total edge weight. To derive an MST, Prim’s algorithm or
Kruskal’s algorithm can be used.
 As we have discussed, one graph may have more than one spanning tree. If
there are n number of vertices, the spanning tree should have n - 1 number
of edges. In this context, if each edge of the graph is associated with a
weight and there exists more than one spanning tree, we need to find the
minimum spanning tree of the graph.
Moreover, if there exist any duplicate weighted edges, the graph may have
multiple minimum spanning tree.

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

Dijkastra’s Algorithm for shortest path for a single


source-
Dijkstra’s algorithm solves the single-source shortest-paths problem on a
directed weighted graph G = (V, E), where all the edges are non-negative
(i.e., w(u, v) ≥ 0 for each edge (u, v) Є E).
In the following algorithm, we will use one function Extract-Min(), which extracts
the node with the smallest key.
Algorithm: Dijkstra’s-Algorithm (G, w, s)
for each vertex v Є G.V
v.d := ∞
v.∏ := NIL
s.d := 0
S := Ф
Q := G.V
while Q ≠ Ф
u := Extract-Min (Q)
S := S U {u}
for each vertex v Є G.adj[u]
if v.d > u.d + w(u, v)
v.d := u.d + w(u, v)
v.∏ := u
Analysis
The complexity of this algorithm is fully dependent on the implementation of
Extract-Min function. If extract min function is implemented using linear search,
the complexity of this algorithm is O(V2 + E).
In this algorithm, if we use min-heap on which Extract-Min() function works to
return the node from Q with the smallest key, the complexity of this algorithm
can be reduced further.
Example
Let us consider vertex 1 and 9 as the start and destination vertex respectively.
Initially, all the vertices except the start vertex are marked by ∞ and the start
vertex is marked by 0.

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

Step1 Step2 Step3 Step4 Step5 Step6 Step7 Step8


Vertex Initial
V1 V3 V2 V4 V5 V7 V8 V6

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

Fractional knapsack problem-


Knapsack
 Given a set of items, each with a weight and a value, determine a subset of
items to include in a collection so that the total weight is less than or equal
to a given limit and the total value is as large as possible.
Fractional Knapsack
In this case, items can be broken into smaller pieces, hence the thief can select
fractions of items.
According to the problem statement,
 There are n items in the store
 Weight of ith item wi>0wi>0
 Profit for ith item pi>0pi>0 and
 Capacity of the Knapsack is W
In this version of Knapsack problem, items can be broken into smaller pieces. So,
the thief may take only a fraction xi of ith item.
0⩽xi⩽10⩽xi⩽1
The ith item contributes the weight xi.wixi.wi to the total weight in the knapsack
and profit xi.pixi.pi to the total profit.
Hence, the objective of this algorithm is to
maximize∑n=1n(xi.pi)maximize∑n=1n(xi.pi)
subject to constraint,
∑n=1n(xi.wi)⩽W∑n=1n(xi.wi)⩽W
It is clear that an optimal solution must fill the knapsack exactly, otherwise we
could add a fraction of one of the remaining items and increase the overall profit.
Thus, an optimal solution can be obtained by
∑n=1n(xi.wi)=W∑n=1n(xi.wi)=W

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

Profit 280 100 120 120

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

Profit 100 280 120 120

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.

Optimal storage on tapes-


 Given programs stored on a computer tape and length of each program is
where , find the order in which the programs should be stored in
the tape for which the Mean Retrieval Time (MRT given as ) is minimized.

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

Matrix chain multiplication problem-


It is a Method under Dynamic Programming in which previous output is taken as
input for next.

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

Given following matrices {A1,A2,A3,...An} and we have to perform the matrix


multiplication, which can be accomplished by a series of matrix multiplications

A1 xA2 x,A3 x.....x An

Matrix Multiplication operation is associative in nature rather commutative. By


this, we mean that we have to follow the above matrix order for multiplication
but we are free to parenthesize the above multiplication depending upon our
need.

In general, for 1≤ i≤ p and 1≤ j ≤ r

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.

Optimal binary search tree-


 A Binary Search Tree (BST) is a tree where the key values are stored in the internal
nodes. The external nodes are null nodes.
 An optimal binary search tree is a BST, which has minimal expected cost of locating
each node

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

Input: keys[] = {10, 12, 20}, freq[] = {34, 8, 50}


There can be following possible BSTs
10 12 20 10 20
\ / \ / \ /
12 10 20 12 20 10
\ / / \
20 10 12 12
I II III IV V
Among all possible BSTs, cost of the fifth BST is minimum.
Cost of the fifth BST is 1*50 + 2*34 + 3*8 = 142

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."

In the following Figure:

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

8 Queen problem Algorithm:


putQueen(row)

for every position col on the same row

if position col is available

place the next queen in position col

if (row<8)

putQueen(row+1);

else success;

the queen from position col

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.

Applications of Graph Coloring:


Graph coloring is one of the most important concepts in graph theory. It is used
in many real-time applications of computer science such as −

 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.

Travelling salesman problem-


problem
 The traveling salesman problem
p (TSP) is an algorithmic problem tasked
with finding the shortest route between a set of points and locations that
's goal is to keep both the travel costs and
must be visited. ... The salesman's
the distance traveled as low as possible.

Example
In the following example, we will illustrate the steps to solve the travelling
salesman problem.

From the above graph, the following table is prepared.

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

The minimum cost path is 35.


1}, we get the minimum value for d [1, 2]. When s =
Start from cost {1, {2, 3, 4}, 1},
3,, select the path from 1 to 2 (cost is 10) then go backwards. When s = 2, we get
the minimum value for d [4, 2]. 2]. Select the path from 2 to 4 (cost is 10) then go
backwards.
When s = 1, we get the minimum value for d [4, 2] but 2 and 4 is already
selected. Therefore, we select d [4, 3] (two possible values are 15 for d [2, 3] and
d [4, 3], but our last node of the path is 4). Select path 4 to 3 (cost is 9), then go
to s = Φ step. We get the minimum value for d [3, 1] (cost is 6).

Exams test your memory, life tests your learning; others will test your patience.
For More Information Visit:
Visit www.dpmishra.com

You might also like