76
Computer Science Computer Science for for Computer Science Computer Science for for Engineers Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing. Dan Gutu 5 th of February 2010

Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Computer ScienceComputer Science forforComputer Science Computer Science forforEngineersEngineersgg

Lecture 12Algorithms – part 2

Prof. Dr. Dr.-Ing. Jivka OvtcharovaDipl. Wi.-Ing. Dan Gutu

5th of February 2010

Page 2: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

O-Calculus: Rules

• Important computation rules:Important computation rules:

))()(()()())(()())(()())()(()()())(()())(()(

nsnrOngnfnsOngandnrOnfnsnrOngnfnsOngandnrOnf

⋅∈⋅⇒∈∈+∈+⇒∈∈

))(()())(()())(()())(()(

))()(()()())(()())(()(

nrOknfkandnrOnfnrOknfkandnrOnf

nsnrOngnfnsOngandnrOnf

∈⋅⇒Ν∈∈∈±⇒Ν∈∈

∈⇒∈∈

• Functions with different groth rate (ascending order):

))(()())(()( nrOknfkandnrOnf ∈⋅⇒Ν∈∈

( )( )p

q nnnnnnnn

nn

nnnc , log,)(log,log,,log

, log

,,log, 2

( )( ) pp

q nnnnnnnn

nn

n ...,,...,,...,, log, log,,log

,log

322222

rithm

s

. Com

plex

ity

( )nnnnn npe

nn,...,,3,,2

log log

5. A

lgo

5.3.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 2

Page 3: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Complexity Classes (1)

Complexity Class O-Calculus Examples:

)1(O

)(log nO

23,10,3

)32log(5log2 nn ++

Constant

L ith i )(log nO

)(nO

)32log(5,log2 nn ++⋅

nnnn 10,log,1 ++

Logarithmic

Linear )(

)log( nnO

,g,

)3log(,log2 nnnn +Logarithmic-Linear

)( knO 516,32 32 ++ nnnPolynomial

rithm

s

. Com

plex

ity

)( naO 34 3,2 nnn +Exponential

5. A

lgo

5.3.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 3

Page 4: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Complexity Classes (2)

)( knO)( naOPolynomic

ExponentialDifferent complexity functions with respect to „small“ values

)log( nnO)( nnO

Polynomicof n

)g(

)(nOLogarithmic-Linear

Linear

rithm

s

. Com

plex

ity

)(log nOLogarithmic5.

Alg

o

5.3.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 4

[Bieh00]

Page 5: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Complexity Classes - Example

Example:Problem area: add all numbers between 1 and 100

Algorithm 1

int sum = 0;

Algorithm 2

int sum = 0;int sum = 0;for (int i = 1; i <= 100; i++){

i

int sum = 0;for (int i = 1; i <= 50; i++) {

isum = sum + i;}

sum = sum + i;sum = sum + (101 – i);

}Same complexity classes!

run time = 2 * n + 2 run time = 4 * n/2 + 2

(2 * n + 2) ∈ O(n) (4 * n/2 + 2) ∈ O(n)

p y

rithm

s

. Com

plex

ity

( ) ( ) ( ) ( )

You see that both algorithms show the same run time behavior. They behave in the

5. A

lgo

5.3.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 5

same way according to the input size of n.

Page 6: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Run Time Behavior - Examples

The following table shows an overview to the access possibilities and the run time behavior of different operations to several data structures from chapter 4

indicated List Front Back

from chapter 4.

access Operations Operations Operations

Binary search tree

Simple linked list

O(log(n))

O(n) O(1) O(n)

Double linked list

Array

O(n)

O(1)

O(1)

O(n)

O(1)

O(1)

O(1)

O(1)

Indicated access : access to an element on a previously given positionrithm

s

. Com

plex

ity

List operations : insert or delete elements from a specific position

Front operation : insert or delete from the beginning of the data structure

Back operation : insert or delete at the end of the data structure

5. A

lgo

5.3.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 6

Back operation : insert or delete at the end of the data structure

Page 7: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Calculating Rules for the O-Calculus

If f and g are functions, and c is a constant.

Rules:Rules: Examples:Examples:

22

)())(()(

fOfOOfOf=

=)())((

)102(10222

22

nOnOOxxOxx

=

++=++

)()()())((

fOfcOfOfOO

=⋅=

)()230()())((

22 nOnOnOnOO

=

)()()()()(

gfOgOfOfOcfO

+=+=+

)()()()()4(

33

33

xxOxOxOxOxO

+=+

=+

rithm

s

. Com

plex

ity

)()()( gfOgOfO +=+ )()()( xxOxOxO +=+

5. A

lgo

5.3.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 7

Page 8: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

O-Calculus: Example (1)

(a) Sum of n numbers: f(n)=5n+3

For: f ∈ O(g), with g(n) = n Notation: f ∈ O(n)( )

• f(n) = n2 +1000nf ∈ O(n2) then choose c = 2, n0 = 1000

n2 + 1000n ≤ c * n2 ∀ n ≥ n0

rithm

s

. Com

plex

ity

5. A

lgo

5.3.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 8

Page 9: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

O-Calculus: Example (2)

product = 1;product = 1; • Before the loop, we have a constant

tiwhile (n > 1) {

if (n mod 2 == 1){

while (n > 1) {

if (n mod 2 == 1){

operation, thus O(1).

• Inside the loop, the

product = product * x;

}

product = product * x;

}

p,operation is constant, whether the IF-case happens or not; thus}

x = x * x;

}

x = x * x;

happens or not; thus, also O(1).

• After the loop, the

n = n div 2;

}

n = n div 2;

}rithm

