-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
75 lines (74 loc) · 1.58 KB
/
Graph.cpp
File metadata and controls
75 lines (74 loc) · 1.58 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<bits/stdc++.h>
using namespace std;
template<typename T>
class Graph{
map<T,list<T> >adjList;
public:
Graph(){
}
void addEdge(T x,T y,bool bidir=true){
adjList[x].push_back(y);
if(bidir) adjList[y].push_back(x);
}
void printAdj(){
for(auto it: adjList){
cout<<it.first<<"->";
for(auto vertex: it.second){
cout<<vertex<<",";
}
cout<<endl;
}
}
void bfs(T src){
map<T,bool>visited;
queue<T>q;
q.push(src);
visited[src]=true;
while(!q.empty()){
T node=q.front();
q.pop();
cout<<node<<",";
for(auto neighbour: adjList[node]){
if(!visited[neighbour]) {
q.push(neighbour);
visited[neighbour]=true;}
}
}
}
void SSSP(T src){
map<T,int>dist;
for(auto it: adjList) dist[it.first]=INT_MAX;
dist[src]=0;
map<T,T> parent;
parent[src]=src;
queue<T>q;
q.push(src);
while(!q.empty()){
T node=q.front();
q.pop();
cout<<node<<",";
for(auto neighbour: adjList[node]){
if(dist[neighbour]==INT_MAX) {
q.push(neighbour);
dist[neighbour]=dist[node]+1;
parent[neighbour]=node;
}
}
}
for(auto it:adjList){
cout<<it.first<<"->"<<dist[it.first]<<endl;
}
}
};
int main(){
Graph<int> g;
g.addEdge(0,4);
g.addEdge(0,1);
g.addEdge(1,2);
g.addEdge(4,3);
g.addEdge(4,2);
g.addEdge(3,2);
g.addEdge(3,5);
g.SSSP(0);
return 0;
}