-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.h
155 lines (124 loc) · 4.89 KB
/
graph.h
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef GRAPH_H
#define GRAPH_H
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <stack>
using namespace std;
struct Edge {
int source;
int target;
Edge() {source = -1; target = -1;}
};
bool operator==(const Edge& e1, const Edge& e2);
bool operator<(const Edge& e1, const Edge& e2);
bool operator>(const Edge& e1, const Edge& e2);
bool operator<=(const Edge& e1, const Edge& e2);
bool operator>=(const Edge& e1, const Edge& e2);
class Graph {
public:
static string TYPE_ARCS_LIST;
static string TYPE_ADJACENCY_LIST;
Graph(int maxSize);
Graph(const string& filename, const string& type);
~Graph();
int numVertices() const;
int numEdges() const;
bool hasVertex(int vertex) const;
bool hasEdge(int source, int target) const;
set<int> const& vertices() const;
vector<Edge> edges() const;
vector<int> const& incomingNeighbors(int vertex) const;
vector<int> const& outgoingNeighbors(int vertex) const;
void print(bool edges) const;
void print() const;
void addVertex(int vertex);
void addEdge(int source, int target);
void deleteVertex(int vertex);
void deleteVertices(const vector<int>& vertices);
void deleteEdge(int source, int target);
void deleteEdges(const vector<Edge>& edges);
void mergeVertex(int vertex);
void mergeVertices(vector<int> vertices);
void mergeVertices(int vertex1, int vertex2);
Graph subgraph(vector<int> vs) const;
int inDegree(int vertex) const;
int outDegree(int vertex) const;
int degree(int vertex) const;
int minDegreeVertex() const;
int maxDegreeVertex() const;
bool isSource(int vertex) const;
bool isSink(int vertex) const;
bool hasLoop(int vertex) const;
vector<int> sources() const;
vector<int> sinks() const;
vector<int> loops() const;
vector< vector<int> > stronglyConnectedComponents();
vector<int> vertexToStronglyConnectedComponentNumber();
Graph groundingKernel() const;
vector<int> in0();
vector<int> out0();
vector<int> loop();
vector<int> in1();
vector<int> out1();
/****************
* Operator PIE *
****************/
vector<Edge> acyclicEdges();
vector<Edge> piEdges();
vector<Edge> pseudoAcyclicEdges();
vector<Edge> pie();
/*****************
* Operator CORE *
*****************/
bool isPiVertex(int vertex);
vector<int> piVertices();
bool isClique(const vector<int>& vs) const;
vector<int> core();
/*****************
* Operator DOME *
*****************/
vector<int> piPredecessors(int vertex) const;
vector<int> piSuccessors(int vertex) const;
vector<int> nonPiPredecessors(int vertex) const;
vector<int> nonPiSuccessors(int vertex) const;
bool isDominated(int source, int target) const;
vector<Edge> dominatedEdges() const;
vector<Edge> dome();
vector<int> reduce(bool applyIn0, bool applyOut0, bool applyLoop,
bool applyIn1, bool applyOut1, bool applyPie,
bool applyCore, bool applyDome, bool verbose);
vector<int> reduce(bool verbose=false);
vector<int> shortestCycle() const;
bool isAcyclic();
bool isFeedbackVertexSet(const vector<int>& fvs) const;
int upperBoundValue(bool verbose=false) const;
vector<int> upperBoundSolution(bool verbose=false) const;
int lowerBoundValue(bool verbose=false) const;
vector<int> minimumFeedbackVertexSet(bool verbose=false) const;
private:
vector< vector<int> > _outgoingNeighbors;
vector< vector<int> > _incomingNeighbors;
set<int> _vertices;
vector<bool> _hasVertex;
int _maxVertices;
int _numVertices;
int _numEdges;
void GraphFromAdjacencyList(const string& filename);
void GraphFromArcsList(const string& filename);
void _init(int maxSize);
vector<int> _shortestCycle(int vertex, int maxLength);
int _tarjanCurrentIndex;
vector<int> _tarjanIndex;
vector<int> _tarjanAncestor;
stack<int> _tarjanStack;
vector<bool> _tarjanInStack;
int _currentComponent;
vector<int> _sccsByNum;
vector< vector<int> > _sccs;
void _tarjan(int vertex, bool byNumber);
vector<int> _mfvs(vector<int> solution, vector<int> bestSolution, int lowerBound,
int level, bool reducible, bool verbose);
};
#endif