Algorithm: Definition (Algorithm) : An Algorithm Is A Finite Set of Instructions That, If Followed, Accomplishes A
Algorithm: Definition (Algorithm) : An Algorithm Is A Finite Set of Instructions That, If Followed, Accomplishes A
Algorithm
The word algorithm comes from the name of a Persian author, Abu Ja'far Mohammedibn Musa al
Khowarizm.
Definition [Algorithm]: An algorithm is a finite set of instructions that, if followed, accomplishes a
particular task.
Algorithms must satisfy the following criteria:
1. Input: Zero or more quantities are externally supplied.
2. Output: At least one quantity is produced.
3. Definiteness: Each instruction is clear and unambiguous.
4. Finiteness: If we trace out the instructions of an algorithm, then for all cases, the algorithm
terminates after a finite number of steps.
5. Effectiveness. Every instruction must be very basic so that it can be carried out by pencil
and paper and it also must be feasible.
1. Devise algorithms: (i).Divide & Conquer,(ii). Branch and Bound ,(iii) Dynamic Programming.
2. Validate algorithms: Check for that it computes the correct answer for all possible legal inputs.
3.Analyze algorithms: Determining how much computing time & storage an algorithm requires
4. Test a program:
(i) Debugging : To determine whether faulty results occur and, if so, to correct them.
(ii). Profiling(performance measurement) : measuring the time and space it takes to
compute the results
Algorithm arrayMax(A, n)
Input array A of n integers
Output maximum element of A
currentMax A[0]
for i 1 to n 1 do
if A[i] currentMax then
currentMax A[i]
return currentMax
ALGORITHM SPECIFICATION
Pseudocode Convention
Algorithms using pseudocode that resembles C and Pascal.
7. For
9. conditional statement
10. procedure
Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and
Recursive Algorithm
A recursive function is a function that is defined in terms of itself. Similarly, an algorithm is said to be
recursive if the same algorithm is invoked in the body.
Example: Towers of Hanoipuzzl
Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one
rings is as depicted
These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits
over the larger one. There are other variations of the puzzle where the number of disks increase, but
the tower count remains the same.
Rules
A few rules to be followed for Tower of Hanoi are :
Only one disk can be moved among the towers at any given time.
Only the "top" disk can be removed.
No large disk can sit over a small disk.
Space complexity
Definition [Space complexity]: The space complexity of an algorithm is the amount of memory it
needs to run to completion.
Example:
The Space needed by algorithm is seen to be the sum of the following component:
a. Fixed Part
b. Variable Part
A fixed part that is independent of the characteristics (eg:number,size)of the inputs and Outputs.4
Ex:
Space for the code
space for variable and fixed-size component variables
A variable part that consists of the space needed by component variables whose size is dependent
on the particular problem instance being solved.
Ex:
Space needed by referenced variables
Recursion stacks space.
The space requirement s(p) of any algorithm p may therefore be written as,
S(P) = c+ Sp(Instance characteristics) Where ‘c’ is a constant.
Algorithm sum(a,n)
{
s=0.0;
for I=1 to n do
s= s+a[I];
return s;
}
The problem instances for this algorithm are characterized by n, the number of elements to
be summed. The space needed d by ‘n’ is one word, since it is of type integer.
The space needed by ‘a’ is the space needed by variables of type array of floating point
numbers.
This is at least ‘n’ words, since ‘a’ must be large enough to hold the ‘n’ elements to be
summed.
So, we obtain S sum(n)>=(n+s)
Time Complexity
Definition [Time Complexity]: The time complexity of an algorithm is the amount of computer time
it needs to run to completion.
The compile time does not depend on the instance characteristics. Also we may assume that a
compiled program will be run several times without recompilation .This rum time is denoted by
tp(instance characteristics).
The number of steps any problem statement is assigned depends on the kind of statement called
steps per execution (s/e).
Example: 1 Time Complexity Analysis of SUM
The following notations are commonly use notations in performance analysis and used to
characterize the complexity of an algorithm:
1. Big–OH (O) ,
2. Big–OMEGA (Ω),
3. Big–THETA (Θ)
4. Little–OH (o)
5. Little –OMEGE ()
In Asymptotic Notation gives us a measure that will work for different operating systems,
compilers and CPUs
It is a way to describe the characteristics of a function in the limit.
It describes the rate of growth of functions.
Focus on what’s important by abstracting away low-order terms and constant factors.
The following are the names of the various time complexities.
Time complexity Name
O(1) Constant
O(log n) Logarithmic
O (n) Linear
O(nlog n) Linear
2
O(n ) Quadratic
3
O(n ) Cubic
n
O(2 ) Exponential
Examples:
(i). 3n+2 = 0(n) as 3n+2 < 4n for all n > 2.
(ii). 3n + 3 = O(n) as 3n + 3 < 4n for all n > 3.
(iii). 10n2+4n +2 = 0(n2)as 10n2 +4n+2 < lln2 for all n >5
Examples:
Examples:
i.
ii.
Performance Measurement
Performance measurement is concerned with obtaining the space and time requirements of a
particular algorithm. These quantities depend on the compiler and options used as well as on the
computer on which the algorithm is run.
We do not consider measuring the run-time space requirements of a program. Rather, we focus on
measuring the computing time of a program. To obtain the computing(or run) time of a program, we
need a clocking procedure. We assume the existence of a program GetTime() that returns the
current time in millisecond
Example:
Example 1:
Where
T(n) is the time for DAndCon any input of size n
g(n) is the time to compute the answer directly for small inputs.
f(n) is the time for dividing P and combining the solutions to subproblems.
The complexity of many divide-and-conquer algorithms is given by recurrences of the form
Binary Search
1. Binary Search finds the position of a specified input value (the search "key") within an array
sorted by key value.
2. In each step, the algorithm compares the search key value with the key value of the middle
element of the array.
3. If the keys match, then a matching element has been found and its index, or position, is
returned.
4. Otherwise, if the search key is less than the middle element's key, then the algorithm repeats its
action on the sub-array to the left of the middle element or, if the search key is greater, then the
algorithm repeats on sub array to the right of the middle element.
5. If the search element is less than the minimum position element or greater than the maximum
position element then this algorithm returns not found.
Example:
Let us select the 14 entries
-15,-6,
6, 0,7, 9, 23,54, 82,101, 112, 125, 131, 142, 151
15
Tracing the Algorithm for two cases Binary decision tree for binary
When x=151 When x=-14 search tree when n=14
Computing time of binary search by giving formulas that describe the best,average, and worst cases
Now we compare the value stored at location 4, with the value being searched, i.e. 31. We find that
the value at location 4 is 27, which is not a match. As the value is greater than 27 and we have a
sorted array, so we also know that the target value must be in the upper portion of the array.
We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.
The value stored at location 7 is not a match, rather it is more than what we are looking for. So, the
value must be in the lower part from this location.
We compare the value stored at location 5 with our target value. We find that it is a match.
The best case occurs when the elements are in increasing order. The number of element
comparisons is n-1
The worst case occurs when the elements are in decreasing order. In this case the number of
element comparisons is 2(n-1).
The average number of comparisons is 3n/2-1
Divide-and-conquer algorithm:
A divide-and-conquer algorithm for this problem would proceed as follows: Let P = (n,
a[i],..., a[j]) denote an arbitrary instance of the problem.
3n/2-2 is the best-, average-, and worst-case number of comparisons when n is a power of two.
Time Complexity
Algorithm
Example: on Board
The Partition algorithm can also be used to obtain an efficient solution for the selection problem. In
this problem, we are given n elements a[1: n] and are required to determine the kth smallest element.
Logic:
If the partitioning element v is positioned at a[j], then j-1 elements are less than or equal to a[j] and n-j
elements are greater than or equal to a[j]. Hence if K< j, then the kth smallest elements in a[1:j].
For the first position in the sorted list, the whole list is scanned sequentially. The first position where
14 is stored presently, we search the whole list and find that 10 is the lowest value.
So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in the list,
appears in the first position of the sorted list.
SORTED UNSORTED
SORTED UNSORTED
for all i and j between1and n.. To compute C(i,j) using this formula, we need n multiplications. As the
matrix C has n2 elements, the time for the resulting matrix multiplication algorithm, which we refer to
as the conventional method is ((n3).
Following is simple Divide and Conquer method to multiply two square matrices.
1) Divide matrices A and B in 4 sub-matrices
sub matrices of size N/2 x N/2 as shown in the below diagram.
2) Calculate following values recursively. ae + bg, af + bh, ce + dg and cf + dh.