Docsity

Decision-Tree Example - Lecture Slides | CS 3343, Study notes of Algorithms and Programming

Toggle sidebar
bg1
CS 5633 Analysis of Algorithms 2/09/06
© 2001 by Charles E. Leiserson; small changes by Carola
Wenk 1
CS 3343 Analysis of Algorithms 110/9/07
CS 3343 – Fall 2007
Sorting
Carola Wenk
Slides courtesy of Charles Leiserson with small
changes by Carola Wenk
CS 3343 Analysis of Algorithms 210/9/07
How fast can we sort?
All the sorting algorithms we have seen so far
are comparison sorts: only use comparisons to
determine the relative order of elements.
•E.g., insertion sort, merge sort, quicksort,
heapsort.
The best worst-case running time that we’ve
seen for comparison sorting is O(nlog n).
Is O(n log n) the best we can do?
Decision trees can help us answer this question.
CS 3343 Analysis of Algorithms 310/9/07
Decision-tree example
a1:a2
a1:a2
a2:a3
a2:a3
a1a2a3
a1a2a3a1:a3
a1:a3
a1a3a2
a1a3a2a3a1a2
a3a1a2
a1:a3
a1:a3
a2a1a3
a2a1a3a2:a3
a2:a3
a1a2a3
a1a2a3a3a2a1
a3a2a1
Each internal node is labeled ai:ajfor i, j ∈{1, 2,…, n}.
•The left subtree shows subsequent comparisons if ai≤aj.
•The right subtree shows subsequent comparisons if ai≥aj.
Sort 〈a1, a2, …, an〉≤
≤
≤
≤
≤
≥
≥
≥
≥
≥
CS 3343 Analysis of Algorithms 410/9/07
Decision-tree example
a1:a2
a1:a2
a2:a3
a2:a3
a1a2a3
a1a2a3a1:a3
a1:a3
a1a3a2
a1a3a2a3a1a2
a3a1a2
a1:a3
a1:a3
a2a1a3
a2a1a3a2:a3
a2:a3
a1a2a3
a1a2a3a3a2a1
a3a2a1
Each internal node is labeled ai:ajfor i, j ∈{1, 2,…, n}.
•The left subtree shows subsequent comparisons if ai≤aj.
•The right subtree shows subsequent comparisons if ai≥aj.
≤
≤
≤
≤
≤
≥
≥
≥
≥
≥
Sort 〈a1, a2, a3〉
= 〈 9, 4, 6 〉:
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Decision-Tree Example - Lecture Slides | CS 3343 and more Study notes Algorithms and Programming in PDF only on Docsity!

CS 3343 Analysis of Algorithms

1

10/9/

CS 3343 – Fall 2007

Sorting

Carola Wenk

Slides courtesy of Charles Leiserson with small

changes by Carola Wenk

CS 3343 Analysis of Algorithms

2

10/9/

How fast can we sort?

All the sorting algorithms we have seen so farare

comparison sorts

: only use comparisons to

determine the relative order of elements.•

E.g

., insertion sort, merge sort, quicksort,

heapsort.

The best worst-case running time that we’veseen for comparison sorting is

O

n

log

n

Is O(n log n) the best we can do?

Decision trees

can help us answer this question.

CS 3343 Analysis of Algorithms

3

10/9/

Decision-tree example

a

1

a

2

a

1

a

2

a

2

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

3

a

1

a

3

a

1

a

3

a

1

a

3

a

2

a

1

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

1

a

3

a

1

a

3

a

2

a

1

a

3

a

2

a

1

a

3

a

2

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

3

a

3

a

2

a

1

a

3

a

2

a

1

Each internal node is labeled

a

i

:

a

j

for

i

,

j

∈

{1, 2,…,

n

}.

  • The left subtree shows subsequent comparisons if

a

i

≤

a

j

.

  • The right subtree shows subsequent comparisons if

a

i

≥

a

j

.

Sort

a

1

a

2

a

n

≤

≤

≤

≤

≤

≥

≥

≥

≥

≥

CS 3343 Analysis of Algorithms

4

10/9/

Decision-tree example

a

1

a

2

a

1

a

2

a

2

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

3

a

1

a

3

a

1

a

3

a

1

a

3

a

2

a

1

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

1

a

3

a

1

a

3

a

2

a

1

