You are on page 1of 93

Advanced Programming and Algorithms I

Winter 2022/2023

Anja Rey

24th October 2021


Recap Last Week & Additional Details

Input ALGORITHM Output

computational problem model algorithm design software design programming

Algorithm in pseudocode: Implementation in Python:


some_function(input):
1 precise description of sequence of 1 def some \ _fuction ( input ) :
computational steps transforming 2 pass
the input into an output
2 return output

Extracts from notebook with first code overview: lecture01_basics.ipynb


Further details æ exercise groups

18/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Recap Last Week & Additional Details

Input ALGORITHM Output

computational problem model algorithm design software design programming

Algorithm in pseudocode: Implementation in Python:


some_function(input):
1 precise description of sequence of 1 def some \ _fuction ( input ) :
computational steps transforming 2 pass
the input into an output
2 return output

Extracts from notebook with first code overview: lecture01_basics.ipynb


Further details æ exercise groups

18/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Recap Last Week & Additional Details

Input ALGORITHM Output

computational problem model algorithm design software design programming

Algorithm in pseudocode: Implementation in Python:


some_function(input):
1 precise description of sequence of 1 def some \ _fuction ( input ) :
computational steps transforming 2 pass
the input into an output
2 return output

Extracts from notebook with first code overview: lecture01_basics.ipynb


Further details æ exercise groups

18/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Recap Last Week & Additional Details

Input ALGORITHM Output

computational problem model algorithm design software design programming

Algorithm in pseudocode: Implementation in Python:


some_function(input):
1 precise description of sequence of 1 def some \ _fuction ( input ) :
computational steps transforming 2 pass
the input into an output
2 return output

Extracts from notebook with first code overview: lecture01_basics.ipynb


Further details æ exercise groups

18/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Recap Last Week & Additional Details

Input ALGORITHM Output

computational problem model algorithm design software design programming

Algorithm in pseudocode: Implementation in Python:


some_function(input):
1 precise description of sequence of 1 def some \ _fuction ( input ) :
computational steps transforming 2 pass
the input into an output
2 return output

Extracts from notebook with first code overview: lecture01_basics.ipynb


Further details æ exercise groups

18/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

7316452

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

7316452

1234567
19/44 Advanced Programming and Algorithms I – Introduction Anja Rey
Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

7316452

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

7316452

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

7316452

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

7316452

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

7716452
3

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

3716452

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

3716452

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

3716452

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

3716452

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

13376452
19/44 Advanced Programming and Algorithms I – Introduction Anja Rey
Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

1376452

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

1376452

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

1376452

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

1376452

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

1367452

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

1367452

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

1346752

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

1346752

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

1345672

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

1345672

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

1234567

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

1234567

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

Algorithm:
insertion_sort(A[0..n ≠ 1], n):
0 1 2 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1376452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i]
6 i Ωi ≠1
7 A[i + 1] Ω key

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

Algorithm:
insertion_sort(A[0..n ≠ 1], n):
0 1 2 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1376452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i]
6 i Ωi ≠1
7 A[i + 1] Ω key

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

Algorithm:
insertion_sort(A[0..n ≠ 1], n):
0 1 2 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1 376452
3 i Ωj ≠1
while i Æ 0 and A[i] > key do
4

5 A[i + 1] Ω A[i] They


6 i Ωi ≠1
7 A[i + 1] Ω key

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

Algorithm:
insertion_sort(A[0..n ≠ 1], n): 24
0 1 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1376452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i] key 6
6 i Ωi ≠1
7 A[i + 1] Ω key

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

Algorithm:
insertion_sort(A[0..n ≠ 1], n):
0 1
24 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1377452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i] key 6
6 i Ωi ≠1
7 A[i + 1] Ω key

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

Algorithm:
insertion_sort(A[0..n ≠ 1], n):
0 1 2 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1377452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i] key 6
6 i Ωi ≠1
7 A[i + 1] Ω key

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

