0% found this document useful (0 votes)
18 views33 pages

INF210 Lec2 Data Structures

This document discusses measuring the efficiency of data structures using Big O notation. It covers topics like time complexity equations, common time complexities like O(1), O(n), O(log n), and why we use Big O notation to measure efficiency in a hardware-independent way based on the number of operations.

Uploaded by

Nader Yasser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views33 pages

INF210 Lec2 Data Structures

This document discusses measuring the efficiency of data structures using Big O notation. It covers topics like time complexity equations, common time complexities like O(1), O(n), O(log n), and why we use Big O notation to measure efficiency in a hardware-independent way based on the number of operations.

Uploaded by

Nader Yasser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Data Structures

INF 210
Fall 2022-2023
Lecture 2
Introduction to Data Structures

INSTRUCTOR
DR/ AYMAN SOLIMAN & DR/ OSAMA FAROUK
Ø Contents
1) Measuring Efficiency with BigO Notation

2) Time Complexity Equations

3) Why BigO?

4) Quick Recap?

5) Types of Time Complexity Equations

6) Final Note on Time Complexity Equations

10/12/22 Dr/ Ayman Soliman 2


1) Measuring Efficiency with BigO Notation
Ø We want a quantifiable way to measure how efficient certain data structures
are at different tasks we might ask of it

§ Searching through

§ Modifying

§ Accessing

10/12/22 Dr/ Ayman Soliman 3


1) Measuring Efficiency with BigO Notation
Ø The industry standard for this kind of measurement

§ BigO Notation

WHAT IS IT?

HOW DO WE
MEASURE IT?
10/12/22 Dr/ Ayman Soliman 4
1) Measuring Efficiency with BigO Notation
Ø BigO Notation

§ A way to basically “Score” a data structure based on 4 criteria

§ The most common functions you might want from a data structure

q Accessing elements

q Searching for an element

q Inserting an element

q Deleting an element

10/12/22 Dr/ Ayman Soliman 5


1) Measuring Efficiency with BigO Notation
Ø By measuring how efficiently a data structure can do these 4 things

§ We can create a “report card”

Accessing Searching Inserting Deleting

10/12/22 Dr/ Ayman Soliman 6


2) Measuring Efficiency with BigO Notation - Time Complexity Equations
Ø A Time Complexity Equation works by inserting the size of the data-set as an
integer n, and returning the number of operations that need to be conducted by
the computer before the function can finish
N = The Size of the Data Set (amount of elements
contained within the data structure)

N = 10
Accessing Searching Inserting Deleting
Equation Equation Equation Equation

10/12/22 Dr/ Ayman Soliman 7


2) Measuring Efficiency with BigO Notation - Time Complexity Equations
Ø A Time Complexity Equation works by inserting the size of the data-set as an
integer n, and returning the number of operations that need to be conducted by
the computer before the function can finish

The Number of operations that need to be conducted


by the computer before completion of that function

Accessing Searching Inserting Deleting


Equation Equation Equation Equation

10/12/22 Dr/ Ayman Soliman 8


2) Measuring Efficiency with BigO Notation - Time Complexity Equations
Ø We always use the worst-case scenario when judging these data structures

8 Operations
2 Operations
50 Operations

42 Operations
1898 Operations

10/12/22 Dr/ Ayman Soliman 9


2) Measuring Efficiency with BigO Notation - Time Complexity Equations
Ø It’s called BigO notation because the syntax for the Time Complexity
equations includes a BigO and then a set of parentheses

§ The parenthesis houses the function

A Function Time Complexity

O of 2
Random
Function() O(2)
10/12/22 Dr/ Ayman Soliman 10
2) Measuring Efficiency with BigO Notation - Time Complexity Equations
Ø It’s called BigO notation because the syntax for the Time Complexity
equations includes a BigO and then a set of parentheses

§ The parenthesis houses the function

Constant Time
A Function Time Complexity

O of 5
Random
Function() O(5)
10/12/22 Dr/ Ayman Soliman 11
2) Measuring Efficiency with BigO Notation - Time Complexity Equations
Ø It’s called BigO notation because the syntax for the Time Complexity
equations includes a BigO and then a set of parentheses

§ The parenthesis houses the function

Constant Time
A Function Time Complexity

O(5)
O of 5
Random
Function()

10/12/22 Dr/ Ayman Soliman 12


2) Measuring Efficiency with BigO Notation - Time Complexity Equations
Ø It’s called BigO notation because the syntax for the Time Complexity
equations includes a BigO and then a set of parentheses

§ The parenthesis houses the function


Constant Time
A Function Time Complexity

O(5)
O of 5
Random
Function()
Ø Most of the time, our integer n, is going to have some adverse-effect on how
many operations it takes
10/12/22 Dr/ Ayman Soliman 13
2) Measuring Efficiency with BigO Notation - Time Complexity Equations
Ø It’s called BigO notation because the syntax for the Time Complexity
equations includes a BigO and then a set of parentheses

§ The parenthesis houses the function

Ø Most of the time, our integer n, is going to have some adverse-effect on how
many operations it takes

Larger Data Set More Instructions

10/12/22 Dr/ Ayman Soliman 14


