SlideShare a Scribd company logo
1 of 52
Download to read offline
Data Structures And Algorithms
Text Books
• Suggested Textbooks:
1. Fundamentals of Data Structures, E.Horowitz and
S.Sahni,1977
2. Data Structures and Algorithms, alfred V.Aho, John
E.Hooperoft, Jeffrey D.Ullman
COURSE OBJECTIVES
1. To impart the basic concepts of data structures and algorithms.
2. To introduce various searching and sorting techniques.
3. To demonstrate operations of linear and non-linear data structure.
4. To develop an application using suitable data structure.
COURSE OUTCOMES
After completion of the course, the student will be able to
1. Understand the specifications for writing algorithms and analyze its
performance.
2. Analyze basic concepts of data structures, computation complexity.
3. Understand linear data structures, various sorting, searching
techniques.
4. Apply various operations on linear and non-linear data structures.
5. Identify appropriate and efficient data structure to implement a
given problem.
UNIT-1
Basic Terminologies & Introduction to algorithm and Data
Organization:
• Algorithm Specification
• Recursion
• Performance Analysis
• Asymptotic Notation
• The Big-O, Omega and Theta notation
• Programming style
• Refinement of coding-time-space trade off
• Testing and Data abstraction
What is an algorithm?
An algorithm is a sequence of unambiguous instructions
for solving a problem, i.e., for obtaining a required
output for any legitimate input in a finite amount of
time.
“computer”
problem
algorithm
input output
Algorithmic solution : a way to teach the computer
Notion of algorithm
An Algorithm must satisfy the following criteria
(features).
1) Input
2) Output
3) Definiteness
4) Finiteness
5) Effectiveness
1)Input- Zero or more quantities 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 a finite number of steps.
5)Effectiveness- Every step of algorithm should be
feasible.
Basic Issues Related to Algorithms
• How to design algorithms
– Understanding the problem (instances, ranges)
– Ascertaining the capabilities of a computational device
(RAM and parallel machines, speed and amount of memory)
– Choosing between exact and approximate algorithms
– Deciding the correct data structure
– Algorithm design techniques
• How to express algorithms (Specification)
– Methods of specifying an algorithm (free language, pseudo
code, flowchart)
Basic Issues Related to Algorithms
• Proving correctness (Verification)
– Algorithm’s Proof
• Analyzing an algorithm (Analysis)
Efficiency, theoretical and empirical analysis, optimality
• Coding an algorithm (Implementation)
Algorithm Design Techniques/Strategies
 Greedy Approach
 Dynamic Programming
 Back Tracking
 Branch and Bound
 Brute Force Technique
 Divide and Conquer
