of 120
7/31/2019 M1 Data Structures
1/120
Resmi N.G.Reference:
Data Structures and Algorithms:
Alfred V. Aho, John E. Hopcroft, Jeffrey D. Ullman
7/31/2019 M1 Data Structures
2/120
Syllabus Module I (11 hours)
Review of Data Types - Scalar Types - Primitive types - Enumerated types - Subranges - Arrays- sparse
matrices - representation - Records - Complexity of Algorithms - Time & Space Complexity of Algorithms -
Recursion: Recursive algorithms - Analysis of Recursive algorithms
Module II (18 hours)
Linear Data Structures - Stacks Queues -Lists - Dequeus - Linked List - singly, doubly linked and circular
lists - Application of linked lists - Polynomial Manipulation - Stack & Queue implementation using Array &
Linked List - Typical problems - Conversion of infix to postfix - Evaluation of postfix expression priority
queues
Module III (18 hours)
Non Linear Structures - Graphs - Trees - Graph and Tree implementation using array and Linked List -
Binary trees - Binary tree traversals - pre-order, in-order and postorder - Threaded binary trees Binary
Search trees - AVL trees - B trees and B+ trees - Graph traversals - DFS, BFS - shortest path - Dijkstras
algorithm, Minimum spanning tree - Kruskal Algorithm, Prims algorithm
Module IV (18 hours)
Searching - Sequential Search - Searching Arrays and Linked Lists - Binary Searching - Searching arrays
and Binary Search Trees - Hashing - Open & Closed Hashing - Hash functions - Resolution of Collision -
Sorting- n2 Sorts - Bubble Sort - Insertion Sort - Selection Sort - n log n Sorts - Quick Sort - Heap Sort -
Merge Sort - External Sort - Merge Files
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
3/120
References Text Books
Aho A.V, Hopcroft J.E. & Ullman J.D, Data Structures and
Algorithms, AddisonWesley
Reference Books
1. Sahni S, Data Structures, Algorithms and Applications in C++,
McGrawHill
2. Wirth N, Algorithms + Data Structures = Programs, Prentice
Hall.
3. Cormen T.H, Leiserson C.E & Rivest R.L, Introduction to
Algorithms in C++, Thomson Books.
4. Fundamentals of Computer Algorithms, Ellis Horowitz, S. Sahni
5. Deshpande P.S, Kakde O.G, C and Data Structures, Dream- tech
India Pvt. Ltd.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
4/120
Module 1 Introduction
Review of Data Types
Scalar types
Primitive types Enumerated types
Subrange types
Arrays - representation sparse matrices
Records - representation Sets - representation
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
5/120
Complexity of Algorithms
Time complexity
Space Complexity
Recursion:
Recursive algorithms
Analysis of Recursive algorithms
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
6/120
Problems to Programs
CS09 303 Data Structures - Module 1
Problem formulation and specification
Design of the solution
Implementation
Testing and documentation
Evaluation of the solution
7/31/2019 M1 Data Structures
7/120
CS09 303 Data Structures - Module 1
Identify the problem
Formal model (mathematical model informalalgorithm)
Check for existing programs for the given problem
Algorithm pseudo-language and stepwise refinement
Implementation
Problems to Programs
7/31/2019 M1 Data Structures
8/120
Algorithm Finite sequence of instructions
Clear meaning
Finite amount of effort
Finite length of time (never enters an infinite loop
on any input.)
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
9/120
Algorithm Specification Pascal Language
Pseudo Language
Constructs of a programming language +
Informal English statements
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
10/120
Problem Solving Process
CS09 303 Data Structures - Module 1
MathematicalModel
Informal
Algorithm
Abstract Data Type(ADT)
Pseudo languageProgram
(Step-wise refinement)
Data Structure
Pascal program
ADT : Mathematical model (together with various operationsdefined on the model).
Designing PhaseData Structure: Logical Model (to represent the mathematical modelunderlying an ADT)Implementation Phase
Both are models with collection of operations
7/31/2019 M1 Data Structures
11/120
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
12/120
The model defines an abstract view to the problem.
This implies that the model focuses only on problem and
tries to define properties of the problem.
These properties include:
the data which are affected and
the operations which are identified
by the problem.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
13/120
As an example, consider the administration of employees
in an institution.
What employee information is needed by the
administration?
What tasks should be allowed?
Employees are real persons who can be characterized with
many properties; a few are:
name, size, date of birth, social number, room number,hobbies.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
14/120
Only some of them are problem specific.
Consequently, you create a model of an employee for the
problem.
This model only implies properties which are needed to fulfill
the requirements of the administration, for instance, name, dateof birth and social number.
These properties are called the data of the (employee) model.
Now you have described real persons with help of an abstract
employee.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
15/120
There must be some operations defined with which the
administration is able to handle the abstract employees.
For example, there must be an operation which allows you
to create a new employee once a new person enters the
institution.
Also, you have to identify the operations which should be
able to be performed on an abstract employee.
You also decide to allow access to the employees' data
only with associated operations.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
16/120
Data Abstraction Abstraction is the structuring of a nebulous(not properly
defined) problem into well-defined entities by defining
their data and operations.
These entities combine data and operations.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
17/120
Properties of Abstract Data Types
With abstraction, you create a well-defined entity whichcan be properly handled.
These entities define the data structure of a set of items.
For example, each administered employee has a name,date of birth and social number.
The data structure can only be accessed with definedoperations.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
18/120
An entity with the properties just described is called an
abstract data type (ADT).
ADT consists of an abstract data structure and operations.
Only the operations are viewable from the outside.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
19/120
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
20/120
Once a new employee is ``created', the data structure is
filled with actual values;
You then have an instance of an abstract employee.
As many instances of an abstract employee as needed can
be created to describe every real employed person.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
21/120
An abstract data type (ADT) is characterized by thefollowing properties:
1. It exports a type.
2. It exports a set of operations. This set is called
interface.
3. Operations of the interface are the one and onlyaccess mechanism to the type's data structure.
4. Axioms and preconditions define the applicationdomain of the type.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
22/120
Data Structure Encapsulation The principle of hiding the used data structure and to only
provide a well-defined interface is known as
encapsulation.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
23/120
To define an ADT for complex numbers.
Complex numbers consists of two parts: real part and
imaginary part.
Both parts are represented by real numbers.
Complex numbers define several operations: addition,
subtraction, multiplication, division etc.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
24/120
To represent a complex number, it is necessary to define
the data structure to be used by its ADT.
Two possibilities:
Both parts are stored in a two-valued array where the
first value indicates the real part and the second value theimaginary part of the complex number.
If x denotes the real part and y the imaginary part, you
could think of accessing them via array subscription:x=c[0] and y=c[1].
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
25/120
Both parts are stored in a two-valued record. If theelement name of the real part is rand that of the imaginarypart is i, x and y can be obtained with: x=c.r and y=c.i.
ADT definition also says that for each access to the data
structure, there must be an operation defined.
The addition of two complex numbers requires you toperform an addition for each part.
Consequently, you must access the value of each partwhich is different for each version.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
26/120
By providing an operation ``add'' you can encapsulate
these details from its actual use.
In an application context you simply ``add two complex
numbers'' regardless of how this functionality is actuallyachieved.
Once you have created an ADT for complex numbers, say
Complex, it can be used in the same way as well-knowndata types such as integers.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
27/120
Abstract Data Type (ADT)
Mathematical model with collection of operationsdefined on that model
Generalization of primitive data types
Eg; Sets of integers together with operations of
union, intersection and set difference. Eg; LIST (of integers)
Make list empty
Get first member of list, return null if empty
Get next member of list, return null if empty Insert integer into list
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
28/120
Implementation of ADT is a translation intostatements of a programming language of thedeclaration that defines a variable to be that ADT,plus a procedure in that language for each operationof that ADT.
Implementation chooses a data structure torepresent the ADT.
Ie; Data structure is used to represent the
mathematical model underlying an ADT.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
29/120
ADT and DATA ABSTRACTION
CS09 303 Data Structures - Module 1
Encapsulation of operations and data (eg:ADT)
2 properties
Generalization
Encapsulation
Advantages
Changes done easily by revising a small section.
No concern of underlying implementation.
7/31/2019 M1 Data Structures
30/120
Data Types, Data Structures and
ADTs Data type determines the set of values which may be
assumed by a variable or expression.
Type of a value denoted by a variable or expression maybe derived from its declaration.
Each operator or function expects arguments of a fixed
type and yields result of a fixed type.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
31/120
DATA TYPES IN PASCAL Pascal supports two major types of data: Simple Vs. Structured
Within these types there are scalar and ordinal kinds of data.
Simple data types cannot be further broken down into anything finer.
Eg;
INTEGER, REAL, BOOLEAN and CHAR.
These four types of data are also called the Pascal standard data types.These four types are also scalar data, since the relational characters < > =
= are all valid between values of any data of these types.
INTEGER, BOOLEAN and CHAR are also ordinal data.
This means that every value of a given type (except the last) has a uniquevalue which comes after it (called its successor) and every value (except thefirst) has a unique value which comes before it (called its predecessor).
Data of type REAL however, is not ordinal, since finding the "next" valueafter a real number value is determined by the number of decimal digits thecomputer can store.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
32/120
Different Data Types Scalar
Primitive
Enumerated
Subranges
Arrays
Records
Sets
Files
Pointers and Cursors
Structure
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
33/120
Primitive Data types Machine dependent
boolean
char
Integer
real
CS09 303 Data Structures - Module 1
Variable declaration in Pascal
var result : realVariable result of type real is declared using keyword var.
7/31/2019 M1 Data Structures
34/120
BOOLEAN
Values: FALSE , TRUE Operations: NOT, AND, OR, assignment
Predefined functions: PRED, SUCC, ORD
Predefined procedures: WRITE, WRITELN
CHAR Values: space < '!' < ... < '0' < '1' < ... < '9' < ':' < ';' < '' < '?' < '@'
< 'A' < 'B' < ... < 'Z' < ... < 'a' < 'b' < ... < 'z
Operations: assignment, comparison with relational characters
Predefined functions: PRED, SUCC, ORD
Predefined procedures: READ, READLN, WRITE, WRITELN
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
35/120
INTEGER
Values: -32768, ..., -1, 0, 1, 2, 3, ... 32767 (= MAXINT) Operations: +, -, *, DIV, MOD, assignment, comparison with
relationals
Predefined functions: PRED, SUCC, ORD, ABS, SQR, SQRT, ODD,
CHR
Predefined procedures: READ, READLN, WRITE, WRITELN
REAL
Values: ratios of integers
Operations: +, -, *, /, assignment, comparison
Predefined functions: ABS, SQR, SQRT, ROUND, TRUNCPredefined procedures: READ, READLN, WRITE, WRITELN
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
36/120
CHRThe chr or character position function returns the character
associated with the ASCII value being asked. eg; chr( 65 ) will return the character A.
ORDThe ord or ordinal function returns the ASCII value of arequested character. In essence, it works backwards to the chrfunction.
Ordinal data types are those which have a predefined, knownset of values. Each value which follows in the set is one greaterthan the previous.
Characters and integers are thus ordinal data types. ord( 'C' ) will return the value 67.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
37/120
SUCC
The successor function determines the next value orsymbol in the set, thus succ( 'd' ) will return e.
PRED
The predecessor function determines the previous value
or symbol in the set, thus pred( 'd' ) will return c.
ord(false) returns 0
ord(true) returns 1
ord(21) returns 21
ord('A') returns 65
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
38/120
USER DEFINED TYPES In Pascal, a programmer can define his/her own constants
(in a const section) and similarly his/her own variables (in
a var section).
User defined data types are placed in a new section of the
program, the TYPE section. This section is found betweenCONST and VAR sections using the reserved word TYPE.
One way to declare a type is to use a subrange.
For example: const Last = 50;
typeValid_numbers = 1..Last;
var X, Y: Valid_numbers;
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
39/120
Enumerated data Type
CS09 303 Data Structures - Module 1
type = (const1,const2,);
eg:
type day = (Mon,Tues,,Sun);
type boolean = (true,false) ;
7/31/2019 M1 Data Structures
40/120
7/31/2019 M1 Data Structures
41/120
type
beverage = ( coffee, tea, cola, soda, milk, water );color = ( green, red, yellow, blue, black, white );var
drink : beverage;
chair : color;
drink := coffee;
chair := green;
ifchair =yellowthen drink := tea;
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
42/120
SUBRANGE TYPES Identifiers can be defined so that they have a restricted
range of values of a given ordinal type.
The syntax is:
Identifier: First value..Last value;
These constructs are called subrange types. Examples:
var
num: -10..19; { Num has integer values -10 to 19inclusive }
alphabet : 'A'..'Z; { Alphabet is char with values 'A'to 'Z' }
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
43/120
Subranges
Used as array bounds
CS09 303 Data Structures - Module 1
type name = ..
eg: type range = low..high
type digit = 0..99
7/31/2019 M1 Data Structures
44/120
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
45/120
Cell
CS09 303 Data Structures - Module 1
Cell
Basic building block of data structures
Capable of holding a value
Data StructureCreated by giving names to aggregates of cells.
7/31/2019 M1 Data Structures
46/120
Arrays
Homogeneous Random access structure
Index
CS09 303 Data Structures - Module 1
array[0..99] of type
var A :array[0..99] ofchar
name:array[index type] ofcelltype
Array is a sequence of cells of a given type
often referred to as celltype.Indextype can be an enumerated data type or subrange.
7/31/2019 M1 Data Structures
47/120
Records Collection of cells called fields
May be of dissimilar types Records can be grouped into arrays.
CS09 303 Data Structures - Module 1
record
:
:
.
.
:
end
7/31/2019 M1 Data Structures
48/120
Kinds Of Record Declaration
CS09 303 Data Structures - Module 1
Using Enumeration
eg;
type complex = record
re:real;
im:real;
end;
type complex = record
re,im:real;
end;
Using var & array combination
var reclist: array[1..4]ofrecord
data:real;
next:integer;end;
7/31/2019 M1 Data Structures
49/120
type
cardsuit = (clubs, diamonds, hearts, spades);
card = record
suit: cardsuit;
value: 1 .. 13;
end;var
hand: array[ 1 .. 13 ] ofcard;
trump: cardsuit;
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
50/120
Array Vs RecordArray
Homogeneous
Run Time
Record
Heterogeneous
Compile time
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
51/120
SETS
Ordered collection of elements + No repetition
Set brackets - individually or subranges
eg:[a..z,A..Z],[0..9],[mon..sun]
Operators defined on all set types - [+,-,*, in ]
CS09 303 Data Structures - Module 1
type T = set ofT0
Eg; type set1 = set of[1..3]
var A: set1;or
var A: set of[1..3];
7/31/2019 M1 Data Structures
52/120
Set Operations
=>Membership operator (in)
=>Bitwise operator (Union, difference, intersection)
=>Relational (=)
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
53/120
FILES
Sequence of values of some particular type
No index type, accessed in order
Number of elements in a file can be time-varying and
unlimited.
File operators: {rewrite, put, reset, get}
CS09 303 Data Structures - Module 1
type T = file ofT0
Eg; type file1 = file ofchar
var A: file1;
Or
var A: file ofchar;
7/31/2019 M1 Data Structures
54/120
POINTERS Cell whose value indicates another cell.
CS09 303 Data Structures - Module 1
var
ptr : cell type;
type
link = cell;cell = record
info: integer;
next: link
end;
7/31/2019 M1 Data Structures
55/120
Integer valued cell, used as pointer to anarray(used in Fortran)
CS09 303 Data Structures - Module 1
CURSOR
4 2 .
1
2
3
4type recordtype = record
cursor:integer;ptr: recordtype
end;
1.2 3
3.4 0
5.6 2
7.8 1
header
nextdata
reclist
7/31/2019 M1 Data Structures
56/120
Complexity of algorithm
CS09 303 Data Structures - Module 1
Time
Space
Understandability
More important is runtime
7/31/2019 M1 Data Structures
57/120
Measuring runtime of a
program(Factors)
CS09 303 Data Structures - Module 1
Input
Quality of code generated by the compiler
Nature and speed of the instructions on the machine
Time complexity of the algorithm
7/31/2019 M1 Data Structures
58/120
Running time of a program Depends on input
It is a function of size of the input.
Denoted by T (n) where n is the size of the input.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
59/120
Views of running time
CS09 303 Data Structures - Module 1
(1) Worst case: maximum inputs
(2) Average case: average number of inputs
(3) Best case: no risk considered
T(n) is measured not in seconds but by growth functions.
Generally worst case running time is calculated .
7/31/2019 M1 Data Structures
60/120
Big-Oh(Worst case)
CS09 303 Data Structures - Module 1
The time complexity of an algorithm quantifies the amount of time takenby an algorithm to run as a function of the size of the input to the problem.
The time complexity of an algorithm is commonly expressed using
Big - Oh notation.
Time complexity is commonly estimated by counting the number of
elementary operations performed by the algorithm
Big - Oh notation usually only provides an upper bound
on the growth rate of the function.
Asymptotic efficiency- how running time of an algorithm increases
with the size of input.
7/31/2019 M1 Data Structures
61/120
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
62/120
Definition of Big-Oh
CS09 303 Data Structures - Module 1
T(n) is O (n)
Means that there exist +ve constants c and n0 such that
for all n n0 , we have, T(n) c n
Find time complexity of T(n) = (n+1)
Find time complexity of T(n) = 3n+2n
O(g(n)) = f(n) : if there exist +ve constants c and n0 such that
0 f(n) c.g(n) , for all n n0
7/31/2019 M1 Data Structures
63/120
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
64/120
Rules for Big-Oh
CS09 303 Data Structures - Module 1
Rule for sum
T(n) of P = O(f(n))
T2(n) of P2 = O(g(n))
Therefore, running time of P1 followed by P2,
ie; T(n) +T2(n) = O(max(f(n),g(n)))
eg; O(n+n) = O(n)
Find running time of O(n),O(n),O(n log n)
Find O(max(f(n),g(n))), if
f(n)={n4, if n is even; n, if n is odd}
g(n)={n, if n is even; n, if n is odd}
7/31/2019 M1 Data Structures
65/120
7/31/2019 M1 Data Structures
66/120
Points to consider when analysing
an algorithm Count the total number of elementary operations in the
algorithm for any given input.
For algorithms with loops, count the maximum number ofiterations for any given input.
For algorithms with recursive function calls, count the
maximum number of recursive function calls on any giveninput.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
67/120
Big O notation will always assume the upper limit where
the algorithm will perform the maximum number of
operations or iterations.
O(1) describes an algorithm that will always execute in
the same time (or space) regardless of the size of the
input data set. O(1) operations run in constant time.
O(N) describes an algorithm whose performance will
grow linearly and in direct proportion to the size of the
input data set. O(n) operations run in linear time.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
68/120
O(N2) represents an algorithm whose performance is
directly proportional to the square of the size of the inputdata set. This is common with algorithms that involvenested iterations over the data set.
Deeper nested iterations will result in O(N3), O(N4) etc.
O(log n) - Any algorithm which cuts the problem in halfeach time is O(log n). O(log n) operations run inlogarithmic time.
O(n log n) Performs O(log n) operation for each item inyour input. O(n log n) operations run in loglinear time.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
69/120
O(2^n) - means that the time taken will double with each
additional element in the input data set. O(2^n) operationsrun in exponential time.
O(n!) - involves doing something for all possible
permutations of the n elements. O(n!) operations run in
factorial time.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
70/120
Elementary operations:
Arithmetic operations
Assignment operation
Testing a condition
Read operation
Write operation
These operations taken independently has complexity O(1).
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
71/120
1. Sequence of statements which is executed only once.
Constant time O(1)
Statement1;
Statement2;
:
:
Statement k;
Total time taken = time(Statement1) + time(Statement2) + +
time(Statement k)
Time for each statement is a constant.
Therefore, total time is a constant and hence T(n) is O(1).
This has complexity O(1) as it does not depend on the number of
statements in the sequence.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
72/120
Example:
Algorithm to check if a number is even or odd.
if n%2 = 0 then ------O(1)
writeln(number is even); ------O(1)
else
writeln(number is odd); ------O(1)
T(n) is O(1).
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
73/120
2. For loop
Linear time O(n)
for i:=1 to n do begin
sequence1;
end
To find time complexity of an algorithm with loops, count the
number of times the loop executes.
Worst case: loop executes n times.
Here, each of the statements in the sequence is O(1).
Total no. of iterations = n Therefore, total time taken = n*O(1) = O(n).
Hence, T(n) is O(n).
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
74/120
Example: Linear Search or Sequential Search
Algorithm to search for a number in an array.
Best case: number is found at first position.
Worst case: number is found at the last position or it is
not present in the array( entire array is searched).
for i:=1 to n do begin
if num = arr[i] then ---O(1) n times
writeln(number found); ---O(1)
end
T(n) is n*O(1) = O(n).
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
75/120
3. If-then-else statements
if condition then
sequence1;
else
sequence2;
Either sequence1 or sequence2 will execute.
Worst case time is the slowest of the 2 possibilities.
T(n) = O(max[time(sequence1), time(sequence2)])
Suppose time(sequence1) = O(1) and time(sequence2) =O(n), then T(n) = O(n).
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
76/120
Example:
Algorithm to print first n numbers if choice = 1 else print
wrong choice. Best case: choice not equal to 1.
Worst case: choice = 1. All n numbers are printed.
if choice = 1 then
for i:=1 to n do begin
if num = arr[i] then ---O(1) n times
writeln(number found); ---O(1)
end
else
writeln(Wrong choice!); ---O(1) T(n) is max(O(n), O(1)) = O(n).
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
77/120
4. Nested for loop
a)Quadratic time O(n2)
for i:=1 to m do begin
for j:=1 to n do begin
sequence1;
end
end
To find time complexity of an algorithm with loops, count the
total number of iterations.
Worst case: Inner loop executes n times and outer loopexecutes m times.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
78/120
Here, each of the statements in the sequence is O(1).
For each iteration of outer loop, no. of iterations of inner loop = n.
Therefore, T(inner loop) = n*O(1) = O(n).
No. of iterations of outer loop = m. Therefore, total no. of iterations ofO(1) sequence = m*n.
Or
There are m repetitions ofO(n) sequence.
Hence, T(n) = m* O(n) = O(mn)
Hence, T(n) is O(n2) when m = n.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
79/120
Example: Displaying a matrix
for i:=1 to n do begin
for j:=1 to n do begin
writeln(arr[i,j]) ; ---O(1) n times n times
end
end
T(n) is n*(n*O(1))) = O(n2).
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
80/120
4. Nested for loop
b) Cubic time O(n3)
Matrix multiplication
for i:=1 to n do begin
for j:=1 to n do begin
c[i,j] := 0;
for k:=1 to n do
c[i,j] := c[i,j] + a[i,k]*b[k,j];
end
To find time complexity of an algorithm with loops, count the
total number of iterations.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
81/120
Here, c[i,j] := c[i,j] + a[i,k]*b[k,j]; is O(1).
The innermost loop executes n times. ie; There are n repetitions of
O(1) sequence. Therefore, T(innermost loop) = n*O(1) = O(n).
No. of iterations of loop with index j = n.
Therefore, total no. of iterations ofO(1) sequence = n*n = n2.
Or
There are n repetitions ofO(n) sequence.
Outer loop executes n times.
Therefore, total no. of iterations of O(1) sequence = n2 *n = n3.
Or
There are n repetitions ofO(n2) sequence.
Hence, T(n) is O(n3). The algorithm takes Cubic time.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
82/120
5. Nested loop where inner loop index depends on outer
loop index.
for i:=1 to m do begin
forj:= i+1 to n do
sequence1;
CS09 303 Data Structures - Module 1
Value of i No. of iterations of inner loop
1 n-12 n-2
: :
n-2 2
n-1 1
7/31/2019 M1 Data Structures
83/120
Total number of iterations = 1+ 2 + + n-2 + n-1
= (n-1)n/2 = (n2/2) - n/2
= O(n2)
Hence, T(n) is O(n2). The algorithm takesQuadratic time.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
84/120
Some more examples 2 loops one after the other
for i:=1 to n do
sequence1;
for j:=1 to m do
sequence2;
Complexity of first loop is O(n) and second is O(m).
By sum rule, runtime of the whole sequence is
T(n) is O(m+n) = O(max(m,n)).
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
85/120
Nested loop followed by non-nested loop
for i:=1 to n do for j:= 1 to n do
sequence1;
for k:=1 to n do
sequence2;
Complexity of first loop is O(n2) and second is O(n).
By sum rule, runtime of the whole sequence is
T(n) is O(max(n2,n)) = O(n2).
CS09 303 Data Structures - Module 1
A l i f b bbl t l ith
7/31/2019 M1 Data Structures
86/120
CS09 303 Data Structures - Module 1
Analysis of bubble sort algorithm
7/31/2019 M1 Data Structures
87/120
Analysis of bubble sort algorithm
Assignment statement takes some constant amount of
time, independent of input size .
ie, (4),(5),(6) each take O(1) time.
Testing condition requires O(1) time. Thus if groupstatements (3) (6) takes O(1) time.
For loop lines (2) (6), the running time is the sum over
each iteration of the loop, of the time spent executing the
loop body for that iteration.
no. of iteration of the loop = n-i
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
88/120
Thus, Time taken by loop body for each iteration is -
O(1).
Time spent in the loop of line (2) - (6) = O((n-i)* 1)
= O(n-i).
Statement (1) is executed (n-1) times.
So the total running time of the program is bounded
above by some constant times.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
89/120
n-1 (n-i)
i=1 = (n-1)(n-2)(n-3)(1)
Sum of n natural numbers = n(n-1)/2- So Here, T(n) = O(n2 ).
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
90/120
Most Familiar Big-Oh notations (Orders of growth inincreasing order)
(1) Constant time O(1)
(2) Logarithmic Time O(logn)
(3) Linear time O(n)
(4) n log n O(nlogn)(5) Quadratic time O(n2)
(6) Cubic time O(n3)
(7) Polynomial time O(nk)
(8) Exponential Time O(k n)
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
91/120
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
92/120
Recursion Recursive Algorithms
Procedure repeatedly calling itself
eg: Factorial Procedure
CS09 303 Data Structures - Module 1
function fact (n : integer):integer;
{fact(n) computes n!}
begin(1) if n
7/31/2019 M1 Data Structures
93/120
Analysis of Factorial Procedure
CS09 303 Data Structures - Module 1
function fact(n : integer):integer;
{fact(n) computes n!}begin
(1) if n
7/31/2019 M1 Data Structures
94/120
Computing total running time of factorial procedure
T(n) = c+ T(n-1) if n>1
(1)= d if n
7/31/2019 M1 Data Structures
95/120
Substitute (4) in (3)
Thus, T(n) = 3c + T(n-3) if n>3 .(5)
Or, For n>k,
T(n) = k.c + T(n-k)
Finally, when k = n-1,T(n) = c(n-1) + T(1)
= c(n-1) + d
= cn c + d
Therefore ,T(n) is O(n).
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
96/120
The Towers of Hanoi
A B C
Goal: Move stack of rings to another peg
Rule 1: Can move only 1 ring at a time
Rule 2: Can never have a larger ring on top of a smallerring
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
97/120
Solution
Move top (n-1) disks from A to B.
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
98/120
The Towers of Hanoi Move larger ring from A to C.
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
99/120
7/31/2019 M1 Data Structures
100/120
The Towers of Hanoi
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
101/120
The Towers of Hanoi
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
102/120
The Towers of Hanoi
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
103/120
The Towers of Hanoi
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
104/120
The Towers of Hanoi
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
105/120
The Towers of Hanoi
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
106/120
The Towers of Hanoi
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
107/120
7/31/2019 M1 Data Structures
108/120
The Towers of Hanoi
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
109/120
The Towers of Hanoi
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
110/120
The Towers of Hanoi
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
111/120
The Towers of Hanoi
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
112/120
The Towers of Hanoi
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
113/120
The Towers of Hanoi
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
114/120
7/31/2019 M1 Data Structures
115/120
The Towers of Hanoi
CS09 303 Data Structures - Module 1
A B C
7/31/2019 M1 Data Structures
116/120
Towers of Hanoi - Complexity
For 1 rings we have 1 operations.
For 2 rings we have 3 operations.
For 3 rings we have 7 operations.
For 4 rings we have 15 operations.
In general, the cost is 2N 1 = O ( 2N).
Each time we increment N, we double the amount of work. This grows incredibly fast!
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
117/120
Deriving Recurrence Relation
function THanoi(n, A, B, C):
THanoi (n-1, A, C, B); //A to B using C
Move larger disc from A to C; THanoi (n-1, B, A, C); //B to C using A
T(n) = T (n-1) + O(1) + T(n-1)
= 2T(n-1) + O(1)
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
118/120
T(n) = 2T(n-1) + O(1) = 2[2T(n-2) + O(1)] + O(1)
= 2[2 [2T(n-3) + O(1)] + O(1)] + O(1)
=23 T(n-3) + 22 O(1) + 2O(1) + O(1)
: = 2n T(0) + (2n -1)O(1)
Therefore, T(n) is O(2n).
The algorithm takes exponential time.
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
119/120
Additional Points
Space complexity
Recursive algorithm(Fibonacci)
Steps in recursion
Recursion Vs Iteration
Disadvantages of recursion
CS09 303 Data Structures - Module 1
7/31/2019 M1 Data Structures
120/120
Thank You