Breadth-First Search
Seen in CS341.
As long as the queue is not empty based on the order of queue. Take things out. and look at all the neighbours. If the neighbours is not visited, we enqueue it.
Time Complexity
Analysis:
- each vertex is enqueued at most once
- so each vertex is dequeued at least once
- so each adjacency list is read at most once (since we visit all the neighbours of a vertex only if it has not been visited)
For all , write number of neighbours of length of degree of .
cf. the adjacency array has cells (Handshaking Lemma)
Total:
Analysis: Before proving the various properties of breadth-first search, let’s take on the easier job of analyzing its running time on an input graph . We use aggregate analysis, as we saw in Section 16.1. After initialization, breadth-first search never whitens a vertex, and thus the test in line 13 ensures that each vertex is enqueued at most once, and hence dequeued at most once. The operations of enqueuing and dequeuing take time, and so the total time devoted to queue operations is z. Because the procedure scans the adjacency list of each vertex only when the vertex is dequeued, it scans each adjacency list at most once. Since the sum of the lengths of all adjacency lists is , the total time spent in scanning adjacency lists is . The overhead for initialization is , and thus the total running time of the BFS procedure is . Thus, breadth-first search runs in time linear in the size of the adjacency-list representation of .
Correctness
Claim
For all vertices , if is true at the end, there is a path in G.
to-understand Proof. Let be the vertices fort which visited is set to true, in this order. We prove: for all , there is a path by induction.
- OK for
- suppose true for . when is set to true, we are examining the neighbours of a certain , . by assumption, there is a path because {} is in , there is a path
- is true
- if is true, we will examine all neighbours of so at the end of Step 7, all will be true, for neighbour of . In particular, will be true
Lemma
For all vertices , there is a path in if and only if is true at the end.
Applications
- testing if there is a path
- testing if is connected in
Exercise
For a connected graph, .
Thu Jan 25, 2024
I visit some node , but why do we do that in BFS?
If I have a parent array, do we still need a visited array?
- No, we can just go check the parent array and check if it’s null or it visited the child.
In the textbook, they use predecessor to mean parent. It’s more general. I kinda got used to it.
Based on this algorithm, we can form a tree!
is the parent.
Example for the algorithm (i’ve recorded the whole process):
BFS Tree
BFS Tree
the BFS Tree is the subgraph made of
- all such that
- all edges , for as above (except )
Claim
The BFS tree is a tree.
Proof: by induction on the vertices for which is not NIL.
- when we set , only one vertex, no edge.
- suppose true before we set was in before, was not, so we add one vertex and one edge to , so remains a tree.
Remark: we make it a rooted tree by choosing as a root.
Shortest paths from the BFS tree
Sub-claim 1
The levels in the queue are non-decreasing
Proof: Exercise
Sub-claim 2
For all vertices if there is an edge , then .
…
I’m very confused: I have recordings.
Very important claim:
Claim
For all in :
- there is a path in iff there is a path in
- if so, the path in level[v]=dist(s,v)$
todo complete the notes, I did it in cs341 but forgot to update over here.