SlideShare una empresa de Scribd logo
1 de 50
Descargar para leer sin conexión
Complexity



         www.tudorgirba.com
computation



information                 information
              computer
memory

                                 bandwidth
Computation requires resources
                                 time

                                 ...
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 }
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 }
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 }
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
Big O Notation

f: N -> N
g: 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

f: N -> N
g: 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)
!"#$%$&'()("'*$+ ,'-.$/'--
O(1)      Constant


O(log n) Logarithmic


O(n)      Linear


O(n2)     Quadratic


O(nk)     Polynomial


O(kn)     Exponential
O(1) ∈ O(log n) ∈ O(n) ∈ O(n2) ∈ O(nk) ∈ O(2n)

                        !"#$%$&'()("'*$+ ,'-.$/'--
O(1)      Constant


O(log n) Logarithmic


O(n)      Linear


O(n2)     Quadratic


O(nk)     Polynomial


O(kn)     Exponential
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
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 2 s =
                                                     1.
 10’000      13     130’000    108         10 4 s = 7 minutes
                                                    2.
                                          10 5 s = 8 hours
                                                    1.1 day
 100’000     17       106      1010
                                         10 s =
                                              6               s
                                                   1.6 we
                                         10 s =
                                             7              eks
                                                   3.8 mo
                                        10 s =
                                            8               nths
1’000’000   1’000    2*107     1012               3.1 yea
                                        10 s =
                                           9                rs
                                                  3.1 dec
                                       10 10 s              a
                                                = 3.1 c des
                                       10 11 s          enturie
                                                = neve          s
                                                       r :)
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(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))
a b c d e f g
                                    a 0 2 3 0 0 0 0
a                       e           b 0 0 1 0 0 0 0
    3               5       3
            2                       c 0 0 0 2 0 0 0
2       c       d               g
                                    d 0 0 0 0 5 4 0
    1               4       3       e 0 0 0 0 0 0 3
b                       f
                                    f 0 0 0 0 0 0 3
                                    g 0 0 0 0 0 0 0
a b c d e f g
                                        a 0 2 3 0 0 0 0
 a                       e              b 0 0 1 0 0 0 0
     3               5       3
             2                          c 0 0 0 2 0 0 0
2        c       d               g
                                        d 0 0 0 0 5 4 0
     1               4       3          e 0 0 0 0 0 0 3
 b                       f
                                        f 0 0 0 0 0 0 3
                                        g 0 0 0 0 0 0 0

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] );
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))
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))
4   2   5   1    6            3




                                         ch
                           : Line ar sear
                E xample
4   2   5   1    6            3




                                         ch
                           : Line ar sear
                E xample
4   2   5   1    6            3




                                         ch
                           : Line ar sear
                E xample
Worst case: 	 	 n steps
Best case:	 	 1 step
Average case:	 n/2 steps



  4        2       5       1    6            3




                                                        ch
                                          : Line ar sear
                               E xample
Best case estimation

f: N -> N
g: N -> N

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

f: N -> N
g: N -> N

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


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

f: N -> N
g: N -> N

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


(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)
Average case estimation

f: N -> N
g: N -> N

f ∈ θ(g)
	 ∃ c 1, c 2, n 0 :
	 (c1,c2,n0∈N) ∧ (c1>0) ∧ (c2>0):
	 (∀n : n∈N ∧ n≥n0 : c1*g(n) ≤ f(n) ≤ c2*g(n))
Average case estimation

f: N -> N
g: N -> N

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



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

f: N -> N
g: N -> N

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



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

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

f: N -> N
g: N -> N

f ∈ θ(g)
	 ∃ c 1, c 2, n 0 :
	 (c1,c2,n0∈N) ∧ (c1>0) ∧ (c2>0):
	 (∀n : n∈N ∧ n≥n0 : c1*g(n) ≤ f(n) ≤ c2*g(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)
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;
	 }
}




                                   E xample
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)
                                   E xample
4



    2                          6



1       3             5



                                  ch
                    : Bina ry sear
                 le
            Examp
