-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
91 lines (74 loc) · 2.29 KB
/
graph.cpp
File metadata and controls
91 lines (74 loc) · 2.29 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
89
90
91
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
struct hash_pair {
int hash64shift(int key) const {
key = (~key) + (key << 18); // key = (key << 18) - key - 1;
key = key ^ (key >> 31);
key = key * 21; // key = (key + (key << 2)) + (key << 4);
key = key ^ (key >> 11);
key = key + (key << 6);
key = key ^ (key >> 22);
return (int)key;
}
template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
return hash64shift(hash64shift(hash1) ^ hash64shift(hash2));
}
};
using vertex_t = int;
using adjacency_list_t = unordered_map<vertex_t, vector<vertex_t>>;
using weight_t = int;
using edge_weight_t =
unordered_map<pair<vertex_t, vertex_t>, weight_t, hash_pair>;
class Graph {
public:
adjacency_list_t adjacencyList;
bool weighted = false;
int vertices = 0;
edge_weight_t edgeWeight;
Graph() = delete;
Graph(adjacency_list_t al, int s, bool w)
: adjacencyList(move(al)), vertices(s), weighted(w) {
// find total number of edges
// for(auto&& l: adjacencyList) edges += l.second.size();
if (!weighted)
return;
for (auto &&l : adjacencyList) {
for (auto v : l.second) {
int wt;
cout << "weight(" << l.first << ", " << v << ") = ";
cin >> wt;
edgeWeight[make_pair(l.first, v)] = wt;
}
}
}
Graph(adjacency_list_t al, edge_weight_t ew, int s, bool w)
: adjacencyList(move(al)), edgeWeight(move(ew)), vertices(s),
weighted(w) {}
Graph(int s, bool w) : vertices(s), weighted(w) {}
vector<vertex_t> neighbours(vertex_t vertex) { return adjacencyList[vertex]; }
void add_edge(int v1, int v2, int weight = 1, bool directed = true) {
if (adjacencyList.find(v1) == adjacencyList.end()) {
adjacencyList[v1] = {v2};
if (!directed)
add_edge(v2, v1, weight);
} else {
adjacencyList[v1].push_back(v2);
if (!directed)
add_edge(v2, v1, weight);
}
if (weighted)
edgeWeight[make_pair(v1, v2)] = weight;
}
weight_t edge_weight(vertex_t v1, vertex_t v2) {
return edgeWeight[make_pair(v1, v2)];
}
};