if the shortest path between two vertex on weighted and directed graph G (maybe has negative edge), is shown by D(u, v), the following claims is always false.
with having negative edges, but didn't have any negative cycle, then
Sigma on D(u,v) (sum on all vertex pairs) cannot be negative.
Why this claims is False?
what is D(u,v) where there's no path from u to v is not given in my
notes, but I think D(u,v)=0 in this case.
Assuming D(u,v) = infinity if there is no path from u to v (I really see no reason to assume otherwise, it is weird to assume D(u,v)=0 in this case), the claim is true.
Proof:
First, assume there is a path for each pair u,v - otherwise sum of all pairs is infinity, and we are done.
For each pair of vertices u,v:
If D(u,v)>0 and D(v,u)>0 this pair contribute positive number to the summation
Otherwise, and without loss of generality, assume D(u,v)<0. Since there are no negative cycles, D(u,v) + D(v,u) >= 0 and thus D(v,u) >= -D(u,v). And as we see, D(v,u) + D(u,v) contribute a non negative number for the summation.
Since the above is true for each pair u,v - there is no pair that can contribute a negative number, and the summation cannot be negative.
QED
with having negative edges, but didn't have any negative cycle, then Sigma on D(u,v) (sum on all vertex pairs) cannot be negative.
D(u, v) = 0 for no arc u -> v
Consider the directed graph:
1 -> 2 -> 3
With each arc having cost -1: there is no negative cost cycle, but the sum over all pairs is negative. So the claim is false because we have found a counterexample.
D(u, v) = infinity for no arc u -> v
In this case, if we want to find a counterexample we must consider a graph that has paths between all pairs of nodes, otherwise the sum will always be positive because we will be adding an infinite quantity.
Consider a path with negative cost from a node x to a node y. Then the cost of the path from y to x must be positive and such that D(x, y) + D(y, x) is not negative, otherwise we'd have a negative cycle, which isn't allowed.
Since each negative cost path must have a positive cost (return path + initial path), the statement is true for this case.
Related
Given a weighed, connected and directed graph G=(V,E) with n vertexes and m edges, and given a pre-calculated shortest path distance's matrix S where S is n*n S(i,j) denotes the weight of shortest path from vertex i to vertex j.
we know just weight of one edge (u, v) is changed (increased or decreased).
for two specific vertex s and t we want to update the shortest path length between these two vertex.
This can be done in O(1).
How is this possible? what is the trick of this answer?
You certainly can for decreases. I assume S will always refer to the old distances. Let l be the new distance between (u, v). Check if
S(s, u) + l + S(v, t) < S(s, t)
if yes then the left hand side is the new optimal distance between s and t.
Increases are impossible. Consider the following graph (edges in red have zero weight):
Suppose m is the minimum weight edge here, except for (u, v) which used to be lower. Now we update (u, v) to some weight l > m. This means we must find m to find the new optimum length.
Suppose we could do this in O(1) time. Then it means we could find the minimum of any array in O(1) time by feeding it into this algorithm after adding (u, v) with weight -BIGNUMBER and then 'updating' it to BIGNUMBER (we can lazily construct the distance matrix because all distances are either 0, inf or just the edge weights). That is clearly not possible, thus we can't solve this problem in O(1) either.
Given a weighted graph G=(V,E) which doesnt include negative cycles, a natural number k, and two verticles: s,t.
How can I find the cheapest route from s to t which its length can be divied by k?
Prepare a new graph G' with vertices V × {0, 1, …, n−1} and for each arc v → w of length ℓ in G, arcs (v, x) → (w, (x + ℓ) mod k). Then use Dijkstra's algorithm to find a shortest path from (s, 0) to (t, 0).
Use BFS with a priority queue, so as to always examine states (= paths from s) that are shortest. Unlike normal Dijkstra, your states are full paths, and you can revisit already-visited vertices as often as they are encountered.
I cannot prove that such an algorithm would be optimal, but at least it should be correct, always returning a valid shortest-path answer if it exists. Runtime for certain graphs and values of K would be very high, and the algorithm may not finish at all if there are no k-divisible paths from s to t but there are loops with a path-length divisible by k. You could find and filter those out first by using a preliminary DFS.
I have Vertices V={s,u,v,x} as well as Edges E={(s,u),(s,x),(s,v),(u,v),(v,x),(x,u)) as well as the following Weights:
W(s, u) = 1
W(v, x) = W(x, u) = W(s, v)=2
W(u, v) = -3
W(s, x) = -1
Now I am executing Initialize(G,w,s) making s the starting point and initialize s.d = 0.
I need the shortest path distances of u,v,x. Since they are all connected to s, I can just use the weight of W(s, u), W(s, v), W(s, x). But x.d would be -1. Is that even applicable ? Could I now use this distance to correctly execute Relax(s,x,w) and get a correct output?
Thanks in Advance
As Dijkstra's algorithm with negative weights says, as long as there are no negative cycles, Bellman-Ford will converge. If there is a negative cycle, it will detect that fact.
If there are negative cycles then there is no solution for it but to pair the cost of getting from A to B with the set of negative edges that were visited along the way so that you can trace them out and not revisit them again. This is theoretically correct but expensive both in memory and running time.
Imagine you are given a weighted undirected complete graph with n nodes with non-negative weights Cij, where for i = j Cii = 0, and for i != j Cij > 0. Suppose you have to find the maximal shortest path between any two nodes i and j. Here, you can easily use Floyd-Warshall, or use Dijkstra n times, or whatever, and then just to find the maximum among all n^2 shortest paths.
Now assume that Cij are not constant, but rather can take two values, Aij and Bij, where 0 <= Aij <= Bij. We also have Aii = Bii = 0. Assume you also need to find the maximal shortest path, but with the constraint that m edges must take value Bij, and other Aij. And, if m > n^2, then all edges are equal to Bij. But, when finding shortest path i -> p1 -> ... -> pk -> j, you are intesrested in the worst case, in the sense that on that path you need to choose those edges to take value of Bij, such that path value is maximal if you have fixed nodes on its direction.
For example, a if you have path of length four i-k-l-j, and, in optimal solution on that path only one weight is changed to Bij, and other take value of Aij. And let m1 = Bik + Akl + Alj, m2 = Aik + Bkl + Alj, m3 = Aik + Akl + Blj, the value of that path is max{m1, m2, m3}. So, among all paths between i and j, you have to choose one such that maximal value (described as in this example) is minimal (which is a variant of definition of shortest path). And you have to do it for all pairs i and j.
You are not given the constraint how many you need to vary on each path, but rather value of m, a number of weights that should be varied in the complete graph. And the problem is to find the maximum value of the shortest path, as described.
Also, my question is: is this NP-hard problem, or there exists some polynomial solution?
I have an undirected graph. For now, assume that the graph is complete. Each node has a certain value associated with it. All edges have a positive weight.
I want to find a path between any 2 given nodes such that the sum of the values associated with the path nodes is maximum while at the same time the path length is within a given threshold value.
The solution should be "global", meaning that the path obtained should be optimal among all possible paths. I tried a linear programming approach but am not able to formulate it correctly.
Any suggestions or a different method of solving would be of great help.
Thanks!
If you looking for an algorithm in general graph, your problem is NP-Complete, Assume path length threshold is n-1, and each vertex has value 1, If you find the solution for your problem, you can say given graph has Hamiltonian path or not. In fact If your maximized vertex size path has value n, then you have a Hamiltonian path. I think you can use something like Held-Karp relaxation, for finding good solution.
This might not be perfect, but if the threshold value (T) is small enough, there's a simple algorithm that runs in O(n^3 T^2). It's a small modification of Floyd-Warshall.
d = int array with size n x n x (T + 1)
initialize all d[i][j][k] to -infty
for i in nodes:
d[i][i][0] = value[i]
for e:(u, v) in edges:
d[u][v][w(e)] = value[u] + value[v]
for t in 1 .. T
for k in nodes:
for t' in 1..t-1:
for i in nodes:
for j in nodes:
d[i][j][t] = max(d[i][j][t],
d[i][k][t'] + d[k][j][t-t'] - value[k])
The result is the pair (i, j) with the maximum d[i][j][t] for all t in 0..T
EDIT: this assumes that the paths are allowed to be not simple, they can contain cycles.
EDIT2: This also assumes that if a node appears more than once in a path, it will be counted more than once. This is apparently not what OP wanted!
Integer program (this may be a good idea or maybe not):
For each vertex v, let xv be 1 if vertex v is visited and 0 otherwise. For each arc a, let ya be the number of times arc a is used. Let s be the source and t be the destination. The objective is
maximize ∑v value(v) xv .
The constraints are
∑a value(a) ya ≤ threshold
∀v, ∑a has head v ya - ∑a has tail v ya = {-1 if v = s; 1 if v = t; 0 otherwise (conserve flow)
∀v ≠ x, xv ≤ ∑a has head v ya (must enter a vertex to visit)
∀v, xv ≤ 1 (visit each vertex at most once)
∀v ∉ {s, t}, ∀cuts S that separate vertex v from {s, t}, xv ≤ ∑a such that tail(a) ∉ S ∧ head(a) ∈ S ya (benefit only from vertices not on isolated loops).
To solve, do branch and bound with the relaxation values. Unfortunately, the last group of constraints are exponential in number, so when you're solving the relaxed dual, you'll need to generate columns. Typically for connectivity problems, this means using a min-cut algorithm repeatedly to find a cut worth enforcing. Good luck!
If you just add the weight of a node to the weights of its outgoing edges you can forget about the node weights. Then you can use any of the standard algorigthms for the shortest path problem.