Breath First Search (BFS)

Using a Queue

Pseudocode

// Global/class scope variables
n = number of nodes in the graph
g = adjacency list representing unweighted graph

// s = start node, e = end node, and 0 <= e, s < n
function bfs(s, e):

	// Do a BFS starting at node s
	prev = solve(s)

	// Return reconstructed path from s -> 2
	return reconstructPath(s, e, prev)

function solve(s):
	q = queue data structure with enqueue and dequeue
	q.enqueue(s)

	visited = [false, ..., false] // size n
	visited[s] = true

	prev = [null, ..., null] // size n
	while !q.isEmpty():
		node = q.dequeue()
		neighbors = g.get(node)

		for (next : neighbors):
			if !visited[next]:
				q.enqueue(next)
				visited[next] = true
				prev[next] = node

	return prev

function reconstructPath(s, e, prev):
	// Reconstruct path going backwards from e
	path = []
	for(at = 2; at != null; at = prev[at]):
		path.add(at)

	path.reverse()

	// If s and e are connected return the path
	if path[0] == s:
		return path
	return []

BFS Shortest Path on a Grid

Motivation

Graph Theory on Grids