Can someone please explain how given an undirected graph G = (V; E); edge lengths le > 0; and edge edges in E.
We can generate the length of the shortest cycle containing edge e.
I understand how to do this in directed graphs, but im not sure how to approach the problem with an undirected graph.
Without modifying the graph: Let e be an edge (u, v). Choose one of the two nodes—I'll choose u—and run an ordinary Dijkstra/BFS starting from u with one minor modification: When making the first hop, you must not add v to the queue. Now search for v.
Related
I have to solve this problem and it has been bugging me for hours and I can't seem to find a valid solution that satisfies the time complexity required.
For any edge e in any graph G, let G\e denote the graph obtained by deleting e from G.
(a)Suppose we are given an edge-weighted directed graph G in which the shortest path σ from vertex s to vertex t passes through every vertex of G. Describe an algorithm to compute the shortest-path distance from s to t in G\e, for every edge e of G, in O(VlogV) time. Your algorithm should output a set of E shortest-path distances,one for each edge of the input graph. You may assume that all edge weights are non-negative.[Hint: If we delete an edge of the original shortest path, how do the old and new shortest paths overlap?
(b) Describe an algorithm to solve the replacement paths problem for arbitrary undirected graphs in O(V log V ) time.
a) Consider the shortest path P between vertices s and t. Since P is a shortest path, there is no edge between any two vertices u and v in P in which the length of shortest path between u and v is bigger than 1 in the induced graph P. Since every vertex in G is present in P, So every edge of G is present in the induced graph of P. So we conclude that every edge in G is between two adjacent vertices in P (not the induced graph of P). The only thing you should check for each edge e which connects vertices u and v in G, is that there is another edge which connects u and v in G. If so, the shortest path doesn't change in G/e, otherwise s and t will lie on different components.
First of all I want to mention that the complexity can't only depend on V since the output should contain E values, therefore I guess it should be O(E + Vlog(V)).
I think the following idea should work for problem b) and thus for a) as well. Let σ be the shortest s-t path.
For every edge e in G/E(σ), the shortest path in G/e is still σ.
If we remove an edge e in σ, then the shortest path in the remaining graph would like this: it would start going along σ, then would continue outside (might be from the very first vertex s, then go back to σ (might be at the very last vertex t). Let's then iterate over all edges e=(u,v) that either go from G/σ to σ, or from one vertex of σ, that is closer to s, to another vertex of σ, that is closer to t, as edges of potential candidate paths for some shortest s-t path in G/e' for some e'. Suppose that w is the last vertex in the shortest s-u path U that belongs to σ. Then U+e+σ[v..t] is a potential candidate for a shortest s-t path in graphs G/e' for all e' in σ[w..v]. In order to efficiently store this candidate, we can use a Segment Tree data structure with min operation. This would give us O(E log(E)) total complexity for all updates after we consider all edges. It might be possible to make it O(Vlog(V)) if we look closer at the structure of the updates, but I am not sure at the moment, might be not. Precomputing vertices w for each shortest s-u path above can be done in a single Dijkstra run, if we already have σ.
So in the end the solution runs in O(Elog(E)) time. I am not sure how to make it O(E + Vlog(V)), it is possible to make Dijkstra to run in this time by using Fibonacci heap, but for the updates for candidates I don't really see a faster solution at the moment.
Consider the problem of Disjoint Hamiltonian Path:
Input: A graph which may be directed or undirected
Output: Does this graph exist at least 2 Hamiltonian Paths that are edge-disjoint?
Edge-disjoint means that no single edge is shared by two paths.
Show that Disjoint Hamiltonian Path is np-complete.
I have been told that this problem is np-complete, but I couldn't prove it is np-hard. I tried reducing the original Hamiltonian Path and Hamiltonian Cycle to this problem but I couldn't think of a solution.
I came up with the following reduction, not sure if it's the simplest but it is simple.
Suppose G is an undirected graph corresponding to an instance of HP.
Now construct a new graph G' in the following way:
Keep every vertex from G.
For every edge (u,v) in G, create 4 additional vertices and connect them in the following way :
Now it is easy to see that if G has a Hamiltonian path, G' will have two edge-disjoint Hamiltonian paths, because every edge was replaced by some subgraph which itself has two edge-disjoint Hamiltonian paths (go straight or take the curvy edges). And if G' has a HP, then so does G because once you enter the subgraph corresponding to one of the original edges you have no choice but to get out of it on the other end, which corresponds to taking the original edge in G. The only "problem" that could occur is if the path were to start or end inside one of these subgraphs, but then we can just ignore the small part of the path which is inside and still get a HP for G.
And notice that G' has a HP => G has a HP => G' has two edge-disjoint HPs. Thus, G has a HP <=> G' has two edge-disjoint HPs.
The transformation can obviously be done in poly-time, thus your problem is NP-Hard.
The directed case is similar, just direct the edges in the transformed graph accordingly.
A strongly connected digraph is a directed graph in which for each two vertices 𝑢 and 𝑣,
there is a directed path from 𝑢 to 𝑣 and a direct path from 𝑣 to 𝑢. Let 𝐺 = (𝑉, 𝐸) be a
strongly connected digraph, and let 𝑒 = (𝑢, 𝑣) ∈ 𝐸 be an edge in the graph.
Design an efficient algorithm which decides whether 𝐺
′ = (𝑉, 𝐸 ∖ {𝑒}), the graph
without the edge 𝑒 is strongly connected. Explain its correctness and analyze its running
time.
So what I did is run BFS and sum the labels, once on the original graph from 𝑢 and then again in G' without the edge (again from 𝑢)
and then : if second sum (in G') < original sum (in G) then the graph isn't strongly connected.
P.S this is a question from my exam which I only got 3/13 points and I'm wondering if i should appeal..
As Sneftel points out, the distance labels can only increase. If u no longer has a path to v, then I guess v's label will be infinite, so the sum of labels will change from finite to infinite. Yet the sum can increase without the graph losing strong connectivity, e.g.,
u<----->v
\ /|
\| /
w
where v's label increases from 1 to 2 because of the indirect path through w.
Since the graph G is strongly connected, G' is strongly connected if and only if there is a path from u to v (this path would replace the edge e).
You can use any path finding algorithm to solve this problem.
Given a weighted undirected graph G and two nodes U,V to get the shortest path. How can i get the shortest path from U to V that uses a even number of edges (if possible to get it) ?
I've found some articles on the web speaking that a modification on the original graph is necessary. But i can't understand how to do it.
There is some good material to study on this problem ?
You'll need to build an intermediate graph and run Dijkstra's on that graph.
Given a graph G = (V, E), create a new graph G' = (V', E'), with V' a new set of vertices v_even and v_odd for every vertex v in V and E' the set of vertices as follows:
If (u, v) is an edge in G, then (u_odd, v_even) and (u_even, v_odd) are edges in G', with the same weight.
Obviously, the new graph has twice as many edges and vertices as the original graph.
Now, if you wanted to find the shortest path between s and t in G, simply run Dijkstra's on G' to find the shortest path between s_even and t_even.
The running time is still O(|V| log |E|).
Make a copy of your graph with the same weights and call it G'.
Connect every vertex of G to the corresponding vertex in G' and set the weight of the new edges to zero.
Delete copy of u from G' and delete v from G.
Now, the set of edges you added between G and G' constitute a matching M. Take that matching and find a minimum augmenting path for that matching.
Such a path must have u as one of its end points and copy of v as the other endpoint, because they are the only uncovered vertices. If you merge back the copies and get rid of the added edges, the path you found corresponds to an even path, because it starts at one copy and ends at another. Also, every even path corresponds to an augmenting path (by same argument), therefore the minimum of one is also the minimum of the other. This is explained in here.
What about running Dijkstra where each node has two values. One is odd (coming from even value) and other is even value.
Let G (U u V, E) be a weighted directed bipartite graph (i.e. U and V are the two sets of nodes of the bipartite graph and E contains directed weighted edges from U to V or from V to U). Here is an example:
In this case:
U = {A,B,C}
V = {D,E,F}
E = {(A->E,7), (B->D,1), (C->E,3), (F->A,9)}
Definition: DirectionalMatching (I made up this term just to make things clearer): set of directed edges that may share the start or end vertices. That is, if U->V and U'->V' both belong to a DirectionalMatching then V /= U' and V' /= U but it may be that U = U' or V = V'.
My question: How to efficiently find a DirectionalMatching, as defined above, for a bipartite directional weighted graph which maximizes the sum of the weights of its edges?
By efficiently, I mean polynomial complexity or faster, I already know how to implement a naive brute force approach.
In the example above the maximum weighted DirectionalMatching is: {F->A,C->E,B->D}, with a value of 13.
Formally demonstrating the equivalence of this problem to any other well known problem in graph theory would also be valuable.
Thanks!
Note 1: This question is based on Maximum weighted bipartite matching _with_ directed edges but with the extra relaxation that it is allowed for edges in the matching to share the origin or destination. Since that relaxation makes a big difference, I created an independent question.
Note 2: This is a maximum weight matching. Cardinality (how many edges are present) and the number of vertices covered by the matching is irrelevant for a correct result. Only the maximum weight matters.
Note 2: During my research to solve the problem I found this paper, I think it would be helpful to others trying to find a solution: Alternating cycles and paths in edge-coloured
multigraphs: a survey
Note 3: In case it helps, you can also think of the graph as its equivalent 2-edge coloured undirected bipartite multigraph. The problem formulation would then turn into: Find the set of edges without colour-alternating paths or cycles which has maximum weight sum.
Note 4: I suspect that the problem might be NP-hard, but I am not that experienced with reductions so I haven't managed to prove it yet.
Yet another example:
Imagine you had
4 vertices: {u1, u2} {v1, v2}
4 edges: {u1->v1, u1->v2, u2->v1, v2->u2}
Then, regardless of their weights, u1->v2 and v2->u2 cannot be in the same DirectionalMatching, neither can v2->u2 and u2->v1. However u1->v1 and u1->v2 can, and so can u1->v1 and u2->v1.
Define a new undirected graph G' from G as follows.
G' has a node (A, B) with weight w for each directed edge (A, B) with weight w in G
G' has undirected edge ((A, B),(B, C)) if (A, B) and (B, C) are both directed edges in G
http://en.wikipedia.org/wiki/Line_graph#Line_digraphs
Now find a maximal (weighted) independent vertex set in G'.
http://en.wikipedia.org/wiki/Vertex_independent_set
Edit: stuff after this point only works if all of the edge weights are the same - when the edge weights have different values its a more difficult problem (google "maximum weight independent vertex set" for possible algorithms)
Typically this would be an NP-hard problem. However, G' is a bipartite graph -- it contains only even cycles. Finding the maximal (weighted) independent vertex set in a bipartite graph is not NP-hard.
The algorithm you will run on G' is as follows.
Find the connected components of G', say H_1, H_2, ..., H_k
For each H_i do a 2-coloring (say red and blue) of the nodes. The cookbook approach here is to do a depth-first search on H_i alternating colors. A simple approach would be to color each vertex in H_i based on whether the corresponding edge in G goes from U to V (red) or from V to U (blue).
The two options for which nodes to select from H_i are either all the red nodes or all the blue nodes. Choose the colored node set with higher weight. For example, the red node set has weight equal to H_i.nodes.where(node => node.color == red).sum(node => node.w). Call the higher-weight node set N_i.
Your maximal weighted independent vertex set is now union(N_1, N_2, ..., N_k).
Since each vertex in G' corresponds to one of the directed edges in G, you have your maximal DirectionalMatching.
This problem can be solved in polynomial time using the Hungarian Algorithm. The "proof" by Vor above is wrong.
The method of structuring the problem for the above example is as follows:
D E F
A # 7 9
B 1 # #
C # 3 #
where "#" means negative infinity. You then resolve the matrix using the Hungarian algorithm to determine the maximum matching. You can multiply the numbers by -1 if you want to find a minimum matching.