How to extract relationships from shortest path algorithm

I have a graph like below.

And below code is used to create the above graph.

MERGE (a:Bus:GenBus{type:'GenBus',gencapacity:630,name:'NCTPS', bus_no:1, available_generation:630,status:'Not Connected'})
MERGE (b:Bus:GenBus{type:'GenBus',gencapacity:450,name:'ETPS', bus_no:2, available_generation:450,status:'Not Connected'})
MERGE (c:Bus:LoadBus{type:'LoadBus',load:285,name:'TONDAIYARPET', bus_no:3,status:'Not Connected'})
MERGE (d:Bus:LoadBus{type:'LoadBus',load:502,name:'MYLAPORE', bus_no:4,status:'Not Connected'})
MERGE (e:Bus:LoadBus{type:'LoadBus',load:275,name:'KTR', bus_no:5,status:'Not Connected'})
MERGE (f:Bus:LoadBus{type:'LoadBus',load:47,name:'KOYEMBEDU', bus_no:6,status:'Not Connected'})
MERGE (g:Bus:LoadBus{type:'LoadBus',load:27,name:'MOSUR', bus_no:7,status:'Not Connected'})
MERGE (h:Bus:LoadBus{type:'LoadBus',load:8,name:'SULURPET', bus_no:8,status:'Not Connected'})
MERGE (i:Bus:LoadBus{type:'LoadBus',load:336,name:'GUMIDIPONDI', bus_no:9,status:'Not Connected'})
MERGE (j:Bus:LoadBus{type:'LoadBus',load:17,name:'SRIPERAMBATHUR', bus_no:10,status:'Not Connected'})
MERGE (k:Bus:LoadBus{type:'LoadBus',load:260,name:'THARAMANI', bus_no:11,status:'Not Connected'})
MERGE (l:Bus:LoadBus{type:'LoadBus',load:73,name:'KADAPERI', bus_no:12,status:'Not Connected'})
MERGE (m:Bus:LoadBus{type:'LoadBus',load:50,name:'ARNI', bus_no:13,status:'Not Connected'})
MERGE (n:Bus:GenBus{type:'GenBus',gencapacity:440,name:'MTPS', bus_no:14, available_generation:440,status:'Not Connected'})
MERGE (o:Bus:LoadBus{type:'LoadBus',load:78,name:'SGPET', bus_no:15,status:'Not Connected'})
MERGE (p:Bus:LoadBus{type:'LoadBus',load:243,name:'SPKOIL', bus_no:16,status:'Not Connected'})
MERGE (q:Bus:LoadBus{type:'LoadBus',load:17,name:'TYLM', bus_no:17,status:'Not Connected'})
MERGE (r:Bus:GenBus{type:'GenBus',gencapacity:220,name:'NEYVELI', bus_no:18, available_generation:220,status:'Not Connected'})
MERGE (s:Bus:GenBus{type:'GenBus',gencapacity:440,name:'MAPS', bus_no:19, available_generation:440,status:'Not Connected'})


MERGE (a)-[:TRANSMISSION_LINE{line_no:1,length:18,available_cap:1000,status:'Not Connected'}]->(b)
MERGE (a)-[:TRANSMISSION_LINE{line_no:2,length:9,available_cap:1000,status:'Not Connected'}]->(c)
MERGE (a)-[:TRANSMISSION_LINE{line_no:3,length:7,available_cap:1000,status:'Not Connected'}]->(e)
MERGE (a)-[:TRANSMISSION_LINE{line_no:4,length:95,available_cap:1000,status:'Not Connected'}]->(g)
MERGE (a)-[:TRANSMISSION_LINE{line_no:5,length:39,available_cap:1000,status:'Not Connected'}]->(i)
MERGE (a)-[:TRANSMISSION_LINE{line_no:6,length:43,available_cap:1000,status:'Not Connected'}]->(j)
MERGE (b)-[:TRANSMISSION_LINE{line_no:7,length:10,available_cap:1000,status:'Not Connected'}]->(c)
MERGE (b)-[:TRANSMISSION_LINE{line_no:8,length:22,available_cap:1000,status:'Not Connected'}]->(e)
MERGE (b)-[:TRANSMISSION_LINE{line_no:9,length:104,available_cap:1000,status:'Not Connected'}]->(g)
MERGE (c)-[:TRANSMISSION_LINE{line_no:10,length:9,available_cap:1000,status:'Not Connected'}]->(d)
MERGE (e)-[:TRANSMISSION_LINE{line_no:11,length:8,available_cap:1000,status:'Not Connected'}]->(f)
MERGE (e)-[:TRANSMISSION_LINE{line_no:12,length:39,available_cap:1000,status:'Not Connected'}]->(j)
MERGE (f)-[:TRANSMISSION_LINE{line_no:13,length:32,available_cap:1000,status:'Not Connected'}]->(j)
MERGE (g)-[:TRANSMISSION_LINE{line_no:14,length:49,available_cap:1000,status:'Not Connected'}]->(j)
MERGE (g)-[:TRANSMISSION_LINE{line_no:15,length:55,available_cap:1000,status:'Not Connected'}]->(q)
MERGE (h)-[:TRANSMISSION_LINE{line_no:16,length:39,available_cap:1000,status:'Not Connected'}]->(i)
MERGE (i)-[:TRANSMISSION_LINE{line_no:17,length:82,available_cap:1000,status:'Not Connected'}]->(j)
MERGE (j)-[:TRANSMISSION_LINE{line_no:18,length:50,available_cap:1000,status:'Not Connected'}]->(k)
MERGE (j)-[:TRANSMISSION_LINE{line_no:19,length:24,available_cap:1000,status:'Not Connected'}]->(l)
MERGE (j)-[:TRANSMISSION_LINE{line_no:20,length:103,available_cap:1000,status:'Not Connected'}]->(m)
MERGE (j)-[:TRANSMISSION_LINE{line_no:21,length:47,available_cap:1000,status:'Not Connected'}]->(p)
MERGE (m)-[:TRANSMISSION_LINE{line_no:22,length:97,available_cap:1000,status:'Not Connected'}]->(o)
MERGE (m)-[:TRANSMISSION_LINE{line_no:23,length:45,available_cap:1000,status:'Not Connected'}]->(q)
MERGE (m)-[:TRANSMISSION_LINE{line_no:24,length:128,available_cap:1000,status:'Not Connected'}]->(s)
MERGE (n)-[:TRANSMISSION_LINE{line_no:25,length:133,available_cap:1000,status:'Not Connected'}]->(o)
MERGE (o)-[:TRANSMISSION_LINE{line_no:26,length:140,available_cap:1000,status:'Not Connected'}]->(q)
MERGE (p)-[:TRANSMISSION_LINE{line_no:27,length:91,available_cap:1000,status:'Not Connected'}]->(q)
MERGE (q)-[:TRANSMISSION_LINE{line_no:28,length:183,available_cap:1000,status:'Not Connected'}]->(r)