Algorithm:
insertion_sort(A[0..n ≠ 1], n):
0 1 2 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1377452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i] key 6
6 i Ωi ≠1
7 A[i + 1] Ω key

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

Algorithm:
insertion_sort(A[0..n ≠ 1], n):
0 1 2 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1367452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i] key 6
6 i Ωi ≠1
7 A[i + 1] Ω key

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

Algorithm:
insertion_sort(A[0..n ≠ 1], n): 34 44
0 1 2 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1367452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i]
6 i Ωi ≠1
7 A[i + 1] Ω key

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Insertion Sort

Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ

Algorithm: Python implementation:


insertion_sort(A[0..n ≠ 1], n):
1 def insertio n_sort ( sequence ) :
1 for j Ω 1 to n ≠ 1 do
2 for j in range (1 , len ( sequence ) ) :
2 key Ω A[j] 3 key = sequence [ j ]
3 i Ωj ≠1 4 i = j - 1
4 while i Æ 0 and A[i] > key do 5 while i >= 0 and sequence [ i ] > key :
6 sequence [ i + 1] = sequence [ i ]
5 A[i + 1] Ω A[i] 7 i = i - 1
6 i Ωi ≠1 8 sequence [ i + 1] = key
7 A[i + 1] Ω key

19/44 Advanced Programming and Algorithms I – Introduction Anja Rey


Efficiency
Running Time I

How efficient is an algorithm?

1 import time
2

3 start = time . perf_counter ()


4 pass
5 end = time . perf_counter ()
6 print ( end - start )

20/46 Advanced Programming and Algorithms I – Efficiency Anja Rey


Running Time I

How efficient is an algorithm?


Resources: Time, Space, Energy, . . .

1 import time
2

3 start = time . perf_counter ()


4 pass
5 end = time . perf_counter ()
6 print ( end - start )

20/46 Advanced Programming and Algorithms I – Efficiency Anja Rey


Running Time I

How efficient is an algorithm?


Resources: Time, Space, Energy, . . .
Approach: Measure the time needed to run an implementation.
1 import time
2

3 start = time . perf_counter ()


4 pass
5 end = time . perf_counter ()
6 print ( end - start )

20/46 Advanced Programming and Algorithms I – Efficiency Anja Rey


Running Time I

How efficient is an algorithm?


Resources: Time, Space, Energy, . . .
Approach: Measure the time needed to run an implementation.
1 import time
2

3 start = time . perf_counter ()


4 pass
5 end = time . perf_counter ()
6 print ( end - start )

See notebook: lecture02_time.ipynb

20/46 Advanced Programming and Algorithms I – Efficiency Anja Rey


Running Time I

How efficient is an algorithm?


Resources: Time, Space, Energy, . . .
Approach: Measure the time needed to run an implementation.
1 import time
2

3 start = time . perf_counter ()


4 pass
5 end = time . perf_counter ()
6 print ( end - start )

See notebook: lecture02_time.ipynb


The actual running time (in seconds) is influenced by:

hardware: processor, cache, pipelining, . . .


software: operation system, programming language, compiler/interpreter, threading, . . .
20/46 Advanced Programming and Algorithms I – Efficiency Anja Rey
Computation Model

Theoretical running time analysis: independent of hardware and software

21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Computation Model

Theoretical running time analysis: independent of hardware and software

Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step

21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Computation Model

Theoretical running time analysis: independent of hardware and software

Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps

21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Computation Model

Theoretical running time analysis: independent of hardware and software

Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps

Approach: Count number of steps

21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Computation Model

Theoretical running time analysis: independent of hardware and software

Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps

Approach: Count number of steps

factorial(n): instance n 4
1 x Ω1
2 for i Ω 2 to n do
3 x Ωx ·i
4 return x

21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Computation Model

Theoretical running time analysis: independent of hardware and software

Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps

Approach: Count number of steps

factorial(n): instance n 4
1 x Ω1 1
2 for i Ω 2 to n do
3 x Ωx ·i
4 return x

21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Computation Model

