forked from Ritu-Kundu/Superbubbles
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGraph.hpp
171 lines (134 loc) · 6.14 KB
/
Graph.hpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
Superbubbles
Copyright (C) 2016 Ritu Kundu, Fatima Vayani, Manal Mohamed, Solon P. Pissis
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
/** Defines the class Graph.
* It represents a graph and also partitions the graph into suubgraphs.
*/
#ifndef GRAPH_HPP
#define GRAPH_HPP
#include "globalDefs.hpp"
#include "helperDefs.hpp"
namespace supbub{
/** Class Graph
* A graph is represented as follows:
* - A vertex is represented by its vertex-id which is an int.
* - Edges between vertices are represented using ajacency list.
* A vertex and vertex-id are inter-changably used.
* This class provides for the following:
* - Adding edges between vertices.
* - Printing the graph in adjacency-list format.
* - Support to query the number of vertices, edges; children and parents of a vertex.
* - Support to partition the graph into set of subgraphs
* -- This set comprises of the following subgraphs:
* --- one corresponding to each of the non-singleton Strongly Connected Component(scc)
* --- one which includes every singleton scc(vertex).
*/
/** type for vertex */
typedef int64_t int64_t;
/** type for list of ids of vertices */
typedef std::list<int64_t> int64_t_LIST;
/** type for iterator of list of vertices */
typedef int64_t_LIST::iterator int64_t_LIST_ITERATOR;
class Graph{
public:
/** Constructor
* @param n total number of vertices
*/
Graph(int64_t n);
/** Destructor */
~Graph();
/** Gives the number of vertices of graph. */
int64_t numVertices();
/** Gives the number of vertices of graph. */
int64_t numEdges();
/** Returns list of children of the vertex.
* Assumes v is valid.
* @param v given vertex-id.
* @return reference to list of ids of child vertices.
*/
int64_t_LIST& getChildren(int64_t v);
/** Returns list of parents of the vertex.
* Assumes v is valid.
* @param v given vertex-id.
* @return reference to list of ids of parent vertices
*
*/
int64_t_LIST& getParents(int64_t v);
/** Returns indegree of the vertex with given vertex-id (if v exists, -1 otherwise).
*/
int64_t getInDegree(int64_t v);
/** Returns outdegree of the vertex with given vertex-id (if v exists, -1 otherwise).
*/
int64_t getOutDegree(int64_t v);
/** Adds an edge
* @param u id of source vertex of edge
* @param v id of terminal vertex of edge
*/
void addEdge(int64_t u, int64_t v);
/** Fills the given array with ids of the subgraphs(coresponding to 'scc') which corresponding vertex belongs to.
*
* Each singleton vetex is added to the subgraph corresponding to id 0.
* Each non-singleton scc corresponds to a subgraph (with id starting from 1).
* Uses Tajan's algorithm to find scc.
* Assumes size of given array is at least equal to number of vertices in the graph.
* @param scc Pointer to the array to be filled in with subgraph-id of each vertex.
* @return number of subgraphs(one corresponding to all vertices in singleton sccs and rest corresponding to each of the non-singleton scc) .
*/
int64_t fillSCC(int64_t* scc);
/* Prints the graph in the form of adjacency list
*/
void printGraph();
//////////////////////// protected ////////////////////////
protected:
/** total number of vertices in the graph */
int64_t _numVertices;
/** total number of edges in the graph */
int64_t _numEdges;
/** adjacency list
* it is the pointer to an array where an element at index say 'u' is a pointer to a list of ids 'v' of all vertices such that there is an edge from the vertex with id u to the vertex with id v.
*/
int64_t_LIST* _adjList;
/** parent list
* it is the pointer to an array where an element at index say 'u' is a pointer to a list of ids 'v' of all vertices such that there is an edge from the vertex with id v to the vertex with id u.
*/
int64_t_LIST* _parentList;
/** array of indegree of each vertex
* it is the pointer to an array where an element at index say 'u' containes n where n is indegree of the vertexwith id u.
*/
int64_t* _inDegree;
/** array of outdegree of each vertex
* it is the pointer to an array where an element at index say 'u' containes n where n is outdegree of the vertex with id u.
*/
int64_t* _outDegree;
//////////////////////// private ////////////////////////
private:
/** Finds out scc(/subgraph) of given vertex recursively by using DFS.
* @param u id of vertex under consideration.
* @param disc Pointer to the array marking the discovery time of each vertex. (Initialize to 0).
* @param low Pointer to the array marking the lowest discovery time reachable from each vertex.
* @param st Pointer to the stack used by the algorithm (Initialize to an empty
stack).
* @param stacked Pointer to the array marking if a vetex id is stacked or not
* - stacked[i]=true if i was pushed into st, else false. (Initialize to false).
* @param tick Alias to the clock used for discovery times (Initialize to 0).
* @param currentScc Alias to the id of the current non-singleton scc being discovered (Initialize to 0)
.
* @param scc Pointer to the array to be filled in with subgraph-id of each vertex.
*/
void
findScc(int64_t u, int64_t* disc, int64_t* low, std::stack<int64_t> *st,
bool* stacked, int64_t& tick, int64_t& currentScc, int64_t* scc);
};
} // end namespace
#endif