18ISE51
18ISE51
Functions
TOPICS TO BE COVERED:
● Introduction
common functions,
An algorithm is composed of a finite set of steps, each of which may require one or more
operations. The possibility of a computer carrying out these operations necessitates that certain
constraints be placed on the type of operations an algorithm can include.
The fourth criterion for algorithms is that they terminate after a finite number of
operations.
The fifth criterion requires that, each operation be effective; each step must be such
that it can, at least in principal, be done by a person using pencil and paper in a finite
amount of time. Performing arithmetic on integers is an example of effective
operation, but arithmetic with real numbers is not, since some values may be
expressible only by infinitely long decimal expansion. Adding two such numbers
would violet the effectiveness property.
• Algorithms that are definite and effective are also called computational procedures.
Euclids algorithm
-gcd(m,n)=gcd(n,m mod n)
Euclids algorithm
Euclids algorithm
function of the input size. Similarly, the space complexity of an algorithm is defined as the
amount of computer memory that is required during the program execution as a function
● In general, the time taken by an algorithm grows with the size of the input, so it is
important to describe the running time of a program as a function of the size of its input.
To do so, need to define the terms "running time" and "size of input.
● Since, running time requirements are more critical than memory requirements. Therefore
basically the running time efficiency of algorithms is categorized as follows: (i) Worst-
● Worst-case time complexity: It is the behaviour of an algorithm with respect to the worst
possible input instance. It denotes the upper bound on the running time for any input.
estimate of the running time for an ‘average’ input. It specifies the expected behaviour of
the algorithm when the input is randomly drawn from a given distribution. Average-case
running time assumes that all inputs of a given size are equally likely.
algorithm under optimal conditions. It denotes the lower bound on the running time for any
input. Therefore, the algorithm will never go athis time limit. For example, the best case
list.
● Algorithm Analysis uses two approaches for resolving time complexity: Iterative approach
● Iterative Approach: If an algorithm contains loops, then the efficiency of that algorithm
may vary depending on the number of loops and the running time of each loop in the
algorithm.
● Recursive Approach: If an algorithm contains recursive call, then the efficiency of that
The time and space complexity is expressed using a function f(n) where n is the input size for a
● We want to predict the rate of growth of complexity as the input size of the problem
increases.
● There are multiple algorithms that find a solution to a given problem and we need to find
i=0
sum=0
while i < n
sum=sum + A[i]
n
i=i + 1
return sum
Order of Growth:
● It describes how the running time of an algorithm increase with size of the input. It is
estimated by taking into account the dominant terms of the running time expression. This
If “size of the input” is multiplied by “k”, then dominant term in t(n) is also
multiplied by “k”
T(kn) = k(an)+b;
or steps executed
● Asymptotic can be defined as a way to describe the behaviour of functions in the limit or
without bounds
● This model assumes, time taken for simple instructions such as addition, multiplication
takes exactly one time unit, thus makes convenient for describing running time
BIG O NOTATION:
● It provides asymptotically upper bound f(n) and gives the worst case complexity
0 ⩽ f(n) ⩽ c.g(n), for all large input n, n ⩾0. Therefore it can be said that g(n) is
Example:
Prove: f(n)= O(g(n)) (i.e f(n) ⩽ c.g(n)) where c>0 and n0⩾0
Proof:
3n+2 ⩽ c.g(n)
3n+2 ⩽4n
i.e n⩾2. So for every n⩾2 where c=4, f(n) ⩽ c.g(n))[∴ f(n)= O(g(n)) ]
● It provides asymptotically lowest bound f(n) and gives the best case complexity
● Let “f” be any non-negative function. Then f(n)= Ω(g(n)), if there exists certain constants
0 ⩽ c.g(n) ⩽f(n), for all large input n, n ⩾0. Therefore it can be said that g(n) is the
Example:
Prove: f(n)= Ω(g(n)) (i.e f(n) ⩾c.g(n)) where c>0 and n0⩾0
Proof:
3n+2 ⩾c.g(n)
3n+2 ⩾n
i.e n⩾1. So for every n⩾1 where c=1, f(n) ⩾ c.g(n))[∴ f(n)= Ω(g(n)) ]
● For any two functions f(n) and g(n) we have f(n)=Θg(n), if and only if
● Let “f” be any non-negative function. Then f(n)= Θ(g(n)), if there exists certain constants
0⩽ c1.g(n)⩽ f(n) ⩽ c2.g(n), for all large input n, n ⩾0. Therefore it can be said that
g(n) is asymptotically tight bound for f(n) and it gives the average case time complexity
Example:
Prove: f(n)= Θ(g(n)) (i.e c1.g(n)⩽f(n) ⩽ c2.g(n)) where c1,c2>0 for n⩾n0, n0⩾1
Proof:
As per the asymptotic upper and lower bound, proved f(n) can be
n⩽ 3n+2 ⩽ 4n
Mathematical analysis (Time Efficiency) of Non-recursive Algorithms General plan for analyzing
efficiency of non-recursive algorithms:
3. Check whether the number of times the basic operation is executed depends only on
the input size n. If it also depends on the type of input, investigate worst, average, and
best case efficiency separately.
4. Set up summation for C(n) reflecting the number of times the algorithm‘s basic
operation is executed.
5. Simplify summation using standard formulas
ALOGORITHM MaxElement(A[0..n-1])
currentMax ← A[i]
return currentMax
Analysis:
b) Assignment
//Output: Returns true if all the elements in A are distinct and false otherwise
for i 0 to n - 2 do
for j i + 1 to n – 1 do
if A[i] = = A[j]
return true
Analysis
4. Let C (n) denotes number of comparisons in worst case: Algorithm makes one
comparison for each repetition of the innermost loop i.e., for each value of the
loop‘s variable j between its limits i + 1 and n – 1; and this is repeated for each
value of the outer loop i.e, for each value of the loop‘s variable i between its limits
0 and n – 2
if n = = 0
return 1
else
return Factorial (n – 1) * n
Analysis:
= [M(n – 2) + 1 ] + 1 = M (n – 2) + 2
= [ M (n – 3) + 1 ] + 3 = M (n – 3) + 3
When i = n, we have
= M (n – n ) + n = M (0 ) + n Since M (0) = 0
=n
M (n) ∈ Θ (n)
else
Analysis:
f> 1
n/2 ┘) + 1
A (n) = A (└ k
Assume n = 2 (smoothness rule)
k k-1
A (2 ) = A (2 ) + 1 for k > 0; A (20) = 0
k k-1 k-2
Solving using “Backward substitution method”: A (2 ) = A (2 ) + 1 = [A (2 )+
k-i
= A (2 )+i
When i = k, we have
k-k 0 0
= Ak(2 ) + k = A (2 ) + k Since A (2 ) = 0
A (2 ) = k
k
Since n = 2 , HENCE k = log2 n
A (n) = log2 n
A (n) ∈ Θ ( log n)
MASTER’S THEOREM
The master method provides a "cookbook" method for solving recurrences of the form
1. If f(n) = Օ(n b )
log a-ε
for some constant ε > 0, then
T(n) = Θ (n log ba )
Note: In this case, f(n) is "equal" to n log ba, i.e. neither term dominates thus the extra
T(n) = Θ (f(n) )
In all three cases it is important to compute the recursive term run time and compare
it asymptotically to the combine term f(n) run time to determine which case holds.
T(n) = Θ (n2)
Examples
T(n) = 9(T(n/3)) + n
T(n) = Θ (n log ba ) = Θ (n 2)
Here, f(n) ≠ (n log ba) i.e to make it equal, need to add some constant ε to (n log
a
b )
e.g. choose ε = 0.2. Hence the equation might satisfy Case3 so checking regularity
⩽(¾) nlogn
⩽(¾) f(n)
Thus regularity holds by choosing c = 3/4 < 1. Therefore the equation satisfies Case
● Sorting
● Searching
● String Processing
● Graph Problems
● Combinatorial Problems
● Geometric Problems
● Numerical Problems
(A) SORTING
•Output: A reordering <a´1, a´2, …, a´n> of the input sequence such that a´1≤
a´2 ≤ … ≤ a´n.
● Why sorting?
•Help searching
● Sorting key
•A specially chosen piece of information used to guide sorting. E.g., sort student
records by names.
SelectionSort(A[0..n - 1])
for i=0 to n - 2 do
min=i
(B) SEARCHING
•Sequential search
•Binary search …
m �(i+j)/2;
else i � m+1;
Time: O(log n)
( C ) STRING PROCESSING
( D ) GRAPH PROBLEMS
●Informal definition
Modeling WWW
Communication networks
Project scheduling …
Shortest-path algorithms
Topological sorting
TOPICS TO BE COVERED :
BRUTE FORCE:
ALGORITHM
SelectionSort(A[0..n - 1])
for i=0 to n - 2 do
min=i
for j=i + 1 to n - 1 do
Seconditeration
Thus,
selection sort is a O(n2) algorithm on all inputs. The number of key swaps is
only O(n) or, more precisely, n-1. This property distinguishes selection sort
positively from many other sorting algorithms.
Compare adjacent elements of the list and exchange them if they are out of order.
Then repeat the process,by doing it repeatedly, and end up bubbling up the largest
element to the last position on the list
ALGORITHM
BubbleSort(A[0..n - 1])
for i=0 to n - 2 do
for j=0 to n - 2 - i do
Bubble sort is not very good for big set of input. However bubble sort is very
simple to code.
PROBLEM : Find a substring of the text that matches the pattern.( find i—the index
of the leftmost character of the first matching substring in the text—such that
Example:
T=bacbababababacab
P=ababaca
The algorithm shifts the pattern always after a single character comparison. In the
worst case, the algorithm may have to make all ‘m’ comparisons before shifting the
Rabin Karp
ALGORITHM
Two Components of KMP algorithm: The prefix function, Π for pattern encapsulates
knowledge about how the pattern matches against shifts of itself. This information
can be used to avoid useless shifts of the pattern ‘p’. In other words, this enables
avoiding backtracking on the string ‘S’.The KMP Matcher with string ‘S’, pattern
‘p’ and prefix function ‘Π’ as inputs, finds the occurrence of ‘p’ in ‘S’ and returns
the number of shifts of ‘p’ after which occurrence is found.
ALGORITHM
Method:
● The traveling salesman problem (TSP) has been intriguing researchers for the
last 150 years by its seemingly simple formulation, In layman’s terms, the
problem asks to find the shortest tour through a given set of n cities that visits
each city exactly once before returning to the city where it started.
Example
Tour Cost
a→b→c→d→a 2+3+7+5 = 17
a→b→d→c→a 2+4+7+8 = 21
a→c→b→d→a 8+3+4+5 = 20
a→c→d→b→a 8+7+4+2 = 21
a→d→b→c→a 5+4+3+8 = 20
Knapsack Problem
Example:
1 2 $20
3 10 $50
4 5 $10
{1} 2 $20
{2} 5 $30
{3} 10 $50
{4} 5 $10
{1,2} 7 $50
{1,3} 12 $70
{1,4} 7 $30
{2,3} 15 $80
{2,4} 10 $40
{3,4} 15 $60
{1,2,4} 12 $60
Assignment Problem
● There are n people who need to be assigned to execute n jobs, one person per
job. (That is, each person is assigned to exactly one job and each job is
assigned to exactly one person.)
● The cost if the ith person is assigned to the j th job is a known quantity C[i, j
] for each pair i, j = 1, 2, . . . , n.
The problem is to find an assignment with the minimum total cost.
Example:
Job 0 Job 1 Job 2 Job 3
Person 0 9 2 7 8
Person 1 6 4 3 7
Person 2 5 8 1 8
Person 3 7 6 9 4
9 2 7 8
C = 6 4 3 7
5 8 1 8
7 6 9 4
Definition:
Divide & conquer is a general algorithm design strategy with a general plan as
follows:
● For solving conceptually difficult problems like Tower Of Hanoi, divide &
conquer is a powerful tool
● Results in efficient algorithms
● Divide & Conquer algorithms are adapted for execution in multi-processor
machines
● Results in algorithms that use memory cache efficiently.
where:f(n) – a function that accounts for the time spent on dividing the problem
into smaller ones and on combining their solutions. Therefore, the order of growth
of T(n) depends on the values of the constants a & b and the order of growth of the
function f(n)
Master theorem
then,
Therefore:
Є =(nlog22 )
Є Θ (n)
It is dichotomic divide and conquer search algorithm. Ti inspects the middle element
of the sorted list. If equal to the sort value, then the position has been found.
Otherwise, if the key is less than the middle element, do a binary search on the first
half, else on the second half.
Algorithm:
l→0
while l ≤ r do
m→( l + r) / 2
if key = = A[m]
return m
Else
R→m-1
Else
l→m+1
return -1
Analysis:
• Depend on Best – key matched with mid element worst – key not found or
key sometimes in the list
C(1) = 1
• Solving the recurrence equation using master theorem, to give the number of
times the search key is compared with an element in the array, we have
C(n) = C(n/2) + 1 a = 1
b=2
f(n) = n0 ; d = 0
case 2 holds:
C(n) = Θ (ndlog n)
= Θ (n0log n)
= Θ ( log n)
Advantages:
• Due to random access of list element, needs arrays instead of linked list.
Definition: Merge sort is a sort algorithm that splits the items to be sorted
into two groups, recursively sorts each group, and merges them into a final sorted
sequence.
● This is a classic divide and conquer sorting algorithm that splits an array into
two pieces, sorts each piece (recursively!), then merges the results back together.
APPROACH
1. If n = 1, it is already sorted
2. If n > 1,
● Split array A[0..n-1] into about equal halves and make copies of
each half in arrays L and R
● Once all elements in one of the arrays are processed, copy the
remaining unprocessed elements from the other array into A.
Analysis:
Cmerge(1) = 0
• Solving the recurrence equation using master theorem: C(n) = 2C(n/2) + n-1
for n > 1
C(1) = 0
Here a = 2; b = 2
f(n) = n; d = 1
Therefore 2 = 21, case 2 holds
C(n) = Θ (ndlog n)
= Θ (n1log n)
= Θ (n log n)
Advantages:
Limitations:
2. Reorder the list so that all elements which are less than the pivot come
before the pivot and all elements greater than pivot come after it. After this
partitioning, the pivot is in its final position. This is called the partition
operation
Features:
● Efficient algorithm
● NOT stable sort
● Significantly faster in practice, than other algorithms
ALGORITHM
//i/p: A sub-array A[l..r] of A[0..n-1],defined by its left and right indices l and r
if l < r
Quicksort(A[l..s-1])
Quicksort(A[s+1..r]
//i/p: A sub-array A[l..r] of A[0..n-1], defined by its left and right indices l and r (l
< r)
//o/p: A partition of A[l..r], with the split position returned as this function‘s value
p→A[l]
i→l
j→r + 1;
Repeat
swap(A[i], a[j])
swap(A[l], A[j])
return j
5 3 1 9 8 2 4 7
2 3 1 4 5 8 9 7
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
Analysis:
C(1) = 1
Best case:
C(n) = Θ (n2)
= Θ (n1log n)
= Θ (n log n)
NOTE:
TOPICS TO BE COVERED :
GREEDY METHOD:
DYNAMIC PROGRAMMING:
KRUSKAL ALGORITHM
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed
so far. If cycle is not formed, include this edge. Else, discard it.
3. Repeat step-2 until there are (V-1) edges in the spanning tree.
The algorithm is a Greedy Algorithm. The Greedy Choice is to pick the smallest weight
edge that does not cause a cycle in the MST constructed so far. Let us understand it with
an example:
After sorting:
Weight Src Dest
1 h g
2 i c
2 g f
4 a b
4 c f
6 i g
7 c d
7 h i
8 a h
8 b c
9 d e
10 f e
11 b h
14 d f
The graph contains 9 vertices and 14 edges. So, the minimum spanning tree formed will
be having (9 – 1) = 8 edges.
Now pick all edges one by one from sorted list of edges
ANALYSIS :
● Let us assume a graph with e number of edges and n number of vertices. Kruskal’s
algorithm starts with sorting of edges.
● Time complexity of sorting algorithm= O (e log e)
● In Kruskal’s algorithm, we have to add an edge to the spanning tree, in each
iteration. This involves merging of two components.
● Time complexity of merging of components= O (e log n)
● Overall time complexity of the algorithm= O (e log e) + O (e log n)
● PRIM’s algorithm has the property that the edges in the set A always form
a single tree, i.e. at each step we have only one connected component.
● It starts with one starting vertex (say v) of a given graph G(V,E)Then, in
each iteration, a minimum weight edge (u, v) connecting a vertex v in the
set A to the vertices in the set is chosen . That is, it always finds an edge (u,
v) of minimum weight such that v∊ A and u∊ V-A. Then it modify the set A
by adding u i.e. This process is repeated until , i.e. until all the vertices are
not in the set A. Following pseudo-code is used to constructing a MCST,
using PRIM’s algorithm
PSEUDOCODE:
Initially empty and keys assigned to vertices are {0, INF, INF, INF, INF, INF, INF, INF} where
INF indicates infinite. Pick the vertex with minimum key value. The vertex A is picked, After
including to mstSet, update key values of adjacent vertices. Adjacent vertices of A are B and H.
The key values of B and H are updated as 4 and 8.
Pick the vertex with minimum key value and not Visited. The vertex B is picked and added to
mstSet. So mstSet now becomes {0, 1}. Update the key values of adjacent vertices of B. The key
value of vertex C becomes 8.
ANALYSIS:
Thus, the total time for Prim’s algorithm is O(V lg V+ E lgV ) = O(E lgV ), which is
asymptotically the same as of Kruskal’s algorithm.
HUFFMAN ENCODING:
● It finds the minimum length bit string which can be used to encode a string of symbols
based on the frequencies of corresponding characters. The most frequent character
gets the smallest code and the least frequent character gets the largest code
● In a fixed-length code each codeword has the same length. In a variable-length
code codewords may have different lengths.
EXAMPLE:
ANALYSIS:
As, for loop in lines 3–8 executes exactly n - 1 times, and since each heap
operation requires time O(lg n). The total running time of HUFFMAN on a set of n
characters is O(n lg n).
● Dijkstra, is a greedy algorithm that solves the single-source shortest path problem
for a directed graph G=(V,E) with non-negative edge weights.
● It generates a SPT (shortest path tree) with given source as root.
● It also maintains two sets, one set contains vertices included in shortest path tree,
other set includes vertices not yet included in shortest path tree. At every step of
the algorithm, it finds a vertex which is in the other set and has minimum distance
from source.
DIJKSTRA(G, w, s)
1 INITIALIZE-SINGLE-SOURCE(G, s)
2S←Ø
3 Q ← V[G]
4 while Q ≠ Ø
5 do u ← EXTRACT-MIN(Q)
6 S ← S ∪{u}
8 do RELAX(u, v, w)
● An example of Dijkstra’s
algorithm.
using adjacency list, it can be reduced to O(E log V) with the help of binary heap.
Dijkstra’s algorithm doesn’t work for graphs with negative weight edges.
KNAPSACK PROBLEM:
Example:
1 18 $25
2 15 $24
3 10 $15
Consider the jobs in the non increasing order of profits subject and maximum
deadline given = 3
Timeslot 1 2 3
Status
ii. Job 5 is added to J as it has the largest profit and thus J = {5} is a feasible one.
Timeslot 1 2 3
Status J5
iii. Now Job 4 is considered. The solution J = {4, 5} is a feasible one with
processing sequence (4, 5)
Timeslot 1 2 3
Status J4 J5
Timeslot 1 2 3
Status J4 J5 J1
Example: C (5, 2)
It should be noted that the above function computes the same sub problems again and
again. See the following recursion tree for n = 5 and k = 2.
The function C (3, 1) is called two times. For large values of n, there will be many
common sub problems.
/ \
C(4, 1) C(4, 2)
/ \ / \
/ \ / \ / \
/ \ / \ / \
Since same sub problems are called again, this problem has Overlapping Sub problems
property. So the Binomial Coefficient problem has both properties of a dynamic
programming problem and the Time Complexity: O (n*k)
Given a directed graph, find out if a vertex j is reachable from another vertex i for all
vertex pairs (i, j) in the given graph. Here reachable mean that there is a path from vertex
i to j. The reach-ability matrix is called transitive closure of a graph.
1111
1111
0001
On the kth iteration, the algorithm determine if a path exists between two vertices i, j
using just vertices among 1,…,k allowed as intermediate
PSUEDOCODE:
Recurrence Relation:
Example:
• Travel maps containing driving distance from one point to another – Represented by
tables – Shortest distance from point A to point B given by intersection of row and
column – Route may pass through other cities represented in the table
• Problem: Find the length of the shortest path between every pair of vertices – Length
of the path is strictly determined by the weight of its edges – It is not based on the
number of edges traversed
EXAMPLE:
a. Given Di-Graph
b. Weighted Matrix