f ∈ O(log n)
f ∈ Ω(1)
f ∈ θ(log n)


                   4



        2                             6



 1             3             5



                                         ch
                           : Bina ry sear
                        le
                   Examp
solvable
 in O(nk)
solvable
 in O(nk)   P
solvable
 in O(nk)   P   NP
solvable
 in O(nk)   P   NP   verifiable
                     in O(nk)
solvable
 in O(nk)   P ⊂ NP   verifiable
                     in O(nk)
solvable
 in O(nk)   P = NP
              ?      verifiable
                     in O(nk)
NP-complete
 NP, and one cannot do better
Hamiltonian path


a                  e

     c      d               g

b                  f




                                           :
                                  p roblem
                            mplete path
                       NP-co onian
                         Hamilt
:
               p roblem
NP-co mplete sman
                le
  Trave ling sa
:
           p roblem
     mplete ing
NP-co h color
   Grap
S={x1, … , xn}
t ∈ N
∃ {y1, … , yk} ⊆ S : Σyi=t ?




                                                 :
                                        p roblem
                           NP-co mplete m
                                          u
                                S ubset s
S={x1, … , xn}
t ∈ N
∃ {y1, … , yk} ⊆ S : Σyi=t ?




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

{11, 16}
{8, 29}
{37}
                                                 :
                                        p roblem
                           NP-co mplete m
                                          u
                                S ubset s
Will this program terminate?




                                g problem
                     Th e haltin dable
                                 ci
                         is unde
Tudor Gîrba
        www.tudorgirba.com




creativecommons.org/licenses/by/3.0/

Más contenido relacionado

La actualidad más candente

Verifying Identities
Verifying IdentitiesVerifying Identities
Verifying Identities
bwlomas
 
Maths assignment
Maths assignmentMaths assignment
Maths assignment
Ntshima
 
Hw2sol
Hw2solHw2sol
Hw2sol
uxxdqq
 
ฟังก์ชัน(function)
ฟังก์ชัน(function)ฟังก์ชัน(function)
ฟังก์ชัน(function)
Yodhathai Reesrikom
 
June 2011 solution
June 2011 solutionJune 2011 solution
June 2011 solution
leroy walker
 

La actualidad más candente (20)

Capitulo 2 corripio
Capitulo 2 corripioCapitulo 2 corripio
Capitulo 2 corripio
 
corripio
corripio corripio
corripio
 
Lesson 15: The Chain Rule
Lesson 15: The Chain RuleLesson 15: The Chain Rule
Lesson 15: The Chain Rule
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
Deber10
Deber10Deber10
Deber10
 
13.30 o2 v bubanja
13.30 o2 v bubanja13.30 o2 v bubanja
13.30 o2 v bubanja
 
Verifying Identities
Verifying IdentitiesVerifying Identities
Verifying Identities
 
Maths assignment
Maths assignmentMaths assignment
Maths assignment
 
Week 10 - Trigonometry
Week 10 - TrigonometryWeek 10 - Trigonometry
Week 10 - Trigonometry
 
ฟังก์ชัน 1
ฟังก์ชัน 1ฟังก์ชัน 1
ฟังก์ชัน 1
 
Hw2sol
Hw2solHw2sol
Hw2sol
 
The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189
 
The Ring programming language version 1.5.4 book - Part 60 of 185
The Ring programming language version 1.5.4 book - Part 60 of 185The Ring programming language version 1.5.4 book - Part 60 of 185
The Ring programming language version 1.5.4 book - Part 60 of 185
 
Integrated exercise a_(book_2_B)_Ans
Integrated exercise a_(book_2_B)_AnsIntegrated exercise a_(book_2_B)_Ans
Integrated exercise a_(book_2_B)_Ans
 
[ACM-ICPC] Sort
[ACM-ICPC] Sort[ACM-ICPC] Sort
[ACM-ICPC] Sort
 
ฟังก์ชัน(function)
ฟังก์ชัน(function)ฟังก์ชัน(function)
ฟังก์ชัน(function)
 
The Ring programming language version 1.5.1 book - Part 51 of 180
The Ring programming language version 1.5.1 book - Part 51 of 180The Ring programming language version 1.5.1 book - Part 51 of 180
The Ring programming language version 1.5.1 book - Part 51 of 180
 
