foundations in data structure and algorithms truong tuan...

57
Foundations in Data Structure and Algorithms Truong Tuan Anh CSE-HCMUT

Upload: others

Post on 08-May-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

Foundations in Data Structure and Algorithms Truong Tuan Anh

CSE-HCMUT

Page 2: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

2

Outline

Basic conceptsRevision

Page 3: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

3

What is Data?

Page 4: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

4

What is Data?

DataData is information that has been translated into a form that ismore convenient to calculate and analyze.

ExampleNumbers, words, measurements, observations or descriptions

of things.

Qualitative data: descriptive informationQuantitative data: numerical information (numbers).

Discrete data: can only take certain values (like whole numbers)Continuous data: can take any value (within a range)

Page 5: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

5

Data Type

Class of data objects that havethe same properties.

Define a data type1. A set of values2. A set of operations on values

Example

Page 6: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

6

Data Structure

What is a data structure?1. A combination of elements in which each is either a

data type or another data structure2. A set of associations or relationships (structure)

that holds the data together

ExampleAn array is a number of elements of the same type in a specific order.

Page 7: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

7

Abstract Data Type

AimUsers know what a data type can do.How it is done is hidden.

DefinitionAn abstract data type is a data declaration packaged together with the operations that are meaningful for the

data type.

1. Declaration of data2. Declaration of operations3. Encapsulation of data and operations

Page 8: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

8

Abstract Data Type

Page 9: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

9

Example: List

InterfaceData: sequence of elements of a particular data typeOperations: accessing, insertion, deletion

ImplementationArrayLinked list

Page 10: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

10

Algorithm

What is an algorithm?The logical steps to solve a problem

What is a program?Program = Data structures + Algorithms

(Niklaus Wirth)

Page 11: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

11

Pseudocode

The most common tool to define algorithmsEnglish-like representation of the algorithm logic

Pseudocode = English + code

English: relaxed syntax being easy to readCode: instructions using basic control structures (sequential, conditional, iterative)

Page 12: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

12

Pseudocode

PseudocodeAlgorithm HeaderAlgorithm Body

Algorithm HeaderNameParameters and their typesPurpose: what the algorithm doesPrecondition: precursor requirements for the parametersPostcondition: taken action and status of the parametersReturn condition: returned value

Page 13: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

13

Pseudocode

PseudocodeAlgorithm HeaderAlgorithm Body

Algorithm BodyStatementsStatement numbers: decimal notation to express levelsVariables: important dataAlgorithm analysis: comments to explain salient pointsStatement constructs: sequence, selection, iteration

Page 14: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

14

Pseudocode: Example

Page 15: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

15

Data Structures

Page 16: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

16

Data Structures

Page 17: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

17

Data StructuresA member of an object can be accessed directly by a dot (.) inserted between the object name and the member name

Page 18: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

18

Data Structures

Page 19: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

19

Data Structures

Page 20: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

20

Exercise

Define a data structure student_t containing a student's name, first name, and age.

Write a code in C++ to take input your data and display it.

Page 21: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

21

Page 22: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

22

Class

Page 23: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

23

Class: Example

Page 24: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

24

Page 25: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

25

Class

Class ConstructorsAutomatically called whenever a new object of a class is created.Initializing member variables or allocate storage of the object.Declared with a name that matches the class name and without any return type; not even void.

Page 26: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

26

Page 27: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

27

Class

Member initialization:

Page 28: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

28

ArrayAn array is a series of elements of the same type placed in

contiguous memory locations that can be individually referenced by a unique identifier with an index.

Page 29: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

29

Class

Page 30: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

30

Algorithm Efficiency

A problem often has many algorithms/solutionsComparing two different algorithms

How fast an algorithm is?How much memory does it cost?

Compare by calculate the computational complexity: Measure of the difficulty degree (time and/or space) of an algorithm.

Page 31: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

31

Algorithm Efficiency

How to calculate the efficiency

efficiency = f(n)

n is the size of a problem (the key number that determines the size of input data)

Page 32: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

32

Linear Loop

for (i = 0; i < 1000; i++)//application code

The number of times the body of the loop is replicated is 1000f(n) = n

for (i = 0; i < 1000; i+=2)//application code

