4. Single Source Shortest Path in Undirected Graph

Single Source Shortest Path in Undirected Graph

Given an Unweighted Graph and a source point, the task is to find the shortest path between the source point and every other point in the graph.

3 Methods to solve this-

  1. Using Bellman-Ford [ TC = O(VE) ]
  2. Using Dijkstra's Algorithm [ TC = O(E + Vlog(V)) ]
  3. Since the graph is Unweighted, we can solve this problem using Modified BFS. [ TC = O(V + E) ]
  • BFS Approach
// Time Complexity : O(V + E) 
// Auxiliary Space: O(V)

#include<bits/stdc++.h> 
using namespace std; 

void BFS(vector<int> adj[], int V, int s,int dist[]) { 
	bool visited[V]; 
	for(int i = 0; i < V; i++) 
		visited[i] = false; 

	queue<int>  q;
	
	visited[s] = true; 
	q.push(s); 

	while(q.empty()==false) 
	{ 
		int u = q.front(); 
		q.pop();
		 
		for(int v : adj[u]){
		    if(visited[v]==false){
		        dist[v]=dist[u]+1;
		        visited[v]=true;
		        q.push(v);
		    }
		} 
	} 
} 

void addEdge(vector<int> adj[], int u, int v){
    adj[u].push_back(v);
    adj[v].push_back(u);
}

int main() { 
	int V=4;
	vector<int> adj[V];
	addEdge(adj,0,1); 
	addEdge(adj,1,2); 
	addEdge(adj,2,3); 
	addEdge(adj,0,2); 
	addEdge(adj,1,3);
    int dist[V];
    for(int i=0;i<V;i++){
        dist[i]=INT_MAX;
    }
	dist[0]=0;
	BFS(adj,V,0,dist); 
    
    for(int i=0;i<V;i++){
        cout<<dist[i]<<" ";
    }

	return 0; 
} 

// O/P: 0 1 1 2 
Comments (6)