Skip to content

Commit ae2b187

Browse files
authored
Merge pull request #480 from wifiBlack/week-11-king-minimum-ordering
Week 11 king minimum ordering
2 parents e4ca583 + 9f177e1 commit ae2b187

6 files changed

Lines changed: 105 additions & 50 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ notUsed
1313
.vscode
1414
taptest.sh
1515
run.sh
16+
get_signatures.sh
17+
ordering_example

docqueries/ordering/kingOrdering.result

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ INSERT INTO additional_sample_1 (source, target, cost, reverse_cost) VALUES
6363
(3, 1, 1, 1);
6464
INSERT 0 24
6565
SELECT * FROM pgr_kingOrdering(
66-
'SELECT id, source, target, cost, reverse_cost FROM additional_sample_1'
67-
);
68-
seq | node
66+
'SELECT id, source, target, cost, reverse_cost FROM additional_sample_1'
67+
);
68+
seq | node
6969
-----+------
7070
1 | 0
7171
2 | 4

include/ordering/kingOrdering.hpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*PGR-GNU*****************************************************************
2+
File: kingOrdering.hpp
3+
4+
Generated with Template by:
5+
Copyright (c) 2015 pgRouting developers
6+
Mail: project@pgrouting.org
7+
8+
Developer:
9+
Copyright (c) 2025 Fan Wu
10+
Mail: wifiblack0131 at gmail.com
11+
12+
------
13+
14+
This program is free software; you can redistribute it and/or modify
15+
it under the terms of the GNU General Public License as published by
16+
the Free Software Foundation; either version 2 of the License, or
17+
(at your option) any later version.
18+
19+
This program is distributed in the hope that it will be useful,
20+
but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
GNU General Public License for more details.
23+
24+
You should have received a copy of the GNU General Public License
25+
along with this program; if not, write to the Free Software
26+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27+
28+
********************************************************************PGR-GNU*/
29+
30+
#ifndef INCLUDE_ORDERING_KINGORDERING_HPP_
31+
#define INCLUDE_ORDERING_KINGORDERING_HPP_
32+
#pragma once
33+
34+
#include <vector>
35+
#include <limits>
36+
#include <iterator>
37+
#include <utility>
38+
#include <string>
39+
40+
#include <boost/config.hpp>
41+
#include <boost/graph/adjacency_list.hpp>
42+
#include <boost/property_map/property_map.hpp>
43+
44+
45+
#include "cpp_common/base_graph.hpp"
46+
#include "cpp_common/interruption.hpp"
47+
#include <boost/graph/king_ordering.hpp>
48+
49+
50+
namespace pgrouting {
51+
52+
template <class G>
53+
std::vector<int64_t>
54+
kingOrdering(G &graph) {
55+
using V = typename G::V;
56+
57+
size_t n = boost::num_vertices(graph.graph);
58+
std::vector<int64_t> results(n);
59+
60+
auto index_map = boost::get(boost::vertex_index, graph.graph);
61+
62+
std::vector<V> colors(n);
63+
auto color_map = boost::make_iterator_property_map(colors.begin(), index_map);
64+
auto degree_map = boost::make_degree_map(graph.graph);
65+
std::vector<V> inv_permutation(n);
66+
67+
CHECK_FOR_INTERRUPTS();
68+
boost::king_ordering(
69+
graph.graph,
70+
inv_permutation.rbegin(),
71+
color_map,
72+
degree_map,
73+
index_map);
74+
75+
size_t j = 0;
76+
for (auto i = inv_permutation.begin(); i != inv_permutation.end(); ++i, ++j) {
77+
results[j] = static_cast<int64_t>(graph.graph[index_map[*i]].id);
78+
}
79+
80+
return results;
81+
}
82+
83+
} // namespace pgrouting
84+
85+
#endif // INCLUDE_ORDERING_ORDERING_HPP_
Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*PGR-GNU*****************************************************************
2-
File: ordering.hpp
2+
File: minDegreeOrdering.hpp
33
44
Generated with Template by:
55
Copyright (c) 2015 pgRouting developers
@@ -27,8 +27,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2727
2828
********************************************************************PGR-GNU*/
2929

