SlideShare a Scribd company logo
Data Structures and Algorithms
(CoSc2092)
Chapter contents
â–ȘIntroduction to Data structures
✓Classification of data structure
✓Abstract Data Types
✓Abstraction
â–ȘAlgorithms
✓Properties of algorithms
✓Algorithm Analysis Concept
✓Complexity Analysis
â–ȘAsymptotic analysis
2
Introduction
â–ȘWhat is Data Structure?
✓It is a set of procedures to define, store, access and manipulate data
✓It is the organized collection of data
✓It is the logical or mathematical model of a particular organization
of data
â–Ș Program = Data Structure + Algorithm
✓Data Structure is the way data is organized in a computer’s memory so
that it can be used effectively and efficiently.
✓Algorithm is the sequence of computational steps to solve a problem.
â–ȘData Structure is a language construct that the programmer has
defined in order to implement an abstract data type.
3
The easiest way to understand DS
â–ȘLet’s understand Data structure with the help of real Life
â–ȘExamples:
✓In dictionary, words are arranged alphabetically in a dictionary.
You can quickly and efficiently search and discover a term.
✓A business cash-in-cash-out statement. Much as certain data
structures, it is straightforward to aggregate and extract data in
orderly columns.
4
Why Data Structure is Important?
â–ȘData structures organize data => more efficient programs.
â–Ș More powerful computers => more complex applications.
â–Ș More complex applications demand more calculations.
â–Ș Complex computing tasks are unlike our everyday experience
â–Ș The choice of data structure and algorithm can make the
difference between a program running in a few seconds or many
days.
â–Ș You can not do anything without data on the world.
5
Why Data Structure is Important?
â–Ș Computer Science deal with storing, organizing and retrieving data
effectively.
â–Ș Computer Programmer should get data from user or another sources and use
them.
â–Ș It is essential ingredient in creating fast and powerful algorithms.
â–Ș For this purpose you have to use data structure for every software program or
system.
â–Ș Each data structure has costs and benefits.
â–Ș Rarely is one data structure better than another in all situations.
â–Ș A data structure requires:
✓Space for each data item it stores,
✓Time to perform each basic operation,
✓Programming effort.
6
Classifications Data Structure
â–Ș There are two types of data structure available for the programming purpose:
✓Primitive data structure: is a fundamental type of data structure that stores the data of
only one type .
✓Non-primitive data structure: is a type of data structure which is a user-defined that
stores the data of different types in a single entity.
7
Abstraction
â–Ș It is an abstraction of a data structure that provides only the interface to
which the data structure must adhere.
â–Ș The interface does not give any specific details about something should be
implemented or in what programming language.
â–Ș Abstraction: It is a technique of hiding the internal details from the user and
only showing the necessary details to the user.
â–Ș Encapsulation: It is a technique of combining the data and the member
function in a single unit is known as encapsulation.
8
What is Abstract Data Type?
â–ȘAn Abstract Data Type is composed of:
✓A collection of data / set of values
✓A set of operations on data
â–ȘSpecification of ADT indicate
✓What the ADT operations do, not how to implement them
â–ȘImplementation of an ADT
✓Includes choosing a particular data structure
✓DS is a constructor that can be defined in a programming language to
store a collection of data.
9
What is ADT?
â–ȘADT consists of an abstract data structure and operations
â–ȘSpecifies:
✓What can be stored in the ADT
✓What operations can be done on/by the ADT
â–ȘE.g. if we are going to model employees of an organization:
✓ADT stores employees with
‱ their relevant attributes and
‱ discarding irrelevant attributes.
✓ADT supports
‱ hiring, firing, retiring, 
 operations.
