diff --git a/chapters/_subfiles/graphs/_racket-assoc_exr.qmd b/chapters/_subfiles/graphs/_racket-assoc_exr.qmd index bddf36b41..9a982e9fc 100644 --- a/chapters/_subfiles/graphs/_racket-assoc_exr.qmd +++ b/chapters/_subfiles/graphs/_racket-assoc_exr.qmd @@ -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) diff --git a/chapters/_subfiles/graphs/_racket-assoc_sol.qmd b/chapters/_subfiles/graphs/_racket-assoc_sol.qmd index f52ad72e3..8abe1bbdd 100644 --- a/chapters/_subfiles/graphs/_racket-assoc_sol.qmd +++ b/chapters/_subfiles/graphs/_racket-assoc_sol.qmd @@ -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)) +``` + ::: diff --git a/chapters/_subfiles/graphs/_racket-bfs_exm.qmd b/chapters/_subfiles/graphs/_racket-bfs_exm.qmd index d670d73ee..79fab22c1 100644 --- a/chapters/_subfiles/graphs/_racket-bfs_exm.qmd +++ b/chapters/_subfiles/graphs/_racket-bfs_exm.qmd @@ -6,7 +6,8 @@ one for each marked vertex $u$ with label $d$, 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 @@ -40,7 +41,11 @@ It uses `neighbors` from @exm-racket-neighbors. 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) +``` ::: diff --git a/chapters/_subfiles/graphs/_racket-dfs_exm.qmd b/chapters/_subfiles/graphs/_racket-dfs_exm.qmd index 0fa09fc3e..87dd5d319 100644 --- a/chapters/_subfiles/graphs/_racket-dfs_exm.qmd +++ b/chapters/_subfiles/graphs/_racket-dfs_exm.qmd @@ -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)))) @@ -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) @@ -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) +``` ::: diff --git a/chapters/_subfiles/graphs/_racket-neighbors_exm.qmd b/chapters/_subfiles/graphs/_racket-neighbors_exm.qmd index 6665c8713..6453276ff 100644 --- a/chapters/_subfiles/graphs/_racket-neighbors_exm.qmd +++ b/chapters/_subfiles/graphs/_racket-neighbors_exm.qmd @@ -4,7 +4,8 @@ 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))) @@ -12,15 +13,22 @@ these functions return the neighbors (@def-adjacent) and the degree (@def-degree (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. - ::: diff --git a/chapters/graphs.qmd b/chapters/graphs.qmd index 37c2c5c2c..ae3b90149 100644 --- a/chapters/graphs.qmd +++ b/chapters/graphs.qmd @@ -13,6 +13,8 @@ format: {{< include ../latex-macros/macros.qmd >}} +{{< include _subfiles/_racket-setup.qmd >}} + ::: {.callout-note appearance='simple' collapse=true} ## Source