Transition system Definition 1.3.1 A transition system S is a pair of - - PowerPoint PPT Presentation

transition system
SMART_READER_LITE
LIVE PREVIEW

Transition system Definition 1.3.1 A transition system S is a pair of - - PowerPoint PPT Presentation

Transition system Definition 1.3.1 A transition system S is a pair of the form S ( C , T = ) where C is the set of configurations and T C C is a relation, the transition relation . Algorithms and Data Structures Chapter 1:


slide-1
SLIDE 1

Transition system

Definition 1.3.1 A transition system S is a pair of the form S

= (C, T )

where C is the set of configurations and T

C C is a relation, the

transition relation.

  • Algorithms and Data Structures

Chapter 1: Transition Systems 1

slide-2
SLIDE 2

Sequences generated by a transition system

Definition 1.3.3 Let S

= (C, T ) be a transition system. S generates a

set of sequences,

S (S ), defined as follows:
  • 1. the finite sequence c0, c1, . . . , cn (for n
0) belongs to S (S ) if

(ι) c0

2 C

(ιι) for all i with 1

i n: (ci 1, ci ) 2 T
  • 2. the infinite sequence c0, c1, . . . , cn, . . . belongs to
S (S ) if

(ι) c0

2 C

(ιι) for all i

1: (ci 1, ci ) 2 T
  • Algorithms and Data Structures

Chapter 1: Transition Systems 2

slide-3
SLIDE 3

Processes generated by a transition system

Definition 1.3.5 Let S

= (C, T ) be a transition system. The set of

processes generated by S, written

P (S ), is the subset of S (S )

containing

  • 1. all infinite sequences of
S (S )
  • 2. all finite sequences c0, c1, . . . , cn
(n ) of S (S ) for which it

holds that there is no c

2 C with (cn, c ) 2 T.

The final configuration of a finite process is called a dead configuration.

  • Algorithms and Data Structures

Chapter 1: Transition Systems 3

slide-4
SLIDE 4

Football

Transition system

F
  • tball

Configurations:

