week 11 - friday. what did we talk about last time? exam 2 post mortem cycle detection ...
TRANSCRIPT
CS221Week 11 - Friday
Last time
What did we talk about last time? Exam 2 post mortem Cycle detection Connectivity
Questions?
Spellchecker
Project 3
Assignment 6
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?
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
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
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
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
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 )
Find the strongly connected components
A
B
I
D
F
H
C
E
G
Topological SortStudent Lecture
Topological Sort
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
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
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
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
Matching
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
Bipartite graph
A B C D E F
A B C D E F
X
Y
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
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
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
Stable Marriage
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
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
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
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
Quiz
Upcoming
Next time…
Finish matching and stable marriage Euler paths and tours
Reminders
Finish Project 3 Due tonight before midnight
Start Assignment 6 Due next Friday
Keep reading Chapter 8