New Microsoft Word Document.docx
Pseudo code for expressing Algorithms
Algorithm is broadly divided into 2 sections.
1. Algorithm heading- It consists of name of algorithm,
problem description, input and output.
Ex:- Algorithm name(v1,v2,……,vn)
In heading section we should write the following things:-
//Problem Description:
//Input:
//Output:
2. Algorithm body- It consists of logical body of the
program by making use of various programming constructs
and assignment statements.
• The Body of an algorithm is written, in which various
programming constructs like if, for, while and some
assignment statements.
• The compound statements should be enclosed with in {
and } brackets.
• Single line comments are written using // as beginning of
comment.
• The Identifier should begin by letter and not by digit. An
identifier can be a combination of alphanumeric string.
• Assignment operator 
Ex:- Variable  Expression
• There are other types of operators like Boolean(true , false),
Logical(and , or , not) and Relational(<,<=,>,>=,=).
• Array indices [ , ] initial index is 0.
• Inputting and outputting can be done using read and write.
Ex:- write(“WELCOME”);
read(val);
• The conditional statements if-then and if-then-else.
If(condition) then statement
If(condition) then statement else statement
We can use brackets also { and }.
• While statement
While(condition) do
{ Statement 1;
Statement 2;
….…
Statement n;
}
• For statement
For variable  value1 to valuen do (step i)
{
Statement 1
Statement 2
……..
Statement n
}
• Repeat-Until statement
repeat
Statement 1
Statement 2
……..
Statement n
until(condition)
• Break statement is used to exit from inner loop.
• Return statement is used to return control from one
point to another.
Examples:-
1. Write an algorithm to find sum of n numbers.
2. Write an algorithm to check the given number is
even or odd.
3. Write an algorithm to find factorial of a number.
4. Write an algorithm to sort the given list of
elements.
Algorithm sum()
// Prob Desc: This alg is used to find sum of n numbers
// input: n value
// output: sum of n numbers
{
Write(“enter n”);
Read(n);
i0;
Repeat
{
sumsum+I;
ii+1;
}
Until(i<=n)
Write(“sum of n numbers is”);
Write(sum);
}
Performance Analysis
• The efficiency of an algorithm can be decided by
measuring the performance of an algorithm. we can
measure the performance of an algorithm by
computing two factors.
1. Amount of TIME required by an algorithm to
execute. (Time Complexity)
2. Amount of STORAGE required by an algorithm.
(Space Complexity)
Space Complexity
• The Space Complexity can be defined as amount of memory
required by an algorithm to run.
• To compute Space Complexity we use two factors:-
• Constant and Instance Characteristics.
• The space requirement S(p) can be given as:
S(p) = C + Sp
Where C is constant i.e fixed part it denotes the space of inputs
and outputs.
This space is an amount of space taken by instruction, variables
and identifiers.
• Sp is a space depends upon instance characteristics.
• This is a variable part whose space requirement depends on
particular problem instance.
Ex:- Addition of two numbers.
S(p) = C Ө(Sp) = 0
Ex:- Addition of array elements.
S(p) >= (n+3)
Ex:- Addition of array elements.(Recursive)
S(p) >= 3(n+1)
The internal stack used for recursion includes space for formal
parameters, local variables and return address.
Time Complexity
• The time complexity of an algorithm is the amount of
computer time required by an algorithm to run to completion.
• Executing time depends on many factors:-
 System load
 Number of other programs running
 Instruction set used
 Speed of underlying hardware
