Removing max edges while keeping minimum distance - algorithm

Suppose we have a graph with vertices from 1 to n.The graph is undirected and the starting point is 1 and we have path from 1 to any other vertex.We also have positive weight on each edge and there are two types of edges - black and red.
The black edges are in the form (1,x) where x is a vertex and red edges can be any pair (x,y) .My question is how can I find the maximum number of black edges I can remove so that the minimal distance from 1 to any other vertex is preserved?

Something like this. Using your favorite path-finding algorithm, find a shortest path from 1 to each other vertex using a cost function of {total weight, number of black edges}. This would produce paths that prefer red edges, other things equal.
You can now remove all black edges that don't belong to any path.

Related

Find a minimum distance with a number of even green edges

Hi I'm a computer science student, in my second year. During my studies, I got stuck with a question I couldn't solve, a question I was exposed to in order to expand my knowledge.
Question: There is an undirect graph, with edges of positive weight, I have to find I am the minimum distance between, in addition The graph has 2 types of edges - blue and green. I need to find a minimum distance between and also the number of its green edges in the tree is even.
I was thinking of an algorithm based on the Dijkstra algorithm.
Let's start from s
Each time we go to the bow with the minimum number.
If we have to go to a green vertex - right after that we try to go - to another green vertex.
I tried to draw my idea but it didn't work properly.
Why doesn't my idea work properly? what am I missing? Thanks for the help.
I would construct a new graph.
For each vertex i in the original graph, construct two vertices in the new graph numbered 2i and 2i+1.
For each blue edge i to j, construct edges 2i to 2j and 2i+1 to 2j+1.
For each green edge i to j, construct edges 2i to 2j+1 and 2i+1 to 2j.
Then Dijkstra's algorithm on the new graph from 2i to 2j will tell you the shortest distance from vertex i to j with an even number of green edges. (2i to 2j+1 will tell the shortest distance with an odd number of green edges.)
The idea is that we switch from the even graph to the odd graph whenever we traverse a green edge.
Your idea sounds like it only considers paths with two consecutive green edges. This will probably work for some graphs, but not all as in some case the optimum route may not include consecutive green edges.
UPDATE
For the graph in your comment:
the new graph looks like:

the shortest even path to minimum weight perfect matching

Given an edge weighted undirected graph and two vertices s and t, weights are non-negative.
Shortest Even Path Problem is to find an s,t-path P having an even number of edges whose total weight is as small as possible. How to reduce Shortest Even Path Problem to minimum weight perfect matching problem
Start with the given graph, imagine painting the nodes blue, and call it Gblue. It has nodes, including sblue and tblue, and undirected edges like ablue <-> bblue.
Make a copy of the graph, paint its nodes green and call it Ggreen.
Now reconnect all of the edges, so that ablue<->bblue and agreen<->bgreen (which have equal weights) become ablue<->bgreen and agreen<->bblue (which have the same weights).
In this combined graph, every edge is between a blue node and a green node, so every path alternates between blue and green. So every path from sblue that has a even number of steps, will end on a green node.
Now on this combined graph, seek the minimum-weight path from sblue to tgreen.
Finally, remove the subscripts.

Shortest paths with 2 constraints (Weight and Colour)

Input: We have a directed graph G=(V,E) and each edge has a weight and a colour {red,green}. We are also given a starting node s.Problem/Algorithm: Can we find for all u edges of G, the shortest paths s-u with at most k red edges ? First approach: We save for each node the shortest path with 0,1...k red edges. We modify Dijkstra's algorithm and depending on the colour of the edges we are looking into, we update the distances respectively. This approach fails due to its complexity. Second approach: We make k copies of G graph (G1,G2 ...Gk+1). In order to utilise the k red edges constraint, while we are searching for shortest paths with Dijkstra, every time we "meet" a red edge {ui,vi} in Gi, we connect ui with vi+1 in Gi+1. Because Gk+1 doesn't have any red edges, we can only reach Gk+1 with at most k edges.But it fails. For example with k=2 if a 2 red edges shortest path is found to X node then will not take into consideration a heavier path with less red edges which could lead to an undiscovered node. (If i had enough reputation i could post an image as example). Any ideas ?
I think your approaches are actually equivalent, provided that for approach #1, you record only the shortest distance to each node for each number of red edges used -- you don't need to record the entire path (just as you don't need to record it for ordinary Dijkstra on an ordinary shortest path problem)
Also this approach is sound. In particular, your reasoning that approach #2 is faulty is itself wrong: for any node X in the original graph, there is no single corresponding node X in the new graph; instead there are separate vertices for each number of red edges used. So the two paths "to X" you are considering are not actually to the same node: one is to (X, 2 red edges used) and one is to e.g. (X, 1 red edge used). Then you can use a single Dijkstra run to calculate shortest paths to all k+1 copies of every vertex (i.e. to the vertices (v, i red edges used) for each 0 <= i <= k and for each v in V(G)), and return the lowest. (I'm assuming here that when you wrote "Can we find for all u edges of G, the shortest paths s-u", you meant "for all nodes u of G, the shortest paths s-u".)
Finally, you need to make sure that for any red edge {u, v} in G, you delete the corresponding edge {ui, vi} for all Gi (as well as add in the edge {ui, vi+1}). You probably intended this, but you weren't explicit about it.

