Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion chapters/_subfiles/graphs/_racket-assoc_exr.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
In Racket, the adjacency list of the office graph (@exm-adjacency-list) can be stored as a list with one list for each vertex,
which starts with the vertex and goes on with its neighbors:

```racket
```{racket}
#| code-fold: false
(define office
'((a b c)
(b a c d)
Expand Down
8 changes: 8 additions & 0 deletions chapters/_subfiles/graphs/_racket-assoc_sol.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ It returns `'(d b e f)`,
the element of `office` that starts with `d`.
Its rest, `'(b e f)`, lists the neighbors of $d$ (@def-adjacent).

Racket returns the same values:

```{racket}
#| code-fold: false
(assoc 'd office)
(rest (assoc 'd office))
```

:::
11 changes: 8 additions & 3 deletions chapters/_subfiles/graphs/_racket-bfs_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
::: {#exm-racket-bfs}

Check warning on line 1 in chapters/_subfiles/graphs/_racket-bfs_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

Example #exm-racket-bfs has no figure. Fix: add a figure of the example's data, model or results, or add <!-- no-figure: reason --> inside the div
#### Breadth-first search in Racket

This Racket version of `bfs` (@def-bfs) keeps the labels in a list of pairs `(list u d)`,
Expand All @@ -6,7 +6,8 @@
so a vertex is marked when `assoc` (@exr-racket-assoc) finds it in that list.
It uses `neighbors` from @exm-racket-neighbors.

```racket
```{racket}
#| code-fold: false
(define (unmarked vs dist)
(if (empty? vs)
empty
Expand Down Expand Up @@ -40,7 +41,11 @@
so `(append (rest queue) new)` removes the front of the queue and adds the new vertices at the back.

Each call of `bfs-loop` is one pass through the loop of @def-bfs.
`(bfs office 'a)` returns `'((a 0) (b 1) (c 1) (d 2) (e 2) (f 3))`,
the marks and labels of @exm-bfs-trace.
From $a$, `bfs` returns the marks and labels of @exm-bfs-trace:

```{racket}
#| code-fold: false
(bfs office 'a)
```

:::
16 changes: 12 additions & 4 deletions chapters/_subfiles/graphs/_racket-dfs_exm.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ and `dfs-each` goes through a list `vs` of vertices,
calling `dfs-from` on each one that is not marked yet.
It uses `neighbors` from @exm-racket-neighbors.

```racket
```{racket}
#| code-fold: false
(define (dfs-from G v marked)
(dfs-each G (neighbors G v) (append marked (list v))))

Expand All @@ -25,7 +26,8 @@ It uses `neighbors` from @exm-racket-neighbors.

With the graph $T$ of @fig-triangles stored as

```racket
```{racket}
#| code-fold: false
(define triangles
'((1 2 3)
(2 1 3)
Expand All @@ -36,7 +38,13 @@ With the graph $T$ of @fig-triangles stored as
(7 5 6)))
```

`(dfs triangles 3)` returns `'(3 1 2 4 5 6 7)`, the order of @exm-dfs-trace,
and `(dfs office 'a)` returns `'(a b c e d f)`, the order of @sol-follow-first.
`(dfs triangles 3)` returns the order of @exm-dfs-trace,
and `(dfs office 'a)` returns the order of @sol-follow-first:

```{racket}
#| code-fold: false
(dfs triangles 3)
(dfs office 'a)
```

:::
20 changes: 14 additions & 6 deletions chapters/_subfiles/graphs/_racket-neighbors_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
::: {#exm-racket-neighbors}

Check warning on line 1 in chapters/_subfiles/graphs/_racket-neighbors_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

Example #exm-racket-neighbors has no figure. Fix: add a figure of the example's data, model or results, or add <!-- no-figure: reason --> inside the div
#### Neighbors and degrees in Racket

With `office` as in @exr-racket-assoc,
these functions return the neighbors (@def-adjacent) and the degree (@def-degree) of a vertex of a graph with no loops or parallel edges:

```racket
```{racket}
#| code-fold: false
(define (neighbors G v)
(rest (assoc v G)))

(define (degree G v)
(length (neighbors G v)))
```

`(neighbors office 'b)` returns `'(a c d)`, and `(degree office 'b)` returns 3.
For the vertex $b$:

```{racket}
#| code-fold: false
(neighbors office 'b)
(degree office 'b)
```

`(for/sum ([x L]) body)` adds up the values of `body` for each element `x` of the list `L`,
so
so this chunk adds up the degrees of the six vertices.
By @thm-handshake, that sum is $2 \card{E}$:

```racket
```{racket}
#| code-fold: false
(for/sum ([row office])
(degree office (first row)))
```

adds up the degrees of the six vertices and returns 16, which is $2 \card{E}$, as @thm-handshake says.

:::
2 changes: 2 additions & 0 deletions chapters/graphs.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ format:

{{< include ../latex-macros/macros.qmd >}}

{{< include _subfiles/_racket-setup.qmd >}}

::: {.callout-note appearance='simple' collapse=true}
## Source

Expand Down
Loading