-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdgeWeightedGraph.h
More file actions
82 lines (72 loc) · 1.82 KB
/
EdgeWeightedGraph.h
File metadata and controls
82 lines (72 loc) · 1.82 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
#pragma once
#include <vector>
#include <stdexcept>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include "Edge.h"
class EdgeWeightedGraph
{
int vertices;
int edges;
using BagIterator = std::vector<Edge>::iterator;
using Bag = std::vector<Edge>;
std::vector<Bag> adjList;
void validateVertex(int v)
{
if (v < 0 || v >= vertices)
{
std::stringstream msg;
msg << "vertex " << v << " is not between 0 and " << (vertices-1);
throw std::out_of_range(msg.str());
}
}
public:
EdgeWeightedGraph(int V) : vertices(V), edges(0), adjList(V) {}
EdgeWeightedGraph(const EdgeWeightedGraph& g) : vertices(g.vertices), edges(g.edges), adjList(g.adjList) {}
int V() const { return vertices; }
int E() const { return edges; }
int addEdge(const Edge& e)
{
int v = e.either();
int w = e.other(v);
validateVertex(v);
validateVertex(w);
edges++;
adjList[v].push_back(e);
adjList[w].push_back(e);
}
Bag& adj(int v)
{
validateVertex(v);
return adjList[v];
}
BagIterator begin(int v)
{
validateVertex(v);
return adjList[v].begin();
}
BagIterator end(int v)
{
validateVertex(v);
return adjList[v].end();
}
int degree(int v)
{
return adjList[v].size();
}
std::string toString()
{
std::stringstream ss;
ss << vertices << " vertices, " << edges << " edges\n";
for (int v = 0; v < vertices; v++)
{
ss.width(2); ss << v << ":\n";
for (Edge& e : adjList[v])
{ ss.width(2); ss << e.toString() << std::endl; }
ss << std::endl;
}
return ss.str();
}
};