Week 11 - Friday
What did we talk about last time? Exam 2 post mortem Cycle detection Connectivity
Spellchecker
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?
Startup 1. Set the number of all nodes to 0 2. 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 0 DFS( u, i ) If any node has a number of 0, the graph is not connected
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
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
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 0 StrongDFS( u, i ) predecessor(v) = min(predecessor(v), predecessor(u)) Else if number(u) < number(v) and u is 0n the stack predecessor(v) = min(predecessor(v), number(u)) 5. If predecessor(v) is number(v) w = pop() while w is not v output w w = pop() output w
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 0 StrongDFS( v, i )
A B I D F H C E G
Student Lecture
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
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
Give a topological sort for the following DAG: A F I C G K D J E H A A H H E E J J D D K K G G C C F F I I
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
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
A A B B C C D D E E F F A A B B C C D D E E F F X Y
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
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
A A B B C C D D E E F F A A B B C C D D E E F F X Y Anna Becky CaitlinDaisyErinFiona Adam Ben CarlosDanEvanFred
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
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
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
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
Finish matching and stable marriage Euler paths and tours
Finish Project 3 Due tonight before midnight Start Assignment 6 Due next Friday Keep reading Chapter 8