Theoretical running time analysis: independent of hardware and software

Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps

Approach: Count number of steps

factorial(n): instance n 4
1 x Ω1 1
2 for i Ω 2 to n do
3 x Ωx ·i
4 return x

21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Computation Model

Theoretical running time analysis: independent of hardware and software

Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps

Approach: Count number of steps

factorial(n): instance n 4
1 x Ω1 1
2 for i Ω 2 to n do
3 x Ωx ·i 1
4 return x

21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Computation Model

Theoretical running time analysis: independent of hardware and software

Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps

Approach: Count number of steps

factorial(n): instance n 4
1 x Ω1 1
for i Ω 2 to n do 3
1 h
2
i
3 x Ωx ·i
4 return x

21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Computation Model

Theoretical running time analysis: independent of hardware and software

Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps

Approach: Count number of steps

factorial(n): instance n 4
1 x Ω1
2

4
for i Ω 2 to n do
x Ωx ·i
return x
i 4
I I k
21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
Computation Model

Theoretical running time analysis: independent of hardware and software

Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps

Approach: Count number of steps

factorial(n): instance n 4
1 x Ω1 1
for i Ω 2 to n do
1 1 11
2
i 5
3 x Ωx ·i
4 return x

21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Computation Model

Theoretical running time analysis: independent of hardware and software

Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps

Approach: Count number of steps

factorial(n): instance n 4
1 x Ω1

Kid
2 for i Ω 2 to n do
3 x Ωx ·i
4 return x

21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Computation Model

Theoretical running time analysis: independent of hardware and software

Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps

Approach: Count number of steps

factorial(n): instance n 4
1 x Ω1
for i Ω 2 to n do

Äh
2

3 x Ωx ·i
4 return x

21/44 Advanced Programming and Algorithms I – Efficiency


9 steps Anja Rey
Computation Model

Theoretical running time analysis: independent of hardware and software

Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps

Approach: Count number of steps


general n 1
factorial(n): instance n 4
1 x Ω1
for i Ω 2 to n do 1 1 11 1
1 1
2

3 x Ωx ·i 4 1
return x
4
1 1

21/44 Advanced Programming and Algorithms I – Efficiency


9 steps In 1 steps Anja Rey
Computation Model

Theoretical running time analysis: independent of hardware and software

Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps

Approach: Count number of steps

factorial(n): insertions_sort([1, 2, 3], 3)

1 x Ω1 13 steps
2 for i Ω 2 to n do
insertions_sort([3, 2, 1], 3)
3 x Ωx ·i
return x
4
23 steps
21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
Computation Model

Theoretical running time analysis: independent of hardware and software

Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps

Approach: Count number of steps

factorial(n): insertions_sort([1, 2, 3], 3)

1 x Ω1
2 for i Ω 2 to n do
insertions_sort([3, 2, 1], 3)
3 x Ωx ·i
4 return x

How can we predict the number of steps in advance?


21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

22/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n

22/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)

22/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)

insertion_sort(A[0..n ≠ 1], n):


1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j]
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i]
6 i Ωi ≠1
7 A[i + 1] Ω key
22/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)

insertion_sort(A[0..n ≠ 1], n):


1 for j Ω 1 to n ≠ 1 do n 1 1 n
2 key Ω A[j]
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i]
6 i Ωi ≠1
7 A[i + 1] Ω key
22/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)

insertion_sort(A[0..n ≠ 1], n):


1 for j Ω 1 to n ≠ 1 do n 1 1 n
2 key Ω A[j]
3 i Ωj ≠1
while i Æ 0 and A[i] > key do
4

5 A[i + 1] Ω A[i] factor In 1


6 i Ωi ≠1
7 A[i + 1] Ω key
22/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)

insertion_sort(A[0..n ≠ 1], n):


1 for j Ω 1 to n ≠ 1 do n 1 1 n
2 key Ω A[j] 4 1
3 i Ωj ≠1 n 1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i]
6 i Ωi ≠1
A[i + 1] Ω key
7

