Graph Algorithms: BFS, Dfs
Graph Algorithms: BFS, Dfs
BFS, DFS
a
2
c
b
3
d
4
a
2
c
b
3
d
4
3
4
1
i 2
3
4
1
0
0
2 3 4
1 0 0
0 0 1
0 1 0
1 0 0
A 2-dimensional
array for the digraph,
called the adjacency
matrix.
3
4
1
2
3
4
2
4
3
1
2
4
5
DFS(v)
for each neighboring nodes w of v do
if visited[w] is false then
set visited[w] to true; call DFS(w) // recursive call
The algorithms time
complexity is also O(n+e)
using the same reasoning as
in the BFS algorithm.
1
2
5
4