-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiscrete-math.html
More file actions
1934 lines (1680 loc) · 103 KB
/
discrete-math.html
File metadata and controls
1934 lines (1680 loc) · 103 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Discrete Math -- Math from Zero to CS - Better Dev</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<header class="topbar">
<button class="sidebar-toggle" aria-label="Open navigation" aria-expanded="false">
<span class="hamburger-icon"></span>
</button>
<a href="index.html" class="logo">Better Dev</a>
</header>
<div class="sidebar-backdrop" aria-hidden="true"></div>
<aside class="sidebar" aria-label="Site navigation">
<div class="sidebar-header">
<span class="sidebar-title">Navigation</span>
<button class="sidebar-close" aria-label="Close navigation">×</button>
</div>
<div class="sidebar-search">
<input type="text" class="sidebar-search-input" placeholder="Search topics..." aria-label="Search topics">
<div class="sidebar-search-results"></div>
</div>
<nav class="sidebar-nav">
<div class="sidebar-group">
<a href="index.html">Home</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Mathematics</div>
<a href="pre-algebra.html">Pre-Algebra</a>
<a href="algebra.html">Algebra</a>
<a href="sequences-series.html">Sequences & Series</a>
<a href="geometry.html">Geometry</a>
<a href="calculus.html">Calculus</a>
<a href="discrete-math.html">Discrete Math</a>
<a href="linear-algebra.html">Linear Algebra</a>
<a href="probability.html">Probability & Statistics</a>
<a href="binary-systems.html">Binary & Number Systems</a>
<a href="number-theory.html">Number Theory for CP</a>
<a href="computational-geometry.html">Computational Geometry</a>
<a href="game-theory.html">Game Theory</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Data Structures & Algorithms</div>
<a href="dsa-foundations.html">DSA Foundations</a>
<a href="arrays.html">Arrays & Strings</a>
<a href="stacks-queues.html">Stacks & Queues</a>
<a href="hashmaps.html">Hash Maps & Sets</a>
<a href="linked-lists.html">Linked Lists</a>
<a href="trees.html">Trees & BST</a>
<a href="graphs.html">Graphs</a>
<a href="sorting.html">Sorting & Searching</a>
<a href="patterns.html">LeetCode Patterns</a>
<a href="dp.html">Dynamic Programming</a>
<a href="advanced.html">Advanced Topics</a>
<a href="string-algorithms.html">String Algorithms</a>
<a href="advanced-graphs.html">Advanced Graphs</a>
<a href="advanced-dp.html">Advanced DP</a>
<a href="advanced-ds.html">Advanced Data Structures</a>
<a href="leetcode-650.html">The 650 Problems</a>
<a href="competitive-programming.html">CP Roadmap</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Languages & Systems</div>
<a href="cpp.html">C++</a>
<a href="golang.html">Go</a>
<a href="javascript.html">JavaScript Deep Dive</a>
<a href="typescript.html">TypeScript</a>
<a href="nodejs.html">Node.js Internals</a>
<a href="os.html">Operating Systems</a>
<a href="linux.html">Linux</a>
<a href="git.html">Git</a>
<a href="backend.html">Backend</a>
<a href="system-design.html">System Design</a>
<a href="networking.html">Networking</a>
<a href="cloud.html">Cloud & Infrastructure</a>
<a href="docker.html">Docker & Compose</a>
<a href="kubernetes.html">Kubernetes</a>
<a href="message-queues.html">Queues & Pub/Sub</a>
<a href="selfhosting.html">VPS & Self-Hosting</a>
<a href="databases.html">PostgreSQL & MySQL</a>
<a href="stripe.html">Stripe & Payments</a>
<a href="distributed-systems.html">Distributed Systems</a>
<a href="backend-engineering.html">Backend Engineering</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">JS/TS Ecosystem</div>
<a href="js-tooling.html">Tooling & Bundlers</a>
<a href="js-testing.html">Testing</a>
<a href="ts-projects.html">Building with TS</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">More</div>
<a href="seans-brain.html">Sean's Brain</a>
</div>
</nav>
</aside>
<div class="container">
<!-- Page Header -->
<div class="page-header">
<div class="breadcrumb"><a href="index.html">Home</a> / Discrete Math</div>
<h1>Discrete Mathematics</h1>
<p>The branch of mathematics built specifically for computer science. Logic, sets, graphs, counting, proofs -- this is where math and programming truly merge.</p>
</div>
<div class="tip-box">
<div class="label">Why This Is the Most Important Math for CS</div>
<p>If you only study one branch of math for computer science, make it discrete math. Every <code>if</code> statement is logic. Every database query is set theory. Every algorithm analysis is Big-O. Every recursive function needs induction to prove correct. This page covers the mathematical foundations you will use every single day as a programmer.</p>
</div>
<!-- Table of Contents -->
<div class="toc">
<h4>Table of Contents</h4>
<a href="#logic">1. Logic and Propositions</a>
<a href="#sets">2. Sets</a>
<a href="#functions">3. Functions and Relations</a>
<a href="#proofs">4. Proof Techniques</a>
<a href="#counting">5. Counting and Combinatorics</a>
<a href="#graphs">6. Graph Theory</a>
<a href="#number-theory">7. Number Theory and Modular Arithmetic</a>
<a href="#boolean">8. Boolean Algebra</a>
<a href="#big-o">9. Big-O Notation</a>
<a href="#recurrences">10. Recurrence Relations</a>
<a href="#quiz">11. Practice Quiz</a>
</div>
<!-- ===================== SECTION 1: LOGIC ===================== -->
<section id="logic">
<h2>1. Logic and Propositions</h2>
<p>Logic is the foundation of all mathematical reasoning -- and all programming. A <strong>proposition</strong> is a declarative statement that is either <strong>true</strong> or <strong>false</strong>, never both, never neither.</p>
<div class="tip-box">
<div class="label">You Already Know This</div>
<p>If you've written an <code>if</code> statement, you already do logic. Discrete math just gives you the <em>formal vocabulary</em> for what you're already doing:</p>
<pre><code><span class="comment"># This code IS logic:</span>
<span class="keyword">if</span> user.is_admin <span class="keyword">and</span> (<span class="keyword">not</span> user.is_banned <span class="keyword">or</span> user.has_override):
grant_access()
<span class="comment"># In math notation: A ∧ (¬B ∨ C)</span>
<span class="comment"># Same thing, just different symbols</span></code></pre>
<p>Every <code>if/elif/else</code> chain is an implication. Every <code>while</code> condition is a proposition evaluated each iteration. Boolean algebra IS programming logic -- the math just lets you prove things about it.</p>
</div>
<div class="example-box">
<div class="label">Examples of Propositions</div>
<ul>
<li>"5 is greater than 3" -- this is <strong>true</strong></li>
<li>"The Earth is flat" -- this is <strong>false</strong></li>
<li>"2 + 2 = 4" -- this is <strong>true</strong></li>
</ul>
<p style="margin-top:0.8rem"><strong>Not propositions:</strong> "What time is it?" (question), "Close the door" (command), "x + 5 = 10" (depends on x -- this is a <em>predicate</em>, not a proposition).</p>
</div>
<h3>Logical Operators</h3>
<p>We combine propositions using <strong>logical operators</strong> (also called connectives). If you have written an <code>if</code> statement, you already know these.</p>
<table>
<tr>
<th>Operator</th>
<th>Symbol</th>
<th>Python</th>
<th>Meaning</th>
</tr>
<tr>
<td>AND</td>
<td style="color:#333333; font-size:1.1rem;">∧</td>
<td><code>and</code></td>
<td>True only when both are true</td>
</tr>
<tr>
<td>OR</td>
<td style="color:#333333; font-size:1.1rem;">∨</td>
<td><code>or</code></td>
<td>True when at least one is true</td>
</tr>
<tr>
<td>NOT</td>
<td style="color:#333333; font-size:1.1rem;">¬</td>
<td><code>not</code></td>
<td>Flips true to false, false to true</td>
</tr>
<tr>
<td>XOR</td>
<td style="color:#333333; font-size:1.1rem;">⊕</td>
<td><code>^</code></td>
<td>True when exactly one is true</td>
</tr>
<tr>
<td>Implication</td>
<td style="color:#333333; font-size:1.1rem;">→</td>
<td>--</td>
<td>If A then B</td>
</tr>
<tr>
<td>Biconditional</td>
<td style="color:#333333; font-size:1.1rem;">↔</td>
<td><code>==</code></td>
<td>A if and only if B</td>
</tr>
</table>
<h3>Truth Tables</h3>
<p>A truth table shows every possible combination of inputs and the resulting output. Here are the truth tables for the core operators.</p>
<h4 style="color:#333333; margin:1.2rem 0 0.6rem;">AND (∧) -- both must be true</h4>
<table>
<tr><th>A</th><th>B</th><th>A ∧ B</th></tr>
<tr><td>T</td><td>T</td><td style="color:#000000; font-weight:600;">T</td></tr>
<tr><td>T</td><td>F</td><td style="color:#888888;">F</td></tr>
<tr><td>F</td><td>T</td><td style="color:#888888;">F</td></tr>
<tr><td>F</td><td>F</td><td style="color:#888888;">F</td></tr>
</table>
<h4 style="color:#333333; margin:1.2rem 0 0.6rem;">OR (∨) -- at least one must be true</h4>
<table>
<tr><th>A</th><th>B</th><th>A ∨ B</th></tr>
<tr><td>T</td><td>T</td><td style="color:#000000; font-weight:600;">T</td></tr>
<tr><td>T</td><td>F</td><td style="color:#000000; font-weight:600;">T</td></tr>
<tr><td>F</td><td>T</td><td style="color:#000000; font-weight:600;">T</td></tr>
<tr><td>F</td><td>F</td><td style="color:#888888;">F</td></tr>
</table>
<h4 style="color:#333333; margin:1.2rem 0 0.6rem;">NOT (¬) -- flip the value</h4>
<table>
<tr><th>A</th><th>¬A</th></tr>
<tr><td>T</td><td style="color:#888888;">F</td></tr>
<tr><td>F</td><td style="color:#000000; font-weight:600;">T</td></tr>
</table>
<h4 style="color:#333333; margin:1.2rem 0 0.6rem;">XOR (⊕) -- exactly one must be true</h4>
<table>
<tr><th>A</th><th>B</th><th>A ⊕ B</th></tr>
<tr><td>T</td><td>T</td><td style="color:#888888;">F</td></tr>
<tr><td>T</td><td>F</td><td style="color:#000000; font-weight:600;">T</td></tr>
<tr><td>F</td><td>T</td><td style="color:#000000; font-weight:600;">T</td></tr>
<tr><td>F</td><td>F</td><td style="color:#888888;">F</td></tr>
</table>
<h3>Implication (→) and Biconditional (↔)</h3>
<p>The <strong>implication</strong> A → B means "if A, then B." It is only false when A is true but B is false. Think of it as a promise: "If it rains, I will bring an umbrella." The promise is only broken if it rains and you don't bring one.</p>
<h4 style="color:#333333; margin:1.2rem 0 0.6rem;">Implication (→)</h4>
<table>
<tr><th>A</th><th>B</th><th>A → B</th></tr>
<tr><td>T</td><td>T</td><td style="color:#000000; font-weight:600;">T</td></tr>
<tr><td>T</td><td>F</td><td style="color:#888888;">F</td></tr>
<tr><td>F</td><td>T</td><td style="color:#000000; font-weight:600;">T</td></tr>
<tr><td>F</td><td>F</td><td style="color:#000000; font-weight:600;">T</td></tr>
</table>
<div class="warning-box">
<div class="label">Tricky Part</div>
<p>When A is false, the implication A → B is <strong>always true</strong> regardless of B. This feels counterintuitive but makes logical sense: a promise made under a false premise cannot be broken. "If pigs fly, then I will give you a million dollars" -- pigs don't fly, so you can't claim I broke my promise.</p>
</div>
<p>The <strong>biconditional</strong> A ↔ B means "A if and only if B" -- both must have the same truth value.</p>
<h4 style="color:#333333; margin:1.2rem 0 0.6rem;">Biconditional (↔)</h4>
<table>
<tr><th>A</th><th>B</th><th>A ↔ B</th></tr>
<tr><td>T</td><td>T</td><td style="color:#000000; font-weight:600;">T</td></tr>
<tr><td>T</td><td>F</td><td style="color:#888888;">F</td></tr>
<tr><td>F</td><td>T</td><td style="color:#888888;">F</td></tr>
<tr><td>F</td><td>F</td><td style="color:#000000; font-weight:600;">T</td></tr>
</table>
<h3>Logical Equivalences</h3>
<p>Two expressions are <strong>logically equivalent</strong> if they have the same truth value for every possible input. The most famous equivalences are <strong>De Morgan's Laws</strong>:</p>
<div class="formula-box">
¬(A ∧ B) = ¬A ∨ ¬B<br>
¬(A ∨ B) = ¬A ∧ ¬B
</div>
<p>In English: "not (A and B)" is the same as "not A or not B." And "not (A or B)" is the same as "not A and not B."</p>
<div class="example-box">
<div class="label">De Morgan's in Python</div>
<pre><code><span class="comment"># These two are equivalent:</span>
<span class="keyword">if</span> <span class="keyword">not</span> (is_admin <span class="keyword">and</span> is_logged_in):
deny_access()
<span class="comment"># Same as:</span>
<span class="keyword">if</span> (<span class="keyword">not</span> is_admin) <span class="keyword">or</span> (<span class="keyword">not</span> is_logged_in):
deny_access()</code></pre>
</div>
<p>Other important equivalences:</p>
<ul>
<li><strong>Double negation:</strong> ¬(¬A) = A</li>
<li><strong>Commutative:</strong> A ∧ B = B ∧ A, and A ∨ B = B ∨ A</li>
<li><strong>Associative:</strong> (A ∧ B) ∧ C = A ∧ (B ∧ C)</li>
<li><strong>Distributive:</strong> A ∧ (B ∨ C) = (A ∧ B) ∨ (A ∧ C)</li>
<li><strong>Contrapositive:</strong> A → B = ¬B → ¬A</li>
</ul>
<div class="tip-box">
<div class="label">Why CS Cares -- Logic</div>
<p>Every <code>if</code> statement, every <code>while</code> loop condition, and every boolean expression in your code is propositional logic. De Morgan's Laws help you simplify complex conditions. Understanding implication helps you write correct assertions and contracts. Logic gates (AND, OR, NOT) are the physical building blocks of every CPU. Formal logic is also the foundation of SQL's <code>WHERE</code> clauses, type systems, and automated theorem proving.</p>
</div>
<div class="formula-box">
<strong>Propositional Logic Laws (Axioms):</strong><br><br>
<strong>Identity:</strong> A ∧ T = A, A ∨ F = A<br>
<strong>Domination:</strong> A ∧ F = F, A ∨ T = T<br>
<strong>Idempotent:</strong> A ∧ A = A, A ∨ A = A<br>
<strong>Double Negation:</strong> ¬(¬A) = A<br>
<strong>Complement:</strong> A ∧ ¬A = F, A ∨ ¬A = T<br>
<strong>Commutative:</strong> A ∧ B = B ∧ A, A ∨ B = B ∨ A<br>
<strong>Associative:</strong> (A ∧ B) ∧ C = A ∧ (B ∧ C)<br>
<strong>Distributive:</strong> A ∧ (B ∨ C) = (A ∧ B) ∨ (A ∧ C)<br>
<strong>De Morgan's:</strong> ¬(A ∧ B) = ¬A ∨ ¬B, ¬(A ∨ B) = ¬A ∧ ¬B
</div>
</section>
<!-- ===================== SECTION 2: SETS ===================== -->
<section id="sets">
<h2>2. Sets</h2>
<p>A <strong>set</strong> is an unordered collection of distinct elements. Sets are one of the most fundamental concepts in all of mathematics -- and they show up everywhere in programming.</p>
<h3>Set Notation</h3>
<p>Sets are written with curly braces. Order does not matter, and duplicates are ignored.</p>
<div class="formula-box">
A = {1, 2, 3, 4, 5}<br>
B = {3, 4, 5, 6, 7}<br>
C = {apple, banana, cherry}
</div>
<p>We write <strong>x ∈ A</strong> to say "x is an element of A," and <strong>x ∉ A</strong> to say "x is not in A."</p>
<div class="example-box">
<div class="label">Example</div>
<p>If A = {1, 2, 3}, then 2 ∈ A is true and 7 ∈ A is false.</p>
</div>
<h3>Set Operations</h3>
<table>
<tr>
<th>Operation</th>
<th>Symbol</th>
<th>Meaning</th>
<th>Python</th>
</tr>
<tr>
<td>Union</td>
<td style="color:#333333;">∪</td>
<td>Everything in A or B (or both)</td>
<td><code>A | B</code></td>
</tr>
<tr>
<td>Intersection</td>
<td style="color:#333333;">∩</td>
<td>Only elements in both A and B</td>
<td><code>A & B</code></td>
</tr>
<tr>
<td>Difference</td>
<td style="color:#333333;">\</td>
<td>Elements in A but not in B</td>
<td><code>A - B</code></td>
</tr>
<tr>
<td>Complement</td>
<td style="color:#333333;">A<sup>c</sup></td>
<td>Everything not in A (relative to universe)</td>
<td><code>U - A</code></td>
</tr>
</table>
<div class="example-box">
<div class="label">Example -- Set Operations</div>
<p>Given A = {1, 2, 3, 4, 5} and B = {3, 4, 5, 6, 7}:</p>
<ul>
<li><strong>A ∪ B</strong> = {1, 2, 3, 4, 5, 6, 7} -- everything from both</li>
<li><strong>A ∩ B</strong> = {3, 4, 5} -- only the overlap</li>
<li><strong>A \ B</strong> = {1, 2} -- in A but not B</li>
<li><strong>B \ A</strong> = {6, 7} -- in B but not A</li>
</ul>
</div>
<h3>Subsets and Special Sets</h3>
<p><strong>A ⊆ B</strong> means "A is a subset of B" -- every element of A is also in B. <strong>A ⊂ B</strong> means "A is a proper subset" -- A is a subset but A ≠ B.</p>
<ul>
<li>The <strong>empty set</strong> ∅ = {} has no elements. It is a subset of every set.</li>
<li><strong>Cardinality</strong> |S| is the number of elements in a set. |{a, b, c}| = 3.</li>
<li>The <strong>power set</strong> P(S) is the set of all subsets of S. If S = {1, 2}, then P(S) = {∅, {1}, {2}, {1, 2}}. The power set always has 2<sup>|S|</sup> elements.</li>
</ul>
<div class="formula-box">
|P(S)| = 2<sup>|S|</sup>
</div>
<h3>Venn Diagrams</h3>
<p>A <strong>Venn diagram</strong> visualizes sets as overlapping circles. Imagine two circles, one for set A and one for set B, that partially overlap. The overlapping region represents A ∩ B (intersection). The entire area covered by both circles is A ∪ B (union). The area in A's circle but not in B's is A \ B (difference). This visual tool makes set relationships immediately intuitive.</p>
<div class="example-box">
<div class="label">Sets in Python</div>
<pre><code>A = {<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>}
B = {<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>}
<span class="builtin">print</span>(A | B) <span class="comment"># Union: {1, 2, 3, 4, 5, 6, 7}</span>
<span class="builtin">print</span>(A & B) <span class="comment"># Intersection: {3, 4, 5}</span>
<span class="builtin">print</span>(A - B) <span class="comment"># Difference: {1, 2}</span>
<span class="builtin">print</span>(A ^ B) <span class="comment"># Symmetric difference (XOR): {1, 2, 6, 7}</span>
<span class="builtin">print</span>(<span class="number">3</span> <span class="keyword">in</span> A) <span class="comment"># Membership: True</span>
<span class="builtin">print</span>(<span class="builtin">len</span>(A)) <span class="comment"># Cardinality: 5</span></code></pre>
</div>
<div class="tip-box">
<div class="label">Why CS Cares -- Sets</div>
<p>Sets are everywhere in computing. <strong>Database queries</strong> are set operations (SQL's UNION, INTERSECT, EXCEPT map directly to ∪, ∩, \). <strong>Type systems</strong> model types as sets of values. Python has a built-in <code>set</code> type with O(1) membership testing. <strong>Hash sets</strong> and <strong>hash maps</strong> are the most-used data structures in real-world code. Even IP address ranges and permission systems are modeled as sets.</p>
</div>
<div class="formula-box">
<strong>Set Identity Laws:</strong><br><br>
<strong>Identity:</strong> A ∪ ∅ = A, A ∩ U = A<br>
<strong>Domination:</strong> A ∪ U = U, A ∩ ∅ = ∅<br>
<strong>Complement:</strong> A ∪ A' = U, A ∩ A' = ∅<br>
<strong>De Morgan's:</strong> (A ∪ B)' = A' ∩ B', (A ∩ B)' = A' ∪ B'<br>
<strong>Distributive:</strong> A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C)
</div>
</section>
<!-- ===================== SECTION 3: FUNCTIONS AND RELATIONS ===================== -->
<section id="functions">
<h2>3. Functions and Relations</h2>
<h3>Relations</h3>
<p>A <strong>relation</strong> from set A to set B is any subset of the <strong>Cartesian product</strong> A × B (all possible ordered pairs). A relation describes how elements of A connect to elements of B.</p>
<div class="example-box">
<div class="label">Example</div>
<p>Let A = {1, 2, 3} and B = {a, b}. The Cartesian product A × B is:</p>
<p>{(1,a), (1,b), (2,a), (2,b), (3,a), (3,b)}</p>
<p>A relation R might be: R = {(1,a), (2,b), (3,a)} -- this picks out specific connections.</p>
</div>
<p>Relations can have special properties:</p>
<ul>
<li><strong>Reflexive:</strong> Every element relates to itself. (a, a) ∈ R for all a.</li>
<li><strong>Symmetric:</strong> If (a, b) ∈ R then (b, a) ∈ R.</li>
<li><strong>Transitive:</strong> If (a, b) ∈ R and (b, c) ∈ R then (a, c) ∈ R.</li>
<li><strong>Equivalence relation:</strong> All three -- reflexive, symmetric, and transitive.</li>
</ul>
<h3>Functions</h3>
<p>A <strong>function</strong> f: A → B is a special relation where every element of A maps to <strong>exactly one</strong> element of B. A is the <strong>domain</strong>, B is the <strong>codomain</strong>.</p>
<p>Functions come in three important flavors:</p>
<table>
<tr>
<th>Type</th>
<th>Definition</th>
<th>Intuition</th>
</tr>
<tr>
<td><strong>Injective</strong> (one-to-one)</td>
<td>Different inputs always give different outputs. If f(a) = f(b), then a = b.</td>
<td>No two inputs share an output.</td>
</tr>
<tr>
<td><strong>Surjective</strong> (onto)</td>
<td>Every element of B is mapped to by some element of A.</td>
<td>Every output is "hit" by some input.</td>
</tr>
<tr>
<td><strong>Bijective</strong> (one-to-one and onto)</td>
<td>Both injective and surjective.</td>
<td>Perfect pairing -- every input maps to a unique output, and every output is used.</td>
</tr>
</table>
<div class="example-box">
<div class="label">Example</div>
<p>f(x) = 2x from integers to integers:</p>
<ul>
<li><strong>Injective?</strong> Yes -- different inputs give different outputs (if 2a = 2b then a = b).</li>
<li><strong>Surjective?</strong> No -- odd numbers like 3 are never output by f.</li>
<li><strong>Bijective?</strong> No -- not surjective.</li>
</ul>
</div>
<h3>Composition of Functions</h3>
<p>If you have f: A → B and g: B → C, the <strong>composition</strong> g ∘ f (read "g of f") is defined as (g ∘ f)(x) = g(f(x)). Apply f first, then g.</p>
<div class="formula-box">
f(x) = x + 1, g(x) = x²<br>
(g ∘ f)(x) = g(f(x)) = g(x + 1) = (x + 1)²<br>
(f ∘ g)(x) = f(g(x)) = f(x²) = x² + 1
</div>
<div class="warning-box">
<div class="label">Order Matters</div>
<p>Composition is <strong>not commutative</strong>: g ∘ f ≠ f ∘ g in general. In the example above, (x + 1)² ≠ x² + 1. This is like how in programming, the order of function calls matters: <code>g(f(x))</code> is different from <code>f(g(x))</code>.</p>
</div>
<h3>Inverse Functions</h3>
<p>If f: A → B is bijective, then there exists an <strong>inverse function</strong> f<sup>-1</sup>: B → A such that f<sup>-1</sup>(f(x)) = x and f(f<sup>-1</sup>(y)) = y. The inverse "undoes" the original function.</p>
<div class="example-box">
<div class="label">Example</div>
<p>f(x) = 2x + 3. To find f<sup>-1</sup>, solve y = 2x + 3 for x:</p>
<p>x = (y - 3) / 2, so f<sup>-1</sup>(y) = (y - 3) / 2</p>
<p>Check: f<sup>-1</sup>(f(5)) = f<sup>-1</sup>(13) = (13 - 3)/2 = 5. It works.</p>
</div>
<div class="tip-box">
<div class="label">Why CS Cares -- Functions</div>
<p><strong>Bijections</strong> are essential in cryptography -- encryption must be reversible (bijective) for decryption to work. <strong>Hash functions</strong> are surjective (many inputs can map to the same hash). Function composition is the basis of <strong>functional programming</strong> and <strong>middleware pipelines</strong>. Understanding injectivity helps you reason about whether a mapping can be reversed (critical for encoding/decoding).</p>
</div>
</section>
<!-- ===================== SECTION 4: PROOF TECHNIQUES ===================== -->
<section id="proofs">
<h2>4. Proof Techniques</h2>
<p>Proofs are how mathematicians verify that something is always true, not just sometimes. As a programmer, proof techniques help you <strong>reason about correctness</strong> -- proving your algorithm works for all inputs, not just the ones you tested.</p>
<h3>Direct Proof</h3>
<p>Start with what you know (premises), apply logical steps, arrive at the conclusion. The most straightforward approach.</p>
<div class="example-box">
<div class="label">Example -- Direct Proof</div>
<p><strong>Claim:</strong> If n is even, then n² is even.</p>
<p><strong>Proof:</strong></p>
<ol>
<li>Assume n is even. By definition, n = 2k for some integer k.</li>
<li>Then n² = (2k)² = 4k² = 2(2k²).</li>
<li>Since 2k² is an integer, n² = 2(integer), so n² is even. □</li>
</ol>
</div>
<h3>Proof by Contradiction</h3>
<p>Assume the opposite of what you want to prove, then show that leads to something impossible (a contradiction). Since the assumption caused a contradiction, it must be false, so the original statement must be true.</p>
<div class="example-box">
<div class="label">Example -- Proof by Contradiction</div>
<p><strong>Claim:</strong> √2 is irrational.</p>
<p><strong>Proof:</strong></p>
<ol>
<li>Assume √2 is rational. Then √2 = a/b where a, b are integers with no common factors.</li>
<li>Squaring both sides: 2 = a²/b², so a² = 2b².</li>
<li>This means a² is even, so a must be even. Write a = 2c.</li>
<li>Then (2c)² = 2b², so 4c² = 2b², so b² = 2c².</li>
<li>This means b² is even, so b is even.</li>
<li><strong>Contradiction:</strong> Both a and b are even, so they share factor 2. But we said they have no common factors.</li>
<li>Therefore √2 is irrational. □</li>
</ol>
</div>
<h3>Proof by Induction</h3>
<p>This is the <strong>most important proof technique for computer science</strong>. Induction proves that a statement holds for all natural numbers by proving two things:</p>
<ol>
<li><strong>Base case:</strong> Show the statement is true for n = 1 (or n = 0).</li>
<li><strong>Inductive step:</strong> Assume the statement is true for some arbitrary n = k (the <em>inductive hypothesis</em>), then prove it is true for n = k + 1.</li>
</ol>
<p>Think of it like dominoes: if the first one falls (base case) and each domino knocks over the next (inductive step), then all dominoes fall.</p>
<div class="example-box">
<div class="label">Example -- Proof by Induction</div>
<p><strong>Claim:</strong> The sum 1 + 2 + 3 + ... + n = n(n+1)/2 for all n ≥ 1.</p>
<p><strong>Base case (n = 1):</strong></p>
<p>Left side: 1. Right side: 1(1+1)/2 = 1. They match. Base case holds.</p>
<p><strong>Inductive hypothesis:</strong> Assume the formula holds for n = k:</p>
<div class="formula-box">1 + 2 + 3 + ... + k = k(k+1)/2</div>
<p><strong>Inductive step:</strong> Prove it holds for n = k + 1:</p>
<div class="formula-box">
1 + 2 + ... + k + (k+1)<br>
= k(k+1)/2 + (k+1) <span style="color:#888888;">// using the inductive hypothesis</span><br>
= k(k+1)/2 + 2(k+1)/2<br>
= (k+1)(k+2)/2<br>
= (k+1)((k+1)+1)/2 <span style="color:#888888;">// this is the formula with n = k+1</span>
</div>
<p>The formula holds for k + 1. By induction, it holds for all n ≥ 1. □</p>
</div>
<h3>Proof by Contrapositive</h3>
<p>Instead of proving "if A then B," prove the logically equivalent statement "if not B then not A." Sometimes this direction is easier.</p>
<div class="example-box">
<div class="label">Example</div>
<p><strong>Claim:</strong> If n² is even, then n is even.</p>
<p><strong>Contrapositive:</strong> If n is odd, then n² is odd.</p>
<p><strong>Proof:</strong> If n is odd, then n = 2k + 1. So n² = (2k+1)² = 4k² + 4k + 1 = 2(2k² + 2k) + 1, which is odd. □</p>
</div>
<div class="tip-box">
<div class="label">Why CS Cares -- Proofs</div>
<p><strong>Induction</strong> is how you prove recursive algorithms are correct. Every time you write a recursive function, you are implicitly doing induction: the base case is your recursion base case, and the recursive call assumes the function works for smaller inputs (inductive hypothesis). <strong>Loop invariants</strong> use induction to prove loops are correct. <strong>Proof by contradiction</strong> is used to prove impossibility results, like "you can't sort faster than O(n log n) with comparisons."</p>
</div>
</section>
<!-- ===================== SECTION 5: COUNTING ===================== -->
<section id="counting">
<h2>5. Counting and Combinatorics</h2>
<p>Combinatorics is the mathematics of counting. How many ways can something happen? This is essential for analyzing algorithms, understanding probability, and solving optimization problems.</p>
<h3>The Multiplication Principle</h3>
<p>If you have <strong>m</strong> ways to do one thing and <strong>n</strong> ways to do another, there are <strong>m × n</strong> ways to do both.</p>
<div class="example-box">
<div class="label">Example</div>
<p>A restaurant has 3 appetizers and 5 entrees. How many different meals (one appetizer + one entree) can you order?</p>
<p>3 × 5 = <strong>15 meals</strong></p>
<p style="margin-top:0.5rem"><strong>In CS terms:</strong> A 4-bit number has 2 × 2 × 2 × 2 = 2<sup>4</sup> = 16 possible values. An 8-character password using 26 lowercase letters has 26<sup>8</sup> = 208 billion possibilities.</p>
</div>
<h3>Factorials</h3>
<p>The <strong>factorial</strong> of n (written n!) is the product of all positive integers up to n:</p>
<div class="formula-box">
n! = n × (n-1) × (n-2) × ... × 2 × 1<br><br>
5! = 5 × 4 × 3 × 2 × 1 = 120<br>
0! = 1 <span style="color:#888888;">(by definition)</span>
</div>
<p>Factorials grow incredibly fast: 10! = 3,628,800 and 20! = 2,432,902,008,176,640,000.</p>
<h3>Permutations -- Order Matters</h3>
<p>A <strong>permutation</strong> is an arrangement where <strong>order matters</strong>. How many ways can you arrange r items chosen from n items?</p>
<div class="formula-box">
P(n, r) = n! / (n - r)!
</div>
<div class="example-box">
<div class="label">Example</div>
<p>How many ways can 3 students win gold, silver, and bronze medals from a class of 10?</p>
<p>P(10, 3) = 10! / 7! = 10 × 9 × 8 = <strong>720 ways</strong></p>
<p>Order matters because gold-silver-bronze is a different outcome than bronze-silver-gold.</p>
</div>
<h3>Combinations -- Order Doesn't Matter</h3>
<p>A <strong>combination</strong> is a selection where <strong>order does NOT matter</strong>. How many ways can you choose r items from n items?</p>
<div class="formula-box">
C(n, r) = n! / (r! × (n - r)!)
</div>
<p>Also written as <span style="font-family:serif;font-size:1.2rem;">(</span><sup style="font-size:0.85rem;">n</sup><sub style="font-size:0.85rem;">r</sub><span style="font-family:serif;font-size:1.2rem;">)</span> and read "n choose r."</p>
<div class="example-box">
<div class="label">Example</div>
<p>How many ways can you pick a committee of 3 people from 10?</p>
<p>C(10, 3) = 10! / (3! × 7!) = 720 / 6 = <strong>120 ways</strong></p>
<p>Order doesn't matter because {Alice, Bob, Carol} is the same committee as {Carol, Alice, Bob}.</p>
</div>
<div class="warning-box">
<div class="label">Permutations vs. Combinations</div>
<p>Ask yourself: <strong>"Does the order matter?"</strong></p>
<ul>
<li>Arranging books on a shelf → <strong>Permutation</strong> (order matters)</li>
<li>Choosing a team from a group → <strong>Combination</strong> (order doesn't matter)</li>
<li>Creating a password → <strong>Permutation</strong> ("abc" ≠ "cba")</li>
<li>Dealing a poker hand → <strong>Combination</strong> (same cards regardless of deal order)</li>
</ul>
</div>
<h3>The Pigeonhole Principle</h3>
<p>If you put <strong>n + 1</strong> items into <strong>n</strong> containers, at least one container must hold <strong>more than one</strong> item. It sounds obvious, but it has powerful consequences.</p>
<div class="example-box">
<div class="label">Examples</div>
<ul>
<li>In any group of 367 people, at least two share a birthday (366 possible days + 1).</li>
<li>If you have 5 pairs of socks in a dark room and grab 6, you must have at least one matching pair.</li>
<li><strong>CS application:</strong> If a hash function maps to 2<sup>32</sup> values, then with 2<sup>32</sup> + 1 inputs, at least two must collide (same hash). This is why <strong>hash collisions are inevitable</strong>.</li>
</ul>
</div>
<div class="tip-box">
<div class="label">Why CS Cares -- Counting</div>
<p><strong>Algorithm analysis</strong> relies on counting: "How many operations does this algorithm perform?" Combinatorics tells you the size of a search space (e.g., how many possible passwords exist), which determines whether brute force is feasible. <strong>Probability</strong> is built on counting (probability = favorable outcomes / total outcomes). The <strong>pigeonhole principle</strong> proves hash collisions are unavoidable, that lossless compression can't compress all files, and many other impossibility results.</p>
</div>
<h3>Stars and Bars (Distributing Identical Items)</h3>
<p>How many ways can you distribute <strong>n identical items</strong> into <strong>k distinct boxes</strong>?</p>
<div class="formula-box">
Number of ways = C(n + k - 1, k - 1) = C(n + k - 1, n)
</div>
<div class="example-box">
<div class="label">Example</div>
<p>How many ways can you put 10 identical balls into 4 different boxes?</p>
<p>C(10 + 4 - 1, 4 - 1) = C(13, 3) = 286 ways</p>
<p><strong>Visualization:</strong> Imagine 10 balls in a row: ○○○○○○○○○○. Adding 3 dividers creates 4 groups. We're choosing where to place 3 dividers among 13 positions.</p>
</div>
<h3>The Inclusion-Exclusion Principle</h3>
<p>When counting elements in overlapping sets, use inclusion-exclusion to avoid double-counting:</p>
<div class="formula-box">
|A ∪ B| = |A| + |B| - |A ∩ B|<br><br>
|A ∪ B ∪ C| = |A| + |B| + |C| - |A ∩ B| - |A ∩ C| - |B ∩ C| + |A ∩ B ∩ C|
</div>
<div class="example-box">
<div class="label">Example</div>
<p>In a class of 100 students, 60 take Math, 50 take Physics, and 20 take both. How many take at least one?</p>
<p>|Math ∪ Physics| = 60 + 50 - 20 = <strong>90 students</strong></p>
<p>The -20 removes those who were counted twice (in both classes).</p>
</div>
<h3>Permutations with Repetition</h3>
<p>How many arrangements of n objects where some are identical?</p>
<div class="formula-box">
n! / (n₁! × n₂! × ... × nₖ!)<br><br>
Where n₁, n₂, ..., nₖ are the counts of each identical type
</div>
<div class="example-box">
<div class="label">Example</div>
<p>How many ways can you arrange the letters in "MISSISSIPPI"?</p>
<p>Total: 11 letters. M: 1, I: 4, S: 4, P: 2</p>
<p>11! / (1! × 4! × 4! × 2!) = 39,916,800 / (1 × 24 × 24 × 2) = <strong>34,650 arrangements</strong></p>
</div>
<h3>Combinations with Repetition</h3>
<p>Choosing r items from n types, where you can choose the same type multiple times:</p>
<div class="formula-box">
C(n + r - 1, r) <span style="color:#888888;">(same as stars and bars)</span>
</div>
<div class="example-box">
<div class="label">Example</div>
<p>An ice cream shop has 5 flavors. How many ways can you choose 3 scoops (repeats allowed)?</p>
<p>C(5 + 3 - 1, 3) = C(7, 3) = <strong>35 ways</strong></p>
<p>This includes choices like (chocolate, chocolate, vanilla) or (strawberry, strawberry, strawberry).</p>
</div>
<h3>Derangements</h3>
<p>A <strong>derangement</strong> is a permutation where <em>no element appears in its original position</em>.</p>
<div class="formula-box">
D(n) = n! × (1 - 1/1! + 1/2! - 1/3! + ... + (-1)ⁿ/n!)
</div>
<div class="example-box">
<div class="label">Example</div>
<p>5 people put their hats in a pile and randomly pick one. How many ways can NO ONE get their own hat?</p>
<p>D(5) = 5! × (1 - 1 + 1/2 - 1/6 + 1/24 - 1/120)</p>
<p>= 120 × (0 + 0.5 - 0.167 + 0.042 - 0.008)</p>
<p>= 120 × 0.367 = <strong>44 derangements</strong></p>
<p><strong>Fun fact:</strong> As n gets large, approximately 1/e (≈ 36.8%) of all permutations are derangements.</p>
</div>
<h3>Binomial Theorem</h3>
<p>The binomial theorem expands (a + b)ⁿ:</p>
<div class="formula-box">
(a + b)ⁿ = Σ C(n, k) × aⁿ⁻ᵏ × bᵏ for k = 0 to n
</div>
<div class="example-box">
<div class="label">Example: Expand (x + y)⁴</div>
<p>= C(4,0)x⁴ + C(4,1)x³y + C(4,2)x²y² + C(4,3)xy³ + C(4,4)y⁴</p>
<p>= x⁴ + 4x³y + 6x²y² + 4xy³ + y⁴</p>
<p><strong>Pascal's Triangle:</strong> The coefficients (1, 4, 6, 4, 1) form row 4 of Pascal's triangle!</p>
</div>
<h3>Combinatorial Identities</h3>
<table>
<tr><th>Identity</th><th>Meaning</th></tr>
<tr><td>C(n, k) = C(n, n-k)</td><td>Choosing k to include = choosing n-k to exclude</td></tr>
<tr><td>C(n, 0) = C(n, n) = 1</td><td>One way to choose nothing or everything</td></tr>
<tr><td>C(n, k) = C(n-1, k-1) + C(n-1, k)</td><td>Pascal's Triangle rule</td></tr>
<tr><td>Σ C(n, k) = 2ⁿ</td><td>Total subsets of n items</td></tr>
</table>
<div class="tip-box">
<div class="label">CS Application: Subsets and Power Sets</div>
<p>A set of n elements has exactly 2ⁿ subsets (including empty set and the full set). This is why iterating over all subsets of n items takes O(2ⁿ) time -- exponential! For n=30, that's over a billion subsets. Understanding this helps you recognize when a brute-force approach is infeasible.</p>
</div>
</section>
<!-- ===================== SECTION 6: GRAPH THEORY ===================== -->
<section id="graphs">
<h2>6. Graph Theory</h2>
<p>A <strong>graph</strong> G = (V, E) consists of <strong>vertices</strong> (nodes) V and <strong>edges</strong> (connections) E. Graphs are one of the most important data structures in computer science -- they model networks, relationships, maps, dependencies, and much more.</p>
<h3>Types of Graphs</h3>
<table>
<tr>
<th>Type</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<td><strong>Undirected</strong></td>
<td>Edges have no direction. If A connects to B, then B connects to A.</td>
<td>Facebook friendships</td>
</tr>
<tr>
<td><strong>Directed</strong></td>
<td>Edges have direction. A → B does not mean B → A.</td>
<td>Twitter follows, web links</td>
</tr>
<tr>
<td><strong>Weighted</strong></td>
<td>Edges have values (costs, distances, capacities).</td>
<td>Road maps with distances</td>
</tr>
<tr>
<td><strong>Unweighted</strong></td>
<td>All edges are equal -- no values attached.</td>
<td>Social connections</td>
</tr>
</table>
<h3>Graph Representations</h3>
<p>There are two main ways to store a graph in code:</p>
<p><strong>Adjacency Matrix</strong> -- a 2D array where matrix[i][j] = 1 if there is an edge from vertex i to vertex j.</p>
<div class="example-box">
<div class="label">Adjacency Matrix</div>
<pre><code><span class="comment"># Graph: A--B, A--C, B--C</span>
<span class="comment"># A B C</span>
<span class="comment"># A [ 0, 1, 1 ]</span>
<span class="comment"># B [ 1, 0, 1 ]</span>
<span class="comment"># C [ 1, 1, 0 ]</span>
matrix = [
[<span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>],
[<span class="number">1</span>, <span class="number">0</span>, <span class="number">1</span>],
[<span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>]
]</code></pre>
<p style="margin-top:0.5rem"><strong>Space:</strong> O(V²). <strong>Edge lookup:</strong> O(1). Best for dense graphs.</p>
</div>
<p><strong>Adjacency List</strong> -- each vertex stores a list of its neighbors.</p>
<div class="example-box">
<div class="label">Adjacency List</div>
<pre><code><span class="comment"># Same graph: A--B, A--C, B--C</span>
graph = {
<span class="string">'A'</span>: [<span class="string">'B'</span>, <span class="string">'C'</span>],
<span class="string">'B'</span>: [<span class="string">'A'</span>, <span class="string">'C'</span>],
<span class="string">'C'</span>: [<span class="string">'A'</span>, <span class="string">'B'</span>]
}</code></pre>
<p style="margin-top:0.5rem"><strong>Space:</strong> O(V + E). <strong>Edge lookup:</strong> O(degree). Best for sparse graphs (most real-world graphs).</p>
</div>
<h3>Key Graph Concepts</h3>
<ul>
<li><strong>Degree</strong> of a vertex: number of edges connected to it. In a directed graph, distinguish between in-degree (edges coming in) and out-degree (edges going out).</li>
<li><strong>Path:</strong> A sequence of vertices connected by edges. A → B → C is a path of length 2.</li>
<li><strong>Cycle:</strong> A path that starts and ends at the same vertex. A → B → C → A.</li>
<li><strong>Tree:</strong> A connected graph with no cycles. A tree with n vertices has exactly n - 1 edges.</li>
<li><strong>Connected graph:</strong> Every vertex can reach every other vertex.</li>
</ul>
<h3>Graph Traversals</h3>
<p>Two fundamental ways to explore a graph:</p>
<table>
<tr>
<th>Algorithm</th>
<th>Strategy</th>
<th>Data Structure</th>
<th>Use Case</th>
</tr>
<tr>
<td><strong>BFS</strong> (Breadth-First Search)</td>
<td>Explore all neighbors first, then their neighbors</td>
<td>Queue</td>
<td>Shortest path (unweighted), level-order</td>
</tr>
<tr>
<td><strong>DFS</strong> (Depth-First Search)</td>
<td>Go as deep as possible, then backtrack</td>
<td>Stack / recursion</td>
<td>Cycle detection, topological sort, maze solving</td>
</tr>
</table>
<div class="example-box">
<div class="label">BFS and DFS in Python</div>
<pre><code><span class="keyword">from</span> collections <span class="keyword">import</span> deque
<span class="keyword">def</span> <span class="function">bfs</span>(graph, start):
visited = <span class="builtin">set</span>()
queue = deque([start])
visited.add(start)
<span class="keyword">while</span> queue:
vertex = queue.popleft()
<span class="builtin">print</span>(vertex, end=<span class="string">' '</span>)
<span class="keyword">for</span> neighbor <span class="keyword">in</span> graph[vertex]:
<span class="keyword">if</span> neighbor <span class="keyword">not</span> <span class="keyword">in</span> visited:
visited.add(neighbor)
queue.append(neighbor)
<span class="keyword">def</span> <span class="function">dfs</span>(graph, start, visited=<span class="keyword">None</span>):
<span class="keyword">if</span> visited <span class="keyword">is</span> <span class="keyword">None</span>:
visited = <span class="builtin">set</span>()
visited.add(start)
<span class="builtin">print</span>(start, end=<span class="string">' '</span>)
<span class="keyword">for</span> neighbor <span class="keyword">in</span> graph[start]:
<span class="keyword">if</span> neighbor <span class="keyword">not</span> <span class="keyword">in</span> visited:
dfs(graph, neighbor, visited)</code></pre>
</div>
<h3>Euler Paths and Circuits</h3>
<p>An <strong>Euler path</strong> visits every <em>edge</em> exactly once. An <strong>Euler circuit</strong> is an Euler path that starts and ends at the same vertex.</p>
<ul>
<li>An undirected graph has an <strong>Euler circuit</strong> if and only if every vertex has even degree.</li>
<li>An undirected graph has an <strong>Euler path</strong> (but not circuit) if and only if exactly two vertices have odd degree.</li>
</ul>
<div class="example-box">
<div class="label">The Konigsberg Bridge Problem</div>
<p>This is the problem that founded graph theory. The city of Konigsberg had 7 bridges connecting 4 land masses. Euler proved it was impossible to walk across every bridge exactly once because more than two vertices had odd degree.</p>
</div>
<div class="tip-box">
<div class="label">Why CS Cares -- Graphs</div>
<p>Graphs are arguably the most important data structure in CS. <strong>The internet</strong> is a graph (pages are vertices, links are edges). <strong>Social networks</strong> are graphs. <strong>GPS navigation</strong> uses weighted graphs with shortest-path algorithms (Dijkstra's). <strong>Package managers</strong> (npm, pip) use directed acyclic graphs for dependency resolution. <strong>Compilers</strong> build abstract syntax trees and control flow graphs. <strong>Databases</strong> use B-trees (tree-structured graphs) for indexing. If you learn one data structure deeply, make it graphs.</p>
</div>
<div class="formula-box">
<strong>Handshaking Lemma:</strong><br><br>
In any undirected graph: Σ deg(v) = 2|E|<br><br>
<span style="color:#555555;">The sum of all vertex degrees equals twice the number of edges. Every edge contributes 1 to each of its endpoints. Consequence: the number of vertices with odd degree is always even.</span>
</div>
</section>
<!-- ===================== SECTION 7: NUMBER THEORY ===================== -->
<section id="number-theory">
<h2>7. Number Theory and Modular Arithmetic</h2>
<p>Number theory studies the properties of integers. It might seem abstract, but it is the mathematical backbone of <strong>cryptography</strong>, <strong>hashing</strong>, and <strong>error detection</strong>.</p>
<h3>Divisibility</h3>
<p>We say <strong>a divides b</strong> (written a | b) if b = a × k for some integer k. For example, 3 | 12 because 12 = 3 × 4.</p>
<h3>Prime Numbers and Factorization</h3>
<p>A <strong>prime number</strong> is greater than 1 and divisible only by 1 and itself. The first primes: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29...</p>
<p>The <strong>Fundamental Theorem of Arithmetic</strong> says every integer greater than 1 can be written as a unique product of primes:</p>
<div class="formula-box">
60 = 2² × 3 × 5<br>
84 = 2² × 3 × 7<br>
100 = 2² × 5²
</div>
<h3>GCD and LCM</h3>
<p>The <strong>Greatest Common Divisor</strong> GCD(a, b) is the largest number that divides both a and b. The <strong>Least Common Multiple</strong> LCM(a, b) is the smallest number that both a and b divide.</p>
<div class="formula-box">
GCD(12, 18) = 6<br>
LCM(12, 18) = 36<br><br>
Useful identity: GCD(a, b) × LCM(a, b) = a × b
</div>
<h3>The Euclidean Algorithm</h3>
<p>An efficient method to compute GCD using repeated division. This is one of the oldest algorithms known (over 2300 years old) and is still used today.</p>
<div class="formula-box">
GCD(a, b) = GCD(b, a mod b) <span style="color:#888888;">until b = 0</span>
</div>
<div class="example-box">
<div class="label">Example -- Euclidean Algorithm</div>
<p>Find GCD(252, 105):</p>
<ul>
<li>GCD(252, 105): 252 mod 105 = 42, so GCD(105, 42)</li>
<li>GCD(105, 42): 105 mod 42 = 21, so GCD(42, 21)</li>
<li>GCD(42, 21): 42 mod 21 = 0, so <strong>GCD = 21</strong></li>
</ul>
</div>