1
00:00:00,000 --> 00:00:04,480
A directed edge from one task to another
says that the first task must

2
00:00:04,560 --> 00:00:05,920
precede the second.

3
00:00:06,160 --> 00:00:10,840
Our analysis plan uses invented tasks:
reads and a reference precede

4
00:00:10,880 --> 00:00:15,440
alignment, alignment precedes counts, and
counts precede a report.

5
00:00:15,840 --> 00:00:18,080
Metadata is an independent task.

6
00:00:18,480 --> 00:00:22,760
A valid order includes every task once and
respects every edge.

7
00:00:22,960 --> 00:00:27,600
This graph describes precedence; it does
not run the analysis or choose a schedule

8
00:00:27,680 --> 00:00:28,720
for limited workers.

9
00:00:29,040 --> 00:00:33,440
We will count unfinished prerequisites and
use a queue to discover which task can

10
00:00:33,520 --> 00:00:34,320
come next.

11
00:00:34,633 --> 00:00:38,633
Count each incoming edge to obtain the
initial indegree of every

12
00:00:38,713 --> 00:00:39,753
vertex.

13
00:00:39,873 --> 00:00:44,553
Put all zero-indegree vertices into a
ready queue, including disconnected

14
00:00:44,873 --> 00:00:45,593
vertices.

15
00:00:45,673 --> 00:00:50,233
Emit one ready vertex by removing it from
the front of the queue and appending it to

16
00:00:50,273 --> 00:00:51,073
the order.

17
00:00:51,113 --> 00:00:54,513
Remove its outgoing edges from the
remaining indegrees.

18
00:00:54,553 --> 00:00:58,673
Release a dependent into the queue only
when its count reaches zero.

19
00:00:58,713 --> 00:01:02,953
Kahn's algorithm repeats these updates
until the queue is empty, then checks

20
00:01:03,033 --> 00:01:05,753
whether the order contains every vertex.

21
00:01:06,000 --> 00:01:09,520
The outgoing lists name each task's
dependents.

22
00:01:09,600 --> 00:01:14,600
Reads points to align, reference points to
align, align points to count,

23
00:01:14,640 --> 00:01:16,680
and count points to report.

24
00:01:16,720 --> 00:01:21,320
Alignment has two incoming edges, while
count and report each have one.

25
00:01:21,360 --> 00:01:24,520
Reads, reference, and metadata have zero.

26
00:01:24,560 --> 00:01:29,200
The initial queue therefore contains all
three of those tasks in dictionary order.

27
00:01:29,280 --> 00:01:33,600
Starting a traversal from reads alone
would omit metadata and would not account

28
00:01:33,640 --> 00:01:35,480
for the reference prerequisite.

29
00:01:35,520 --> 00:01:40,160
Every task must appear as a key, including
tasks with no dependents.

30
00:01:40,400 --> 00:01:42,240
Emit reads first.

31
00:01:42,440 --> 00:01:47,280
Removing its outgoing edge reduces align's
count from two to one, so align

32
00:01:47,360 --> 00:01:48,773
stays out of the queue.

33
00:01:48,827 --> 00:01:50,520
Emit reference next.

34
00:01:50,560 --> 00:01:54,880
Its edge reduces that remaining count to
zero, and align joins the back of the

35
00:01:54,960 --> 00:01:55,560
queue.

36
00:01:55,600 --> 00:01:59,640
Emit metadata before align because
metadata was already waiting.

37
00:01:59,680 --> 00:02:01,400
It changes no other count.

38
00:02:01,440 --> 00:02:04,800
This tie policy comes from the queue and
the dictionary order.

39
00:02:04,880 --> 00:02:09,520
Several valid orders can satisfy the same
dependency graph; this procedure does not

40
00:02:09,720 --> 00:02:11,360
promise alphabetical order.

41
00:02:11,600 --> 00:02:15,893
Emit align after both of its prerequisites
appear in the order.

42
00:02:15,947 --> 00:02:18,187
Its removal releases count.

43
00:02:18,213 --> 00:02:21,720
Emit count and its outgoing edge releases
report.

44
00:02:21,760 --> 00:02:24,853
Emit report to complete the six-task
order.

45
00:02:24,907 --> 00:02:29,760
For every unprocessed vertex, the stored
indegree equals its incoming edge count

46
00:02:29,840 --> 00:02:31,813
from unprocessed vertices.

47
00:02:31,867 --> 00:02:36,640
That invariant means a ready vertex has no
missing predecessor in the output.

48
00:02:36,800 --> 00:02:41,200
Removing exactly one emitted vertex's
outgoing edges preserves the invariant for

49
00:02:41,280 --> 00:02:42,720
the next iteration.

50
00:02:42,967 --> 00:02:47,447
A and B now depend on each other, creating
a directed cycle.