s

. Com

plex

ity operation is also constant, thus O(1).

• Question: How often}

product = product * x;

}

product = product * x;5. A

lgo

5.3. Question: How often

is the loop run?

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 9

Page 10: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Tips on O-Notation

Note:

• The O-Notation gives an „upper limit“. That doesn‘t mean that the algorithm indeed requires this amount of time. It just means that in no case the algorithm requires more timeno case the algorithm requires more time.

• The O-Notation estimates the run time for an infinite input. But no real input is infinite, and therefore the actual dimension of theno real input is infinite, and therefore the actual dimension of the input should be considered, too.

rithm

s

. Com

plex

ity

5. A

lgo

5.3.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 10

[rtc04]

Page 11: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Tips on O-Notation

Note:

})()(::0|{)( cnfcngNncRgfO N +≤∈∀>∃∈=

• The system dependent constants c1,2 are usually unknown. They

})()(::0|{)( 212,1 cnfcngNncRgfO +⋅≤∈∀>∃∈= +

must not necessarily be small though. If they are large, this can be a significant risk: suppose an algorithm needs n2 nanoseconds (quadratic complexity), and another one “only” needs n centuries(quadratic complexity), and another one only needs n centuries (linear complexity). With respect to the O-notation a linear complexity appears to be better than a quadratic one! Therefore the O t ti h i ’t it bl f th d i iO-notation here isn’t suitable for the decision.

• O-notation is a theoretical estimation! For a qualitative statements the system too must be taken into consideration i e processorrit

hms

. Com

plex

ity

the system, too, must be taken into consideration, i.e. processor, usage (data amount), etc.

5. A

lgo

5.3.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 11

[rtc04]

Page 12: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Outline

Lecture Content

1. Preface

2. Basics

3. Object orientation

4. Data Structures4. Data Structures

5. Algorithms

5 1 Introduction

5.2. Characteristics

5 3 Complexity

5.1. Introduction

5.3. Complexity

5.4. Design Methods

5.5. Examples of Algorithmsp g

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 12

Page 13: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Algorithm Design - Requirements

Requirements for algorithm design are:

• Systematic and reproducible design- allows for systematic testing and verification

f f- simplifies care, maintenance, and advancement of programs

• Division of labor in the design (in large problem areas)- Early structuring

- Distribution into smaller problems

hods

• Efficiency of the designed algorithm- Time complexityrit

hms

. Des

ign

met

h

- Storage complexity

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 13

Page 14: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Algorithm Design

• So far, we only considered short and simple algorithms. The design of such algorithms is relatively problem free, since they are manageable and the operation sequence can quickly be derived from theand the operation sequence can quickly be derived from the mathematical definition of the problem area.

• In practice, often extensive and rather complex algorithms are p act ce, o te e te s e a d at e co p e a go t s a enecessary. The design of such algorithms is complicated.

• Algorithm design is a part of Software Engineering and is today partially supported by computers.

• The most important concepts are:

hods

- Stepwise refining

- Modularizing

rithm

s

. Des

ign

met

h

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 14

Page 15: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Stepwise Refining

• The elementary operations that describe an algorithm in its final form are generally relatively simple.

• If an algorithm is considered to be a complex operation, that is comprised of many elementary operations and run structures the question arisesof many elementary operations and run structures, the question arises how we can get from the complex operation to the elementary operation in a simple and manageable manner.

hods

abstractalgorithm

Algorithm(elementary operations)

Problem area(complex operations)rit

hms

. Des

ign

met

h

algorithm (elementary operations)(complex operations)

Stepwise refining5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 15

Page 16: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Stepwise Refining - Principle

Principles of stepwise refining:

1. Sketch a raw algorithm with abstract operations and data types

2. Refine the operations in the first step, that means: implementation with fewer abstract operations and data types

3. Repeat the refining step till you have an algorithm, that only contains

hods

the run structures and elementary operations that are available.

rithm

s

. Des

ign

met

h

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 16

Page 17: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Stepwise Refining - Example (1)

Example: Sorting with direct insertion

Problem area:A i li t F (k k k k ) f l h ll b t d i dA given list F = (k1, k2, k3, … , kn) of values shall be sorted in an order < .

step 1:sort(k1, k2, k3, … , kn); The algorithm is first defined as an

b t t tihods sort(k1, k2, k3, … , kn); abstract operation.

rithm

s

. Des

ign

met

h

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 17

Page 18: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Stepwise Refining - Example (2)

step 2:

The algorithm will be refined through multiple abstract or concrete operations, in which the sorting problem is reduced to a series of insert-operations.

for (i = 2; i <= n; i++) {

insert i th element a[i] into theinsert i-th element a[i] into thelist (a[1], a[2], … a[n]) at its correct position

}hods

rithm

s

. Des

ign

met

h

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 18

Page 19: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Stepwise Refining - Example (3)

step 3:

The insert operation is refined through the input of concrete insert-positions. At this position the elementary operations are already abstractly described.

for (i = 2; i <= n; i++) {

int j = i - 1;int j i 1;

note the value of a[i]while((j != 0) && (a[i] < a[j])) {

hods

push a[j] one spot to the rightj = j - 1;

}rithm

s

. Des

ign

met

h

}

insert the noted value in the position here (j + 1)

}

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 19

Page 20: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Stepwise Refining - Example (4)

step 4:

The abstract descriptions of the elementary operations are translated into concrete operations. Thus, the finished algorithm appears in JAVA-code:

for (i = 2; i <= n; i++) {

int j = i - 1;

int k = a[i];int k = a[i];

while((j != 0) && (a[i] < a[j])) {

a[j + 1] = a[j];

hods

j = j - 1;

}

[j 1] krithm

s

. Des

ign

met

h

a[j + 1] = k;

}5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 20

Page 21: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Stepwise Refining

• Stepwise refining requires that simultaneous refining of the algorithm component parts matches with one another. That means that single component parts of the algorithm should beThat means, that single component parts of the algorithm should be refined to the same level.

• This leads to the disadvantage, that the refining of large and complex algorithms can be very complex.

• A division of the problem into smaller parts can help. The parts can then hods p p p p

be individually refined by stepwise refining and later be put together again. This is called modularizing.

rithm

s

. Des

ign

met

h

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 21

Page 22: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Modularization (1)

• Modularization decomposes the problem into parts…

- that are clearly separated from one another:that are clearly separated from one another: a partial problem solves a specific problem that can’t be further subdivided.

- that are largely independent from one another,The solutions to single partial problems don‘t influence each other, but can make use of one another.

- whose solutions are interchangeable to alternative solutions of the hods g

particular partial problem without side effects.

rithm

s

. Des

ign

met

h

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 22

Page 23: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Modularization (2)

• The creation of algorithms and software follows the „building block principle“. Single solution building blocks and partial solutions are called modulescalled modules.

• The largely independent modules are provided with a clear-cut ifi d i t f Thi ll f th i l d l t b ilspecified interface. This allows for the single modules to be easily

combined later on.

• Single modules, esp. algorithms, can be independently tested from one another, verified, or advanced.

hods

• Modularization allows for an unproblematic and good transition to successor versions of a software product by the exchange of

d lrithm

s

. Des

ign

met

h

modules.

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 23

Page 24: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Modularizing (3)

Possibilities for classifying modules:

P bl i t d d l i ti• Problem oriented modularization,The module is subdivided into closed processing units, so that each module fulfills a problem specific task. p p

• Data oriented modularization,The module is split according to the data on which the module will workThe module is split according to the data on which the module will work on.

F ti i t d d l i tihods

• Function oriented modularizationAlgorithms and esp. modules with similar functions are combined together. rit

hms

. Des

ign

met

h

g

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 24

Page 25: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Design Techniques

• The methods “stepwise refining” and “modularizing” are generally valid and recognized design concepts. They are universally used.

• In contrast to this, design techniques include solution ideas and concepts that are designed for specific problem areas. For example, th f ll ithe following:

- Systematic Trying and Backtracking

- Divide and Conquer.

hods

rithm

s

. Des

ign

met

h

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 25

Page 26: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Systematic Trying and Backtracking (1)

Assumption:

• An exact or direct solution of the problem is not possible in appropriate time.

• It is possible to generate all possible solutions or partial solutions of the solution spacesolution space.

Procedure:

C t th l ti ( ith l ti t )• Create the solution space (e.g. with a solution tree)

• Systematic (recursive) search of the solution space.

hods

• If a partial solution leads to an invalid solution, the last step is un-done. Now try to lead the reduced partial solution to a valid general solution by trying another wayrit

hms

. Des

ign

met

h

by trying another way.

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 26

Page 27: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Systematic Trying and Backtracking (2)

Procedure:

1. Starting with the node of the Solution tree

solution tree that represents the current partial solution, we test if the next (succeeding) node t e e t (succeed g) odeleads to a success (what success means depends on the

bl ) No solutionproblem)