June 2011 solution
June 2011 solutionJune 2011 solution
June 2011 solution
 
Math IA
Math IAMath IA
Math IA
 
Ky-thuat-bien-doi-vi-phan-de-tim-nguyen-ham
Ky-thuat-bien-doi-vi-phan-de-tim-nguyen-hamKy-thuat-bien-doi-vi-phan-de-tim-nguyen-ham
Ky-thuat-bien-doi-vi-phan-de-tim-nguyen-ham
 

Similar a 08 - Complexity

4th Period Review(Carta)With Answers
4th Period Review(Carta)With Answers4th Period Review(Carta)With Answers
4th Period Review(Carta)With Answers
beremontalvo
 
TMUA 2021 Paper 1 Solutions (Handwritten).pdf
TMUA 2021 Paper 1 Solutions (Handwritten).pdfTMUA 2021 Paper 1 Solutions (Handwritten).pdf
TMUA 2021 Paper 1 Solutions (Handwritten).pdf
ssuser625c41
 
ใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าา
ใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าาใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าา
ใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าา
Fern Leelasittikul
 
ใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าา
ใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าาใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าา
ใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าา
Fern Leelasittikul
 
Question 1. a) (i)(4+i2).(1+i3)4(1+i3)+i2(1+i3)4+i12+i2-6.docx
Question 1.  a) (i)(4+i2).(1+i3)4(1+i3)+i2(1+i3)4+i12+i2-6.docxQuestion 1.  a) (i)(4+i2).(1+i3)4(1+i3)+i2(1+i3)4+i12+i2-6.docx
Question 1. a) (i)(4+i2).(1+i3)4(1+i3)+i2(1+i3)4+i12+i2-6.docx
IRESH3
 
ใบความรู้ฟิสิกส์
ใบความรู้ฟิสิกส์ใบความรู้ฟิสิกส์
ใบความรู้ฟิสิกส์
Fern Leelasittikul
 
Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)
asghar123456
 

Similar a 08 - Complexity (20)

Time complexity
Time complexityTime complexity
Time complexity
 
Sk7 ph
Sk7 phSk7 ph
Sk7 ph
 
Sk7 ph
Sk7 phSk7 ph
Sk7 ph
 
Prelude to halide_public
Prelude to halide_publicPrelude to halide_public
Prelude to halide_public
 
4th Period Review(Carta)With Answers
4th Period Review(Carta)With Answers4th Period Review(Carta)With Answers
4th Period Review(Carta)With Answers
 
TMUA 2021 Paper 1 Solutions (Handwritten).pdf
TMUA 2021 Paper 1 Solutions (Handwritten).pdfTMUA 2021 Paper 1 Solutions (Handwritten).pdf
TMUA 2021 Paper 1 Solutions (Handwritten).pdf
 
ใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าา
ใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าาใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าา
ใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าา
 
Ch15s
Ch15sCh15s
Ch15s
 
ใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าา
ใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าาใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าา
ใบความรู้ฟิสิกส์ เสดพร้อมส่ง แสงจ้าา
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
 
Maths 301 key_sem_1_2007_2008
Maths 301 key_sem_1_2007_2008Maths 301 key_sem_1_2007_2008
Maths 301 key_sem_1_2007_2008
 
Question 1. a) (i)(4+i2).(1+i3)4(1+i3)+i2(1+i3)4+i12+i2-6.docx
Question 1.  a) (i)(4+i2).(1+i3)4(1+i3)+i2(1+i3)4+i12+i2-6.docxQuestion 1.  a) (i)(4+i2).(1+i3)4(1+i3)+i2(1+i3)4+i12+i2-6.docx
Question 1. a) (i)(4+i2).(1+i3)4(1+i3)+i2(1+i3)4+i12+i2-6.docx
 
Hydrologic analysis and design 4th edition mc cuen solutions manual
Hydrologic analysis and design 4th edition mc cuen solutions manualHydrologic analysis and design 4th edition mc cuen solutions manual
Hydrologic analysis and design 4th edition mc cuen solutions manual
 
