CHAPTER 1:
Introduction to
Data Structures and
Algorithms
OUTLINE
Introduction to Data Structures
 Abstract data Types
 Abstraction
Algorithms
 Properties of an algorithm
 Algorithm analysis concepts
• Complexity Analysis
• Asymptotic Analysis
Introduction to Data Structures
• Group of data elements which provides an efficient
utilization of data (storing, accessing and organizing) in the
computer.
• The main idea behind using data structures is to minimize
the time and space complexities.
Introduction to Data Structures
• Data Structures are widely used in almost every aspect of
Computer Science, i.e., Operating system, Compiler
Design, Artificial Intelligence, etc.
• A data structure is the implementation of an ADT.
Introduction to Data Structures
Introduction to Data Structures
Abstract Data Types:
What is data type?
 It is defined the type of data.
 It defines a certain domain of values.
 It also defines the operations allowed on those values.
 Example: take ‘int’, it takes only integer values. And
operations like addition, subtraction, …
 What about ‘float’, ‘double’, ‘char’, …
Introduction to Data Structures
User defined data type:
Values and operations are defined by the user not
by the language itself.
Example: struct, union, enumeration,…
Abstract Data Types:
 Abstract Data type (ADT) is a type (class) for objects
whose behavior is defined by a set of values and a set
of operations.
Introduction to Data Structures
 ADT Composed of:
 a collection of data (certain domain of values) and
 operation allowed on these values.
 It does not specify how data will be organized in
memory and what algorithms will be used for
implementing the operations.
 It is called “abstract” because it gives an
implementation-independent view.
 ADT implementation can be array or linked list.
Introduction to Data Structures
ADT is a black box which hides the inner structure and
design of the data type.
 Example: List, Stack, Queue,…
 Stack data types: Consists of elements of same type arranged in a sequential order.
 Stack operation: initialization, push, pop, …,
Abstraction: The process of providing only the essentials
and hiding the details is known as abstraction.
 For example: We use basic data types without prior
knowledge of how they are implemented.
Introduction to Data Structures
Algorithms
• Algorithm is defined as a process or set of well-defined
instructions that are typically used to solve a particular
(group of) problems or perform a specific type of
calculation.
• A set of finite rules or instructions to be followed in
calculations.
• A step-by-step procedure for solving a mathematical
problem.
Algorithms
• Algorithm is every where, computer science, mathematics,
operational research, AI, Data Science,…
• Algorithms can be range simple to complex depending on
what you want to achieve.
Algorithms
Algorithms
• Properties of an algorithm
Algorithms
Properties of Algorithm:
• It should terminate after a finite time.
• It should produce at least one output.
• It should take zero or more input.
• It should be deterministic means giving the same output
for the same input case.
• Every step in the algorithm must be effective i.e. every
step should do some work.
Algorithms
Types of Algorithm:
• Brute force Algorithm
• Recursive Algorithm
• Backtracking Algorithm
• Searching and Sorting Algorithm
• Hashing Algorithm
• Divide and Conquer Algorithm
• Greedy Algorithm
• Dynamic Programming Algorithm
• Randomized Algorithm
Disadvantages of Algorithm: challenging, time consuming,…
Algorithm Analysis Concepts
❑ Computational complexity is a measure of the amount of
computing resources (time and space) that a particular algorithm
consumes when it runs.
❑ Computer Scientists use mathematical measures of complexity
that allow them to predict, before writing the code.
❑ How fast an algorithm will run and how much memory it will
require.
Algorithm Analysis Concepts
Such predictions are important guides for programmers
implementing and selecting algorithms for real-world
applications.
It characterize the time taken by an algorithm with respect
to input size (independent of the machine, language and
compiler).
It is used for evaluating the variations of execution time on
different algorithms.
Complexity Analysis
What is the need for Complexity Analysis?
 Complexity Analysis determines the resources (amount of
time and space) required to execute it.
 It is used for comparing different algorithms on different
input sizes.
 Complexity helps to understand the difficulty of a problem.
 CA measured by how much time(CPU) and space
