Skip to content

Commit 839599e

Browse files
authored
Merge pull request #471 from wifiBlack/week-8-king-minimum-ordering
Week 8 king minimum ordering
2 parents f775ed2 + 01b6773 commit 839599e

File tree

12 files changed

+378
-51
lines changed

12 files changed

+378
-51
lines changed

docqueries/ordering/kingOrdering.pg

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,3 @@ 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);

docqueries/ordering/kingOrdering.result

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,27 @@ SET
66
SELECT * FROM pgr_kingOrdering(
77
'SELECT id, source, target, cost, reverse_cost FROM edges'
88
);
9-
ERROR: No results found
9+
seq | node
10+
-----+------
11+
1 | 13
12+
2 | 14
13+
3 | 2
14+
4 | 4
15+
5 | 1
16+
6 | 9
17+
7 | 3
18+
8 | 8
19+
9 | 5
20+
10 | 7
21+
11 | 12
22+
12 | 6
23+
13 | 11
24+
14 | 17
25+
15 | 10
26+
16 | 16
27+
17 | 15
28+
(17 rows)
1029

11-
CONTEXT: SQL function "pgr_kingordering" statement 1
1230
/* -- q2 */
1331
ROLLBACK;
1432
ROLLBACK

docqueries/ordering/minDegreeOrdering.result

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,27 @@ SET
66
SELECT * FROM pgr_minDegreeOrdering(
77
'SELECT id, source, target, cost, reverse_cost FROM edges'
88
);
9-
ERROR: No results found
9+
seq | node
10+
-----+------
11+
1 | 8
12+
2 | 12
13+
3 | 1
14+
4 | 9
15+
5 | 16
16+
6 | 1
17+
7 | 15
18+
8 | 1
19+
9 | 4
20+
10 | 10
21+
11 | 8
22+
12 | 14
23+
13 | 3
24+
14 | 4
25+
15 | 1
26+
16 | 2
27+
17 | 6
28+
(17 rows)
1029

11-
CONTEXT: SQL function "pgr_mindegreeordering" statement 1
1230
/* -- q2 */
1331
ROLLBACK;
1432
ROLLBACK

include/ordering/ordering.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,9 @@ kingOrdering(G &graph) {
7070
boost::king_ordering(graph.graph, inv_perm. rbegin(), color_map, degree_map, index_map);
7171
size_t j = 0;
7272
for (auto i = inv_perm.begin(); i != inv_perm.end(); ++i, ++j) {
73-
results[j] = static_cast<int64_t>(index_map[*i]);
73+
results[j] = static_cast<int64_t>(graph.graph[index_map[*i]].id);
7474
}
7575

76-
throw(std::to_string(results.size()));
7776
return results;
7877
}
7978

@@ -109,7 +108,7 @@ minDegreeOrdering(G &graph) {
109108
index_map);
110109

111110
for (size_t i = 0; i < n; ++i) {
112-
results[i] = static_cast<int64_t>(inv_perm[i]);
111+
results[i] = static_cast<int64_t>(graph.graph[inv_perm[i]].id);
113112
}
114113

115114
return results;

just_start_week8

Whitespace-only changes.

pgtap/ordering/cuthillMckeeOrdering/no_crash_test.pg

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2020
BEGIN;
2121

2222
UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost);
23-
SELECT CASE WHEN NOT min_version('3.4.0') THEN plan(1) ELSE plan(7) END;
23+
SELECT CASE WHEN NOT min_version('3.4.0') THEN plan(1) ELSE plan(4) END;
2424

2525
PREPARE edges AS
2626
SELECT id, source, target, cost, reverse_cost FROM edges;
@@ -46,22 +46,15 @@ BEGIN
4646
RETURN;
4747
END IF;
4848

