Sie sind auf Seite 1von 21

CSCE 310: Decrease and Conquer Algorithms

September 2, 2016
Ryan Patrick

Outline
Homework Questions
Decrease-and-Conquer Algorithms
Decrease by a Constant
Topological Sorting
Combinatorial Objects

Decrease/Conquer
Patrick
Questions
Decrease &
Conquer
Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Permutations
Subsets

21

Outline
Homework Questions
Decrease-and-Conquer Algorithms
Decrease by a Constant
Topological Sorting
Combinatorial Objects

Decrease/Conquer
Patrick
3

Questions
Decrease &
Conquer
Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Permutations
Subsets

21

Homework Questions?
Decrease/Conquer
Patrick
4

Questions
Decrease &
Conquer
Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

21

Outline
Homework Questions
Decrease-and-Conquer Algorithms
Decrease by a Constant
Topological Sorting
Combinatorial Objects

Decrease/Conquer
Patrick
Questions
5

Decrease &
Conquer
Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Permutations
Subsets

21

Decrease-and-Conquer Algorithms
Decrease/Conquer
Patrick
Questions
6

Decrease &
Conquer
Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Sometimes Divide-and-Conquer Unnecessary


Binary Search
Decrease by More Than a Constant Factor?
Quicksort

21

Decrease by a Constant
Decrease/Conquer
Patrick
Questions
Decrease &
Conquer
7

Keep Structure of Larger Problem


Reduce Problem Size in Terms of Number of Elements

21

Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Decrease by a Constant
Example: Factorial
Decrease/Conquer
Patrick
Questions
Decrease &
Conquer
8

How can factorials be computed when decreasing by a constant?

21

Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Decrease by a Constant
Example: Factorial
Decrease/Conquer
Patrick
Questions

n!

Decrease &
Conquer
9

n (n 1)!

n 1 (n 2)!

..
.
21

Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Decrease by a Constant
Example: Insertion Sort
Decrease/Conquer
Patrick
Questions
Decrease &
Conquer
10

21

Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Topological Sorting
Decrease/Conquer
Patrick
Questions
Decrease &
Conquer
11

Order Nodes of Directed Graph


Must Be Directed, Acyclic Graph (DAG)

21

Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Directed Graphs
Decrease/Conquer
Patrick
Questions
Decrease &
Conquer
12

Also Known As Digraph


Graph with Directed Edges between Nodes
Asymmetric Adjacency Matrix/Lists

21

Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Topological Sort
Example: Course Prerequisites
Decrease/Conquer
Patrick

C1C1

C4C4

Questions
Decrease &
Conquer
13

C3C3

C2C2

C5C5
21

Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Topological Sort
Example: Course Prerequisites
Decrease/Conquer

Inorder Traversal with Tracked Dead Ends

Patrick

C1

Questions

C4

Decrease &
Conquer
14

C3

C2

C5
21

Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Topological Sort
Example: Course Prerequisites
Decrease/Conquer

Source Removal (A source is a node with no incoming edges)

Patrick

C1

Questions

C4

Decrease &
Conquer
15

C3

C2

C5
21

Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Topological Sort: Clicker Question


How many ways can you take 1 class per semester and complete the program in 5 semesters?
Decrease/Conquer
Patrick
Questions
Decrease &
Conquer

A. 1

16

B. 2
C. 3
D. 5
E. None of the Above

21

Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Permutations
Johnson-Trotter
Decrease/Conquer
Patrick
Questions
Decrease &
Conquer

17

21

Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Permutations
Lexicographical Order (Page 146)
Decrease/Conquer
Patrick
Questions
Decrease &
Conquer

1. First Permutation: 12 . . . n
2. i =largest index such that ai < ai+1
3. j =largest index such that ai < aj

18

4. Swap ai and aj
5. Reverse Order of ai+1 through an
6. Add Permutation to List
7. If Permutation Had Consecutive Elements in Increasing Order, Go to Step 2

21

Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Permutations: Lexicographical Order


Example: HUSK

Permutation
1234
1243
1324
1342
1423
1432
2134

i
3
2
3
2
3
1
3

j
4
3
4
3
4
4
4

2431
3124

1
3

3
4

3421
4123

1
3

2
4

4312

After Swap
1243
1423
1342
1432
1432
2431
2143
..
.
3421
3142
..
.
4321
4132
..
.
4321
DONE

After Reverse
1243
1324
1342
1423
1432
2134
2143
3124
3142
4123
4132
4321

Subsets
A = {a0 , a1 , . . . , an }
Decrease/Conquer
Patrick
Questions
Decrease &
Conquer

20

21

Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Any Questions?
Decrease/Conquer
Patrick
Questions
Decrease &
Conquer

21

21

Constant
Topological Sort
Combinatorial
Objects
Permutations
Subsets

Das könnte Ihnen auch gefallen