SlideShare a Scribd company logo
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

Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
 

What's hot (20)

Database programming
Database programmingDatabase programming
Database programming
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLEUnit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
10 instruction sets characteristics
10 instruction sets characteristics10 instruction sets characteristics
10 instruction sets characteristics
 
Unit I Database concepts - RDBMS & ORACLE
Unit I  Database concepts - RDBMS & ORACLEUnit I  Database concepts - RDBMS & ORACLE
Unit I Database concepts - RDBMS & ORACLE
 
Oracle: Control Structures
Oracle: Control StructuresOracle: Control Structures
Oracle: Control Structures
 
database language ppt.pptx
database language ppt.pptxdatabase language ppt.pptx
database language ppt.pptx
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
Sql commands
Sql commandsSql commands
Sql commands
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Division algorithm
Division algorithmDivision algorithm
Division algorithm
 
Cursors.ppt
Cursors.pptCursors.ppt
Cursors.ppt
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Data structure and algorithm All in One
Data structure and algorithm All in OneData structure and algorithm All in One
Data structure and algorithm All in One
 
Datastructure
DatastructureDatastructure
Datastructure
 
number system
number systemnumber system
number system
 

Similar to DSA

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
rajesshs31r
 
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
rajesshs31r
 
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
 

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_lec_1eregdsgdfgdgdfgdfgdfg.ppt
algorithm_lec_1eregdsgdfgdgdfgdfgdfg.pptalgorithm_lec_1eregdsgdfgdgdfgdfgdfg.ppt
algorithm_lec_1eregdsgdfgdgdfgdfgdfg.ppt
 
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
 

Recently uploaded

Recently uploaded (20)

How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 

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.