34
CS221 Week 11 - Friday

Week 11 - Friday. What did we talk about last time? Exam 2 post mortem Cycle detection Connectivity

Embed Size (px)

Citation preview

Page 1: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

CS221Week 11 - Friday

Page 2: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Last time

What did we talk about last time? Exam 2 post mortem Cycle detection Connectivity

Page 3: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Questions?

Page 4: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Spellchecker

Project 3

Page 5: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Assignment 6

Page 6: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Connectivity

Page 7: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Connected graph?

Connected for an undirected graph:There is a path from every node to every other node

How can we determine if a graph is connected?

Page 8: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

DFS to the rescue again!

Startup1. Set the number of all nodes to 02. Pick an arbitrary node u and run DFS( u, 1 )

DFS( node v, int i )1. Set number(v) = i++2. For each node u adjacent to v

If number(u) is 0DFS( u, i )

If any node has a number of 0, the graph is not connected

Page 9: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Directed connectivity

Weakly connected directed graph:If the graph is connected when you make all the edges undirected

Strongly connected directed graph:If for every pair of nodes, there is a path between them in both directions

Page 10: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Short of strong connectivity Components of a directed graph can be

strongly connected A strongly connected component is a

subgraph such that all its nodes are strongly connected

To find strongly connected components, we can use a special DFS

It includes the notion of a predecessor node, which is the lowest numbered node in the DFS that can reach a particular node

Page 11: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

DFS used for strong connectivity StrongDFS( node v, int i )

1. Set number(v) = i++2. Set predecessor(v) to number(v)3. Push(v)4. For each node u adjacent to v

If number(u) is 0StrongDFS( u, i )predecessor(v) = min(predecessor(v), predecessor(u))

Else if number(u) < number(v) and u is 0n the stackpredecessor(v) = min(predecessor(v), number(u))

5. If predecessor(v) is number(v)w = pop()while w is not v

output ww = pop()

output w

Page 12: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Full strongly connected component algorithm

StronglyConnected( Vertices V ) Set i = 1 For each node v in V

Set number(v) = 0 For each node v in V

If number(v) is 0StrongDFS( v, i )

Page 13: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Find the strongly connected components

A

B

I

D

F

H

C

E

G

Page 14: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Topological SortStudent Lecture

Page 15: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Topological Sort

Page 16: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Directed acyclic graph

A directed acyclic graph (DAG) is a directed graph without cycles in it Well, obviously

These can be used to represent dependencies between tasks

An edge flows from the task that must be completed first to a task that must come after

This is a good model for course sequencing Especially during advising

A cycle in such a graph would mean there was a circular dependency

Page 17: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Topological sort

A topological sort gives an ordering of the tasks such that all tasks are completed in dependency ordering

In other words, no task is attempted before its prerequisite tasks have been done

There are usually multiple legal topological sorts for a given DAG

Page 18: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Topological sort

Give a topological sort for the following DAG:

A F I C G K D J E H

A

HE

JD

KG

C

F I

Page 19: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Topological sort algorithm Create list L Add all nodes with no incoming edges into set S While S is not empty

Remove a node u from S Add u to L For each node v with an edge e from u to v▪ Remove edge e from the graph▪ If v has no other incoming edges, add v to S

If the graph still has edges Print "Error! Graph has a cycle"

Otherwise Return L

Page 20: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Matching

Page 21: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Bipartite graphs

A bipartite graph is one whose nodes can be divided into two disjoint sets X and Y

There can be edges between set X and set Y

There are no edges inside set X or set Y

A graph is bipartite if and only if it contains no odd cycles

Page 22: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Bipartite graph

A B C D E F

A B C D E F

X

Y

Page 23: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Maximum matching

A perfect matching is when every node in set X and every node in set Y is matched

It is not always possible to have a perfect matching

We can still try to find a maximum matching in which as many nodes are matched up as possible

Page 24: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Matching algorithm

1. Come up with a legal, maximal matching

2. Take an augmenting path that starts at an unmatched node in X and ends at an unmatched node in Y

3. If there is such a path, switch all the edges along the path from being in the matching to being out and vice versa

4. If there is another augmenting path, go back to Step 2

Page 25: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Match the graph

A B C D E F

A B C D E F

X

Y

Anna

Becky

Caitlin

Daisy Erin Fiona

Adam

Ben Carlos

Dan Evan Fred

Page 26: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Stable Marriage

Page 27: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Imagine n men and n women

All 2n people want to get married Each woman has ranked all n men in

order of preference Each man has ranked all n women in

order of preference We want to match them up so that

the marriages are stable

Page 28: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Stability

Consider two marriages: Anna and Bob Caitlin and Dan

This pair of marriages is unstable if Anna likes Dan more than Bob and Dan

likes Anna more than Caitlin or Caitlin likes Bob more than Dan and Bob

likes Caitlin more than Anna We want to arrange all n marriages

such that none are unstable

Page 29: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Gale-Shapley algorithm

n rounds In each round, every unengaged man

proposes to the woman he likes best (who he hasn’t proposed to already)

An unengaged woman must accept the proposal from the one of her suitors she likes best

If a woman is already engaged, she accepts the best suitor only if she likes him better than her fiance

Page 30: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Lessons from Stable Marriage It’s a graph problem where we never look at

a graph The algorithm is guaranteed to terminate

A woman can’t refuse to get engaged If a man gets dumped, he will eventually

propose to everyone The algorithm is guaranteed to find stable

marriages Unfortunately, it’s male-optimal and female-

pessimal The lesson: Women should propose to men

Page 31: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Quiz

Page 32: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Upcoming

Page 33: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Next time…

Finish matching and stable marriage Euler paths and tours

Page 34: Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity

Reminders

Finish Project 3 Due tonight before midnight

Start Assignment 6 Due next Friday

Keep reading Chapter 8