1
00:00:00,000 --> 00:00:02,640
A graph connects vertices through edges.

2
00:00:02,880 --> 00:00:07,120
It can contain cycles, multiple paths, and
disconnected components.

3
00:00:07,760 --> 00:00:12,080
Our example has a four-vertex cycle and
one isolated vertex.

4
00:00:12,560 --> 00:00:16,320
Breadth-first search uses a queue to find
unweighted distances.

5
00:00:16,720 --> 00:00:20,760
Depth-first search uses a stack to explore
connected components.

6
00:00:21,040 --> 00:00:25,440
Both algorithms must remember discovered
vertices so a cycle does not send them

7
00:00:25,520 --> 00:00:27,520
through the same vertices repeatedly.

8
00:00:27,833 --> 00:00:30,313
Remove a vertex from the frontier.

9
00:00:30,393 --> 00:00:34,473
A queue chooses the oldest pending vertex,
while a stack chooses the

10
00:00:34,553 --> 00:00:35,433
newest.

11
00:00:35,673 --> 00:00:38,740
Inspect neighbors through the adjacency
dictionary.

12
00:00:38,926 --> 00:00:43,913
Discover unseen vertices by marking each
one before inserting it into the frontier.

13
00:00:43,993 --> 00:00:47,620
This order prevents two parents from
adding the same vertex.

14
00:00:47,646 --> 00:00:52,233
A visited vertex never becomes unseen
again, so each vertex enters this

15
00:00:52,393 --> 00:00:54,313
frontier at most once.

16
00:00:54,533 --> 00:00:56,853
A connects to B and C.

17
00:00:56,973 --> 00:01:01,573
Both B and C connect to D, which creates
two routes from A to D.

18
00:01:01,693 --> 00:01:05,693
Each undirected edge appears in both
endpoint neighbor lists.

19
00:01:05,733 --> 00:01:09,813
E is isolated and still appears as a
dictionary key with an empty neighbor

20
00:01:09,893 --> 00:01:10,560
list.

21
00:01:10,586 --> 00:01:14,373
Omitting that key would lose the isolated
component when we later count

22
00:01:14,973 --> 00:01:15,613
components.

23
00:01:15,653 --> 00:01:20,213
Neighbor order can change traversal order,
but not unweighted distances.

24
00:01:20,467 --> 00:01:23,667
Expand A first, with distance zero.

25
00:01:23,907 --> 00:01:28,067
Discover B and C at distance one and
enqueue them in that order.

26
00:01:28,147 --> 00:01:31,867
Expand B next and discover D at distance
two.

27
00:01:31,907 --> 00:01:35,787
A is already known, so the edge back to A
adds no work.

28
00:01:35,827 --> 00:01:40,787
The queue contains C before D because all
distance-one work must finish before the

29
00:01:40,867 --> 00:01:42,187
next distance layer.

30
00:01:42,227 --> 00:01:45,987
That layer order makes the first
discovered distance shortest.

31
00:01:46,233 --> 00:01:50,633
Expand C and inspect D, which was already
marked when B discovered

32
00:01:50,753 --> 00:01:51,273
it.

33
00:01:51,393 --> 00:01:53,233
Do not enqueue D again.

34
00:01:53,273 --> 00:01:57,753
Expand D and find only known neighbors, so
the queue becomes empty.

35
00:01:57,833 --> 00:02:02,273
Distances are zero for A, one for B and C,
and two for D.

36
00:02:02,313 --> 00:02:05,326
E is absent because it is unreachable from
A.

37
00:02:05,380 --> 00:02:09,593
Do not assign it distance zero, which
already means the starting vertex.

38
00:02:09,833 --> 00:02:14,433
Initialize the start distance to zero and
put the start into a deque.

39
00:02:14,473 --> 00:02:17,406
Remove the oldest vertex with popleft.

40
00:02:17,460 --> 00:02:22,206
For each unseen neighbor, set its distance
to the current distance plus one.

41
00:02:22,340 --> 00:02:25,633
Append that neighbor only after the
dictionary records it.

42
00:02:25,673 --> 00:02:29,553
The distance dictionary therefore serves
as the visited set too.

43
00:02:29,593 --> 00:02:34,153
This algorithm assumes unit-cost edges;
ordinary BFS does not solve

