Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
74 views

Data Str-Time &space Complexity

This document provides details about a data structures course, including course outcomes, prerequisites, related courses, and topics to be covered such as algorithm performance analysis, time complexity, space complexity, and asymptotic notations. It also defines key concepts like algorithms, data structures, and the importance of analyzing time and space complexity. Example topics to be covered include sorting algorithms, trees, hashing techniques, and graph traversal.

Uploaded by

Tom Jones
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Data Str-Time &space Complexity

This document provides details about a data structures course, including course outcomes, prerequisites, related courses, and topics to be covered such as algorithm performance analysis, time complexity, space complexity, and asymptotic notations. It also defines key concepts like algorithms, data structures, and the importance of analyzing time and space complexity. Example topics to be covered include sorting algorithms, trees, hashing techniques, and graph traversal.

Uploaded by

Tom Jones
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Department of

Computer Science and Engineering

DATA STRUCTURES
Unit 1-Linear structures
Year/Sem : II/III
Subject Code: 1151CS102
Topic : Time and space complexity
Faculty Name : Vijitha.S
Date : 06.08.2020
School of Computing
Vel Tech Rangarajan Dr. Sagunthala R&D Institute of
Science and Technology
COURSE DETAILS
Preamble:
This course provides an introduction to the basic concepts and techniques of
Linear and nonlinear data Structures and Analyze the various algorithm.
•Prerequisite Courses:
1150CS201 Problem Solving using C

•Related Courses:

Sl. No Course Code Course Name

1 1151CS105 System Software

2 1151CS106 Design and Analysis of Algorithm


and Project
3 1151CS107 Management
Database Management System
(SEPM)

4 1151CS108 Operating Systems

5
8/06/2020
1151CS111 Department
ComputerofNetworks
Computer Science and Engineering
COURSE OUTCOMES
 Identify and explain user defined data types, linear data structures for
solving real world problems.

 Design modular programs on non linear data structures and


algorithms for solving engineering problems efficiently.

 Illustrate special trees and Hashing Techniques.

 Apply searching techniques in graph traversal

 Apply sorting techniques for real world problems.

8/06/2020 Department of Computer Science and Engineering


Contents

 Data Structures-Definition
 Algorithm
 Algorithm Performance Analysis
 Algorithm Complexity
 Asymptotic notations
 Time Complexity
 Time Complexity of sorting algorithms
 Space Complexity
and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Data Strutures-Definition
Data is a Collection of raw facts.

Data structure is a representation of the logical


relationship existing between individual elements of
data.

Data structure is a specialized format for organizing


and storing data in memory that considers not only
elements stored but also their relationship to each other.
and Project
Management
Data structure affects the design of both structural &
(SEPM)

functional aspects of a program.


8/06/2020 Department of Computer Science and Engineering
Data structures-Definition

Program=algorithm+datastructure

 Data structure is a data organization, management,


and storage format that enables efficient access and
modification.

 Some examples of Data Structures are arrays,


Linked List, Stack, Queue, etc.

 Real time Examples:


and Project Google map, Tree, Graph
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Algorithm

• An algorithm is a finite set of instructions or logic,


written in order, to accomplish a certain predefined
task.

• Algorithm is not the complete code or program, it is


just the core logic(solution) of a problem.

• It can be expressed either as an informal high level


description as pseudo-code or using a flowchart.
and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Algorithm

Why do we need Time and Space Complexity of


algorithm ?

To measure 1.Algorithm performance/efficiency,


2.Time Complexity.
3.Space Complexity

and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Algorithm Complexity

 As an algorithm is a sequence of steps to solve a


problem, there may be more than one algorithm to
solve a problem.

 The choice of particular algorithm depends upon the


following considerations:

1. Performance required - time complexity.


2. Memory required - space complexity.
and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Algorithm Complexity

Any good algorithm should satisfy 2 obvious


conditions:

1. Compute desired output for the given problem-


correctness of algorithm
2. Be effective (fast) - complexity of algorithm

Complexity of algorithm measures how fast is the


algorithm (time complexity) and what amount of
memory it uses (space complexity) - time and
memory
8/06/2020 Department of Computer Science and Engineering
Algorithm Complexity
 The complexity of an algorithm is a function
describing the efficiency of the algorithm in terms
of the amount of data the algorithm must process.

 An algorithm is said to be efficient and fast, if it


takes less time to execute and consumes less
memory space.

 The performance of an algorithm is measured on


the basis of following properties:
and Project
Management
1. Time Complexity (SEPM)

2. Space Complexity
8/06/2020 Department of Computer Science and Engineering
Algorithm complexity

Less Time

Efficient
Algorithm

Less
Memory

and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Algorithm complexity

 There is often a time-space-tradeoff involved in a


