All pairs shortest paths with dynamic programming - algorithm

All,
I am reading about the relationship between all pairs shortest path and matrix multiplication.
Consider the multiplication of the weighted adjacency matrix with
itself - except, in this case, we replace the multiplication operation
in matrix multiplication by addition, and the addition operation by
minimization. Notice that the product of weighted adjacency matrix
with itself returns a matrix that contains shortest paths of length 2
between any pair of nodes.
It follows from this argument that A to power of n contains all shortest paths.
Question number 1:
My question is that in a graph we will be having at most n-1 edges between two nodes in a path, on what basis is the author discussing of path of length "n"?
Following slides
www.infosun.fim.uni-passau.de/br/lehrstuhl/.../Westerheide2.PPT
On slide 10 it is mentioned as below.
dij(1) = cij
dij(m) = min (dij(m-1), min1≤k≤n {dik(m-1) + ckj}) --> Eq 1
= min1≤k≤n {dik(m-1) + ckj} ------------------> Eq 2
Question 2: how author concluded Eq 2 from Eq 1.
In Cormen et al book on introduction to algorithms, it is mentioned as below:
What are the actual shortest-path weights delta(i, j)? If the graph contains no negative-weight cycles, then all shortest paths are simple and thus contain at most n - 1 edges. A path from vertex i to vertex j with more than n - 1 edges cannot have less weight than a shortest path from i to j. The actual shortest-path weights are therefore given by
delta(i,j) = d(i,j) power (n-1) = (i,j) power (n) = (i,j) power (n+1) = ...
Question 3: in above equation how author came with n, n+1 edges as we have at most n-1, and also how above assignment works?
Thanks!