a

3

a

2

a

1

a

3

a

2

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

3

a

3

a

2

a

1

a

3

a

2

a

1

Each internal node is labeled

a

i

:

a

j

for

i

,

j

∈

{1, 2,…,

n

}.

  • The left subtree shows subsequent comparisons if

a

i

≤

a

j

.

  • The right subtree shows subsequent comparisons if

a

i

≥

a

j

.

≤

≤

≤

≤

≤

≥

≥

≥

≥

≥

Sort

a

1

a

2

a

3

CS 3343 Analysis of Algorithms

5

10/9/

Decision-tree example

a

1

a

2

a

1

a

2

a

2

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

3

a

1

a

3

a

1

a

3

a

1

a

3

a

2

a

1

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

1

a

3

a

1

a

3

a

2

a

1

a

3

a

2

a

1

a

3

a

2

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

3

a

3

a

2

a

1

a

3

a

2

a

1

Each internal node is labeled

a

i

:

a

j

for

i

,

j

∈

{1, 2,…,

n

}.

  • The left subtree shows subsequent comparisons if

a

i

≤

a

j

.

  • The right subtree shows subsequent comparisons if

a

i

≥

a

j

.

≤

≤

≤

≤

≤

≥

≥

≥

≥

Sort

a

1

a

2

a

3

9

≥

4

CS 3343 Analysis of Algorithms

6

10/9/

Decision-tree example

a

1

a

2

a

1

a

2

a

2

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

3

a

1

a

3

a

1

a

3

a

1

a

3

a

2

a

1

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

1

a

3

a

1

a

3

a

2

a

1

a

3

a

2

a

1

a

3

a

2

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

3

a

3

a

2

a

1

a

3

a

2

a

1

Each internal node is labeled

a

i

:

a

j

for

i

,

j

∈

{1, 2,…,

n

}.

  • The left subtree shows subsequent comparisons if

a

i

≤

a

j

.

  • The right subtree shows subsequent comparisons if

a

i

≥

a

j

.

≤

≤

≤

≤

≤

≥

≥

≥

≥

9

≥

6

Sort

a

1

a

2

a

3

CS 3343 Analysis of Algorithms

7

10/9/

Decision-tree example

a

1

a

2

a

1

a

2

a

2

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

3

a

1

a

3

a

1

a

3

a

1

a

3

a

2

a

1

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

1

a

3

a

1

a

3

a

2

a

1

a

3

a

2

a

1

a

3

a

2

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

3

a

3

a

2

a

1

a

3

a

2

a

1

Each internal node is labeled

a

i

:

a

j

for

i

,

j

∈

{1, 2,…,

n

}.

  • The left subtree shows subsequent comparisons if

a

i

≤

a

j

.

  • The right subtree shows subsequent comparisons if

a

i

≥

a

j

.

≤

≤

≤

≤

≥

≥

≥

≥

≥

4

≤

6

Sort

a

1

a

2

a

3

CS 3343 Analysis of Algorithms

8

10/9/

Decision-tree example

a

1

a

2

a

1

a

2

a

2

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

3

a

1

a

3

a

1

a

3

a

1

a

3

a

2

a

1

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

2

a

1

a

3

a

1

a

3

a

2

a

1

a

3

a

2

a

1

a

3

a

2

a

3

a

2

a

3

a

2

a

3

a

1

a

2

a

3

a

1

a

3

a

2

a

1

a

3

a

2

a

1

≤

≤

≤

≤

≤

≥

≥

≥

≥

≥

Each leaf contains a permutation

〈π(1)

,

π(2)

,…,

π

(

n

)

〉

to

indicate that the ordering

a

π

(1)

≤

a

π

(2)

≤

L

≤

a

π

(n)

has been

established.

4

≤

6

≤

9

Sort

a

1

a

2

a

3

CS 3343 Analysis of Algorithms

13

10/9/

Counting sort

for

i

to

k

do

C

[

i

]

for

j

to

n

do

C

[

A

[

j

]]

C

[

A

[

j

]] + 1

C

