algorithm - Finding the shortest path in a colored graph -


the problem trying solve is:

given graph each edge colored in either red or blue:

a) give algorithm produces path between 2 vertices (s,t) goes on minimal amount of red edges.

b) give algorithm produces path between 2 vertices (s,t) goes on minimal amount of blue edges, out of paths go s t pass through minimal amount of red edges.

so far: a) can use modified bfs algorithm. when looking @ vertex, v, put vertices connected blue edge v first in queue, , add rest end of queue. thus, first time algorithm encounter t path in question.

how prove correctness of algorithm? seems greedy. how expand answer b)?

thanks time.

there 2 important details algorithm need keep in mind:

  1. nodes can added queue multiple times in scenario. allowed explore them once, though. can enforce maintaining boolean flag per node tells whether have explored node.
  2. you can stop algorithm take t out of queue, not put in.

if way (and question doesn't indicate don't), algorithm indeed correct.

how prove correctness of algorithm?

if assign edge weight 1 red edges , edge weight 0 blue edges, problem reduces find shortest path in transformed graph. let's apply dijkstra problem. show algorithm in fact implements dijkstra, proves correctness.

we can show ever have nodes of 2 different distances in priority queue: if m minimum distance of node in priority queue, can't have node of distance m + 2 in queue, because mean must have explored node distance m + 1, impossible because explore nodes in order of increasing distance.

your modified bfs in fact implements dijkstra algorithm using double-ended queue 2-value priority queue: if q queue, there index i, such q[1..i] contains nodes of distance m , q[i+1..] contains nodes of distance m + 1.

how expand answer b)?

you can extend concept maintaining 4-value priority queue, example implemented 2 double-ended queue. 1 queue hold nodes of distance m red edges, other hold nodes of distance m + 1 red edges. both ordered increasing number of blue edges (there 2 different distance values in each of these).


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -