50
Complexity www.tudorgirba.com

08 - Complexity

Embed Size (px)

DESCRIPTION

I used this set of slides for the lecture on Complexity I gave at the University of Zurich for the 1st year students following the course of Formale Grundlagen der Informatik.

Citation preview

Page 1: 08 - Complexity

Complexity

www.tudorgirba.com

Page 2: 08 - Complexity

computerinformation information

computation

Page 3: 08 - Complexity

Computation requires resources

memory

bandwidth

time

...

Page 4: 08 - Complexity
Page 5: 08 - Complexity

1 public long factorial (int n) {

2 long factorial = 1;

3 int i = 1;

4 while ( i ≤ n ) {

5 factorial = factorial * i;

6 i = i + 1;

7 }

8 return factorial

9 }

Page 6: 08 - Complexity

1 public long factorial (int n) {

2 long factorial = 1; 1

3 int i = 1; 1

4 while ( i ≤ n ) { 4

5 factorial = factorial * i; 4

6 i = i + 1; 3

7 }

8 return factorial 1

9 }

Page 7: 08 - Complexity

1 public long factorial (int n) {

2 long factorial = 1; 1 1

3 int i = 1; 1 1

4 while ( i ≤ n ) { 4 n

5 factorial = factorial * i; 4 n

6 i = i + 1; 3 n

7 }

8 return factorial 1 1

9 }

Page 8: 08 - Complexity

1 public long factorial (int n) {

2 long factorial = 1; 1 1

3 int i = 1; 1 1

4 while ( i ≤ n ) { 4 n

5 factorial = factorial * i; 4 n

6 i = i + 1; 3 n

7 }

8 return factorial 1 1

9 }

f(n) = 1+1+4*n+4*n+3*n+1 = 11*n+3

Page 9: 08 - Complexity

f: N -> Ng: N -> N

f ∈ O(g) ⇔ ∃ c, n0 : (c,n0∈N) ∧ (c>0) : (∀n : n∈N ∧ n>n0 : f(n) ≤ c*g(n))

Big O Notation

Page 10: 08 - Complexity

f: N -> Ng: N -> N

f ∈ O(g) ⇔ ∃ c, n0 : (c,n0∈N) ∧ (c>0) : (∀n : n∈N ∧ n>n0 : f(n) ≤ c*g(n))

f: N % N, f(n)=11*n+3.f ∈ O(n)

Big O Notation

Page 11: 08 - Complexity

O(1) Constant

O(log n) Logarithmic

O(n) Linear

O(n2) Quadratic

O(nk) Polynomial

O(kn) Exponential

exponentialquadraticlinearlogarithmicconstant

!"#$%$&'()("'*$+ ,'-.$/'--'*$%01.02

Page 12: 08 - Complexity

O(1) Constant

O(log n) Logarithmic

O(n) Linear

O(n2) Quadratic

O(nk) Polynomial

O(kn) Exponential

exponentialquadraticlinearlogarithmicconstant

!"#$%$&'()("'*$+ ,'-.$/'--'*$%01.02O(1) ∈ O(log n) ∈ O(n) ∈ O(n2) ∈ O(nk) ∈ O(2n)

Page 13: 08 - Complexity

N log N N log N N2 2N N!10 3 33 100 1024 2*106

20 4 86 400 106 2*1018

100 7 664 10’000 1031 10157

1’000 10 10’000 106

10’000 13 130’000 108

100’000 17 106 1010

1’000’000 1’000 2*107 1012

Page 14: 08 - Complexity

N log N N log N N2 2N N!10 3 33 100 1024 2*106

20 4 86 400 106 2*1018

100 7 664 10’000 1031 10157

1’000 10 10’000 106

10’000 13 130’000 108

100’000 17 106 1010

1’000’000 1’000 2*107 1012

102 s = 1.7 minutes104 s = 2.8 hours105 s = 1.1 days106 s = 1.6 weeks107 s = 3.8 months108 s = 3.1 years109 s = 3.1 decades1010 s = 3.1 centuries1011 s = never :)