2. If not: try the next node

No solution possibility

hods

3. If no succeeding node leads to a positive result, go back to the ancestor of the current node,rit

hms

. Des

ign

met

h

ancestor of the current node, and continue the search there. No

solution possibility

Solution found

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 27

possibility

Page 28: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Systematic Trying and Backtracking (3)

• Backtracking algorithms usually have a exponential time complexity.

• This procedure is only recommendable if no other algorithm is known, or developing a better algorithm is too expensive.

Problems suitable for Backtracking are:• Problems suitable for Backtracking are:

- Jigsaw – or game problems:search for the best move, search for positions with certain properties, p p p

- Graph problems:search for certain paths (round trip)

hods

- Optimization problems and combinatorics

- etc.

rithm

s

. Des

ign

met

h

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 28

Page 29: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Divide and Conquer (1)

• The basic idea of the „Divide and Conquer“ approach is to split problems into smaller parts so that it is easier to handle small problems and jobs and not the entire problem at onceproblems and jobs, and not the entire problem at once.

• The solutions to the parts are in the end used for solving the entire problem. p ob e

• In general, this method is used recursively until small problem sizes are achieved.

• This approach can have a better efficiency if the time for solving the smaller problems and the time for combining the solutions is less

hods

than solving the problem all at once.

rithm

