File: graph.h

package info (click to toggle)
indigo 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 48,936 kB
  • sloc: ansic: 332,816; cpp: 169,470; python: 20,033; java: 13,701; cs: 9,979; asm: 8,475; sql: 6,743; xml: 6,354; javascript: 1,245; sh: 555; php: 506; makefile: 54
file content (274 lines) | stat: -rw-r--r-- 7,606 bytes parent folder | download
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/****************************************************************************
 * Copyright (C) from 2009 to Present EPAM Systems.
 *
 * This file is part of Indigo toolkit.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ***************************************************************************/

#ifndef __graph_h__
#define __graph_h__

#include "base_cpp/array.h"
#include "base_cpp/list.h"
#include "base_cpp/non_copyable.h"
#include "base_cpp/obj_array.h"
#include "base_cpp/obj_pool.h"
#include "graph/filter.h"
#include "graph/graph_iterators.h"

#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable : 4251)
#endif

namespace indigo
{
    enum
    {
        FILTER_EQ,
        FILTER_NEQ,
        FILTER_MORE
    };

    enum
    {
        TOPOLOGY_RING = 1,
        TOPOLOGY_CHAIN = 2
    };

    struct VertexEdge
    {
        VertexEdge()
        {
        }
        VertexEdge(int vertex, int edge) : v(vertex), e(edge)
        {
        }

        int v;
        int e;
    };

    class DLLEXPORT Vertex
    {
    public:
        Vertex(Pool<List<VertexEdge>::Elem>& pool) : neighbors_list(pool)
        {
        }
        ~Vertex()
        {
        }

        List<VertexEdge> neighbors_list;

        NeighborsAuto neighbors() const;

        int neiBegin() const
        {
            return neighbors_list.begin();
        }
        int neiEnd() const
        {
            return neighbors_list.end();
        }
        int neiNext(int i) const
        {
            return neighbors_list.next(i);
        }

        int neiVertex(int i) const
        {
            return neighbors_list[i].v;
        }
        int neiEdge(int i) const
        {
            return neighbors_list[i].e;
        }

        int findNeiVertex(int idx) const;
        int findNeiEdge(int idx) const;

        int degree() const
        {
            return neighbors_list.size();
        }

    private:
        Vertex(const Vertex&); // no implicit copy
    };

    struct Edge
    {
        int beg;
        int end;

        int findOtherEnd(int i) const
        {
            if (i == beg)
                return end;
            if (i == end)
                return beg;
            return -1;
        }
    };

    class CycleBasis;

    class DLLEXPORT Graph : public NonCopyable
    {
    public:
        DECL_ERROR;

        explicit Graph();
        virtual ~Graph();

        VerticesAuto vertices();

        EdgesAuto edges();

        virtual void clear();

        const Vertex& getVertex(int idx) const;

        const Edge& getEdge(int idx) const;

        int vertexBegin() const
        {
            return _vertices->begin();
        }
        int vertexEnd() const
        {
            return _vertices->end();
        }
        int vertexNext(int i) const
        {
            return _vertices->next(i);
        }
        int vertexCount() const
        {
            return _vertices->size();
        }

        int edgeBegin() const
        {
            return _edges.begin();
        }
        int edgeEnd() const
        {
            return _edges.end();
        }
        int edgeNext(int i) const
        {
            return _edges.next(i);
        }
        int edgeCount() const
        {
            return _edges.size();
        }

        int addVertex();
        int addEdge(int beg, int end);

        int findEdgeIndex(int beg, int end) const;
        bool haveEdge(int beg, int end) const;
        bool hasEdge(int idx) const;
        bool hasVertex(int idx) const;
        int getEdgeEnd(int beg, int edge) const;

        void swapEdgeEnds(int edge_idx);
        void removeEdge(int idx);
        void removeVertex(int idx);
        void removeAllEdges();

        bool findPath(int from, int where, Array<int>& path_out) const;

        void makeSubgraph(const Graph& other, const Array<int>& vertices, Array<int>* vertex_mapping);
        void makeSubgraph(const Graph& other, const Array<int>& vertices, Array<int>* vertex_mapping, const Array<int>* edges, Array<int>* edge_mapping);
        void makeSubgraph(const Graph& other, const Filter& filter, Array<int>* mapping_out, Array<int>* inv_mapping);
        void cloneGraph(const Graph& other, Array<int>* mapping);

        void buildEdgeMapping(const Graph& other, Array<int>* mapping, Array<int>* edge_mapping);

        void mergeWith(const Graph& other, Array<int>* mapping);

        void makeEdgeSubgraph(const Graph& other, const Array<int>& vertices, const Array<int>& edges, Array<int>* v_mapping, Array<int>* e_mapping);

        int getEdgeTopology(int idx);
        void setEdgeTopology(int idx, int topology);
        void validateEdgeTopologies();

        static bool isConnected(Graph& graph);
        static bool isChain_AssumingConnected(const Graph& graph);
        static bool isTree(Graph& graph);
        static void filterVertices(const Graph& graph, const int* filter, int filter_type, int filter_value, Array<int>& result);
        static void filterEdges(const Graph& graph, const int* filter, int filter_type, int filter_value, Array<int>& result);
        static int findMappedEdge(const Graph& graph, const Graph& mapped_graph, int edge_idx, const int* mapping);

        int vertexCountSSSR(int idx);
        int vertexSmallestRingSize(int idx);
        bool vertexInRing(int idx);
        int edgeSmallestRingSize(int idx);

        List<int>& sssrEdges(int idx);
        List<int>& sssrVertices(int idx);
        int sssrCount();

        int vertexComponent(int v_idx);
        int countComponents();
        int countComponentVertices(int comp_idx);
        int countComponentEdges(int comp_idx);
        const Array<int>& getDecomposition();

    protected:
        void _mergeWithSubgraph(const Graph& other, const Array<int>& vertices, const Array<int>* edges, Array<int>* mapping, Array<int>* edge_mapping);

        Pool<List<VertexEdge>::Elem>* _neighbors_pool;
        ObjPool<Vertex>* _vertices;
        Pool<Edge> _edges;

        Array<int> _topology; // for each edge: TOPOLOGY_RING, TOPOLOGY_CHAIN, or -1 (not calculated)
        bool _topology_valid;

        Array<int> _v_smallest_ring_size, _e_smallest_ring_size;
        Array<int> _v_sssr_count;
        Pool<List<int>::Elem>* _sssr_pool;
        ObjArray<List<int>> _sssr_vertices;
        ObjArray<List<int>> _sssr_edges;
        bool _sssr_valid;

        Array<int> _component_numbers;
        Array<int> _component_vcount;
        Array<int> _component_ecount;
        bool _components_valid;
        int _components_count;

        void _calculateTopology();
        void _calculateSSSR();
        void _calculateSSSRInit();
        void _calculateSSSRByCycleBasis(CycleBasis& basis);
        void _calculateSSSRAddEdgesAndVertices(const Array<int>& cycle, List<int>& edges, List<int>& vertices);
        void _calculateComponents();

        // This is a bad hack for those who are too lazy to handle the mappings.
        // NEVER USE IT.
        void _cloneGraph_KeepIndices(const Graph& other);
    };

} // namespace indigo

#ifdef _WIN32
#pragma warning(pop)
#endif

#endif