51
00:02:47,527 --> 00:02:52,047
C depends on B, but it has no outgoing
edge back into that cycle.

52
00:02:52,087 --> 00:02:56,047
C is blocked downstream without belonging
to the cycle itself.

53
00:02:56,087 --> 00:03:00,327
Only D has indegree zero, so the queue
emits D and then becomes

54
00:03:00,407 --> 00:03:01,087
empty.

55
00:03:01,127 --> 00:03:04,967
The emitted count is one while the graph
has four vertices.

56
00:03:05,127 --> 00:03:08,607
Returning that partial order would
incorrectly report success.

57
00:03:08,647 --> 00:03:13,207
The function raises ValueError because the
remaining graph contains a cycle, without

58
00:03:13,327 --> 00:03:16,487
claiming that every blocked vertex lies on
that cycle.

59
00:03:16,733 --> 00:03:21,453
Suppose a finite directed graph has no
zero-indegree vertex.

60
00:03:21,533 --> 00:03:25,253
Every vertex then has an incoming edge
from some predecessor.

61
00:03:25,293 --> 00:03:27,480
Follow a predecessor repeatedly.

62
00:03:27,586 --> 00:03:32,573
Because the graph has finitely many
vertices, that walk must revisit a vertex,

63
00:03:32,653 --> 00:03:34,466
producing a directed cycle.

64
00:03:34,600 --> 00:03:38,066
Apply the argument to the remaining graph
when the queue empties.

65
00:03:38,120 --> 00:03:42,386
If any vertices remain, their positive
indegrees imply a cycle.

66
00:03:42,440 --> 00:03:47,133
If the original graph is acyclic, each
nonempty remaining graph must instead

67
00:03:47,213 --> 00:03:50,333
contain a vertex that the procedure can
emit next.

68
00:03:50,600 --> 00:03:54,920
Our dictionary maps each prerequisite to
its outgoing dependents.

69
00:03:54,960 --> 00:03:59,480
The library dictionary accepted by
graphlib's TopologicalSorter maps each

70
00:03:59,560 --> 00:04:01,520
vertex to its predecessors.

71
00:04:01,560 --> 00:04:05,960
For align, that predecessor list contains
reads and reference.

72
00:04:06,120 --> 00:04:10,520
Convert each edge before passing the graph
to the library, and include vertices with

73
00:04:10,600 --> 00:04:12,400
empty predecessor lists.

74
00:04:12,440 --> 00:04:17,160
The library's static order produces one
valid ordering and raises CycleError if

75
00:04:17,320 --> 00:04:19,293
a cycle prevents completion.

76
00:04:19,427 --> 00:04:23,960
Comparing exact output lists can reject
another valid answer; check that every

77
00:04:24,160 --> 00:04:27,960
vertex occurs once and every edge points
forward instead.

78
00:04:28,200 --> 00:04:33,000
Each edge is counted once during
initialization and removed once after its

79
00:04:33,160 --> 00:04:34,920
source vertex is emitted.

80
00:04:35,040 --> 00:04:38,400
Each vertex enters and leaves the ready
queue once.

81
00:04:38,440 --> 00:04:42,840
Expected time is therefore O of V plus E,
assuming expected

82
00:04:43,080 --> 00:04:46,707
constant-time dictionary operations on
bounded-size keys.

83
00:04:46,733 --> 00:04:50,760
The auxiliary state contains indegrees and
the queue, using O of V

84
00:04:50,840 --> 00:04:51,747
space.

85
00:04:51,773 --> 00:04:55,520
The returned order also uses O of V output
space.

86
00:04:55,560 --> 00:05:00,013
Repeated adjacency entries are counted and
removed with their multiplicity.

87
00:05:00,147 --> 00:05:04,520
Printing every indegree after every step
adds display work that is absent from the

88
00:05:04,600 --> 00:05:05,800
ordinary function.

89
00:05:06,033 --> 00:05:10,433
Two prerequisites test whether a dependent
enters the queue too early.

90
00:05:10,673 --> 00:05:15,513
A disconnected vertex tests whether
initialization includes every task.

91
00:05:15,713 --> 00:05:20,513
A cycle with an outgoing dependent tests
whether you confuse blocked vertices with

92
00:05:20,593 --> 00:05:21,793
cycle members.

93
00:05:21,993 --> 00:05:26,673
Also test an empty graph, a self loop, a
repeated edge, and a neighbor

94
00:05:26,753 --> 00:05:28,433
absent from the dictionary.

95
00:05:28,513 --> 00:05:32,593
When several orders are allowed, validate
their edge constraints instead of one

96
00:05:32,673 --> 00:05:34,113
remembered sequence.

97
00:05:34,153 --> 00:05:39,153
The written lesson includes these cases, a
library comparison, and repeat exercises

98
00:05:39,233 --> 00:05:42,513
for the decision and construction versions
of course scheduling.