â–Ș An ADT tells what is to be done and data structure tells how it is to be
done.
â–Ș In other words, we can say that ADT gives us the blueprint while data
structure provides the implementation part.
10
What is ADT?
11
What is ADT?
â–ȘThere are lots of formalized and standard Abstract data types
✓such as Stacks, Queues, Trees, etc.
12
What is Algorithm?
â–ȘIt is a process or a set of rules required to perform calculations or
some other problem-solving operations especially by a computer.
â–ȘThe formal definition:
✓It contains the finite set of instructions which are being carried in a
specific order to perform the specific task.
✓It is not the complete program or code; it is just a solution (logic) of a
problem, which can be represented either as an informal description
using a Flowchart or Pseudocode.
13
Properties of an Algorithm
â–Ș The following are the characteristics of an algorithm:
✓ Input
✓ Output
✓ Finiteness
✓ Definiteness
✓ Sequence
✓ Feasibility
✓ Correctness
✓ Language Independent
14
Why do we need Algorithms?
â–ȘWe need algorithms because of the following reasons:
✓Scalability:
‱ It helps us to understand the scalability.
‱ When we have a big real-world problem, we need to scale it down into small-
small steps to easily analyze the problem.
✓Performance:
‱ The real-world is not easily broken down into smaller steps.
‱ If the problem can be easily broken into smaller steps means that the problem is
feasible.
15
Importance of Algorithms
â–ȘTheoretical importance:
✓When any real-world problem is given to us and we break the problem into
small-small modules.
✓To break down the problem, we should know all the theoretical aspects.
â–ȘPractical importance:
✓As we know that theory cannot be completed without the practical
implementation.
✓So, the importance of algorithm can be considered as both theoretical and
practical.
â–ȘIssues of Algorithms
â–Ș The following are the issues that come while designing an algorithm:
✓How to design algorithms
✓How to analyze algorithm efficiency
16
Algorithm Analysis Concept
â–Ș It refers to the process of determining the amount of computing time and
storage space required by different algorithms.
â–Ș It is a process of predicting the resource requirement of algorithms in a
given environment.
â–Ș Main resources are:
✓Running Time
✓Memory Usage
✓Communication Bandwidth
17
Algorithm Analysis
â–ȘThe algorithm can be analyzed in two levels
✓First is before creating the algorithm and
✓Second is after creating the algorithm.
â–Ș The following are the two analysis of an algorithm:
✓Priori Analysis:
‱ Theoretical analysis of an algorithm which is done before implementing
the algorithm. Various factors can be considered before implementing the
algorithm like processor speed, which has no effect on the implementation
part.
✓Posterior Analysis:
‱ practical analysis of an algorithm.
‱ The practical analysis is achieved by implementing the algorithm using any
programming language.
‱ This analysis basically evaluate that how much running time and space
taken by the algorithm.
18
Complexity Analysis of Algorithm
â–Ș Complexity Analysis of Algorithm is the systematic study of the cost of
computation measured either:
✓in time units or
✓in operations performed, or
✓in the amount of storage space required
â–Ș The goal is to have a meaningful measure that permits comparison of
algorithms independent of operating platform.
19
Complexity Analysis of Algorithm
â–ȘThe performance of the algorithm can be measured in two factors:
✓Time complexity:
‱ The amount of time required to complete the execution.
‱ Is denoted by the big O notation. Here, big O notation is the asymptotic
notation to represent the time complexity.
‱ Is mainly calculated by counting the number of steps to finish the
execution.
✓Space complexity:
‱ The amount of space required to solve a problem and produce an output.
‱ Similar to the time complexity, space complexity is also expressed in big O
notation.
20
How to measure the efficiency of algorithms?
â–ȘThere are two approaches, Empirical & Theoretical.
â–ȘEmpirical:
✓competing algorithms and trying them on different instances.
â–ȘTheoretical:
✓Determining the quantity of resources required mathematically
(Execution time, memory space, etc.) needed by each algorithm
â–ȘHowever, it is difficult to use actual clock-time as a consistent
measure of an algorithm‘s efficiency, because clock-time can vary
based on many things. For example:
✓Specific processor speed, Current processor load
✓Specific data for a particular run of the program (Input Size, Input
Properties)
✓Operating Environment 21
Cont...
â–ȘAn algorithm’s performance depends on internal and external
factors.
â–ȘInternal:
✓The algorithm’s efficiency, in terms of:
‱ Time required to run
‱ Space (memory storage) required to run
â–ȘExternal:
✓The algorithm’s efficiency, in terms of:
‱ Size of the input to the algorithm
‱ Speed of the computer on which it is run
‱ Quality of the compiler
22
Cont...
â–ȘWhat metric should be used to judge performance of an algorithms?
✓Length of the program (lines of code)
✓Ease of programming (bugs, maintenance)
✓Memory required
✓Running time
â–ȘRunning time is the dominant standard.
✓Quantifiable and easy to compare
â–ȘAn algorithm may run differently depending on:
✓The hardware platform (PC, processor speed)
✓The programming language (C, Java, C++)
✓The programmer (you, me)
23
Phases of Complexity analysis
â–ȘComplexity analysis involves two distinct phases.
â–ȘAlgorithm Analysis:
✓Analysis of the algorithm or data structure to produce a function T(n)
that describes the algorithm in terms of the operations performed in order
to measure the complexity of the algorithm.
â–ȘOrder of Magnitude Analysis:
✓Analysis of the function T(n) to determine the general complexity
category to which it belongs.
24
Complexity analysis Rules
â–ȘWe assume an arbitrary time unit.
â–ȘExecution of one of the following operations takes time 1:
✓Assignment Operation
✓Single Input/output Operation
✓Single Boolean Operations
✓Single Arithmetic Operations
✓Function Return
25
Cont...
â–ȘRunning time of a selection statement (if, switch)is:
✓The time for the condition evaluation + The maximum of the running times
for the individual clauses in the selection.
â–ȘLoops:
✓Running time for a loop is equal to:
‱ The running time for the statements inside the loop * number of iterations.
✓The total running time of a statement inside a group of nested loops is:
‱ The running time of the statements multiplied by the product of the sizes of all
the loops.
✓For nested loops, analyze inside out.
✓Always assume that the loop executes the maximum number of iterations
possible.
â–ȘRunning time of a function call is:
✓1 for setup + the time for any parameter calculations + the time required
for the execution of the function body.
26
Example
27
void func()
{
int x=0;
int i=0;
int j=1;
cout<<"Enter an Integer value";
cin>>n;
while (i<n)
{ x++;
i++;
}
while (j<n)
{ j++;
}
}
Time Units to Compute
-------------------------------------------------
1 for the first assignment statement: x=0;
1 for the second assignment statement: i=0;
1 for the third assignment statement: j=1;
1 for the output statement.
1 for the input statement.
In the first while loop:
n+1 tests
n loops of 2 units for the two increment (addition)
operations
In the second while loop:
n tests
n-1 increments
-------------------------------------------------------
T (n)= 1+1+1+1+1+n+1+2n+n+n-1 = 5n+5 = O(n)
???
Next class

