examples/06_GRAPH_BASED_DSA/_images/depth-first-search/graph-dfs-step-0
examples/06_GRAPH_BASED_DSA/_images/depth-first-search/graph-dfs-step-1
examples/06_GRAPH_BASED_DSA/_images/depth-first-search/graph-dfs-step-2
examples/06_GRAPH_BASED_DSA/_images/depth-first-search/graph-dfs-step-3
examples/06_GRAPH_BASED_DSA/_images/depth-first-search/graph-dfs-step-4
examples/06_GRAPH_BASED_DSA/_images/depth-first-search/graph-dfs-step-5
init()
function, we run DFS function on every node
// ∈ = "is an element of"
// shorthand
DFS(G, u)
u.visited = true
For each v ∈ G.Adj[u]
if v.visited == false
DFS(G, v)
init() {
For each u ∈ G
u.visited = false
For each u ∈ G // is this necessary?
DFS(G, u)
}
// clearer wording
DepthFirstSearch(Graph, u)
u.visited = true
For each vertex ∈ Graph.Adj[u]
if vertex.visited == false
DepthFirstSearch(Graph, vertex)
init() {
For each u ∈ G
u.visited = false
For each u ∈ G
DepthFirstSearch(Graph, u)
}
NODE: code has been simplified in order to focus on algorithm rather than other details
see examples/06_GRAPH_BASED_DSA/depth-first-search
Python
def depth_first_search(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
print(start)
for next in graph[start] - visited:
depth_first_search(graph, next, visited)
return visited
graph = {
'0': set(['1', '2']),
'1': set(['0', '3', '4']),
'2': set(['0']),
'3': set(['1']),
'4': set(['2', '3']),
}
depth_first_search(graph, '0')
C++
#include <iostream>
#include <list>
using namespace std;
class Graph {
int numVertices;
list<int> *adjLists;
bool *visited;
public:
Graph(int V);
void addEdge(int src, int dest);
void DFS(int vertex);
};
// Initialize graph
Graph::Graph(int vertices) {
numVertices = vertices;
adjLists = new list<int>[vertices];
visited = new bool[vertices];
}
// Add edges
void Graph::addEdge(int src, int dest) {
adjLists[src].push_front(dest);
}
// DFS algorithm
void Graph::DFS(int vertex) {
visited[vertex] = true;
list<int> adjList = adjLists[vertex];
cout << vertex << " ";
list<int>::iterator i;
for (i = adjList.begin(); i != adjList.end(); ++i)
if (!visited[*i])
DFS(*i);
}
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 3);
g.DFS(2);
return 0;
}
Java
import java.util.*;
class Graph {
private LinkedList<Integer> adjLists[];
private boolean visited[];
// Graph creation
Graph(int vertices) {
adjLists = new LinkedList[vertices];
visited = new boolean[vertices];
for (int i = 0; i < vertices; i++) {
adjLists[i] = new LinkedList<Integer>();
}
}
// Add edges
void addEdge(int src, int dest) {
adjLists[src].add(dest);
}
// DFS Algorithm
void depthFirstSearch(int vertex) {
visited[vertex] = true;
System.out.print(vertex + " ");
Iterator<Integer> iterator = adjLists[vertex].listIterator();
while (iterator.hasNex()) {
int adjacent = iterator.next();
if (!visited[adjacent]) {
depthFirstSearch(adjacent);
}
}
}
public static void main(String args[]) {
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 3);
System.out.println("Following is Depth First Traversal for 2");
g.depthFirstSearch(2);
}
}
O(V + E)
, where V
is number of nodes and E
is number of edgesO(V)