BEGIN;
BEGIN
SET client_min_messages TO NOTICE;
SET
Adding an additional column to store if its part of the contracted graph
ALTER TABLE edge_table ADD is_contracted BOOLEAN DEFAULT false;
ALTER TABLE
Adding a new edge joining vertices 3 and 5 with cost=2 to the edge table
INSERT INTO edge_table(id, source, target, cost, reverse_cost, is_contracted)
VALUES (19, 3, 5, 2, 2, true);
INSERT 0 1
Adding a new edge joining vertices 3 and 9 with cost=2 to the edge table
INSERT INTO edge_table(id, source, target, cost, reverse_cost, is_contracted)
VALUES (20, 3, 9, 2, 2, true);
INSERT 0 1
Adding a new edge joining vertices 5 and 11 with cost=2 to the edge table
INSERT INTO edge_table(id, source, target, cost, reverse_cost, is_contracted)
VALUES (21, 5, 11, 2, 2, true);
INSERT 0 1
Adding a new edge joining vertices 9 and 11 with cost=2 to the edge table
INSERT INTO edge_table(id, source, target, cost, reverse_cost, is_contracted)
VALUES (22, 9, 11, 2, 2, true);
INSERT 0 1
Case 1: Both source and target belong to the contracted graph.
We are going to route from 3 to 11
Since 3 and 11 both are in the contracted graph we need not add any vertices.
SELECT * FROM
pgr_dijkstra($$
    SELECT id, source, target, cost, reverse_cost
    FROM edge_table
    WHERE source IN (3, 5, 6, 9, 11, 15, 17)
        AND target IN (3, 5, 6, 9, 11, 15, 17)$$,
    3, 11, false);
 seq | path_seq | node | edge | cost | agg_cost 
-----+----------+------+------+------+----------
   1 |        1 |    3 |    5 |    1 |        0
   2 |        2 |    6 |   11 |    1 |        1
   3 |        3 |   11 |   -1 |    0 |        2
(3 rows)

Case 2: source belongs to a contracted graph, while target belongs to a vertex subgraph.
We are going to route from 3 to 7
Since 7 is in the contracted subgraph of vertex 5 we add {7, 8} to the vertex set, so the vertex set becomes (3, 5, 6, 7, 8, 9, 11, 15, 17)
SELECT * FROM pgr_dijkstra($$
    SELECT id, source, target, cost, reverse_cost
    FROM edge_table
    WHERE source IN (3, 5, 6, 9, 11, 15, 17,   7, 8)
        AND target IN (3, 5, 6, 9, 11, 15, 17,   7, 8)$$,
    3, 7, false);
 seq | path_seq | node | edge | cost | agg_cost 
-----+----------+------+------+------+----------
   1 |        1 |    3 |   19 |    2 |        0
   2 |        2 |    5 |    7 |    1 |        2
   3 |        3 |    8 |    6 |    1 |        3
   4 |        4 |    7 |   -1 |    0 |        4
(4 rows)

Case 3: source belongs to a contracted graph, while target belongs to a edge subgraph.
We are going to route from 3 to 13
Since 13 is in the contracted subgraph of edge (5, 11) we add {10, 13} to the vertex set, so the vertex set becomes (3, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17)
SELECT * FROM pgr_dijkstra($$
    SELECT id, source, target, cost, reverse_cost
    FROM edge_table
    WHERE source IN (3, 5, 6, 9, 11, 15, 17,    10, 13)
        AND target IN (3, 5, 6, 9, 11, 15, 17,    10, 13)$$,
    3, 13, false);
 seq | path_seq | node | edge | cost | agg_cost 
-----+----------+------+------+------+----------
   1 |        1 |    3 |    5 |    1 |        0
   2 |        2 |    6 |   11 |    1 |        1
   3 |        3 |   11 |   12 |    1 |        2
   4 |        4 |   10 |   14 |    1 |        3
   5 |        5 |   13 |   -1 |    0 |        4
(5 rows)

Case 4: source belongs to a vertex subgraph, while target belongs to a edge subgraph.
We are going to route from 7 to 13
Since 13 is in the contracted subgraph of edge (5, 11) we add {10, 13} to the vertex set, and since 7 is in the contracted subgraph of vertex 5 we add {7, 8} to the vertex sets (3, 5, 6, 7, 8, 9, 11, 15, 17)
SELECT * FROM pgr_dijkstra($$
    SELECT id, source, target, cost, reverse_cost
    FROM edge_table
    WHERE source IN (3, 5, 6, 9, 11, 15, 17,    7, 8,   10, 13)
        AND target IN (3, 5, 6, 9, 11, 15, 17,   7, 8,   10, 13)$$,
    7, 13, false);
 seq | path_seq | node | edge | cost | agg_cost 
-----+----------+------+------+------+----------
   1 |        1 |    7 |    6 |    1 |        0
   2 |        2 |    8 |    7 |    1 |        1
   3 |        3 |    5 |   10 |    1 |        2
   4 |        4 |   10 |   14 |    1 |        3
   5 |        5 |   13 |   -1 |    0 |        4
(5 rows)

Case 5: The path contains a shortcut.
We are going to route from 3 to 9
Since 3 and 9 both are in the contracted graph we need not add any vertices.
SELECT * FROM pgr_dijkstra($$
    SELECT id, source, target, cost, reverse_cost
    FROM edge_table
    WHERE source IN (3, 5, 6, 7, 8, 9, 11, 15, 17)
        AND target IN (3, 5, 6, 7, 8, 9, 11, 15, 17)$$,
    3, 9, false);
 seq | path_seq | node | edge | cost | agg_cost 
-----+----------+------+------+------+----------
   1 |        1 |    3 |   20 |    2 |        0
   2 |        2 |    9 |   -1 |    0 |        2
(2 rows)

SELECT id, source, target, cost, reverse_cost, is_contracted
FROM edge_table
WHERE is_contracted = true
ORDER BY id;
 id | source | target | cost | reverse_cost | is_contracted 
----+--------+--------+------+--------------+---------------
 19 |      3 |      5 |    2 |            2 | t
 20 |      3 |      9 |    2 |            2 | t
 21 |      5 |     11 |    2 |            2 | t
 22 |      9 |     11 |    2 |            2 | t
(4 rows)

This implies that it is a shortcut and should be expanded
SELECT * FROM pgr_dijkstra($$
    SELECT id, source, target, cost, reverse_cost
    FROM edge_table
    WHERE source IN (3, 5, 6, 7, 8, 9, 11, 15, 17)
        AND target IN (3, 5, 6, 7, 8, 9, 11, 15, 17)
        AND is_contracted=false$$,
    3, 9, false);
 seq | path_seq | node | edge | cost | agg_cost 
-----+----------+------+------+------+----------
   1 |        1 |    3 |    5 |    1 |        0
   2 |        2 |    6 |    9 |    1 |        1
   3 |        3 |    9 |   -1 |    0 |        2
(3 rows)

ROLLBACK;
ROLLBACK