(memory) required to solve a particular problem.
Complexity Analysis
Different types of Complexity exist:
1. Constant Complexity, Lookup an arbitrary index , A[0], A[6], A[99],…
2. Logarithmic Complexity, binary search
3. Linear Complexity, linear search
4. Quadratic Complexity, Square NxN Matrix
5. Polynomial Complexity, triple loop
6. Exponential Complexity, Tower of Hanoi
7. Factorial Complexity, Factorials of a number, Permutation.
Complexity Analysis
Polynomial time:
• Linear search : n
• Binary search : logn
• Insertion Sort : n2
• Merge Sort : nlogn
• Matrix Multiplication
Exponential Time:
• 0/1 knapsack : 2n
• Traveling Salesman problem : 2n
• Sum of subsets : 2n
• Graph coloring : 2n
• Hamiltonian Cycle : 2n
Complexity Analysis
Complexity Analysis
Examining the exact (or Measuring the actual) running
time is not the best solution to calculate the time
complexity.
The running time is generally depends on the size of the
inputs.
Consider the n-size array:
Assume we want to add one more element at the
beginning of the list. 10
20 30 40
Complexity Analysis
We need three shift to the right and make the first slot
empty. Then we can add.
 Assume, if one shift takes 1 unit of time, then three shifts will
take 3 units of time.
It is not a problem doing this, but what is number of
elements are 1000,100,000, or more in the array? It needs
this much time of unit of operations.
So, shifting is costly however adding element is the same.
 Running time generally depends on input size.
10 20 30 40
Complexity Analysis
 Therefore, if the size of the input is ‘n’, then f(n) is a function of ‘n’
denotes the time complexity.
 In other words, f(n) represents the number of instructions executed
for the input value n.
 For example: for(int i=1;i≤ n;i++){
cout<<“Hello world!”;//Assume this instruction will take 1unit of
time.
}
 “cout” instruction is executed “n” number of times. Therefore f(n) = n.
 How to find f(n)?
 Finding f(n) for small size of input may be easy, but difficult for large input size.
Complexity Analysis
We can compare two data structures or algorithms for a
particular operation by comparing their f(n) values.
We are interested in growth rate of f(n) with respect to n,
but it is impossible for smaller input size.
 How do you know growth rate of f(n)?
Example: f(n) = 5n²+6n+12,
 n represents time or number of instructions executed.
Let’s see the contribution of each terms.
Complexity Analysis
Complexity Analysis
Complexity Analysis
Complexity Analysis
So what can we conclude?
The larger value of n, the squared term consumes much of
the time nearly 100%. And the constant and n terms
doesn’t affect on the complexity of the time. We can
remove(discard) these terms. So, f(n) =5n² n².
This approximate measure of time complexity is know as
asymptotic complexity. This what we are interested in
during time complexity.
Asymptotic Analysis
 Asymptotic Analysis is evaluates the performance of an algorithm in
terms of input size. It describes run-time performance(complexity).
 Examining(Measuring) the exact(actual) running time is not the
best solution to calculate the time complexity.  Asymptotic
Analysis.
 It deals with how an algorithm performs as the size of the input
grows.
 Helps to understand the best case, average case, and worst
case scenario of an algorithm.
 Asymptotic analysis commonly used in complexity analysis.
ASYMPTOTIC NOTATIONS
• The three most commonly used asymptotic notations are:
• Big-O Notation (O-notation)
• Omega Notation (Ω-notation)
• Theta Notation (Θ-notation)
Big-O Notation (O-notation)
• O stands for Order of , So O(N) is read “Order of N”.
• It is the most widely used notation for Asymptotic Analysis.
• It specifies the upper bound of a function.
• The maximum time required by an algorithm or the worst-
case time complexity.
O-Notation
• It returns the highest possible output value(big-O) for a
given input.
• Allows an algorithm to complete statement execution in
the longest amount of time.
• It implies that the function will never grow faster than this
upper bound.
Big-O Notation (O-notation)
 If f(n) describes the running time of an algorithm, then
f(n) = O(g(n)), if there exist a positive constant C and n0
such that, f(n) ≤ c.g(n) for all n ≥ n0.
 This simply means that f(n) doesn’t grow faster than g(n).
Big-O Notation (O-notation)
 Example 1:
Let f(n) = n, and g(n) = 2n; Is f(n) = O(g(n))?
f(n) ≤ c.g(n), n ≤ c.2n, for c=1, n =1
₀
. no b/c n<=2n ==1<=2;
 Example 2:
Let f(n) = 4n+3 and g(n) = n; Is f(n) = O(g(n))?
 Let’s show, f(n) ≤ c.g(n)  4n+3 ≤ n.
