-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.cpp
114 lines (97 loc) · 3.63 KB
/
graph.cpp
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
// Copyright 2021 András Mihálykó MIT License
#include <iostream>
#include "graph.h"
void SimpleGraph::readFromInput() {
std::cin >> numberOfVertices;
int numbOfEdges;
std::cin >> numbOfEdges;
for (int i = 0; i < numbOfEdges; i++) {
int a, b;
std::cin >> a >> b;
if (a == b) {
std::cerr << "No loop edges are allowed." << std::endl;
throw 12;
}
addEdge(a, b);
}
}
void DirectedHyperGraph::print() const {
std::cout << "Number of vertices: " << getNumberOfVertices() << std::endl;
std::cout << "Number of hyperedges: " << getNumberOfEdges() << std::endl;
std::cout << "Edges:" << std::endl;
for (auto const& e : directedHyperEdges) {
e->print();
}
}
void DirectedHyperGraph::printUndirectedHyperedges() const {
std::cout << "Number of vertices: " << getNumberOfVertices() << std::endl;
std::cout << "Number of hyperedges: " << getNumberOfEdges() << std::endl;
std::cout << "Undirected Hyper Edges:" << std::endl;
for (auto const& e : undirectedHyperEdges) {
if (e->isStillExistsing())
e->print();
}
}
void DirectedHyperEdge::changeUnderlyingEdge(std::shared_ptr<HyperEdge> newHyperEdge) {
hyperEdge->deleteHyperedge();
hyperEdge = newHyperEdge;
}
std::shared_ptr<Vertex> DirectedHyperGraph::getVertex(int id) const {
if (vertices.size() > id) {
return vertices[id];
}
throw std::runtime_error("No vertex found with index " + std::to_string(id));
return std::shared_ptr<Vertex>();
}
int DirectedHyperGraph::getInDegree(int id) const {
if (inDegree.size() > id) {
return inDegree[id];
}
throw std::runtime_error("No vertex found with index " + std::to_string(id));
return -1;
}
void DirectedHyperGraph::addHyperEdge(std::shared_ptr<HyperEdge> edge, const std::shared_ptr<Vertex>& head) {
/*
Adds undirected hyperedge every time if it encounters for every "inMcomp"
*/
undirectedHyperEdges.push_back(edge);
directedHyperEdges.push_back(std::make_shared<DirectedHyperEdge>(head, edge));
headOf(head)->push_front(directedHyperEdges.back());
increaseInDegree(head);
}
void DirectedHyperGraph::addDirEdge(const std::shared_ptr<Vertex>& head, const std::shared_ptr<Vertex>& tail) {
/*
Adds one directed edge as a hyperedge. No new non-trivial M-component appears by this
*/
std::vector<std::shared_ptr<Vertex> > vertices = {head, tail};
std::shared_ptr<HyperEdge> edge = std::make_shared<HyperEdge>(vertices, undirectedHyperEdges.size());
undirectedHyperEdges.push_back(edge);
directedHyperEdges.push_back(std::make_shared<DirectedHyperEdge>(head, edge));
headOf(head)->push_front(directedHyperEdges.back());
increaseInDegree(head);
}
void DirectedHyperGraph::changeDirection(DirectedHyperEdge& edge, const std::shared_ptr<Vertex>& to) {
edge.setHead(to);
increaseInDegree(to);
}
void DirectedHyperGraph::readFromInput() {
int m;
std::cout << "Number of edges" << std::endl;
std::cin >> m;
std::cout << "Edges (head first, finish with -1)" << std::endl;
for (int i = 0; i < m; i++) {
std::vector<std::shared_ptr<Vertex> > edgeVertices;
int h = -1;
int v;
std::cin >> v;
while (v >= 0 && v < getNumberOfVertices()) {
edgeVertices.push_back(vertices[v]);
if (h == -1) // Hack to store head
h = v;
std::cin >> v;
}
std::shared_ptr<HyperEdge> newHyperEgde =
std::make_shared<HyperEdge>(edgeVertices, undirectedHyperEdges.size());
addHyperEdge(newHyperEgde, vertices[h]);
}
}