ใบความรู้ฟิสิกส์
ใบความรู้ฟิสิกส์ใบความรู้ฟิสิกส์
ใบความรู้ฟิสิกส์
 
Resposta cap-1-halliday-8-edição
Resposta cap-1-halliday-8-ediçãoResposta cap-1-halliday-8-edição
Resposta cap-1-halliday-8-edição
 
A common random fixed point theorem for rational ineqality in hilbert space ...
 A common random fixed point theorem for rational ineqality in hilbert space ... A common random fixed point theorem for rational ineqality in hilbert space ...
A common random fixed point theorem for rational ineqality in hilbert space ...
 
Math11
Math11Math11
Math11
 
4th Semester (June; July-2015) Computer Science and Information Science Engin...
4th Semester (June; July-2015) Computer Science and Information Science Engin...4th Semester (June; July-2015) Computer Science and Information Science Engin...
4th Semester (June; July-2015) Computer Science and Information Science Engin...
 
Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)
 
Cálculo ii howard anton - capítulo 16 [tópicos do cálculo vetorial]
Cálculo ii   howard anton - capítulo 16 [tópicos do cálculo vetorial]Cálculo ii   howard anton - capítulo 16 [tópicos do cálculo vetorial]
Cálculo ii howard anton - capítulo 16 [tópicos do cálculo vetorial]
 

Más de Tudor Girba

Más de Tudor Girba (20)

Beyond software evolution: Software environmentalism
Beyond software evolution: Software environmentalismBeyond software evolution: Software environmentalism
Beyond software evolution: Software environmentalism
 
Software craftsmanship meetup (Zurich 2015) on solving real problems without ...
Software craftsmanship meetup (Zurich 2015) on solving real problems without ...Software craftsmanship meetup (Zurich 2015) on solving real problems without ...
Software craftsmanship meetup (Zurich 2015) on solving real problems without ...
 
GT Spotter
GT SpotterGT Spotter
GT Spotter
 
Don't demo facts. Demo stories! (handouts)
Don't demo facts. Demo stories! (handouts)Don't demo facts. Demo stories! (handouts)
Don't demo facts. Demo stories! (handouts)
 
Don't demo facts. Demo stories!
Don't demo facts. Demo stories!Don't demo facts. Demo stories!
Don't demo facts. Demo stories!
 
Humane assessment on cards
Humane assessment on cardsHumane assessment on cards
Humane assessment on cards
 
Underneath Scrum: Reflective Thinking
Underneath Scrum: Reflective ThinkingUnderneath Scrum: Reflective Thinking
Underneath Scrum: Reflective Thinking
 
1800+ TED talks later
1800+ TED talks later1800+ TED talks later
1800+ TED talks later
 
Software assessment by example (lecture at the University of Bern)
Software assessment by example (lecture at the University of Bern)Software assessment by example (lecture at the University of Bern)
Software assessment by example (lecture at the University of Bern)
 
Humane assessment: Taming the elephant from the development room
Humane assessment: Taming the elephant from the development roomHumane assessment: Taming the elephant from the development room
Humane assessment: Taming the elephant from the development room
 
Moose: how to solve real problems without reading code
Moose: how to solve real problems without reading codeMoose: how to solve real problems without reading code
Moose: how to solve real problems without reading code
 
Software Environmentalism (ECOOP 2014 Keynote)
Software Environmentalism (ECOOP 2014 Keynote)Software Environmentalism (ECOOP 2014 Keynote)
Software Environmentalism (ECOOP 2014 Keynote)
 
The emergent nature of software systems
The emergent nature of software systemsThe emergent nature of software systems
The emergent nature of software systems
 
Presenting is storytelling at Uni Zurich - slides (2014-03-05)
Presenting is storytelling at Uni Zurich - slides (2014-03-05)Presenting is storytelling at Uni Zurich - slides (2014-03-05)
Presenting is storytelling at Uni Zurich - slides (2014-03-05)
 
Presenting is storytelling at Uni Zurich - handouts (2014-03-05)
Presenting is storytelling at Uni Zurich - handouts (2014-03-05)Presenting is storytelling at Uni Zurich - handouts (2014-03-05)
Presenting is storytelling at Uni Zurich - handouts (2014-03-05)
 
