-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
76 lines (68 loc) · 2.35 KB
/
graph.cpp
File metadata and controls
76 lines (68 loc) · 2.35 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
#include "graph.hpp"
#include <sstream>
#include <iomanip>
namespace Routing
{
//----------------------------------------------------
void Graph::addEdge(const uint32_t src, const uint32_t dest)
{
this->vertices_.insert(src);
this->vertices_.insert(dest);
this->edges_[src].insert(Endpoint(dest));
}
//----------------------------------------------------
void Graph::removeEdge(const uint32_t src, const uint32_t dest)
{
this->edges_[src].erase(Endpoint(dest));
}
//----------------------------------------------------
void Graph::removeVertex(const uint32_t vertex)
{
this->vertices_.erase(vertex);
}
//----------------------------------------------------
std::string Graph::toString() const
{
std::ostringstream oss;
// Determine appropriate width to separate numbers
std::uint32_t w = 1, temp = this->edges_.size();
while(temp)
{
temp /= 10; //Remove next significant digit
++w; //Keep track of digits removed
}
// Creating an adjacency matrix
//
// First row: all vertices
auto it = this->vertices_.begin();
// Leave initial row blank so we get a conventional matrix representation
oss << std::setw(w*2) << *it;
for(++it; it != this->vertices_.end(); ++it)
{
oss << std::setw(w) << *it;
}
// Subsequent rows: each vertex and its adjacency to other vertices (1 if adjacent; 0 if not)
for(auto it = this->vertices_.begin(); it != this->vertices_.end(); ++it)
{
oss << "\n" << std::setw(w) << *it;
for(auto it2 = this->vertices_.begin(); it2 != this->vertices_.end(); ++it2)
{
// Find source vertex in edge map
auto srcIt = this->edges_.find(*it);
if(srcIt != this->edges_.end())
{
// Found it. Look for an edge to the adjacent vertex
if(srcIt->second.find(*it2) != srcIt->second.end())
{
oss << std::setw(w) << "1";
}
else
{
oss << std::setw(w) << "0";
}
}
}
}
return oss.str();
}
}