s

. Des

ign

met

h

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 29

Page 30: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Divide and Conquer (2)

• The Divide-and-Conquer-Strategy for the solution of a problem consists of 3 steps:

Di id Th bl i di id d i t t- Divide: The problem is divided into parts

- Conquer: The single parts are solved

Combine The sol tions of the single parts are re combined to the sol tion of- Combine: The solutions of the single parts are re-combined to the solution of the original problem.

hods

rithm

s

. Des

ign

met

h

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 30

Page 31: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Divide and Conquer (3)

Example: Sorting algorithm „Mergesort“ (alphabetical order)

Symbol chain

durch 2

Dividingby 2

hods Merging

rithm

s

. Des

ign

met

h

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 31

Source: http://de.wikipedia.org/wiki/Mergesort

Page 32: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Divide and Conquer (3)ho

ds

rithm

s

. Des

ign

met

h

5. A

lgo

5.4.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 32

Page 33: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Recommended Literature

[Algo04] Vorlesung „Algorithmen“, AIFB – Uni Karlsruhe, 2004

[Bieh00] Ingrid Biehl: „Grundzüge der Informatik 4, SS 2000”,Fachgebiet Kryptographie und Computeralgebra, TU-Darmstadt, 2000

[Schn98] H.-J.Schneider (Hrsg.), Lexikon der Informatik, Oldenbourg Verlag, 1998

[rtc04] http://www linux related de/index html?/coding/o notation htm[rtc04] http://www.linux-related.de/index.html?/coding/o-notation.htm

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 33

[DynD04]

Page 34: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Outline

Lecture Content

1. Preface

2. Basics

3. Object orientation

4. Data Structures4. Data Structures

5. Algorithms

5 1 Introduction

5.2. Characteristics

5 3 Complexity

5.1. Introduction

5.3. Complexity

5.4. Design Methods

5.5. Examples of Algorithmsp g

5.5.1 Sorting procedures

5.5.2 Voronoi Diagrams

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 34

5.5.3 Convex Cover

5.5.4 Network flow

Page 35: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Bubblesort

Principle:

• Bubblesort is one of the simplest sorting algorithms

• In the first passage, the smallest element is sought for, in the second passage, the second smallest, etc.

• The array is traversed from back to front. First, the last element is compared to the second last element. If the last element is smaller than the second last they switch The same operation is performed with the secondsecond last, they switch. The same operation is performed with the second last and the third last element. By this, the larger elements stay in place, while the smaller elements are forwarded.

Alg

orith

ms

roce

dure

s

• Depending on the sorting direction (upwards or downwards), the bigger or the smaller elements ascent like bubbles in the water.

ampl

es fo

r A

.1. S

ortin

g P

r

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 35

Page 36: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Quicksort

Principle:

• Quicksort chooses an element (pivot element) from the list to be sorted and devides the list into two sublists, a lower one, that contains all elements smaller and an upper one, that contains all elements bigger or equal to the pivot element. equa to t e p ot e e e t