Underneath Scrum: Reflective Thinking (talk at Scrum Breakfast Bern, 2013)
Underneath Scrum: Reflective Thinking (talk at Scrum Breakfast Bern, 2013)Underneath Scrum: Reflective Thinking (talk at Scrum Breakfast Bern, 2013)
Underneath Scrum: Reflective Thinking (talk at Scrum Breakfast Bern, 2013)
 
Demo-driven innovation teaser
Demo-driven innovation teaserDemo-driven innovation teaser
Demo-driven innovation teaser
 
Software assessment essentials (lecture at the University of Bern 2013)
Software assessment essentials (lecture at the University of Bern 2013)Software assessment essentials (lecture at the University of Bern 2013)
Software assessment essentials (lecture at the University of Bern 2013)
 
Demo-driven innovation (University of Zurich, June 2013)
Demo-driven innovation (University of Zurich, June 2013)Demo-driven innovation (University of Zurich, June 2013)
Demo-driven innovation (University of Zurich, June 2013)
 
Humane assessment with Moose at GOTO Aarhus 2011
Humane assessment with Moose at GOTO Aarhus 2011Humane assessment with Moose at GOTO Aarhus 2011
Humane assessment with Moose at GOTO Aarhus 2011
 

08 - Complexity

  • 1. Complexity www.tudorgirba.com
  • 2. computation information information computer
  • 3. memory bandwidth Computation requires resources time ...
  • 4.
  • 5. 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 }
  • 6. 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 }
  • 7. 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 }
  • 8. 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
  • 9. Big O Notation f: N -> N g: N -> N f ∈ O(g) ∃ c, n0 : (c,n0∈N) ∧ (c>0) : (∀n : n∈N ∧ n>n0 : f(n) ≤ c*g(n))
  • 10. Big O Notation f: N -> N g: 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)
  • 11. !"#$%$&'()("'*$+ ,'-.$/'-- O(1) Constant O(log n) Logarithmic O(n) Linear O(n2) Quadratic O(nk) Polynomial O(kn) Exponential
  • 12. O(1) ∈ O(log n) ∈ O(n) ∈ O(n2) ∈ O(nk) ∈ O(2n) !"#$%$&'()("'*$+ ,'-.$/'-- O(1) Constant O(log n) Logarithmic O(n) Linear O(n2) Quadratic O(nk) Polynomial O(kn) Exponential
  • 13. 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
  • 14. 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 2 s = 1. 10’000 13 130’000 108 10 4 s = 7 minutes 2. 10 5 s = 8 hours 1.1 day 100’000 17 106 1010 10 s = 6 s 1.6 we 10 s = 7 eks 3.8 mo 10 s = 8 nths 1’000’000 1’000 2*107 1012 3.1 yea 10 s = 9 rs 3.1 dec 10 10 s a = 3.1 c des 10 11 s enturie = neve s r :)
  • 15. O(c*f) = O(f) O(f) + O(g) = O(f+g) = max{O(f),O(g)} O(f*g) = O(f)*O(g)
  • 16. 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))
  • 17. a b c d e f g a 0 2 3 0 0 0 0 a e b 0 0 1 0 0 0 0 3 5 3 2 c 0 0 0 2 0 0 0 2 c d g d 0 0 0 0 5 4 0 1 4 3 e 0 0 0 0 0 0 3 b f f 0 0 0 0 0 0 3 g 0 0 0 0 0 0 0
  • 18. a b c d e f g a 0 2 3 0 0 0 0 a e b 0 0 1 0 0 0 0 3 5 3 2 c 0 0 0 2 0 0 0 2 c d g d 0 0 0 0 5 4 0 1 4 3 e 0 0 0 0 0 0 3 b f f 0 0 0 0 0 0 3 g 0 0 0 0 0 0 0 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] );
  • 19.
  • 20. 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))
  • 21. 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))
  • 22. 4 2 5 1 6 3 ch : Line ar sear E xample
  • 23. 4 2 5 1 6 3 ch : Line ar sear E xample
  • 24. 4 2 5 1 6 3 ch : Line ar sear E xample
  • 25. Worst case: n steps Best case: 1 step Average case: n/2 steps 4 2 5 1 6 3 ch : Line ar sear E xample
  • 26. Best case estimation f: N -> N g: N -> N f ∈ Ω(g) ∃ c, n0 : (c,n0∈N) ∧ (c>0) : (∀n : n∈N ∧ n>n0 : f(n) ≥ c*g(n))
  • 27. Best case estimation f: N -> N g: N -> N f ∈ Ω(g) ∃ c, n0 : (c,n0∈N) ∧ (c>0) : (∀n : n∈N ∧ n>n0 : f(n) ≥ c*g(n)) (3n2+6n+9)*log(1+2n) ≤ 36 n2 * log n (3n2+6n+9)*log(1+2n) ∈ O(n2 * log n)
  • 28. Best case estimation f: N -> N g: N -> N f ∈ Ω(g) ∃ c, n0 : (c,n0∈N) ∧ (c>0) : (∀n : n∈N ∧ n>n0 : f(n) ≥ c*g(n)) (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)
  • 29. Average case estimation f: N -> N g: N -> N f ∈ θ(g) ∃ c 1, c 2, n 0 : (c1,c2,n0∈N) ∧ (c1>0) ∧ (c2>0): (∀n : n∈N ∧ n≥n0 : c1*g(n) ≤ f(n) ≤ c2*g(n))
  • 30. Average case estimation f: N -> N g: N -> N f ∈ θ(g) ∃ c 1, c 2, n 0 : (c1,c2,n0∈N) ∧ (c1>0) ∧ (c2>0): (∀n : n∈N ∧ n≥n0 : c1*g(n) ≤ f(n) ≤ c2*g(n)) (3n2+6n+9)*log(1+2n) ∈ O(n2 * log n)
  • 31. Average case estimation f: N -> N g: N -> N f ∈ θ(g) ∃ c 1, c 2, n 0 : (c1,c2,n0∈N) ∧ (c1>0) ∧ (c2>0): (∀n : n∈N ∧ n≥n0 : c1*g(n) ≤ f(n) ≤ c2*g(n)) (3n2+6n+9)*log(1+2n) ∈ O(n2 * log n) (3n2+6n+9)*log(1+2n) ∈ Ω(n2 * log n)
  • 32. Average case estimation f: N -> N g: N -> N f ∈ θ(g) ∃ c 1, c 2, n 0 : (c1,c2,n0∈N) ∧ (c1>0) ∧ (c2>0): (∀n : n∈N ∧ n≥n0 : c1*g(n) ≤ f(n) ≤ c2*g(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)
  • 33. 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; } } E xample
  • 34. 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) E xample
  • 35. 4 2 6 1 3 5 ch : Bina ry sear le Examp
  • 36. f ∈ O(log n) f ∈ Ω(1) f ∈ θ(log n) 4 2 6 1 3 5 ch : Bina ry sear le Examp
  • 40. solvable in O(nk) P NP verifiable in O(nk)
  • 41. solvable in O(nk) P ⊂ NP verifiable in O(nk)
  • 42. solvable in O(nk) P = NP ? verifiable in O(nk)
  • 43. NP-complete NP, and one cannot do better
  • 44. Hamiltonian path a e c d g b f : p roblem mplete path NP-co onian Hamilt
  • 45. : p roblem NP-co mplete sman le Trave ling sa
  • 46. : p roblem mplete ing NP-co h color Grap
  • 47. S={x1, … , xn} t ∈ N ∃ {y1, … , yk} ⊆ S : Σyi=t ? : p roblem NP-co mplete m u S ubset s
  • 48. S={x1, … , xn} t ∈ N ∃ {y1, … , yk} ⊆ S : Σyi=t ? S= {8, 11, 16, 29, 37} t = 37 {11, 16} {8, 29} {37} : p roblem NP-co mplete m u S ubset s
  • 49. Will this program terminate? g problem Th e haltin dable ci is unde
  • 50. Tudor Gîrba www.tudorgirba.com creativecommons.org/licenses/by/3.0/