3) Measuring Efficiency with BigO Notation - Why BigO?
Ø We measure efficiency in # of operations performed because measuring by
how long the function takes to run would be silly

§ Measuring by time is biased towards better hardware.

>
Speed

Laptop
Super-computer
10/12/22 Dr/ Ayman Soliman 15
4) Measuring Efficiency with BigO Notation - Quick Recap?

We measure efficiency based on 4 metrics

Accessing Searching Inserting Deleting

Modeled by an equation which takes in size of data-set (n) and returns


number of operations needed to be performed by the computer to complete
that task

What the data structure is good at, and what the data structure is bad at

10/12/22 Dr/ Ayman Soliman 16


4) Measuring Efficiency with BigO Notation - Quick Recap?
Ø A Data Structures efficiency isn’t the end all be all for deciding on which data
structure to use in your program

§ Some have specific quarks or features which separate them

Accessing Searching Inserting Deleting

10/12/22 Dr/ Ayman Soliman 17


5) Types of Time Complexity Equations
Ø Let’s dive straight into what the actual equations mean in terms of efficiency

§ 6 most common Time Complexity Equations

O(1) O(n) O(log n) O(n log n) O(n²) O(2n)

10/12/22 Dr/ Ayman Soliman 18


5) Types of Time Complexity Equations
Ø The absolute best a data structure can “score” on each criteria is O(1)

§ No matter what the size of your data set is, the task will be completed in a
single instruction

Operations Required Size of Data Set (N)

1 1, 100,
1000000,..
10/12/22 Dr/ Ayman Soliman 19
5) Types of Time Complexity Equations
Ø The absolute best a data structure can “score” on each criteria is O(1)

§ No matter what the size of your data set is, the task will be completed in a
single instruction

Number of Operations
When we graph volume of data vs # of instructions required

# of operations remains constant

10/12/22 Dr/ Ayman Soliman 20


5) Types of Time Complexity Equations
Ø The next fastest type of time complexity equation is O(log n)

§ Still provides fast completion time

§ Gets more efficient as the size of the data set increases


Number of Operations

Number of Operations

Number of Operations
Slower Than Faster Than

10/12/22 Dr/ Ayman Soliman 21


5) Types of Time Complexity Equations
Ø The next fastest type of time complexity equation is O(log n)

§ Still provides fast completion time

§ Gets more efficient as the size of the data set increases


Number of Operations

Number of Operations

Number of Operations
Skyrockets Increases More Slowly Increases Way More Slowly

10/12/22 Dr/ Ayman Soliman 22


5) Types of Time Complexity Equations
Ø The next fastest type of time complexity equation is O(log n)

§ Still provides fast completion time

§ Gets more efficient as the size of the data set increases

10/12/22 Dr/ Ayman Soliman 23


5) Types of Time Complexity Equations
Ø The next fastest type of time complexity equation is O(log n)

§ Still provides fast completion time

§ Gets more efficient as the size of the data set increases

10/12/22 Dr/ Ayman Soliman 24


5) Types of Time Complexity Equations
Ø The next common time complexity efficiency equation type that’s going to
come up is O(n)

§ The last of the “decent” equations

Operations Required Size of Data Set (N)


Number of Operations

10 10
10/12/22 Dr/ Ayman Soliman 25
5) Types of Time Complexity Equations
Ø The next common time complexity efficiency equation type that’s going to
come up is O(n)

§ The last of the “decent” equations

Operations Required Size of Data Set (N)


Number of Operations

50 50
10/12/22 Dr/ Ayman Soliman 26
5) Types of Time Complexity Equations
Ø The next common time complexity efficiency equation type that’s going to
come up is O(n)

§ The last of the “decent” equations

Operations Required Size of Data Set (N)


Number of Operations

1000 1000
10/12/22 Dr/ Ayman Soliman 27
5) Types of Time Complexity Equations
Ø The next type of equation that will come up is O(n log n)

§ The first which is relatively bad in terms of efficiency

Number of Operations

10/12/22 Dr/ Ayman Soliman 28


5) Types of Time Complexity Equations
Ø The next type of equation that will come up is O(n log n)

§ The first which is relatively bad in terms of efficiency

Number of Operations
Low slope

10/12/22 Dr/ Ayman Soliman 29


5) Types of Time Complexity Equations
Ø The next type of equation that will come up is O(n log n)

§ The first which is relatively bad in terms of efficiency

Number of Operations
Slope Increases

10/12/22 Dr/ Ayman Soliman 30


5) Types of Time Complexity Equations
Ø The last 2 types of equations are O(n²) and O(2n)

§ Very bad in terms of efficiency

§ Exponential in structure
Number of Operations

Number of Operations
O(n )
2 O(2 )
n

10/12/22 Dr/ Ayman Soliman 31


6) Final Note on Time Complexity Equations
Ø Time Complexity Equations are NOT the only metric you should be using the
gauge which data structure to use

§ Some provide other functionality that make them extremely useful

e s s i ng Search
ing
A c c O
O(n) (log n)
Some Data
Other Functionality
Structure
Inserting Deleting
O(n2) O(n2 )

10/12/22 Dr/ Ayman Soliman 32


10/12/22 Dr/ Ayman Soliman 33

You might also like