49-
RETURN QUERY
50-
SELECT isnt_empty('edges', 'Should be not empty to tests be meaningful');
51-
RETURN QUERY
52-
SELECT is_empty('null_ret', 'Should be empty to tests be meaningful');
53-
RETURN QUERY
54-
SELECT set_eq('null_ret_arr', 'SELECT NULL::BIGINT[]', 'Should be empty to tests be meaningful');
55-
56-
-- pgr_cuthillMckeeOrdering
57-
params = ARRAY[
58-
'$$SELECT id, source, target, cost, reverse_cost FROM edges$$'
59-
]::TEXT[];
60-
subs = ARRAY[
61-
'NULL'
62-
]::TEXT[];
63-
64-
RETURN query SELECT * FROM no_crash_test('pgr_cuthillMckeeOrdering', params, subs);
49+
-- pgr_cuthillMckeeOrdering
50+
params = ARRAY[
51+
'$$SELECT id, source, target, cost, reverse_cost FROM edges$$'
52+
]::TEXT[];
53+
subs = ARRAY[
54+
'NULL'
55+
]::TEXT[];
56+
57+
RETURN query SELECT * FROM no_crash_test('pgr_cuthillMckeeOrdering', params, subs);
6558

6659
END
6760
$BODY$
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
/*PGR-GNU*****************************************************************
3+
4+
Copyright (c) 2018 pgRouting developers
5+
Mail: project@pgrouting.org
6+
7+
------
8+
This program is free software; you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation; either version 2 of the License, or
11+
(at your option) any later version.
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
********************************************************************PGR-GNU*/
20+
BEGIN;
21+
22+
UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost);
23+
SELECT CASE WHEN NOT min_version('4.0.0') THEN plan(1) ELSE plan(4) END;
24+
25+
26+
CREATE OR REPLACE FUNCTION edge_cases()
27+
RETURNS SETOF TEXT AS
28+
$BODY$
29+
BEGIN
30+
31+
IF NOT min_version('4.0.0') THEN
32+
RETURN QUERY
33+
SELECT skip(1, 'Function is new on 4.0.0');
34+
RETURN;
35+
END IF;
36+
37+
-- 0 edge, 0 vertex test
38+
39+
PREPARE q1 AS
40+
SELECT * FROM pgr_kingOrdering(
41+
'SELECT id, source, target, cost, reverse_cost FROM edges WHERE id > 18'
42+
);
43+
44+
RETURN QUERY
45+
SELECT is_empty('q1', 'Graph with 0 edge and 0 vertex -> Empty row is returned');
46+
47+
-- 1 vertex test: 6 -- 6
48+
49+
PREPARE q2 AS
50+
SELECT *
51+
FROM pgr_kingOrdering('SELECT id, source, source AS target, cost, reverse_cost FROM edges WHERE id = 2');
52+
53+
RETURN QUERY
54+
SELECT set_eq('q2', $$VALUES (1, 6)$$, '6 -- 6: Same node returned');
55+
56+
-- 2 vertices test (connected): 3 -- 7; 7 -- 3
57+
58+
PREPARE q3 AS
59+
SELECT *
60+
FROM pgr_kingOrdering('SELECT id, source, target, cost, reverse_cost FROM edges WHERE id = 7'
61+
);
62+
63+
RETURN QUERY
64+
SELECT set_eq('q3', $$VALUES (1,3), (2,7)$$, '3 --7; 7 -- 3: Natural order of edges');
65+
66+
PERFORM todo_start('Same graph, but with different order of edges');
67+
-- 2 vertices test (connected): 7 -- 3; 3 -- 7
68+
69+
PREPARE q4 AS
70+
SELECT *
71+
FROM pgr_kingOrdering('SELECT id, target AS source, source AS target, cost, reverse_cost FROM edges WHERE id = 7'
72+
);
73+
74+
RETURN QUERY
75+
SELECT set_eq('q4', $$VALUES (1,3), (2,7)$$, '7 --3; 3 -- 7: Does not matter if 3 comes first or second');
76+
PERFORM todo_end();
77+
78+
END;
79+
$BODY$
80+
LANGUAGE plpgsql;
81+
82+
SELECT edge_cases();
83+
84+
85+
SELECT * FROM finish();
86+
ROLLBACK;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
/*PGR-GNU*****************************************************************
3+
4+
Copyright (c) 2018 pgRouting developers
5+
Mail: project@pgrouting.org
6+
7+
------
8+
This program is free software; you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation; either version 2 of the License, or
11+
(at your option) any later version.
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
********************************************************************PGR-GNU*/
20+
BEGIN;
21+
22+
UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost);
23+
SELECT CASE WHEN NOT min_version('4.0.0') THEN plan(1) ELSE plan(54) END;
24+
25+
CREATE OR REPLACE FUNCTION inner_query()
26+
RETURNS SETOF TEXT AS
27+
$BODY$
28+
BEGIN
29+
30+
IF NOT min_version('4.0.0') THEN
31+
RETURN QUERY
32+
SELECT skip(1, 'pgr_kingOrdering is new on 4.0.0');
33+
RETURN;
34+
END IF;
35+
36+
RETURN QUERY
37+
SELECT style_dijkstra('pgr_kingOrdering(', ')');
38+
39+
END;
40+
$BODY$
41+
LANGUAGE plpgsql;
42+
43+
SELECT inner_query();
44+
45+
SELECT finish();
46+
ROLLBACK;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
/*PGR-GNU*****************************************************************
3+
4+
Copyright (c) 2018 pgRouting developers
5+
Mail: project@pgrouting.org
6+
7+
------
8+
This program is free software; you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation; either version 2 of the License, or
11+
(at your option) any later version.
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
********************************************************************PGR-GNU*/
20+
BEGIN;
21+
22+
UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost);
23+
SELECT CASE WHEN NOT min_version('4.0.0') THEN plan(1) ELSE plan(4) END;
24+
25+
PREPARE edges AS
26+
SELECT id, source, target, cost, reverse_cost FROM edges;
27+
28+
PREPARE null_ret AS
29+
SELECT id FROM vertices WHERE id IN (-1);
30+
31+
PREPARE null_ret_arr AS
32+
SELECT array_agg(id) FROM vertices WHERE id IN (-1);
33+
34+
35+
36+
CREATE OR REPLACE FUNCTION no_crash()
37+
RETURNS SETOF TEXT AS
38+
$BODY$
39+
DECLARE
40+
params TEXT[];
41+
subs TEXT[];
42+
BEGIN
43+
IF NOT min_version('4.0.0') THEN
44+
RETURN QUERY
45+
SELECT skip(1, 'Function is new on 4.0.0');
46+
RETURN;
47+
END IF;
48+
49+
-- pgr_kingOrdering
50+
params = ARRAY[
51+
'$$SELECT id, source, target, cost, reverse_cost FROM edges$$'
52+
]::TEXT[];
53+
subs = ARRAY[
54+
'NULL'
55+
]::TEXT[];
56+
57+
RETURN query SELECT * FROM no_crash_test('pgr_kingOrdering', params, subs);
58+
59+
END
60+
$BODY$
61+
LANGUAGE plpgsql VOLATILE;
62+
63+
64+
SELECT * FROM no_crash();
65+
66+
SELECT finish();
67+
ROLLBACK;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
/*PGR-GNU*****************************************************************
3+
4+
Copyright (c) 2018 pgRouting developers
5+
Mail: project@pgrouting.org
6+
7+
------
8+
This program is free software; you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation; either version 2 of the License, or
11+
(at your option) any later version.
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
********************************************************************PGR-GNU*/
20+
BEGIN;
21+
22+
UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost);
23+
SELECT CASE WHEN NOT min_version('4.0.0') THEN plan(1) ELSE plan(54) END;
24+
25+
CREATE OR REPLACE FUNCTION inner_query()
26+
RETURNS SETOF TEXT AS
27+
$BODY$
28+
BEGIN
29+
30+
IF NOT min_version('4.0.0') THEN
31+
RETURN QUERY
32+
SELECT skip(1, 'pgr_minDegreeOrdering is new on 4.0.0');
33+
RETURN;
34+
END IF;
35+
36+
RETURN QUERY
37+
SELECT style_dijkstra('pgr_minDegreeOrdering(', ')');
38+
39+
END;
40+
$BODY$
41+
LANGUAGE plpgsql;
42+
43+
SELECT inner_query();
44+
45+
SELECT finish();
46+
ROLLBACK;

0 commit comments

Comments
 (0)