Chi-Tech
chi_directed_graph.h
Go to the documentation of this file.
1#ifndef CHI_DIRECTED_GRAPH_H
2#define CHI_DIRECTED_GRAPH_H
3
5#include <stack>
6
7//###################################################################
8/**Simple implementation of a directed graph. This implementation was
9 * considered to serve more versatile strategies with regards to grid
10 * parallel partitioning.*/
12{
13public:
14
15 //============================================= Vertex accessor definition
16 /**Allows semi-sane access to vertices even if
17 * they are removed from the graph.*/
19 {
20 private:
21 std::vector<GraphVertex> vertices_;
22 std::vector<bool> vertex_valid_flags_;
23 public:
24 void AddVertex(size_t id, void* context);
25 void AddVertex(void* context);
26 void RemoveVertex(size_t v);
27
28 GraphVertex& operator[](size_t v);
29
30 //############################ iterator Class Definition
31 /**Internal iterator class for vertex accessor.*/
33 {
34 public:
37
38 iterator(VertexAccessor& in_block, size_t i) :
39 ref_block(in_block),
40 ref_element(i) {}
41
43 {
44 iterator i = *this;
45 bool stop = false;
46 do
47 {
49 if (ref_element>=ref_block.vertices_.size()) stop = true;
50 if (not stop)
52 } while (not stop);
53 return i;
54 }
56 {
57 bool stop = false;
58 do
59 {
61 if (ref_element>=ref_block.vertices_.size()) stop = true;
62 if (not stop)
64 } while (not stop);
65 return *this;
66 }
70 { return &(ref_block.vertices_[ref_element]); }
71 bool operator==(const iterator& rhs) const
72 { return ref_element == rhs.ref_element; }
73 bool operator!=(const iterator& rhs) const
74 { return ref_element != rhs.ref_element; }
75 };
76 //############################ End of iterator Class Definition
77
79 {
80 size_t count=0;
81 if (vertex_valid_flags_.empty()) return {*this, count};
82 if (vertex_valid_flags_[count]) return {*this, count};
83
84 //Get next valid or end
85 while (true)
86 {
87 if (count >= vertices_.size()) return {*this, count};
88 if (vertex_valid_flags_[count]) return {*this, count};
89 ++count;
90 }
91 }
92
93 iterator end(){return {*this, vertices_.size()};}
94
95 size_t size() {return vertices_.size();}
96
97 size_t GetNumValid()
98 {
99 size_t count=0;
100 for (bool val : vertex_valid_flags_)
101 if (val) ++count;
102
103 return count;
104 }
105
106 void clear() {vertices_.clear(); vertex_valid_flags_.clear();}
107 };
108 //============================================= End of Vertex accessor def
109
111
112 void AddVertex(size_t id, void* context = nullptr);
113 void AddVertex(void* context = nullptr);
114 void RemoveVertex(size_t v);
115 bool AddEdge(size_t from, size_t to, double weight=1.0);
116 void RemoveEdge(size_t from, size_t to);
117
118 size_t GetNumSinks()
119 {
120 size_t count=0;
121 for (auto& v : vertices)
122 if (v.ds_edge.empty() and not v.us_edge.empty())
123 ++count;
124 return count;
125 }
126
128 {
129 size_t count=0;
130 for (auto& v : vertices)
131 if (v.us_edge.empty() and not v.ds_edge.empty())
132 ++count;
133 return count;
134 }
135
136private:
137 void DFSAlgorithm(std::vector<size_t>& traversal,
138 std::vector<bool>& visited,
139 size_t cur_vid);
140
141 void SCCAlgorithm(size_t u, int& time,
142 std::vector<int>& disc,
143 std::vector<int>& low,
144 std::vector<bool>& on_stack,
145 std::stack<size_t>& stack,
146 std::vector<std::vector<size_t>>& SCCs);
147
148public:
149 std::vector<std::vector<size_t>>
151
152 std::vector<size_t> GenerateTopologicalSort();
153
154 std::vector<size_t> FindApproxMinimumFAS();
155
156 void PrintGraphviz(int location_mask=0);
157
158 void PrintSubGraphviz(const std::vector<int>& verts_to_print,
159 int location_mask=0);
160
161 std::vector<std::pair<size_t,size_t>> RemoveCyclicDependencies();
162
163 void Clear();
164
166}; //CHI_DIRECTED_GRAPH_H
167
168#endif
bool operator!=(const iterator &rhs) const
iterator(VertexAccessor &in_block, size_t i)
bool operator==(const iterator &rhs) const
void AddVertex(size_t id, void *context)
std::vector< GraphVertex > vertices_
std::vector< size_t > GenerateTopologicalSort()
void SCCAlgorithm(size_t u, int &time, std::vector< int > &disc, std::vector< int > &low, std::vector< bool > &on_stack, std::stack< size_t > &stack, std::vector< std::vector< size_t > > &SCCs)
std::vector< std::pair< size_t, size_t > > RemoveCyclicDependencies()
void AddVertex(size_t id, void *context=nullptr)
std::vector< size_t > FindApproxMinimumFAS()
VertexAccessor vertices
void PrintSubGraphviz(const std::vector< int > &verts_to_print, int location_mask=0)
std::vector< std::vector< size_t > > FindStronglyConnectedComponents()
void RemoveEdge(size_t from, size_t to)
void DFSAlgorithm(std::vector< size_t > &traversal, std::vector< bool > &visited, size_t cur_vid)
void RemoveVertex(size_t v)
bool AddEdge(size_t from, size_t to, double weight=1.0)
void PrintGraphviz(int location_mask=0)