• For this purpose, an element is sought in the lower list that is bigger than (or equal to) the pivot element. This element is too big for the lower list and mustn’t stay there. Accordingly, an element smaller than the pivot element is sought in the upper list. These elements switch places and are thus placed in the correct sublist The procedure is repeated until the upperA

lgor

ithm

s

roce

dure

s

placed in the correct sublist. The procedure is repeated, until the upper and the lower search meet. Thus two correct sublists (see above) are created in one run.

ampl

es fo

r A

.1. S

ortin

g P

r

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 36

Page 37: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Mergesort

Principle:

• Similar to quicksort, the method is based on the Divide-and-Conquer-strategy.

• The list to be sorted is subdivided into two sublists of equal size.

• The corresponding lists are subdivided continuously, until they reach a length of 1.

M i i f d b i th fi t l t f t bli t th• Merging is performed by comparing the first elements of two sublists: the smaller one is deleted and added to the new (sorted) list.

Alg

orith

ms

roce

dure

s

ampl

es fo

r A

.1. S

ortin

g P

r

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 37

Page 38: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example: Mergesort (1)

Unsorted Data Set

S���� ���� �����

Part A Part B

S �� S���

Alg

orith

ms

roce

dure

s

Part A (sorted) Part B (sorted)

M�� ��������ampl

es fo

r A

.1. S

ortin

g P

r ( )

S����� ���� ����

5.5.

Exa

5.5.

Sorted Data Set

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 38

Page 39: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example: Mergesort (2)

Procedure: Divide the list into two lists (of the same size) and divide these again and again until the sub-lists contain only one single element.

4 602 5 7134 602 5 713

3 1452 7 0 6

Alg

orith

ms

roce

dure

s

52 34 1 7 0 6

ampl

es fo

r A

.1. S

ortin

g P

r

52 34 1 7 0 65.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 39

Page 40: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example: Mergesort (3)

Now, two sublists are sorted into one list that is twice as long. For this, the first elements of each list are compared and the smaller one is transferred into the new list until all the elements are sortedinto the new list, until all the elements are sorted.

52 34 1 7 0 6

52 43 1 7 0 6

Alg

orith

ms

roce

dure

s

ampl

es fo

r A

.1. S

ortin

g P

r

5 0432 1 6 7

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 40

Page 41: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example: Mergesort (4)

• The last step consists of merging two lists of length n/2 to one list of length n by sorting them. :

• Compare both halves with an index i and an index j for each element and copy the next largest element in the results list.

List 2List 1 List 2List 1

5 0432 1 6 7

Alg

orith

ms

roce

dure

s

index i index j

ampl

es fo

r A

.1. S

ortin

g P

r

Results list (empty)

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 41

Page 42: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example: Mergesort (5)

• Compare the first element of the first list with the first element of the second list.

• Copy the smallest element into the results list.

List 2List 1

5 0432 1 6 7

Alg

orith

ms

roce

dure

s

index i index j

Results listampl

es fo

r A

.1. S

ortin

g P

r

Results list

0

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 42

Page 43: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example: Mergesort (6)

• Compare the first element of the first list with the second element of the second list.

• Copy the smallest element into the results list.

List 2List 1

5 0432 1 6 7

Alg

orith

ms

roce

dure

s

index i index j

Results listampl

es fo

r A

.1. S

ortin

g P

r

Results list

0 1

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 43

Page 44: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example: Mergesort (7)

• Compare the first element of the first list with the third element of the second list.

• Copy the smallest element into the results list.

List 2List 1

5 0432 1 6 7

Alg

orith

ms

roce

dure

s

Results list

index i index j

ampl

es fo

r A

.1. S

ortin

g P

r

Results list

0 1 2

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 44

Page 45: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example: Mergesort (8)

• Compare the second element of the first list with the third element of the second list.

• Copy the smallest element into the results list.

List 2List 1

i d j

5 0432 1 6 7

Alg

orith

ms

roce

dure

s

Results list

index i index j

ampl

es fo

r A

.1. S

ortin

g P

r

0 1 2 3

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 45

Page 46: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example: Mergesort (9)

• Compare the third element of the first list with the third element of the second list.

• Copy the smallest element into the results list.

List 2List 1

i d j

5 0432 1 6 7

Alg

orith

ms

roce

dure

s

Results list

index i index j

ampl

es fo

r A

.1. S

ortin

g P

r

Results list

0 1 2 3 4

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 46

Page 47: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example: Mergesort (10)

• Compare the fourth element of the first list with the third element of the second list.

• Copy the smallest element into the results list.

List 2List 1

i d j

5 0432 1 6 7

Alg

orith

ms

roce

dure

s

Results list

index i index j

ampl

es fo