Consider c = 5, then 4n+3 ≤ 5.n 3 ≤5n-4n 3 ≤n or n ≥ 3, So n = 3.
₀
 Therefore, f(n) ≤ c.g(n), for all n ≥ 3, where c=5,and n = 3.
₀
f(n) = O(n), i.e.; the growth rate is linear.
Big-O Notation (O-notation)
n f(n) g(n)
1 7 5
2 11 10
3 15 15
4 19 20
5 23 25
6 27 30
7 31 35
8 35 40
9 39 45
10 43 50
Big-O Notation (O-notation)
Big-O Notation (O-notation)
• Let f(n) +4, and g(n) = , is f(n) c.g(n)?
≤
Big-O Notation (O-notation)
Big-O Notation (O-notation)
Omega Notation (Ω-notation)
 Omega notation represents the lower bound of the running
time of an algorithm. Thus, it provides the best case
complexity of an algorithm.
allows an algorithm to complete statement execution in the
shortest amount of time.
Let g and f be the function from the set of natural numbers
to itself.
The function f is said to be Ω(g), if there is a constant c > 0
and a natural number n0 such that c*g(n) ≤ f(n) for all n ≥ n0.
Omega Notation (Ω-notation)
Theta Notation (Θ-notation)
•Let g and f be the function from the set of natural numbers.
The function f is said to be Θ(g), if there are constants c1, c2
> 0 and a natural number n0 such that c1* g(n) ≤ f(n) ≤ c2 *
g(n) for all n ≥ n0.
• Used to analyze the Average case.
Conti…
• The main idea of asymptotic analysis is to have a
measure of the efficiency of algorithms, regardless of
machine specification as the input size growth.
Thank you !!

