SlideShare ist ein Scribd-Unternehmen logo
1 von 52
Downloaden Sie, um offline zu lesen
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.

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

LR(0) PARSER
LR(0) PARSERLR(0) PARSER
LR(0) PARSER
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
Oop
OopOop
Oop
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Shell sort
Shell sortShell sort
Shell sort
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Number theory and cryptography
Number theory and cryptographyNumber theory and cryptography
Number theory and cryptography
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structure
 
Big o notation
Big o notationBig o notation
Big o notation
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
 
[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)
 
Binary search
Binary searchBinary search
Binary search
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
 
Hash table
Hash tableHash table
Hash table
 
Algorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structureAlgorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structure
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
asymptotic notations i
asymptotic notations iasymptotic notations i
asymptotic notations i
 
Algorithms KS1 & KS2
Algorithms KS1 & KS2Algorithms KS1 & KS2
Algorithms KS1 & KS2
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxElton John Embodo
 

Kürzlich hochgeladen (20)

Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docx
 

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.