### Doubt-_-'s blog

By Doubt-_-, history, 6 weeks ago,

If you are given a weighted and directed graph, which can have multi-edges between nodes. Each edge will have a color associated with it (let it be i). Find the shortest path between node A and node B, if there are multiples shortest paths, print the shortest path with the least number of colors in it. What can be the optimized solution for this?

Anything better than finding all shorted paths and then iterating thru each one and finding different colors for each, and printing the one with the least number of colors..?

If there's a similar question on the internet and someone can provide me a link for that, it would be really helpful.

• +6

 » 6 weeks ago, # |   0 Auto comment: topic has been updated by Doubt-_- (previous revision, new revision, compare).
 » 6 weeks ago, # |   +18 If I understood correctly, even the unweighted version of the question you are asking is NP-hard. One can reduce the set cover problem to your problem quite easily:Consider an arbitrary instance of a set cover problem:Given $K$ subsets $S_1, S_2, ..., S_k \subseteq \lbrace 1, ..., n \rbrace$, choose as few as possible such that their union is $\lbrace 1, ..., n \rbrace$. Let's create a graph with $n+1$ vertices, and add for each $i$ and each $x \in S_i$ the arc $(x, x+1)$ of color $i$. The path with the minimum number of colors between $1$ and $n+1$ corresponds to the solution of the set cover problem. It is known that set cover is NP-hard, therefore this problem is also NP-hard.
•  » » 6 weeks ago, # ^ |   0 for a graph with weighted edges, simply exploring all the routes from node A to node B, and then for all the routes with min path sum, can't I check which path has the least number of colour switches? I do agree with your NP-hard explanation, but I was just confused why won't the brute force work in polynomial time..?
•  » » » 6 weeks ago, # ^ | ← Rev. 2 →   0 Consider a graph with $n + 1$ vertices and $2n$ edges, for each $i = 1..n$ there are two edges, that connect $i$ and $i + 1$. If you bruteforce all the paths from $1$ to $n + 1$, you will find $2^n$ paths, so the bruteforce would be exponentially slow.
•  » » » » 6 weeks ago, # ^ |   0 thanks a lot!!