• Therefore the time complexity is in terms of frequency count.
• Frequency count is a count denoting number of times of
execution of a statement.
• Measuring an input size
• Measuring running time
T(n)= C(n) Cop
- Where T(n) is running time of basic operation
- Cop is Time taken by the basic operation to execute
- C(n) is number of times the operation needs to be
executed
• Order of Growth:- Measuring the performance of an
algorithm in relation with the input size n is called Order
of Growth.
• Example the order of growth for varying input
size of n
logn n nlogn n*n POW(2,n)
0 1 0 1 2
1 2 2 4 4
2 4 8 16 16
3 8 24 64 256
4 16 64 256 65,536
5 32 160 1024 4,294,967,296
Asymptotic Notations
• To choose the best algorithm, we need to check efficiency of
each algorithm.
• The efficiency can be measured by computing time
complexity of each algorithm.
• Asymptotic notation is a shorthand way to represent the time
complexity.
• Using asymptotic notations we can give time complexity as
“Fastest possible”, “Slowest Possible” or “Average Time”.
• Various notations such as ,ϴ and  used are called
Asymptotic notations.
1.Big oh Notation
• The Big oh notation is denoted by “”.
• It is a method of representing the upper bound of algorithms
running time.
• Using Big oh notation we can give longest amount of time taken by
the algorithm to complete.
• Definition:- f(n)=O(g(n))
Let f(n) and g(n) be two non negative functions.
Let n0 and constant c are two integers such that no denotes some
value of input and n>=n0
similarly c is some constant such that c>0
we can write F(n)<=c*g(n) for all n>=n0
• Then F(n) is Big oh of g(n). It is also denoted as F(n) Є
O(g(n)). In other words F(n) is <=g(n) is multiple of
some constant c.
• Ex:- Consider the function F(n)=2n+2 and g(n)=n2
then we have to find some constant c so that
f(n)<=c*g(n).
if n=1 then f(n) > g(n)
if n=2 then f(n) > g(n)
if n=3 then f(n) < g(n)
• Thus always upper bound of existing time is obtained
by Big oh notation.
2.Omega Notation
• Omega notation is denoted by “”.
• This notation is used to represent the Lower Bound of
algorithm’s running time.
• Using Omega notation we can denote shortest amount of
time taken by algorithm.
Definition:-
• A function f(n) is said to be in (g(n)) if f(n) is bounded below
by some positive constant multiple of g(n) such that
• F(n) > = c*g(n) for all n>=n0
• It is denoted as f(n) Є (g(n))
• Ex:- Consider f(n)=2n2+5 and g(n)=7n
n=0 ,1,2 and 3
3.Theta Notation
• The theta notation is denoted by “ϴ”.
• By this method the running time is between upper bound and
lower bound.
• Definition:-
• Let f(n) and g(n) be two non negative functions. There are two
positive constants namely C1 and C2 such that
C1*g(n)<=f(n)<=C2*g(n) for all n, n>=n0
f(n) Є ϴ (g(n))
Ex:- f(n)=2n+8 and g(n)=7n where n>=2
Similarly f(n)=2n+8 and g(n)=7n
i.e 5n < 2n+8 < 7n for n>=2
Performance Analysis
1. O(1): Time complexity of a function (or set of statements) is
considered as O(1) if it doesn’t contain loop, recursion and
call to any other non-constant time function.
2. O(n): Time Complexity of a loop is considered as O(n) if the
loop variables is incremented / decremented by a constant
amount.
3. O(Logn):Time Complexity of a loop is considered as O(Logn) if
the loop variables is divided / multiplied by a constant
amount.
4) O(nc): Time complexity of nested loops is equal to the
number of times the innermost statement is executed.
For example the algorithm addition of two matrices
contains two loops then we have O(n2) time complexity.
5) O(LogLogn): Time Complexity of a loop is considered as
O(LogLogn) if the loop variables is reduced / increased
exponentially by a constant amount.
Analysis of Algorithms
• We can have three cases to analyze an algorithm:
1) Worst Case
2) Average Case
3) Best Case
Ex:- Linear Search
int search(int arr[], int n, int x)
{
int i;
for (i=0; i<n; i++)
{
if (arr[i] == x)
return i;
}
return -1;
}
• If we use the ϴ notation for time complexity then
• Worst Case is ϴ(n).
• Average Case is
∑(i=1 to n+1)(ϴ(i))
-----------------------
n+1
• i.e Θ(n)
• Best Case is ϴ(1)
Binary Search
• Worst Case O(n) for an unsorted array and O(logn)
for sorted array.
• Best Case is O(1) (if the key element is equal to the
first element of the given array).
• Average Case O(logn)
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l)/2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid-1, x);
return binarySearch(arr, mid+1, r, x);
}
return -1;
}
Big oh Analysis of Algorithms
Time Complexities of all Sorting Algorithms
Best Average Worst
Selection Sort Ω(n^2) θ(n^2) O(n^2)
Bubble Sort Ω(n) θ(n^2) O(n^2)
Insertion Sort Ω(n) θ(n^2) O(n^2)
Heap Sort Ω(n log(n)) θ(n log(n)) O(n log(n))
Quick Sort Ω(n log(n)) θ(n log(n)) O(n^2)
Merge Sort Ω(n log(n)) θ(n log(n)) O(n log(n))
• Programming Style
1. Clarity and simplicity of Expression: The programs should be
designed in such a manner so that the objectives of the program is
clear.
2. Naming: In a program, you are required to name the module,
processes, and variable, and so on. Care should be taken that the
naming style should not be cryptic and non-representative.
For Example: a = 3.14 * r * r
area_of_circle = 3.14 * radius * radius;
• Programming Style
3. Spaces: Style related to white space is commonly used to enhance
readability. For instance, compare the following syntactically
equivalent examples of C code:
int i;
for(i=0;i<10;++i){
printf("%d",i*i+i);
}
versus
int i;
for (i = 0; i < 10; ++i) {
printf("%d", i * i + i);
}
• Programming Style
4. Tabs: tabs work fine provided they are used consistently, restricted to
logical indentation, and not used for alignment.
int ix;
long sum;
5. Control Constructs: It is desirable that as much as a possible single entry
and single exit constructs used.
6. Indentation: Indentation styles assist in identifying control flow and
blocks of code. In some programming languages, indentation is used to
delimit logical blocks of code; correct indentation in these cases is more
than a matter of style. In other languages, indentation and white space do
not affect function, although logical and consistent indentation makes code
more readable.
7. Nesting: Deep nesting of loops and conditions greatly harm the
static and dynamic behaviour of a program. It also becomes difficult
to understand the program logic, so it is desirable to avoid deep
nesting.
8. Module size: The module size should be uniform. The size of the
module should not be too big or too small.
Refinement of coding-time-Space trade off
Testing:-
• Technically Testing is a process to check if the application is
working same as it was supposed to do, and not working as it was
not supposed to do.
• Main objective of Testing is to find bugs and errors in an
application which get missed during the unit testing by the
developer.
• A unit is the smallest testable part of any software. It usually has
one or a few inputs and usually a single output.
• A unit may be an individual program, function, procedure, etc
• Benefits:
• Unit testing increases confidence in changing / maintaining code.
If good unit tests are written and if they are run every time any
code is changed, we will be able to promptly catch any defects
introduced due to the change. Also, if codes are already made less
interdependent to make unit testing possible, the unintended
impact of changes to any code is less.
• Codes are more reusable. In order to make unit testing possible,
codes need to be modular. This means that codes are easier to
reuse.
• Debugging is easy. When a test fails, only the latest changes need
to be debugged.
• Data abstraction
• Data Abstraction is the property by virtue of which only the
essential details are displayed to the user.
• The trivial or the non-essentials units are not displayed to the user.
Ex: A car is viewed as a car rather than its individual components.
• Data Abstraction may also be defined as the process of identifying
only the required characteristics of an object ignoring the
irrelevant details.
• The properties and behaviors of an object differentiate it from
other objects of similar type and also help in classifying/grouping
the objects.
• Consider a real-life example of a man driving a car.
• The man only knows that pressing the accelerators will increase
the speed of car or applying brakes will stop the car, but he does
not know about how on pressing the accelerator the speed is
actually increasing, he does not know about the inner mechanism
of the car or the implementation of the accelerator, brakes, etc in
the car. This is what abstraction is.
• Advantages of Abstraction
• It reduces the complexity of viewing the things.
• Avoids code duplication and increases reusability.
• Helps to increase security of an application or program as only
important details are provided to the user.

