Find a path from vertex s to vertex t with minimal number of color alternates - graph

Let be a directed graph, and let be an edge-coloring in red and blue. Let s,t be vertices in G. Find a path from s to t (if exists) so that the number of color changes along this path is minimal.
I have tried to do as follows:
Let be the graph obtained by removing all the
blue-colored edges of G. Let be the graph obtained
by removing all the red-colored of G.
Let
be the strongly connected graph of , computed
using this algorithm.
Let be the
strongly connected graph of , computed using
this algorithm.
Color the vertices of
in red, and color the vertices of
in blue.
Let be
the graph obtained by merging with
.
Define the weight of each (existing)
edge in G' as 0.
For each such that u
belongs to the strongly connected component and v
belongs to the strongly connected component do as
follows:
if and add edge to G' and
define its weight as 1.
Use Dijkstra algorithm to find a shortest
path from the blue strongly connected component of s, to both the
blue and red strongly connected components of t.
Use Dijkstra
algorithm to find a shortest path from the red strongly connected
component of s, to both the blue and red strongly connected
components of t.
Let p denote the shortest path among the four we
have just found. (namely, p has minimal number of color alternates). p is a series of strongly connected components.
Expand each of them using DFS, to find a corresponding path in G.
This algorithm can run in O(E+V*log(v)). Can it be improved or simplified?

I don't fully understand your algorithm, specifically in stage 4 you will color every vertex with two different colored edges in two colors - blue and red...
Therefor I will not try and improve your algorithm but will present one of my own - a variant of BFS with time of O(E + V).
The idea: Iterate over the edges of the graph and measure depth as the number of times you switched colors.
Note: We will run the algorithm twice, first assume that the first edge of the path is red, second assume that the first edge of the path is blue, than take the minimum.
Run BFS only on red edges starting from s (which is the first element in the BFS queue), if you viewed a vertex on a blue edge keep it in a different queue.
Mark all of the nodes you saw with the number i (at the beginning i=0).
Take the queue for the blue edges and make it your primary queue.
Run stages 1 to 3 but switch the colors and add 1 to i.
At the end the number in t is the minimal number of swaps performed to reach t.

Related

Different types of nodes in graph theory

I am trying to figure out whether a path in a graph has various branches or not. For example, this path does not:
But this branch does:
Degree of the orange node > 2. Is there a term to denote whether a graph or node is 'branchable' or not? It seems like this is sometimes called a "junction": https://www.quora.com/What-is-difference-between-a-node-and-junction-in-electrical-circuits
there is no such term yet you can determine whether a node is a root or a leaf. leaf nodes don't have successors such as the pink and red nodes in graph 2, they only have ancestors. in other words, they have incoming edges and no out coming edges (if we imply that it's a directed graph)

finding maximum weight subgraph

