Skip to content

Commit f775ed2

Browse files
authored
Merge pull request #467 from wifiBlack/week-7-king-minimum-ordering
Week 7 king minimum ordering
2 parents dc0a6f5 + 1ab5641 commit f775ed2

File tree

4 files changed

+72
-20
lines changed

4 files changed

+72
-20
lines changed

docqueries/ordering/kingOrdering.pg

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,26 @@ SELECT * FROM pgr_kingOrdering(
55
'SELECT id, source, target, cost, reverse_cost FROM edges'
66
);
77
/* -- q2 */
8+
9+
CREATER TABLE kot AS (
10+
id SERIAL,
11+
source BIGINT,
12+
target BIGINT,
13+
cost BIGINT defult 1)
14+
;
15+
16+
INSERT INTO kot (source, target) VALUES
17+
(0, 3),
18+
(0, 5),
19+
(1, 2),
20+
(1, 4),
21+
(1, 6),
22+
(1, 9),
23+
(2, 3),
24+
(2, 4),
25+
(3, 5),
26+
(3, 8),
27+
(4, 6),
28+
(5, 6),
29+
(5, 7),
30+
(6, 7);

include/ordering/ordering.hpp

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3535
#include <limits>
3636
#include <iterator>
3737
#include <utility>
38+
#include <string>
3839

3940
#include <boost/config.hpp>
4041
#include <boost/graph/adjacency_list.hpp>
@@ -52,34 +53,66 @@ namespace pgrouting {
5253
template <class G>
5354
std::vector<int64_t>
5455
kingOrdering(G &graph) {
55-
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
56-
boost::property<boost::vertex_color_t, boost::default_color_type,
57-
boost::property<boost::vertex_degree_t, int>>>
58-
Graph;
59-
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
56+
using V = typename G::V;
57+
using B_G = typename G::B_G;
58+
using vertices_size_type = typename boost::graph_traits<B_G>::vertices_size_type;
6059

61-
std::vector<int64_t> results;
60+
size_t n = boost::num_vertices(graph.graph);
61+
std::vector<int64_t> results(n);
6262

6363
auto index_map = boost::get(boost::vertex_index, graph.graph);
64-
auto color_map = boost::get(boost::vertex_color, graph.graph);
64+
std::vector<vertices_size_type> colors(n);
65+
auto color_map = boost::make_iterator_property_map(colors.begin(), index_map);
6566
auto degree_map = boost::make_degree_map(graph.graph);
67+
std::vector<V> inv_perm(n);
6668

67-
std::vector<Vertex> inv_perm(boost::num_vertices(graph.graph));
6869
CHECK_FOR_INTERRUPTS();
69-
70-
boost::king_ordering(graph.graph, inv_perm.rbegin(), color_map, degree_map, index_map);
71-
for (std::vector<Vertex>::const_iterator i = inv_perm.begin(); i != inv_perm.end(); ++i) {
72-
auto seq = graph[*i].id;
73-
results.push_back({{seq}, {static_cast<int64_t>(graph.graph[*i].id)}});
74-
seq++;
70+
boost::king_ordering(graph.graph, inv_perm. rbegin(), color_map, degree_map, index_map);
71+
size_t j = 0;
72+
for (auto i = inv_perm.begin(); i != inv_perm.end(); ++i, ++j) {
73+
results[j] = static_cast<int64_t>(index_map[*i]);
7574
}
75+
76+
throw(std::to_string(results.size()));
7677
return results;
7778
}
7879

7980
template <class G>
80-
std::vector<std::vector<int64_t>>
81+
std::vector<int64_t>
8182
minDegreeOrdering(G &graph) {
83+
using B_G = typename G::B_G;
84+
using vertices_size_type = typename boost::graph_traits<B_G>::vertices_size_type;
85+
86+
size_t n = boost::num_vertices(graph.graph);
87+
std::vector<int64_t> results(n);
88+
89+
auto index_map = boost::get(boost::vertex_index, graph.graph);
90+
91+
std::vector<vertices_size_type> degree(n, 0);
92+
auto degree_map = boost::make_iterator_property_map(degree.begin(), index_map);
93+
94+
std::vector<vertices_size_type> supernode_sizes(n, 1);
95+
auto supernode_map = boost::make_iterator_property_map(supernode_sizes.begin(), index_map);
96+
97+
std::vector<vertices_size_type> perm(n);
98+
std::vector<vertices_size_type> inv_perm(n);
99+
82100
CHECK_FOR_INTERRUPTS();
101+
102+
boost::minimum_degree_ordering(
103+
graph.graph,
104+
degree_map,
105+
inv_perm.data(),
106+
perm.data(),
107+
supernode_map,
108+
0,
109+
index_map);
110+
111+
for (size_t i = 0; i < n; ++i) {
112+
results[i] = static_cast<int64_t>(inv_perm[i]);
113+
}
114+
115+
return results;
83116
}
84117

85118
} // namespace pgrouting

just_to_start_week6

Whitespace-only changes.

src/ordering/ordering_driver.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,22 @@ do_ordering(
8181
}
8282
hint = "";
8383

84-
std::vector<int64_t>results;
84+
std::vector<int64_t> results;
8585
UndirectedGraph undigraph;
8686
undigraph.insert_edges(edges);
87-
#if 0
8887
if (which == 0) {
8988
results = minDegreeOrdering(undigraph);
9089
} else if (which ==1) {
9190
results = kingOrdering(undigraph);
9291
}
93-
#endif
9492
auto count = results.size();
9593

96-
#if 0
9794
if (count == 0) {
9895
*err_msg = to_pg_msg("No results found \n");
9996
*return_tuples = NULL;
10097
*return_count = 0;
10198
return;
10299
}
103-
#endif
104100

105101
(*return_tuples) = pgr_alloc(count, (*return_tuples));
106102
for (size_t i = 0; i < count; i++) {

0 commit comments

Comments
 (0)