List of Induction problems
Posted on October 1, 2019
Tags: induction
1 Induction on queue
n
is the starting node of the bfsedgelist
is the neighbor nodes ofn
int n, vector<vector<int>>& edgelist, vector<int>& dist, queue<int>& q) {
void bfs(
for (int i : edgelist[n]) {
if (dist[i] != -1) continue;
= dist[n] + 1;
dist[i] ;
q.push(i)
}# invariant: top of queue is an immediate neighbor node of n
if (!q.empty()) {
= q.front();
n ;
q.pop();
bfs(n, edgelist, dist, q)
} }
The topological ordering on the neighbors of n
aka head of queue is the induction variable.
Front of Queue End of Queue n | neigh1_A | neigh1_B | neigh1_C | ... | neigh4_A | neigh4_B | ...
2 Examples
- PROBLEM: A set of N nodes of a COMPLETE graph with 1 way edges connecting them. Show there exist a path that reaches every node.
- SOLUTION:
- Pick X to be some arbitrary node.
- Partition K nodes to 2 set of disjoint nodes K and L
- let K nodes to be the complete path from an arbitrary origin Y that ENDS at X.
- let L nodes to be the complete path from an arbitrary origin Y that STARTS at X.
- K and L are disjoint sets of nodes
- IH is what allows us to build paths K and L