UNIT 1_DAA_NOTES_2023
UNIT 1_DAA_NOTES_2023
Algorithm: An Algorithm is a finite sequence of instructions, each of which has a clear meaning
and can be performed with a finite amount of effort in a finite length of time. No matter what the
input values may be, an algorithm terminates after executing a finite number of instructions.
In addition every algorithm must satisfy the following criteria:
Input: there are zero or more quantities, which are externally supplied;
Output: at least one quantity is produced
Definiteness: each instruction must be clear and unambiguous;
Finiteness: if we trace out the instructions of an algorithm, then for all cases the algorithm
will terminate after a finite number of steps;
Effectiveness: every instruction must be sufficiently basic that it can in principle be
carried out by a person using only pencil and paper.
ALGORITHM PROGRAM
Design Implementation
Advantages of Algorithms:
It is easy to understand.
Algorithm is a step-wise representation of a solution to a given problem.
In algorithm the problem is broken down into smaller pieces or steps hence, it is
easier
for the programmer to convert it into an actual program.
Disadvantages of Algorithms:
As we know, an algorithm is a finite step-by-step set of instructions, which are meaningful and
used to solve any particular problem. To perform every process, there is a set of Algorithms,
which lets us know how to proceed with the problem and solve it in some steps.
The analysis of an algorithm is a technique that measures the performance of an
algorithm. The factors over which the algorithms majorly depend are the space and time
complexities. There are other factors as well, which we use for the analysis of algorithms.
Importance of Analysis
Performance Analysis: The performance of a program is the amount of computer memory and
time needed to run a program. We use two approaches to determine the performance of a
program. One is analytical, and the other experimental. In performance analysis we use
analytical methods, while in performance measurement we conduct experiments.
We can analyze performance of an algorithms by the means of following:
Time Complexity: Time complexity is the amount of time taken by an algorithm to run, as a
function of the length of the input. It measures the time taken to execute each statement of code
in an algorithm.
The algorithms are generally of two types (i) Iterative, and (ii) Recursive in nature
Example 1
Example 2
Example 3
Example 4
Example 5
Substitution Method: This method is used to find the complexity of recursive function. Find the values
of the function up to three steps and then back substitute the values to get the answer.
Example 1
Example 2
Example 3
Example 4
Example 5
Example 6
Example 7
This method is the pictorial representation of an iteration method, which is in the form of a tree.
At each level the roots are expanded. In this we consider the term in the recurrence as root. It is
useful when divide and conquer algorithm is used. This method can only be used when the right
hand side of the recurrence relation has minimum 2 recursive terms and 1 non-recursive term.
In a recursion tree ,each node represents the cost of a single sub-problem somewhere in the set of
recursive problems invocations .we sum the cost within each level of the tree to obtain a set of
per level cost, and then we sum all the per level cost to determine the total cost of all levels of
recursion .
Example 1
Example 2
Example 3
Example 4
Master’s Theorem:
Example 2 (Case 1)
Example 3 (case 3a)
Example 5 (Case 1)
Example
Asymptotic Notations
Asymptotic notation provides the basic vocabulary for discussing the design and analysis of
algorithms. Asymptotic means a line that tends to converge to a curve, which may or may not
eventually touch the curve. It is a line that stays within the bounds. It is a quick way to talk about
the fastest possible or slowest possible running time of an algorithm. It describes the rate of
growth of functions.
Asymptotic notations suppresses the constant factors (system dependent) and lower order terms
(irrelevant for large inputs)
1) Big Oh (O)
The Big-O notation describes the worst-case running time of a program. We compute the Big-O of an
algorithm by counting how many iterations an algorithm will take in the worst-case scenario with an input
of N. We typically consult the Big-O because we must always plan for the maximum time an algorithm
can take. For example, O (log n) describes the Big-O of a binary search algorithm. It is depicted as
Example 1
Example 2
Example 3
SORTING ALGORITHMS
INSERTION SORT: In insertion sort the items are inserted into its proper place in the final list.
The simplest implementation of this requires two list structures – the source list and the list into
which the sorted items are inserted. To save memory, most implementations use an in-place sort
that works by moving the current item pass the already sorted items and repeatedly swapping it
with the preceding item until in place.
HEAP SORT
Heap Sort
Binary Heap: Binary Heap is an array object can be viewed as Complete Binary Tree. Each node of the
Binary Tree corresponds to an element in an array
Length [A]: number of elements in array 2.
Heap-Size[A], number of elements in a heap stored within array A.
The root of tree A [1] and gives index 'i' of a node that indices of its parents, left child, and the right child
can be computed.
1. PARENT (i)
2 Return floor (i/2)
3.LEFT (i)
4. Return 2i
5.RIGHT (i)
6. Return 2i+1
This takes us (again, correctly) to the 6. Now, 4's index is 7, we want to go to the parent, so we calculate
7/2 =3 which takes us to the 17.
Heap Property: A binary heap can be classified as Max Heap or Min Heap
1. Max Heap: In a Binary Heap, for every node I other than the root, the value of the node is
greater than or equal to the value of its highest child.
2. A [PARENT (i) ≥A[i] Thus, the highest element in a heap is stored at the root. Following is an
example of MAX-HEAP
3. MIN-HEAP: In MIN-HEAP, the value of the node is lesser than or equal to the value of its lowest
child. 1. A [PARENT (i) ≤A[i]
4.
HEAPIFY