Skip to content

Commit 7c7cf2d

Browse files
committed
various edits + additions to design patterns
1 parent 8b79e5a commit 7c7cf2d

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

docs/algorithms.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ END IF
169169

170170
When an if statement has cascading `ELSE IF` statements
171171

172-
```js
172+
```txt
173173
IF the student's mark was below 50
174174
{
175175
write "Fail"
@@ -372,7 +372,7 @@ Big $O$ notation:
372372

373373
!!! note
374374

375-
[https://www.bigocheatsheet.com/](https://www.bigocheatsheet.com/)
375+
Table of useful time complexities: [https://www.bigocheatsheet.com/](https://www.bigocheatsheet.com/)
376376

377377
### Selection sort
378378

@@ -426,7 +426,8 @@ $$O(n)$$
426426
### Merge-sort
427427

428428
<img src="images/Merge-sort-example-300px.gif" alt="Merge-sort-example-300px">
429-
<sup>Gif of mergesort</sup>
429+
430+
A commonly used [divide and conquer](#divide-and-conquer) sorting algorithm. In fact it's probably one of the best examples of divide and conquer there is. The fundamental procedure of merge sort is to split apart the input (dividing) and solve each of those parts individually (conquoring) and then building the solution back together. This is a key difference between divide and conquer and [decrease and conquer](#decrease-and-conquer) as the input is being *divided* into sub-problems that are **all** solved, instead of *decreasing* the problem by discarding parts that don't need to be solved.
430431

431432
## Searching
432433

@@ -462,8 +463,9 @@ $$O(\log_2 n) \implies O(\log n)$$
462463

463464
!!! info
464465

465-
- Use [Divide and Conquer](#divide-and-conquer) when you **==don't== have overlapping sub problems**
466-
- Use [Dynamic Programming](#dynamic-programming) when you **==do== have overlapping sub problems**
466+
- Use [Divide and Conquer](#divide-and-conquer) when you **don't have overlapping sub problems**
467+
- Use [Decrease and Conquer](#decrease-and-conquer) when you can **discard/ignore parts of the input** entirely
468+
- Use [Dynamic Programming](#dynamic-programming) when you **do have overlapping sub problems**
467469
- Use [Backtracking](#backtracking) when you need to backtrack 😎
468470

469471
#### Brute Force
@@ -474,15 +476,12 @@ Checking every possible combination to see if it is valid as you go along. But i
474476

475477
#### Decrease and Conquer
476478

477-
!!! warning
478-
479-
**Decrease** and conquer decreases, and does not divide. Hence the time complexity reccurance of:
479+
!!! summary
480480

481-
$$T(n) = T(n-b)$$ Not $$T(n) = T\left(\frac{n}{b}\right)$$
482-
483-
[Master theorem](#master-theorem)
481+
Remember decrease and conquer as "**decreasing** the problem by discarding things I don't need, and conquoring the original problem with what I have left"
484482

485483
Decrease and Conquer algorithms make the problem smaller by reducing problem at each iteration. They can reduce the problem by
484+
486485
- Constant amount
487486
- Variable amount
488487

@@ -491,11 +490,13 @@ Decrease and Conquer algorithms make the problem smaller by reducing problem at
491490
Efficient when the problem can be systematically decreased to a solution
492491

493492
**Steps for Decrease & Conquer:**
493+
494494
- Reduce original problem to a smaller instance of the same problem
495495
- Solve the smaller instance
496496
- Extend the solution of the smaller instance to get the solution to the overall problem
497497

498498
**Examples**:
499+
499500
- [BFS Breadth First Search](graphs.md#bfs-breadth-first-search)
500501
- [DFS Depth First Search](graphs.md#dfs-depth-first-search)
501502

@@ -505,15 +506,11 @@ Efficient when the problem can be systematically decreased to a solution
505506

506507
<img src="images/Pasted image 20220728144049.png" alt="Pasted image 20220728144049">
507508

508-
!!! warning
509-
510-
**Divide** and conquer divides, and does not staticly decrease. Hence the time complexity reccurance of:
511-
512-
$$T(n) = T\left(\frac{n}{b}\right)$$ Not $$T(n) = T(n-b)$$
513-
514-
[Master theorem](#master-theorem)
509+
When the input size is divided into smaller parts and each smaller part is solved in isolation to build to a final solution.
515510

516-
When the input size is divided into smaller parts
511+
!!! note
512+
513+
A good explanation of how divide and conquer is different to [decrease and conquer](#decrease-and-conquer) is available in the [merge sort](#merge-sort) section
517514

518515
#### Dynamic Programming
519516

@@ -528,6 +525,7 @@ When the input size is divided into smaller parts
528525

529526

530527
Examples:
528+
531529
- [Floyd-Warshall algorithm](graphs.md#floyd-warshall-algorithm)
532530
- [Belman-Ford algorithm](graphs.md#belman-ford-algorithm)
533531

@@ -536,13 +534,16 @@ Examples:
536534
In general, DP solutions are used to maximise or minimise. Knapsack or coin change.
537535

538536
They store previous answers to stop the repetition of overlapping sub-problems
537+
539538
##### "overlapping sub-problems"
540539

541540
Identical problems that are occurring multiples times during computation
542541

543-
#### Backtracking
542+
### Backtracking
544543

545-
>[!warning] Not sure if this is an actual design pattern
544+
!!! danger
545+
546+
This is not a design pattern. It's more of an 'approach' to solve a problem
546547

547548
lmao sudoku
548549

mkdocs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ markdown_extensions:
3333
- admonition
3434
- pymdownx.details
3535
- pymdownx.superfences
36+
- def_list
37+
- pymdownx.tasklist:
38+
custom_checkbox: true
3639
- toc:
3740
permalink: true
3841
- attr_list

0 commit comments

Comments
 (0)