mCRL2
Loading...
Searching...
No Matches
Graph_impl.h
Go to the documentation of this file.
1// Copyright (c) 2009-2013 University of Twente
2// Copyright (c) 2009-2013 Michael Weber <michaelw@cs.utwente.nl>
3// Copyright (c) 2009-2013 Maks Verver <maksverver@geocities.com>
4// Copyright (c) 2009-2013 Eindhoven University of Technology
5//
6// Distributed under the Boost Software License, Version 1.0.
7// (See accompanying file LICENSE_1_0.txt or copy at
8// http://www.boost.org/LICENSE_1_0.txt)
9
10// Don't include this directly; include Graph.h instead!
11
12#ifndef MCRL2_PG_GRAPH_IMPL_H
13#define MCRL2_PG_GRAPH_IMPL_H
14
15#include "mcrl2/pg/Graph.h"
16#include "mcrl2/pg/DenseMap.h"
17
18#include <unordered_map>
19#include <iterator>
20#include <cassert>
21
22// Note: code below is not currently used and has not been properly tested.
23#if 0
24EdgeIterator &EdgeIterator::operator=(const EdgeIterator &ei)
25{
26 g = ei.g;
27 v = ei.v;
28 e = ei.e;
29 return *this;
30}
31
32std::pair<verti, verti> EdgeIterator::operator*()
33{
34 return std::pair<verti, verti>(v, g->successors_[e]);
35}
36
37std::pair<verti, verti> EdgeIterator::operator++()
38{
39 if (++e < g->E_) while (g->successor_index_[v + 1] < e) ++v;
40 return **this;
41}
42
43std::pair<verti, verti> EdgeIterator::operator++(int)
44{
45 std::pair<verti, verti> result = **this;
46 ++*this;
47 return result;
48}
49#endif
50
51template<class ForwardIterator>
53 ForwardIterator vertices_begin,
54 ForwardIterator vertices_end,
55 bool proper,
57{
58 assert(vertices_begin <= vertices_end);
59
60 // FIXME: determine which cut-off value works best:
61 if (static_cast<verti>(std::distance(vertices_begin, vertices_end)) < graph.V()/3)
62 {
63 std::unordered_map<verti, verti> map;
64 return make_subgraph(graph, vertices_begin,
65 vertices_end, map, proper, edge_dir);
66 }
67 else
68 {
69 DenseMap<verti, verti> map(0, graph.V());
70 return make_subgraph(graph, vertices_begin, vertices_end,
71 map, proper, edge_dir);
72 }
73}
74
75template<class ForwardIterator, class VertexMapT>
77 ForwardIterator vertices_begin,
78 ForwardIterator vertices_end,
79 VertexMapT &vertex_map,
80 bool proper,
81 EdgeDirection edge_dir )
82{
83 assert(this != &graph);
84
85 verti num_vertices = 0;
86 edgei num_edges = 0;
87
88 // Create a map of old->new vertex indices, while counting vertices:
89 for (ForwardIterator it = vertices_begin; it != vertices_end; ++it)
90 {
91 vertex_map[*it] = num_vertices++;
92 }
93
94 // Count number of new edges:
95 for (ForwardIterator it = vertices_begin; it != vertices_end; ++it)
96 {
97 const_iterator a, b;
98 if (graph.edge_dir() & EDGE_SUCCESSOR)
99 {
100 a = graph.succ_begin(*it);
101 b = graph.succ_end(*it);
102 }
103 else
104 {
105 a = graph.pred_begin(*it);
106 b = graph.pred_end(*it);
107 }
108 while (a != b) num_edges += (vertex_map.find(*a++) != vertex_map.end());
109 }
110
111 // Allocate memory:
112 reset(num_vertices, num_edges, edge_dir ? edge_dir : graph.edge_dir());
113
115 {
116 // Assign new successors:
117 verti v = 0;
118 edgei e = 0;
119 for (ForwardIterator it = vertices_begin; it != vertices_end; ++it)
120 {
121 successor_index_[v++] = e;
122 verti *begin = &successors_[e];
123 for (const_iterator succ_it = graph.succ_begin(*it),
124 succ_end = graph.succ_end(*it);
125 succ_it != succ_end; ++succ_it)
126 {
127 typename VertexMapT::const_iterator it(vertex_map.find(*succ_it));
128 if (it != vertex_map.end()) successors_[e++] = (*it).second;
129 }
130 verti *end = &successors_[e];
131 if (!std::is_sorted(begin, end, std::less<verti>()))
132 {
133 std::sort(begin, end);
134 }
135 if (proper) assert(begin != end); /* proper parity game graph */
136 }
137 assert(v == V_ && e == E_);
138 successor_index_[v] = e;
139 }
140
142 {
143 // Assign new predecessors:
144 verti v = 0;
145 edgei e = 0;
146 for (ForwardIterator it = vertices_begin; it != vertices_end; ++it)
147 {
148 predecessor_index_[v++] = e;
149 verti *begin = &predecessors_[e];
150 for (const_iterator pred_it = graph.pred_begin(*it),
151 pred_end = graph.pred_end(*it);
152 pred_it != pred_end; ++pred_it)
153 {
154 typename VertexMapT::const_iterator it(vertex_map.find(*pred_it));
155 if (it != vertex_map.end()) predecessors_[e++] = it->second;
156 }
157 verti *end = &predecessors_[e];
158 if (!std::is_sorted(begin, end, std::less<verti>()))
159 {
160 std::sort(begin, end);
161 }
162 }
163 assert(v == V_ && e == E_);
164 predecessor_index_[v] = e;
165 }
166}
167
168#endif // MCRL2_PG_GRAPH_IMPL_H
std::size_t edgei
type used to number edges
Definition Graph.h:25
std::size_t verti
type used to number vertices
Definition Graph.h:24
verti * successors_
Definition Graph.h:278
const_iterator succ_end(verti v) const
Definition Graph.h:193
void reset(verti V, edgei E, EdgeDirection edge_dir)
Definition Graph.cpp:35
EdgeDirection edge_dir() const
Definition Graph.h:185
void make_subgraph(const StaticGraph &graph, ForwardIterator vertices_begin, ForwardIterator vertices_end, bool proper, EdgeDirection edge_dir=EDGE_NONE)
Definition Graph_impl.h:52
edgei * predecessor_index_
Definition Graph.h:283
verti V_
Definition Graph.h:271
const_iterator succ_begin(verti v) const
Definition Graph.h:188
edgei * successor_index_
Definition Graph.h:283
verti V() const
Definition Graph.h:179
verti * predecessors_
Definition Graph.h:278
const_iterator pred_begin(verti v) const
Definition Graph.h:198
EdgeDirection
Definition Graph.h:84
@ EDGE_SUCCESSOR
Definition Graph.h:86
@ EDGE_PREDECESSOR
Definition Graph.h:87
const_iterator pred_end(verti v) const
Definition Graph.h:203
EdgeDirection edge_dir_
Definition Graph.h:286
const verti * const_iterator
Definition Graph.h:66
edgei E_
Definition Graph.h:272