Docsity

CS 161 — Bipartite Graph Algorithms, Exams of Nursing

Toggle sidebar
bg1
CS 161 — Bipartite Graph Algorithms
Table of Contents
1. Key Definitions
2. Problem 1 — Bipartite Recognition
3. Problem 2 — Matchings and Perfect Matchings
4. Problem 3 — Alternating and Augmenting Paths
5. Problem 4 — Maximum Matching via Max Flow
6. Problem 5 — Stable Matching
7. Problem 6 — Runtime Analysis
8. Key Takeaways
Key Definitions
A graph G=(V,E)G = (V, E)G=(V,E) is bipartite if its vertices can be partitioned into two
independent sets UUU and VVV, such that every edge has one endpoint in UUU and one
endpoint in VVV.cs161-stanford.github
A matching is a set of edges with no shared endpoints. A perfect matching matches every
vertex exactly once. In a bipartite graph with parts V1V_1V1 and V2V_2V2, a perfect matching
requires ∣V1∣=∣V2∣|V_1| = |V_2|∣V1∣=∣V2∣. stanford-cs161.github+1
An alternating path alternates between unmatched and matched edges. An augmenting path is
an alternating path that begins and ends at unmatched vertices; flipping its edges increases the
matching size by 1.columbia
Problem 1 — Bipartite Recognition
10 points
Design an algorithm that determines whether an undirected graph G=(V,E)G = (V, E)G=(V,E) is
bipartite. State its running time and prove correctness.
Solution
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download CS 161 — Bipartite Graph Algorithms and more Exams Nursing in PDF only on Docsity!

CS 161 — Bipartite Graph Algorithms

Table of Contents

  1. Key Definitions
  2. Problem 1 — Bipartite Recognition
  3. Problem 2 — Matchings and Perfect Matchings
  4. Problem 3 — Alternating and Augmenting Paths
  5. Problem 4 — Maximum Matching via Max Flow
  6. Problem 5 — Stable Matching
  7. Problem 6 — Runtime Analysis
  8. Key Takeaways

Key Definitions

A graph G=(V,E)G = (V, E)G=(V,E) is bipartite if its vertices can be partitioned into two independent sets UUU and VVV, such that every edge has one endpoint in UUU and one endpoint in VVV.cs161-stanford.github

A matching is a set of edges with no shared endpoints. A perfect matching matches every vertex exactly once. In a bipartite graph with parts V1V_1V1 and V2V_2V2, a perfect matching requires ∣V1∣=∣V2∣|V_1| = |V_2|∣V1∣=∣V2∣. stanford-cs161.github+

An alternating path alternates between unmatched and matched edges. An augmenting path is an alternating path that begins and ends at unmatched vertices; flipping its edges increases the matching size by 1.columbia

Problem 1 — Bipartite Recognition

10 points

Design an algorithm that determines whether an undirected graph G=(V,E)G = (V, E)G=(V,E) is bipartite. State its running time and prove correctness.

Solution

Use BFS or DFS with two colors.

text IS-BIPARTITE(G): color every vertex as uncolored

for each vertex s in V: if s is uncolored: color[s] = RED queue = [s]

while queue is not empty: u = dequeue(queue)

for each neighbor v of u: if v is uncolored: color[v] = opposite(color[u]) enqueue(queue, v) else if color[v] = color[u]: return False

return True

Correctness

If GGG is bipartite, let UUU and VVV be its two parts. Color every vertex in UUU red and every vertex in VVV blue. Every edge connects different colors, so the algorithm never finds a same-color edge.

If the algorithm returns False, it found an edge (u,v)(u, v)(u,v) whose endpoints have the same color. In a BFS or DFS tree, adjacent vertices differ by one level in parity. A same-color edge therefore creates an odd cycle, and a graph with an odd cycle cannot be bipartite.

Running Time

With adjacency lists, BFS visits each vertex once and examines each edge a constant number of times:

Θ(n+m)\Theta(n + m)Θ(n+m)

Stanford’s CS 161 section materials describe exactly this coloring approach using DFS, with the same odd-cycle logic.cs161-stanford.github

Problem 2 — Matchings and Perfect Matchings

15 points

Problem 3 — Alternating and Augmenting Paths

20 points

Let MMM be a matching in a bipartite graph GGG.

Part A — 5 points

Define an alternating path.

Part B — 5 points

Define an augmenting path.

Part C — 5 points

