1

Let's say i have a directed graph G(V,E) with positive integer weights on it's edges.What i need to do is find the minimum distance between some of it's nodes.In this minimum distance path i can use at most K reverse edges. For example, for this input:

6 (nodes)

9 (edges)

2 (positive integer K)

10 (number of edge pairs i need to find the minimum distance)

2 1 2 (edge for 2->1 with weight 2)

3 2 7 (edge for 3->2 with weight 7)

4 5 6

1 3 8

1 4 4

5 2 8

5 6 10 (edge 7)

1 5 5 (edge 8)

4 2 5 (edge 9)

1 6 1 (question 1:find minimum distance from 1->6 using 1 reverse edge)

3 5 0 (question 2:find minimum distance from 3->5 using 0 reverse edge)

1 2 0

3 5 1

1 2 1

4 3 1

6 4 0

2 6 2

6 4 1

6 4 2 (question 10)

The different parts of the input can be separated using the number of edges(9,first 9 lines containing 3 numbers) and numbers of questions asked(10,the 10 lines following after the edges)

Output has to be:

15 (Answer to question 1:minimum distance from 1->6 using 1 reverse edge is 15)

14

9

13

2

12

IMPOSSIBLE (there is no path between these two edges using 0 reverse edges)

17

24

16

I thought about running Dijkstra for each question and for each edge instead of just calculating the single minimum distance from source use reverse edges too and updating the value as much as possible without using more than K reverse edges.(The label would be something like (node number,minimum distance from source,reverse edges used).But dijkstra finds shortest paths from source to all other nodes and i think this is probably an overkill for my instance.Could this somehow be done more efficiently?

  • "*But dijkstra finds shortest paths from source to all other nodes*" -- there are variants (including the original) which find the shortest part between two nodes. – canton7 Mar 03 '19 at 16:28
  • I don't think you're going to get better than Dijkstra's, with a modification to allow your reverse edges. – canton7 Mar 03 '19 at 16:46
  • Possible duplicate of [All shortest paths in a graph using K reverse edges](https://stackoverflow.com/questions/54754866/all-shortest-paths-in-a-graph-using-k-reverse-edges) – גלעד ברקן Mar 03 '19 at 19:02

1 Answers1

1

You can run dijkstra on each node considering the graph as undirected graph. You need to keep track of minimum distance and the number of reversed nodes used. For example,let's take starting node to be 3.

Suppose we need to find (3 1 0) and (3 1 1) in the question.

We initialize node 3 as (0,0) and all other nodes as (INFINITY,0). From node 3, we can go to either node 2 in straight direction or node 1 in reverse direction. So we get

Node 1(8,1) and Node 2(7,0)

From Node 2, we go to Node 1 in straight direction. Hence we get

Node1(8,1)(9,0). This means Minimum value from node 3 to node 1 is 9 if 0 edges are reversed and 8 if 1 edge is reversed.

For each Node you can track these in HashMap and the answer would be Minimum distance between 0 to k. For example we may get Node2(7,0)(10,2). here the answer will when two edges are allowed to be reversed is still 7 as 10>7.

The complexity of this algorithm will be O(n*(n+m)).

P.Gupta
  • 485
  • 4
  • 18
  • How would i know if an edge is reversed though?Could i name for example edge from 3 to 1 as 31 and if i am at node 1 know that this edge is normaly 3->1 thus crossing it makes it reversed? –  Mar 03 '19 at 17:05
  • You can use two separate adjacent lists to keep track of in and out edges. If your graph contains an edge 1 to 3, then one list will contain edge 1->3 and the other will contain 3 ->1 – P.Gupta Mar 03 '19 at 19:24