r A

.1. S

ortin

g P

r

Results list

0 1 2 3 4 5

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 47

Page 48: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example: Mergesort (11)

• Copy the next element from the second list into the results list.

List 2List 1

i d j

5 0432 1 6 7

Alg

orith

ms

roce

dure

s

Results list

index i index j

ampl

es fo

r A

.1. S

ortin

g P

r

Results list

0 1 2 3 4 5 6

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 48

Page 49: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example: Mergesort (12)

• Copy the last element from the second list in the results list.

List 2List 1

i d i i d j

5 0432 1 6 7

Alg

orith

ms

roce

dure

s

Results list

index i index j

ampl

es fo

r A

.1. S

ortin

g P

r

0 1 2 3 4 5 6 7

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 49

Page 50: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example: Mergesort (13)

52 34 1 7 0 652 34 1 7 0 6

52 43 1 7 0 6

5 0432 1 6 7Alg

orith

ms

roce

dure

s

5 0432 1 6 7

ampl

es fo

r A

.1. S

ortin

g P

r

2 760 1 5435.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 50

Page 51: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example: Mergesort – Java Implementation

public class MergeSorter { // The function mergesort sorts an order aprivate static int[] a, b; // Help array bprivate static void merge(int lo, int m, int hi) {

int i, j, k;

// The function mergesort sorts an order a// from the lowest index lo to the upper index hi

private static void mergesort(int lo, int hi) {if (lo<hi) {

// copy both halves of a in the help array bfor (i=lo; i<=hi; i++)

b[i]=a[i];1

if (lo hi) {int m=(lo+hi)/2;mergesort(lo, m);mergesort(m+1, hi);merge(lo, m, hi);

i=lo; j=m+1; k=lo;// copy back the next largest element

while (i<=m && j<=hi)if (b[i]< b[j])

g ( , , );}

}

public static void sort(int[] a0) {if (b[i]<=b[j])a[k++]=b[i++];

elsea[k++]=b[j++];

p ( [] ) {a=a0;System.out.println(„unsorted order:");

for (int i=0; i<a.length; i++)System.out.print(a[i]+" ");A

lgor

ithm

s

roce

dure

s

a[k++]=b[j++];// copy the rest of the first half if still there

while (i<=m)a[k++]=b[i++];

y p ( [ ] );

int n=a.length;b=new int[n];mergesort(0, n-1);am

ples

for A

.1. S

ortin

g P

r

a[k++]=b[i++];while (j<=hi)

a[k++]=b[j++];}

g ( , );System.out.println("\nsorted order: ");for (int i=0; i<a.length; i++)

System.out.print(a[i]+" "); }

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 51

}}

}}

Page 52: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example: Mergesort– Java Implementation

// The TestMergeSorter is a test class

public class TestMergeSorter {

public static void main(String[] args) {

int array[] = {2,5,4,3,1,7,0,6};

MergeSorter.sort(array);

}

}

Output on the screen:

Alg

orith

ms

roce

dure

s

The unsorted order:

2 5 4 3 1 7 0 6

ampl

es fo

r A

.1. S

ortin

g P

r

The sorted order:

0 1 2 3 4 5 6 7 5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 52

Page 53: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Advantages, Disadvantages and Complexity of the Sorting Procedures (1)

• Bubble sort- Expense:

Best case: field is already sorted: T(n) = O(n)

Worst case: field is decreasingly sorted: T(n) = O(n2)

- Advantages and disadvantages:

☺ Easy to program and understand

☺ Can easily be modified to be very fast by sorting data

☺ Doesn‘t need additional storage, stabile

Alg

orith

ms

roce

dure

s

Slow with random data, because there are many comparisons

Still slow when the data is partially sorted.

ampl

es fo

r A

.1. S

ortin

g P

r

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 53

Page 54: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Advantages, Disadvantages and Complexity of the Sorting Procedures (2)

• Quick sort- Expense:

Best case: T(n) = Θ(n log(n))

Worst case: T(n) = Θ(n2)

- Advantages and disadvantages:

☺ Doesn‘t need additional storage

☺ As a rule, generally faster than Merge sort by a factor of 2

☺ In practice, often the fastest algorithm

Alg

orith

ms

roce

dure

s

Not stabile

Worst case Θ(n2)

ampl

es fo

r A

.1. S

ortin

g P

r

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 54

Page 55: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Advantages, Disadvantages and Complexity of the Sorting Procedures (3)

• Merge Sort- Expense: T (n) = Θ(n log(n) )

- Advantages and disadvantages:

☺ Can be bettered in the sense that you can separately treat simple cases y p y pwith 2 or 3 elements.

☺ Expense for all cases is T (n) = Θ(n log(n) )

Algorithm requires additional storage space.

