0% found this document useful (0 votes)
6 views64 pages

Module - 1 - PART-A

Uploaded by

u2209008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views64 pages

Module - 1 - PART-A

Uploaded by

u2209008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 64

Data Structures & Algorithms

101009/IT200C (Module 1)
S2 CU

Mr. Tinku Soman Jacob


Asst. Prof IT
RSET

101009/IT200C _S2 CU_Module-1 DIT, RSET 1


Index
• Basic Concepts of Data Structures
• Algorithm Specification
• Recursion
• Performance Analysis
• Asymptotic Notation
• Space Complexity
• Time Complexity
• Programming Style
• Refinement of coding
• Data Abstraction
101009/IT200C _S2 CU_Module-1 DIT, RSET 2
Data Structures
• Data Structure – is the study of the organization of data in the main memory and their
processing.

• The logical or mathematical model to organize the data in the computer memory (main
or secondary) and the method to process them are collectively called Data Structures.

• Methods are the algorithms.

• Data structure: organization or structuring of data which can be used to effectively solve
a problem.

101009/IT200C _S2 CU_Module-1 DIT, RSET 3


Data Structures
• Data Structure are the basic building blocks of the programming.
• Algorithm + DS = Program
• Data Structure is the collection of data objects together with the set of
operations that can be legally applied to elements of that data object.
• Operations
1. Creation
2. Insertion
3. Traversal
4. Searching
5. Sorting
6. Deletion
7. Updation
8. Merging
101009/IT200C _S2 CU_Module-1 DIT, RSET 4
Data Structures - Types

101009/IT200C _S2 CU_Module-1 DIT, RSET 5


Data Structures - Types

101009/IT200C _S2 CU_Module-1 DIT, RSET 6


Common Terms/Stepwise refinement
technique
• Algorithm

• Pseudo-code – mixture of high level programming construct and natural


language that primitive operation can be identified quickly analyze the
efficiency of algorithm- part English and part structured code

• Flowchart

• Program

101009/IT200C _S2 CU_Module-1 DIT, RSET 7


Algorithm
• Definition – An algorithm is a finite set of instructions that, if followed,
accomplishes a particular task.
• Algorithm must specify the following criteria
1. Input – zero or more quantity that 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 finite number of steps.
5. Effectiveness- each instruction must be basic enough to be carried out, in
principles, by a person using only pencil and paper. It is not enough that each
operation be defined as in (3); it also must be feasible.
101009/IT200C _S2 CU_Module-1 DIT, RSET 8
Algorithm Example (Selection Sort)
• Suppose we must devise a program that sorts a set of n >=1 integers.

• A simple solution is - From those integers that are currently unsorted, find
the smallest and place it next in the sorted list.

• it is not an algorithm since it leaves several unanswered questions.

• For example, it does not tell us where and how the integers are initially
stored, or where we should place the result.

• We assume that the integers are stored in an array, list, such that the ith
integer is stored in the ith position, list [i ], 0 <= i < n.
101009/IT200C _S2 CU_Module-1 DIT, RSET 9
Algorithm Example (Selection Sort)

0 1 2 3 4 5 6 7
12 5 87 25 9 65 98 34
5 12 87 25 9 65 98 34
5 9 87 25 12 65 98 34
5 9 12 25 87 65 98 34
5 9 12 25 87 65 98 34
5 9 12 25 34 65 98 87
5 9 12 25 34 65 98 87
5 9 12 25 34 65 87 98

101009/IT200C _S2 CU_Module-1 DIT, RSET 10


Algorithm Example (Selection Sort)
• Selection sort algorithm

• Two clearly defined subtasks remain: finding the smallest integer and
interchanging it with list [i]

101009/IT200C _S2 CU_Module-1 DIT, RSET 11


Algorithm Example (Selection Sort)
void selection(int a[],int n)
{
int i,pass,min,tmp;
for(pass=0;pass<n-1;pass++)
{
min=pass;
for(i=pass+1;i<n;i++)
if(a[i]<a[min])
min=i;
if(min!=pass)
{
tmp=a[pass];
a[pass]=a[min];
a[min]=tmp;
}
}
print(a,n);
} 101009/IT200C _S2 CU_Module-1 DIT, RSET 12
Recursive algorithms
• Programmers view function as something that is invoked (called) by
another function. It executes its code and then return control to the
calling function.

