


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth analysis of depth first search (dfs) and breadth first search (bfs) algorithms using stacks and queues. The visitation problem, depth first search requirements, depth first search using a stack, breadth first search requirements, and breadth first search using a queue. The document also includes questions related to dfs and bfs algorithms.
Typology: Summaries
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Given a directed graph G and a start vertex s ∈ V [G], a visitation algorithm for (G, s) is an algorithm which visits all vertices of G that are reachable from s. In principle, the visitation of a vertex u could consist of any kind of action (such as computing the credit score of the customer represented by the vertex u).
Every vertex reachable from s must be visited exactly once. In order to enforce this rule, we use a Boolean array visited on V [G]. Initially, visited[u] is false for all u ∈ V [G], and at the end of the algorithm, visited[u] is true for all vertices reachable from s, and false for all vertices unreachable from s. The actual act of visiting a vertex u is represented by the assignment statement visited[u] := true.
Let n be the number of vertices of G and m the number of edges. We write Adj[v] to be the set of out-neighbors of v. A vertex u is an out-neighbor of v if there is a directed edge from v to u. We write In[v] to be the set of in-neighbors of v. A vertex u is an in-neighbor of v if there is a directed edge from u to v, that is, if v is an out-neighbor of u.
We say that a visitation algorithm is a depth first search or DFS, algorithm, if vertices are visited in depth first order. The requirements of depth first search are as follows:
For a given directed graph and start vertex s, the order of DFS visitation is not necessarily unique.
All DFS algorithms, as far as I know, use a stack. It is possible to write a DFS algorithm without an explicit stack data structure by using recursion, but that’s “cheating,” since you are actually
making use of the run-time stack. Since the purpose of this section is to show how to use a stack for DFS search, we will not allow any of our algorithms to use recursion.
Each of our algorithms uses a stack S of vertices. A vertex can be pushed onto the stack more than once, in fact, the same vertex could be on the stack in several places simultaneously. Being pushed onto the stack does not necessarily imply visitation; we must indicate the visitation of v by explicitly writing visited[v] := true, and that assignment may not occur more than once.
Here is a stack algorithm for depth first search, which we call DFS-A(G, s).
DFS-A(G,s) for all v in V[G] do visited[v] := false end for S := EmptyStack Push(S,s) while not Empty(S) do u := Pop(S) if not visited[u] then visted[u] := true for all w in Adj[u] do if not visited[w] then Push(S,w) end if end for end if end while
DFS-A can have multiple copies on the stack at the same time. However, the total number of iterations of the innermost loop of DFS-A cannot exceed the number of edges of G, and thus the size of S cannot exceed m. The running time of DFS-A is O(n + m).
It is possible to write a DFS algorithm where no vertex is ever in the stack in more than one place, but it is somewhat trickier. We give such an algorithm, DFS-B, below.
DFS-B(G,s) for all v in V[G] do visited[v] := false end for S := EmptyStack visited[s] := true Push(S,s) while not Empty(S) do u := Pop(S) if there is at least one unvisited vertex in Adj[u] then Pick w to be any unvisited vertex in Adj[u] Push(S,u) visited[w] := true Push(S,w)
Enqueue(Q,w) end if end for end if end while
In BFS-A, a vertex could be in Q in several places simultaneously. The number of iterations of the innermost loop of BFS-A does not exceed m, and therefore the size of Q never exceeds m The running time of BFS-A is O(n + m).
On the other hand, algorithm BFS-B never has multiple copies of a vertex in Q.
BFS-B(G,s) for all v in V[G] do visited[v] := false end for Q := EmptyQueue visited[s] := true Enqueue(Q,s) while not Empty(Q) do u := Dequeue(Q) for all w in Adj[u] do if not visited[w] then visited[w] := true Enqueue(Q,w) end if end while
In BFS-B, the size of Q is never larger than n. The running time of BFS-B is O(n + m).