44
00:02:34,233 --> 00:02:36,473
general weighted shortest paths.

45
00:02:36,700 --> 00:02:40,940
DFS expands A and pushes B then C, marking

46
00:02:41,020 --> 00:02:41,940
both.

47
00:02:41,980 --> 00:02:45,780
DFS expands C because C is now the stack
top.

48
00:02:45,820 --> 00:02:47,473
It discovers D.

49
00:02:47,527 --> 00:02:51,140
DFS expands D, whose neighbors are already
marked.

50
00:02:51,180 --> 00:02:55,140
DFS expands B last, then the stack
empties.

51
00:02:55,180 --> 00:02:59,740
The first component has expansion order A,
C, D, B.

52
00:02:59,820 --> 00:03:03,660
This is a traversal order, not a shortest-
path distance order.

53
00:03:03,900 --> 00:03:08,620
The outer loop next considers vertices
that the first search already visited and

54
00:03:08,700 --> 00:03:09,793
skips them.

55
00:03:09,847 --> 00:03:14,620
When it reaches E, E is unseen, so the
algorithm starts another stack

56
00:03:14,740 --> 00:03:15,700
with E.

57
00:03:15,740 --> 00:03:19,367
E has no neighbors and forms a component
by itself.

58
00:03:19,393 --> 00:03:22,460
There are two components in the undirected
example.

59
00:03:22,540 --> 00:03:26,940
Applying this same procedure to a directed
graph would not compute strongly connected

60
00:03:27,540 --> 00:03:28,220
components.

61
00:03:28,467 --> 00:03:32,547
Create a visited set shared across all
component searches.

62
00:03:32,707 --> 00:03:37,387
Iterate through every adjacency key and
skip an already visited start.

63
00:03:37,427 --> 00:03:40,334
Mark the start before creating its stack.

64
00:03:40,440 --> 00:03:43,227
Create a separate output list for this
component.

65
00:03:43,267 --> 00:03:47,587
The shared visited set prevents an already
completed component from being searched

66
00:03:47,667 --> 00:03:51,347
again when the outer loop reaches one of
its later vertices.

67
00:03:51,567 --> 00:03:55,327
Pop the current vertex and append it to
this component.

68
00:03:55,407 --> 00:03:58,580
Mark every unseen neighbor before pushing
it.

69
00:03:58,634 --> 00:04:02,900
When the stack empties, no reachable
neighbor remains undiscovered.

70
00:04:03,034 --> 00:04:07,620
Append the completed component to the
result, then continue the outer loop.

71
00:04:07,674 --> 00:04:12,180
The visited set grows throughout the
algorithm and never loses a vertex.

72
00:04:12,234 --> 00:04:16,367
That invariant keeps every vertex in
exactly one component.

73
00:04:16,600 --> 00:04:20,040
Each vertex enters the frontier at most
once.

74
00:04:20,160 --> 00:04:24,200
Each adjacency entry is inspected when its
vertex is expanded.

75
00:04:24,280 --> 00:04:28,920
An undirected edge appears twice, which
changes only a constant factor.

76
00:04:29,200 --> 00:04:33,720
Expected time is big O of V plus E with
expected constant-time hash

77
00:04:33,800 --> 00:04:34,867
operations.

78
00:04:34,893 --> 00:04:39,453
The visited set and frontier use big O of
V auxiliary space.

79
00:04:39,587 --> 00:04:44,520
Returned distances or component lists also
use space proportional to vertices.

80
00:04:44,767 --> 00:04:49,407
Use BFS when the question needs unweighted
shortest distances.

81
00:04:49,487 --> 00:04:54,474
Use either traversal for reachability or
undirected component discovery.

82
00:04:54,580 --> 00:04:59,567
Test an isolated vertex, a cycle, a self-
loop, and an empty graph where the

83
00:04:59,647 --> 00:05:01,220
function permits it.

84
00:05:01,434 --> 00:05:05,887
Replacing popleft with a list front
removal can add quadratic shifts on a wide

85
00:05:06,367 --> 00:05:06,954
graph.

86
00:05:07,060 --> 00:05:11,487
State the graph representation and edge
assumptions before the complexity claim.