Finding a spanning tree using exactly k red edges in a graph with edges colored by red/blue in linear time

Given a graph G with red and blue edges and a constant K, devise a deterministic, linear time algorithm that finds a spanning tree of G with exactly K red edges (or returns False if such a spanning tree does not exist).
What we have done so far:
Let every red edge have weight of -1 and every blue edge have weight of 0.
Find a minimum spanning tree (using a standard linear time algorithm). So, we have a spanning tree T with minimal weight, meaning we used as many red edges as we can because red edges will only decrease the weight.
If there are less than K red edges in T, we return False.
If there are exactly K red edges, we are done, T is the answer.
If there are more than K red edges, we need to replace them with blue ones.
This is our problem, how do we do that in linear time?
Every blue edge added will create a cycle, so removing one red edge from the cycle will work but how can we ensue linearity in this way? Is this even a good approach?
HINTS
You can do this in linear time using two passes of Prim's algorithm. (Usually Prim's algorithm is not linear time, but when you only have two types of edge you do not need to spend time sorting edges).
Pass 1
In the first pass follow blue edges before red edges and mark any red edges that you are forced to take.
If you mark C edges in this process then we know that there must be at least C red edges in any spanning tree solution, so if C>K, it is impossible.
Pass 2
Suppose we have found C ( < K ) marked edges in the first pass.
In the second pass follow red edges before blue, until the total number of additional red edges reaches K-C. In the second pass you are also allowed to follow the red edges marked in the first pass (and these do not count towards your total).
If your additional red edges does not reach K-C then it is impossible.

finding shortest valid path in a colored-edge graphs

Given a directed graph G, with edges colored either green or purple, and a vertex S in G, I must find an algorithm that finds the shortest path from s to each vertex in G so the path includes at most two purple edges (and green as much as needed).
I thought of BFS on G after removing all the purple edges, and for every vertex that the shortest path is still infinity, do something to try to find it, but I'm kinda stuck, and it takes alot of the running time as well...
Any other suggestions?
Thanks in advance
Duplicate your graph, so you have G1, G2, G3, such in each graph you redirect purple edges as follows: if (u1, v1) is a purple edge in G1, then change it to (u1, v2).
I.e. You have three graphs where each time you take a purple edge it moves you to the next graph, and on G3 there are no more purple edges. That construction builds the purple restriction into the structure, and will then be enforced automatically.
Now you simply find the shortest path from s to all nodes as usual. With the results you have to choose which is shorter between each path from s to u1, u2, u3.
All in all the construction takes linear time, and the new graph size is O(1) times larger than the original, so APSP takes the same time, and the final run to determine which of the three paths found is shortest is also linear.
You need three shortest paths to each vertex: paths with 0, 1 and 2 purple edges. In your algorithm, you need to store three different path lengths at each vertex. When you consider an s-t path with k purple edges and an edge t-u, if the edge is green then the potential s-t-u path has k purple edges, and if the s-t-u path is shorter than other s-u paths with k purple edges, you store its length at slot k of vertex u. Alternatively, if the t-u edge is purple, you work with slot k+1 of vertex u. Can you work out the rest by yourself?
Note that any path without loops in a graph like yours with n vertices can be at most n-1 long.
Assign length n to each purple edge where the n is the number of vertices. Find the shortest path from S to every other vertex, using, e.g. the Dijkstra algorithm.
Now, examine the path lengths:
if the path length is less than n, then you have a path of only green edges, since even one purple edge will increase the path length over n-1;
if the path length is inside [n, 2n), then you have exactly one purple edge, since two or more purple edges will increase the path length over 2n-1;
if the path length is inside [2n, 3n), then you have exactly two purple edges, since three or more purple edges will increase the path length over 3n-1;
otherwise a path with at most two purple edges does not exist.

Resources