More Related Content

What's hot

System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit IIManoj Patil
 
Computer instructions
Computer instructionsComputer instructions
Computer instructionsAnuj Modi
 
8085 micro processor- notes
8085 micro  processor- notes8085 micro  processor- notes
8085 micro processor- notesDr.YNM
 
Basic computer engineering
Basic computer engineeringBasic computer engineering
Basic computer engineeringsayruchi
 
Asynchronous data transfer
Asynchronous data transferAsynchronous data transfer
Asynchronous data transferpriya Nithya
 
Basic ops concept of comp
Basic ops  concept of compBasic ops  concept of comp
Basic ops concept of compgaurav jain
 
System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSARASWATHI S
 
THE GENERATION OF COMPUTER
THE GENERATION OF COMPUTERTHE GENERATION OF COMPUTER
THE GENERATION OF COMPUTERp.j. pranavan
 
introduction to microprocessor and microcomputer
introduction to microprocessor and microcomputerintroduction to microprocessor and microcomputer
introduction to microprocessor and microcomputerSatya P. Joshi
 
Register Reference Instructions | Computer Science
Register Reference Instructions | Computer ScienceRegister Reference Instructions | Computer Science
Register Reference Instructions | Computer ScienceTransweb Global Inc
 
security and ethical challenges in information systems
security and ethical challenges in information systemssecurity and ethical challenges in information systems
security and ethical challenges in information systemshilal12
 
Introduction to loaders
Introduction to loadersIntroduction to loaders
Introduction to loadersTech_MX
 
Computer architecture pipelining
Computer architecture pipeliningComputer architecture pipelining
Computer architecture pipeliningMazin Alwaaly
 
RISC and CISC Processors
RISC and CISC ProcessorsRISC and CISC Processors
RISC and CISC ProcessorsAdeel Rasheed
 

What's hot (20)

System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit II
 
Design of a two pass assembler
Design of a two pass assemblerDesign of a two pass assembler
Design of a two pass assembler
 
Computer instructions
Computer instructionsComputer instructions
Computer instructions
 
8085 micro processor- notes
8085 micro  processor- notes8085 micro  processor- notes
8085 micro processor- notes
 
Basic computer engineering
Basic computer engineeringBasic computer engineering
Basic computer engineering
 
Asynchronous data transfer
Asynchronous data transferAsynchronous data transfer
Asynchronous data transfer
 
