Module 1 3
Module 1 3
Module 1 3
Problem
Algorithm
CHARACTERISTICS OF ALGORITHM
The non ambiguity requirement for each step of an algorithm cannot
be compromised
The range of inputs for which an algorithm work has to be specified
carefully
The same algorithm can be represented in several different ways
Several algorithm for solving the same problem may exist
Algorithm for the same problem can be based on different ideas and
can solve the problem with dramatically different speed
3 Design & Analysis of Algorithms
Euclids algorithm:
gcd(m,n)=gcd(n,m mod n)
gcd(60,24)=gcd(24,12)=gcd(12,0)=12
( gcd(m,0)=m )
Algorithm Euclid(m,n)
// computes gcd(m,n) by Euclids algorithm
// input: two non negative,non zero integers
// output: gcd of m&n
While (n≠0) do
r m mod n
mn
nr
return m
Example for 60,24 the algorithm will try first 24 then 23 and so on until it
reaches 12, where it stops.
4 Design & Analysis of Algorithms
NOTE: The above algorithm does not work correctly when one of its input
is zero.
Step 3: identify all the common factors in the two prime expansions found
in step 1 & step 2 ( if P is the common factor occurring Pm & Pn times in
m&n respectively, it should be min {Pm,Pn} times)
Step 4: compute the product of all the common factors 7 return it as gcd.
Ex: 60 = 2 . 2 . 3 .5
24 = 2 . 2 . 2 . 3
gcd( 60,24) = 2 . 2 . 3 = 12
Ex:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
23 5 7 9 11 13 15 17 19 21 23 25
23 5 7 11 13 17 19 23 25
23 5 7 11 13 17 19 23
number more than once. Obviously P.P should not be greater than n and
therefore P cannot exceed √n rounded down.
Understand the
problem
Design an algorithm
DESIGN AN ALGORITHM
Build a computational model of the solving process
Simplicity
Data terms can range from elementary data types to data structures
Arrays
Linked list
ARRAY
Is a sequence of n terms of the same data type that are stored
contiguously in computer memory and made accessible by specifying a
value of the array’s index
Each and every element of an array can be accessed in the same constant
amount of time regardless of where in the array element in question is
located ( this differentiate pair linked list)
Array are used to implement a variety of other data structures like string.
(also we have bit string)
Header
Arrays and linked list are two principle choices in representing a more
abstract data structures called a linear list of simply a list
PRIORITY QUEUES
A priority queue implementation is based on an ingenious data structure
called heap
GRAPHS
Informally : “ a collection of points in the plane called vertices or nodes,
some of them connected by line segments called edges or arches”
Formally: “ a graph G=(V, E) is defined by the pair of two sets. A finite set V
of vertices and set of E of pairs (vertices) these edges “
KEY TERM OF GRAPHS
Adjacent vertices (u,v)
Undirected edges (u,v)
Directed edges(u,v)→ u is head & v is tail
a c b a c b
d e f
d e f
Fig(a) fig(b)
(c,e) }
GRAPH REPRESENTATIO N
a b c d e f c d
a
a 0 0 1 1 0 0 b c f
b 0 0 1 0 0 1 c a b e
c 1 1 0 0 1 0 d a e
d 1 0 0 0 1 0 e c d f
e 0 0 1 1 0 1 f b e
f 0 1 0 0 1 0
1 if edge exist
A(i,j) =
0 if no edge
≥1 if there is a edge
C(i,j) =
0 if there is no edge
5
a b
1 7 4
c 2 d
a b c d
a 0 5 1 0 a b5 c1
b 5 0 7 4 b a5 c7 d4
c 1 7 0 2 c a1 b7 d2
d 0 4 2 0 d b4 c2
TREES
Is a connected acyclic graph
In tree |E|=|V|-1
Fails for f
a
g h
b e c
i
ROOTED TREES
This has root (decided)
State space trees that under line two important algorithm design technique
backtracking and branch and bound
TERMINOLOGIES OF NODES IN TREES
Ancestors of v
Parent of v
Child of v
Sibling are vertex having same parent
Leaf : no children
By types of problems
By design technique
Design techniques
ANALYSIS OF ALGORITHM
Why analyze the problem??
An algorithm can be analyzed in terms of time efficiency or space
utilization. We will consider only the former right now. The running time of
an algorithm is influenced by several factors:
If c(n) = ½ n(n-1), how much longer the algorithm run if we double the its
input size?
“It is for these reasons the efficiency analysis frame work ignores
multiplicative constants and concentrates on the count’s order of growth to
within a constant multiple for large-size inputs.”
GENERALISING RUNNING TIME
Comparing the growth of the running time as the input grows to the growth
of known functions.
2. sum = 0
3. i = 0
4. while i < n
7. i = i + 1
8. mean = sum / n
The computing time for this algorithm in terms on input size n is:
T(n) = 4n + 5
i←i+1
If I < n return I
Cworst(n)= n
Cbest(n)=1
Cavg(n)=(n+1)/2
Average case analysis is not so easy; it is not just by taking average of best
case & worst case efficiency
20 Design & Analysis of Algorithms
Analysis of Algorithms
Asymptotic Notation
“Helps in making meaning but inexact statements about
The word asymptotic means that it is the study of function of a parameter n, as n become larger
and larger without any bounds.Here we are concerned with how the runtime of an algorithm
increases with the size of the input. We can do analysis of algorithms which will accurately
reflect the way in which the computation time will increase with the size, but ignores the other
details with little effect on total. It permits us to provide upper and lower bounds on the value of
a function f for suitably large values.
(big-oh)
Ω(big-omega)
Ө(big-theta)
if there are positive integers C and N such that f(n) <= C * g(n) for all integers n >= N.
With this definition, we can clearly see the difference between the three types of notation:
In all three graphs above, n0 is the minimal possible value to get valid bounds, but any greater
value will work.
21 Design & Analysis of Algorithms
Theorem: For any two functions f(n) and g(n), f(n) = (g(n)) if and only if f(n) = O(g(n)) and f(n)
= (g(n)).
EXAMPLE:
As implied by the theorem above, to show this result, we must show two properties:
f(n) = O (n3)
f(n) = (n3)
First, we show (i), using the same techniques we've already seen for big-Oh.
We consider N = 1, and thus we only consider n >= 1 to show the big-Oh result.
f(n) = 3n3 + 3n - 1
< 3n3 + 3n + 1
= 7n3
thus, with
Next, we show (ii). Here we must provide a lower bound for f(n). Here, we choose a value for
N, such that the highest order term in f(n) will always dominate (be greater than) the lower order
terms.
We choose N = 2, since for n >=2, we have n3 >= 8. This will allow n3 to be larger than the
remainder of the polynomial (3n - 1) for all n >= 2.
So, by subtracting an extra n3 term, we will form a polynomial that will always be less than f(n)
for n >= 2.
f(n) = 3n3 + 3n - 1
= 2n3
f(n) = (n3)
22 Design & Analysis of Algorithms
Big-Oh Operations
Summation Rule
Suppose T1(n) = O(f1(n)) and T2(n) = O(f2(n)). Further, suppose that f2 grows no faster than
f1, i.e., f2(n) = O(f1(n)). Then, we can conclude that T1(n) + T2(n) = O(f1(n)). More generally,
the summation rule tells us O(f1(n) + f2(n)) = O(max(f1(n), f2(n))).
Proof:
Suppose that C and C' are constants such that T1(n) <= C * f1(n) and T2(n) <= C' * f2(n).
Product Rule:
Suppose T1(n) = O(f1(n)) and T2(n) = O(f2(n)). Then, we can conclude that
The Product Rule can be proven using a similar strategy as the Summation Rule proof.
General Rules:
• All basic statements (assignments, reads, writes, conditional testing, library calls) run in
constant time: O(1).
• The time to execute a loop is the sum, over all times around the loop, of the time to
execute all the statements in the loop, plus the time to evaluate the condition for
termination. Evaluation of basic termination conditions is O(1) in each iteration of the
loop.
Compute the big-Oh running time of the following C++ code segment:
for (i = 2; i < n; i++) {
sum += i;
}
23 Design & Analysis of Algorithms
• The number of iterations of a for loop is equal to the top index of the loop minus the
bottom index, plus one more instruction to account for the final conditional test.
• Note: if the for loop terminating condition is i <= n, rather than i < n, then the number of
times the conditional test is performed is:
• ((top_index + 1) – bottom_index) + 1)
Consider the sorting algorithm shown below. Find the number of instructions executed and the
complexity of this algorithm.
for (i = 1; i < n; i++) {
SmallPos = i;
Smallest = Array[SmallPos];
for (j = i+1; j <= n; j++)
if (Array[j] < Smallest) {
SmallPos = j;
Smallest = Array[SmallPos]
}
Array[SmallPos] = Array[i];
Array[i] = Smallest;
}
The total computing time is:
= 5n - 5 + (4n2 - 2n) / 2
= 5n - 5 + 2n2 - n
= 2n2 + 4n - 5
= O(n2)
24 Design & Analysis of Algorithms
EXAMPLE :
The following program segment initializes a two-dimensional array A (which has n rows
and n columns) to be an n x n identity matrix – that is, a matrix with 1’s on the diagonal
and 0’s everywhere else. More formally, if A is an n x n identity matrix, then:
Here is a simple linear search algorithm that returns the index location of a value in an array.
1 + 3 + 5 + ... + (2n - 1) / n
= (2 (1 + 2 + 3 + ... + n) - n) / n
We know that 1 + 2 + 3 + ... + n = n (n + 1) / 2, so the average number of
lines executed is:
[2[n(n+1)/2] – n]/n
=n
=O(n)
25 Design & Analysis of Algorithms
An Example:
Consider the Algorithm
Algorithm MaxElement(A[0..n-1])
//Determines the value largest element
//Input:A[0..n-1]
//output: value of largest element
MaxValueßa[0]
For iß 1 to n-1 do
If A[i] > maxval maxvalßA[i]
Return MaxVal.
Summation formulas
• ∑i=0u 1 = u-l +1 where l ≤ u
≈1/2 n2 ɛ Ө(n2)
Example
26 Design & Analysis of Algorithms
//Computes n! recursively,
If n= 0 return 1
Here M(n) is defined implicitly, i.e. in terms of its value at another point, namely n-1. Such
equations are termed as Recurrence Relations or Recurrence.
M(0) = 0
[the call stops when n=0] [no multiplication when n=0]
Now we set as follows
M(n)=0 , n=0
We also have
F(n) = F(n-1) . N for n> 0
F(n)= 1 n=0
Back substitution to solve recurrence
3) Check whether basic operation vary for different inputs of the same size.
worst case
Best case &
Average case efficiency
4) Set up a recurrence relation with initial condition w.r.t basic operation.
5) Solve the recurrence and ascertain its order of growth.
BRUTE FORCE
The simplest of the design strategies.
Brute force is a straight forward approach to solving a problem, usually directly based on
the problem statement and definition of the concepts involved.
Brute force strategy I s indeed the one that is easiest to apply.
It should not be over looked as an upper text algorithm design strategy.
Unlike others it is applicable for a wide variety of problem like computing sum of n
numbers, largest of n and so on.
For some like sorting, searching, matrix multiplication etc. it reasonable algorithm with
some of practical value.
For some, it is designing more efficient one may be unjustifiable, if only a few instances
of the problem need to be solved. Hence brute force can solve with acceptable speed.
Even if too efficient a brute force can be still useful for solving small size instances of a
problem.
Selection sort:
We start selection sort by scanning the entire given list to find its smallest element and the
exchange it with the first element, putting the smallest element in its final position in the sorted
list. Then we scan list, starting with the second element, putting the second element in its final
position. Generally, on the i th pass through the list, which we number from 0 to n-2, the
algorithm searches for the smallest element among the last n-1 elements and swaps it with Ai:
Min ← i
For j← i+1 to n-1 do
If A[j] < A[min]
Min ← j
Swap A[j] and A[min]
As an example, the action of the algorithm on the list 89, 45, 68, 90, 29, 34, 17 is illustrated
below:
89 45 68 90 29 34 17
17 45 68 90 29 34 89
17 29 68 90 45 34 89
17 29 34 90 45 68 89
17 29 34 45 90 68 89
17 29 34 45 68 90 89
17 29 34 45 68 89 90
The analysis of selection sort is straight forward.The input’s size given by the number of
elements n;the algorithm’s basic operation is the key comparison A[j]<A[min].the number of
times it is executed depends only on the array size:
n−2 n −1 n−2
( n−1 ) n
C(n)= ∑❑ ∑ 1 = ∑ (n−1−i) =
2
i=0 j =i +1 i=0
Thus selection sort is a θ(n2) algorithm on all inputs. Note, however, that the number of key
swaps is only θ(n) or, more precisely, n-1. This property distinguishes selection sort positively
from many others sorting algorithms.
Bubble sort:
Another brute-force application to the sorting problem is to compare adjacent elements of the
list and exchange them if they are out of order. By doing it repeatedly, we end up “bubbling up”
the largest element to the last position on the list. The next pass bubbles up the second largest
element, and so on until, after n-1 passes, the list is sorted. Pass i(0<i<n-2)of bubble sort can be
represented by the following diagram:
The number of key comparisons for the bubble sort version given above is the same for all
arrays of size n; it is obtained by a sum that is almost identical to the sum for selection sort:
n−2 n−2−i n−2
C(n) = ∑❑ ∑ 1 = ∑ [ ( n−2−i) −0+1]
i=0 j=0 i=0
n−2
n(n−1)
= ∑ (n−1−i) =
2
Є θ(n2)
i=0
The number of keys swaps, however, depends on the input. For the worst case of decreasing
arrays, it is the same as the number of key comparisons:
n(n−1)
Sworst(n) = C(n) = Є θ(n2)
2
i← i+1
if i< n return i
else return -1
another straight forward improvement can be incorporated in sequential search if a given list is
known to be sorted searching in such a list can be stopped as soon as an element greater than
or equal to the search key is encountered.
We want to find i, the index of the leftmost character of the first matching sub string in the text,
to……..ti……….ti+j…..ti+m-1……..tn-1 text T
↑ ↑ ↑
Po Pj Pm-1 pattern P
If matches other than the first one need to be for and a string-matching algorithm can simply
continue working until the entire text is executed.
Note: The last position in the text which can be still be a beginning of a matching string in n-m.
Exhaustive search:
It is simply a brute force approach to combinational problems. It suggests generating each and
every element of the problems domain, selecting those of them that satisfy all the constraints
and then finding a desired element.
It can be stared as the problem of finding the shortest Hamiltonian circuit of the graph. Hamilton
circuit is defined as a cycle that passes through all the vertices of the graph exactly once.
a b
5 3
8 7
c 1 d
Tour Length
a→ b→ c→ d→ a l = 2+8+1+7=18
a→ b→ d→ c→ a l = 2+3+1+5=11
a→ c→ b→ d→ a l=5+8+3+7=23
a→ c→ b→ d→ a l=5+1+3+2=11
a→ d→ b→ c→ a l=7+3+8+5=23
a→ d→ c→ b→ a l=7+1+8+2=18
Tour is sequence of n+1 adjacent vertices, where first and last vertices are the same.
Compute the tour length and find the shortest one.
All vertices can be got with n-1 permutations.
Some of the tours can be avoided.
Total number of permutations will still be (n-1)!/2.
Knapsack problem:
Given n items of known weights w1, w2,…..wn & v1, v2,…..vn and a knapsack of capacity w.
Find the most valuable subset of the items that fit into the knapsack.
33 Design & Analysis of Algorithms
Φ 0 $0
{1} 7 $42
{2} 3 $12
{3} 4 $40
{4} 5 $25
{1,2} 10 $36
{1,4} 12 $52
{2,3} 7 $37
{2,4} 8 $65