forked from aryan-02/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Breadth_First_Search.cpp
38 lines (38 loc) · 1.45 KB
/
Breadth_First_Search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
//This is a graph traversal method that finds shortest path in an unweighted graph
//It traverses the graph in level order
void BreadthFirstSearch(int Source, vector<int> AdjacencyList[], int Nodes, int ShortestPath[Nodes]){
bool NodeVisited[Nodes];
for(int i = 0;i<Nodes;i++) NodeVisited[i] = false;
queue<pair<int, int>> Queue; //first element contains the node, second element contains shortest distance to the node
Queue.push(make_pair(Source, 0));
NodeVisited[Source] = true;
while(!Queue.empty()){
pair<int, int> CurrentNode = Queue.front(); Queue.pop();
ShortestPath[CurrentNode.first] = CurrentNode.second;
for(int Neighbour: AdjacencyList[CurrentNode.first]){
if(!NodeVisited[Neighbour]){
Queue.push(make_pair(Neighbour, CurrentNode.second + 1));
NodeVisited[Neighbour] = true;
}
}
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); //FAST IO
int Nodes, Edges, Source;
cin>>Nodes>>Edges>>Source;
vector<int> AdjacencyList[Nodes];//Adjacency List to store the Graph
int ShortestPath[Nodes];
//creating adjacency list
for(int i = 0;i<Edges;i++) {
int start, end;
cin >> start >> end;
AdjacencyList[start].push_back(end);
}
BreadthFirstSearch(Source, AdjacencyList, Nodes, ShortestPath);
}