Find least colorful path in a graph

限于喜欢 提交于 2021-01-29 03:09:27

问题


The problem i'm trying to solve is this:

Given a graph G = (V,E) such that every edge is colored in one of 10 colors, and two vertices: s, t.

I need to find an algorithm that produces a (shortest) path from s to t, that goes over a minimal amount of colors.

My idea was to duplicate the graph 10 times:

The first duplicate will include only edges of one color

The second will include only edges of two colors... and so on.

Also, I connect an outer node: s' to every "s" node in every duplicate.

But, it has occurred to me that for this approach I need to duplicate the graph not 10 times but around 10! (or maybe even 2^10?) times for every combination of colors.

So what would be an efficient algorithm to solve this?


回答1:


I don't believe there's an easy algorithm to solve this, since the general form of the problem is NP hard. That is, in an arbitrarily colored graph, finding a shortest path between two vertices which touches a minimal set of colors is NP hard.

Thus, while it's possible there's slightly better algorithms, your idea of solving 1024 variants of the graph (one for each subset of of your 10 colors) is likely to be reasonable.

Proof

The proof works by reducing the hitting set problem to it. The hitting set problem is NP complete, so the reduction to your problem shows your problem is NP hard.

Recall that the hitting set problem takes sets X1...Xn, each with elements from some universe U and one is asked to find a minimal set {x1, ..., xk} such that for all i, there's a j such that xj in Xi.

The colors in the graph will be elements of U. Let the graph itself consist of n+1 vertices. These will be X0 (a start node, named only for notational convenience below) and vertices representing X1 ... Xn.

For each x in Xi+1, connect Xi to Xi+1 with an edge of color x.

Then in this graph, all paths from X0 to Xn have length n, but one that uses a minimal number of colors corresponds exactly a minimum hitting set.

Note that this expands the definition of graph to include multiple edges between nodes. If that's not ok then one add an extra node in the middle of each edge of the constructed graph.




回答2:


Edit: As suggested by Paul , the below approach won't work.

Try this approach, i am not sure about the correctness.

Start by merging nodes , which are same of same color and share a edge. This will shrink the graph which will then only contain edges (u->v) where u and v both have different colors .

After this give a constant weight to all the edges in the graph. Run dijkstra over the graph and keep track of the colors you have visited, and everytime you visit a new color , update the whole unvisited graph with new higher weights from visited to non-visted nodes which are not in the set of used colors.



来源:https://stackoverflow.com/questions/41919194/find-least-colorful-path-in-a-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!