Not stabile

Alg

orith

ms

roce

dure

s

Note: The consistent generation of help fields is ineffective. Therefore, an algorithm is faster when you create a large help field and apply this overam

ples

for A

.1. S

ortin

g P

r

algorithm is faster when you create a large help field and apply this over and over again.

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 55

Page 56: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Outline

Lecture Content

1. Preface

2. Basics

3. Object orientation

4. Data Structures4. Data Structures

5. Algorithms

5 1 Introduction

5.2. Characteristics

5 3 Complexity

5.1. Introduction

5.3. Complexity

5.4. Design Methods

5.5. Examples of Algorithmsp g

5.5.1 Sorting procedures

5.5.2 Voronoi Diagrams

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 56

5.5.3 Convex Cover

5.5.4 Network flow

Page 57: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Motivation (1)

• As an interdisciplinary subject area of technical computer science, robotics deals with the navigation processes for a mobile and sensor-led robot among othersled robot, among others.

• The navigation of a mobile robot is outlined in the following steps:

1. Mapping and localizationGenerate a map of the environment anddetermine your positiondetermine your position

2. Path designCalculate the optimal path from your

Alg

orith

ms

iagr

ams

current point to the goal point

3. Rules of motionFollow the previously calculated path witham

ples

for A

.2. V

oron

oi D

Follow the previously calculated path withhelp from sensors

Source: Fraunhofer IPA

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 57

Page 58: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Motivation (2)

• As an example to the practical application of algorithms, we will take a closer look at the path design.

• The goal of path design in robotics is to give robots the ability to independently design their own movements and actions starting from the start situation to the target situation.sta t s tuat o to t e ta get s tuat o

• This is essential for being able to develop autonomous robots.

• Even when such autonomy is not required, these processes can serve as programming relief for users, in that he only has to define the task, since the robot can then carry out the rest of the taskA

lgor

ithm

s

iagr

ams

the robot can then carry out the rest of the task.

• Complex tasks in a dynamic environment make manual path specification almost impossibleam

ples

for A

.2. V

oron

oi D

specification almost impossible.

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 58

Page 59: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example (1)

• Exploratory robots

- Steering exploratory robots on Mars is not possible due to the long signal run time bet een earth and Marstime between earth and Mars.

- If a controller were to send a steering signal to the vehicle, he would have to wait 40 minutes until he sees from which cliff it is going to fall down. g g

Alg

orith

ms

iagr

ams

ampl

es fo

r A

.2. V

oron

oi D

source: NASA

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 59

Page 60: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example (2)

• Service robots

- Service robots perform tasks for people that are, for instance, not able to be performed b people or are too dangero sperformed by people or are too dangerous.

- This type of robot is still being developed, and it just now starting to be seen as a possibility for performing household and office tasks. p y p g

- Possible application areas are: assistance systems, museum robots, autonomous road sweepers, and production assistants

Alg

orith

ms

iagr

ams

ampl

es fo

r A

.2. V

oron

oi D

source: Fraunhofer IPA

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 60

Page 61: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example (3)

• Game robots

- The fast progressive development and potential of robots is shown every year Alg

orith

ms

iagr

ams

p g p p y yin RoboCup

- The latest processes and constructions are demonstrated in a fun and titi i t h tham

ples

for A

.2. V

oron

oi D

competitive manner against each other.

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 61

source: RoboCup 2005, Noriaki Mitsunaga

Page 62: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Example (4)A

lgor

ithm

s

iagr

ams

ampl

es fo

r A

.2. V

oron

oi D

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 62

Page 63: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Problem Area: Path design

• Problem of path design:

Starting from a start position and orientation, a target position and orientation should be achieved. Determine the path π, that defines the complete order of positions and orientations, so that no barrier is touched and the goal is reached. touc ed a d t e goa s eac ed

Example: The parallel parking problemExample: The parallel parking problem

Alg

orith

ms

iagr

ams

ampl

es fo

r A

.2. V

oron

oi D

source: Marco Loh, TU München

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 63

Page 64: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Voronoi Diagrams

• As a possible path design strategy, a Voronoi diagram can be used.

• In the context of path design, the Voronoi diagram shows those paths that maintain a maximum distance to the barriers (points).

barriersbarriers

Alg

orith

ms

iagr

ams

Possible pathsampl

es fo

r A

.2. V

oron

oi D

Voronoi Diagram

Possible paths

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 64

Page 65: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Construction of Voronoi Diagrams

• One possibility for constructing Voronoi Diagrams is the following recursive algorithm, based on the principle of the „divide and conquer“ approachapproach.

RequirementsRequirements

A number P of n points are given in a plane.

Al ithAlgorithm1. Divide P into 2 equally sized heaps P1 and P2

