DATA STRUCTURES AND ALGORITHMS - Computer...

41
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by Đnanç TAHRALI

Transcript of DATA STRUCTURES AND ALGORITHMS - Computer...

Page 1: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

DATA STRUCTURES AND

ALGORITHMS

Lecture Notes 2

Prepared by Đnanç TAHRALI

Page 2: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

2

Recapture

Asymptotic Notations

O Notation

Ω Notation

Θ Notation

o Notation

Page 3: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

3

Big Oh Notation (O)

Provides an “upper bound” for the function f

Definition : T(N) = O (f(N)) if there are positive constants

c and n0 such that

T(N) ≤ c f(N) when N ≥ n0

T(N) grows no faster than f(N)

growth rate of T(N) is less than or equal to growth rate of f(N) for large N

f(N) is an upper bound on T(N) not fully correct !

Page 4: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

4

Omega Notation (Ω)

Definition :

T(N) = Ω (f(N)) if there are positive constants

c and n0 such that T(N) ≥ c f(N) when N≥ n0

T(N) grows no slower than f(N)

growth rate of T(N) is greater than or equal to growth rate of f(N) for large N

f(N) is a lower bound on T(N)

not fully correct !

Page 5: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

5

Theta Notation (θ)

Definition :

T(N) = θ (h(N)) if and only if

T(N) = O(h(N)) and T(N) = Ω(h(N))

T(N) grows as fast as h(N)

growth rate of T(N) and h(N) are equal for large N

h(N) is a tight bound on T(N)

not fully correct !

Page 6: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

6

Little o Notation (o)

Definition :

T(N) = o(p(N)) if

T(N) = O(p(N)) and T(N)≠θ(p(N))

p(N) grows strictly faster than T(N)

growth rate of T(N) is less than the growth rate of p(N) for large N

p(N) is an upperbound on T(N) (but not tight)

not fully correct !

Page 7: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

7

ROAD MAP

Model

What to Analyze ?

Running Time Analysis

General Rules

Recursive Calls

Maximum Subsequence Sum Problem

Binary Search

Experimentally checking analysis

Page 8: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

8

MODEL

A formal framework for analysis (simplify the real computers)

There are many models

Automata

Turing Machine

RAM

Page 9: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

9

MODEL

We use RAM model (normal computer)

addition

multiplication take a unit time

comparison

assignment

fixed size word (32 bit)

no complicated operation supported

requires an algorithm (algorithm may not take unit time)

Page 10: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

10

What to Analyze ?

Running Time (most important !)

Required memory space

Run-time complexity is effected by

compiler

usually effects the constants &

computer lower order terms

algorithm use asymptotic notation

Page 11: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

11

Running Time Analysis

Emprical after implementation

Theoritical before implementation

If there are many algorithms ideas, we need to evaluate them without implementation

We will use 0-notation drop constants

ignore lower order terms

Page 12: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

12

Running Time Analysis

Example:

int sum (int N)

int i, partialsum; does not countpartialsum = 0; 1for (i=1; i<=N; i++) 1+(N+1)+N

partialsum+=i*i*i; 3Nreturn partialsum; 1

= 5N + 4= θ(N)

Page 13: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

13

ROAD MAP

Model

What to Analyze ?

Running Time Analysis

General Rules

Recursive Calls

Maximum Subsequence Sum Problem

Binary Search

Experimentally checking analysis

Page 14: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

14

GENERAL RULES

RULE 1 : For LoopsThe running time of a for loop is at most the running time of the statements in the for loop times the number of iterations

int i, a = 0;

for (i=0; i<n; i++)

print i;

a=a+i;

return i;

T(n) = θ(n)

Page 15: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

15

GENERAL RULES

RULE 2 : Nested LoopsAnalyze nested loops inside out

Example :

for (int i=1;i<=q;i++)

for (int j=1;j<=r;j++)

k++;

θ(r) θ(q)

T(n) = θ(r*q)

Page 16: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

16

GENERAL RULES

RULE 3 : Consequtive StatementsAdd the running times

for … θ(N)

…;

for … θ(N2)

for … θ(N2)

…;

Page 17: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

17

GENERAL RULES

RULE 4 : If / Else

if (condition) T3(n) S1; T1(n) T(n)

elseS2; T2(n)

Running time is never more than the running time of the test plus larger of the running times of S1 and S2

(may overestimate but never underestimates)

T(n) ≤ T3(n) + max (T1(n), T2(n))

Page 18: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

18

