-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphUsingMap.cpp
More file actions
57 lines (51 loc) · 1.31 KB
/
graphUsingMap.cpp
File metadata and controls
57 lines (51 loc) · 1.31 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
#include<bits/stdc++.h>
using namespace std;
void insNode(unordered_map<char, list<pair<char, int>>> &adj, int u, int v, int wg){
adj[u].push_back({v, wg});
}
void print(unordered_map<char, list<pair<char, int>>> adj){
for(auto &[f, s] : adj){
cout << f << "->";
for(auto &[ff, ss] : s){
cout << ff << " " << ss << ", ";
}
cout << "\n";
}
}
void bfsHelper(unordered_map<char, list<pair<char, int>>> adj, char start, map<char, bool> &vis){
queue<char> q;
q.push(start);
vis[start] = true;
while(!q.empty()){
char x = q.front();
q.pop();
cout << x << " ";
for(auto &[f, s] : adj[x]){
if(vis[f] != true){
q.push(f);
vis[f] = true;
}
}
}
}
void bfs(unordered_map<char, list<pair<char, int>>> adj){
map<char, bool> vis;
for(auto &[f, s] : adj){
if(vis[f] != true){
bfsHelper(adj, f, vis);
}
}
}
int main(){
unordered_map<char, list<pair<char, int>>> adj;
insNode(adj, 'A', 'B', 2);
insNode(adj, 'B', 'E', 3);
insNode(adj, 'E', 'D', 3);
insNode(adj, 'C', 'D', 2);
insNode(adj, 'D', 'B', 4);
insNode(adj, 'A', 'C', 4);
insNode(adj, 'Y', 'Z', 10);
// print(adj);
bfs(adj);
return 0;
}