The n vs n-1 is just an unfortunate variable name choice. He should have used a different letter instead to be more clear.
A^1 has the shortest paths of length up to 1 (trivially)
A^2 has the shortest paths of length up to 2
A^k has the shortest paths of length up to k
Eq 2 does not directly come from Eq1. Eq 2 is just the second term from the first equation. I presume this is a formatting or copy-paste error (I can't check - your ppt link is broken)
The author is just explicitly pointing out that you have nothing to gain by adding more then n-1 edges to the path:
A^(n-1), //the shortest paths of length up tp (n-1)
is equal to A^n //the shortest paths of length up tp (n)
is equal to A^(n+1) //the shortest paths of length up tp (n+1)
...
This is just so that you can safely stop your computations at (n-1) and be sure that you have the minimum paths among all paths of all lengths. (This is kind of obvious but the textbook has a point in being strict here...)

In a graph we will be having atmost n-1 edges between two nodes in a path, on what basis author is discussing of path of length "n"?
You're confusing the multiple measures being discussed:
A^n represents the "shortest paths" (by weight) of length n between vertices.
"at most n-1 edges between two nodes" -- I presume in this case you're thinking of n as the size of the graph.
The graph could have hundreds of vertices but your adjacency matrix A^3 shows the shortest paths of length 3. Different n measures.

Related

Proof that using adjacency matrix for bipartite testing has Ω(n^2)

so in one of my lectures I came across the proof for:
𝐓𝐡𝐞𝐨𝐫𝐞𝐦: 𝐴𝑛𝑦 algorithm that determines if a graph is bipartite
that has as its input an undirected graph 𝐺 = (𝑉, 𝐸) represented
as an 𝑛 × 𝑛 adjacency matrix, has the running time of Ω(𝑛^2)
We assume an algorithm ALG which test for bipartiteness (returns either true or false). And we also assume we have a graph 𝐺0 = (𝑉, 𝐸0) with 𝑉 = {1,2, … , 𝑛} and 𝐸0 = { 1, 𝑖 : 2 ≤ 𝑖 ≤ 𝑛} (as this is a star it is a bipartite graph)
Within the proof there's a step saying:
"For a given algorithm ALG, we will construct another graph 𝐺1 st: if ALG performs less than (𝑛−1)C2 accesses to the adjacency matrix 𝐴 of 𝐺0,
then ALG will not distinguish between 𝐺0 and 𝐺1, and 𝐺1 is not bipartite."
My question is what does (n-1)C2 accesses mean. Is it saying that for example if we have a different V = {A,B,C,D} then ALG will look at all node pairs except for the ones between D and the other nodes ?
Sorry if this isn't clear this proof really confused me.
G0 is an n-vertex star graph. It's bipartite, but if you add any other edge to it, the resulting graph is not. There are n−1 choose 2 = (n−1)(n−2)/2 = Ω(n2) other edges that we can add. Every correct algorithm must check every single one in order to verify that G0 is bipartite.

How many different adjacency matrix does a graph with N vertices and E edges have?

The answer is N! and I don't understand how it is.
My view:
Assuming it is an undirected graph;
The dimension of each row in a adj. matrix of a vertex is N irrespective of the number of edges. Hence, the number of permutation possible for first row= N!.
Total permutation for second row= (N-1)! since one cell has already been taken care in the first row.
Similarly, third row= (N-2)!
.
.
.
For Nth row=1
Total permutation= N! + N-1!+...+1!
I am confused if considering an undirected or directed graph will yield different result or not. How will the answer change if we consider graph to be directed?
I'll take a shot at it, but please ask questions if it's unclear. For it to be N!, we are assuming an undirected graph.
For a graph with N vertices, it would be represented with an NxN matrix (2D array) with each value being either 0 (edge does not exist) or 1 (edge exists). In this, we are not considering other weights on edges.
Then, we consider all of the possible assignments. If there are N different choices on the first row, there must be N-1 choices on the second row (since we already know about the edge 1,2) and so on.
N * (N-1) * (N-2) * ... * 1 = N!

All pairs shortest path in a connected graph with N nodes and N-1 edges

I have a graph with N nodes (2 <= N <= 50000) and N is even. The values of the Nodes are always a number between 1 and N/2. It's granted that there is only one path between any pair of nodes and the weight of the edges is always one. How can i sum the distance of all nodes with equal value ?
This is a example:
The number inside the square is the value of the node and the small number below it its the identification of the node.
In this example the sum is distance(1,6) + distance(2,5) + distance(3,4) = 5
Floyd-Marshall or simple BFS its to expensive for this case.
I've seen that on DAGs is possible to get the shortest path with a topological sort. In this case it is a good approach ?
I'm assuming here that you have a disjoint partition of your node set into pairs, represented by numbers from 1 to N/2. I'm also assuming that by "there is only one path between any pair of nodes" you really mean any pair and not just those of the same color.
In that case, first realize that your graph is a tree. So root it arbitrarily, and traverse it in depth-first order to compute the depth of all nodes. Note that for two nodes x and y, if their lowest common ancestor is l, then
distance(x, y) = distance(x, l) + distance(y, l)
= depth(x) - depth(l) + depth(y) - depth(l)
= depth(x) + depth(y) - 2*depth(l)
You can use Tarjan's off-line LCA algorithm to compute the LCA of all your pairs in (almost) linear time and compute the distances. You don't even need to store the LCAs in this case.
Runtime: O(n * α(n)) with naive disjoint set union, O(n) with the improvements from "A Linear-Time Algorithm for a Special Case of
Disjoint Set Union", Gabow & Tarjan, 1983

Code on Weighted, Acyclic Graph

We have a Code on Weighted, Acyclic Graph G(V, E) with positive and negative edges. we change the weight of this graph with following code, to give a G without negative edge (G'). if V={1,2...,n} and G_ij be a weight of edge i to edge j.
Change_weight(G)
for t=1 to n
for j=1 to n
G_i=min G_ij for All K
if G_i < 0 (we have a bar on G)
G_ij = G_ij+G_i for all j
G_ki = G_ki+G_i for all k
We have two axioms:
1) the shortest path between every two vertex in G is the same as G'.
2) the length of shortest path between every two vertex in G is the same as G'.
i read one pdf that has low quality, i'm not sure the code exactly mentioned, and add the picture. in this book he say the above axioms is false, anyone could help me? i think these are true?
i think two is false as following counter example, the original graph is given in left, and after the algorithm is run, the result is in right the shortest path between 1 to 3 changed, it passed from vertex 2 but after the algorithm is run it never passed from vertex 2.
Algorithm
My reading of the PDF is:
Change_weight(G)
for i=i to n
for j=1 to n
c_i=min c_ij for all j
if c_i < 0
c_ij = c_ij-c_i for all j
c_ki = c_ki+c_i for all k
The interpretation is that for each vertex we increase its outgoing edges by c_i, and decrease the incoming edges by c_i, where c_i is chosen such that all outgoing edges become non-negative.
Claim 1
"the shortest path between every two vertex in G is the same as G'"
With my reading of the pdf, this claim is true because every path between vertices i and j is changed by the same amount (c_i-c_j) and so the relative order of paths is unchanged. (Note that the path may go via intermediate vertices, but the net effect is 0 because for each intermediate vertex k we decrease the length by c_k when entering, but increase by c_k when exiting.)
Claim 2
"the length of shortest path between every two vertex in G is the same as G'".
This cannot be true - suppose we start with an original graph which has a single edge A to B with weight -1.
In the modified graph this weight will become 0.
Therefore the length of the shortest path has changed from -1 in G to 0 in G' so the statement is false.
Example
Shown below is what would happen to your graph as you applied this algorithm to node 1, followed by node 2:
Topological sort
Note that as shown in the example, we still end up with some negative weights which is probably unintended. This is because the weights of incoming edges are reduced.
However, if we work backwards through the graph (e.g. by using a topological sort), then we will always end up with non-negative weights everywhere.
In the given example, working backwards means we first update 2, and then 1 as shown below:

All pairs shortest path with varying weights

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?

Resources