-
Notifications
You must be signed in to change notification settings - Fork 3
/
Graph.h
65 lines (50 loc) · 1.18 KB
/
Graph.h
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
#include <bits/stdc++.h>
using namespace std;
class Graph {
public:
int nodeCount, edgeCount;
int maxDegree;
int *adjacencyList, *adjacencyListPointers;
public:
int getNodeCount() {
return nodeCount;
}
int getEdgeCount() {
return edgeCount;
}
int getMaxDegree() {
return maxDegree;
}
void readGraph() {
int u, v;
cin >> nodeCount >> edgeCount;
// Use vector of vectors temporarily to input graph
vector<int> *adj = new vector<int>[nodeCount];
for (int i = 0; i < edgeCount; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
// Copy into compressed adjacency List
adjacencyListPointers = new int[nodeCount +1];
adjacencyList = new int[2 * edgeCount +1];
int pos = 0;
for(int i=0; i<nodeCount; i++) {
adjacencyListPointers[i] = pos;
for(int node : adj[i])
adjacencyList[pos++] = node;
}
adjacencyListPointers[nodeCount] = pos;
// Calculate max degree
maxDegree = INT_MIN;
for(int i=0; i<nodeCount; i++)
maxDegree = max(maxDegree, (int)adj[i].size());
delete[] adj;
}
int *getAdjacencyList(int node) {
return adjacencyList;
}
int *getAdjacencyListPointers(int node) {
return adjacencyListPointers;
}
};