Data Structures
Unit - 1
Fundamentals of Algorithms
Algorithm
Analysis of Algorithm
Best Case and Worst Case Complexities
Analysis of Algorithm using Data Structures
Performance Analysis
Time, Space, Amortized Time Complexities
-- Asymptotic Notation
Algorithm
Definition 1 :
An algorithm is any well defined computational procedure that takes some value or set of values as input
and produces some value or set of values as output
Sequence of computational steps to transform input to output
Tool for solving a well defined computational problem
Specifies the computational procedure for achieving the ip / op relationship
Definition 2:
An algorithm is a sequence of unambigous instructions for solving a problem ie for a required op
for any legitimate ip in a finite amount of time
Example – Ascending Order
Problem : Sort a sequence of numbers into non-decreasing order
Input : A sequence of numbers (a1,a2….an)
Output: A permutation( reordered ) sequence of numbers (a1’,a2’,…a’n )
( 3,2,1) reordered as (1,2,3) – Sorting Alg.
Such an ip sequence is called an instance of the sorting algorithm
Instance
An instance of a problem/algorithm consists of the ip needed to compute a solution to a problem
After the design of an algorithm its efficiency is calculated through
cpu time needed
memory required – Space
Memory Space and Running Time
How much memory is required by an algorithm for execution – Memory Space
The computational time of an algorithm is running time
Correctness
An algorithm is said to correct if for every ip instance, it halts with the correct op
An incorrect algorithm might not halt al all or some ip instances or might halt with wrong op
An algorithm can be specified in English as a computer program or even as hardware design
Applications
Storing and retrieving
DNA pattern storing – Database
Internet Usage
E-commerce
Cryptography
Data Structure
A data structure is a way to store and organize data in order to facilitate access and modifications.
No single DS works well for all purposes – Types required
Problem Types
Hard problems – no efficient solution known
NP-Complete problems – Subset of hard problems – no efficient solution but not proved
Analysis of Algorithms
Analysis based on resources such as
Memory
Communication band width
Hardware
Best algorithm will be selected from several algorithms
Technology , resource model ,cost
Important factors while analyzing are
Time Efficiency – how fast an algorithm runs
Space Efficiency – memory space needed.
Measuring An Input’s size
Large input – long run time
Eg Matrices – order ,no of multiplications
Selection of parameters
Sorting,searching – list size
Polynomial – degrees or coefficients
Units for Measuring Running Time
Standard measures are – second,millisecond…
Depends on hardware
Depends on quality of program,compiler
Internal metric – no of times each operation in the algorithm executed
Depends on most important operation of the algorithm called basic operation, the operation
contributing the most of the total running time.
No. of times basic operation is executed.
Usually innermost loop of an algorithm
Eg key comparision searching,sorting
Matrix – Multiplication ,Addition
Calculation
Cop – execution time of basic operation
C(n)- times of execution of basic operation
Running time T(n )=Cop.C(n)
Order of Growth
Measured using
logarithmic function – log2 n , nlog2 n
Exponential function- 2n,n!
Linear function- n, n2,n3
Best Case , Worst Case and Average Case Complexities
Space complexity – storing instructions and data and time complexity – running time
Three types of time complexities
worst case time complexity Worst case efficiency
best case time complexity or Best case efficiency
average case time complexity or Average case efficiency
Best case efficiency - minimum running time for input size n – C best (n)
Worst case efficiency – maximum running time for input size n – Cworst(n)
Average case efficiency – By the above measures we can not get correct information about algorithm eg
for typical or random ip
The time required to run a typical ip data of size n – not considered as a good measure
Eg Sequential search algorithm
search key k
Algorithm: seq search(A[0 .. n-1], k)
// input : array of unordered elements and search key k
// output: returns the index(position) of the element in A or -1 if not present
i←1
While i<n and a[i]≠ k do
i←i–1
If i<n return I
else return -1
Basic operation is key comparison
Worst case complexity -Cworst(n) = n
Best case complexity -C best (n)= 1
Average case complexity Cavg(n)
needs some assumptions that uses probability
Probability of a successful search p(0<=p<=1)
For a successful search the probability of a match occurring in i th position is p/n
Unsuccessful search it is (1-p)
Cavg(n )= p/n[1+2..+i..+n] +n(1-p) = p(n+1)/2 + n(1-p)
Asymptotic Notation
• Q, O, W, o, w
• Used to describe the running times of algorithms
• Instead of exact running time, say Q(n2)
• Defined for functions whose domain is the set of natural numbers, N
• Determine sets of functions, in practice used to compare two functions
Q-notation
We say g(n) is an
asymptotically tight bound
for f(n)
O-notation
We say g(n) is an asymptotic upper bound f
W-notation
We say g(n) is an
asymptotic lower bound
for f(n)