announcements exam next week review on monday in class. review on wednesday in lab. disjointsets lab...

19
Announcements Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday.

Upload: bryce-gibbs

Post on 11-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

AnnouncementsAnnouncementsExam Next weekReview on

Monday in class.Review on

Wednesday in lab.DisjointSets lab

program due tonight.

BucketSort lab due next Wednesday.

Page 2: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

On Monday…On Monday…Radix Sort2 proofs:

◦ A Lower bound for comparison based sorting.

◦ A lower bound for swapping adjacent elements.

Graphs◦ Definitions◦ DFS◦ BFS

Page 3: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

TodayTodayQuick review of DFS and BFSTopological SortIntro to Dijkstra’s

Page 4: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

Depth First SearchDepth First Search

Start with 1 2,4,3

◦ Stop at 3 Backtrack to 4, can we search? 7,6

◦ Stop at 2 Backtrack to 6, can we search? 10,13

◦ Stop at 13

1 2 5

10

6

11

15

3 4 7 12

9

13

14

8

Backtrack to 10,6,79, 11, 8, 5

◦ Stop at 5Backtrack to 8,1115,14,12

◦ STOP – All nodes are marked!!

OutputList:

1 2 4 3 7 610

13

911

8 515

14

12

1 2

3

5

6

11

4

10

15

7 9

8

12

13

14

Page 5: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

Depth First Search – Depth First Search – Real Life Real Life ApplicationApplication

From xkcd.com

Page 6: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

Breadth First SearchBreadth First Search

L0: 1L1: 2, 3L2: 4,5,6,11L3: 7,8,10,9,15L4: 13,12,14

1 2 5

10

6

11

15

3 4 7 12

9

13

14

1 2

3

5

6

11

4

8

10

15

7 9

8

12

13

14

Final output: 1, 2, 3, 4, 5, 6, 11, 7, 8, 10, 9, 15, 13, 12, 14

Page 7: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

Topological SortTopological Sort

Page 8: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

Topological SortTopological SortThe goal of a Topological Sort:

◦ When given a list of items with dependencies (i.e. item 5 must be completed before item 3, etc.)

◦ Produce an ordering of the items that satisfies the given constraints.

In order for the problem to be solvable, there can’t be a cyclic set of constraints.◦ We can’t have item 5 must be completed

before item 3, item 3 must be completed before item 7, and item 7 must be completed before item 5. Since that would be IMPOSSIBLE!!

Page 9: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

Topological SortTopological Sort We can model dependencies

(or constraints) like these using

◦ A Directed Acyclic Graph

Given a set of items and constraints, we create the corresponding graph as follows:1)Each item corresponds to a vertex

in the graph.

2)For each constraint where item A must finish before item B, place a directed edge A B.

A stands for waking up,

B stands for taking a shower,

C stands for eating breakfast

D leaving for work◦ The constraints would be

A before B, A before C, B before D, and C before D.

A B

C D

A topological sort would give A, B, C, D.

Page 10: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

Topological SortTopological SortConsider the following CS classes and their

prereq’s:Prereq’s: COP 3223 before COP 3330 and

COP 3502. COP 3330 before COP 3503. COP 3502 before COT 3960, COP

3503, CDA 3103. COT 3100 before COT 3960. COT 3960 before COP 3402 and

COT 4210. COP 3503 before COT 4210.

The goal of a topological sort would be to find an ordering of these classes that you can take.

How Convenient!!How Convenient!!

CS classes:◦ COP 3223◦ COP 3502◦ COP 3330◦ COT 3100,◦ COP 3503◦ CDA 3103◦ COT 3960 (Foundation

Exam)◦ COP 3402◦ COT 4210.

Page 11: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

Topological Sort AlgorithmTopological Sort AlgorithmWe can use a DFS in our algorithm to

determine a topological sort!◦ The idea is:

When you do a DFS on a directed acyclic graph, eventually you will reach a node with no outgoing edges. Why? Because if this never happened, you hit a cycle, because the

number of nodes if finite. This node that you reach in a DFS is “safe” to place at

the end of the topological sort. Think of leaving for work!

◦ Now what we find is If we have added each of the vertices “below” a vertex

into our topological sort, it is safe then to add this one in. If we added in Leaving for work at the end, then we can

surely add taking a shower.

Page 12: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

Topological SortTopological Sort

If we explore from A◦ And if we have

marked B,C,D as well as anything connected And added all of them

into our topological sort in backwards order.

Then we can add A!

So basically all you have to do is run a DFS◦ But add the fact

that add the end of the recursive function, add the node the DFS was called with to the end of the topological sort.

A

B C D

Page 13: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

Topological Sort Step-by-Topological Sort Step-by-Step DescriptionStep Description1) Do a DFS starting with some node.

2) The “last” node you reach before you are forced to backtrack.

◦ Goes at the end of the topological sort.

3) Continue with your DFS, placing each “dead end” node into the topo sort in backwards order.

Page 14: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

Example on the BoardExample on the Board

Page 15: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

Topological Sort CodeTopological Sort Codetop_sort(Adjacency Matrix adj, Array ts)

{

n = adj.last

k = n //assume k is global

for i=1 to n

visit[i] = false

for i=1 to n

if (!visit[i])

top_sort_rec(adj, i, ts)

}

Page 16: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

Topological Sort CodeTopological Sort Codetop_sort_rec(Adjacency Matrix adj, Vertex start, Array ts)

{

visit[start] = true

trav = adj[start] //trav points to a LL

while (trav != null)

{

v = trav.ver

if (!visit[v])

top_sort_recurs(adj, v, ts);

trav = trav.next

}

ts[k] = start, k=k-1

}

Page 17: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

Topological Sort ExampleTopological Sort ExampleConsider the following items of

clothing to wear:

Shirt Slacks Shoes Socks Belt Undergarments

1 2 3 4 5 6

There isn't exactly one order to put these items on, but we must adhere to certain restrictions

◦ Socks must be put on before Shoes◦ Undergarments must be put on before Slacks and

Shirt◦ Slacks must be put on before Belt◦ Slacks must be put on before Shoes

Page 18: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

Finish Example on the Finish Example on the BoardBoard

Page 19: Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday

ReferencesReferencesSlides adapted from Arup Guha’s

Computer Science II Lecture notes: http://www.cs.ucf.edu/~dmarino/ucf/cop3503/lectures/

Additional material from the textbook:Data Structures and Algorithm Analysis in

Java (Second Edition) by Mark Allen WeissAdditional images:

www.wikipedia.comxkcd.com