-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.hpp
70 lines (58 loc) · 1.37 KB
/
graph.hpp
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
/*
* Created by mbenlioglu on Oct 03, 2020.
*/
#pragma once
#include <string>
#include <fstream>
#include <chrono>
#include <algorithm>
#include <queue>
#include <unordered_set>
#include <vector>
/**
* Struct to hold performance metrics (Total MST weight and elapsed time to calculate)
*/
struct perfData {
int mstWeight;
double duration;
};
/**
* List of algorithms used in MST calculation
*/
enum Algorithm {
KRUSKAL, PRIM
};
template<Algorithm alg = KRUSKAL>
class Graph {
public:
/**
* Struct to hold weighted directed edge information
*/
struct Edge {
unsigned src, dest;
int weight;
};
private:
// Timing
using clock = std::chrono::high_resolution_clock;
using duration = std::chrono::duration<double>; // in seconds
unsigned numVertices{}, numEdges{};
int mstWeight{};
std::vector<Edge> edgeList;
std::vector<Edge> edgeHeap;
std::vector<std::vector<std::pair<unsigned, int> > > MST;
// Prim
std::vector<std::vector<Edge> > adjList;
std::vector<Edge> adjHeap;
// Functions
perfData kruskalMST();
perfData primsMST();
auto findPath(unsigned u, unsigned v);
public:
Graph() = default;
explicit Graph(std::string &filename);
explicit Graph(const char *filename);
perfData computeMST();
perfData recomputeMST(const Edge &e);
};
#include "graph.cpp"