Skip to main content

Graph Data Structure Traversal Methods

What is Graph Data Structure​

Graph Data Structure is a collection of vertices connected by edges. The vertices(V) are also referred as nodes and lines connecting any two nodes are called edges(E). A graph is denoted by G(V,E).

Local Image

Traversal Methods in Graph​

Two very popular traversal methods in Graph are:

  • BFS (Breadth First Search)
  • DFS (Depth First Search)

Breadth First Search traversal algorithm in graph is similar to level order traversal of tree. It starts at a specified vertex and visits all its neighbors before moving on to the next level of neighbors. BFS is commonly used in algorithms for pathfinding, connected components, and shortest path problems in graphs.

Algorithm:​

  1. Use adjacency list or adjacency matrix to store the graph, queue and a visiting array to keep the track of the visited vertex.
  2. Strat traversal from any vertex.
  3. Drop that vertex into queue
  4. Take out the vertex from queue and explore it.
  5. While exploring, go to all the adjacent vertices and add them to queue.
  6. Repeat step 4 and 5 unitl queue is not empty.

BFS Program​

/*write a cpp program to implement bfs algorithm*/
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
void bfs(int source, vector<vector<int>>&graph,int vertex)
{
queue<int>qu;
vector<int>visited(vertex,0);
qu.push(source);
visited[source]=1;
cout<<source<<" ";
while(!qu.empty())
{
int u=qu.front();
qu.pop();
for(auto it: graph[u])
{
if (visited[it]==0)
{
cout<<it<<" ";
visited[it]=1;
qu.push(it);
}
}
}

}
int main()
{
int vertex=7;
vector<vector<int>>adjlis(vertex); //using adjacent list method to represent graph
//there are 9 edges in the graph
adjlis[0].push_back(2);
adjlis[0].push_back(1);
adjlis[0].push_back(3);
adjlis[1].push_back(3);
adjlis[2].push_back(3);
adjlis[2].push_back(4);
adjlis[3].push_back(4);
adjlis[4].push_back(5);
adjlis[4].push_back(6);

bfs(0,adjlis,vertex);
return 0;
}

Output:

0 2 1 3 4 5 6 

Depth First Search traversal algorithm is similar to Pre-order traversal in binary tree. DFS use stack data structure here (or recursion as stack).he algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking.

Algorithm:​

  1. Use adjacency list or adjacency matrix to store the graph, queue and a visiting array to keep the track of the visited vertex.
  2. Start with any vertex and start exploring.
  3. After getting first element suspend the previous one in the stack and now start exploring this vertex.
  4. If cannot explore more go back to previous vertex (backtraking) from stack and explore that
  5. Repeat 3 and 4 until stack is empty.

DFS Program​

#include <iostream>
#include <vector>
using namespace std;

// Function for DFS traversal
void DFS(int u, vector<vector<int>>& G, vector<int>& visited) {
cout << u << " ";
visited[u] = 1;

for (int v = 0; v < G.size(); ++v) {
if (G[u][v] == 1 && visited[v] == 0) {
DFS(v, G, visited);
}
}
}

int main() {
int vertex=7;

//using adjacent matrix method to represent graph
vector<vector<int>> G{{0,1,1,1,0,0,0},
{1,0,0,1,1,0,0},
{1,0,0,1,0,0,0},
{1,1,1,0,1,0,0},
{0,1,0,1,0,1,1},
{0,0,0,0,1,0,0},
{0,0,0,0,1,0,0}};


vector<int> visited(vertex, 0); // Initialize visited array to 0

cout << "DFS traversal starting from vertex 0: ";
DFS(0, G, visited);

return 0;
}

Output:

DFS traversal starting from vertex 0: 0 1 3 2 4 5 6