Unit 3 sp assembler
Unit 3 sp assemblerUnit 3 sp assembler
Unit 3 sp assembler
 
First pass of assembler
First pass of assemblerFirst pass of assembler
First pass of assembler
 
Basic ops concept of comp
Basic ops  concept of compBasic ops  concept of comp
Basic ops concept of comp
 
System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro calls
 
THE GENERATION OF COMPUTER
THE GENERATION OF COMPUTERTHE GENERATION OF COMPUTER
THE GENERATION OF COMPUTER
 
introduction to microprocessor and microcomputer
introduction to microprocessor and microcomputerintroduction to microprocessor and microcomputer
introduction to microprocessor and microcomputer
 
Microprocessor lab manual
Microprocessor lab manualMicroprocessor lab manual
Microprocessor lab manual
 
Register Reference Instructions | Computer Science
Register Reference Instructions | Computer ScienceRegister Reference Instructions | Computer Science
Register Reference Instructions | Computer Science
 
Lecture 3 instruction set
Lecture 3  instruction setLecture 3  instruction set
Lecture 3 instruction set
 
security and ethical challenges in information systems
security and ethical challenges in information systemssecurity and ethical challenges in information systems
security and ethical challenges in information systems
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Introduction to loaders
Introduction to loadersIntroduction to loaders
Introduction to loaders
 
Computer architecture pipelining
Computer architecture pipeliningComputer architecture pipelining
Computer architecture pipelining
 
RISC and CISC Processors
RISC and CISC ProcessorsRISC and CISC Processors
RISC and CISC Processors
 

Similar to DSA

2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptxRahikAhmed1
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptxjinkhatima
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity CalculationAkhil Kaushik
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptxNishaS88
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptxrajesshs31r
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxrajesshs31r
 
Algorithm in Computer, Sorting and Notations
Algorithm in Computer, Sorting  and NotationsAlgorithm in Computer, Sorting  and Notations
Algorithm in Computer, Sorting and NotationsAbid Kohistani
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoreambikavenkatesh2
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...AntareepMajumder
 
Design and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit oneDesign and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit onessuserb7c8b8
 
C++ Notes PPT.ppt
C++ Notes PPT.pptC++ Notes PPT.ppt
C++ Notes PPT.pptAlpha474815
 

Similar to DSA (20)

2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
 
Unit ii algorithm
Unit   ii algorithmUnit   ii algorithm
Unit ii algorithm
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptx
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
Algorithm in Computer, Sorting and Notations
Algorithm in Computer, Sorting  and NotationsAlgorithm in Computer, Sorting  and Notations
Algorithm in Computer, Sorting and Notations
 
Algorithms.pdf
Algorithms.pdfAlgorithms.pdf
Algorithms.pdf
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Design and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit oneDesign and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit one
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
C++ Notes PPT.ppt
C++ Notes PPT.pptC++ Notes PPT.ppt
C++ Notes PPT.ppt
 

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

