target = 22 left=0 · right=10
mid = left + ⌊(right − left)/2⌋
a[left] ≤ target ≤ a[right]
→ 2 ≤ 22 ≤ 99 ✓
a[mid] < target ⇒ left = mid + 1
a[mid] > target ⇒ right = mid − 1
a[mid] = target ⇒ found
size 11
O(log n), 4 probes
target = 22 left=0 · right=10 · mid=5
mid = 0 + ⌊(10 − 0)/2⌋ = 5
a[left] ≤ target ≤ a[right]
→ 2 ≤ 22 ≤ 99 ✓
a[mid] < target ⇒ left = mid + 1
a[mid] > target ⇒ right = mid − 1
a[mid] = target ⇒ found
size 11
O(log n), 4 probes
target = 22 left=0 · right=4
mid = left + ⌊(right − left)/2⌋
a[left] ≤ target ≤ a[right]
→ 2 ≤ 22 ≤ 22 ✓
a[mid] < target ⇒ left = mid + 1
a[mid] > target ⇒ right = mid − 1
a[mid] = target ⇒ found
size 11 → 5
O(log n), 4 probes
target = 22 left=0 · right=4 · mid=2
mid = 0 + ⌊(4 − 0)/2⌋ = 2
a[left] ≤ target ≤ a[right]
→ 2 ≤ 22 ≤ 22 ✓
a[mid] < target ⇒ left = mid + 1
a[mid] > target ⇒ right = mid − 1
a[mid] = target ⇒ found
size 11 → 5
O(log n), 4 probes
target = 22 left=3 · right=4
mid = left + ⌊(right − left)/2⌋
a[left] ≤ target ≤ a[right]
→ 12 ≤ 22 ≤ 22 ✓
a[mid] < target ⇒ left = mid + 1
a[mid] > target ⇒ right = mid − 1
a[mid] = target ⇒ found
size 11 → 5 → 2
O(log n), 4 probes
target = 22 left=3 · right=4 · mid=3
mid = 3 + ⌊(4 − 3)/2⌋ = 3
a[left] ≤ target ≤ a[right]
→ 12 ≤ 22 ≤ 22 ✓
a[mid] < target ⇒ left = mid + 1
a[mid] > target ⇒ right = mid − 1
a[mid] = target ⇒ found
size 11 → 5 → 2
O(log n), 4 probes
target = 22 left=4 · right=4
mid = left + ⌊(right − left)/2⌋
a[left] ≤ target ≤ a[right]
→ 22 ≤ 22 ≤ 22 ✓
a[mid] < target ⇒ left = mid + 1
a[mid] > target ⇒ right = mid − 1
a[mid] = target ⇒ found
size 11 → 5 → 2 → 1
O(log n), 4 probes
target = 22 left=4 · right=4 · mid=4
mid = 4 + ⌊(4 − 4)/2⌋ = 4
a[left] ≤ target ≤ a[right]
→ 22 ≤ 22 ≤ 22 ✓
a[mid] < target ⇒ left = mid + 1
a[mid] > target ⇒ right = mid − 1
a[mid] = target ⇒ found
size 11 → 5 → 2 → 1
O(log n), 4 probes
1def topo_sort(adj, indeg):
2 q = deque(v for v in adj if indeg[v] == 0)
3 order = []
4 while q:
5 u = q.popleft()
6 order.append(u)
7 for v in adj[u]:
8 indeg[v] -= 1
9 if indeg[v] == 0:
10 q.append(v)
11 return order # < n nodes => cycle
A node is ready once everything pointing into it is done (in-degree 0). A node that never gets there means a cycle.
1def topo_sort(adj, indeg):
2 q = deque(v for v in adj if indeg[v] == 0)
3 order = []
4 while q:
5 u = q.popleft()
6 order.append(u)
7 for v in adj[u]:
8 indeg[v] -= 1
9 if indeg[v] == 0:
10 q.append(v)
11 return order # < n nodes => cycle
A node is ready once everything pointing into it is done (in-degree 0). A node that never gets there means a cycle.
1def topo_sort(adj, indeg):
2 q = deque(v for v in adj if indeg[v] == 0)
3 order = []
4 while q:
5 u = q.popleft()
6 order.append(u)
7 for v in adj[u]:
8 indeg[v] -= 1
9 if indeg[v] == 0:
10 q.append(v)
11 return order # < n nodes => cycle
A node is ready once everything pointing into it is done (in-degree 0). A node that never gets there means a cycle.
1def topo_sort(adj, indeg):
2 q = deque(v for v in adj if indeg[v] == 0)
3 order = []
4 while q:
5 u = q.popleft()
6 order.append(u)
7 for v in adj[u]:
8 indeg[v] -= 1
9 if indeg[v] == 0:
10 q.append(v)
11 return order # < n nodes => cycle
A node is ready once everything pointing into it is done (in-degree 0). A node that never gets there means a cycle.
1def topo_sort(adj, indeg):
2 q = deque(v for v in adj if indeg[v] == 0)
3 order = []
4 while q:
5 u = q.popleft()
6 order.append(u)
7 for v in adj[u]:
8 indeg[v] -= 1
9 if indeg[v] == 0:
10 q.append(v)
11 return order # < n nodes => cycle
A node is ready once everything pointing into it is done (in-degree 0). A node that never gets there means a cycle.
1def topo_sort(adj, indeg):
2 q = deque(v for v in adj if indeg[v] == 0)
3 order = []
4 while q:
5 u = q.popleft()
6 order.append(u)
7 for v in adj[u]:
8 indeg[v] -= 1
9 if indeg[v] == 0:
10 q.append(v)
11 return order # < n nodes => cycle
A node is ready once everything pointing into it is done (in-degree 0). A node that never gets there means a cycle.
1def topo_sort(adj, indeg):
2 q = deque(v for v in adj if indeg[v] == 0)
3 order = []
4 while q:
5 u = q.popleft()
6 order.append(u)
7 for v in adj[u]:
8 indeg[v] -= 1
9 if indeg[v] == 0:
10 q.append(v)
11 return order # < n nodes => cycle
A node is ready once everything pointing into it is done (in-degree 0). A node that never gets there means a cycle.
1def dfs(u):
2 disc[u] = low[u] = len(disc) + 1
3 stack.append(u); on_stack[u] = True
4 for v in adj[u]:
5 if v not in disc: # tree edge
6 dfs(v)
7 low[u] = min(low[u], low[v])
8 elif on_stack[v]: # back edge
9 low[u] = min(low[u], disc[v])
10 if low[u] == disc[u]: # root of SCC
11 sccs.append(pop_until(stack, u))
low[u] = earliest discovery time reachable from u’s subtree plus one back edge to the stack. If low[u] = disc[u], nothing escapes, so u roots an SCC.
1def dfs(u):
2 disc[u] = low[u] = len(disc) + 1
3 stack.append(u); on_stack[u] = True
4 for v in adj[u]:
5 if v not in disc: # tree edge
6 dfs(v)
7 low[u] = min(low[u], low[v])
8 elif on_stack[v]: # back edge
9 low[u] = min(low[u], disc[v])
10 if low[u] == disc[u]: # root of SCC
11 sccs.append(pop_until(stack, u))
low[u] = earliest discovery time reachable from u’s subtree plus one back edge to the stack. If low[u] = disc[u], nothing escapes, so u roots an SCC.
1def dfs(u):
2 disc[u] = low[u] = len(disc) + 1
3 stack.append(u); on_stack[u] = True
4 for v in adj[u]:
5 if v not in disc: # tree edge
6 dfs(v)
7 low[u] = min(low[u], low[v])
8 elif on_stack[v]: # back edge
9 low[u] = min(low[u], disc[v])
10 if low[u] == disc[u]: # root of SCC
11 sccs.append(pop_until(stack, u))
low[u] = earliest discovery time reachable from u’s subtree plus one back edge to the stack. If low[u] = disc[u], nothing escapes, so u roots an SCC.
1def dfs(u):
2 disc[u] = low[u] = len(disc) + 1
3 stack.append(u); on_stack[u] = True
4 for v in adj[u]:
5 if v not in disc: # tree edge
6 dfs(v)
7 low[u] = min(low[u], low[v])
8 elif on_stack[v]: # back edge
9 low[u] = min(low[u], disc[v])
10 if low[u] == disc[u]: # root of SCC
11 sccs.append(pop_until(stack, u))
low[u] = earliest discovery time reachable from u’s subtree plus one back edge to the stack. If low[u] = disc[u], nothing escapes, so u roots an SCC.
1def dfs(u):
2 disc[u] = low[u] = len(disc) + 1
3 stack.append(u); on_stack[u] = True
4 for v in adj[u]:
5 if v not in disc: # tree edge
6 dfs(v)
7 low[u] = min(low[u], low[v])
8 elif on_stack[v]: # back edge
9 low[u] = min(low[u], disc[v])
10 if low[u] == disc[u]: # root of SCC
11 sccs.append(pop_until(stack, u))
low[u] = earliest discovery time reachable from u’s subtree plus one back edge to the stack. If low[u] = disc[u], nothing escapes, so u roots an SCC.
1def dfs(u):
2 disc[u] = low[u] = len(disc) + 1
3 stack.append(u); on_stack[u] = True
4 for v in adj[u]:
5 if v not in disc: # tree edge
6 dfs(v)
7 low[u] = min(low[u], low[v])
8 elif on_stack[v]: # back edge
9 low[u] = min(low[u], disc[v])
10 if low[u] == disc[u]: # root of SCC
11 sccs.append(pop_until(stack, u))
low[u] = earliest discovery time reachable from u’s subtree plus one back edge to the stack. If low[u] = disc[u], nothing escapes, so u roots an SCC.
1def dfs(u):
2 disc[u] = low[u] = len(disc) + 1
3 stack.append(u); on_stack[u] = True
4 for v in adj[u]:
5 if v not in disc: # tree edge
6 dfs(v)
7 low[u] = min(low[u], low[v])
8 elif on_stack[v]: # back edge
9 low[u] = min(low[u], disc[v])
10 if low[u] == disc[u]: # root of SCC
11 sccs.append(pop_until(stack, u))
low[u] = earliest discovery time reachable from u’s subtree plus one back edge to the stack. If low[u] = disc[u], nothing escapes, so u roots an SCC.
1def dfs(u):
2 disc[u] = low[u] = len(disc) + 1
3 stack.append(u); on_stack[u] = True
4 for v in adj[u]:
5 if v not in disc: # tree edge
6 dfs(v)
7 low[u] = min(low[u], low[v])
8 elif on_stack[v]: # back edge
9 low[u] = min(low[u], disc[v])
10 if low[u] == disc[u]: # root of SCC
11 sccs.append(pop_until(stack, u))
low[u] = earliest discovery time reachable from u’s subtree plus one back edge to the stack. If low[u] = disc[u], nothing escapes, so u roots an SCC.
1def dfs(u):
2 disc[u] = low[u] = len(disc) + 1
3 stack.append(u); on_stack[u] = True
4 for v in adj[u]:
5 if v not in disc: # tree edge
6 dfs(v)
7 low[u] = min(low[u], low[v])
8 elif on_stack[v]: # back edge
9 low[u] = min(low[u], disc[v])
10 if low[u] == disc[u]: # root of SCC
11 sccs.append(pop_until(stack, u))
low[u] = earliest discovery time reachable from u’s subtree plus one back edge to the stack. If low[u] = disc[u], nothing escapes, so u roots an SCC.
1def dfs(u):
2 disc[u] = low[u] = len(disc) + 1
3 stack.append(u); on_stack[u] = True
4 for v in adj[u]:
5 if v not in disc: # tree edge
6 dfs(v)
7 low[u] = min(low[u], low[v])
8 elif on_stack[v]: # back edge
9 low[u] = min(low[u], disc[v])
10 if low[u] == disc[u]: # root of SCC
11 sccs.append(pop_until(stack, u))
low[u] = earliest discovery time reachable from u’s subtree plus one back edge to the stack. If low[u] = disc[u], nothing escapes, so u roots an SCC.
1def dfs(u):
2 disc[u] = low[u] = len(disc) + 1
3 stack.append(u); on_stack[u] = True
4 for v in adj[u]:
5 if v not in disc: # tree edge
6 dfs(v)
7 low[u] = min(low[u], low[v])
8 elif on_stack[v]: # back edge
9 low[u] = min(low[u], disc[v])
10 if low[u] == disc[u]: # root of SCC
11 sccs.append(pop_until(stack, u))
low[u] = earliest discovery time reachable from u’s subtree plus one back edge to the stack. If low[u] = disc[u], nothing escapes, so u roots an SCC.
1def dfs(u):
2 disc[u] = low[u] = len(disc) + 1
3 stack.append(u); on_stack[u] = True
4 for v in adj[u]:
5 if v not in disc: # tree edge
6 dfs(v)
7 low[u] = min(low[u], low[v])
8 elif on_stack[v]: # back edge
9 low[u] = min(low[u], disc[v])
10 if low[u] == disc[u]: # root of SCC
11 sccs.append(pop_until(stack, u))
low[u] = earliest discovery time reachable from u’s subtree plus one back edge to the stack. If low[u] = disc[u], nothing escapes, so u roots an SCC.
1def dfs(u):
2 disc[u] = low[u] = len(disc) + 1
3 stack.append(u); on_stack[u] = True
4 for v in adj[u]:
5 if v not in disc: # tree edge
6 dfs(v)
7 low[u] = min(low[u], low[v])
8 elif on_stack[v]: # back edge
9 low[u] = min(low[u], disc[v])
10 if low[u] == disc[u]: # root of SCC
11 sccs.append(pop_until(stack, u))
low[u] = earliest discovery time reachable from u’s subtree plus one back edge to the stack. If low[u] = disc[u], nothing escapes, so u roots an SCC.
1def dfs(u):
2 disc[u] = low[u] = len(disc) + 1
3 stack.append(u); on_stack[u] = True
4 for v in adj[u]:
5 if v not in disc: # tree edge
6 dfs(v)
7 low[u] = min(low[u], low[v])
8 elif on_stack[v]: # back edge
9 low[u] = min(low[u], disc[v])
10 if low[u] == disc[u]: # root of SCC
11 sccs.append(pop_until(stack, u))
low[u] = earliest discovery time reachable from u’s subtree plus one back edge to the stack. If low[u] = disc[u], nothing escapes, so u roots an SCC.