Skip to content

Commit 3137b0c

Browse files
committed
graph-bfs-dfs (in fact just a howto)
1 parent b4f6cc1 commit 3137b0c

File tree

3 files changed

+338
-11
lines changed

3 files changed

+338
-11
lines changed

notebooks/_toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ parts:
2424
- file: howtos/encodings/HOWTO-encodings-nb
2525
- file: howtos/requests/HOWTO-requests-nb
2626
- file: howtos/adnwalk/HOWTO-adnwalk-nb
27+
- glob: tps/graph-bfs-dfs/README*
2728
- file: howtos/subprocesses/HOWTO-subprocesses-nb
2829
- caption: TPs basiques
2930
chapters:
@@ -46,7 +47,6 @@ parts:
4647
- glob: tps/explang/README*
4748
- caption: TPs graphes
4849
chapters:
49-
- glob: tps/graph-bfs-dfs/README*
5050
- glob: tps/graph-shortest-path/README*
5151
- file: tps/metro/README-metro-nb
5252
- glob: tps/pagerank-thrones/README*

notebooks/tps/graph-bfs-dfs/.teacher/README-graph-bfs-dfs-corrige-nb.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ language_info:
1414
pygments_lexer: ipython3
1515
---
1616

17-
# graph browsing
17+
# walking a graph: BFS - DFS
18+
19+
+++
20+
21+
Licence CC BY-NC-ND, Thierry Parmentelat
1822

1923
+++ {"tags": ["prune-cell"]}
2024

@@ -26,9 +30,17 @@ there are no difference - apart from this very cell - between the teacher and th
2630

2731
+++
2832

29-
## depth-first or breadth-first scanning
33+
```{admonition} just a HOWTO
34+
:class: tip
35+
36+
this notebook contains running code, there is no exercise attached
37+
```
38+
39+
+++
40+
41+
## depth-first or breadth-first
3042

31-
given a non-valued directed graph G, and a start vertex V, there are 2 famous algorithm to walk the graph from V
43+
given a non-valued directed graph G, and a start vertex V, there are 2 famous algorithms to walk the graph from V
3244

3345
* depth-first (DF) browsing, and
3446
* breadth-first (BF) browsing
@@ -61,8 +73,9 @@ these 2 algorithms would yield the nodes in the following orders
6173

6274
+++ {"cell_style": "split"}
6375

64-
DF browsing **from `v`** would scan
65-
```
76+
DF browsing **from `v`** would scan (the newlines are only cosmetic)
77+
78+
```text
6679
v v1 v11 v111 v112
6780
v12 v121 v122
6881
v2 v21 v211 v212
@@ -71,7 +84,7 @@ v22 v221 v222
7184

7285
+++ {"cell_style": "split"}
7386

74-
BF browsing **from `v`** would scan
87+
BF browsing **from `v`** would scan (ditto)
7588
```
7689
v
7790
v1 v2
@@ -85,16 +98,14 @@ v111 v112 v121 v122 v211 v212 v221 v222
8598

8699
+++
87100

88-
we want to write a **generator** that implements these 2 browsing policies from a graph and vertex.
89-
101+
we want to write a **generator** that implements these 2 browsing policies from a graph and vertex.
90102
of course, only the nodes reachable from the entry vertex will be browsed by this method
91103

92104
+++
93105

94106
## algorithms
95107

96-
the 2 algorithms used to perform these scans are, interestingly, **very close** to one another
97-
108+
the 2 algorithms used to perform these scans are, interestingly, **very close** to one another
98109
in both cases we need a STORAGE object, where we can `store()` things and `retrieve()` them later on
99110

100111
+++
@@ -108,9 +119,12 @@ Let us consider the following 2 storage implementations:
108119
* `Fifo` implements a *first-in-first-out* policy
109120
* `Filo` does the exact opposite and has a *first-in-last-out* policy
110121

122+
```{admonition} list or deque
123+
111124
Remember the regular `list` class is more optimized for a `append()/pop()` usage
112125
113126
So to work around that, we're using a `deque` class, instead of a simple list; it is actually useful only in the `Filo` case, but this way we have a more homogeneous code
127+
```
114128

115129
**Exercise**: you may wish to factorize both into a single class...
116130

0 commit comments

Comments
 (0)