|
| 1 | +.. |
| 2 | + **************************************************************************** |
| 3 | + pgRouting Workshop Manual |
| 4 | + Copyright(c) pgRouting Contributors |
| 5 | +
|
| 6 | + This documentation is licensed under a Creative Commons Attribution-Share |
| 7 | + Alike 3.0 License: http://creativecommons.org/licenses/by-sa/3.0/ |
| 8 | + **************************************************************************** |
| 9 | + |
| 10 | +Boost Dijkstra Example |
| 11 | +=============================================================================== |
| 12 | + |
| 13 | +Using, pgRouting you can translate `C++ dijkstra code <http://www.boost.org/doc/libs/1_59_0/libs/graph/example/dijkstra-example.cpp>`_ into SQL commands. |
| 14 | + |
| 15 | +.. rubric:: Boost Code |
| 16 | + |
| 17 | +.. code-block:: cpp |
| 18 | +
|
| 19 | + const int num_nodes = 5; |
| 20 | + enum nodes { A, B, C, D, E }; = 1 |
| 21 | + char name[] = "ABCDE"; |
| 22 | + Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E), |
| 23 | + Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B) |
| 24 | + }; |
| 25 | + int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 }; |
| 26 | +
|
| 27 | +Here is the tranlasted SQL commands. |
| 28 | + |
| 29 | +.. rubric:: Translated Code |
| 30 | + |
| 31 | +.. code-block:: sql |
| 32 | + |
| 33 | + DROP TABLE IF EXISTS table1; |
| 34 | + CREATE TABLE table1 ( |
| 35 | + id SERIAL, |
| 36 | + source INTEGER, |
| 37 | + target INTEGER, |
| 38 | + source_name TEXT, |
| 39 | + target_name TEXT, |
| 40 | + cost FLOAT |
| 41 | + ); |
| 42 | + DROP TABLE IF EXISTS table1_vertices; |
| 43 | + CREATE TABLE table1_vertices ( |
| 44 | + vid SERIAL, |
| 45 | + name TEXT |
| 46 | + ); |
| 47 | + |
| 48 | + INSERT INTO table1_vertices (name) VALUES ('A'); |
| 49 | + INSERT INTO table1_vertices (name) VALUES ('B'); |
| 50 | + INSERT INTO table1_vertices (name) VALUES ('C'); |
| 51 | + INSERT INTO table1_vertices (name) VALUES ('D'); |
| 52 | + INSERT INTO table1_vertices (name) VALUES ('E'); |
| 53 | + |
| 54 | + INSERT INTO table1 (source_name, target_name, cost) VALUES ('A', 'C', 1); |
| 55 | + INSERT INTO table1 (source_name, target_name, cost) VALUES ('B', 'B', 2); |
| 56 | + INSERT INTO table1 (source_name, target_name, cost) VALUES ('B', 'D', 1); |
| 57 | + INSERT INTO table1 (source_name, target_name, cost) VALUES ('B', 'E', 2); |
| 58 | + INSERT INTO table1 (source_name, target_name, cost) VALUES ('C', 'B', 7); |
| 59 | + INSERT INTO table1 (source_name, target_name, cost) VALUES ('C', 'D', 3); |
| 60 | + INSERT INTO table1 (source_name, target_name, cost) VALUES ('D', 'E', 1); |
| 61 | + INSERT INTO table1 (source_name, target_name, cost) VALUES ('E', 'A', 1); |
| 62 | + INSERT INTO table1 (source_name, target_name, cost) VALUES ('E', 'B', 1); |
| 63 | + |
| 64 | + UPDATE table1 |
| 65 | + SET source = table1_vertices.vid |
| 66 | + FROM table1_vertices |
| 67 | + WHERE source_name = name; |
| 68 | + |
| 69 | + UPDATE table1 SET target = table1_vertices.vid |
| 70 | + FROM table1_vertices |
| 71 | + WHERE target_name = name; |
| 72 | + |
| 73 | + -- Their output starts with 0 so we subtract 1 to the vid |
| 74 | + -- pgrouting: no paths or 0 length (aka I am there, so no path) are not included |
| 75 | + SELECT end_vid-1, agg_cost FROM pgr_dijkstra( |
| 76 | + 'SELECT id, source, target, cost FROM table1', |
| 77 | + 1, ARRAY[1, 2, 3, 4, 5], true) where edge < 0 order by end_vid; |
| 78 | +
|
| 79 | +Output from running the following SQL command: |
| 80 | + |
| 81 | +.. rubric:: Output |
| 82 | + |
| 83 | +.. code-block:: sql |
| 84 | +
|
| 85 | + ?column? | agg_cost |
| 86 | + ----------+---------- |
| 87 | + 1 | 6 |
| 88 | + 2 | 1 |
| 89 | + 3 | 4 |
| 90 | + 4 | 5 |
| 91 | + (4 rows) |
0 commit comments