๐ Graph Traversal โ Comprehensive Study Guide
**Graph Traversal** refers to **systematically visiting** all the vertices of a graph. It is an essential concept in graph algorithms used in **networking, artificial intelligence, and shortest path problems**.
โก Why is Graph Traversal Important?
- โ **Used in pathfinding (AI, Robotics, GPS Navigation).**
- ๐ **Essential for cycle detection & topological sorting.**
- ๐ก **Used in Web Crawling & Social Networks.**
- ๐ **Forms the foundation of advanced graph algorithms.**
๐ Types of Graph Traversal
๐ค **1. Breadth-First Search (BFS) โ Level Order Traversal**
Explores all neighbors before moving to deeper levels.
void BFS(vector>& adj, int start) {
queue q;
vector visited(adj.size(), false);
q.push(start);
visited[start] = true;
while (!q.empty()) {
int node = q.front();
q.pop();
cout << node << " ";
for (int neighbor : adj[node]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
q.push(neighbor);
}
}
}
}
๐ฅ **2. Depth-First Search (DFS) โ Recursive & Iterative**
Explores **as deep as possible** before backtracking.
๐ **Recursive DFS**
void DFS(vector>& adj, int node, vector& visited) {
visited[node] = true;
cout << node << " ";
for (int neighbor : adj[node]) {
if (!visited[neighbor]) {
DFS(adj, neighbor, visited);
}
}
}
๐ **Iterative DFS (Using Stack)**
void iterativeDFS(vector>& adj, int start) {
stack s;
vector visited(adj.size(), false);
s.push(start);
while (!s.empty()) {
int node = s.top();
s.pop();
if (!visited[node]) {
cout << node << " ";
visited[node] = true;
for (auto it = adj[node].rbegin(); it != adj[node].rend(); ++it) {
if (!visited[*it]) s.push(*it);
}
}
}
}
๐ Detecting Cycles in Graphs
๐ **Cycle Detection in Directed Graph (Using DFS)**
bool detectCycleDFS(int node, vector>& adj, vector& visited, vector& recStack) {
visited[node] = true;
recStack[node] = true;
for (int neighbor : adj[node]) {
if (!visited[neighbor] && detectCycleDFS(neighbor, adj, visited, recStack))
return true;
else if (recStack[neighbor])
return true;
}
recStack[node] = false;
return false;
}
๐ Topological Sorting (Ordering of Nodes)
๐ **1. Topological Sorting Using DFS**
Used in **task scheduling & dependency resolution**.
void topologicalSortDFS(int node, vector>& adj, vector& visited, stack& s) {
visited[node] = true;
for (int neighbor : adj[node])
if (!visited[neighbor])
topologicalSortDFS(neighbor, adj, visited, s);
s.push(node);
}
๐ **2. Topological Sorting Using Kahnโs Algorithm (BFS)**
void kahnTopologicalSort(vector>& adj, int V) {
vector inDegree(V, 0);
for (auto& edges : adj) for (int v : edges) inDegree[v]++;
queue q;
for (int i = 0; i < V; i++) if (inDegree[i] == 0) q.push(i);
while (!q.empty()) {
int node = q.front(); q.pop();
cout << node << " ";
for (int neighbor : adj[node]) if (--inDegree[neighbor] == 0) q.push(neighbor);
}
}
โณ Time Complexity Analysis
Algorithm | Time Complexity |
---|---|
BFS | O(V + E) |
DFS | O(V + E) |
Cycle Detection | O(V + E) |
Topological Sorting | O(V + E) |
๐ Real-World Applications of Graph Traversal
- ๐ก **Google Maps & GPS Navigation** โ Finding the shortest route.
- ๐ต๏ธ **Web Crawling (Google Search Engine)** โ Exploring all websites.
- ๐ค **AI & Robotics** โ Pathfinding in automated systems.
- ๐ฎ **Game Development** โ AI-based movement in strategy games.