Types of complexition

)(min)(||

ITNTNI

best ==

∑=

→=NI

av computetodifficultIITNT||

)Pr().()(

usedusuallyITNTNI

worst →==

)(max)(||

)()()( NTNTNT bestavworst ≥≥

))(())(()( nTnTOnT bestworst Ω==

Page 19: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

19

GENERAL RULES

RULE 4 : If / Elseif (condition) T3(n)

S1; T1(n) T(n)else

S2; T2(n)

Tw (n) = T3(n) + max (T1(n), T2(n)) Tb (n) = T3(n) + min (T1(n), T2(n)) Tav (n) = p(T)T1(n) + p(F)T2(n) + T3(n)

p(T) p (condition = True)p(F) p (condition = False)

Page 20: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

20

GENERAL RULES

Example :

if (condition) T3(n) = θ(n) S1; T1(n) = θ(n2) T(n)

elseS2; T2(n) = θ(n)

Tw (n) = T3 (n) + max (T1 (n) , T2 (n)) = θ(n2)Tb (n) = T3 (n) + min (T1 (n), T2 (n)) = θ(n)

if p(T) = p(F) = ½Tav (n) = p(T)T1 (n) + p(F)T2 (n) + T3 (n)= θ(n2)

T(n) = O (n2) = Ω (n)

Page 21: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

21

ROAD MAP

Model

What to Analyze ?

Running Time Analysis

General Rules

Recursive Calls

Maximum Subsequence Sum Problem

Binary Search

Experimentally checking analysis

Page 22: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

22

RECURSIVE CALLS

Example 1:Algorithm for computing factorial

int factorial (int n)

if (n<=1)

return 1;else

return n*factorial(n- 1);

T(n) = cost of evaluation of factorial of nT(n) = 4 + T(n-1)T(1) = 2

1 for multiplication+ 1 for substraction+ cost of evaluation of factorial(n-1)

Page 23: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

23

RECURSIVE CALLS

T(n) = 4 + T(n-1)

T(n) = 4 + 4+ T(n-2)

T(n) = 4 + 4 + 4 + T(n-3)...

T(n) = k*4 + T(n-k) k= n-1 =>

T(n) = (n-1)*4 + T(n-(n-1))

T(n) = (n-1)*4 + T(1)

T(n) = (n-1)*4 + 2

T(n) = θ (n)

Page 24: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

24

RECURSIVE CALLS

Example 2:Algorithm for fibonacci seriesFib (int N)if (N<=1)

return 1;else

return Fib(N-1)+Fib(N-2);

T(N) = T(N-1) + T(N-2) + 4 , T(1)=T(0)=2

=> T(N) = O(2n) (by induction)

Page 25: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

25

ROAD MAP

Model

What to Analyze ?

Running Time Analysis

General Rules

Recursive Calls

Maximum Subsequence Sum Problem

Binary Search

Experimentally checking analysis

Page 26: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

26

MAXIMUM SUBSEQUENCE SUM PROBLEM

Given (possibly negative) integers A1, A2,…, AN, find

max

1 ≤ i ≤ j ≤ N

-2 11 -4 13 -5 -2

A[2,4] sum = 20

A[2,5] sum = 15

There are many algorithms to solve this problem !

=

j

ikkA

Page 27: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

27

MAXIMUM SUBSEQUENCE SUM PROBLEM

for small N all algorithms are fast

for large N O(N) is the best

Page 28: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

28

MAXIMUM SUBSEQUENCE SUM PROBLEM

1) Try all possibilities exhaustively

for each i (0 to N-1)

for each j (i to N-1)

compute the sum of subsequence for (i to j)

check if it is maximum

T(N) = O(N3)

Page 29: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

29

Algorithm 1:

© Mark Allen Weiss, Data Structures and Algorithm Analysis in C

Page 30: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

30

MAXIMUM SUBSEQUENCE SUM PROBLEM

2) How to compute sum of a subsequence

We can use previous subsequence sum to calculate current one in O(1) time

T(N) = O(N2)

∑∑−

==

+=1j

ikkj

j

ikk AAA

Page 31: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

31

Algorithm 2:

© Mark Allen Weiss, Data Structures and Algorithm Analysis in C

Page 32: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

32

MAXIMUM SUBSEQUENCE SUM PROBLEM

3) Compeletely different algorithm ? Divide and Conquer Strategy

Maximum subsequence can be in L

solved recursively in R

in the middle, in both sideslargest sum in L ending with middle element+largest sum in R begining with middle element

Page 33: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

33© Mark Allen Weiss, Data Structures and Algorithm Analysis in C

Page 34: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

34

Algorithm 4:

© Mark Allen Weiss, Data Structures and Algorithm Analysis in C

Page 35: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

35

ROAD MAP

Model

What to Analyze ?

Running Time Analysis

General Rules

Recursive Calls

Maximum Subsequence Sum Problem

Binary Search

Experimentally checking analysis

Page 36: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

36

Binary Search

© Mark Allen Weiss, Data Structures and Algorithm Analysis in C

Page 37: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

37

ROAD MAP

Model

What to Analyze ?

Running Time Analysis

General Rules

Recursive Calls

Maximum Subsequence Sum Problem

Binary Search

Checking your analysis experimentally

Page 38: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

38

Checking Your Analysis Experimentally

Implement your algorithm, measure the running time of your implementation check with theoretical results

When N doubles (2x) linear => 2x quadratic => 4x cubic => 8x

what about logarithm ? O(N) O(N logN)not easy to see the difference !

Page 39: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

39

Checking Your Analysis Experimentally

Tt (N) = theoretical running timeTe (N) = experimental running time

Let Tt(N) = O(f(N))

compute

if converges to a constantf(N) is a tight bound

if converges to zeronot tight bound (overestimate)

if divergesunderestimate

)(

)(

)(

)(

Nf

NT

NT

NT e

t

e =

Page 40: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

40

Checking Your Analysis Experimentally

Te /TtTeTtN

Page 41: DATA STRUCTURES AND ALGORITHMS - Computer Sciencecobweb.cs.uga.edu/~jam/home/courses/csci2720/lecture2.pdf3 Big Oh Notation (O) Provides an “upper bound” for the function f Definition

41

Checking Your Analysis Experimentally