30-
#ifndef INCLUDE_ORDERING_ORDERING_HPP_
31-
#define INCLUDE_ORDERING_ORDERING_HPP_
30+
#ifndef INCLUDE_ORDERING_MINDEGREEORDERING_HPP_
31+
#define INCLUDE_ORDERING_MINDEGREEORDERING_HPP_
3232
#pragma once
3333

3434
#include <vector>
@@ -44,48 +44,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
4444

4545
#include "cpp_common/base_graph.hpp"
4646
#include "cpp_common/interruption.hpp"
47-
#include <boost/graph/king_ordering.hpp>
4847
#include <boost/graph/minimum_degree_ordering.hpp>
4948

5049

5150
namespace pgrouting {
5251

53-
template <class G>
54-
std::vector<int64_t>
55-
kingOrdering(G &graph) {
56-
using B_G = typename G::B_G;
57-
using V = typename G::V;
58-
59-
size_t n = boost::num_vertices(graph.graph);
60-
std::vector<int64_t> results(n);
61-
62-
auto index_map = boost::get(boost::vertex_index, graph.graph);
63-
64-
std::vector<V> colors(n);
65-
auto color_map = boost::make_iterator_property_map(colors.begin(), index_map);
66-
auto degree_map = boost::make_degree_map(graph.graph);
67-
std::vector<V> inv_perm(n);
68-
69-
CHECK_FOR_INTERRUPTS();
70-
boost::king_ordering(
71-
graph.graph,
72-
inv_perm.rbegin(),
73-
color_map,
74-
degree_map,
75-
index_map);
76-
77-
size_t j = 0;
78-
for (auto i = inv_perm.begin(); i != inv_perm.end(); ++i, ++j) {
79-
results[j] = static_cast<int64_t>(graph.graph[index_map[*i]].id);
80-
}
81-
82-
return results;
83-
}
84-
8552
template <class G>
8653
std::vector<int64_t>
8754
minDegreeOrdering(G &graph) {
88-
using B_G = typename G::B_G;
8955
using V = typename G::V;
9056

9157
size_t n = boost::num_vertices(graph.graph);
@@ -99,27 +65,26 @@ minDegreeOrdering(G &graph) {
9965
std::vector<V> supernode_sizes(n, 1);
10066
auto supernode_map = boost::make_iterator_property_map(supernode_sizes.begin(), index_map);
10167

102-
std::vector<V> perm(n);
103-
std::vector<V> inv_perm(n);
68+
std::vector<V> permutation(n);
69+
std::vector<V> inv_permutation(n);
10470

105-
auto [vi, vi_end] = boost::vertices(graph.graph);
106-
for (; vi != vi_end; ++vi) {
107-
degree_map[*vi] = boost::degree(*vi, graph.graph);
71+
for (V v = 0; v < n; ++v) {
72+
degree_map[v] = boost::degree(v, graph.graph);
10873
}
10974

11075
CHECK_FOR_INTERRUPTS();
11176

11277
boost::minimum_degree_ordering(
11378
graph.graph,
11479
degree_map,
115-
inv_perm.begin(),
116-
perm.begin(),
80+
inv_permutation.begin(),
81+
permutation.begin(),
11782
supernode_map,
11883
0,
11984
index_map);
12085

12186
size_t j = 0;
122-
for (auto i = inv_perm.begin(); i != inv_perm.end(); ++i, ++j) {
87+
for (auto i = inv_permutation.begin(); i != inv_permutation.end(); ++i, ++j) {
12388
results[j] = static_cast<int64_t>(graph.graph[index_map[*i]].id);
12489
}
12590

just_start_week11

Whitespace-only changes.

src/ordering/ordering_driver.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3838
#include "cpp_common/alloc.hpp"
3939
#include "cpp_common/assert.hpp"
4040

41-
#include "ordering/ordering.hpp"
41+
#include "ordering/kingOrdering.hpp"
42+
#include "ordering/minDegreeOrdering.hpp"
4243

4344

4445

@@ -85,8 +86,10 @@ do_ordering(
8586
hint = "";
8687

8788
std::vector<int64_t> results;
88-
UndirectedGraph undigraph;
89+
auto vertices(pgrouting::extract_vertices(edges));
90+
UndirectedGraph undigraph(vertices);
8991
undigraph.insert_edges(edges);
92+
9093
if (which == 2) {
9194
results = minDegreeOrdering(undigraph);
9295
} else if (which ==3) {

0 commit comments

Comments
 (0)