root/sandbox/sigis/christian.gonzalez/trunk/README.shortest_path

Revision 332, 6.8 KB (checked in by christian.gonzalez, 18 months ago)

- "has_reverse_cost" --> deprecated, no used by the function internally. Now however, with only passing the

argument "directed" and "reverse_cost" in the SQL does the same

- added parameter "bidirectional" --> it
- added file README.shortest_path.
- elog --> ereport.
- text2char --> eliminated, now using -->DatumGetCString(DirectFunctionCall?1(textout,PointerGetDatum?( ))).
- palloc --> SPI_palloc.
- repalloc --> SPI_repalloc.
- pfree --> SPI_pfree.
- the proccess for calculating the min_id was improved, now calculated in function "fetch_edge".
- minor improvements.
- added some comments.
- added some macro fucntions.
- added constant definitions.
- reorganized the source code

Line 
1----------------------------------------------------------------------------------------
2The Shortest Path based on Dijkstra algorithm
3----------------------------------------------------------------------------------------
4
5The shortest_path functions have the following signature:
6
7CREATE OR REPLACE FUNCTION shortest_path(sql text, source_id integer, target_id integer,
8                                         directed boolean, has_reverse_cost boolean)
9RETURNS SETOF path_result
10
11Where path_result is:
12
13CREATE TYPE path_result AS (vertex_id integer, edge_id integer, cost float8);
14
15Arguments 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
44EXAMPLE:
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           
60Legend: 
61        ------> Unidirectional element
62        ------- Bidirectional element
63          |
64          |             Bidirectional element
65          |
66         
67creating my Graph tables:
68
69-- Vertices of my Graph
70CREATE 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
78CREATE 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
89INSERT INTO graph1_vertices VALUES (100, 'A');
90INSERT INTO graph1_vertices VALUES (200, 'B');
91INSERT INTO graph1_vertices VALUES (300, 'C');
92INSERT INTO graph1_vertices VALUES (400, 'D');
93INSERT INTO graph1_vertices VALUES (500, 'E');
94INSERT INTO graph1_vertices VALUES (600, 'F');
95INSERT INTO graph1_vertices VALUES (700, 'G');
96INSERT INTO graph1_vertices VALUES (800, 'H');
97
98INSERT INTO graph1_edges VALUES (1, 100, 200, false, 10, 0.5); -- A->B
99INSERT INTO graph1_edges VALUES (2, 200, 400, true, 110, 0.7);  -- B->D
100INSERT INTO graph1_edges VALUES (3, 100, 300, true, 100, 0.9); -- A->C
101INSERT INTO graph1_edges VALUES (4, 400, 300, true, 20, 1);    -- D->C
102INSERT INTO graph1_edges VALUES (5, 300, 600, true, 50, 1);    -- C->F
103INSERT INTO graph1_edges VALUES (6, 300, 500, false, 60, 1);   -- C->E
104INSERT INTO graph1_edges VALUES (7, 500, 700, true, 35, 1);    -- E->G
105INSERT INTO graph1_edges VALUES (8, 700, 600, false, 25, .05); -- G->F
106INSERT INTO graph1_edges VALUES (9, 800, 600, false, 45, 1);   -- H->F
107
108Testing my graph:
109
110ROUTE 1:
111Undirected Graph:
112SELECT * 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
115result:
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
125Directed Graph:
126SELECT * 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
129result:
130 vertex_id | edge_id | cost
131-----------+---------+------
132(0 rows)
133
134ROUTE 2:
135Undirected Graph:
136SELECT * 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
139result:
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
150Directed Graph:
151SELECT * 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
154result:
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
164Directed Graph with reverse_cost (old version):
165SELECT * 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
177Directed Graph with reverse_cost (new version):
178SELECT * 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
190LICENCE
191
192Most features are available under GPL.
193Some Boost extesions are available under Boost license (see LICENSE_1_0.txt)
194
195AUTHORS
196Christian Gonzalez <christian.gonzalez@sigis.com.ve>
197Anton A. Patrushev <anton@orkney.co.jp>
198Mario H. Basa <mbasa@orkney.co.jp>
199
200Dijkstra part was made by
201Sylvain Pasche <sylvain.pasche@camptocamp.com>
202
Note: See TracBrowser for help on using the browser.