22/44 Advanced Programming and Algorithms I – Efficiency


n 1 Anja Rey
Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)

insertion_sort(A[0..n ≠ 1], n):


1 for j Ω 1 to n ≠ 1 do n 1 1 n
2 key Ω A[j] 4 1
3 i Ωj ≠1 n 1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i] depends on warst case
6 i Ωi ≠1
A[i + 1] Ω key
7

22/44 Advanced Programming and Algorithms I – Efficiency


n d Anja Rey
Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)

insertion_sort(A[0..n ≠ 1], n):


1 for j Ω 1 to n ≠ 1 do n 1 1 n
2 key Ω A[j] 4 1
3 i Ωj ≠1 n d
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i] depends on warst case
6 i Ωi ≠1
input sorted in reverse order
7 A[i + 1] Ω key
n
22/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)

reverse order
insertion_sort(A[0..n ≠ 1], n):
1 for j Ω 1 to n ≠ 1 do n 1 d n
2 key Ω A[j] 4 1
3 i Ωj ≠1 n d
4

5
while i Æ 0 and A[i] > key do
A[i + 1] Ω A[i] always true for ie 90 j d
6 i Ωi ≠1
A[i + 1] Ω key
7

22/44 Advanced Programming and Algorithms I – Efficiency


n d Anja Rey
Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)

reverse order
insertion_sort(A[0..n ≠ 1], n):
1 for j Ω 1 to n ≠ 1 do n 1 1 n
2 key Ω A[j] 4 1
3 i Ωj ≠1

Für
4 while i Æ 0 and A[i] > key do
c
5 A[i + 1] Ω A[i]
6 i Ωi ≠1
A[i + 1] Ω key
7

22/44 Advanced Programming and Algorithms I – Efficiency


n 1 Anja Rey
Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)

reverse order
insertion_sort(A[0..n ≠ 1], n):
1 for j Ω 1 to n ≠ 1 do n 1 1 n
2 key Ω A[j] u 1
3 i Ωj ≠1
while i Æ 0 and A[i] > key do
c
En
4

5 A[i + 1] Ω A[i]
6 i Ωi ≠1
A[i + 1] Ω key
7

22/44 Advanced Programming and Algorithms I – Efficiency


1 Anja Rey
Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)

reverse order
insertion_sort(A[0..n ≠ 1], n):
1 for j Ω 1 to n ≠ 1 do n 1 1 n
2 key Ω A[j] 4 1
3 i Ωj ≠1 n 1

F2
while i Æ 0 and A[i] > key do
ja
4
wie
5 A[i + 1] Ω A[i]
6

22/44
i Ωi ≠1
A[i + 1] Ω key
Advanced Programming and Algorithms I – Efficiency
n
we
d E Anja Rey
Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)

reverse order
insertion_sort(A[0..n ≠ 1], n):
1 for j Ω 1 to n ≠ 1 do n 1 1 n
2 key Ω A[j] 4 1
11
3 i Ωj ≠1 n d n n
YYFtu
3w.cn
while i Æ 0 and A[i] > key do
2j n
4
wie
5 A[i + 1] Ω A[i]
nu
6

22/44
i Ωi ≠1
A[i + 1] Ω key
Advanced Programming and Algorithms I – Efficiency
n 1
2
Eij Anja Rey
Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)

reverse order
insertion_sort(A[0..n ≠ 1], n):
1 for j Ω 1 to n ≠ 1 do n 1 1 n
2 key Ω A[j] 4 1
3

4
i Ωj ≠1
while i Æ 0 and A[i] > key do
ü Eig 945min
5 A[i + 1] Ω A[i]
6

22/44
i Ωi ≠1
A[i + 1] Ω key
Advanced Programming and Algorithms I – Efficiency
d
2
FIT
RE N
we

Anja Rey
Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
• asymptotic analysis: ignore constants (may become twice as fast in the future or on another
machine, already ignore differences in operations)

22/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
• asymptotic analysis: ignore constants (may become twice as fast in the future or on another
machine, already ignore differences in operations)

