0

I just saw a solution of a question that modifying Dijkstra to get the shortest path with a max of K coloured edge. I am wondering what if we want find the shortest path with coloured node instead of edge, how are we gonna modify Dijkstra to do the trick?

What I come up with is that on top of Dijkstra, I add an integer variable let say i. Then make a map to record how many coloured node it takes to get there, and if there is a way that passed through less coloured node, update it. And we will take the path with least coloured node. But this seems something is wrong, any suggestion?

 Algorithm  Dijkstra ( G , s in V(G), c(v) in{black, white}, K ) 
 1.  for  each vertex u in V(G)  do  dist[u] <- +infinity
 2.  dist[s] <- 0 ;   p[s] <- null 
 3. c(s)=black? r <- 1 : r <- 0
 4. Q <- ConstructMinHeap(V(G), dist) 
 5. M <- Map(s, r)               
 6.  while Q != null do 
 7.     u <- DeleteMin(Q)                           
 8.     for  each v in Adj[u]  do   
 9.         if M.value(u) is null then do
 10.                M <- Map(u, M.value(v) + c(u)=black? 1 : 0)
 11.            else
 12.                M.value(u) < (M.value(v) + c(u)=black? 1 : 0)? Update : do nothing
 13.            end-if
 14.             if   dist[v] >  dist[u] + w(u,v) and M.value < K then do 
 15.                dist[v] <- dist[u] + w(u,v)             
 16.                p[v] <- u                               
 17.                UpHeap(v,Q)             
 18.            end-if
 19.        end-for
 20.     end-while                          
 end 
swordgit
  • 115
  • 10

1 Answers1

0

If you use a priority queue to rank your options, consider using both the distance so far and the number of coloured nodes passed through to determine the order of priority. In this manner, you can use the traditional Dijkstra and let the determination of a minimum path be determined by your priority ranking.

Prof3sa
  • 9
  • 3