CSCE411 Exam 2 - Breadth First Search

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/6

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

7 Terms

1
New cards

BFS Description

Finds the shortest path from source node s to every other v ∈ V.

  1. Initialize all nodes to have NIL parent, ∞ dist, U status

  2. Initialize s to have 0 dist, D status

  3. Initialize a queue Q and add s to it

  4. While the queue is not empty, dequeue a vertex

  5. Iterate through all adjacent edges and set their status, parent, and distance

  6. Mark the node as explored and continue

2
New cards

BFS Pseudocode

for v ∈ V do
	v.parent = NIL
	v.dist = ∞
	v.status = U
end for
s.dist = 0
s.status = D

Initialize Q
Enqueue(s)
while |Q| > 0 do
	u = Dequeue(Q)
	N(u) = Adj[u]
	for v in N(u) do
		if v.status == U then
			v.status = D
			v.parent = i
			v.dist = u.dist + 1
			Enqueue(v)
		end if
	end for
	u.status = E
end while

3
New cards

BFS Analysis

Every edge and vertex is visited once, O(V+E)

4
New cards

BFS Benefits

BFS’s primary usecases are

  1. Finding shortest paths

  2. Finding connected components

  3. Creating a predecessor and a BFS tree

5
New cards

BFS and Shortest Paths

BFS tells us the minimum distance between s and every other node in the graph.

6
New cards

BFS and Connected Components

If G is undirected, BFS finds the connected component that s belongs to.

7
New cards

BFS and Trees

BFS will return a predecessor tree G = (V,E) such that

V = { s } ∪ { v ∈ V | v.parent ≠ NIL }
E = { (v.parent, v) : v ∈ V - {s} }

BFS will also return a BFS tree.