We assume that $|V| = n$ and $|E| = m$.
Graphs can be:

Assume that there are no duplicate/repeated edges between two nodes. Then the relationship between $m$ and $n$ are shown below.
| Graph type | Self-loops not allowed | Self-loops allowed |
|---|---|---|
| Directed graph | $m \in [0, n(n-1)]$ | $m \in [0, n^2]$ |
| Undirected graph | $m \in [0, n(n-1)/2]$ | $m \in [0, n(n+1)/2]$ |
Two major types of graph representations:
Space $= \Theta(|V| + |E|) = \Theta(n + m)$
Space $= \Theta(|V|^2) = \Theta(n^2)$
\begin{algorithm}
\begin{algorithmic}
\FUNCTION{GraphTraversal}{$G = (V,E)$}
\STATE \{ Initially all nodes are unmarked \}
\FOR{each vertex $u \in V$}
\STATE $u.mark \gets 0$
\STATE $u.parent \gets null$
\ENDFOR
\STATE{}
\STATE \{ Traverse each connected component if it is not traversed \}
\STATE Global $count \gets 0$
\FOR{each vertex $u \in V$}
\IF{$u.mark = 0$}
\STATE \CALL{GraphTraversal}{$u$}
\ENDIF
\ENDFOR
\ENDFUNCTION
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\begin{algorithmic}
\FUNCTION{GraphTraversal}{$u$}
\STATE Create a data structure $S$
\STATE $u.parent \gets null$;
$S.\text{Add}(u)$
\STATE{}
\WHILE{$S$ is not empty}
\STATE \{ Stage 1. Remove \}
\STATE $u \gets S.\text{Remove}()$
\IF{$u.mark = 0$}
\STATE \{ Stage 2. Mark \}
\STATE $count \gets count + 1$; $u.mark \gets count$
\STATE \{ Stage 3. Add children \}
\FOR{each neighbor $v \in \text{Neighbors}(u)$}
\STATE $v.parent \gets u$; $S.\text{Add}(v)$
\ENDFOR
\ENDIF
\ENDWHILE
\ENDFUNCTION
\end{algorithmic}
\end{algorithm}
| Data structure $S$ | Traversal algorithm | Add and Remove |
|---|---|---|
| Stack | Depth-first traversal | Push and Pop |
| Queue | Breadth-first traversal | Enqueue and Dequeue |
| Priority queue | Best-first traversal | Add and RemoveMin |
$\text{Adjacency list: }\langle\text{Time, Space}\rangle = \langle |V|+|E| \times T, |V|+|E| \rangle$
$\text{Adjacency matrix: }\langle\text{Time, Space}\rangle = \langle |V|^2 +|E| \times T, |V|^2 \rangle$
$\text{where, } T = \max(\text{time(Add)}, \text{time(Remove)})$
Depth-first: Exploring one path fully before trying alternatives.
Breadth-first: Checking all immediate options before going deeper.
| Feature | Depth-first | Breadth-first |
|---|---|---|
| Data structure | Stack | Queue |
| Recursive | ✓, ✗ | ✗ |
| Finding any path (not shortest) | ✓ | ✗ |
| Finding shortest path (unweighted) | ✗ | ✓ |
| Cycle detection | ✓ | ✓ |
| Topological sorting | ✓ | ✗ |
| Connected components | ✓ | ✓ |
| Backtracking | ✓ | ✗ |
DFS:
BFS:
\begin{algorithm}
\begin{algorithmic}
\FUNCTION{DFS}{$G = (V,E)$}
\FOR{each vertex $u \in V$}
\STATE $u.mark \gets 0$
\STATE $u.parent \gets null$
\ENDFOR
\STATE Global $count \gets 0$
\FOR{each vertex $u \in V$}
\IF{$u.mark = 0$}
\STATE \CALL{DFS}{$u$}
\ENDIF
\ENDFOR
\ENDFUNCTION
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\begin{algorithmic}
\FUNCTION{DFS}{$u$}
\STATE Create a stack $S$
\STATE $u.parent \gets null$; $S.\text{Push}(u)$
\WHILE{$S$ is not empty}
\STATE \{ Stage 1. Remove \}
\STATE $u \gets S.\text{Pop}()$
\IF{$u.mark = 0$}
\STATE \{ Stage 2. Mark \}
\STATE $count \gets count + 1$; $u.mark \gets count$
\STATE \{ Stage 3. Add children \}
\FOR{each neighbor $v \in \text{Neighbors}(u)$}
\STATE $v.parent \gets u$; $S.\text{Push}(v)$
\ENDFOR
\ENDIF
\ENDWHILE
\ENDFUNCTION
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\begin{algorithmic}
\FUNCTION{DFS}{$G = (V,E)$}
\FOR{each vertex $u \in V$}
\STATE $u.mark \gets 0$
\STATE $u.parent \gets null$
\ENDFOR
\STATE Global $count \gets 0$
\FOR{each vertex $u \in V$}
\IF{$u.mark = 0$}
\STATE \CALL{DFS}{$u$}
\ENDIF
\ENDFOR
\ENDFUNCTION
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\begin{algorithmic}
\FUNCTION{DFS}{$u$}
\STATE $count \gets count + 1$; $u.mark \gets count$
\FOR{each neighbor $v \in \text{Neighbors}(u)$}
\IF{$v.mark = 0$}
\STATE $v.parent \gets u$
\STATE \CALL{DFS}{$v$}
\ENDIF
\ENDFOR
\ENDFUNCTION
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\begin{algorithmic}
\FUNCTION{BFS}{$G = (V, E)$}
\FOR{each vertex $u \in V$}
\STATE $u.mark \gets 0$
\STATE $u.parent \gets null$
\ENDFOR
\STATE Global $count \gets 0$
\FOR{each vertex $u \in V$}
\IF{$u.mark = 0$}
\STATE \CALL{BFS}{$u$}
\ENDIF
\ENDFOR
\ENDFUNCTION
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\begin{algorithmic}
\FUNCTION{BFS}{$u$}
\STATE Create a queue $Q$
\STATE $u.parent \gets null$; $Q.\text{Enqueue}(u)$
\STATE{}
\WHILE{$Q$ is not empty}
\STATE \{ Stage 1. Remove \}
\STATE $u \gets Q.\text{Dequeue}()$
\IF{$u.mark = 0$}
\STATE \{ Stage 2. Mark \}
\STATE $count \gets count + 1$; $u.mark \gets count$
\STATE \{ Stage 3. Add children \}
\FOR{each neighbor $v \in \text{Neighbors}(u)$}
\STATE $v.parent \gets u$; $Q.\text{Enqueue}(v)$
\ENDFOR
\ENDIF
\ENDWHILE
\ENDFUNCTION
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\begin{algorithmic}
\FUNCTION{BFS}{$G = (V, E)$}
\FOR{each vertex $u \in V$}
\STATE $u.mark \gets 0$
\STATE $u.parent \gets null$
\ENDFOR
\STATE Global $count \gets 0$
\FOR{each vertex $u \in V$}
\IF{$u.mark = 0$}
\STATE \CALL{BFS}{$u$}
\ENDIF
\ENDFOR
\ENDFUNCTION
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\begin{algorithmic}
\FUNCTION{BFS}{$u$}
\STATE Create a queue $Q$
\STATE $count \gets count + 1$; $u.mark \gets count$
\STATE $u.parent \gets null$; $Q.\text{Enqueue}(u)$
\STATE{}
\WHILE{$Q$ is not empty}
\STATE \{ Stage 1. Remove \}
\STATE $u \gets Q.\text{Dequeue}()$
\STATE \{ Stage 2. Mark and add unmarked children \}
\FOR{each neighbor $v \in \text{Neighbors}(u)$}
\IF{$v.mark = 0$}
\STATE $count \gets count + 1$; $v.mark \gets count$
\STATE $v.parent \gets u$; $Q.\text{Enqueue}(v)$
\ENDIF
\ENDFOR
\ENDWHILE
\ENDFUNCTION
\end{algorithmic}
\end{algorithm}