Skip to content

Commit 43b895a

Browse files
update on css
1 parent 5e0c98f commit 43b895a

9 files changed

Lines changed: 1929 additions & 0 deletions

File tree

docs/Algorithms_and_data_structures/bfs_algorithm.md

Lines changed: 466 additions & 0 deletions
Large diffs are not rendered by default.

docs/Algorithms_and_data_structures/dfs_algorithm.md

Lines changed: 533 additions & 0 deletions
Large diffs are not rendered by default.

docs/Algorithms_and_data_structures/dijkstra_algorithm.md

Lines changed: 594 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 336 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,336 @@
1+
# Queue vs Heap Queue (`heapq`)
2+
> Understanding why BFS uses queues and Dijkstra uses priority queues.
3+
4+
---
5+
6+
# What is a Queue?
7+
8+
A queue works like:
9+
10+
```text
11+
First In
12+
First Out
13+
```
14+
15+
This is called:
16+
17+
# FIFO
18+
19+
---
20+
21+
# Example
22+
23+
```text
24+
[A, B, C]
25+
```
26+
27+
Removing:
28+
29+
```text
30+
A
31+
```
32+
33+
because:
34+
35+
```text
36+
A entered first
37+
```
38+
39+
---
40+
41+
# deque
42+
43+
Python provides:
44+
45+
```python
46+
from collections import deque
47+
```
48+
49+
`deque` means:
50+
51+
```text
52+
double-ended queue
53+
```
54+
55+
Useful operations:
56+
57+
```python
58+
append()
59+
popleft()
60+
```
61+
62+
---
63+
64+
# Why BFS uses deque
65+
66+
BFS explores:
67+
68+
```text
69+
level by level
70+
```
71+
72+
Example:
73+
74+
```text
75+
start
76+
├── A
77+
├── B
78+
└── C
79+
```
80+
81+
BFS processes:
82+
83+
```text
84+
A first
85+
then B
86+
then C
87+
```
88+
89+
because:
90+
91+
```text
92+
they were inserted first
93+
```
94+
95+
---
96+
97+
# BFS Behavior
98+
99+
```text
100+
ORDER OF ARRIVAL
101+
```
102+
103+
This is PERFECT for:
104+
105+
```python
106+
deque
107+
```
108+
109+
---
110+
111+
# What is heapq?
112+
113+
`heapq` is Python's built-in:
114+
115+
# Priority Queue
116+
117+
---
118+
119+
# Difference from normal queue
120+
121+
A normal queue removes:
122+
123+
```text
124+
oldest element
125+
```
126+
127+
A priority queue removes:
128+
129+
```text
130+
lowest cost first
131+
```
132+
133+
---
134+
135+
# Example
136+
137+
You insert:
138+
139+
```python
140+
(5, "B")
141+
(1, "A")
142+
(3, "C")
143+
```
144+
145+
---
146+
147+
# heapq automatically organizes:
148+
149+
```text
150+
lowest cost first
151+
```
152+
153+
Then:
154+
155+
```python
156+
heapq.heappop(queue)
157+
```
158+
159+
returns:
160+
161+
```python
162+
(1, "A")
163+
```
164+
165+
EVEN IF:
166+
167+
```text
168+
A was inserted later
169+
```
170+
171+
---
172+
173+
# Why Dijkstra uses heapq
174+
175+
Dijkstra constantly asks:
176+
177+
```text
178+
"What is the CHEAPEST current route?"
179+
```
180+
181+
NOT:
182+
183+
```text
184+
"What was inserted first?"
185+
```
186+
187+
That is why:
188+
189+
```python
190+
deque
191+
```
192+
193+
is not ideal for Dijkstra.
194+
195+
---
196+
197+
# Dijkstra needs:
198+
199+
```text
200+
LOWEST COST FIRST
201+
```
202+
203+
Which is exactly what:
204+
205+
```python
206+
heapq
207+
```
208+
209+
does.
210+
211+
---
212+
213+
# Dijkstra Example
214+
215+
```python
216+
import heapq
217+
218+
queue = []
219+
220+
heapq.heappush(queue, (2, "roof1"))
221+
heapq.heappush(queue, (1, "corridorA"))
222+
heapq.heappush(queue, (5, "goal"))
223+
224+
print(heapq.heappop(queue))
225+
```
226+
227+
Output:
228+
229+
```python
230+
(1, "corridorA")
231+
```
232+
233+
---
234+
235+
# Visual Comparison
236+
237+
# deque / BFS
238+
239+
```text
240+
ORDER OF ARRIVAL
241+
```
242+
243+
```text
244+
A → B → C
245+
```
246+
247+
---
248+
249+
# heapq / Dijkstra
250+
251+
```text
252+
LOWEST COST FIRST
253+
```
254+
255+
```text
256+
1 → 2 → 5
257+
```
258+
259+
---
260+
261+
# Fly-in Connection
262+
263+
# BFS
264+
265+
Good for:
266+
- unweighted graphs
267+
- same movement costs
268+
269+
---
270+
271+
# Dijkstra
272+
273+
Good for:
274+
- weighted graphs
275+
- movement costs
276+
- route optimization
277+
278+
Fly-in is:
279+
280+
```text
281+
Weighted Graph Pathfinding
282+
```
283+
284+
So:
285+
286+
# heapq + Dijkstra
287+
fits the project much better.
288+
289+
---
290+
291+
# Mental Model
292+
293+
# BFS / deque
294+
295+
Think:
296+
297+
```text
298+
"Who arrived first?"
299+
```
300+
301+
---
302+
303+
# Dijkstra / heapq
304+
305+
Think:
306+
307+
```text
308+
"Who is currently the cheapest?"
309+
```
310+
311+
---
312+
313+
# Final Mental Image
314+
315+
# deque
316+
317+
```text
318+
Supermarket line
319+
```
320+
321+
First customer:
322+
- enters first
323+
- leaves first
324+
325+
---
326+
327+
# heapq
328+
329+
```text
330+
Hospital emergency room
331+
```
332+
333+
Most urgent patient:
334+
- treated first
335+
- even if arrived later
336+

docs/Python_core/json_and_data_validation/json_and_data_validation.md renamed to docs/Python_core/Data_validation/json_and_data_validation.md

File renamed without changes.

docs/Python_core/Pydantic/pydantic_concepts_review.md renamed to docs/Python_core/Data_validation/pydantic_concepts_review.md

File renamed without changes.
File renamed without changes.

docs/Python_core/Virtual_environments/virtual_environments.md renamed to docs/Python_core/Environment_and_packages/virtual_environments.md

File renamed without changes.

docs/Python_core/Classes/class_methods_and_attributes.md renamed to docs/Python_core/OOP/class_methods_and_attributes.md

File renamed without changes.

0 commit comments

Comments
 (0)