Find a minimum distance with a number of even green edges - algorithm

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:

Related

Dynamic Programming - Edges difference problem

I just started learning about Dynamic Programming, and in my assignment I was required to describe an algorithm that solves the following problem:
Let G be a directed graph, where every edge is colored either green or yellow, and a source vertex s. For every vertex in V, find a path that the difference between the number of green and yellow edges is minimal. That algorithm needs to be O(|V|+|E|) complexity for each vertex.
Input: Given a directed graph G=(V, E), a source vertex s in V and edges colored with this coloring function: For every edge e in the graph, c(e)->{yellow, green}. There is a path to every vertex v in V from the source vertex s.
The weight of a path 'P' in the graph, is the difference between the number of green edges and the number of yellow edges in the graph
Output: For every vertex v in V, return the minimal weight of a path P from s to v.
So I started thinking about a solution to this problem. I realized that I can exchange the colors with numbers, -1 and 1 and work from there, but so far I haven't been able to find a solution.
My most fleshed out solution looks at the group of neighbors of target a vertex and does the following:
Find the minimum weight between:
The sum of the "weight" of the edge (-1, 1) leading to a vertex 'k' from the current vertex's neighbor group, and the optimum of 'k'.
The optimum of the other neighbors, excluding the vertex 'k'.
terminate when reaching s or the neighbors group is empty.
However, this solution doesn't feel right, as it doesn't look at a vertex - but at a group of vertices, and I don't think it meets the complexity demands. I tried writing the formula: image
I tried experimenting with solutions that start from the vertex s and end at a target vertex v, but I was unable to reach a solid termination condition. For example, it can't terminate when reaching v, since there might be cycles involving v that result in a minimum result.
I was hoping to get some guidance on this problem, thanks in advance!
This doesn't necessarily strike me as a case for dynamic programming, so maybe I've missed something and there's a quicker dynamic solution, but you can achieve an O(VE) running time using a modified Bellman-Ford. This asymptotic time bound is not the same as O(V + E) per vertex! But for a connected graph (since the source can reach all the vertices) it is the same overall.
I'll leave you to look up the details of the Bellman Ford algorithm (for single source shortest paths), in the spirit of it being an assignment, but suffice it to say that your vertices could store a positive score for more yellow than green, a negative score for the opposite, and their weight in either case would be the absolute value of this attribute. Your 'relax' function would then operate as follows, assuming we use a score attribute on each vertex (this score is not the weight, it can be positive or negative):
RELAX(u, v):
new_score = u.score
if the edge u -> v is green:
new_score -= 1
else:
new_score += 1
if abs(new_score) < v.score:
v.score = new_score

Removing max edges while keeping minimum distance

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.

Algorithm for dividing graph into edge pairs

I've received a task to find an algorithm which divides a graph G(V,E) into pairs of neighboring edges (colors the graph, such that every pair of neighboring edges has the same color).
I've tried to tackle this problem by drawing out some random graphs and came to a few conclusions:
If a vertex is connected to 2(4,6,8...) vertices of degree 1, these make a pair of edges.
If a vertex of degree 1 is directly connected to a cycle, it doesn't matter which edge of the cycle is paired with the lone edge.
However, I couldn't come up with any other conclusions, so I tried a different approach. I thought about using DFS, finding articulation points and dividing graph into subgraphs with an even number of edges, because those should be dividable by this rule as well, and so on until I end up with only subgraphs of |E(G')| = 2.
Another thing I've come up with is to create a graph G', where E(G) = V(G') and V(G) = E(G'). That way I could get a graph, where I could remove pairs of vertices (former edges) either via DFS or always starting with leaf vertices along with their adjacent vertices.
The last technique is most appealing to me, but it seems to be the slowest one. Any feedback or tips on which of these methods would be the best is much appreciated.
EDIT: In other words, imagine the graph as a layout of a town. Vertices being crossroads, edges being the roads. We want to decorate (sweep, color) each road exactly once, but we can only decorate two connected roads at the same time. I hope this helps for clarification.
For example, having graph G with E={ab,bd,cd,ac,ae,be,bf,fd}, one of possible pair combinations is P={{ab,bf},{ac,cd},{ae,eb},{bd,df}}.
One approach is to construct a new graph G where:
A vertex in G corresponds to an edge in the original graph
An edge in G connects vertices a and b in G where a and b represent edges in the original graph that meet at a vertex in the original graph
Then, if I have understood the original problem correctly, the objective for G is to find the maximum matching, which can be done, for example, with the Blossom algorithm.

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.

Find path between 2 vertices with x red vertices

I've tried a couple of approaches and they all ended in a dead end.
The question:
Given a G=(V,E) a directed unweighted graph in which every vertex is colored red or blue.
Let there be vertices s and t from V. Describe an algorithm that would find a path between s and t which would hold the least red vertices. Explain the algorithm, prove it and show its complexity.
I've thought of 2 approaches and discarded both:
Use an adjacency list sorted by colors (blue first red last) and run a DFS - bad idea
Set the weight of each edge from a red vertex to 2 and blues to 1 and run Dijkstra - found a counterexample
I would really be happy to get some help with the right direction. I prefer NOT to get full answers but rather hits/tips.
Consider setting weight=1 for any edge that goes to a red vertex. Then show that the cost of a path from s with n red vertices is either n or n-1, depending on the color of s.
Well I've found an answer to this question, not me but a friend of mine after I showed him the weighted way to do it.
Instead of using one queue in the BFS algorithm we will use 2 queues: one for the blue vertices and one for the red vertices.
we check what color is S and add it to the right queue, as long as queue of the blue vertices is not empty we dequeue from it and continue the regular BFS if we encounter a red vertex we enqueue it to the red queue and if we find a blue vertex we enqueue it to the blue queue.
if the blue queue is empty we dequeue from the red queue.
eventually the array pi[|V|] will hold a backwards representation to vertex v with least of red vertices.
since there is no real change to the BFS algorithm and not to it's data structures the proof of correctness holds and the time complexity is O(|V|+|E|) as in BFS.
thanks for the help guys!

Resources