28
Asymptotic Analysis
â–ȘWhile checking the running time for any algorithm, we want three
things:
✓The algorithm should be machine-independent.
✓It should work on all possible inputs.
✓It should be general and not programming language-specific.
â–ȘHowever, we never do accurate analysis, rather we do approximate
analysis. We are interested in the increase in the running time with
the increase in the input size.
â–ȘThus, we are only interested in the ‘growth of the function’, which
we can measure/analyses through asymptotic analysis.
29
Asymptotic Analysis
â–ȘThe term asymptotic means approaching a value or curve
arbitrarily closely (i.e., as some sort of limit is taken).
â–ȘAsymptotic analysis:
✓It is a general methodology to compare or to find the efficiency of any
algorithm.
✓We measure the efficiency in terms of the growth of the function.
‱ The growth of any function depends on how much the running time is increasing
with the increase in the size of the input.
â–ȘAsymptotic analysis of an algorithm refers to defining the
mathematical boundary of its run-time performance.
30
Asymptotic Analysis
â–ȘThe time required by an algorithm falls under three types.
✓Best Case:
‱ Minimum time required for program execution.
✓Average Case:
‱ Average time required for program execution.
✓Worst Case
‱ Maximum time required for program execution.
31
Asymptotic Notation
â–ȘAsymptotic Notation:
✓Whenever we want to perform analysis of an algorithm, we need to
calculate the complexity of that algorithm.
✓But when we calculate the complexity of an algorithm it does not provide
the exact amount of resource required.
✓So instead of taking the exact amount of resource, we represent that
complexity in a general form (Notation) which produces the basic nature
of that algorithm.
✓We use that general form (Notation) for analysis process.
✓So asymptotic notation is a language that allow us to analyze an
algorithm's running time by identifying its behavior as the input size for
the algorithm increases.
✓It is a mathematical representation of its complexity.
32
Asymptotic Notation
â–ȘIn asymptotic notation:
✓When we want to represent the complexity of an algorithm, we use only
the most significant terms in the complexity of that algorithm and ignore
least significant terms in the complexity of that algorithm
‱ Here, complexity can be Space Complexity or Time Complexity.
â–ȘExample: T(n) = ck nk + ck-1 nk-1 + ck-2 nk-2 + 
 + c1n + co