Page 15: 08 - Complexity

O(c*f) = O(f)

O(f) + O(g) = O(f+g) = max{O(f),O(g)}

O(f*g) = O(f)*O(g)

Page 16: 08 - Complexity

O(c*f) = O(f)

O(f) + O(g) = O(f+g) = max{O(f),O(g)}

O(f*g) = O(f)*O(g)

O(f) ⊆ O(g) ⇔ f ∈ O(g)O(f) = O(g) ⇔ (O(f) ⊆ O(g)) ∧ (O(g) ⊆ O(f))O(f) ⊂ O(g) ⇔ (O(f) ⊆ O(g)) ∧ (O(g) ≠ O(f))

Page 17: 08 - Complexity

a b c d e f gabcdefg

0 2 3 0 0 0 00 0 1 0 0 0 00 0 0 2 0 0 00 0 0 0 5 4 00 0 0 0 0 0 30 0 0 0 0 0 30 0 0 0 0 0 0

1

3

22

5 3

34b

a

c d

e

f

g

Page 18: 08 - Complexity

a b c d e f gabcdefg

0 2 3 0 0 0 00 0 1 0 0 0 00 0 0 2 0 0 00 0 0 0 5 4 00 0 0 0 0 0 30 0 0 0 0 0 30 0 0 0 0 0 0

1

3

22

5 3

34b

a

c d

e

f

g

procedure FloydWarshall () for k := 1 to n for i := 1 to n for j := 1 to n path[i][j] = min ( path[i][j], path[i][k]+path[k][j] );

Page 19: 08 - Complexity
Page 20: 08 - Complexity

O(2*n-1) = O(n)

O(n*(n+1)/2) = O(n2)

O(log n2) = O(log n)

O((3*n2 + 6*n+9)*log(1+2*n)) = O(n2log(n))

Page 21: 08 - Complexity

O(2*n-1) = O(n)

O(n*(n+1)/2) = O(n2)

O(log n2) = O(log n)

O((3*n2 + 6*n+9)*log(1+2*n)) = O(n2log(n))

Page 22: 08 - Complexity

Example: Linear search

4 2 5 1 6 3

Page 23: 08 - Complexity

4 2 5 1 6 3

Example: Linear search

Page 24: 08 - Complexity

4 2 5 1 6 3

Example: Linear search

Page 25: 08 - Complexity

4 2 5 1 6 3

Worst case: n stepsBest case: 1 stepAverage case: n/2 steps

Example: Linear search

Page 26: 08 - Complexity

f: N -> Ng: N -> N

f ∈ Ω(g) ⇔ ∃ c, n0 : (c,n0∈N) ∧ (c>0) : (∀n : n∈N ∧ n>n0 : f(n) ≥ c*g(n))

Best case estimation

Page 27: 08 - Complexity

f: N -> Ng: N -> N

f ∈ Ω(g) ⇔ ∃ c, n0 : (c,n0∈N) ∧ (c>0) : (∀n : n∈N ∧ n>n0 : f(n) ≥ c*g(n))

Best case estimation

(3n2+6n+9)*log(1+2n) ≤ 36 n2 * log n(3n2+6n+9)*log(1+2n) ∈ O(n2 * log n)

Page 28: 08 - Complexity

f: N -> Ng: N -> N

f ∈ Ω(g) ⇔ ∃ c, n0 : (c,n0∈N) ∧ (c>0) : (∀n : n∈N ∧ n>n0 : f(n) ≥ c*g(n))

Best case estimation

(3n2+6n+9)*log(1+2n) ≤ 36 n2 * log n(3n2+6n+9)*log(1+2n) ∈ O(n2 * log n)

(3n2+6n+9)*log(1+2n) ≥ n2 * log n(3n2+6n+9)*log(1+2n) ∈ Ω(n2 * log n)

Page 29: 08 - Complexity

f: N -> Ng: N -> N

f ∈ θ(g) ⇔ ∃ c1, c2, n0 : (c1,c2,n0∈N) ∧ (c1>0) ∧ (c2>0): (∀n : n∈N ∧ n≥n0 : c1*g(n) ≤ f(n) ≤ c2*g(n))

