blog/ib002/10-graphs/bfs-tree.md
Matej Focko 7427475022
chore: transfer all KBs to single one
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-11-05 15:25:15 +01:00

149 lines
3.1 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Distance boundaries from BFS tree on undirected graphs
description: |
Short explanation of distance boundaries deduced from a BFS tree.
---
## Introduction
As we have talked on the seminar, if we construct from some vertex $u$ BFS tree on an undirected graph, we can obtain:
- lower bound of length of the shortest path between 2 vertices from the _height difference_
- upper bound of length of the shortest path between 2 vertices from the _path through the root_
## Lower bound
Consider the following graph:
![BFS graph](/files/ib002/bfs-tree/bfs_graph.svg)
<details>
<summary>Source for the rendered graph</summary>
```dot showLineNumbers
graph {
a -- c
a -- e
c -- i
c -- b
e -- j
i -- d
b -- h
d -- h
h -- j
}
```
</details>
We run BFS from the vertex $a$ and obtain the following BFS tree:
![BFS tree](/files/ib002/bfs-tree/bfs_tree.svg)
<details>
<summary>Source for the BFS tree</summary>
```dot showLineNumbers
digraph {
a -> c
a -> e
c -> b
c -> i
e -> j
b -> h
i -> d
}
```
</details>
Let's consider pair of vertices $e$ and $h$. For them we can safely lay, from the BFS tree, following properties:
- lower bound: $2$
- upper bound: $4$
By having a look at the graph we started from, we can see that we have a path $e, j, h$ that has a length 2. Apart from that we can also notice there is another path from $e$ to $h$ and that is $e, a, c, i, d, h$. And that path has a length of $5$. Doesn't this break our statements at the beginning? (_I'm leaving that as an exercise ;)_)
## Proof by contradiction
Let's keep the same graph, but break the lower bound, i.e. I have gotten a lower bound $2$, but „there must be a shorter path“! ;)
Now the more important question, is there a shorter path in that graph? The answer is no, there's no shorter path than the one with length $2$. So what can we do about it? We'll add an edge to have a shorter path. Now we have gotten a lower bound of $2$, which means the only shorter path we can construct has $1$ edge and that is $e, h$ (no intermediary vertices). Let's do this!
![BFS tree](/files/ib002/bfs-tree/bfs_graph_with_additional_edge.svg)
<details>
<summary>Source for the BFS graph with an additional edge</summary>
```dot showLineNumbers
graph {
a -- c
a -- e
c -- i
c -- b
e -- j
e -- h
i -- d
b -- h
d -- h
h -- j
}
```
</details>
Okay, so we have a graph that breaks the rule we have laid. However, we need to run BFS to obtain the new BFS tree, since we have changed the graph.
:::tip
Do we need to run BFS after **every** change?
­I am leaving that as an exercise ;)
:::
![BFS tree](/files/ib002/bfs-tree/bfs_tree_with_additional_edge.svg)
<details>
<summary>Source for the BFS tree</summary>
```dot showLineNumbers
digraph {
a -> c
a -> e
c -> b
c -> i
e -> h
e -> j
i -> d
}
```
</details>
Oops, we have gotten a new BFS tree, that has a height difference of 1.
:::tip
Try to think about a way this can be generalized for shortening of minimal length 3 to minimal length 2 ;)
:::