✓too complicated
✓too many terms
✓Difficult to compare two expressions, each with 10 or 20 terms
â–ȘDo we really need that many terms?
33
Asymptotic Notation
â–Ș Keep just one term!
✓the fastest growing term (dominates the runtime)
â–Ș No constant coefficients are kept
✓Constant coefficients affected by machines, languages, etc.
â–Ș Asymptotic behavior (as n gets large) is determined entirely by the leading
term.
✓Example. T(n) = 10n3 + n2 + 40n + 800
‱ If n = 1,000, then T(n) = 10,001,040,800
‱ error is 0.01% if we drop all but the n3 term leading term.
34
Asymptotic Notation
35
â–ȘMajorly we use three types of asymptotic notations to represent
the complexity of an algorithms.
Asymptotic Notation
â–ȘBig-Oh Notation (O):
✓is used to define the upper bound of an algorithm in terms of Time
Complexity.
✓is always indicates the maximum time required by an algorithm for all
input values.
✓is describes the worst case of an algorithm time complexity.
â–ȘConsider function f(n) as time complexity of an algorithm and g(n)
is the most significant term.
✓If f(n) <= c.g(n) for all n >= n0, c > 0 and n0 >= 1.
✓Then we can represent f(n) as O(g(n)).
✓f(n)=O(g(n))
36
Asymptotic Notation
â–Ș Example:
â–Ș Consider the following f(n) and g(n)
✓f(n) = 3n + 2
✓g(n) = n
â–Ș If we want to represent f(n) as O(g(n)) then it must satisfy
✓f(n) <= c.g(n) for all n >= n0, c > 0 and n0 >= 1
✓f(n) <= c.g(n) ⇒3n + 2 <= cn
â–Ș Above condition is always TRUE for all values of
✓c = 4 and n >= 2.
â–Ș By using Big - Oh notation we can represent the time complexity as follows...
✓3n + 2 = O(n)
37
Asymptotic Notation
â–ȘBig-Omega Notation (Ω):
✓ is used to define the lower bound of an algorithm in terms of Time
Complexity.
✓is always indicates the minimum time required by an algorithm for all
input values.
✓describes the best case of an algorithm time complexity.
â–ȘConsider function f(n) as time complexity of an algorithm and g(n)
is the most significant term.
✓If f(n) >= c.g(n) for all n >= n0, c > 0 and n0 >= 1.
✓Then we can represent f(n) as Ω(g(n)).
✓f(n) = Ω(g(n))
38
Asymptotic Notation
â–Ș Example:
â–Ș Consider the following f(n) and g(n)
✓f(n) = 3n + 2
✓g(n) = n
â–Ș If we want to represent f(n) as Ω(g(n)) then it must satisfy
✓f(n) >= c.g(n) for all values of c > 0 and n0>= 1
✓f(n) >= c.g(n) ⇒3n + 2 >= C n
â–Ș Above condition is always TRUE for all values of
✓c = 1 and n >= 1.
â–Ș By using Big - Omega notation we can represent the time complexity as
follows
✓3n + 2 = Ω(n)
39
Asymptotic Notation
â–ȘBig-Theta Notation (Θ):
✓is used to define the average bound of an algorithm in terms of Time
Complexity.
✓is always indicates the average time required by an algorithm for all input
values.
✓is describes the average case of an algorithm time complexity.
â–ȘConsider function f(n) as time complexity of an algorithm and g(n)
is the most significant term.
✓If c1.g(n) <= f(n) <= c2. g(n) for all n >= n0, c1 > 0, c2 > 0 and n0 >= 1.
✓Then we can represent f(n) as Θ(g(n)).
✓f(n) = Θ(g(n))
40
Asymptotic Notation
â–Ș Example:
â–Ș Consider the following f(n) and g(n).
✓f(n) = 3n + 2
✓g(n) = n
â–Ș If we want to represent f(n) as Θ(g(n)) then it must satisfy
✓c1.g(n) <= f(n) <= c2.g(n) for all values of c1 > 0, c2 > 0 and n0>= 1
✓c1.g(n) <= f(n) <= c2.g(n) ⇒ c1n <= 3n + 2 <= c2n
â–Ș Above condition is always TRUE for all values of
✓c1 = 1, c2 = 4 and n >= 2.
â–Ș By using Big - Theta notation we can represent the time complexity as
follows.
✓3n + 2 = Θ(n)
41
End of Chapter!

More Related Content

PDF
Cs6402 design and analysis of algorithms may june 2016 answer key
DOCX
AI Lab Manual.docx
DOC
Generalized transition graphs
PPTX
Formal Methods lecture 01
PPT
Software Engineering Fundamentals
PPTX
Operator precedance parsing
PPTX
Symbolic Mathematics
PPTX
Artefacts of the Process
Cs6402 design and analysis of algorithms may june 2016 answer key
AI Lab Manual.docx
Generalized transition graphs
Formal Methods lecture 01
Software Engineering Fundamentals
Operator precedance parsing
Symbolic Mathematics
Artefacts of the Process

What's hot (20)

PDF
Compiler Construction | Lecture 1 | What is a compiler?
PPTX
Bootstrap loader
PPTX
PRESCRIPTIVE PROCESS MODEL(SOFTWARE ENGINEERING)
PDF
Syntax Directed Definition and its applications
PPTX
Software project management introduction
PPTX
Sum of subset problem.pptx
PPT
Backtracking Algorithm.ppt
DOCX
Techniques & applications of Compiler
PPTX
halstead software science measures
PPTX
Algorithm - Introduction
DOCX
Concept of Failure, error, fault and defect
PPTX
Asymptotic Notations
PDF
Parallel sorting Algorithms
PPSX
Complete C programming Language Course
PPTX
Halsted’s Software Science-An analytical technique
PDF
Lecture Note-1: Algorithm and Its Properties
PPTX
Algorithm Introduction
PPT
Asymptotic notations
PPTX
Asymptotic notations
PPTX
Introduction Of C++
Compiler Construction | Lecture 1 | What is a compiler?
Bootstrap loader
PRESCRIPTIVE PROCESS MODEL(SOFTWARE ENGINEERING)
Syntax Directed Definition and its applications
Software project management introduction
Sum of subset problem.pptx
Backtracking Algorithm.ppt
Techniques & applications of Compiler
halstead software science measures
Algorithm - Introduction
Concept of Failure, error, fault and defect
Asymptotic Notations
Parallel sorting Algorithms
Complete C programming Language Course
Halsted’s Software Science-An analytical technique
Lecture Note-1: Algorithm and Its Properties
Algorithm Introduction
Asymptotic notations
Asymptotic notations
Introduction Of C++
Ad

Similar to Data structure and algorithm Chapter_1.pdf (20)