101009/IT200C _S2 CU_Module-1 DIT, RSET 13


Recursive algorithms
• Direct recursion – function that can calls themselves

• Indirect recursion- function that call another function that invoke the
calling function again

101009/IT200C _S2 CU_Module-1 DIT, RSET 14


Recursive algorithms
• Algorithm for calculating factorial value of a number

Step 1: a number n is inputted

Step 2: variable final is set as 1

Step 3: final<= final * n

Step 4: decrease n

Step 5: verify if n is equal to 0

Step 6: if n is equal to zero, goto step 8 (break out of loop)

Step 7: else goto step 3

Step 8: the result final is printed


101009/IT200C _S2 CU_Module-1 DIT, RSET 15
Recursive algorithms
• Recursive algorithm is defined as a method of simplification that
divides the problem into sub-problems of the same nature. The result
of one recursion is treated as the input for the next recursion. The
repletion is in the self-similar fashion manner.
int factorialA(int n)
{
return n * factorialA(n-1);

101009/IT200C _S2 CU_Module-1 DIT, RSET 16


Basic Terminologies

• Key – this is the element to be searched.


• Item – same as key. Its is an element under search.
• Table – collection of all records is called table. column in a table
called field. A special field by which each record can be uniquely
identified I called key field.
• Successful – a search will be termed as successful if the key is found
in the domain of search.
• Unsuccessful – when the entire domain of search is exhausted and the
key is not available then the search will be termed unsuccessful or a
failure.

101009/IT200C _S2 CU_Module-1 DIT, RSET 17


Linear Vs Binary Search

No. Binary Search Linear Search


1 Works on sorted list only Works on sorted as well as unsorted list

2 Highly efficient if items are in sorted form Highly efficient if items are fewer & present at the
starting of the list
3 Fast access Slow access

4 Useful in 1-D arrays only Useful in 1-D as well as multi-dimensional arrays

5 Not suitable for linked list Appropriate for both array and linked list

6 No of comparison is less Large no of comparison is necessary, if items are


present at the last part of the list.

101009/IT200C _S2 CU_Module-1 DIT, RSET 18


Linear Vs Binary Search

34 7 23 56 32 87

7 23 32 34 56 87

101009/IT200C _S2 CU_Module-1 DIT, RSET 19


Binary Search
• In Binary search algorithm, the target key is examined in a sorted
sequence and this algorithm starts searching with the middle item of
the sorted sequence.
a. If the middle item is the target value, then the search item is
found and it returns True.
b. If the target key < middle item, then search for the target value
in the first half of the list.
c. If the target key > middle item, then search for the target value
in the second half of the list.
• In binary search as the list is ordered, so we can eliminate half of the
values in the list in each iteration

101009/IT200C _S2 CU_Module-1 DIT, RSET 20


Binary Search (Recursive)
Binary (a[ ],lo,hi,key)
{
int mid,lo=0,hi=n-1;
if(lo<=hi)
{
mid=(lo+hi)/2
if(a[mid]=key)
print element found
else if(a[mid]<key) Binary(a,mid+1,hi,key);
else Binary(a,lo,mid-1,key);
}
return;
}
101009/IT200C _S2 CU_Module-1 DIT, RSET 21
Binary Search (Non-Recursive)
Binary (a[],lo,hi,key)
{
int mid,lo=0,hi=n-1;
while(lo<=hi)
{
mid=(lo+hi)/2
if(a[mid]=key)
print element found; break;
else if(a[mid]<key) lo=mid+1;
else hi=mid-1;
}
return;
}
101009/IT200C _S2 CU_Module-1 DIT, RSET 22
Binary Search
• In Binary Search, each comparison eliminates about half of the items
from the list.
• Consider a list with n items, then about n/2 items will be eliminated
after first comparison. After second comparison, n/4 items of the list
will be eliminated. If this process is repeated for several times, then
there will be just one item left in the list.
• The number of comparisons required to reach to this point is n/2i = 1.
If we solve for i, then it gives us i = log2n.
• The maximum number is comparison is logarithmic in nature, hence
the time complexity of binary search is O(log n).

101009/IT200C _S2 CU_Module-1 DIT, RSET 23


Performance Analysis
• Main criteria for judging a program includes
1. Does the program meet the original specifications of the task?

2. Does it work correctly?

3. Does the program contain documentation that shows how to use it and how it
works?

4. Does the program effectively use functions to create logical units?

5. Is the program’s code readable?

Performance Analysis-

6. Does the program effectively use primary and secondary storage?

7. Is the program’s running time acceptable for the task?


101009/IT200C _S2 CU_Module-1 DIT, RSET 24
Performance Analysis
• Performance evaluation

• Performance analysis (Analytical method)

• Heart of complexity theory (a branch of computer science)

• Focus on estimating the time and space which are machine independent.

• Performance measurement (Experimental method)

• Obtains machine dependent running time

• These running times are used to identify inefficient code segments.

101009/IT200C _S2 CU_Module-1 DIT, RSET 25


Performance Evaluation
• Performance evaluation can be loosely divided into two major phases:
(i) a prior estimates (performance analysis)
(ii) a Posterior testing(performance measurement)
• A Priori Analysis − This is a theoretical analysis of an algorithm. Efficiency of an
algorithm is measured by assuming that all other factors, for example, processor
speed, are constant and have no effect on the implementation.
• A Posterior Analysis − This is an empirical analysis of an algorithm. The selected
algorithm is implemented using programming language. This is then executed on
target computer machine. In this analysis, actual statistics like running time and
space required, are collected.

101009/IT200C _S2 CU_Module-1 DIT, RSET 26


Performance Evaluation
A priori analysis A Posteriori analysis
Priori analysis is an absolute analysis. Posteriori analysis is a relative analysis.
It is independent of language of compiler and types of It is dependent on language of compiler and type of
hardware. hardware
It will give approximate answer. It will give exact answer.
It uses the asymptotic notations to represent how much It doesn’t use asymptotic notations to represent the
time the algorithm will take in order to complete its time complexity of an algorithm.
execution.
The time complexity of an algorithm using a priori The time complexity of an algorithm using a posteriori
analysis is same for every system. analysis differ from system to system
If the algorithm running faster, credit goes to the If the time taken by the program is less, then the credit
programmer. will go to compiler and hardware.
It is done before execution of an algorithm. It is done after execution of an algorithm.
It is cheaper than Posteriori Analysis. It is costlier than priori analysis because of
requirement of software and hardware for execution.
Maintenance Phase is not required to tune the Maintenance Phase is required to tune the algorithm.
algorithm.
27
101009/IT200C _S2 CU_Module-1 DIT, RSET
Performance Analysis - Definitions
• Space complexity of a program is the amount of memory that it needs to run to
completion.

• Time complexity of a program is the amount of computer time that it needs to run
to completions.

101009/IT200C _S2 CU_Module-1 DIT, RSET 28


Space Complexity
• Space needed by a program is the sum of following components.
1. Fixed space requirements- refers to space requirements that do not depend on the number
and size of the program’s input and output.
• Includes space for
• Instructions, simple variables, fixed size structured variables (struct) and constants

2. Variable space requirements- consists of space needed by structured variables whose size
depends on the particular instance, I, of the problem being solved.
• Includes additional space required when a function uses recursion.

• Sp(I) – function of some characteristics of the instance I.

• Characteristics – number, size and values of the inputs and outputs associated with I.
101009/IT200C _S2 CU_Module-1 DIT, RSET 29
Space Complexity
• Eg: if our input is an array containing n numbers then n is an instance
characteristics.

• Total space complexity S(P) = c + Sp(I), c is the constant representing the fixed
space requirements.

• When analyzing space complexity of a program, we concerned with only the


variable space requirements.

101009/IT200C _S2 CU_Module-1 DIT, RSET 30


Space Complexity - Examples

101009/IT200C _S2 CU_Module-1 DIT, RSET 31


Space Complexity - Examples

101009/IT200C _S2 CU_Module-1 DIT, RSET 32


Time Complexity
• The time, T(P), taken by a program, P, is the sum of its compile time and its run
time(execution time)

• Compile time is similar to fixed space component since it does not depend on the
instance characteristics.

• Determining Tp is not an easy task because we must know how the compiler translates
our source program into object code.

• Running time – use system clock to time the program or count the number of
operations the program performs, which is machine independent, but we must know
how101009/IT200C
to divide the program into distinct
_S2 CU_Module-1 DIT, RSET steps. 33
Time Complexity
• A program step is a syntactically or semantically meaningful program segment
whose execution time is independent of the instance characteristics.

• The time required to execute each statement that is counted as one step be
independent of the instance characteristics.

• Eg: a=2; count =1

• a= 2*b+3*c/d-e+g/g/a/b/c; count =1

101009/IT200C _S2 CU_Module-1 DIT, RSET 34


Time Complexity
• The unit time is calculated as follows
• For comment- 0
• Declarative statements-0
• Expression – 1
• Assignment -1
• Iterations- depends
• Switch-1 for head, for case it depends
• If- depends
• Function invocation – 1
• Memory management statement – 1
• Function statement -0
• Jump statements – 1

101009/IT200C _S2 CU_Module-1 DIT, RSET 35


Time Complexity - Example

101009/IT200C _S2 CU_Module-1 DIT, RSET 36


Time Complexity - Example

101009/IT200C _S2 CU_Module-1 DIT, RSET 37


Time Complexity - Example

101009/IT200C _S2 CU_Module-1 DIT, RSET 38


Time Complexity - Example

101009/IT200C _S2 CU_Module-1 DIT, RSET 39


Time Complexity - Example

101009/IT200C _S2 CU_Module-1 DIT, RSET 40


Time Complexity - Example

101009/IT200C _S2 CU_Module-1 DIT, RSET 41


Time Complexity - Example

If n=0, Step count = 2

101009/IT200C _S2 CU_Module-1 DIT, RSET 42


Time Complexity - Example

101009/IT200C _S2 CU_Module-1 DIT, RSET 43


Time Complexity - Example

101009/IT200C _S2 CU_Module-1 DIT, RSET 44


Task – 1

101009/IT200C _S2 CU_Module-1 DIT, RSET 45


Task – 2

101009/IT200C _S2 CU_Module-1 DIT, RSET 46


Asymptotic Notation (O,Ω,θ)

• Terminology that enable to make meaningful statements about the


time and space complexities of a program.

• Big ‘oh’- O : works as upper bound of the algorithm/worst case

• Omega - Ω : works as lower bound of the algorithm/Best case

• Theta – θ : works as average bound of the algorithm/Average case

• 1<logn<√n<n<nlogn<n2<n3….<2n<3n..<nn
101009/IT200C _S2 CU_Module-1 DIT, RSET 47
Big ‘oh’ (O)

• Definition - f(n) = O(g(n)) iff (if and only if) there exit positive constants c and n0

such that f(n) <= c. g(n) for all n, n>=n0

• Eg: 3n + 2 = O(n) as 3n + 2 <=4n for all n>=2

• Eg: 3n+3 = O(n) as 3n+ 3 <=4n for all n>=3

• Eg: 100n+6 = O(n) as 100n+6<=101n for all n>=10

• Eg: 10n2+4n+2 =O(n2) as 10n2+4n+2 <=11n2 for n>=5

• If f(n) is a polynomial of degree k then f(n) = O(nk)


101009/IT200C _S2 CU_Module-1 DIT, RSET 48
Big ‘oh’ (O) - Example

• Eg: 3n + 2 = O(n) as 3n + 2 <=4n for all n>=2

n=0, 3x0+2 <=4x0 X

n=1, 3x1+2 <=4x1 X

n=2, 3x2+2 <=4x2 ƴ

n=3, 3x3+2 <=4x3 ƴ

101009/IT200C _S2 CU_Module-1 DIT, RSET 49


Big ‘oh’ (O) - Example

• O(1) – constant computing time

• O(n) – linear time complexity

• O(n2) – quadratic time complexity

• O(n3) – cubic time complexity

• O(2n) – exponential time complexity

• O(log n) – logarithmic time complexity


101009/IT200C _S2 CU_Module-1 DIT, RSET 50
Omega (Ω)

• Definition - f(n) = Ω(g(n)) iff (if and only if) there exit positive
constants c and n0 such that f(n) >= c. g(n) for all n, n>=n0

• Eg: 3n + 2 = Ω(n) as 3n + 2 >=3n for all n>=1

• Eg: 3n+3 = Ω(n) as 3n+ 3 >=3n for all n>=1

• Eg: 100n+6 = Ω(n) as 100n+6>=100n for all n>=1

• Eg: 10n2+4n+2 = Ω(n2) as 10n2+4n+2 >=10n2 for n>=1


101009/IT200C _S2 CU_Module-1 DIT, RSET 51
Theta (θ)
• Definition - f(n) = θ(g(n)) iff (if and only if) there exit positive
constants c1, c2 and n0 such that c1. g(n) <= f(n) <= c2. g(n) for all n,

n>=n0

• Eg: 3n + 2 = θ(n) as 3n + 2 >=3n for all n>=2 and 3n+2<=4n for all
n>=2

• Its more precise than O & Ω

101009/IT200C _S2 CU_Module-1 DIT, RSET 52


Time Complexity

101009/IT200C _S2 CU_Module-1 DIT, RSET 53


Examples
sum = 0 -------------------------1
for (i=0;i<n;i++) ----------------n O(n)
sum ++ ---------1

(n-1 -0 + 1) = n => O(n)

--------------------------------------------------------------------------------------
sum = 0 -------------------------1
for (i=0;i<n;i++) ----------------n O(n2)
for (j=0;j<n;j++) ----------------n
sum ++ ---------1
= = n => O(n2)
_S2 CU_Module-1
101009/IT200C DIT, RSET 54
Examples
sum = 0 -------------------------1
for (i=0;i<n;i++) ----------------n O(n3)
for (j=0;j<n;j++) ----------------n
for (k=0;k<n;k++) ----------------n
sum ++ ---------1

= n = n2 => O(n3)

101009/IT200C _S2 CU_Module-1 DIT, RSET 55


Examples
sum = 0 -------------------------1
for (i=0;i<n;i++) ----------------n O(n3)
for (j=0;j<n*n;j++) ----------------n 2
sum ++ ---------1

= n2 => O(n3)

101009/IT200C _S2 CU_Module-1 DIT, RSET 56


Examples

void function(int n) O(n3)


{
for(int i = 0; i < n*n; i++) ------------n2
for (int j = 0; j < n; j++) -------------n
“something taking O(1)
time”-----1
}
101009/IT200C _S2 CU_Module-1 DIT, RSET 57
Examples

void function(int n) O(n3)


{
for(int i = 0; i < n*n; i++) ------------n2
for (int j = 0; j < n/2; j++) -------------n
“something taking O(1)
time”-----1
}
101009/IT200C _S2 CU_Module-1 DIT, RSET 58
Examples

void function(int n) O(n logn)


{
for(int i = 0; i < n; i++) ------------n
for (int j = 1; j < n; j*=2) -------------
logn
“something taking O(1)
time”-----1
A loop running through i = 1, 2, 4, ..., n runs O(log n) times!
}
101009/IT200C _S2 CU_Module-1 DIT, RSET 59
Data Abstraction
• Data Type : A data type is a collection of objects and a set of
operations that act on those objects.
• Abstract Data Type: An abstract data type (ADT) is a data type that is
organized in such a way that the specification of the objects and the
specification of the operations on the objects is separated from the
representation of the objects and the implementation of the
operations.

101009/IT200C _S2 CU_Module-1 DIT, RSET 60


How does specification of the operations of an ADT differ
from the implementation of the operations?

• Specification: function names, types of arguments and result, description of what


the function does, not implementation
• ADT is implementation independent
• Functions of a data type can be classified into three:
• Creator/constructor: creates a new instance of the designated type
• Transformers: creates a new instance of the designated type using one or more other
instances.
• Observers/reporters: provide information about an instance of the type, but do not change
the instance

ADT definition includes at leastDIT, one


_S2 CU_Module-1
101009/IT200C RSET function from each of these categories.
61
101009/IT200C _S2 CU_Module-1 DIT, RSET 62
101009/IT200C _S2 CU_Module-1 DIT, RSET 63
Components of an ADT
• Name of the ADT
• Objects
• no reference to representation
• Functions
• Symbols x and y – data type natural number
• True and false- Boolean
• Uses functions that are defined on the set of integers, plus, minus
• In order to define one data type, operations of other data type may be required.
• ::=  is defined as

101009/IT200C _S2 CU_Module-1 DIT, RSET 64

You might also like