Tu 2h43m 4

22/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
• asymptotic analysis: ignore constants (may become twice as fast in the future or on another
machine, already ignore differences in operations)

Tu 2h43m 4
time on Computer 1 2m13n 4 ca
time on Computer 2 2nd In 4 cz
22/44 Advanced Programming and Algorithms I – Efficiency
n En 2 23 Anja Rey
Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
• asymptotic analysis: ignore constants (may become twice as fast in the future or on another
machine, already ignore differences in operations)
• asymptotic growth: n æ infty

22/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


Running Time II

Observation: running time


• depends on input size (e.g., parameter n)
• depends on type of input (e.g., best case, worst case input)

Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
• asymptotic analysis: ignore constants (may become twice as fast in the future or on another
machine, already ignore differences in operations)
• asymptotic growth: n æ infty
MTU

22/44 Advanced Programming and Algorithms I – Efficiency Anja Rey


O-Notation

Defintion: O-Notation
gut Let f , g : N æ R>0 .

O(f (n)) = {g(n) | ÷c > 0, n0 > 0 : ’n Ø n0 : g(n) Æ c · f (n)}.

In
Intuition: g(n) œ O(f (n)) means that g(n) grows at most as fast as f (n) for n æ Œ.

Representative functions and their hierarchy: Let c Ø 2, 0 < ‘ Æ 1/2.


Ô
O(1) ™ O(log n) ™ O(log2 n) ™ O(logc n) ™ O(log n‘ ) ™ O( n) ™ O(n)
O(n) ™ O(n2 ) ™ O(nc ) ™ O(2n ) = O(c n )

Insertion sort has an (asymptotic worst-case) running time in O(n2 ).


23/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
O-Notation

Defintion: O-Notation
Let f , g : N æ R>0 .

O(f (n)) = {g(n) | ÷c > 0, n0 > 0 : ’n Ø n0 : g(n) Æ c · f (n)}.

Intuition: g(n) œ O(f (n)) means that g(n) grows at most as fast as f (n) for n æ Œ.

Representative functions and their hierarchy: Let c Ø 2, 0 < ‘ Æ 1/2.


Ô
O(1) ™ O(log n) ™ O(log2 n) ™ O(logc n) ™ O(log n‘ ) ™ O( n) ™ O(n)
O(n) ™ O(n2 ) ™ O(nc ) ™ O(2n ) = O(c n )

Insertion sort has an (asymptotic worst-case) running time in O(n2 ).


23/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
O-Notation

Defintion: O-Notation
Let f , g : N æ R>0 .

O(f (n)) = {g(n) | ÷c > 0, n0 > 0 : ’n Ø n0 : g(n) Æ c · f (n)}.

Intuition: g(n) œ O(f (n)) means that g(n) grows at most as fast as f (n) for n æ Œ.

Representative functions and their hierarchy: Let c Ø 2, 0 < ‘ Æ 1/2.


Ô
O(1) ™ O(log n) ™ O(log2 n) ™ O(logc n) ™ O(log n‘ ) ™ O( n) ™ O(n)
O(n) ™ O(n2 ) ™ O(nc ) ™ O(2n ) = O(c n )

Insertion sort has an (asymptotic worst-case) running time in O(n2 ).


23/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
O-Notation

Defintion: O-Notation
Let f , g : N æ R>0 .

O(f (n)) = {g(n) | ÷c > 0, n0 > 0 : ’n Ø n0 : g(n) Æ c · f (n)}.

Intuition: g(n) œ O(f (n)) means that g(n) grows at most as fast as f (n) for n æ Œ.

Representative functions and their hierarchy: Let c Ø 2, 0 < ‘ Æ 1/2.


Ô
O(1) ™ O(log n) ™ O(log2 n) ™ O(logc n) ™ O(log n‘ ) ™ O( n) ™ O(n)
O(n) ™ O(n2 ) ™ O(nc ) ™ O(2n ) = O(c n )