PPTX
Introduction to databae eChapter 1-.pptx
PPTX
Lecture 1 and 2
PPT
assignment character education assignment
PPTX
Intro to Data Structure & Algorithms
PPTX
Chapter 1- IT.pptx
PPTX
Basic of Data Structure - Data Structure - Notes
PDF
Utility Layer in data science and its types
PPTX
Data Structure and Algorithms
PPTX
ADS Introduction
PPT
algo 1.ppt
PDF
Machine Learning for Capacity Management
 
PPTX
datastructuresandalgorithm-module1-230307012644-4c895c84.pptx
PDF
Introductionofdatastructure 110731092019-phpapp01
PDF
Introduction of data_structure
PPT
part 1 - intorduction data structure 2021 mte.ppt
PPTX
Ece engineering college Stack and queue.pptx
PPTX
Clean architecture
PPTX
Result processing system (Project)
PDF
data structure
PPT
ORACLE 12C-New-Features
Introduction to databae eChapter 1-.pptx
Lecture 1 and 2
assignment character education assignment
Intro to Data Structure & Algorithms
Chapter 1- IT.pptx
Basic of Data Structure - Data Structure - Notes
Utility Layer in data science and its types
Data Structure and Algorithms
ADS Introduction
algo 1.ppt
Machine Learning for Capacity Management
 
datastructuresandalgorithm-module1-230307012644-4c895c84.pptx
Introductionofdatastructure 110731092019-phpapp01
Introduction of data_structure
part 1 - intorduction data structure 2021 mte.ppt
Ece engineering college Stack and queue.pptx
Clean architecture
Result processing system (Project)
data structure
ORACLE 12C-New-Features
Ad

Recently uploaded (20)

PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
System and Network Administraation Chapter 3
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Nekopoi APK 2025 free lastest update
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
history of c programming in notes for students .pptx
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
PDF
Digital Strategies for Manufacturing Companies
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
top salesforce developer skills in 2025.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
Design an Analysis of Algorithms II-SECS-1021-03
Upgrade and Innovation Strategies for SAP ERP Customers
System and Network Administraation Chapter 3
Internet Downloader Manager (IDM) Crack 6.42 Build 41
VVF-Customer-Presentation2025-Ver1.9.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
CHAPTER 2 - PM Management and IT Context
Designing Intelligence for the Shop Floor.pdf
Operating system designcfffgfgggggggvggggggggg
Nekopoi APK 2025 free lastest update
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Digital Systems & Binary Numbers (comprehensive )
history of c programming in notes for students .pptx
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
Digital Strategies for Manufacturing Companies
How to Migrate SBCGlobal Email to Yahoo Easily
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
wealthsignaloriginal-com-DS-text-... (1).pdf
top salesforce developer skills in 2025.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development

