Title: Geoinformation Technology: lecture 10a Algorithms and Complexity
1Geoinformation Technology lecture 10a
Algorithms and Complexity
- Prof. Dr. Thomas H. Kolbe
- Institute for Geodesy and Geoinformation Science
- Technische Universität Berlin
These slides contain material developed at the
Institute for Cartography and Geoinformation,
Univ. of Bonn, Courtesy of Prof. Plümer, Dr.
Gröger
2Area of a Polygon
2
(x3,y3)
(x4,y4)
F
(x2,y2)
(x5,y5)
(x1,y1)
3Area Formula by Gauss
4Iteration, For-Loop
BEGIN
5Iteration, For-Loop
(x3,y3)
(x4,y4)
(x2,y2)
(x5,y5)
(x1,y1)
f 0
f 0
6Iteration, For-Loop
(x3,y3)
(x4,y4)
(x2,y2)
(x5,y5)
(x1,y1)
f 0
FOR k1 TO 5 DO
for(k 1 k lt 5 k)
7Iteration, For-Loop
(x3,y3)
(x4,y4)
(x2,y2)
(x5,y5)
(x1,y1)
f 0
for(k 1 k lt 5 k)
BEGIN
8Iteration, For-Loop
(x3,y3)
(x4,y4)
(x2,y2)
(x5,y5)
(x1,y1)
f 0
(x6,y6)
for(k 1 k lt 5 k)
f f ((xk - xk1)(yk yk1))
f f ((xk - xk1)(yk yk1))
9Iteration, For-Loop
(x3,y3)
(x4,y4)
(x2,y2)
(x5,y5)
(x1,y1)
f 0
for(k 1 k lt 5 k)
f f ((xk - xk1)(yk yk1))
END
10Iteration, For-Loop
(x3,y3)
(x4,y4)
(x2,y2)
(x5,y5)
(x1,y1)
f 0
for(k 1 k lt 5 k)
f f ((xk - xk1)(yk yk1))
flaeche f/2
flaeche f/2
11Iteration, For-Loop
4
(x3,y3)
(x4,y4)
(x2,y2)
(x5,y5)
(x1,y1)
f 0
for(k 1 k lt 5 k)
f f ((xk - xk1)(yk yk1))
flaeche f/2
END
12Iteration, For-Loop
4
(x3,y3)
(x4,y4)
(x2,y2)
(x5,y5)
(x1,y1)
f 0
for(k 1 k lt 5 k)
f f ((xk - xk1)(yk yk1))
flaeche f/2
13Geoinformation Technology lecture 10b
Recursion
- Prof. Dr. Thomas H. Kolbe
- Institute for Geodesy and Geoinformation Science
- Technische Universität Berlin
These slides contain material developed at the
Institute for Cartography and Geoinformation,
Univ. of Bonn, Courtesy of Prof. Plümer, Dr.
Gröger
14Overview
- The principle of recursion
- Example Faculty function
15Illustration of Recursion
16The Principle of Recursion
3
- A recursive definition reduces a problem to a
smaller problem of the same kind. - A recursive function maps a recursive definition
to a program / algorithm.
17Example Factorial function
4
Definition For n ? 0 it is defined
18Java Syntax
5
- if (condition) statement1else statement2If
the condition is true, execute
statement1,otherwise execute statement2. - int my_function(...) ...... return
integer_expression ..... Function Call
int i my_function(...) - The function my_function returns the result of
theinteger-expression, which follows the return
keyword.
19Factorial Function
6
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
20Factorial Function
7
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
fak(4)
n 4
21Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
22Factorial Function
7
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
23Factorial Function
7
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
24Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
n 3
fak(3)
25Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
26Factorial Function
7
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
27Factorial Function
7
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
28Factorial Function
7
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
fak(2)
29Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
30Factorial Function
7
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
fak(1)
31Factorial Function
7
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
32Factorial Function
7
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
fak(0)
33Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
34Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
35Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
fak(0) 1
36Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
37Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
fak(1) 11
38Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
fak(1) 1
39Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
40Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
fak(2) 21
41Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
fak(2) 2
42Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
43Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
fak(3) 32
44Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
fak(3) 6
45Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
46Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
47Factorial Function
int fak(int n) if(n0) return 1 else
if (ngt0) return n fak(n-1) else
return -1
fak(4) 24
48Geoinformation Technology lecture 10c
Algorithmic Complexity
- Prof. Dr. Thomas H. Kolbe
- Institute for Geodesy and Geoinformation Science
- Technische Universität Berlin
These slides contain material developed at the
Institute for Cartography and Geoinformation,
Univ. of Bonn, Courtesy of Prof. Plümer, Dr.
Gröger
49Asymptotic Complexity of Algorithms
20
- Big Oh Notation (also called Landau symbol)
- Definition
- characterizes the asymptotical
behaviour of function f for very large
input lengths n. - f typically specifies the number of
computation steps or storage units.
where
50Properties of O(f)
- Product
- Sum
- Multiplication with a constant
51Big Oh Notation - Examples
- The fastest growing term within a (finite) sum of
functions determines the order of f(n) - O (log n) O (log(nc)), because they differ only
by constant factor c - O (logx n) O (logy n) with x?y
- but O(cn) ? O(dn) with c?d
52Common Orders of Complexity Funktions
- In increasing order of complexity
- (as n increases to infinity c is an arbitrary
constant) - O(1) - constant
- O(log n) - logarithmic
- O(n) - linear
- O(n log n) - loglinear, quasilinear
- O(n²) - quadratic
- O(nc), cgt1 - polynomial
- O(cn) - exponential (sometimes called geometric)
- O(n!) - factorial (sometimes called
combinatorial) - O(2cn) - double exponential
53Inclusion
2
54How fast grows... ?
n log n
n
log n
55Nichts wächst so schnell ...
in Englishnothing grows as fast as exponential
exp n
n²
n log n
... wie exponentiell!
56Algorithmic Complexity - Examples
- How complex is
- Addition/Multiplication of numbers
- with fixed lengths, e.g. double ab,
ab O(1) - comparison a lt b O(1)
- assignment a b O(1)
- (conditional) jump while(a gt b) O(1)
- access to array element a i O(1)
- i-th element of a list (length n) O(n)
- insertion at the beginning of a list O(1)
- insertion at the beginning of an array O(n)
57Worst Case Complexity (Examples)
- Searching within
- an unsorted array O(n)
- a list O(n)
- a binary search tree O(n)
- an AVL tree (balanced tree) O(log n)
- Sorting
- with Quicksort O(n²)
- with AVL trees O(n log n)
- Searching a point within an appropriate
structure O(log n) - Generation of an appropriate structure for
points O(n log n)