My graph is as follows:
I need to find a maximum weight subgraph.
The problem is as follows:
There are n Vectex clusters, and in every Vextex cluster, there are some vertexes. For two vertexes in different Vertex cluster, there is a weighted edge, and in the same Vextex cluster, there is no edge among vertexes. Now I
want to find a maximum weight subgraph by finding only one vertex in each
Vertex cluster. And the total weight is computed by adding all weights of the edges between the selected vertex. I add a picture to explain the problem. Now I know how to model this problem by ILP method. However, I do not know how to solve it by an approximation algorithm and how to get its approximation ratio.
Could you give some solutions and suggestions?
Thank you very much. If any unclear points in this description,
please feel free to ask.
I do not think you can find an alpha-approx for this problem, for any alpha. That is because if such an approximation exists, then it would also prove that the unique games conjecture(UGC) is false. And disproving (or proving) the UGC is a rather big feat :-)
(and I'm actually among the UGC believers, so I'd say it's impossible :p)
The reduction is quite straightforward, since any UGC instance can be described as your problem, with weights of 0 or 1 on edges.
What I can see as polynomial approximation is a 1/k-approx (k the number of clusters), using a maximum weight perfect matching (PM) algorithm (we suppose the number of clusters is even, if it's odd just add a 'useless' one with 1 vertex, 0 weights everywhere).
First, you need to build a new graph. One vertex per cluster. The weight of the edge u, v has the weight max w(e) for e edge from cluster u to cluster v. Run a max weight PM on this graph.
You then can select one vertex per cluster, the one that corresponds to the edge selected in the PM.
The total weight of the solution extracted from the PM is at least as big as the weight of the PM (since it contains the edges of the PM + other edges).
And then you can conclude that this is a 1/k approx, because if there exists a solution to the problem that is more than k times bigger than the PM weight, then the PM was not maximal.
The explanation is quite short (lapidaire I'd say), tell me if there is one part you don't catch/disagree with.
Edit: Equivalence with UGC: unique label cover explained.
Think of a UGC instance. Then, every node in the UGC instance will be represented by a cluster, with as many nodes in the cluster as there are colors in the UGC instance. Then, create edge with weight 0 if they do not correspond to an edge in the UGC, or if it correspond to a 'bad color match'. If they correspond to a good color match, then give it the weight 1.
Then, if you find the optimal solution to an instance of your problem, it means it corresponds to an optimal solution to the corresponding UGC instance.
So, if UGC holds, it means it is NP-hard to approximate your problem.
Introduce a new graph G'=(V',E') as follows and then solve (or approximate) the maximum stable set problem on G'.
Corresponding to each edge a-b in E(G), introduce a vertex v_ab in V'(G') where its weight is equal to the weight of the edge a-b.
Connect all of vertices of V'(G') to each other except for the following ones.
The vertex v_ab is not connected to the vertex v_ac, where vertices b and c are in different clusters in G. In this manner, we can select both of these vertices in an stable set of G' (Hence, we can select both of the corresponding edges in G)
The vertex v_ab is not connected to the vertex v_cd, where vertices a, b, c and d are in different clusters in G. In this manner, we can select both of these vertices in an stable set of G' (Hence, we can select both of the corresponding edges in G)
Finally, I think you can find an alpha-approximation for this problem. In other words, in my opinion the Unique Games Conjecture is wrong due to the 1.999999-approximation algorithm which I proposed for the vertex cover problem.

Pathfinding through graph with special vertices

Heyo!
So I've got this directed and/or undirected graph with a bunch of vertices and edges. In this graph there is a start vertex and an end vertex. There's also a subset of vertices which are coloured red (this subset can include the start and end vertices). Also, no pair of vertices can have more than one edge between them.
What I have to do is to find:
A) The shortest path that passes no red vertices
B) If there is a path that passes at least one red vertex
C) The path with the greatest amount of red vertices
D) The path with the fewest amount of red vertices
For A I use a breadth first search ignoring red branches. For B I simply brute force it with a depth first search of the graph. And for C and D I use dynamic programming, memoizing the number of red vertices I find in all paths, using the same DFS as in B.
I am moderately happy with all the solutions and I would very much appreciate any suggestions! Thanks!!
For A I use a breadth first search ignoring red branches
A) is a Typical pathfinding problem happening in the sub-graph that contains no red edges. So your solution is good (could be improved with heuristics if you can come up with one, then use A*)
For B I simply brute force it with a depth first search of the graph
Well here's the thing. Every optimal path A->C can be split at an arbitrary intermediate point B. A Nice property of optimal paths, is that every sub-path is optimal. So A->B and B->C are optimal.
This means if you know you must travel from some start to some end through an intermediary red vertex, you can do the following:
Perform a BFS from the start vertex
Perform a BFS from the endvertex backwards (If your edges are directed - as I think - you'll have to take them in reverse, here)
Alternate expanding both BFS so that both their 'edge' (or open lists, as they are called) have the same distance to their respective start.
Stop when:
One BFS hits a red vertex encountered by (or in the 'closed' list of) the other one. In this case, Each BFS can construct the optimal path to that commen vertex. Stitch both semi-paths, and you have your optimal path with at least a red vertex.
One BFS is stuck ('open' list is empty). In this case, there is no solution.
C) The path with the greatest amount of red vertices
This is a combinatorial problem. the first thing I would do is make a matrix of reachability of [start node + red nodes + end nodes] where:
reachability[i, j] = 1 iff there is a path from node i to node j
To compute this matrix, simply perform one BFS search starting at the start node and at every red node. If the BFS reaches a red node, put a 1 in the corresponding line/column.
This will abstract away the underlying complexity of the graph, and make an order of magnitude speedup on the combinatorial search.
The problem is now a longest path problem through that connectivity matrix. dynamic programming would be the way to go indeed.
D) The path with the fewest amount of red vertices
Simply perform a Dijkstra search, but use the following metric when sorting the nodes in the 'open' list:
dist(start, a) < dist(start, b) if:
numRedNodesInPath(start -> a) < numRedNodesInPath(start -> b)
OR (
numRedNodesInPath(start -> a) == numRedNodesInPath(start -> b)
AND
numNodesInPath(start -> a) < numNodesInPath(start -> a)
)
For this, when discovering new vertices, you'll have to store the path leading up to them (well, just the nb of nodes in the path, as well as the nb of red nodes, separately) in a dedicated map to be fetched. I mention this because usually, the length of the path is stored implicitly as the position of the verrtex in the array. You'll have to enforce it explicitely in your case.
Note on length optimality:
Even though you stated you did not care about length optimality outside of problem A), the algorithm I provided will produce shortest-length solutions. In many cases (like in D) it helps Dijkstra converge better I believe.