CHAPTER 1 Introduction to Data Structures and Algorithms (2).pptx

  • 1.
    CHAPTER 1: Introduction to DataStructures and Algorithms
  • 2.
    OUTLINE Introduction to DataStructures  Abstract data Types  Abstraction Algorithms  Properties of an algorithm  Algorithm analysis concepts • Complexity Analysis • Asymptotic Analysis
  • 3.
    Introduction to DataStructures • Group of data elements which provides an efficient utilization of data (storing, accessing and organizing) in the computer. • The main idea behind using data structures is to minimize the time and space complexities.
  • 4.
    Introduction to DataStructures • Data Structures are widely used in almost every aspect of Computer Science, i.e., Operating system, Compiler Design, Artificial Intelligence, etc. • A data structure is the implementation of an ADT.
  • 5.
  • 6.
    Introduction to DataStructures Abstract Data Types: What is data type?  It is defined the type of data.  It defines a certain domain of values.  It also defines the operations allowed on those values.  Example: take ‘int’, it takes only integer values. And operations like addition, subtraction, …  What about ‘float’, ‘double’, ‘char’, …
  • 7.
    Introduction to DataStructures User defined data type: Values and operations are defined by the user not by the language itself. Example: struct, union, enumeration,… Abstract Data Types:  Abstract Data type (ADT) is a type (class) for objects whose behavior is defined by a set of values and a set of operations.
  • 8.
    Introduction to DataStructures  ADT Composed of:  a collection of data (certain domain of values) and  operation allowed on these values.  It does not specify how data will be organized in memory and what algorithms will be used for implementing the operations.  It is called “abstract” because it gives an implementation-independent view.  ADT implementation can be array or linked list.
  • 9.
    Introduction to DataStructures ADT is a black box which hides the inner structure and design of the data type.  Example: List, Stack, Queue,…  Stack data types: Consists of elements of same type arranged in a sequential order.  Stack operation: initialization, push, pop, …, Abstraction: The process of providing only the essentials and hiding the details is known as abstraction.  For example: We use basic data types without prior knowledge of how they are implemented.
  • 10.
  • 11.
    Algorithms • Algorithm isdefined as a process or set of well-defined instructions that are typically used to solve a particular (group of) problems or perform a specific type of calculation. • A set of finite rules or instructions to be followed in calculations. • A step-by-step procedure for solving a mathematical problem.
  • 12.
    Algorithms • Algorithm isevery where, computer science, mathematics, operational research, AI, Data Science,… • Algorithms can be range simple to complex depending on what you want to achieve.
  • 13.
  • 14.
  • 15.
    Algorithms Properties of Algorithm: •It should terminate after a finite time. • It should produce at least one output. • It should take zero or more input. • It should be deterministic means giving the same output for the same input case. • Every step in the algorithm must be effective i.e. every step should do some work.
  • 16.
    Algorithms Types of Algorithm: •Brute force Algorithm • Recursive Algorithm • Backtracking Algorithm • Searching and Sorting Algorithm • Hashing Algorithm • Divide and Conquer Algorithm • Greedy Algorithm • Dynamic Programming Algorithm • Randomized Algorithm Disadvantages of Algorithm: challenging, time consuming,…
  • 17.
    Algorithm Analysis Concepts ❑Computational complexity is a measure of the amount of computing resources (time and space) that a particular algorithm consumes when it runs. ❑ Computer Scientists use mathematical measures of complexity that allow them to predict, before writing the code. ❑ How fast an algorithm will run and how much memory it will require.
  • 18.
    Algorithm Analysis Concepts Suchpredictions are important guides for programmers implementing and selecting algorithms for real-world applications. It characterize the time taken by an algorithm with respect to input size (independent of the machine, language and compiler). It is used for evaluating the variations of execution time on different algorithms.
  • 19.
    Complexity Analysis What isthe need for Complexity Analysis?  Complexity Analysis determines the resources (amount of time and space) required to execute it.  It is used for comparing different algorithms on different input sizes.  Complexity helps to understand the difficulty of a problem.  CA measured by how much time(CPU) and space (memory) required to solve a particular problem.
  • 20.
    Complexity Analysis Different typesof Complexity exist: 1. Constant Complexity, Lookup an arbitrary index , A[0], A[6], A[99],… 2. Logarithmic Complexity, binary search 3. Linear Complexity, linear search 4. Quadratic Complexity, Square NxN Matrix 5. Polynomial Complexity, triple loop 6. Exponential Complexity, Tower of Hanoi 7. Factorial Complexity, Factorials of a number, Permutation.
  • 21.
    Complexity Analysis Polynomial time: •Linear search : n • Binary search : logn • Insertion Sort : n2 • Merge Sort : nlogn • Matrix Multiplication Exponential Time: • 0/1 knapsack : 2n • Traveling Salesman problem : 2n • Sum of subsets : 2n • Graph coloring : 2n • Hamiltonian Cycle : 2n
  • 22.
  • 23.
    Complexity Analysis Examining theexact (or Measuring the actual) running time is not the best solution to calculate the time complexity. The running time is generally depends on the size of the inputs. Consider the n-size array: Assume we want to add one more element at the beginning of the list. 10 20 30 40
  • 24.
    Complexity Analysis We needthree shift to the right and make the first slot empty. Then we can add.  Assume, if one shift takes 1 unit of time, then three shifts will take 3 units of time. It is not a problem doing this, but what is number of elements are 1000,100,000, or more in the array? It needs this much time of unit of operations. So, shifting is costly however adding element is the same.  Running time generally depends on input size. 10 20 30 40
  • 25.
    Complexity Analysis  Therefore,if the size of the input is ‘n’, then f(n) is a function of ‘n’ denotes the time complexity.  In other words, f(n) represents the number of instructions executed for the input value n.  For example: for(int i=1;i≤ n;i++){ cout<<“Hello world!”;//Assume this instruction will take 1unit of time. }  “cout” instruction is executed “n” number of times. Therefore f(n) = n.  How to find f(n)?  Finding f(n) for small size of input may be easy, but difficult for large input size.
  • 26.
    Complexity Analysis We cancompare two data structures or algorithms for a particular operation by comparing their f(n) values. We are interested in growth rate of f(n) with respect to n, but it is impossible for smaller input size.  How do you know growth rate of f(n)? Example: f(n) = 5n²+6n+12,  n represents time or number of instructions executed. Let’s see the contribution of each terms.
  • 27.
  • 28.
  • 29.
  • 30.
    Complexity Analysis So whatcan we conclude? The larger value of n, the squared term consumes much of the time nearly 100%. And the constant and n terms doesn’t affect on the complexity of the time. We can remove(discard) these terms. So, f(n) =5n² n². This approximate measure of time complexity is know as asymptotic complexity. This what we are interested in during time complexity.
  • 31.
    Asymptotic Analysis  AsymptoticAnalysis is evaluates the performance of an algorithm in terms of input size. It describes run-time performance(complexity).  Examining(Measuring) the exact(actual) running time is not the best solution to calculate the time complexity.  Asymptotic Analysis.  It deals with how an algorithm performs as the size of the input grows.  Helps to understand the best case, average case, and worst case scenario of an algorithm.  Asymptotic analysis commonly used in complexity analysis.
  • 32.
    ASYMPTOTIC NOTATIONS • Thethree most commonly used asymptotic notations are: • Big-O Notation (O-notation) • Omega Notation (Ω-notation) • Theta Notation (Θ-notation)
  • 33.
    Big-O Notation (O-notation) •O stands for Order of , So O(N) is read “Order of N”. • It is the most widely used notation for Asymptotic Analysis. • It specifies the upper bound of a function. • The maximum time required by an algorithm or the worst- case time complexity.
  • 34.
    O-Notation • It returnsthe highest possible output value(big-O) for a given input. • Allows an algorithm to complete statement execution in the longest amount of time. • It implies that the function will never grow faster than this upper bound.
  • 35.
    Big-O Notation (O-notation) If f(n) describes the running time of an algorithm, then f(n) = O(g(n)), if there exist a positive constant C and n0 such that, f(n) ≤ c.g(n) for all n ≥ n0.  This simply means that f(n) doesn’t grow faster than g(n).
  • 36.
    Big-O Notation (O-notation) Example 1: Let f(n) = n, and g(n) = 2n; Is f(n) = O(g(n))? f(n) ≤ c.g(n), n ≤ c.2n, for c=1, n =1 ₀ . no b/c n<=2n ==1<=2;  Example 2: Let f(n) = 4n+3 and g(n) = n; Is f(n) = O(g(n))?  Let’s show, f(n) ≤ c.g(n)  4n+3 ≤ n. Consider c = 5, then 4n+3 ≤ 5.n 3 ≤5n-4n 3 ≤n or n ≥ 3, So n = 3. ₀  Therefore, f(n) ≤ c.g(n), for all n ≥ 3, where c=5,and n = 3. ₀ f(n) = O(n), i.e.; the growth rate is linear.
  • 37.
    Big-O Notation (O-notation) nf(n) g(n) 1 7 5 2 11 10 3 15 15 4 19 20 5 23 25 6 27 30 7 31 35 8 35 40 9 39 45 10 43 50
  • 38.
  • 39.
    Big-O Notation (O-notation) •Let f(n) +4, and g(n) = , is f(n) c.g(n)? ≤
  • 40.
  • 41.
  • 42.
    Omega Notation (Ω-notation) Omega notation represents the lower bound of the running time of an algorithm. Thus, it provides the best case complexity of an algorithm. allows an algorithm to complete statement execution in the shortest amount of time. Let g and f be the function from the set of natural numbers to itself. The function f is said to be Ω(g), if there is a constant c > 0 and a natural number n0 such that c*g(n) ≤ f(n) for all n ≥ n0.
  • 43.
  • 44.
    Theta Notation (Θ-notation) •Letg and f be the function from the set of natural numbers. The function f is said to be Θ(g), if there are constants c1, c2 > 0 and a natural number n0 such that c1* g(n) ≤ f(n) ≤ c2 * g(n) for all n ≥ n0. • Used to analyze the Average case.
  • 45.
    Conti… • The mainidea of asymptotic analysis is to have a measure of the efficiency of algorithms, regardless of machine specification as the input size growth.
  • 46.

Editor's Notes

  • #1 Find the total time unit taken for the following code? Details of Asymptotic Notation?
  • #7 Data types such as int, float, double, long, etc. are considered to be in-built (primitive) data types.
  • #8 Advantage of ADT: Possible to change the implementation the DS any time without affecting the interface.
  • #9 ADT can be List, Stack, Queue but we don’t know how they are implemented (abstraction).
  • #10 Application program = client program which uses data structure. Implementation = program which implements data structures.
  • #25 F(n) is keeping the count of the number of instructions.
  • #35 O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0 }.
  • #36 O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ f(n) ≤ c.g(n) for all c>0, and n ≥ n0 }.
  • #37 O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ f(n) ≤ c.g(n) for all c>0, and n ≥ n0 }.
  • #38 Let f(n) = 5n2+4, and g(n) =n2, is f(n) ≤ c.g(n)?
  • #40 https://www.youtube.com/watch?v=HFjJgSguqUA n2longn
  • #41 https://www.youtube.com/watch?v=HFjJgSguqUA
  • #42 Ω(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }.
  • #43 Ω(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }.