You are on page 1of 7

Content

• Introduction
• Linear data structure
DATA STRUCTURE • List, Stack, Queue
• Nonlinear data structure
• Tree, Hash Table, Heap
INTRODUCTION

Phạm Nguyên Khang


pnkhang@cit.ctu.edu.vn

CAN THO, 7/7/2022


1

Introduction Introduction
• What is a Data Structure? • What is a Data Structure? Real
• A way of organizing data so that it can be used world
effectively. data

Data structure

Basic Data Type

2 3
Introduction Introduction
5
• What is a Data Structure? Real
• Why data structure?
world
7 • Essential ingredients in creating fast powerful
data Fraction (Real algorithms (programs)
world) • Help to manage and organize data (in a very nature
Fraction (Data structure) way)
• Make code cleaner and easier to understand
denom: int

nominator: int
Data structure
• Data structure can make difference between having
an okay product and an outstanding one.

Basic Data Type int, char, float

4 5

Introduction Introduction
• Abstract Data Types vs Data Structures • Example
• An abstract Data Type (ADT) is an abstraction of a • Mode of transportation to get from point A to point
data structure which provides only the interface to B
which a data structure must adhere to.
• There are many specific modes of transportation to
• The interface does not give any specific details about get from point A to point B, such as: walking,
how something should be implemented or in what biking, … Which one do you choose?
programming language.

6 7
Introduction Introduction
• Examples • How to build a data structure?
• Basic building blocks: primitive data type (int, …)
Abstraction (ADT) Implementation (Data structure) • Grouping methods: create a complex block from
List makes Q be an empty set simpler ones
• Index-based: Array (static or dynamic)
• Name-based: Structure (e.g., struct in C)
Queue Linked List Based Queue • Linking mechanism: Pointer, Cursor
Array based Queue
Stack based Queue
Vehicle Golf Cart
Bicycle

Smart Ca

8 9

Computational Complexity
Introduction
Analysis
• Quiz • Two important questions of programmers:
• Create a data structure (in C language) to store a 2D • How much time does this algorithm (program) need
triangle defined by its three vertexes. to finish?
• How much space does this algorithm (program) need
for its computation?

10 11
Computational Complexity Computational Complexity
Analysis Analysis
• Big-Oh notation: O(f(n)) • Complexities ordered in from smallest to largest
• Upper bound of the complexity in the worst case, where n is the input size:
helping to quantify performance as the input size • Constant: O(1)
become arbitrarily large. • Logarithmic: O(log(n))
• Ex: 3n2 + n + 4 = O(n2) • Linear: O(n)
• Linearithmic: O(nlog(n))
• Quadric: O(n2)
• Cubic: O(n3)
• Exponential: O(bn), b>1
• Factorial: O(n!)

12 13

Computational Complexity Computational Complexity


Analysis Analysis
• Big-Oh notation: O(f(n)) a = 4; i = 0;
• c = O(1) where c is a constant b = 2; while (i < 11)
• O(n + c) = O(n) c = a + 7*b + 2;
• O(cn) = O(n), c > 0 i = i + 1;
• O(cf(n)) = O(f(n)), c > 0
• Addition rule:
• T1(n) = O(f1(n)) and T2(n) = O(f2(n)) then T(n) = ? T(n) = ?
• T1(n) + T2(n) = O(max(f1(n), f2(n)))
• Ex:
f(n) = 7log(n)3 + 15n2 + 2n3 + 8
= O(n3)

14 15
Computational Complexity Computational Complexity
Analysis Analysis
a = 4; i = 0; i = 0; i = 0;
b = 2; while (i < 11) while (i < n) while (i < n)
c = a + 7*b + 2; i = i + 1;
i = i + 1; i = i + 3;

T(n) = O(1) T(n) = O(1) T(n) = ? T(n) = ?

16 17

Computational Complexity Computational Complexity


Analysis Analysis
i = 0; i = 0; for (i = 0; i < n; i++)
while (i < n) while (i < n) for (j = 0; j < n; j++)
i = i + 1; cnt += 1;
i = i + 3;
T(n) = ?
T(n) = n = O(n) T(n) = n/3 = O(n)

18 19
Computational Complexity Computational Complexity
Analysis Analysis
for (i = 0; i < n; i++) for (i = 1; i <= n; i++)
for (j = 0; j < n; j++) for (j = 1; j <= i; j++)
cnt += 1; cnt += 1;

T(n) = n2 = O(n2) T(n) = ?

20 21

Computational Complexity Computational Complexity


Analysis Analysis
for (i = 1; i <= n; i++) Quiz:
for (j = 1; j <= i; j++)
cnt += 1; for (i = 1; i <= n*n; i++)
for (j = 1; j <= 2*n; j++)
T(n) = 1 + 2 + … + (n-1) + n cnt += 1;
= n(n+1)/2 = n2/2 + n/2
= O(n2) for (j = 1; j <= 100*n*n; j++)
cnt += 1;

T(n) =?

22 23
THANK YOU

24

You might also like