How to identified face from line drawing?

A line drawing is like a graph but its vertices have x,y position. There are no crossing edges. For example, a line drawing like this is a line drawing with 13 vertices numbered by 0-12. A face is a cycle that doesn't have a path that 'inside' it. Faces in the example would be
(0,1,3,2,0), (2,3,5,4,2), (4,5,8,7,4), (7,8,12,11,7) and (0,2,4,7,11,10,9,6,0)
The cycle (0,1,3,5,4,2,0) is NOT a face because there is a path that located inside it, named (2,3). Cycle (0,1,3,5,8,12,11,10,9,6,0) is also NOT a face because there is a path (0,2,4,7,11), located inside it. What algorithm can I use to identify faces like the ones in the example?
Assume all your edges are line segments; every planar graph can be drawn using only line segments. Also assume the graph is connected. Now the algorithm is pretty simple:
Construct a directed graph, such that the vertices are same as in the original graph and there's two directed edges for every original edge, one in each direction
Start with a random (directed) edge that's not been used yet. At its end, choose the next outgoing edge clockwise (or counterclockwise will do as well, just always the same). To decide which edge that is, you'll have to compute from the coordinates of vertices in the planar embedding. You'd better precompute this edge order for each vertex beforehand.
Keep doing that with the end of the selected edge, until you reach the starting vertex. At that point, you've completed a face.
When there's no unused edges, you've found all faces in the graph
Or, use a library like Boost, that has an efficient implementation of such task

Adjusting a MST to account for O(1) changes to edge weights

Let G = (V,E) be an undirected graph. Let w(e) be a weighting function with positive weights. Let T be a minimum spanning tree of G with respect to w.
Given a set of edges S, where S is a subset of E(G), define a new weighting function Q as Q(e) = w(e) if e is not in S, and Q(e) = w(e)+100 if e is in S. Design an algorithm that accepts as input, G, T, w and a set S, |S| = 10 and outputs a minimum spanning tree w.r.t. Q. Make it run in O(V+E) time.
Ok: what I've learned since I originally asked this question is that partitioning an MST is to "break apart" a single edge, which makes two separate components, each MST's of the vertices in their own components. So, in this problem, the edges in S might break up the MST into smaller MST's (11, right?). I have to find the lightest edges that connect one component to another.
My plan is to start at one vertex and expand with BFS until I cover an entire one of these components. For every u in the component, u.color = black. Then, I go back and cover the component again with BFS, this time finding all the edges that connect to vertices that are not colored black, therefore not contained within the component, and crossing the existing cut. The vertices opposite of these edges are placed in a Queue R. Once I am done, I u = RemoveMin(R), which is runs O(lgE). Since it will only be called every time I cover a component, it will run overall at a max of 10*O(lgE), which is still O(lgE). So once I remove u, I perform BFS on the new component, so that all u.color = black in that component. I go through all the black vertices again so that I may queue all the white vertices with updated keys into R. I do u = RemoveMin(R).
So I really think that this works and is proveable. Can anyone suggest something similar?
Any help, however small, would be appreciado.

Resources