Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Commit

Permalink
fix: typos in bfs/dfs
Browse files Browse the repository at this point in the history
  • Loading branch information
storopoli committed Oct 4, 2024
1 parent ccbf28f commit f1f4b2d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
24 changes: 15 additions & 9 deletions slides/slides-pt.typ
Original file line number Diff line number Diff line change
Expand Up @@ -1963,16 +1963,16 @@ proposicional pode ser tornada verdadeira* por meio de uma atribuição adequada
#pseudocode-list(
title: smallcaps[dado um grafo $G$, um vértice raiz _root_, e valor-alvo $T$:],
)[
+ $Q =: "queue"$
+ _root_.explored $= "true"$
+ $Q := "queue"$
+ _root_.explored $:= "true"$
+ $Q$.enqueue(_root_)
+ *while* $!Q$.empty():
+ $v := Q$.dequeue()
+ *if* $v = T$
+ *return* $v$
+ *for* todas as arestas de $v$ para $w$ *in* $G$.adjacentEdges(v):
+ *if* $!w$.explored:
+ $w$.explored
+ $w$.explored $:= "true"$
+ $w$.parent $:= v$
+ $Q$.enqueue($w$)
+ *return* _null_
Expand All @@ -1986,9 +1986,11 @@ proposicional pode ser tornada verdadeira* por meio de uma atribuição adequada
=== Exemplo em C

#align(horizon)[
#text(size: 9pt)[
#text(size: 7pt)[
```c
void bfs(int grafo[][MAX], int inicio, int n) {
int bfs(int grafo[][MAX], int inicio, int n) {
if (inicio->value == T)
return inicio->id;
int visitado[MAX] = {0};
int fila[MAX], frente = 0, traseira = 0;
Expand All @@ -1997,15 +1999,17 @@ proposicional pode ser tornada verdadeira* por meio de uma atribuição adequada
while (frente < traseira) {
int atual = fila[frente++];
printf("%d ", atual);
for (int i = 0; i < n; i++) {
if (atual->value == T)
return atual->id;
if (grafo[atual][i] && !visitado[i]) {
visitado[i] = 1;
fila[traseira++] = i;
}
}
}
return -1;
}
```
]
Expand Down Expand Up @@ -2083,7 +2087,7 @@ proposicional pode ser tornada verdadeira* por meio de uma atribuição adequada
#pseudocode-list(
title: smallcaps[dado um grafo $G$, um vértice $v$, e valor-alvo $T$:],
)[
+ $v$.discovered
+ $v$.discovered $:= "true"$
+ *if* $v = T$
+ *return* $v$
+ *for* todas as arestas de $v$ para $w$ *in* $G$.adjacentEdges(v):
Expand All @@ -2102,15 +2106,17 @@ proposicional pode ser tornada verdadeira* por meio de uma atribuição adequada
#align(horizon)[
#text(size: 13pt)[
```c
void dfs(int grafo[][MAX], int atual, int visitado[], int n) {
int dfs(int grafo[][MAX], int atual, int visitado[], int n) {
if (atual->value == T)
return atual->id;
visitado[atual] = 1;
printf("%d ", atual);
for (int i = 0; i < n; i++) {
if (grafo[atual][i] && !visitado[i]) {
dfs(grafo, i, visitado, n);
}
}
return -1;
}
```
]
Expand Down
26 changes: 16 additions & 10 deletions slides/slides.typ
Original file line number Diff line number Diff line change
Expand Up @@ -1941,16 +1941,16 @@ subtrees is at most 1.
#pseudocode-list(
title: smallcaps[given a graph $G$, a _root_ vertex, and target value $T$:],
)[
+ $Q =: "queue"$
+ _root_.explored $= "true"$
+ $Q := "queue"$
+ _root_.explored $:= "true"$
+ $Q$.enqueue(_root_)
+ *while* $!Q$.empty():
+ $v := Q$.dequeue()
+ *if* $v = T$
+ *return* $v$
+ *for* all edges from $v$ to $w$ *in* $G$.adjacentEdges(v):
+ *if* $!w$.explored:
+ $w$.explored
+ $w$.explored $:= "true"$
+ $w$.parent $:= v$
+ $Q$.enqueue($w$)
+ *return* _null_
Expand All @@ -1964,9 +1964,11 @@ subtrees is at most 1.
=== Example in C

#align(horizon)[
#text(size: 9pt)[
#text(size: 7pt)[
```c
void bfs(int graph[][MAX], int start, int n) {
int bfs(int graph[][MAX], int start, int n, int target) {
if (start->value == T)
return start->id;
int visited[MAX] = {0};
int queue[MAX], front = 0, rear = 0;
Expand All @@ -1975,15 +1977,17 @@ subtrees is at most 1.
while (front < rear) {
int current = queue[front++];
printf("%d ", current);
for (int i = 0; i < n; i++) {
if (current->value == T)
return current->id;
if (graph[current][i] && !visited[i]) {
visited[i] = 1;
queue[rear++] = i;
}
}
}
return -1;
}
```
]
Expand Down Expand Up @@ -2058,7 +2062,7 @@ subtrees is at most 1.
#pseudocode-list(
title: smallcaps[given a graph $G$, a vertex $v$, and target value $T$:],
)[
+ $v$.discovered
+ $v$.discovered $:= "true"$
+ *if* $v = T$
+ *return* $v$
+ *for* all edges from $v$ to $w$ *in* $G$.adjacentEdges(v):
Expand All @@ -2075,17 +2079,19 @@ subtrees is at most 1.
=== Example in C (Recursive)

#align(horizon)[
#text(size: 15pt)[
#text(size: 14pt)[
```c
void dfs(int graph[][MAX], int current, int visited[], int n) {
int dfs(int graph[][MAX], int current, int visited[], int n) {
if (current->value == T)
return current->id;
visited[current] = 1;
printf("%d ", current);
for (int i = 0; i < n; i++) {
if (graph[current][i] && !visited[i]) {
dfs(graph, i, visited, n);
}
}
return -1;
}
```
]
Expand Down

0 comments on commit f1f4b2d

Please sign in to comment.