1
00:00:00,000 --> 00:00:04,400
Backtracking explores a choice and
restores the state before another choice

2
00:00:04,480 --> 00:00:05,200
uses it.

3
00:00:05,760 --> 00:00:08,480
The restoration belongs to the correctness
argument.

4
00:00:08,880 --> 00:00:13,840
Our example lists every subset of the
distinct values two, five, and eight.

5
00:00:14,480 --> 00:00:17,920
Each position has an exclude branch and an
include branch.

6
00:00:18,480 --> 00:00:22,800
We will follow all eight answers and
explain why every saved path needs a

7
00:00:22,880 --> 00:00:23,840
separate copy.

8
00:00:24,167 --> 00:00:28,300
The next index identifies the first
undecided position.

9
00:00:28,354 --> 00:00:33,180
The current path contains exactly the
selected values from earlier positions.

10
00:00:33,234 --> 00:00:36,207
The destination stores completed subsets.

11
00:00:36,247 --> 00:00:39,554
Excluding a value leaves the path
unchanged.

12
00:00:39,740 --> 00:00:44,327
Including it appends the value before the
next call and pops it after that call

13
00:00:44,407 --> 00:00:45,407
returns.

14
00:00:45,447 --> 00:00:48,967
Either branch must return with its
caller's path restored.

15
00:00:49,200 --> 00:00:52,520
At the input length, there is no remaining
choice.

16
00:00:52,560 --> 00:00:55,973
Save a copy of the path as one complete
subset.

17
00:00:56,027 --> 00:00:59,867
The empty subset appears when every
position is excluded.

18
00:00:59,893 --> 00:01:03,240
It is a required answer, not a failure to
choose.

19
00:01:03,280 --> 00:01:07,200
The two branches partition possible
answers according to whether they contain

20
00:01:07,240 --> 00:01:08,693
the current position.

21
00:01:08,747 --> 00:01:12,400
Repeating that partition covers every
position choice once.

22
00:01:12,633 --> 00:01:16,153
Exclude all three values and save the
empty subset.

23
00:01:16,553 --> 00:01:21,233
Include eight only after returning to the
last decision, and save eight.

24
00:01:21,433 --> 00:01:23,793
Pop eight before returning again.

25
00:01:23,993 --> 00:01:27,273
Include five next, while two remains
excluded.

26
00:01:27,593 --> 00:01:30,393
Excluding eight in that branch saves five.

27
00:01:30,793 --> 00:01:35,753
Each saved list is independent, so later
changes to the working path cannot change

28
00:01:35,833 --> 00:01:37,193
these completed answers.

29
00:01:37,500 --> 00:01:41,500
Include eight with five and save the pair
five, eight.

30
00:01:41,820 --> 00:01:44,860
That completes every choice that excludes
two.

31
00:01:45,060 --> 00:01:48,180
Restore the path before the outer include
branch.

32
00:01:48,220 --> 00:01:52,740
Include two alone and exclude both
remaining values to save two.

33
00:01:52,780 --> 00:01:56,833
Include eight with two next, while five
remains excluded.

34
00:01:56,887 --> 00:01:59,260
The saved pair is two, eight.

35
00:01:59,340 --> 00:02:02,860
Restoration prevents five from leaking
into this branch.

36
00:02:03,100 --> 00:02:07,180
Include five with two and exclude eight to
save the pair two,

37
00:02:07,260 --> 00:02:08,180
five.

38
00:02:08,220 --> 00:02:12,727
Include all three values for the final
subset two, five, eight.

39
00:02:12,753 --> 00:02:16,967
The traversal then pops each included
value as its call returns.

40
00:02:17,073 --> 00:02:20,967
All eight subsets are saved, and the
working path is empty again.

41
00:02:20,993 --> 00:02:23,900
The original input list was never
modified.

42
00:02:24,133 --> 00:02:29,093
The helper receives the values, next
index, current path, and destination

43
00:02:29,173 --> 00:02:30,453
explicitly.

44
00:02:30,573 --> 00:02:34,213
Its optional trace flag controls the
printed explanation.

45
00:02:34,293 --> 00:02:38,013
The base case copies the path into the
result and returns.

46
00:02:38,053 --> 00:02:41,533
This is where a completed choice becomes
durable output.

47
00:02:41,573 --> 00:02:45,893
After that guard, the exclude branch
advances the index without changing the

48
00:02:45,973 --> 00:02:46,720
path.

49
00:02:46,746 --> 00:02:50,293
The optional message makes that decision
visible in the trace.

50
00:02:50,533 --> 00:02:55,013
The include branch appends the current
value and explores the next undecided

51
00:02:55,573 --> 00:02:56,186
position.

52
00:02:56,240 --> 00:03:01,053
The pop removes exactly that append after
the recursive call returns.

53
00:03:01,093 --> 00:03:05,693
Trace messages expose these actions but do
not control the decisions.

54
00:03:05,733 --> 00:03:10,533
The wrapper starts at index zero with an
empty path and a new result list for each

55
00:03:10,613 --> 00:03:11,333
call.

56
00:03:11,567 --> 00:03:16,207
Appending the path itself stores a
reference to one mutable list.

57
00:03:16,407 --> 00:03:19,860
Later pops would change what every result
entry sees.

58
00:03:19,914 --> 00:03:23,527
A shallow copy creates a separate outer
list at the leaf.

59
00:03:23,567 --> 00:03:26,687
Integer values need no deeper copy here.

60
00:03:26,847 --> 00:03:31,407
Repeated input values create a different
issue: distinct position choices can then

61
00:03:31,487 --> 00:03:36,367
produce equal value subsets, so a unique-
subset problem needs a duplicate

62
00:03:36,807 --> 00:03:37,327
policy.

63
00:03:37,567 --> 00:03:40,687
There are two to the n completed subsets.

64
00:03:40,887 --> 00:03:45,727
Copying them requires big Theta of n times
two to the n total time and output

65
00:03:45,807 --> 00:03:46,794
storage.

66
00:03:46,820 --> 00:03:51,647
The live path and recursive calls use only
linear auxiliary space because one

67
00:03:51,727 --> 00:03:53,287
branch runs at a time.

68
00:03:53,327 --> 00:03:57,060
Exponential output does not mean
exponential stack depth.

69
00:03:57,114 --> 00:04:00,367
The empty input still returns one empty
list.

70
00:04:00,600 --> 00:04:04,440
An empty input tests whether you emit the
empty subset.

71
00:04:04,520 --> 00:04:08,520
Two successive calls test whether state
leaks between calls.

72
00:04:08,600 --> 00:04:12,920
A changed returned subset tests whether
the outputs share one mutable

73
00:04:13,120 --> 00:04:13,800
list.

74
00:04:13,840 --> 00:04:18,600
For a constrained search, prune a branch
only when no future choice can repair its

75
00:04:18,680 --> 00:04:19,773
violation.

76
00:04:19,827 --> 00:04:24,600
For permutations, choose the next unused
position and explain which state each

77
00:04:24,840 --> 00:04:26,120
return must restore.
