1 | ---------------------------------------------------------------------------------------- |
---|
2 | The Shortest Path based on Dijkstra algorithm |
---|
3 | ---------------------------------------------------------------------------------------- |
---|
4 | |
---|
5 | The shortest_path functions have the following signature: |
---|
6 | |
---|
7 | CREATE OR REPLACE FUNCTION shortest_path(sql text, source_id integer, target_id integer, |
---|
8 | directed boolean, has_reverse_cost boolean) |
---|
9 | RETURNS SETOF path_result |
---|
10 | |
---|
11 | Where path_result is: |
---|
12 | |
---|
13 | CREATE TYPE path_result AS (vertex_id integer, edge_id integer, cost float8); |
---|
14 | |
---|
15 | Arguments are: |
---|
16 | |
---|
17 | * sql (text): a SQL query, which should return a set of rows with the following columns: |
---|
18 | - id (int4): an int4 identifier of the edge |
---|
19 | - source (int4): an int4 identifier of the source vertex |
---|
20 | - target (int4): an int4 identifier of the target vertex |
---|
21 | - cost (float8): double precision value of the edge traversal cost. (a negative cost |
---|
22 | will prevent the edge from being inserted in the graph). |
---|
23 | - bidirectional (bool) (optional): specify if the edge is bidirectional, when you use it, |
---|
24 | the algorithm is more efficient!, because only the |
---|
25 | marked edges are added instead of all all edges. |
---|
26 | - reverse_cost (float8) (optional): the cost for the reverse traversal of the |
---|
27 | edge. This is only used when the directed |
---|
28 | and has_reverse_cost parameters are true (see the above |
---|
29 | remark about negative costs). |
---|
30 | |
---|
31 | * dource_id (int4): start node id |
---|
32 | |
---|
33 | * target_id (int4): end node id |
---|
34 | |
---|
35 | * directed (bool): true if the graph is directed. if you want to use bidirectional and/or reverse_cost |
---|
36 | parameters in the query, you need to pass it as true. otherwise these will not be taken. |
---|
37 | |
---|
38 | * has_reverse_cost (bool): (deprecated, no used by the function internally. Now however, with only passing the |
---|
39 | argument "directed" and "reverse_cost" in the SQL does the same). |
---|
40 | if true, the reverse_cost column of the SQL generated set of rows |
---|
41 | will be used for the cost of the traversal of the edge in the |
---|
42 | opposite direction. |
---|
43 | |
---|
44 | EXAMPLE: |
---|
45 | |
---|
46 | graph1 |
---|
47 | |
---|
48 | 10 |
---|
49 | A----->B |
---|
50 | | | |
---|
51 | 100 | |110 |
---|
52 | | | |
---|
53 | E<------C------D |
---|
54 | | 60 | 20 |
---|
55 | 35| |50 |
---|
56 | | | |
---|
57 | G------>F<-----H |
---|
58 | 25 15 |
---|
59 | |
---|
60 | Legend: |
---|
61 | ------> Unidirectional element |
---|
62 | ------- Bidirectional element |
---|
63 | | |
---|
64 | | Bidirectional element |
---|
65 | | |
---|
66 | |
---|
67 | creating my Graph tables: |
---|
68 | |
---|
69 | -- Vertices of my Graph |
---|
70 | CREATE TABLE graph1_vertices |
---|
71 | ( |
---|
72 | id integer, |
---|
73 | name varchar(25), |
---|
74 | CONSTRAINT pk_graph1_vertices_gid PRIMARY KEY (id) |
---|
75 | ) WITH (OIDS=TRUE); |
---|
76 | |
---|
77 | -- Edges of my Graph |
---|
78 | CREATE TABLE graph1_edges |
---|
79 | ( |
---|
80 | id integer, |
---|
81 | source integer, |
---|
82 | target integer, |
---|
83 | bidirectional bool, |
---|
84 | cost float, |
---|
85 | reverse_cost float, |
---|
86 | CONSTRAINT pk_graph1_edges_gid PRIMARY KEY (id) |
---|
87 | ) WITH (OIDS=TRUE); |
---|
88 | |
---|
89 | INSERT INTO graph1_vertices VALUES (100, 'A'); |
---|
90 | INSERT INTO graph1_vertices VALUES (200, 'B'); |
---|
91 | INSERT INTO graph1_vertices VALUES (300, 'C'); |
---|
92 | INSERT INTO graph1_vertices VALUES (400, 'D'); |
---|
93 | INSERT INTO graph1_vertices VALUES (500, 'E'); |
---|
94 | INSERT INTO graph1_vertices VALUES (600, 'F'); |
---|
95 | INSERT INTO graph1_vertices VALUES (700, 'G'); |
---|
96 | INSERT INTO graph1_vertices VALUES (800, 'H'); |
---|
97 | |
---|
98 | INSERT INTO graph1_edges VALUES (1, 100, 200, false, 10, 0.5); -- A->B |
---|
99 | INSERT INTO graph1_edges VALUES (2, 200, 400, true, 110, 0.7); -- B->D |
---|
100 | INSERT INTO graph1_edges VALUES (3, 100, 300, true, 100, 0.9); -- A->C |
---|
101 | INSERT INTO graph1_edges VALUES (4, 400, 300, true, 20, 1); -- D->C |
---|
102 | INSERT INTO graph1_edges VALUES (5, 300, 600, true, 50, 1); -- C->F |
---|
103 | INSERT INTO graph1_edges VALUES (6, 300, 500, false, 60, 1); -- C->E |
---|
104 | INSERT INTO graph1_edges VALUES (7, 500, 700, true, 35, 1); -- E->G |
---|
105 | INSERT INTO graph1_edges VALUES (8, 700, 600, false, 25, .05); -- G->F |
---|
106 | INSERT INTO graph1_edges VALUES (9, 800, 600, false, 45, 1); -- H->F |
---|
107 | |
---|
108 | Testing my graph: |
---|
109 | |
---|
110 | ROUTE 1: |
---|
111 | Undirected Graph: |
---|
112 | SELECT * FROM shortest_path('SELECT id::int4, source::int4, target::int4, cost::float8 FROM graph1_edges', |
---|
113 | (SELECT id::int4 FROM graph1_vertices WHERE name = 'B'), (SELECT id::int4 FROM graph1_vertices WHERE name = 'H'),false, false); |
---|
114 | |
---|
115 | result: |
---|
116 | vertex_id | edge_id | cost |
---|
117 | -----------+---------+------ |
---|
118 | 200 | 1 | 10 |
---|
119 | 100 | 3 | 100 |
---|
120 | 300 | 5 | 50 |
---|
121 | 600 | 9 | 45 |
---|
122 | 800 | -1 | 0 |
---|
123 | (5 rows) |
---|
124 | |
---|
125 | Directed Graph: |
---|
126 | SELECT * FROM shortest_path('SELECT id::int4, source::int4, target::int4, cost::float8 FROM graph1_edges', |
---|
127 | (SELECT id::int4 FROM graph1_vertices WHERE name = 'B'), (SELECT id::int4 FROM graph1_vertices WHERE name = 'H'),true, false); |
---|
128 | |
---|
129 | result: |
---|
130 | vertex_id | edge_id | cost |
---|
131 | -----------+---------+------ |
---|
132 | (0 rows) |
---|
133 | |
---|
134 | ROUTE 2: |
---|
135 | Undirected Graph: |
---|
136 | SELECT * FROM shortest_path('SELECT id::int4, source::int4, target::int4, cost::float8 FROM graph1_edges', |
---|
137 | (SELECT id::int4 FROM graph1_vertices WHERE name = 'B'), (SELECT id::int4 FROM graph1_vertices WHERE name = 'G'),false, false); |
---|
138 | |
---|
139 | result: |
---|
140 | vertex_id | edge_id | cost |
---|
141 | -----------+---------+------ |
---|
142 | 200 | 1 | 10 |
---|
143 | 100 | 3 | 100 |
---|
144 | 300 | 5 | 50 |
---|
145 | 600 | 8 | 25 |
---|
146 | 700 | -1 | 0 |
---|
147 | (5 rows) |
---|
148 | |
---|
149 | |
---|
150 | Directed Graph: |
---|
151 | SELECT * FROM shortest_path('SELECT id::int4, source::int4, target::int4, cost::float8 FROM graph1_edges', |
---|
152 | (SELECT id::int4 FROM graph1_vertices WHERE name = 'B'), (SELECT id::int4 FROM graph1_vertices WHERE name = 'G'),true, false); |
---|
153 | |
---|
154 | result: |
---|
155 | vertex_id | edge_id | cost |
---|
156 | -----------+---------+------ |
---|
157 | 200 | 2 | 110 |
---|
158 | 400 | 4 | 20 |
---|
159 | 300 | 6 | 60 |
---|
160 | 500 | 7 | 35 |
---|
161 | 700 | -1 | 0 |
---|
162 | (5 rows) |
---|
163 | |
---|
164 | Directed Graph with reverse_cost (old version): |
---|
165 | SELECT * FROM shortest_path('SELECT id::int4, source::int4, target::int4, cost::float8, reverse_cost::float8 FROM graph1_edges', |
---|
166 | (SELECT id::int4 FROM graph1_vertices WHERE name = 'B'), (SELECT id::int4 FROM graph1_vertices WHERE name = 'G'),true, true); |
---|
167 | |
---|
168 | vertex_id | edge_id | cost |
---|
169 | -----------+---------+------ |
---|
170 | 200 | 1 | 0.5 |
---|
171 | 100 | 3 | 100 |
---|
172 | 300 | 5 | 50 |
---|
173 | 600 | 8 | 0.05 |
---|
174 | 700 | -1 | 0 |
---|
175 | (5 rows) |
---|
176 | |
---|
177 | Directed Graph with reverse_cost (new version): |
---|
178 | SELECT * FROM shortest_path('SELECT id::int4, source::int4, target::int4, cost::float8, reverse_cost::float8 FROM graph1_edges', |
---|
179 | (SELECT id::int4 FROM graph1_vertices WHERE name = 'B'), (SELECT id::int4 FROM graph1_vertices WHERE name = 'G'),true, false); |
---|
180 | |
---|
181 | vertex_id | edge_id | cost |
---|
182 | -----------+---------+------ |
---|
183 | 200 | 1 | 0.5 |
---|
184 | 100 | 3 | 100 |
---|
185 | 300 | 5 | 50 |
---|
186 | 600 | 8 | 0.05 |
---|
187 | 700 | -1 | 0 |
---|
188 | (5 rows) |
---|
189 | |
---|
190 | LICENCE |
---|
191 | |
---|
192 | Most features are available under GPL. |
---|
193 | Some Boost extesions are available under Boost license (see LICENSE_1_0.txt) |
---|
194 | |
---|
195 | AUTHORS |
---|
196 | Christian Gonzalez <christian.gonzalez@sigis.com.ve> |
---|
197 | Anton A. Patrushev <anton@orkney.co.jp> |
---|
198 | Mario H. Basa <mbasa@orkney.co.jp> |
---|
199 | |
---|
200 | Dijkstra part was made by |
---|
201 | Sylvain Pasche <sylvain.pasche@camptocamp.com> |
---|
202 | |
---|