I want to find shortest path between GenBus nodes to LoadBus nodes based on TRANSMISSION_LINE property length.
I want to extract the TRANSMISSION_LINEs of that shortest path and set their property status:Connected and update available_cap = available_cap - load. Here load is load of the LoadBus

MATCH (s:GenBus{bus_no:14}), (l:LoadBus{bus_no:16})
CALL algo.shortestPath.stream(s, l, "length")
YIELD nodeId,cost
with collect(nodeId) as q ,l
MATCH path = (a)-[r:TRANSMISSION_LINE]-(b)
where id(a)in q and id(b) in q
return path

Note: I have unchecked the connect result nodes option in settings

If i run the above code, it gives output as below.

Here the shortest path is 14 ->15 -> 17 -> 16 . So, it's ok.

But for the case of shortest path between GenBus1 to LoadBus17 , it become problem.
Below is the code. I just changed the numbers in above code.

MATCH (s:Bus{bus_no:1 }), (l:Bus{bus_no: 17})
CALL algo.shortestPath.stream(s, l, "length")
YIELD nodeId,cost
with collect(nodeId) as q ,l
MATCH path = (a)-[r:TRANSMISSION_LINE]-(b)
where id(a)in q and id(b) in q 
return path

Output:

In this case the shortest path is 1 -> 10 -> 7 -> 17. But It is showing extra relationship between node1 and node17.

How to get the correct output?
I mean it should not show that extra relationship.

I am very new to neo4j.

The shortestPath algorithm does not support returning a path directly, but if you use the kshortestPaths algorithm, and set path:true in the configuration, you can return a path without any further cypher wrangling:

MATCH (start:Loc{name:'A'}), (end:Loc{name:'D'})
CALL algo.kShortestPaths.stream(start, end, 3, 'cost', {path: true})
YIELD path
RETURN path
LIMIT 1

See the documentation here for more details: https://neo4j.com/docs/graph-algorithms/current/labs-algorithms/yen-s-k-shortest-path/

The reason your final query isn't working is because the MATCH statement is looking for any node that was returned by your shortest path algorithm call (source or target) with a TRANSMISSION_LINE relationship to any other node returned by your shortest path algorithm. Two of the nodes in your path have a transmission line relationship between them, so you get an extra relationship.

Thanks for the reply madam. I tried the following code.

MATCH (s:Bus{bus_no:1}),(l:Bus{bus_no:17})
CALL algo.kShortestPaths.stream(s,  l,  1,  'length' , {path:True})
YIELD path
return path

Output:

What it is doing is, it is creating some new relationships and showing length values in it. If we see the above picture, it is not showing the properties like line_no , available_cap , status , and also length is shown as cost. The relationships in the output are not original. But the nodes are original.
I want to set property status = 'Connected' to the path relationships.
Is there any way to extract the original relationships ?

And one more thing is, if i execute the following code,

MATCH (l:Bus{bus_no:17}), (s:Bus{bus_no:1})
CALL algo.kShortestPaths.stream(s, l, 3, 'length' ,{})
YIELD index, nodeIds, costs
RETURN [node in algo.getNodesById(nodeIds) | node.bus_no] AS Bus_no,
       costs as length,
       reduce(acc = 0.0, cost in costs | acc + cost) AS total_length

output is:

For the second path, it is showing less distance than first path. Actually in between bus_no 1 and bus_no 7 length is 95. But it is taking length = 18. I am using windows 10 OS,i5 procesor. When execute the same command in another pc with ubuntu OS, it is showing correctly(second path is [1,7,17] ,length is [95,55] and totalCost is 150).
Is there any bug for this algorithm for windows? or is there anything wrong in code?

I found solution for it.

MATCH (start:Loc{name:'A'}), (end:Loc{name:'D'})
CALL apoc.algo.dijkstra(start, end, 'TRANSMISSION_LINE','length')
YIELD path, weight
RETURN path, weight
1 Like