2 C l l t th V i Di f P d PAlg

orith

ms

iagr

ams

2. Calculate the Voronoi Diagram for P1 and P2

3. Merge both Voronoi Diagrams for P1 and P2 to the Voronoi Diagram for P

ampl

es fo

r A

.2. V

oron

oi D

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 65

Page 66: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Construction of Voronoi Diagrams – Example (1)

Given heap of points P

p2

p4

Alg

orith

ms

iagr

ams

p1

p3

p5

ampl

es fo

r A

.2. V

oron

oi D

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 66

Page 67: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Construction of Voronoi Diagrams – Example (2)

1. Divide P into 2 equally sized heaps P1 and P2

p2

p4

Alg

orith

ms

iagr

ams

p1

p3

p5

ampl

es fo

r A

.2. V

oron

oi D

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 67

P1 P2

Page 68: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Construction of Voronoi Diagrams – Example (3)

2. Calculate the Voronoi Diagram for P1 and P2

B th i b k d f th i t h P th t tiBy the recursive break down of the point heap P, the construction problem area of a Voronoi Diagram can be reduced to 2 simple cases.

case 1: Voronoi Diagram construction with two points

The vertical line sets up a Voronoi Diagram with 2 pointsA

lgor

ithm

s

iagr

ams

Voronoi Diagram with 2 points

ampl

es fo

r A

.2. V

oron

oi D

p4 p5

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 68

Page 69: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Construction of Voronoi Diagrams – Example (4)

2. Calculate the Voronoi Diagram for P1 and P2

By the recursive break down of the point heap P, the construction problem area of a Voronoi Diagram can be reduced to 2 simple cases.

C 2 V i Di t ti ith th i tCase 2: Voronoi Diagram construction with three points

p3 The vertical lines of all point i t tA

lgor

ithm

s

iagr

ams

pairs are cut at a common intersection. They set up a Voronoi diagram with 3 points.

ampl

es fo

r A

.2. V

oron

oi D

p1 p2

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 69

Page 70: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Construction of Voronoi Diagrams – Example (5)

2. Calculate the Voronoi Diagram for P1 and P2

p2

p4

Alg

orith

ms

iagr

ams

p1

p3

p5

ampl

es fo

r A

.2. V

oron

oi D

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 70

P1 P2

Page 71: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Construction of Voronoi Diagrams – Example (6)

3. Merge both Voronoi Diagrams for P1 and P2

3.1. Connect the closests neighbour along the dividing line

p2

p4

Alg

orith

ms

iagr

ams

ampl

es fo

r A

.2. V

oron

oi D p1

p3

p5

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 71

P1 P2

Page 72: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Construction of Voronoi Diagrams – Example (7)

3. Merge both Voronoi Diagrams for P1 and P2

3.2. Construct the new vertical lines

p2

p4

Alg

orith

ms

iagr

ams

ampl

es fo

r A

.2. V

oron

oi D p1

p3

p5

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 72

P1 P2

Page 73: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Construction of Voronoi Diagrams – Example (8)

3. Merge both Voronoi Diagrams for P1 and P2

3.3. Cut the vertical lines at the intersecting points

p2

p4

Alg

orith

ms

iagr

ams

p1

p3

p5

ampl

es fo

r A

.2. V

oron

oi D

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 73

P1 P2

Page 74: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Construction of Voronoi Diagrams – Example (9)

Result: The finished Voronoi Diagram for P

p2

p4

Alg

orith

ms

iagr

ams

p1

p3

p5

ampl

es fo

r A

.2. V

oron

oi D

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 74

Page 75: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Voronoi Diagrams: An Application

• Example of a complex environment and the corresponding Voronoi Diagram and the chosen path of a mobile robot

Alg

orith

ms

iagr

ams

ampl

es fo

r A

.2. V

oron

oi D

5.5.

Exa

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 75

source: http://www.cs.columbia.edu/~pblaer/projects/path_planner/applet.shtml

Page 76: Computer ScienceComputer Science for Engineers · Computer ScienceComputer Science for Engineers Lecture 12 Algorithms – part 2 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing

Voronoi Diagrams: Advantages and Disadvantages

• Advantages:

- Maximum distance to the barriersbarriers

- The robot can easily test- with help from the distancing p gsensors - if the correct path will be followed and if it‘s not to be followed correct itself source: Fraunhofer IPAfollowed, correct itself.

• Disadvantages:

- The path is normally not thegoal

Alg

orith

ms

iagr

ams

The path is normally not the shortest way

- When there are only a few

ampl

es fo

r A

.2. V

oron

oi D

barriers, then only a few paths can be generated5.

5.Ex

a

5.5.

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 5 - WS 09/10 - Slide 76

Start