Explain why augmenting along an augmenting path increases the size of the matching by exactly

Part D — 5 points

State the central theorem connecting augmenting paths and maximum matchings.

Solutions

Part A

An alternating path is a path whose edges alternate between edges not in the current matching MMM and edges in MMM.columbia

Part B

An augmenting path is an alternating path that begins and ends at unmatched vertices.columbia

Part C

Suppose an augmenting path has kkk unmatched edges and k−1k - 1k−1 matched edges. Flip the membership of every edge on the path:

 Remove the k−1k - 1k−1 matched edges from MMM.  Add the kkk unmatched edges to MMM.

The new matching has size:

∣M∣−(k−1)+k=∣M∣+1|M| - (k - 1) + k = |M| + 1∣M∣−(k−1)+k=∣M∣+

Thus, augmentation increases the matching size by exactly 1.columbia

Part D

A matching MMM is maximum if and only if there is no augmenting path with respect to MMM.

Therefore, a standard maximum-matching algorithm starts with the empty matching and repeatedly finds an augmenting path until none remains.columbia

Problem 4 — Maximum Matching via Max Flow

20 points

Let G=(V1∪V2,E)G = (V_1 \cup V_2, E)G=(V1∪V2,E) be an unweighted bipartite graph with ∣V1∣=∣V2∣=n|V_1| = |V_2| = n∣V1∣=∣V2∣=n and mmm edges.

Describe how to determine whether GGG has a perfect matching using a max-flow reduction. State the flow-network construction, the correctness argument, and the running time.

Solution

Flow-network construction

Construct a directed flow network HHH:

  1. Direct every original bipartite edge from V1V_1V1 to V2V_2V2.
  2. Add a source sss.
  3. Add a sink ttt.
  4. Add a directed edge from sss to every vertex in V1V_1V1.
  5. Add a directed edge from every vertex in V2V_2V2 to ttt.
  6. Assign capacity 1 to every edge in HHH.

Then compute a maximum sss-ttt flow using Ford-Fulkerson.stanford-cs161.github+

Correctness

If GGG has a perfect matching MMM, send 1 unit of flow along each matched path:

Solution

Algorithm

text Gale-Shapley(doctors, hospitals): initialize every doctor and hospital as free

while some doctor i is free and has not applied to every hospital: j = highest-ranked hospital to which i has not yet applied i applies to j

if j is free: match i and j else if j prefers i to its current doctor k: match i and j make k free else: j rejects i

This is the Deferred Acceptance Algorithm, also called Gale-Shapley.stanford-cs161.github+

Why it terminates

Each doctor applies to each hospital at most once. With nnn doctors and nnn hospitals, there are at most:

n2n^2n

applications total. Therefore, the algorithm terminates after at most O(n2)O(n^2)O(n2) iterations.stanford-cs161.github

Why it produces a stable matching

Suppose doctor iii and hospital jjj form an unstable pair. Then iii prefers jjj to their assigned hospital, and jjj prefers iii to its assigned doctor.

But if iii preferred jjj, then iii must have applied to jjj earlier. If jjj preferred iii, then jjj would not have rejected iii in favor of a less-preferred doctor. This contradicts the algorithm’s behavior.

Therefore, no blocking pair exists, and the returned matching is stable.stanford-cs161.github+

Problem 6 — Runtime Analysis

20 points

Let G=(V1∪V2,E)G = (V_1 \cup V_2, E)G=(V1∪V2,E) be a bipartite graph with nnn vertices on each side and mmm edges.

For each statement, determine whether it is True or False.

Part A — 5 points

Bipartiteness can be tested in O(n+m)O(n + m)O(n+m) time.

Part B — 5 points

A maximum bipartite matching can be found by reducing to max flow.

Part C — 5 points

Using Ford-Fulkerson, the bipartite perfect-matching decision problem can be solved in O(mn)O(mn)O(mn) time.

Part D — 5 points

Gale-Shapley always terminates in O(n)O(n)O(n) time for nnn doctors and nnn hospitals.

Solutions

Part A

True. BFS or DFS coloring examines each vertex and edge a constant number of times, giving O(n+m)O(n + m)O(n+m).cs161-stanford.github

Part B

True. Add a source, sink, and unit-capacity edges. A maximum flow corresponds to a maximum matching.stanford-cs161.github+

Part C

True. The maximum flow value is at most nnn, so Ford-Fulkerson performs at most nnn