Data structure and algorithm Chapter_1.pdf

  • 1. Data Structures and Algorithms (CoSc2092)
  • 2. Chapter contents â–ȘIntroduction to Data structures ✓Classification of data structure ✓Abstract Data Types ✓Abstraction â–ȘAlgorithms ✓Properties of algorithms ✓Algorithm Analysis Concept ✓Complexity Analysis â–ȘAsymptotic analysis 2
  • 3. Introduction â–ȘWhat is Data Structure? ✓It is a set of procedures to define, store, access and manipulate data ✓It is the organized collection of data ✓It is the logical or mathematical model of a particular organization of data â–Ș Program = Data Structure + Algorithm ✓Data Structure is the way data is organized in a computer’s memory so that it can be used effectively and efficiently. ✓Algorithm is the sequence of computational steps to solve a problem. â–ȘData Structure is a language construct that the programmer has defined in order to implement an abstract data type. 3
  • 4. The easiest way to understand DS â–ȘLet’s understand Data structure with the help of real Life â–ȘExamples: ✓In dictionary, words are arranged alphabetically in a dictionary. You can quickly and efficiently search and discover a term. ✓A business cash-in-cash-out statement. Much as certain data structures, it is straightforward to aggregate and extract data in orderly columns. 4
  • 5. Why Data Structure is Important? â–ȘData structures organize data => more efficient programs. â–Ș More powerful computers => more complex applications. â–Ș More complex applications demand more calculations. â–Ș Complex computing tasks are unlike our everyday experience â–Ș The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days. â–Ș You can not do anything without data on the world. 5
  • 6. Why Data Structure is Important? â–Ș Computer Science deal with storing, organizing and retrieving data effectively. â–Ș Computer Programmer should get data from user or another sources and use them. â–Ș It is essential ingredient in creating fast and powerful algorithms. â–Ș For this purpose you have to use data structure for every software program or system. â–Ș Each data structure has costs and benefits. â–Ș Rarely is one data structure better than another in all situations. â–Ș A data structure requires: ✓Space for each data item it stores, ✓Time to perform each basic operation, ✓Programming effort. 6
  • 7. Classifications Data Structure â–Ș There are two types of data structure available for the programming purpose: ✓Primitive data structure: is a fundamental type of data structure that stores the data of only one type . ✓Non-primitive data structure: is a type of data structure which is a user-defined that stores the data of different types in a single entity. 7
  • 8. Abstraction â–Ș It is an abstraction of a data structure that provides only the interface to which the data structure must adhere. â–Ș The interface does not give any specific details about something should be implemented or in what programming language. â–Ș Abstraction: It is a technique of hiding the internal details from the user and only showing the necessary details to the user. â–Ș Encapsulation: It is a technique of combining the data and the member function in a single unit is known as encapsulation. 8
  • 9. What is Abstract Data Type? â–ȘAn Abstract Data Type is composed of: ✓A collection of data / set of values ✓A set of operations on data â–ȘSpecification of ADT indicate ✓What the ADT operations do, not how to implement them â–ȘImplementation of an ADT ✓Includes choosing a particular data structure ✓DS is a constructor that can be defined in a programming language to store a collection of data. 9
  • 10. What is ADT? â–ȘADT consists of an abstract data structure and operations â–ȘSpecifies: ✓What can be stored in the ADT ✓What operations can be done on/by the ADT â–ȘE.g. if we are going to model employees of an organization: ✓ADT stores employees with ‱ their relevant attributes and ‱ discarding irrelevant attributes. ✓ADT supports ‱ hiring, firing, retiring, 
 operations. â–Ș An ADT tells what is to be done and data structure tells how it is to be done. â–Ș In other words, we can say that ADT gives us the blueprint while data structure provides the implementation part. 10
  • 12. What is ADT? â–ȘThere are lots of formalized and standard Abstract data types ✓such as Stacks, Queues, Trees, etc. 12
  • 13. What is Algorithm? â–ȘIt is a process or a set of rules required to perform calculations or some other problem-solving operations especially by a computer. â–ȘThe formal definition: ✓It contains the finite set of instructions which are being carried in a specific order to perform the specific task. ✓It is not the complete program or code; it is just a solution (logic) of a problem, which can be represented either as an informal description using a Flowchart or Pseudocode. 13
  • 14. Properties of an Algorithm â–Ș The following are the characteristics of an algorithm: ✓ Input ✓ Output ✓ Finiteness ✓ Definiteness ✓ Sequence ✓ Feasibility ✓ Correctness ✓ Language Independent 14
  • 15. Why do we need Algorithms? â–ȘWe need algorithms because of the following reasons: ✓Scalability: ‱ It helps us to understand the scalability. ‱ When we have a big real-world problem, we need to scale it down into small- small steps to easily analyze the problem. ✓Performance: ‱ The real-world is not easily broken down into smaller steps. ‱ If the problem can be easily broken into smaller steps means that the problem is feasible. 15
  • 16. Importance of Algorithms â–ȘTheoretical importance: ✓When any real-world problem is given to us and we break the problem into small-small modules. ✓To break down the problem, we should know all the theoretical aspects. â–ȘPractical importance: ✓As we know that theory cannot be completed without the practical implementation. ✓So, the importance of algorithm can be considered as both theoretical and practical. â–ȘIssues of Algorithms â–Ș The following are the issues that come while designing an algorithm: ✓How to design algorithms ✓How to analyze algorithm efficiency 16
  • 17. Algorithm Analysis Concept â–Ș It refers to the process of determining the amount of computing time and storage space required by different algorithms. â–Ș It is a process of predicting the resource requirement of algorithms in a given environment. â–Ș Main resources are: ✓Running Time ✓Memory Usage ✓Communication Bandwidth 17
  • 18. Algorithm Analysis â–ȘThe algorithm can be analyzed in two levels ✓First is before creating the algorithm and ✓Second is after creating the algorithm. â–Ș The following are the two analysis of an algorithm: ✓Priori Analysis: ‱ Theoretical analysis of an algorithm which is done before implementing the algorithm. Various factors can be considered before implementing the algorithm like processor speed, which has no effect on the implementation part. ✓Posterior Analysis: ‱ practical analysis of an algorithm. ‱ The practical analysis is achieved by implementing the algorithm using any programming language. ‱ This analysis basically evaluate that how much running time and space taken by the algorithm. 18
  • 19. Complexity Analysis of Algorithm â–Ș Complexity Analysis of Algorithm is the systematic study of the cost of computation measured either: ✓in time units or ✓in operations performed, or ✓in the amount of storage space required â–Ș The goal is to have a meaningful measure that permits comparison of algorithms independent of operating platform. 19
  • 20. Complexity Analysis of Algorithm â–ȘThe performance of the algorithm can be measured in two factors: ✓Time complexity: ‱ The amount of time required to complete the execution. ‱ Is denoted by the big O notation. Here, big O notation is the asymptotic notation to represent the time complexity. ‱ Is mainly calculated by counting the number of steps to finish the execution. ✓Space complexity: ‱ The amount of space required to solve a problem and produce an output. ‱ Similar to the time complexity, space complexity is also expressed in big O notation. 20
  • 21. How to measure the efficiency of algorithms? â–ȘThere are two approaches, Empirical & Theoretical. â–ȘEmpirical: ✓competing algorithms and trying them on different instances. â–ȘTheoretical: ✓Determining the quantity of resources required mathematically (Execution time, memory space, etc.) needed by each algorithm â–ȘHowever, it is difficult to use actual clock-time as a consistent measure of an algorithm‘s efficiency, because clock-time can vary based on many things. For example: ✓Specific processor speed, Current processor load ✓Specific data for a particular run of the program (Input Size, Input Properties) ✓Operating Environment 21
  • 22. Cont... â–ȘAn algorithm’s performance depends on internal and external factors. â–ȘInternal: ✓The algorithm’s efficiency, in terms of: ‱ Time required to run ‱ Space (memory storage) required to run â–ȘExternal: ✓The algorithm’s efficiency, in terms of: ‱ Size of the input to the algorithm ‱ Speed of the computer on which it is run ‱ Quality of the compiler 22
  • 23. Cont... â–ȘWhat metric should be used to judge performance of an algorithms? ✓Length of the program (lines of code) ✓Ease of programming (bugs, maintenance) ✓Memory required ✓Running time â–ȘRunning time is the dominant standard. ✓Quantifiable and easy to compare â–ȘAn algorithm may run differently depending on: ✓The hardware platform (PC, processor speed) ✓The programming language (C, Java, C++) ✓The programmer (you, me) 23
  • 24. Phases of Complexity analysis â–ȘComplexity analysis involves two distinct phases. â–ȘAlgorithm Analysis: ✓Analysis of the algorithm or data structure to produce a function T(n) that describes the algorithm in terms of the operations performed in order to measure the complexity of the algorithm. â–ȘOrder of Magnitude Analysis: ✓Analysis of the function T(n) to determine the general complexity category to which it belongs. 24
  • 25. Complexity analysis Rules â–ȘWe assume an arbitrary time unit. â–ȘExecution of one of the following operations takes time 1: ✓Assignment Operation ✓Single Input/output Operation ✓Single Boolean Operations ✓Single Arithmetic Operations ✓Function Return 25
  • 26. Cont... â–ȘRunning time of a selection statement (if, switch)is: ✓The time for the condition evaluation + The maximum of the running times for the individual clauses in the selection. â–ȘLoops: ✓Running time for a loop is equal to: ‱ The running time for the statements inside the loop * number of iterations. ✓The total running time of a statement inside a group of nested loops is: ‱ The running time of the statements multiplied by the product of the sizes of all the loops. ✓For nested loops, analyze inside out. ✓Always assume that the loop executes the maximum number of iterations possible. â–ȘRunning time of a function call is: ✓1 for setup + the time for any parameter calculations + the time required for the execution of the function body. 26
  • 27. Example 27 void func() { int x=0; int i=0; int j=1; cout<<"Enter an Integer value"; cin>>n; while (i<n) { x++; i++; } while (j<n) { j++; } } Time Units to Compute ------------------------------------------------- 1 for the first assignment statement: x=0; 1 for the second assignment statement: i=0; 1 for the third assignment statement: j=1; 1 for the output statement. 1 for the input statement. In the first while loop: n+1 tests n loops of 2 units for the two increment (addition) operations In the second while loop: n tests n-1 increments ------------------------------------------------------- T (n)= 1+1+1+1+1+n+1+2n+n+n-1 = 5n+5 = O(n)
  • 29. Asymptotic Analysis â–ȘWhile checking the running time for any algorithm, we want three things: ✓The algorithm should be machine-independent. ✓It should work on all possible inputs. ✓It should be general and not programming language-specific. â–ȘHowever, we never do accurate analysis, rather we do approximate analysis. We are interested in the increase in the running time with the increase in the input size. â–ȘThus, we are only interested in the ‘growth of the function’, which we can measure/analyses through asymptotic analysis. 29
  • 30. Asymptotic Analysis â–ȘThe term asymptotic means approaching a value or curve arbitrarily closely (i.e., as some sort of limit is taken). â–ȘAsymptotic analysis: ✓It is a general methodology to compare or to find the efficiency of any algorithm. ✓We measure the efficiency in terms of the growth of the function. ‱ The growth of any function depends on how much the running time is increasing with the increase in the size of the input. â–ȘAsymptotic analysis of an algorithm refers to defining the mathematical boundary of its run-time performance. 30
  • 31. Asymptotic Analysis â–ȘThe time required by an algorithm falls under three types. ✓Best Case: ‱ Minimum time required for program execution. ✓Average Case: ‱ Average time required for program execution. ✓Worst Case ‱ Maximum time required for program execution. 31
  • 32. Asymptotic Notation â–ȘAsymptotic Notation: ✓Whenever we want to perform analysis of an algorithm, we need to calculate the complexity of that algorithm. ✓But when we calculate the complexity of an algorithm it does not provide the exact amount of resource required. ✓So instead of taking the exact amount of resource, we represent that complexity in a general form (Notation) which produces the basic nature of that algorithm. ✓We use that general form (Notation) for analysis process. ✓So asymptotic notation is a language that allow us to analyze an algorithm's running time by identifying its behavior as the input size for the algorithm increases. ✓It is a mathematical representation of its complexity. 32
  • 33. Asymptotic Notation â–ȘIn asymptotic notation: ✓When we want to represent the complexity of an algorithm, we use only the most significant terms in the complexity of that algorithm and ignore least significant terms in the complexity of that algorithm ‱ Here, complexity can be Space Complexity or Time Complexity. â–ȘExample: T(n) = ck nk + ck-1 nk-1 + ck-2 nk-2 + 
 + c1n + co ✓too complicated ✓too many terms ✓Difficult to compare two expressions, each with 10 or 20 terms â–ȘDo we really need that many terms? 33
  • 34. Asymptotic Notation â–Ș Keep just one term! ✓the fastest growing term (dominates the runtime) â–Ș No constant coefficients are kept ✓Constant coefficients affected by machines, languages, etc. â–Ș Asymptotic behavior (as n gets large) is determined entirely by the leading term. ✓Example. T(n) = 10n3 + n2 + 40n + 800 ‱ If n = 1,000, then T(n) = 10,001,040,800 ‱ error is 0.01% if we drop all but the n3 term leading term. 34
  • 35. Asymptotic Notation 35 â–ȘMajorly we use three types of asymptotic notations to represent the complexity of an algorithms.
  • 36. Asymptotic Notation â–ȘBig-Oh Notation (O): ✓is used to define the upper bound of an algorithm in terms of Time Complexity. ✓is always indicates the maximum time required by an algorithm for all input values. ✓is describes the worst case of an algorithm time complexity. â–ȘConsider function f(n) as time complexity of an algorithm and g(n) is the most significant term. ✓If f(n) <= c.g(n) for all n >= n0, c > 0 and n0 >= 1. ✓Then we can represent f(n) as O(g(n)). ✓f(n)=O(g(n)) 36
  • 37. Asymptotic Notation â–Ș Example: â–Ș Consider the following f(n) and g(n) ✓f(n) = 3n + 2 ✓g(n) = n â–Ș If we want to represent f(n) as O(g(n)) then it must satisfy ✓f(n) <= c.g(n) for all n >= n0, c > 0 and n0 >= 1 ✓f(n) <= c.g(n) ⇒3n + 2 <= cn â–Ș Above condition is always TRUE for all values of ✓c = 4 and n >= 2. â–Ș By using Big - Oh notation we can represent the time complexity as follows... ✓3n + 2 = O(n) 37
  • 38. Asymptotic Notation â–ȘBig-Omega Notation (Ω): ✓ is used to define the lower bound of an algorithm in terms of Time Complexity. ✓is always indicates the minimum time required by an algorithm for all input values. ✓describes the best case of an algorithm time complexity. â–ȘConsider function f(n) as time complexity of an algorithm and g(n) is the most significant term. ✓If f(n) >= c.g(n) for all n >= n0, c > 0 and n0 >= 1. ✓Then we can represent f(n) as Ω(g(n)). ✓f(n) = Ω(g(n)) 38
  • 39. Asymptotic Notation â–Ș Example: â–Ș Consider the following f(n) and g(n) ✓f(n) = 3n + 2 ✓g(n) = n â–Ș If we want to represent f(n) as Ω(g(n)) then it must satisfy ✓f(n) >= c.g(n) for all values of c > 0 and n0>= 1 ✓f(n) >= c.g(n) ⇒3n + 2 >= C n â–Ș Above condition is always TRUE for all values of ✓c = 1 and n >= 1. â–Ș By using Big - Omega notation we can represent the time complexity as follows ✓3n + 2 = Ω(n) 39
  • 40. Asymptotic Notation â–ȘBig-Theta Notation (Θ): ✓is used to define the average bound of an algorithm in terms of Time Complexity. ✓is always indicates the average time required by an algorithm for all input values. ✓is describes the average case of an algorithm time complexity. â–ȘConsider function f(n) as time complexity of an algorithm and g(n) is the most significant term. ✓If c1.g(n) <= f(n) <= c2. g(n) for all n >= n0, c1 > 0, c2 > 0 and n0 >= 1. ✓Then we can represent f(n) as Θ(g(n)). ✓f(n) = Θ(g(n)) 40
  • 41. Asymptotic Notation â–Ș Example: â–Ș Consider the following f(n) and g(n). ✓f(n) = 3n + 2 ✓g(n) = n â–Ș If we want to represent f(n) as Θ(g(n)) then it must satisfy ✓c1.g(n) <= f(n) <= c2.g(n) for all values of c1 > 0, c2 > 0 and n0>= 1 ✓c1.g(n) <= f(n) <= c2.g(n) ⇒ c1n <= 3n + 2 <= c2n â–Ș Above condition is always TRUE for all values of ✓c1 = 1, c2 = 4 and n >= 2. â–Ș By using Big - Theta notation we can represent the time complexity as follows. ✓3n + 2 = Θ(n) 41