Average case estimation

Page 30: 08 - Complexity

f: N -> Ng: N -> N

f ∈ θ(g) ⇔ ∃ c1, c2, n0 : (c1,c2,n0∈N) ∧ (c1>0) ∧ (c2>0): (∀n : n∈N ∧ n≥n0 : c1*g(n) ≤ f(n) ≤ c2*g(n))

Average case estimation

(3n2+6n+9)*log(1+2n) ∈ O(n2 * log n)

Page 31: 08 - Complexity

f: N -> Ng: N -> N

f ∈ θ(g) ⇔ ∃ c1, c2, n0 : (c1,c2,n0∈N) ∧ (c1>0) ∧ (c2>0): (∀n : n∈N ∧ n≥n0 : c1*g(n) ≤ f(n) ≤ c2*g(n))

Average case estimation

(3n2+6n+9)*log(1+2n) ∈ O(n2 * log n)

(3n2+6n+9)*log(1+2n) ∈ Ω(n2 * log n)

Page 32: 08 - Complexity

f: N -> Ng: N -> N

f ∈ θ(g) ⇔ ∃ c1, c2, n0 : (c1,c2,n0∈N) ∧ (c1>0) ∧ (c2>0): (∀n : n∈N ∧ n≥n0 : c1*g(n) ≤ f(n) ≤ c2*g(n))

Average case estimation

(3n2+6n+9)*log(1+2n) ∈ O(n2 * log n)

(3n2+6n+9)*log(1+2n) ∈ Ω(n2 * log n)

(3n2+6n+9)*log(1+2n) ∈ θ(n2 * log n)

Page 33: 08 - Complexity

boolean f ( int[][] a , int n ) { for ( int i = 0 ; i < n ; i++ ) { for ( int j = i + 1 ; j < n ; j++ ) { if ( a[i][j] == 0 ) {return false;} } return true; }}

Example

Page 34: 08 - Complexity

boolean f ( int[][] a , int n ) { for ( int i = 0 ; i < n ; i++ ) { for ( int j = i + 1 ; j < n ; j++ ) { if ( a[i][j] == 0 ) {return false;} } return true; }}

f ∈ O(n2)f ∈ Ω(1)f ∈ θ(n2)

Example

Page 35: 08 - Complexity

1

2

3

4

5

6

Example: Binary search

Page 36: 08 - Complexity

1

2

3

4

5

6

Example: Binary search

f ∈ O(log n)f ∈ Ω(1)f ∈ θ(log n)

Page 37: 08 - Complexity

solvablein O(nk)

Page 38: 08 - Complexity

Psolvablein O(nk)

Page 39: 08 - Complexity

P NPsolvablein O(nk)

Page 40: 08 - Complexity

P NPsolvablein O(nk)

verifiablein O(nk)

Page 41: 08 - Complexity

P NP⊂solvablein O(nk)

verifiablein O(nk)

Page 42: 08 - Complexity

P NP=?solvablein O(nk)

verifiablein O(nk)

Page 43: 08 - Complexity

NP-completeNP, and one cannot do better

Page 44: 08 - Complexity

NP-complete problem:

Hamiltonian path

b

a

c d

e

f

g

Hamiltonian path

Page 45: 08 - Complexity

NP-complete problem:

Traveling salesman

Page 46: 08 - Complexity

NP-complete problem:

Graph coloring

Page 47: 08 - Complexity

NP-complete problem:

Subset sum

S={x1, … , xn}t ∈ N∃ {y1, … , yk} ⊆ S : Σyi=t ?

Page 48: 08 - Complexity

NP-complete problem:

Subset sum

S={x1, … , xn}t ∈ N∃ {y1, … , yk} ⊆ S : Σyi=t ?

S= {8, 11, 16, 29, 37}t = 37

{11, 16}{8, 29}{37}

Page 49: 08 - Complexity

The halting problem

is undecidable

Will this program terminate?