-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph_cyclic_sorter.cpp
142 lines (125 loc) · 4.1 KB
/
graph_cyclic_sorter.cpp
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
/*This file is part of Circle Packings.
Circle Packings 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.
Circle Packings 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 Circle Packings. If not, see <http://www.gnu.org/licenses/>.*/
#include "graph_cyclic_sorter.hpp"
template <typename T> Graph_Cyclic_Sorter<T>::Graph_Cyclic_Sorter(Graph<T> *G)
{
G_ = G;
}
template <typename T> Graph<Empty> Graph_Cyclic_Sorter<T>::neighbors_subgraph(const unsigned int &index) const
{
std::vector<vertex_label> N = G_->get_neighbors_by_index(index);
Graph<Empty> G;
unsigned int i;
for(i=0; i<N.size(); i++)
{
G.add_isolated_vertex(N[i], Empty(), false);
}
unsigned int j;
for(i=0; i<N.size(); i++)
{
for(j=0; j<i; j++)
{
if (G_->are_neighbor_labels(N[i],N[j]))
{
G.add_edge_by_labels(N[i], N[j]);
}
}
}
return G;
}
template <typename T> bool Graph_Cyclic_Sorter<T>::cyclic_sort(Graph<Empty>& G)
{
if (G.nb_vertices() > 3)
{
unsigned int i=0;
while (i<G.nb_vertices() && G.get_neighbors_by_index(i).size() != 2)
{
i++;
}
if(i==G.nb_vertices())
{
return false;
}
vertex_label label = G.get_label_by_index(i);
vertex_label left_label = G.get_neighbors_by_index(i)[0];
vertex_label right_label = G.get_neighbors_by_index(i)[1];
G.remove_vertex_by_index(i);
if (G.are_neighbor_labels(left_label, right_label))
{
if(cyclic_sort(G))
{
reinsert_neighbor(label, left_label, right_label, G);
return true;
}
else
{
return false;
}
}
else
{
G.add_edge_by_labels(left_label, right_label);
if(cyclic_sort(G))
{
reinsert_neighbor(label, left_label, right_label,G);
G.remove_edge_by_labels(left_label, right_label);
return true;
}
else
{
return false;
}
}
}
return true;
}
template <typename T> void Graph_Cyclic_Sorter<T>::reinsert_neighbor(const vertex_label &label,const vertex_label &left_label, const vertex_label &right_label, Graph<Empty> & G)
{
unsigned int left_index = G.label_to_index(left_label);
unsigned int right_index = G.label_to_index(right_label);
unsigned int index_min = (left_index < right_index)? left_index : right_index;
unsigned int index_max = (left_index < right_index)? right_index : left_index;
unsigned int index = (index_min == 0 && index_max == G.nb_vertices()-1)? G.nb_vertices() : index_min + 1;
G.insert_isolated_vertex(index, label, Empty());
G.add_edge_by_labels(left_label, label);
G.add_edge_by_labels(label, right_label);
return;
}
template <typename T> bool Graph_Cyclic_Sorter<T>::neighbors_cyclic_sort(const unsigned int &index)
{
Graph<Empty> G = neighbors_subgraph(index);
if (cyclic_sort(G))
{
unsigned int j;
for (j=0; j<G.nb_vertices(); j++)
{
G_->set_neighbor_label_by_indices(index, j, G.get_label_by_index(j));
}
return true;
}
else
{
return false;
}
}
template <typename T> bool Graph_Cyclic_Sorter<T>::total_cyclic_sort()
{
unsigned int i=0, N = G_->nb_vertices();
while (i< N && neighbors_cyclic_sort(i))
{
i++;
}
return (i==N);
}
template class Graph_Cyclic_Sorter<Empty>;
template class Graph_Cyclic_Sorter<Point>;
template class Graph_Cyclic_Sorter<Circle>;