# How to extract shortest path from floydwarshall algorithm pgrouting

I've tried Floyd-Warshall algorithm using pgrouting with the following code;

SELECT * FROM pgr_floydwarshall(
'SELECT gid AS id,
source,
target,
meters AS cost
FROM jalan')


and the result is:

The question is How to extract path to new table from the result above if the source node = 9 and target node = 24?

First, to filter your result, simply add your required condition to your query. So here, it's your query with the addition WHERE start_vid = 9 AND end_vid = 24.

Here you can find further info on the usage of WHERE clauses.

After that, just insert your result into your desired table as explained in the documentation.

• I'm able to have a result using where clause as the picture below, tinypic.com/r/2nkuln8/9 the next question is how can i join the result with road table (geom column) so that it can be visualized in a map/join with geom column? Commented Jun 29, 2016 at 11:23
• You cannot do that using the FloydWarshall, because it doesn't give you the path itself, but only the cost. See the documentation. docs.pgrouting.org/2.2/en/src/allpairs/doc/… If you need the path, you can use a path finding algorithm like Dijkstra or A-star. Commented Jun 29, 2016 at 12:10
• I compare some algorithms for my thesis, including floyd-warshall and dijkstra. Is there a way to get the shortest path from floyd-warshall algorithm using pgrouting? Commented Jun 29, 2016 at 13:35
• No there isn't. See the first point of the main characteristics. Commented Jun 29, 2016 at 13:47
SELECT * FROM pgr_floydwarshall(
'SELECT gid AS id,
source,
target,
meters AS cost
FROM jalan')
WHERE start_vid = 9 AND end_vid = 24;


You were actually computing the shortest path from/to every node you have which was surely taking too much time, you need to add the where clause to you query so you can specify the origin and destination

• I'm able to have a result using where clause as the picture below, tinypic.com/r/2nkuln8/9 the next question is how can i join the result with road table (geom column) so that it can be visualized in a map? Commented Jun 28, 2016 at 17:39
• you can launch your query from the database manager in qgis this way you import the result directly in the map Commented Jun 28, 2016 at 17:42
• I'm sorry I still didn't get it, because the DB manager asked me to define the unique values and geometry column which is not found in the query result (as attached in pic below). How can I handle this? tinypic.com/r/wtiwjm/9 Commented Jun 28, 2016 at 17:50
• I assume that you have a table with the geometries, different from the jalan table, right ? if yes can you post a screenshot of select * from yourTable; please Commented Jun 28, 2016 at 17:57
• I used "jalan" table as my edge_table, the table has gid, cost and of course it has "geom" column. The issue is when I run the floyd warshall query, the result table did not include "geom" and/or "ID" column. tinypic.com/r/hwamia/9 Commented Jun 29, 2016 at 13:27