Question : Minimal routes through preferred vertices - algorithm

I need help to solve this question:
you have a graph G=(V,E) , nodes pair s ≠ t ∈ V and you have subset of nodes U ⊆ V Which exists ø ≠ U ≠ V and s,t ∉ U.
for each path P we mark it L(P) the length of the path (number of edges in the path) and #P(U) number of the nodes U in the path, write an algorithm to find a path between from s to t that should visit U exactly 2 times And the minimum length of all these tracks.
In other words the algorithm should return a path from s to t where #P(U)=2 and lets say we have another path P' from s to t where #P'(U)=2 then L(P)≤ L(P')
( it is permissible for the track to pass through A certain vertex more than once) .
Help : use graph reduction G'

Divide the trajectory into 5 steps
{s} ∪X → U → X∪ {ε} → U → X∪ {t} where X = V \\ (U∪ {s, t}) (X group of vertices out of V that are not Preferred vertices, neither s nor t) which will ensure that #P (U) = 2 and we will use BFS to find the shortest route.
Algorithm -
Constructed from G (V, E) New directional graph G ^ '= (V ^', E ^ ') as follows:
Each preferred vertex u∈U, we add twice to V ^ 'so u_4∈V ^', u_2∈V ^ '. We will add s and t to the new graph and for each x∈X vertex we will create x_1, x_3, x_5 in the new graph.
For each arc (s, x) ∈E | x∈X, {(s, x_1)} ∈E ^ '
Per arc (s, u) ∈E | u∈U, {(s, u_2)} ∈E ^ '
For each arc (x, u) ∈E | x∈X, u∈U, {(x_1, u_2), (x_3, u_4)} ∈E ^ '
Per arc (u, x) ∈E | u∈U, x∈X, {(u_2, x_3), (u_4, x_5)} ∈E ^ '
Per arc (u, u ') ∈E | u, u'∈U, {(u_2, u_4 ^ ')} ∈E ^'
Per arc (u, t) ∈E | u∈U, {(u_4, t)} ∈E ^ '
For each arc (x, t) ∈E | x∈X, {(x_5, t)} ∈E ^ '
We will now run a BFS scan on graph G 'starting from s. For the first appearance of t in the scan, the trajectory from s to t (after lowering the labels for there to be a trajectory in G) is the shortest trajectory that satisfies the question conditions. If no t is found in the scan - then there is no such trajectory.
The correctness of the algorithm -
#P (U) = 2 - By defining the graph G 'we guarantee that each trajectory from s to t will pass exactly twice in the vertices of U (this can be seen by labeling the vertices to stages 1-5 and arcs that always progress in stages without the possibility of skipping stages 2 and 4 where there are vertices of U).
The shortest route - using a BFS scan ensures acceptance of the shortest route, because the scan is performed at levels when a vertex that appeared at an earlier level - will not appear at a later level.
Finally, the labels are lowered to ensure that the route is in G.
Complexity of the algorithm -
The graph G 'is constructed by going over all the vertices and arcs of G once and therefore ran in Θ (m + n), scanning BFS over G' running in Θ (m + n) (by BFS runtime) and we obtained that the runtime The total is Θ (m + n).

Previous solution presented by 'evyatar weiss' is partial, since it's not considering the fact that you can move within X uninterruptedly (U is limited to 2 vertices per route, but X is not limited). Following is an example of a graph to which this solution will not work (I added in pencil the missing vertices in order to fix it).
In order to solve this, the following vericies should be added to G':
For each arc (x,x')∈E | x,x'∈X, {(x_1, x'_1), (x_3, x'_3), (x_5, x'_5)}∈E'

Related

Create an algorithm for computing the shortest path with constraints on the vertices in O(m + nlogn)

So I'm trying to write an algorithm for computing the shortest path with constraints on the vertices you can visit in O(m + nlogn) time. In this problem, we are given an indirect weighted (non negative) graph G = (V, E), a set of vertices X ⊂ V, and two vertices s, t ∈ V \ X. The graph is given with adjacency lists and we can assume that it takes O(1) time to determine if a vertex is in X. I'm trying to write an algorithm which can compute the shortest path between s and t in G such that the path includes at most one vertex from X if such a path exists in O(m + nlogn) time. I know that this algorithm would require a modified Dijkstra's algorithm but I'm unsure how to proceed from here. Could anyone please help me out? Thank you
Construct a new graph by taking two disjoint copies of your graph G (call them G0 and G1), and for each edge (v, x) in G (with x in X), add an additional edge in the combined graph from v in G0 to x in G1. This new graph has twice as many vertices as G, and at most three times as many edges.
The shortest path from s in G0 to t in G1 is the shortest path from s to t in G going through at least one vertex of X.
On this new graph, Dijkstra's algorithm (with the priority queue implemented with a fibonacci heap) works in time O(3m + 2n log 2n) = O(m + n log n).
A possibility is to double the number of vertices not in X.
For each vertex v not in X, you create v0 and v1: v0 is accessible only without passing from a vertex in X, v1 is accessible by passing through one and only one vertex in X.
Let us call w another vertex. Then:
if w is in X, v not in X:
length (w, v0) = infinite and dist(v1) = min (dist(v1), dist(w) + length(w, v))
if w is in X, v in X:
length (w, v) = infinite
if w is not in X, v not in X:
dist (v0) = min (dist(v0), dist (w0) + length (w, v))
dist (v1) = min (dist(v1), dist (w1) + length (w, v))
if w is not in X, v is in X:
dist (v) = min (dist(v), dist (w0) + length (w, v))

Find the Optimal vertex cover of a tree with k blue vertices

I need to find a 'dynamic - programming' kind of solution for the following problem:
Input:
Perfect Binary-Tree, T = (V,E) - (each node has exactly 2 children except the leafs).
V = V(blue) ∪ V(black).
V(blue) ∩ V(black) = ∅.
(In other words, some vertices in the tree are blue)
Root of the tree 'r'.
integer k
A legal Solution:
A subset of vertices V' ⊆ V which is a vertex cover of T, and |V' ∩ V(blue)| = k. (In other words, the cover V' contains k blue vertices)
Solution Value:
The value of a legal solution V' is the number of vertices in the set = |V'|.
For convenience, we will define the value of an "un-legal" solution to be ∞.
What we need to find:
A solution with minimal Value.
(In other words, The best solution is a solution which is a cover, contains exactly k blue vertices and the number of vertices in the set is minimal.)
I need to define a typical sub-problem. (Like, if i know what is the value solution of a sub tree I can use it to find my value solution to the problem.)
and suggest a formula to solve it.
To me, looks like you are on the right track!
Still, I think you will have to use an additional parameter to tell us how far is any picked vertex from the current subtree's root.
For example, it can be just the indication whether we pick the current vertex, as below.
Let fun (v, b, p) be the optimal size for subtree with root v such that, in this subtree, we pick exactly b blue vertices, and p = 1 if we pick vertex v or p = 0 if we don't.
The answer is the minimum of fun (r, k, 0) and fun (r, k, 1): we want the answer for the full tree (v = r), with exactly k vertices covered in blue (b = k), and we can either pick or not pick the root.
Now, how do we calculate this?
For the leaves, fun (v, 0, 0) is 0 and fun (v, t, 1) is 1, where t tells us whether vertex v is blue (1 if yes, 0 if no).
All other combinations are invalid, and we can simulate it by saying the respective values are positive infinities: for example, for a leaf vertex v, the value fun (v, 3, 1) = +infinity.
In the implementation, the infinity can be just any value greater than any possible answer.
For all internal vertices, let v be the current vertex and u and w be its children.
We have two options: to pick or not to pick the vertex v.
Suppose we pick it.
Then the value we get for f (v, b, 1) is 1 (the picked vertex v) plus the minimum of fun (u, x, q) + fun (w, y, r) such that x + y is either b if the vertex v is black or b - 1 if it is blue, and q and r can be arbitrary: if we picked the vertex v, the edges v--u and v--w are already covered by our vertex cover.
Now let us not pick the vertex v.
Then the value we get for f (v, b, 0) is just the minimum of fun (u, x, 1) + fun (w, y, 1) such that x + y = b: if we did not pick the vertex v, the edges v--u and v--w have to be covered by u and w.

Find the simple path with at least 5 edges in directed graph

I have a question.
Given a directed graph (G = V, E) and the source vertex s from V group.
we want to check whether there is a simple path (no circles) from s to any vertex in G with at least 5 edges.
Offer as efficient an algorithm as possible that solves the problem for a graph G that can contain circles.
please I need your help
Thanks :-)
We need to find any 5-edge simple directed path starting at vertex s. This path will look like:
s -> a -> b -> c -> d -> e (all distinct)
Now let's go through the all possible values of c (any vertex besides s) and then for every c value we can go through all edges that do not contain s and c vertices and for the edge (x, y) do the following:
if edge (s, x) exists and edge (y, c) exists
put (x, y) in AB edges list
if edge (c, x) exists
put (x, y) in DE edges list
This can be done in O(|E|). Then we need to find a pair of edges (E1, E2) such that E1 is in AB, E2 is in DE and they don't share any vertex in common. The latter can be done in O(|E|).
We can take a graph G' = (V, DE) and find the degrees of the vertices. Then for every edge (a, b) from AB we need to check that
degree(a) + degree(b) = |DE| + x
where x = 1 if (a, b) is in DE, otherwise x = 0. If this equality does not hold it means that there is an edge in DE that contains neither a nor b and we can just iterate through entire DE to find the answer.
The overall complexity will be O(|V||E|) with O(|E|) additional memory.

Proof of correctness: Algorithm for diameter of a tree in graph theory

In order to find the diameter of a tree I can take any node from the tree, perform BFS to find a node which is farthest away from it and then perform BFS on that node. The greatest distance from the second BFS will yield the diameter.
I am not sure how to prove this, though? I have tried using induction on the number of nodes, but there are too many cases.
Any ideas would be much appreciated...
Let's call the endpoint found by the first BFS x. The crucial step is proving that the x found in this first step always "works" -- that is, that it is always at one end of some longest path. (Note that in general there can be more than one equally-longest path.) If we can establish this, it's straightforward to see that a BFS rooted at x will find some node as far as possible from x, which must therefore be an overall longest path.
Hint: Suppose (to the contrary) that there is a longer path between two vertices u and v, neither of which is x.
Observe that, on the unique path between u and v, there must be some highest (closest to the root) vertex h. There are two possibilities: either h is on the path from the root of the BFS to x, or it is not. Show a contradiction by showing that in both cases, the u-v path can be made at least as long by replacing some path segment in it with a path to x.
[EDIT] Actually, it may not be necessary to treat the 2 cases separately after all. But I often find it easier to break a configuration into several (or even many) cases, and treat each one separately. Here, the case where h is on the path from the BFS root to x is easier to handle, and gives a clue for the other case.
[EDIT 2] Coming back to this later, it now seems to me that the two cases that need to be considered are (i) the u-v path intersects the path from the root to x (at some vertex y, not necessarily at the u-v path's highest point h); and (ii) it doesn't. We still need h to prove each case.
I'm going to work out j_random_hacker's hint. Let s, t be a maximally distant pair. Let u be the arbitrary vertex. We have a schematic like
u
|
|
|
x
/ \
/ \
/ \
s t ,
where x is the junction of s, t, u (i.e. the unique vertex that lies on each of the three paths between these vertices).
Suppose that v is a vertex maximally distant from u. If the schematic now looks like
u
|
|
|
x v
/ \ /
/ *
/ \
s t ,
then
d(s, t) = d(s, x) + d(x, t) <= d(s, x) + d(x, v) = d(s, v),
where the inequality holds because d(u, t) = d(u, x) + d(x, t) and d(u, v) = d(u, x) + d(x, v). There is a symmetric case where v attaches between s and x instead of between x and t.
The other case looks like
u
|
*---v
|
x
/ \
/ \
/ \
s t .
Now,
d(u, s) <= d(u, v) <= d(u, x) + d(x, v)
d(u, t) <= d(u, v) <= d(u, x) + d(x, v)
d(s, t) = d(s, x) + d(x, t)
= d(u, s) + d(u, t) - 2 d(u, x)
<= 2 d(x, v)
2 d(s, t) <= d(s, t) + 2 d(x, v)
= d(s, x) + d(x, v) + d(v, x) + d(x, t)
= d(v, s) + d(v, t),
so max(d(v, s), d(v, t)) >= d(s, t) by an averaging argument, and v belongs to a maximally distant pair.
Here's an alternative way to look at it:
Suppose G = ( V, E ) is a nonempty, finite tree with vertex set V and edge set E.
Consider the following algorithm:
Let count = 0. Let all edges in E initially be uncolored. Let C initially be equal to V.
Consider the subset V' of V containing all vertices with exactly one uncolored edge:
if V' is empty then let d = count * 2, and stop.
if V' contains exactly two elements then color their mutual (uncolored) edge green, let d = count * 2 + 1, and stop.
otherwise, V' contains at least three vertices; proceed as follows:
Increment count by one.
Remove all vertices from C that have no uncolored edges.
For each vertex in V with two or more uncolored edges, re-color each of its green edges red (some vertices may have zero such edges).
For each vertex in V', color its uncolored edge green.
Return to step (2).
That basically colors the graph from the leaves inward, marking paths with maximal distance to a leaf in green and marking those with only shorter distances in red. Meanwhile, the nodes of C, the center, with shorter maximal distance to a leaf are pared away until C contains only the one or two nodes with the largest maximum distance to a leaf.
By construction, all simple paths from leaf vertices to their nearest center vertex that traverse only green edges are the same length (count), and all other simple paths from a leaf vertex to its nearest center vertex (traversing at least one red edge) are shorter. It can furthermore be proven that
this algorithm always terminates under the conditions given, leaving every edge of G colored either red or green, and leaving C with either one or two elements.
at algorithm termination, d is the diameter of G, measured in edges.
Given a vertex v in V, the maximum-length simple paths in G starting at v are exactly those that contain contain all vertices of the center, terminate at a leaf, and traverse only green edges between center and the far endpoint. These go from v, across the center, to one of the leaves farthest from the center.
Now consider your algorithm, which might be more practical, in light of the above. Starting from any vertex v, there is exactly one simple path p from that vertex, ending at a center vertex, and containing all vertices of the center (because G is a tree, and if there are two vertices in C then they share an edge). It can be shown that the maximal simple paths in G having v as one endpoint all have the form of the union of p with a simple path from center to leaf traversing only green edges.
The key point for our purposes is that the incoming edge of the other endpoint is necessarily green. Therefore, when we perform a search for the longest paths starting there, we have access to those traversing only green edges from leaf across (all vertices of) the center to another leaf. Those are exactly the maximal-length simple paths in G, so we can be confident that the second search will indeed reveal the graph diameter.
1:procedureTreeDiameter(T)
2:pick an arbitrary vertex v where v∈V
3:u = BFS ( T, v )
4:t = BFS ( T, u )
5:return distance ( u, t )
Result: Complexity = O(|V|)

Min s-t cut in a network

I am trying to simulate a network of wireless sensor nodes in order to research about the robustness of the network. I am faced with the following problem:
I have a network of nodes with some edge capacities. This is equivalent to something like network flow problem in algorithms. There is a source node (which detects certain events) and a sink node (my base station). Now, I want to find the minimum s-t cut in the network so that the size of the source set is minimized. The source set here refers to the set of nodes separated by the min s-t cut that contains the source.
e.g. if the s-t cut, C = {S,T}, then there is a set of edges which can be removed to separate the network into two sets, S and T and the set S contains the source and T contains the sink. The cut is minimum when the sum of capacities of the edges in the cut is minimum among all possible s-t cuts. There can be several such min-cuts. I need to find a min-cut that has least number of elements in the set S
Note that this is not the original problem but I have tried to simplify it in order to express it in terms of algorithms.
I believe that you can solve this problem by finding a minimum cut in a graph with slightly modified constraints. The idea is as follows - since the cost of a cut is equal to the total capacity crossing the cut, we could try modifying the graph by adding in an extra edge from each node in the graph to t that has capacity one. Intuitively, this would mean that every node in the same part of the cut as s would contribute one extra cost to the total cost of the cut, because the edge from that node to t would cross the edge. Of course, this would definitely mess up the actual min-cut because of the extra capacity. To fix this, we apply the following transformation - first, multiply the capacities of the edges by n, where n is the number of nodes in the graph. Then add one to each edge. The intuition here is that by multiplying the edge capacities by n, we've made it so that the cost of the min-cut (ignoring the new edges from each node to t) will be n times the original cost of the cut. When we then add in the extra one-capacity edges from each node to t, the maximum possible contribution these edges can make to the cost of the cut is n - 1 (if every node in the graph except for t is on the same side as s). Thus the cost of the old min-cut was C, the cost of the new min-cut (S, V - S) is nC + |S|, where |S| is the the number of nodes on the same side of the cut as s.
More formally, the construction is as follows. Given a directed, capacitated graph G and a (source, sink) pair (s, t), construct the graph G' by doing the following:
For each edge (u, v) in the graph, multiply its capacity by n.
For each node v in the graph, add a new edge (v, t) with capacity 1.
Compute a min s-t cut in the graph.
I claim that a min s-t cut in the graph G' corresponds to a min s-t cut in graph G with the fewest number of nodes on the same side of the cut as s. The proof is as follows. Let (S, V - S) be a min s-t cut in G'. First, we need to show that (S, V - S) is a min s-t cut in G. This proof is by contradiction; assume for the sake of contradiction that there is an s-t cut (S', V - S') whose cost is lower than the cost of (S, V - S). Let the cost of (S', V - S') in G be C' and let the cost of (S, V - S) in G be C. Now, let's consider the cost of these two cuts in G'. By constriction, the cost of C' would be nC' + |S'| (since each node on the S' side of the cut contributes one capacity across the cut) and the cost of C would be nC + |S|. Since we know that C' < C, we must have that C' + 1 ≤ C. Thus
nC + |S| ≥ n(C' + 1) + |S| = nC' + n + |S|
Now, note that 0 ≤ |S| < n and 0 ≤ |S'| < n, because there can be at most n nodes on the same side of the cut as s. Thus means that
nC + |S| ≥ nC' + n + |S| > nC' + |S'| + |S| > nC' + |S'|
But this means that the cost of (S, V - S) in G' is greater than the cost of (S', V - S') in G', contradicting the fact that (S, V - S) is a min s-t cut in G'. This allows us to conclude that any min s-t cut in G' is also a min s-t cut in G.
Now, we need to show that not only is a min s-t cut in G' also a min s-t cut in G, but it corresponds to a min s-t cut in G with the fewest number of nodes on the same side of the cut as s. Again, this proof is by contradiction; suppose that (S, V - S) is a min s-t cut in G' but that there is some min s-t cut in G with fewer nodes on the s side of the cut. Call this better cut (S', V - S'). Since (S, V - S) is a min s-t cut in G', it's also a min s-t cut in G, so the cost of (S', V - S') and (S, V - S) in G is some number C. Then the cost of (S, V - S) and (S', V - S') in G' will be nC + |S| and nC + |S'|, respectively. We know that nC + |S'| < nC + |S|, since we've chosen (S', V - S') to be an s-t min cut in G with the fewest number of nodes on the same side as S. But this means that (S', V - S') has a lower cost than (S, V - S), contradicting the fact that (S, V - S) is a min s-t cut in G'. Thus our assumption was wrong and (S, V - S) is a min s-t cut in G with the fewest number of nodes on the same side as S. This completes the correctness proof of the construction.
Hope this helps!
tl;dr Compute an max s-t flow and let S be the set of nodes reachable from s by arcs of positive residual capacity.
Proof of correctness: clearly S is an min s-t cut (cut = set of nodes in the part containing s). Suppose that S* is an s-t cut smaller than S (i.e., |S*| < |S|). By an easy counting argument, let u be a node in S - S*. If we add a positive capacity arc from u to t, then the computed flow has an augmenting path and is no longer maximum, but the capacity of the cut S* is unchanged, since u and t both belong to V - S*. We conclude by weak duality that S* is not a min cut.
In fact, the class of s-t min cuts is a distributive lattice under intersection and union, so every instance of your problem has a unique solution.
In your question and comment I think you say two different thing, First Finding minmum s-t cut such that separates node source and think and it's weight is minimum (weight will be calculated by remove edges sizes) and this can be done with Ford-Fulkerson Algorithm and here is sample implementation in java (also Matlab has a function graphmaxflow) also it's available in igraph library.
But as your comment and first part of question you asked for finding min cut, such that number of nodes in s part is minimized, In this case you should remove all edge of S to have a groups of size 1,n-1, Or you should rephrase your question.

Resources