-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
88 lines (73 loc) · 1.88 KB
/
Copy pathGraph.cpp
File metadata and controls
88 lines (73 loc) · 1.88 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
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class Graph {
private:
map<T, vector<T>> adjList;
int V;
public:
Graph() {}
Graph(int V) {
this->V = V;
}
void addEdge(T src, T dest, bool directed) {
if(directed)
{
this->adjList[src].push_back(dest);
}else
{
this->adjList[src].push_back(dest);
this->adjList[dest].push_back(src);
}
}
void bfs(T src) {
map<T, bool> visited;
queue<T> q;
q.push(src);
visited[src] = true;
while (!q.empty()) {
T current = q.front();
q.pop();
cout << current << " ";
for (T neighbor : adjList[current]) {
if (!visited[neighbor]) {
q.push(neighbor);
visited[neighbor] = true;
}
}
}
}
void dfsUtil(T src, map<T, bool>& visited) {
cout << src << " ";
visited[src] = true;
for (T neighbour : adjList[src]) {
if (!visited[neighbour]) {
dfsUtil(neighbour, visited);
}
}
}
void dfs(T src) {
map<T, bool> visited;
dfsUtil(src, visited);
}
};
Graph<string> creatLocalGraph(Graph<string> g)
{
g.addEdge("Chuadanga", "Meherpur", false);
g.addEdge("Chuadanga", "Alamdanga", false);
g.addEdge("Alamdanga", "Kushtia", false);
g.addEdge("Meherpur", "Kushtia", false);
g.addEdge("Kushtia", "Pabna", false);
g.addEdge("Pabna", "Rajshahi", false);
g.addEdge("Chuadanga", "Jhenaidah", false);
g.addEdge("Jhenaidah", "Kushtia", false);
g.addEdge("Jhenaidah", "Jashore", false);
g.addEdge("Jashore", "Khulna", false);
return g;
}
int main() {
Graph<string> g(10);
g = creatLocalGraph(g);
g.bfs("Khulna");
return 0;
}