[

i

] = |{key =

i

for

i

to

k

do

C

[

i

]

C

[

i

] +

C

[

i

–1]

C

[

i

] = |{key

i

for

j

n

downto

do

B

[

C

[

A

[

j

]]]

A[

j

]

C

[

A

[

j

]]

C

[

A

[

j

]] – 1

1. 2. 3. 4.

CS 3343 Analysis of Algorithms

10/9/

Counting-sort example

A

B

1

2

3

4

5

C

1

2

3

4

CS 3343 Analysis of Algorithms

15

10/9/

Loop 1

A

B

1

2

3

4

5

C

1

2

3

4

for

i

to

k

do

C

[

i

]

1.

CS 3343 Analysis of Algorithms

10/9/

Loop 2

A

B

1

2

3

4

5

C

1

2

3

4

for

j

to

n

do

C

[

A

[

j

]]

C

[

A

[

j

]] + 1

C

[

i

] = |{key =

i

2.

CS 3343 Analysis of Algorithms

17

10/9/

Loop 2

A

B

1

2

3

4

5

C

1

2

3

4

for

j

to

n

do

C

[

A

[

j

]]

C

[

A

[

j

]] + 1

C

[

i

] = |{key =

i

2.

CS 3343 Analysis of Algorithms

10/9/

Loop 2

A

B

1

2

3

4

5

C

1

2

3

4

for

j

to

n

do

C

[

A

[

j

]]

C

[

A

[

j

]] + 1

C

[

i

] = |{key =

i

2.

CS 3343 Analysis of Algorithms

19

10/9/

Loop 2

A

B

1

2

3

4

5

C

1

2

3

4

for

j

to

n

do

C

[

A

[

j

]]

C

[

A

[

j

]] + 1

C

[

i

] = |{key =

i

2.

CS 3343 Analysis of Algorithms

10/9/

Loop 2

A

B

1

2

3

4

5

C

1

2

3

4

for

j

to

n

do

C

[

A

[

j

]]

C

[

A

[

j

]] + 1

C

[

i

] = |{key =

i

2.

CS 3343 Analysis of Algorithms

25

10/9/

Loop 4

A

B

1

2

3

4

5

C

1

2

3

4

C'

for

j

n

downto

do

B

[

C

[

A

[

j

]]]

A[

j

]

C

[

A

[

j

]]

C

[

A

[

j

]] – 1

4.

CS 3343 Analysis of Algorithms

10/9/

Loop 4

A

B

1

2

3

4

5

C

1

2

3

4

C'

for

j

n

downto

do

B

[

C

[

A

[

j

]]]

A[

j

]

C

[

A

[

j

]]

C

[

A

[

j

]] – 1

4.

CS 3343 Analysis of Algorithms

27

10/9/

Loop 4

A

B

1

2

3

4

5

C

1

2

3

4

C'

for

j

n

downto

do

B

[

C

[

A

[

j

]]]

A[

j

]

C

[

A

[

j

]]

C

[

A

[

j

]] – 1

4.

CS 3343 Analysis of Algorithms

10/9/

Loop 4

A

B

1

2

3

4

5

C

1

2

3

4

C'

for

j

n

downto

do

B

[

C

[

A

[

j

]]]

A[

j

]

C

[

A

[

j

]]

C

[

A

[

j

]] – 1

4.

CS 3343 Analysis of Algorithms

29

10/9/

Loop 4

A

B

1

2

3

4

5

C

1

2

3

4

C'

for

j

n

downto

do

B

[

C

[

A

[

j

]]]

A[

j

]

C

[

A

[

j

]]

C

[

A

[

j

]] – 1

4.

CS 3343 Analysis of Algorithms

10/9/

Loop 4

A

B

1

2

3

4

5

C

1

2

3

4

C'

for

j

n

downto

do

B

[

C

[

A

[

j

]]]

A[

j

]

C

[

A

[

j

]]

C

[

A

[

j

]] – 1

4.

CS 3343 Analysis of Algorithms

31

10/9/

Loop 4

A

B

1

2

3

4

5

C

1

2

3

4

C'

for

j

n

downto

do

B

[

C

[

A

[

j

]]]

A[

j

]

C

[

A

[

j

]]

C

[

A

[

j

]] – 1

4.

CS 3343 Analysis of Algorithms

10/9/

Loop 4

A

B

1

2

3

4

5

C

1

2

3

4

C'

for

j

n

downto

do

B

[

C

[

A

[

j

]]]

A[

j

]

C

[

A

[

j

]]

C

[

A

[

j

]] – 1

4.