problem, that is, it cannot be solved with few
computing time and low memory consumption.

 One has to make a compromise and to exchange


computing time for memory consumption or vice
versa.

 It is depending on which algorithm one chooses and


and Project
how one parameterizes
Management
(SEPM) it.

8/06/2020 Department of Computer Science and Engineering


Algorithm complexity

Less Time More Time

More Memory Less Memory

and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Asymptotic notations
Asymptotic notations are mathematical tools to
represent time complexity of algorithms 

• Big O -  O(n)
• Big Omega -  Ω(n)
• Big Theta -  θ(n)

and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


ASYMPTOTIC NOTATION
Mathematical notations used to describe the running
time of an algorithm.

Theta Notation (Θ-notation):


 Represents the upper and the lower bound of the
running time of an algorithm
 Used for analyzing the average case complexity
and Project
Management
of an algorithm.
(SEPM)

8/06/2020 Department of Computer Science and Engineering


ASYMPTOTIC NOTATION
Big-O Notation (O-notation)
 Represents the upper bound of the running
time of an algorithm.
 Gives the worst case complexity of an
algorithm.
Omega Notation (Ω-notation)
 Represents the lower bound of the running
and Project
time of an algorithm.
Management
(SEPM)

 Provides best case complexity of an algorithm.


8/06/2020 Department of Computer Science and Engineering
Time complexity
Time Complexity:

 Time complexity of an algorithm signifies the total


time required by the program to run till its
completion.

 Time Complexity of an algorithm represents the


amount of time required by the algorithm to run to
and Project
completion. Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Time complexity

 Time requirements can be defined as a numerical


function T(n), where T(n) can be measured as the
number of steps, provided each step consumes
constant time.

 The lower time complexity the faster algorithm.

 It has three cases: Best-case 


Average-case 
Worst-case.
and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Types of time complexity
Types of time complexity:

1. Best case time complexity


2. Average case time complexity
3. Worst case time complexity

Best case Time Complexity:

  It is the minimum amount of time that an algorithm


requires for an input of size n. 

8/06/2020 Department of Computer Science and Engineering


Types of time complexity
Average case time complexity:

 The average-case running time of an algorithm is an


estimate of the running time for an "average" input.

 Computation of average-case running time entails


knowing all possible input sequences.

 The probability distribution of occurrence of these


sequences, and the running times for the individual
and Project
sequences. Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Types of time complexity
Worst case time complexity

 It is otherwise known as Pessimistic Time


Complexity

 It is the function defined by the maximum amount of


time needed by an algorithm for an input of size n.

and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Examples of Time complexity

For Eg, we have the following unsorted list [1, 5, 3, 9, 2,


4, 6, 7, 8] and we need to find the index of a value in this
list using linear search.

Best-case:

 The best case would be to search for the value 1.

 Since this is the first value of the list, it would be


found in the firstManagement
iteration.
and Project

(SEPM)

8/06/2020 Department of Computer Science and Engineering


Examples of Time complexity

Average-case:

 This is the average case complexity of solving the


problem.

 This complexity is defined with respect to the


distribution of the values in the input data the
average-case would be when we’re searching for
some value in the “middle” of the list, for example,
and Project
the value 2. Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Exmples of Time complexity
Worst-case:

 This is the complexity of solving the problem for the


worst input of size n.

 In our example, the worst-case would be to search


for the value 8, which is the last element from the list.

8/06/2020 Department of Computer Science and Engineering


Time complexity

and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Time complexity
Constant Time: O(1)

An algorithm is said to have a constant time when it is


not dependent on the size of input data (n).The running
time will always be the same.

For example:

if a > b:
return True and Project
else: Management
(SEPM)
return False
8/06/2020 Department of Computer Science and Engineering
Time complexity
Linear Time O(n):

 An algorithm is said to have a linear time complexity


when the running time increases at most linearly
with the size of the input data.

For example:
for value in data:
print(value)

Other Examples: and Project


Management
 Get the max/min value in an array.
(SEPM)

 Print all the values in a list.


8/06/2020 Department of Computer Science and Engineering
Time complexity
Logarithmic Time O(log n):

An algorithm is said to have a logarithmic time


complexity when it reduces the size of the input data in
each step (it don’t need to look at all values of the input
data).
5 7 9 13 32 33 42 54 56 88

33 42 54 56 88
Step 1
and Project
Management 33 42
(SEPM) Step 2

33
Step 3
8/06/2020 Department of Computer Science and Engineering
Time complextiy
For example:

for index in range(0, len(data), 3):


print(data[index])

 Algorithms with logarithmic time complexity are


commonly found in operations on binary trees or
when using binary search.

and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Time complexity

Quasilinear/Linearithmic Time — O(n log n):