The number of times the body of the loop is replicated is 500f(n) = n/2

Page 33: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

33

Linear Loop

Page 34: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

34

Logarithmic Loops

The number of times the body of the loop is replicated is f(n) = log2 n

Page 35: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

35

Logarithmic Loops

Page 36: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

36

Nested Loop

Iterations = Outer loop iterations * Inner loop iterations

The number of times the body of the loop is replicated isf(n) = n*log2 n

Page 37: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

37

Nested Loop

Page 38: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

38

Quadratic Loop

The number of times the body of the loop is replicated isf(n) = n2

Page 39: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

39

Dependent Quadratic Loop

The number of times the body of the loop is replicated isf(n) = 1 + 2 + … + n = n*(n+1)/2

Page 40: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

40

Quadratic Loop

Page 41: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

41

Asymptotic Complexity

Algorithm efficiency is considered with only big problem sizesWe are not concerned with an exact measurement of an algorithm's efficiencyTerms that do not substantially change the function's magnitude are eliminated

Page 42: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

42

Big-O Notation

O(g(n)) = f(n) : ∃ positive constants c and n0,

such that ∀n ≥ n0, we have0 ≤ f(n) ≤ c*g(n)

For function g(n), we define O(g(n)), big-O of n, as the set:

g(n) is an asymptotic upper bound for f(n).

Intuitively: Set of all functions whose rate of growth is the same as or lower than that of g(n).

Page 43: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

43

Examplef(n) = 2n + 5g(n) = n

Consider the condition2n + 5 <= n

will this condition ever hold? No!

How about if we tack a constant to n?2n + 5 <= 3n

the condition holds for values of n greater than or equal to 5This means we can select c = 3 and n0 = 5

Page 44: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

44

2n+5

n

2n+53n

point where 3n“beats” 2n+5

2n+5 is O(n)

Example

Page 45: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

45

Big-O Notation

Set the coefficient of the term to 1Keep the largest term and discard the othersSome example of Big-O:

log2n, n, nlog2n, n2, nk, 2n, n!

Page 46: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

46

arrayMax Example

Algorithm arrayMax(A,n):Pre: An array A storing n integers.Post: The maximum element in A.currentMax ← A[0]

for i ← 1 to n - 1 do

if currentMax < A[i] then

currentMax ← A[i]

return currentMax

What is the running time of arrayMax?

Page 47: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

47

arrayMax Example

Depending on what operations we decide to count, running time function

f(n) = a*n + bRegardless, f(n) is O(n)Or, equivalently, the running time of algorithm arrayMax is O(n)

Page 48: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

48

Standard Measures of Efficiency

Assume instruction speed of 1 microsecond and 10 instructions inloop

n = 10000

Page 49: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

49

Standard Measures of Efficiency

Page 50: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

50

Big-O Analysis Example

Page 51: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

51

Big-O Analysis Example

Nested loop

Page 52: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

52

Big Omega Ω-notation

g(n) is an asymptotic lower bound for f(n).

Intuitively: Set of all functions whose rate of growth is the same as or higher than that of g(n).

Ω(g(n)) = f(n) : ∃ positive constants c and n0,such that ∀n ≥ n0, we have

0 ≤ c*g(n) ≤ f(n)

For function g(n), we define Ω(g(n)), big-Omega of n, as the set:

Page 53: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

53

Example

5n2 + 7 is Ω(?)

Page 54: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

54

Time Costing Operations

The most time consuming: data movement to/from memory/storageOperations under consideration:

ComparisonsArithmetic operationsAssignments

Page 55: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

55

Time Costing

Big-O can be used for categorizing or characterizing functionsFor example, the statements:

2n + 3 is O(n) and 5n is O(n)→ 2n + 3 and 5n are in the same category

→ If the functions are running times of two algorithms, the algorithms are thus comparable

Page 56: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

56

Read More

Big-ThetaLittle-OLittle-Omega

Page 57: Foundations in Data Structure and Algorithms Truong Tuan Anhanhtt/Slidesss/DataAlgo/DataAlgo-Intro.pdf · 4 What is Data? zData zData is information that has been translated into

57

Takeaways

Basic conceptsBasic data structuresAlgorithm complexity