Insertion sort has an (asymptotic worst-case) running time in O(n2 ).


23/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
O-Notation

Defintion: O-Notation
Let f , g : N æ R>0 .

O(f (n)) = {g(n) | ÷c > 0, n0 > 0 : ’n Ø n0 : g(n) Æ c · f (n)}.

Intuition: g(n) œ O(f (n)) means that g(n) grows at most as fast as f (n) for n æ Œ.

Representative functions and their hierarchy: Let c Ø 2, 0 < ‘ Æ 1/2.


Ô
O(1) ™ O(log n) ™ O(log2 n) ™ O(logc n) ™ O(log n‘ ) ™ O( n) ™ O(n)
O(n) ™ O(n2 ) ™ O(nc ) ™ O(2n ) = O(c n )

Insertion sort has an (asymptotic worst-case) running time in O(n2 ).


23/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
O-Notation

flu
Defintion: O-Notation
gut Let f , g : N æ R>0 .

O(f (n)) = {g(n) | ÷c > 0, n0 > 0 : ’n Ø n0 : g(n) Æ c · f (n)}.

d
n
Intuition: g(n) œ O(f (n)) means that g(n) grows at most as fast as f (n) for n æ Œ.

Representative functions and their hierarchy: Let c Ø 2, 0 < ‘ Æ 1/2.


Ô
O(1) ™ O(log n) ™ O(log2 n) ™ O(logc n) ™ O(log n‘ ) ™ O( n) ™ O(n)
O(n) ™ O(n2 ) ™ O(nc ) ™ O(2n ) = O(c n )

Insertion sort has an (asymptotic worst-case) running time in O(n2 ).


23/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
O-Notation

Defintion: O-Notation
Let f , g : N æ R>0 .

O(f (n)) = {g(n) | ÷c > 0, n0 > 0 : ’n Ø n0 : g(n) Æ c · f (n)}.

In Tu 2h43m 4 e 049
Intuition: g(n) œ O(f (n)) means that g(n) grows at most as fast as f (n) for n æ Œ.
J

Representative functions and their hierarchy: Let c Ø 2, 0 < ‘ Æ 1/2.


Ô
O(1) ™ O(log n) ™ O(log2 n) ™ O(logc n) ™ O(log n‘ ) ™ O( n) ™ O(n)
O(n) ™ O(n2 ) ™ O(nc ) ™ O(2n ) = O(c n )

Insertion sort has an (asymptotic worst-case) running time in O(n2 ).


23/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
Related Concepts

Average Running Time


• worst case might only occur rarely
• compute average running time over all possible inputs of one size

24/46 Advanced Programming and Algorithms I – Efficiency Anja Rey


Related Concepts

Average Running Time


• worst case might only occur rarely
• compute average running time over all possible inputs of one size

Space Complexity
• memory needed to store information in the algorithm
• worst case: upper bound
• average case: all possible inputs of one size
æ see Exercise 2

24/46 Advanced Programming and Algorithms I – Efficiency Anja Rey


Related Concepts

Average Running Time


• worst case might only occur rarely
• compute average running time over all possible inputs of one size

Space Complexity
• memory needed to store information in the algorithm
• worst case: upper bound
• average case: all possible inputs of one size
æ see Exercise 2

Energy Complexity
• energy consumed by the algorithm
• comparably new research field in theory
• Python is rather inefficient.. TODO

24/46 Advanced Programming and Algorithms I – Efficiency Anja Rey


What’s next?

Within this week:


• Exercise groups start today:
• Mon 2;30 pm in seminar room 25.13.U1.23
• Fri 2:30 online via webex.
• Work on Exercise Sheet 2.
• Receive feedback on Sheet 1.

Next week:

• Mon 12:30 pm: Hand in exercise (via ILIAS).


• Lecture on Monday:
• How do we proof an algorithm works?
• How can we evaluate and improve code quality (I)?

25/46 Advanced Programming and Algorithms I – Efficiency Anja Rey

You might also like