-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathWeighted_graph.cpp
57 lines (42 loc) · 963 Bytes
/
Weighted_graph.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
Implementation of a weighted graph using adjacency list.
Note : each vertex in the graph is generic.
*/
#include<iostream>
#include<list>
#include<map>
using namespace std;
template <typename T>
class Graph {
map<T, list<pair<T, int>>> neighbourMap;
bool directed;
public:
Graph(bool directed=false) {
this->directed = directed;
}
void addEdge(T u, T v, int w) {
neighbourMap[u].push_back({v, w});
if(!directed) neighbourMap[v].push_back({u, w});
}
void print() {
for(pair<T, list<pair<T, int>>> p : neighbourMap) {
cout << p.first << " : ";
for(pair<T, int> neighbour : p.second) {
cout << "(" << neighbour.first << ", " << neighbour.second << ") ";
}
cout << endl;
}
}
};
int main() {
Graph<char> g(true);
g.addEdge('A', 'B', 2);
g.addEdge('A', 'C', 1);
g.addEdge('B', 'C', 4);
g.addEdge('B', 'D', 5);
g.addEdge('C', 'D', 3);
g.addEdge('C', 'E', 0);
g.addEdge('D', 'E', 2);
g.print();
return 0;
}