DSA

  • 1. Data Structures And Algorithms
  • 2. Text Books • Suggested Textbooks: 1. Fundamentals of Data Structures, E.Horowitz and S.Sahni,1977 2. Data Structures and Algorithms, alfred V.Aho, John E.Hooperoft, Jeffrey D.Ullman
  • 3. COURSE OBJECTIVES 1. To impart the basic concepts of data structures and algorithms. 2. To introduce various searching and sorting techniques. 3. To demonstrate operations of linear and non-linear data structure. 4. To develop an application using suitable data structure.
  • 4. COURSE OUTCOMES After completion of the course, the student will be able to 1. Understand the specifications for writing algorithms and analyze its performance. 2. Analyze basic concepts of data structures, computation complexity. 3. Understand linear data structures, various sorting, searching techniques. 4. Apply various operations on linear and non-linear data structures. 5. Identify appropriate and efficient data structure to implement a given problem.
  • 5. UNIT-1 Basic Terminologies & Introduction to algorithm and Data Organization: • Algorithm Specification • Recursion • Performance Analysis • Asymptotic Notation • The Big-O, Omega and Theta notation • Programming style • Refinement of coding-time-space trade off • Testing and Data abstraction
  • 6. What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time. “computer” problem algorithm input output Algorithmic solution : a way to teach the computer Notion of algorithm
  • 7. An Algorithm must satisfy the following criteria (features). 1) Input 2) Output 3) Definiteness 4) Finiteness 5) Effectiveness
  • 8. 1)Input- Zero or more quantities 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 a finite number of steps. 5)Effectiveness- Every step of algorithm should be feasible.
  • 9. Basic Issues Related to Algorithms • How to design algorithms – Understanding the problem (instances, ranges) – Ascertaining the capabilities of a computational device (RAM and parallel machines, speed and amount of memory) – Choosing between exact and approximate algorithms – Deciding the correct data structure – Algorithm design techniques • How to express algorithms (Specification) – Methods of specifying an algorithm (free language, pseudo code, flowchart)
  • 10. Basic Issues Related to Algorithms • Proving correctness (Verification) – Algorithm’s Proof • Analyzing an algorithm (Analysis) Efficiency, theoretical and empirical analysis, optimality • Coding an algorithm (Implementation)
  • 11. Algorithm Design Techniques/Strategies  Greedy Approach  Dynamic Programming  Back Tracking  Branch and Bound  Brute Force Technique  Divide and Conquer New Microsoft Word Document.docx
  • 12. Pseudo code for expressing Algorithms Algorithm is broadly divided into 2 sections. 1. Algorithm heading- It consists of name of algorithm, problem description, input and output. Ex:- Algorithm name(v1,v2,……,vn) In heading section we should write the following things:- //Problem Description: //Input: //Output:
  • 13. 2. Algorithm body- It consists of logical body of the program by making use of various programming constructs and assignment statements. • The Body of an algorithm is written, in which various programming constructs like if, for, while and some assignment statements. • The compound statements should be enclosed with in { and } brackets. • Single line comments are written using // as beginning of comment.
  • 14. • The Identifier should begin by letter and not by digit. An identifier can be a combination of alphanumeric string. • Assignment operator  Ex:- Variable  Expression • There are other types of operators like Boolean(true , false), Logical(and , or , not) and Relational(<,<=,>,>=,=). • Array indices [ , ] initial index is 0. • Inputting and outputting can be done using read and write. Ex:- write(“WELCOME”); read(val);
  • 15. • The conditional statements if-then and if-then-else. If(condition) then statement If(condition) then statement else statement We can use brackets also { and }. • While statement While(condition) do { Statement 1; Statement 2; ….… Statement n; }
  • 16. • For statement For variable  value1 to valuen do (step i) { Statement 1 Statement 2 …….. Statement n } • Repeat-Until statement repeat Statement 1 Statement 2 …….. Statement n until(condition)
  • 17. • Break statement is used to exit from inner loop. • Return statement is used to return control from one point to another. Examples:- 1. Write an algorithm to find sum of n numbers. 2. Write an algorithm to check the given number is even or odd. 3. Write an algorithm to find factorial of a number. 4. Write an algorithm to sort the given list of elements.
  • 18. Algorithm sum() // Prob Desc: This alg is used to find sum of n numbers // input: n value // output: sum of n numbers { Write(“enter n”); Read(n); i0; Repeat { sumsum+I; ii+1; } Until(i<=n) Write(“sum of n numbers is”); Write(sum); }
  • 19. Performance Analysis • The efficiency of an algorithm can be decided by measuring the performance of an algorithm. we can measure the performance of an algorithm by computing two factors. 1. Amount of TIME required by an algorithm to execute. (Time Complexity) 2. Amount of STORAGE required by an algorithm. (Space Complexity)
  • 20. Space Complexity • The Space Complexity can be defined as amount of memory required by an algorithm to run. • To compute Space Complexity we use two factors:- • Constant and Instance Characteristics. • The space requirement S(p) can be given as: S(p) = C + Sp Where C is constant i.e fixed part it denotes the space of inputs and outputs. This space is an amount of space taken by instruction, variables and identifiers.
  • 21. • Sp is a space depends upon instance characteristics. • This is a variable part whose space requirement depends on particular problem instance. Ex:- Addition of two numbers. S(p) = C Ө(Sp) = 0 Ex:- Addition of array elements. S(p) >= (n+3) Ex:- Addition of array elements.(Recursive) S(p) >= 3(n+1) The internal stack used for recursion includes space for formal parameters, local variables and return address.
  • 22. Time Complexity • The time complexity of an algorithm is the amount of computer time required by an algorithm to run to completion. • Executing time depends on many factors:-  System load  Number of other programs running  Instruction set used  Speed of underlying hardware • Therefore the time complexity is in terms of frequency count. • Frequency count is a count denoting number of times of execution of a statement.
  • 23. • Measuring an input size • Measuring running time T(n)= C(n) Cop - Where T(n) is running time of basic operation - Cop is Time taken by the basic operation to execute - C(n) is number of times the operation needs to be executed • Order of Growth:- Measuring the performance of an algorithm in relation with the input size n is called Order of Growth.
  • 24. • Example the order of growth for varying input size of n logn n nlogn n*n POW(2,n) 0 1 0 1 2 1 2 2 4 4 2 4 8 16 16 3 8 24 64 256 4 16 64 256 65,536 5 32 160 1024 4,294,967,296
  • 25. Asymptotic Notations • To choose the best algorithm, we need to check efficiency of each algorithm. • The efficiency can be measured by computing time complexity of each algorithm. • Asymptotic notation is a shorthand way to represent the time complexity. • Using asymptotic notations we can give time complexity as “Fastest possible”, “Slowest Possible” or “Average Time”. • Various notations such as ,ϴ and  used are called Asymptotic notations.
  • 26. 1.Big oh Notation • The Big oh notation is denoted by “”. • It is a method of representing the upper bound of algorithms running time. • Using Big oh notation we can give longest amount of time taken by the algorithm to complete. • Definition:- f(n)=O(g(n)) Let f(n) and g(n) be two non negative functions. Let n0 and constant c are two integers such that no denotes some value of input and n>=n0 similarly c is some constant such that c>0 we can write F(n)<=c*g(n) for all n>=n0
  • 27. • Then F(n) is Big oh of g(n). It is also denoted as F(n) Є O(g(n)). In other words F(n) is <=g(n) is multiple of some constant c.
  • 28. • Ex:- Consider the function F(n)=2n+2 and g(n)=n2 then we have to find some constant c so that f(n)<=c*g(n). if n=1 then f(n) > g(n) if n=2 then f(n) > g(n) if n=3 then f(n) < g(n) • Thus always upper bound of existing time is obtained by Big oh notation.
  • 29. 2.Omega Notation • Omega notation is denoted by “”. • This notation is used to represent the Lower Bound of algorithm’s running time. • Using Omega notation we can denote shortest amount of time taken by algorithm. Definition:- • A function f(n) is said to be in (g(n)) if f(n) is bounded below by some positive constant multiple of g(n) such that • F(n) > = c*g(n) for all n>=n0 • It is denoted as f(n) Є (g(n))
  • 30. • Ex:- Consider f(n)=2n2+5 and g(n)=7n n=0 ,1,2 and 3
  • 31. 3.Theta Notation • The theta notation is denoted by “ϴ”. • By this method the running time is between upper bound and lower bound. • Definition:- • Let f(n) and g(n) be two non negative functions. There are two positive constants namely C1 and C2 such that C1*g(n)<=f(n)<=C2*g(n) for all n, n>=n0 f(n) Є ϴ (g(n))
  • 32. Ex:- f(n)=2n+8 and g(n)=7n where n>=2 Similarly f(n)=2n+8 and g(n)=7n i.e 5n < 2n+8 < 7n for n>=2
  • 33. Performance Analysis 1. O(1): Time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain loop, recursion and call to any other non-constant time function. 2. O(n): Time Complexity of a loop is considered as O(n) if the loop variables is incremented / decremented by a constant amount. 3. O(Logn):Time Complexity of a loop is considered as O(Logn) if the loop variables is divided / multiplied by a constant amount.
  • 34. 4) O(nc): Time complexity of nested loops is equal to the number of times the innermost statement is executed. For example the algorithm addition of two matrices contains two loops then we have O(n2) time complexity. 5) O(LogLogn): Time Complexity of a loop is considered as O(LogLogn) if the loop variables is reduced / increased exponentially by a constant amount.
  • 35. Analysis of Algorithms • We can have three cases to analyze an algorithm: 1) Worst Case 2) Average Case 3) Best Case Ex:- Linear Search int search(int arr[], int n, int x) { int i; for (i=0; i<n; i++) { if (arr[i] == x) return i; } return -1; }
  • 36. • If we use the ϴ notation for time complexity then • Worst Case is ϴ(n). • Average Case is ∑(i=1 to n+1)(ϴ(i)) ----------------------- n+1 • i.e Θ(n) • Best Case is ϴ(1)
  • 37. Binary Search • Worst Case O(n) for an unsorted array and O(logn) for sorted array. • Best Case is O(1) (if the key element is equal to the first element of the given array). • Average Case O(logn)
  • 38. int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l)/2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid-1, x); return binarySearch(arr, mid+1, r, x); } return -1; }
  • 39. Big oh Analysis of Algorithms
  • 40. Time Complexities of all Sorting Algorithms Best Average Worst Selection Sort Ω(n^2) θ(n^2) O(n^2) Bubble Sort Ω(n) θ(n^2) O(n^2) Insertion Sort Ω(n) θ(n^2) O(n^2) Heap Sort Ω(n log(n)) θ(n log(n)) O(n log(n)) Quick Sort Ω(n log(n)) θ(n log(n)) O(n^2) Merge Sort Ω(n log(n)) θ(n log(n)) O(n log(n))
  • 41. • Programming Style 1. Clarity and simplicity of Expression: The programs should be designed in such a manner so that the objectives of the program is clear. 2. Naming: In a program, you are required to name the module, processes, and variable, and so on. Care should be taken that the naming style should not be cryptic and non-representative. For Example: a = 3.14 * r * r area_of_circle = 3.14 * radius * radius;
  • 42. • Programming Style 3. Spaces: Style related to white space is commonly used to enhance readability. For instance, compare the following syntactically equivalent examples of C code: int i; for(i=0;i<10;++i){ printf("%d",i*i+i); } versus int i; for (i = 0; i < 10; ++i) { printf("%d", i * i + i); }
  • 43. • Programming Style 4. Tabs: tabs work fine provided they are used consistently, restricted to logical indentation, and not used for alignment. int ix; long sum; 5. Control Constructs: It is desirable that as much as a possible single entry and single exit constructs used. 6. Indentation: Indentation styles assist in identifying control flow and blocks of code. In some programming languages, indentation is used to delimit logical blocks of code; correct indentation in these cases is more than a matter of style. In other languages, indentation and white space do not affect function, although logical and consistent indentation makes code more readable.
  • 44. 7. Nesting: Deep nesting of loops and conditions greatly harm the static and dynamic behaviour of a program. It also becomes difficult to understand the program logic, so it is desirable to avoid deep nesting. 8. Module size: The module size should be uniform. The size of the module should not be too big or too small.
  • 46.
  • 47.
  • 48.
  • 49. Testing:- • Technically Testing is a process to check if the application is working same as it was supposed to do, and not working as it was not supposed to do. • Main objective of Testing is to find bugs and errors in an application which get missed during the unit testing by the developer. • A unit is the smallest testable part of any software. It usually has one or a few inputs and usually a single output. • A unit may be an individual program, function, procedure, etc
  • 50. • Benefits: • Unit testing increases confidence in changing / maintaining code. If good unit tests are written and if they are run every time any code is changed, we will be able to promptly catch any defects introduced due to the change. Also, if codes are already made less interdependent to make unit testing possible, the unintended impact of changes to any code is less. • Codes are more reusable. In order to make unit testing possible, codes need to be modular. This means that codes are easier to reuse. • Debugging is easy. When a test fails, only the latest changes need to be debugged.
  • 51. • Data abstraction • Data Abstraction is the property by virtue of which only the essential details are displayed to the user. • The trivial or the non-essentials units are not displayed to the user. Ex: A car is viewed as a car rather than its individual components. • Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details. • The properties and behaviors of an object differentiate it from other objects of similar type and also help in classifying/grouping the objects.
  • 52. • Consider a real-life example of a man driving a car. • The man only knows that pressing the accelerators will increase the speed of car or applying brakes will stop the car, but he does not know about how on pressing the accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of the accelerator, brakes, etc in the car. This is what abstraction is. • Advantages of Abstraction • It reduces the complexity of viewing the things. • Avoids code duplication and increases reusability. • Helps to increase security of an application or program as only important details are provided to the user.