• An algorithm is said to have a quasilinear time


complexity when each operation in the input data
have a logarithm time complexity.

• It is commonly seen in sorting algorithms (e.g.


mergesort, timsort, heapsort).

and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Time complexity

and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Time complexity
Quadratic Time: O(n2)

An algorithm is said to have a quadratic time complexity


when it needs to perform a linear time operation for
each value in the input data.

for example:

for x in data:
for y in data: and Project
print(x, y) Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Time complexity
Bubble sort  is a great example of quadratic time
complexity since for each value it needs to compare to
all other values in the list.
4 2 6 5 9 8

2 4 6 5 9 8

2 4 5 6 9 8

2 4
and Project
5 6 8 9
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Time complexity

Exponential Time — O(2^n)

 An algorithm is said to have an exponential time


complexity when the growth doubles with each
addition to the input data set.

 This kind of time complexity is usually seen in brute-


force algorithms.

 Another example of an exponential time algorithm is


and Project

the recursive calculation


Management
(SEPM) of Fibonacci numbers:

8/06/2020 Department of Computer Science and Engineering


Time complexity

The Fibonacci Sequence is the series of numbers: 0,


1, 1, 2, 3, 5, 8, 13, 21, 34 ….

def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)

and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Time complexity
Factorial — O(n!)

An algorithm is said to have a factorial time complexity


when it grows in a factorial way based on the size of the
input data, for example:

2! = 2 x 1 = 2
3! = 3 x 2 x 1 = 6
4! = 4 x 3 x 2 x 1 = 24
5! = 5 x 4 x 3 x 2 x 1 = 120
6! = 6 x 5 x 4 x 3 xManagement
2 x 1 = 720
and Project

7! = 7 x 6 x 5 x 4 x(SEPM)
3 x 2 x 1 = 5.040
8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 40.320
8/06/2020 Department of Computer Science and Engineering
Time complexity

 As you may see it grows very fast, even for a small


size input.

 A great example of an algorithm which has a factorial


time complexity is the Heap’s algorithm, which is
used for generating all possible permutations
of n objects.
and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


EXAMPLE
#include <stdio.h>
int main()
O(1)
{
    printf("Hello World");
}

#include <stdio.h>
void main()
{ O(N)
    int i, n ;
scanf(“%d”,&n); and Project
    for (i = 1; i <= n; i++) { Management
(SEPM)
        printf("Hello Word !!!");
    }
}8/06/2020 Department of Computer Science and Engineering
EXAMPLE

O(N^2)

8/06/2020 Department of Computer Science and Engineering


Time complexity

and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Space complexity
 Space complexity is the amount of memory used by
the algorithm including the input values to the
algorithm to execute and produce the result.

 Many algorithms have inputs that can vary in size,


e.g., an array.
and Project
 In such cases, the space complexity will depend on
Management
(SEPM)
the size of the input
8/06/2020 Department of Computer Science and Engineering
Space complexity
 For fixed-size inputs, the complexity will be a
constant O(1).

 An algorithm generally requires space for following


components:

 Instruction Space: Its the space required to store


the executable version of the program. This space is
fixed, but varies depending upon the number of lines
of code in the program.
and Project
Management
(SEPM)
 Data Space: Its the space required to store all the
constants and variables value.
8/06/2020 Department of Computer Science and Engineering
Space Complexity

Space complexity S(I) of any algorithm I is


S(I) = C + S(P)
Where C is the fixed part and S(P) is the variable part of
the algorithm which depends on instance characteristic
P.

Example:
Algorithm: SUM(A, B)

and Project
Step 1 - START Management
(SEPM)
Step 2 - C ← A + B + 10
Step
8/06/2020
3 - Stop Department of Computer Science and Engineering
Space complexity

 Here we have three variables A, B and C and one


constant.

 Hence S(I) = 1+3.

 Now space depends on data types of given variables


and constant types and it will be multiplied
accordingly.
and Project
Management
(SEPM)

8/06/2020 Department of Computer Science and Engineering


Space complexity
Performance Analysis:
Example 1:

1)Algorithm abc(x,y,z)
return x*y*z+(x-y):
S(P)=C+Sp

S(P) = 3 (variables)

 Where S(P) is space


and Project complexity of algorithm
Management
 C= Fixed (SEPM)

 Sp=Variable
8/06/2020 Department of Computer Science and Engineering
Space complexity
Example 2:
Algorithm sum(x,n)
{
total=0
for i1 to n do
total=total+x[i]
S(P)=C+Sp
}
S(P)=3+n Where S(P) is space complexity of
algorithm. and Project
Management
3= Fixed (SEPM)
n=Variable
8/06/2020 Department of Computer Science and Engineering
Thank You

Department of Computer Science and Engineering

You might also like