-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUndirectedGraph.hpp
More file actions
118 lines (106 loc) · 3.54 KB
/
Copy pathUndirectedGraph.hpp
File metadata and controls
118 lines (106 loc) · 3.54 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef UNDIRECTEDGRAPH_HPP
#define UNDIRECTEDGRAPH_HPP
#include <string>
#include <unordered_map>
#include <vector>
#include "Vertex.hpp"
/**
* Implements an undirected graph. Any edge in the graph
* represents a bidirectional connection between two vertices.
*
* Implements methods for producing a minimum spanning tree of the
* graph, as well as calculating the total length of the shortest
* paths between each pair of vertices.
*/
class UndirectedGraph {
public:
/**
* Constructs an empty UndirectedGraph with no vertices and
* no edges.
*/
UndirectedGraph(){};
/**
* Destructs an UndirectedGraph.
*/
~UndirectedGraph();
/**
* Inserts an edge into the graph. If an edge already exists between
* the vertices, updates the cost and length of the edge to match the
* passed parameters.
*
* If either of the named vertices does not exist, it is created.
*/
void addEdge(const std::string &from, const std::string &to,
unsigned int cost, unsigned int length);
/**
* Returns the total cost of all edges in the graph.
*
* Since this graph is undirected, is calcualted as the cost
* of all Edges terminating at all Vertices, divided by 2.
*/
unsigned int totalEdgeCost() const;
/**
* Removes all edges from the graph except those necessary to
* form a minimum cost spanning tree of all vertices using Prim's
* algorithm.
*
* The graph must be in a state where such a spanning tree
* is possible. To call this method when a spanning tree is
* impossible is undefined behavior.
*/
UndirectedGraph minSpanningTree();
/**
* Determines the combined distance from the given Vertex to all
* other Vertices in the graph using Dijkstra's algorithm.
*
* Returns max possible distance if the given Vertex does not appear
* in the graph, or if any of the Vertices in the graph are not
* reachable from the given Vertex. Otherwise, returns the combined
* distance.
*/
unsigned int totalDistance(const std::string &from);
/**
* Determines the combined distance from all Vertices to all other
* Vertices in the graph.
*
* Returns max possible distance if the graph is not connected.
*/
unsigned int totalDistance();
private:
/**
* Map of vertex name to Vertex.
*/
std::unordered_map<std::string, Vertex*> vertices;
};
/**
* Comparison functor for use with Dijkstra's algorithm. Allows Vertices
* to be added to a priority queue more than once, with different weights.
*
* Each pair represents a Vertex and its weight when it was added to the
* queue. This guarantees that the weight used to order the Vertices in
* the queue never changes (a required invariant of a priority queue),
* even though the weight of the Vertex itself may change.
*
* Returns true if left's weight when it was inserted into the queue is
* higher than right's weight when it was inserted into the queue.
*/
class DijkstraVertexComparator {
public:
bool operator()(const std::pair<Vertex*, unsigned int> &left,
const std::pair<Vertex*, unsigned int> &right){
if( left.second > right.second )
return true;
else
return false;
}
};
class MSTComparator {
public:
bool operator()(Edge &left, Edge &right){
if( left.getCost() > right.getCost() )
return true;
else
return false;
}
};
#endif