f [t, X, a, b ℄ j 0 t 90, X 2 fA, B, R g, a, b 2 N g [t, A, a, b ℄
  • [t
+ 2, B, a, b ℄

if t

88 [t, A, a, b ℄
  • [t
+ 2, B, a + 1, b ℄ if t 88 [t, A, a, b ℄
  • [t
+ 1, B, a, b ℄

if t

89 [t, A, a, b ℄
  • [t
+ 1, B, a + 1, b ℄ if t 89 [90, A, a, b ℄
  • [90, R, a, b
℄ [t, B, a, b ℄
  • [t
+ 2, A, a, b ℄

if t

88 [t, B, a, b ℄
  • [t
+ 2, A, a, b + 1 ℄ if t 88 [t, B, a, b ℄
  • [t
+ 1, A, a, b ℄

if t

89 [t, B, a, b ℄
  • [t
+ 1, A, a, b + 1 ℄ if t 89 [90, B, a, b ℄
  • [90, R, a, b

Algorithms and Data Structures Chapter 1: Transition Systems 4

slide-5
SLIDE 5

Induction principle

Induction principle Let P

(0 ), P (1 ), . . . , P (n ), . . . be statements. If

a) P

(0 ) is true

b) for all n

0 it holds that P (n ) implies P (n + 1 ),

then P

(n ) is true for all n 0.

Algorithms and Data Structures Chapter 1: Transition Systems 5

slide-6
SLIDE 6

Invariance principle

Invariance principle for transition systems Let S

= (C, T ) be a tran-

sition system and let c0

2 C be a configuration. If I (c ) is a statement

about the configurations of the system, the following holds. If a) I

(c0 ) is true

b) for all

(c, c ) 2 T it holds that I (c ) implies I (c )

then I

(c ) is true for any configuration c that occurs in a sequence

starting with c0.

Algorithms and Data Structures Chapter 1: Transition Systems 6

slide-7
SLIDE 7

Termination principle

Termination principle for transition systems Let S

= (C, T ) be a

transition system and let µ : C

! N be a function. If

for all

(c, c ) 2 T it holds that µ (c ) > µ (c )

then all processes in

P (S ) are finite.

Algorithms and Data Structures Chapter 1: Transition Systems 7

slide-8
SLIDE 8

Nim

Transition system

Nim

Configurations:

fA, B g N [A, n ℄
  • [B, n
2 ℄ if n 2 [A, n ℄
  • [B, n
1 ℄ if n 1 [B, n ℄
  • [A, n
2 ℄ if n 2 [B, n ℄
  • [A, n
1 ℄ if n 1

Algorithms and Data Structures Chapter 1: Transition Systems 8

slide-9
SLIDE 9

Towers of Hanoi

Transition system

Hanoi (n )

Configurations:

f [A, B, C ℄ j fA, B, C g a partition of f1, . . . , n g g [A, B, C ℄
  • [A
n fr g, B [ fr g, C ℄ if (r = min A ) ^ (r < min B ) [A, B, C ℄
  • [A
n fr g, B, C [ fr g ℄ if (r = min A ) ^ (r < min C ) [A, B, C ℄
  • [A
[ fr g, B n fr g, C ℄ if (r = min B ) ^ (r < min A ) [A, B, C ℄
  • [A, B
n fr g, C [ fr g ℄ if (r = min B ) ^ (r < min C ) [A, B, C ℄
  • [A
[ fr g, B, C n fr g ℄ if (r = min C ) ^ (r < min A ) [A, B, C ℄
  • [A, B
[ fr g, C n fr g ℄ if (r = min C ) ^ (r < min B )

Algorithms and Data Structures Chapter 1: Transition Systems 9

slide-10
SLIDE 10

Euclid’s algorithm

Transition system

Eu lid

Configurations:

f [m, n ℄ j m, n 1 g [m, n ℄
  • [m
n, n ℄

if m

> n [m, n ℄
  • [m, n
m ℄ if m < n

Algorithms and Data Structures Chapter 1: Transition Systems 10

slide-11
SLIDE 11

Expressions

Transition system

Exp ressions

Configurations:

f0, 1, +, E, T, (, ) g
  • αEβ
αTβ

αEβ

αT +Eβ

αTβ

α0β

αTβ

α1β

αTβ

α (E )β

Algorithms and Data Structures Chapter 1: Transition Systems 11

slide-12
SLIDE 12

Expressions (context-free)

Transition system

Exp ressions

Configurations:

f0, 1, +, E, T, (, ) g
  • E
T, T +E

T

0, 1, (E )

Algorithms and Data Structures Chapter 1: Transition Systems 12

slide-13
SLIDE 13

Graph coloring

Transition system

GraphColo ring

Configurations: Danish graphs

  • if there are no pink nodes

Algorithms and Data Structures Chapter 1: Transition Systems 13

slide-14
SLIDE 14

Red-black tree

Definition 1.5.7 A red-black tree is binary search tree in which all internal nodes are colored either red or black, in which the leaves are black, and Invariant I2 Each red node has a black parent. Invariant I3 There is the same number of black nodes on all paths from the root to a leaf.

  • 2

3 9 5 13 17 11 7

Algorithms and Data Structures Chapter 1: Transition Systems 14

slide-15
SLIDE 15

Insertion

Illegitimate red node:

x

Invariant I

2: Each legitimate red node has a black parent. Algorithms and Data Structures Chapter 1: Transition Systems 15

slide-16
SLIDE 16

Insertion: transitions 1 and 2

The illegitimate node is the root of the tree:

x 1 2

  • x

1 2

The illegitimate node has a black father:

A x 1 2 3

  • A

1 2 3 x

Algorithms and Data Structures Chapter 1: Transition Systems 16

slide-17
SLIDE 17

Insertion: transitions 3.1 and 3.2

The illegitimate node has a red father and a red uncle:

1 2 x B A C 4 5 3

  • A

C B x 1 2 3 4 5 1 2 3 5 4 A B C x

  • A

B C x 1 2 3 4 5

Algorithms and Data Structures Chapter 1: Transition Systems 17

slide-18
SLIDE 18

Insertion: transitions 4.1 and 4.2

The illegitimate node has a red father and a black uncle:

1 3 2 5 4 x B A C

  • B

x A C 1 3 2 5 4 x B A C 1 2 3 4 5

  • x

B A C 1 3 5 4 2

Algorithms and Data Structures Chapter 1: Transition Systems 18

slide-19
SLIDE 19

Deletion

Illegitimate black node: Invariant I

1 The tree satisfies I1 if we remove the illegitimate node.

Invariant I

2 Each red node has a legitimate black father. Algorithms and Data Structures Chapter 1: Transition Systems 19

slide-20
SLIDE 20

Deletion: transition 1

The illegitimate node is the root:

  • Algorithms and Data Structures

Chapter 1: Transition Systems 20

slide-21
SLIDE 21

Deletion: transitions 2 and 3

The illegitimate node has a red father and a red closer nephew:

A B C 1 2 3 4

  • 1

3 2 4 C A B

The illegitimate node has a red father and a black closer nephew:

1 2 3 4 A B C

  • 1

3 2 4 B A C

Algorithms and Data Structures Chapter 1: Transition Systems 21

slide-22
SLIDE 22

Deletion: transitions 4.1 and 4.2

The illegitimate node has a black father, a black sibling and one red nephew:

1 2 3 4 C B A

  • C

A B 1 2 3 4 1 B A C 2 3 4

  • A

1 2 3 4 B C

Algorithms and Data Structures Chapter 1: Transition Systems 22

slide-23
SLIDE 23

Deletion: transition 5

The illegitimate node has a black father, a black sibling and two black nephews

1 B A 2 3 C 4 D 5

  • A

B C 1 2 3 4 D 5

Algorithms and Data Structures Chapter 1: Transition Systems 23

slide-24
SLIDE 24

Deletion: transition 6

The illegitimate node has a black father and a red sibling:

1 2 B A 3

  • B

A 2 1 3

Algorithms and Data Structures Chapter 1: Transition Systems 24

slide-25
SLIDE 25

The transition system

Commands

Transition system

Commands

Configurations:

f [C,σ ℄ j C a command-sequence, σ a state g [x e; C 0,σ ℄
  • [C
0,σ hx:e i ℄ [ if if if b then then then C1 else else else C2; C 0,σ ℄
  • [C1; C
0,σ ℄

if σ

(b ) = true [ if if if b then then then C1 else else else C2; C 0,σ ℄
  • [C2; C
0,σ ℄

if σ

(b ) = false [ while while while b do do do C; C 0,σ ℄
  • [C;
while while while b do do do C; C 0,σ ℄ if σ (b ) = true [ while while while b do do do C; C 0,σ ℄
  • [C
0,σ ℄

if σ

(b ) = false

Algorithms and Data Structures Chapter 2: Algorithm Theory 25

slide-26
SLIDE 26

Euclid’s algorithm

Algorithm

Eu lid (m, n )

Input : m, n

1

Output : r

= gcd (m0, n0 )

Method :

while while while m 6 = n do do do if if if m > n then then then

m

m n else else else

n

n m;

r

m

Algorithms and Data Structures Chapter 2: Algorithm Theory 26

slide-27
SLIDE 27

Execution of Euclid(72,45)

Step# Action State# m n r 72 45 1 m<>n 1 72 45 2 m > n 2 72 45 3 m <– m-n 3 27 45 4 m<>n 4 27 45 5 m > n 5 27 45 6 n <– n-m 6 27 18 7 m<>n 7 27 18 8 m > n 8 27 18 9 m <– m-n 9 9 18 10 m<>n 10 9 18 11 m > n 11 9 18 12 n <– n-m 12 9 9 13 m<>n 13 9 9 14 r <– m 14 9 9 9

slide-28
SLIDE 28

Correctness

Algorithm

A (
  • )

Input : In Output : Out Method : C Definition 2.3.1 The algorithm

A is correct if any process for the

transition system

Commands starting in a configuration [C,σ ℄, where

σ satisfies In, is finite and ends with a configuration σ

0 satisfying

Out.

  • Algorithms and Data Structures

Chapter 2: Algorithm Theory 27

slide-29
SLIDE 29

Decorations

[ fI g while while while b do do do C; C 0,σ ℄
  • [C;
fI g while while while b do do do C; C 0,σ ℄ if σ (b ) = true.

Definition 2.3.2 An assertion U of a decorated algorithm is valid for a process if for all configurations of the form

[ fU gC,σ ℄ in the

process, the assertion U is satisfied by the state σ.

  • Algorithms and Data Structures

Chapter 2: Algorithm Theory 28

slide-30
SLIDE 30

Euclid’s algorithm (decorated)

Algorithm

Eu lid (m, n )

Input : m, n

1

Output : r

= gcd (m0, n0 )

Method :

fI g while while while m 6 = n do do do if if if m > n then then then

m

m n else else else

n

n m;

r

m

I : gcd

(m, n ) = gcd (m0, n0 ),

Algorithms and Data Structures Chapter 2: Algorithm Theory 29

slide-31
SLIDE 31

Validity

Algorithm

A (
  • )

Input : In Output : Out Method : C Definition 2.3.3 The algorithm

A is valid if all its assertions are valid

for all processes starting in a configuration

[C,σ ℄ where σ satisfies

In.

  • Algorithms and Data Structures

Chapter 2: Algorithm Theory 30

slide-32
SLIDE 32

Proof-burdens

fU gC fV g

For any state σ, if σ satisfies U and the execution of C in σ leads to σ

0 (ie. [C,σ ℄
  • σ
0), then σ 0 must satisfy V.

Algorithms and Data Structures Chapter 2: Algorithm Theory 31

slide-33
SLIDE 33

Proof principle for simple proof-burdens

Proof principle for simple proof-burdens Let C

= c1;
  • ; ck be a

sequence of assignments and let x1, . . . , xn be the variables of the

  • algorithm. Suppose that C executed in σ leads to σ
  • 0. Then the proof-

burden

fU gC fV g is proved by proving the implication

U

(x1, . . . , xn ) ) V (x

1, . . . , x n

)

—where x

1, . . . , x n are the values of the variables in σ

0 expressed as

functions of their values in σ.

Algorithms and Data Structures Chapter 2: Algorithm Theory 32

slide-34
SLIDE 34

Proof principle for compound proof-burdens

Proof principle for compound proof-burdens A proof-burden of the form

fU gC1; C2 fV g gives rise to the proof-burdens fU gC1 fW g

and

fW gC2 fV g.

A proof-burden of the form

fU g if if if b then then then C1 else else else C2 fV g gives rise to

the proof-burdens

fU ^ b gC1 fV g

and

fU ^ :b gC2 fV g.

A proof-burden of the form

fU g while while while b do do do C fV g gives rise to the

proof-burdens U

) I

(basis)

fI ^ b gC fI g (invariance)

I

^ :b ) V (conclusion).

Algorithms and Data Structures Chapter 2: Algorithm Theory 33

slide-35
SLIDE 35

Termination

Algorithm

A (
  • )

Input : In Output : Out Method : C Definition 2.3.4 We say that

A terminates if any process starting in a

configuration

[C,σ ℄, where σ satisfies In, is finite.
  • Algorithms and Data Structures

Chapter 2: Algorithm Theory 34

slide-36
SLIDE 36

Termination principle

Termination principle for algorithms Let x1, . . . , xn be the variables

  • f an algoritm
A. A terminates if for every loop fI g while while while b do do do C

in its method with I as valid invariant, there exists an integer-valued function µ

(x1, . . . , xn ) satisfying

a) I

) µ (x1, . . . , xn )

b) I

^ b ) µ (x1, . . . , xn ) > µ (x

1, . . . , x n

)

—where x

1, . . . , x n are the values of the variables after an iteration

expressed as functions of their values before.

Algorithms and Data Structures Chapter 2: Algorithm Theory 35

slide-37
SLIDE 37

Extended version of Euclid’s algorithm

Algorithm

ExtendedEu lid (m, n )

Input : m, n

1

Output :

(r = gcd (m0, n0 ) ) ^ (s = lcm (m0, n0 ) )

Method : p

m; q n; fI g while while while m 6 = n do do do if if if m > n then then then

m

m n; p p + q else else else

n

n m; q q + p;

r

m; s (p + q ) =2

I :

(mq + np = 2m0n0 ) ^ (gcd (m, n ) = gcd (m0, n0 ) ).

Algorithms and Data Structures Chapter 2: Algorithm Theory 36

slide-38
SLIDE 38

Factorial

Algorithm

F a to rial (n )

Input : n Output : r

= n0!

Method : r

1; fI g while while while n 6 = 0 do do do

r

r n;

n

n 1;

I :

(r = n0! =n! ) ^ (n0 n ).

Algorithms and Data Structures Chapter 2: Algorithm Theory 37

slide-39
SLIDE 39

Power sum

Algorithm

P
  • w
erSum (x, n )

Input :

(x 6 = 0 ) ^ (n )

Constants: x, n Output : r

= n

i

=0xi

Method : r

1; m 0; fI g while while while m 6 = n do do do

r

r x + 1;

m

m + 1;

I :

(r = m

i

=0xi ) ^ (n m ).

Algorithms and Data Structures Chapter 2: Algorithm Theory 38

slide-40
SLIDE 40

Finding maximum in an array

Algorithm

  • A
  • Input

:

  • Constants: A

Output : r

max A

Method : r

  • ; i
0; I
  • i
  • A
  • r
A i ℄
  • r
A i ℄;

i

i 1

I :

i
  • A
  • r
max A 0..i ℄
  • µ
A, i, r
  • A
  • i

Algorithms and Data Structures Chapter 2: Algorithm Theory 39

slide-41
SLIDE 41

Time complexity

Algorithm

  • x1, . . . , xn
  • Input

: In Output : Out Method : C Definition 2.6.1 The time complexity of

is the function T taking

a state σ satisfying In to the length of the process starting at

C,σ ℄.
  • We can regard T
as a function of the input parameters x1, . . . , xn

Algorithms and Data Structures Chapter 2: Algorithm Theory 40

slide-42
SLIDE 42

Time complexity for

  • Steps

r

  • ; i
0;

2

I
  • i
  • A
  • A
  • 1
  • r
A i ℄
  • r
A i ℄;

between

A and 2 A
  • i
i 1 A
  • Total number of steps:

3

A
  • 3
T
  • A
  • 4
A
  • 3

More precisely, T

  • A
equals 3 A
  • 3 plus the number of

times A

i ℄ is strictly larger than max A 0..i ℄, with i running through

the indices 0, . . . ,

A
  • 1.

Algorithms and Data Structures Chapter 2: Algorithm Theory 41

slide-43
SLIDE 43

Time complexity for Euclid

  • m
  • n
  • m
n
  • m
m n
  • n
n m

m n T m n T m n T m n T m n T

  • 1 1

2 2 1 5 3 1 8 4 1 11 5 1 14

  • 1 2

5 2 2 2 3 2 8 4 2 5 5 2 11

  • 1 3

8 2 3 8 3 3 2 4 3 11 5 3 11

  • 1 4 11 2 4

5 3 4 11 4 4 2 5 4 14

  • 1 5 14 2 5 11 3 5 11 4 5 14 5 5

2

  • 1 6 17 2 6

8 3 6 5 4 6 8 5 6 17

  • .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Algorithms and Data Structures Chapter 2: Algorithm Theory 42

slide-44
SLIDE 44

Worst-case time complexity

Algorithm

  • x1, . . . , xn
  • Input

: In Output : Out Method : C Definition 2.6.2 Let size be a function mapping states satisfying In to non-negative integers. The worst-case time complexity of

is the

function mapping n

0 to the maximum of T
  • σ
for states σ

(satisfying In) with size

σ
  • n.
  • We can regard size as a function of the input parameters x1, . . . , xn,

and write T

  • A
  • 4
A
  • 3
  • A
  • Algorithms and Data Structures

Chapter 2: Algorithm Theory 43

slide-45
SLIDE 45

Exponentiation in linear time

Algorithm

  • x, p
  • Input

: p Constants: x, p Output : r

xp

Method : r

1; q p; I
  • q
  • r
r x; q q 1

I :

rxq xp
  • q
  • µ
x, p, r, q
  • q

Algorithms and Data Structures Chapter 3: Fundamental algorithms 44

slide-46
SLIDE 46

Exponentiation in logarithmic time

Algorithm

  • x, p
  • Input

: p Constants: x, p Output : r

xp

Method : r

1; q p; h x; I
  • q
  • q even
  • q
q 2; h h h
  • q
q 1; r r h

I :

rhq xp
  • q
  • µ
x, p, r, q, h
  • q

Algorithms and Data Structures Chapter 3: Fundamental algorithms 45

slide-47
SLIDE 47

Scanning

  • A

i

I :

i
  • A
  • I
  • µ
A, i, . . .
  • A
  • i
  • ; i
0; I
  • i
  • A
  • ; i
i 1;
  • Algorithms and Data Structures

Chapter 3: Fundamental algorithms 46

slide-48
SLIDE 48

Linear search

Algorithm

  • A, s
  • Input

:

  • Constants: A, s

Output :

r
  • A
  • s
  • A
0..r ℄
  • r
  • A
  • A
r ℄ s
  • Method

: r

0; I
  • r
  • A
  • A
r ℄
  • s
  • r
r 1;

I :

r
  • A
  • s
  • A
0..r ℄
  • µ
A, s, r
  • A
  • r

Algorithms and Data Structures Chapter 3: Fundamental algorithms 47

slide-49
SLIDE 49

Binary search: specification and invariant

Algorithm

  • A, s
  • Input

: A sorted Constants: A, s Output :

r
  • A
  • A
0..r ℄ s
  • s
A r.. A
  • low

A high

low

0; high
  • A
; I
  • low
  • high
  • ;

r

low

Algorithms and Data Structures Chapter 3: Fundamental algorithms 48

slide-50
SLIDE 50

Binary search: the algorithm

Algorithm

  • A, s
  • Input

: A sorted Constants: A, s Output :

r
  • A
  • A
0..r ℄ s
  • s
A r.. A
  • Method

: low

0; high
  • A
; I
  • low
  • high
  • m
  • low
high
  • 2;
  • A
m ℄ s
  • low
m 1
  • high
m;

r

low

I :

low high
  • A
  • A
0..low ℄ s
  • s
A high.. A
  • µ
A, s, low, high, m, r
  • high
low

Algorithms and Data Structures Chapter 3: Fundamental algorithms 49

slide-51
SLIDE 51

Insertion sort

Algorithm

  • A
  • Input

:

  • Output

:

A perm A0
  • A sorted
  • Method

: i

0; I
  • i
  • A
  • j
i; J
  • j
  • A
j 1 ℄ A j ℄
  • A
j 1 ℄ A j ℄;

j

j 1;

i

i 1

I :

A perm A0
  • i
  • A
  • A
0..i ℄ sorted
  • J :
A perm A0
  • j
i
  • A
  • A
0..j ℄,A j..i 1 ℄ sorted
  • µI
A, i, j
  • A
  • i

µJ

A, i, j
  • j

Algorithms and Data Structures Chapter 3: Fundamental algorithms 50

slide-52
SLIDE 52

Merge sort

Algorithm

  • A
  • Input

:

  • Output

:

A perm A0
  • A sorted
  • Method

:

  • A
  • 1
  • B
A 0.. A
  • 2
℄;

C

A
  • A
  • 2..
A
  • ℄;
  • B
;
  • C
; W
  • A, B, C
  • Algorithm
  • A, B, C
  • Input

:

  • A
  • B
  • C
  • B, C sorted
  • Constants: B, C

Output :

A perm BC
  • A sorted
  • W :
A A0
  • B perm A1
  • C perm A2
  • B, C sorted
  • µ
A
  • A
  • Algorithms and Data Structures

Chapter 3: Fundamental algorithms 51

slide-53
SLIDE 53

Merge sort: time complexity, assuming n a power of 2

level 4 level 3 level 2 level 1 level 0

Time spent at level i (for 0

i log n): 2i n 2i n time units.

Algorithms and Data Structures Chapter 3: Fundamental algorithms 52

slide-54
SLIDE 54

Merge: specification

Algorithm

  • A, B, C
  • Input

:

  • A
  • B
  • C
  • B, C sorted
  • Constants: B, C

Output :

A perm BC
  • A sorted
  • So, with
A
  • 11 and

B

  • 1, 1, 4, 7, 8

and C

  • 1, 2, 2, 3, 4, 7

we should obtain A

  • 1, 1, 1, 2, 2, 3, 4, 4, 7, 7, 8
℄.

Algorithms and Data Structures Chapter 3: Fundamental algorithms 53

slide-55
SLIDE 55

Merge: scanning invariant

  • A

k=i+j B i C j

I :

  • A
  • B
  • C
  • i
  • B
  • j
  • C
  • A
0..i j ℄ perm B 0..i ℄C 0..j ℄
  • A
0..i j ℄ sorted
  • ;
I
  • i
j
  • A
  • ;
  • Algorithms and Data Structures

Chapter 3: Fundamental algorithms 54

slide-56
SLIDE 56

Merge: the algorithm

Algorithm

  • A, B, C
  • Input

:

  • A
  • B
  • C
  • B, C sorted
  • Constants: B, C

Output :

A perm BC
  • A sorted
  • Method

: i

0; j 0; I
  • i
j
  • A
  • i
  • B
  • j
  • C
  • B
i ℄ C j ℄
  • A
i j ℄ B i ℄; i i 1
  • A
i j ℄ C j ℄; j j 1

I :

  • A
  • B
  • C
  • i
  • B
  • j
  • C
  • A
0..i j ℄ perm B 0..i ℄C 0..j ℄
  • A
0..i j ℄ sorted
  • µ
A, B, C, i, j
  • A
  • i
j
  • Algorithms and Data Structures

Chapter 3: Fundamental algorithms 55

slide-57
SLIDE 57

Dutch flag: specification

w A b

Algorithm

A, s
  • Input

:

  • Constants: s

Output :

A perm A0
  • w
b
  • A
  • A
0..w ℄ s
  • A
w..b ℄ s
  • A
b.. A
s
  • Algorithms and Data Structures

Chapter 3: Fundamental algorithms 56

slide-58
SLIDE 58

Dutch flag: scanning invariant

  • i

b A w

I :

A perm A0
  • w
b i
  • A
  • A
0..w ℄ s
  • A
w..b ℄ s
  • A
b..i ℄ s
  • ; i
0; I
  • i
  • A
  • ; i
i 1;
  • Algorithms and Data Structures

Chapter 3: Fundamental algorithms 57

slide-59
SLIDE 59

Dutch flag: basis

  • ; i
I
  • w=b=i

A

  • w
0; b

Algorithms and Data Structures Chapter 3: Fundamental algorithms 58

slide-60
SLIDE 60

Dutch flag: invariance

I i
  • A
  • ; i
i 1 I
  • A
i ℄ s
  • A
i ℄ s
  • 1 :
I
  • i
  • A
  • A
i ℄ s
  • ; i
i 1 I
  • 2 :
I
  • i
  • A
  • A
i ℄ s
  • ; i
i 1 I
  • 3 :
I
  • i
  • A
  • A
i ℄ s
  • ; i
i 1 I
  • Algorithms and Data Structures

Chapter 3: Fundamental algorithms 59

slide-61
SLIDE 61

Dutch flag: invariance, red

I
  • i
  • A
  • A
i ℄ s
  • ; i
i 1 I
  • i

b A w

  • A
i ℄ A w ℄; w w 1; A i ℄ A b ℄; b b 1

Algorithms and Data Structures Chapter 3: Fundamental algorithms 60

slide-62
SLIDE 62

Dutch flag: invariance, white

I
  • i
  • A
  • A
i ℄ s
  • ; i
i 1 I
  • i

b A w

  • A
i ℄ A b ℄; b b 1

Algorithms and Data Structures Chapter 3: Fundamental algorithms 61

slide-63
SLIDE 63

Dutch flag: invariance, blue

I
  • i
  • A
  • A
i ℄ s
  • ; i
i 1 I
  • i

b A w

  • λ

Algorithms and Data Structures Chapter 3: Fundamental algorithms 62

slide-64
SLIDE 64

Dutch flag: conclusion

I
  • i
  • A
  • Out
  • w

A b i

  • λ

Algorithms and Data Structures Chapter 3: Fundamental algorithms 63

slide-65
SLIDE 65

Dutch flag: the algorithm

Algorithm

A, s
  • Input

:

  • Constants: s

Output :

A perm A0
  • w
b
  • A
  • A
0..w ℄ s
  • A
w..b ℄ s
  • A
b.. A
s
  • Method

: w

0; b 0; i 0; I
  • i
  • A
  • A
i ℄ s
  • A
i ℄ A w ℄; w w 1; A i ℄ A b ℄; b b 1
  • A
i ℄ s
  • A
i ℄ A b ℄; b b 1;

i

i 1

I :

A perm A0
  • w
b i
  • A
  • A
0..w ℄ s
  • A
w..b ℄ s
  • A
b..i ℄ s
  • µ
A, s, w, b, i
  • A
  • i

Algorithms and Data Structures Chapter 3: Fundamental algorithms 64

slide-66
SLIDE 66

Dutch flag: from low to high

  • A

low w b high

Algorithm

A, low, high, s
  • Input

: 0

low high
  • A
  • Constants: low, high, A
0..low ℄, A high.. A
  • ℄, s

Output :

A low..high ℄ perm A0 low..high ℄
  • low
w b high
  • A
low..w ℄ s
  • A
w..b ℄ s
  • A
b..high ℄ s
  • Algorithms and Data Structures

Chapter 3: Fundamental algorithms 65

slide-67
SLIDE 67

Quick sort

Algorithm

  • A, low, high
  • Input

: 0

low high
  • A
  • Constants: low, high, A
0..low ℄, A high.. A

Output :

A low..high ℄ perm A0 low..high ℄
  • A
low..high ℄ sorted
  • Method

:

  • high
low 1
  • r
  • low, high
; w, b
  • A, low, high, A
r ℄ ;
  • A, low, w
;
  • A, b, high
  • µ
A, low, high
  • high
low

Algorithms and Data Structures Chapter 3: Fundamental algorithms 66

slide-68
SLIDE 68

Quick select

Algorithm

A, low, high, k
  • Input

:

low k high
  • A
  • Constants: low, high, A
0..low ℄, A high.. A
  • ℄, k

Output :

A low..high ℄ perm A0 low..high ℄
  • A
low..k ℄ A k ℄ A k..high ℄
  • Method

: r

  • low, high
; w, b
  • A, low, high, A
r ℄ ;
  • k
w
  • A, low, w, k
  • k
b
  • A, b, high, k
  • µ
A, low, high
  • high
low

Algorithms and Data Structures Chapter 3: Fundamental algorithms 67

slide-69
SLIDE 69

Maximal subsum

Algorithm

  • A
  • Input

:

  • Constants: A

Output : r

ms A
  • Method

:

  • ; i
0; I
  • i
  • A
  • ; i
i 1;
  • I :
i
  • A
  • r
ms A 0..i ℄
  • Algorithms and Data Structures

Chapter 3: Fundamental algorithms 68

slide-70
SLIDE 70

Maximal subsum: maintaining the invariant

ms

A 0..i 1 ℄ is the maximum of

the maximal subsum obtained by not using A

i ℄, and

the maximal subsum obtained by using A

i ℄

So, we remember also the maximal right subsum mrs

A 0..i ℄
  • I :
i
  • A
  • r
ms A 0..i ℄
  • h
mrs A 0..i ℄
  • Algorithms and Data Structures

Chapter 3: Fundamental algorithms 69

slide-71
SLIDE 71

Maximal subsum: the algorithm

Algorithm

  • A
  • Input

:

  • Constants: A

Output : m

ms A
  • Method

: r

0; h 0; i 0; I
  • i
  • A
  • h
max h A i ℄, 0 ;

r

max r, h ;

i

i 1

µ

A, r, h, i
  • A
  • i

Algorithms and Data Structures Chapter 3: Fundamental algorithms 70

slide-72
SLIDE 72

Longest monotone sequence

Algorithm

  • A
  • Input

:

  • Constants: A

Output : r

llms A
  • Method

:

  • ; i
0; I
  • i
  • A
  • ; i
i 1;
  • I :
i
  • A
  • r
llms A 0..i ℄
  • Algorithms and Data Structures

Chapter 3: Fundamental algorithms 71

slide-73
SLIDE 73

Longest monotone sequence: maintaining the invariant

In the i’th iteration of the loop, B

l ℄ for 1 l
  • A
must contain

the necessary information about a monotone sequence from A

0..i ℄ of length l whose last element is minimal (if there is no

sequence of length l, we just record that). I :

i
  • A
  • r
llms A 0..i ℄
  • B
l ℄ msl A 0..i ℄ for 0 l
  • A
  • msl
A 0..i ℄
  • if l

minimal last element in a monotone sequence of length l from A

0..i ℄

if such exists

  • therwise

Algorithms and Data Structures Chapter 3: Fundamental algorithms 72

slide-74
SLIDE 74

Longest monotone sequence: the algorithm

Algorithm

  • A
  • Input

:

  • Constants: A

Output : r

llms A
  • Method

: B

  • A
  • 1 :
  • ℄; B
  • ; r
0; i 0; I
  • i
  • A
  • l
  • B, A
i ℄ ;

B

l ℄ A i ℄; r max r, l ; i i 1

µ

A, B, r, i
  • A
  • i

Algorithm

  • B, s
  • Input

: B sorted Constants: B, s Output :

l
  • B
  • B
0..l ℄ s
  • s
B l.. B
  • Algorithms and Data Structures

Chapter 3: Fundamental algorithms 73