How do I construct an efficient algorithm for finding vertex which is furthest from the set S of vertices? - algorithm

Given a undirected weighted graph with n vertices and m edges. How do I construct an algorithm which takes at most O((n+m) log(n+m)) for finding the vertex which it's min shortest path distance to a set of vertices S \in V is maximized?
I know I can loop through all vertices and using dijkstra's algorithm find the shortest path to each of the vertices in S but that will surely take much more than O((n+m) log(n+m)).

I am assuming from your ideas (possible use of dijkstra's shortest path algorithm) that weight of every edge is greater or equal than zero.
A clean solution to your problem is to create a new vertex v and add zero weight edges between v and every vertex in S. Run dijkstra's algorithm from v and the answer you look for is the farthest vertex from v.
You can prove by yourself that solution of your problem is equal to the one calculated on the new graph as it is straighforward. Also, it only run dijkstra's algorithm once so it fits on your time complexity constraints.

Related

Backtrack after running Johnson algorithm

I have a question which I was asked in some past exams at my school and I can't find an answer to it.
Is it possible knowing the final matrix after running the Johnson Algorithm on a graph, to know if it previously had negative cycles or not? Why?
Johnson Algorithm
Johnson's Algorithm is a technique that is able to compute shortest paths on graphs. Which is able to handle negative weights on edges, as long as there does not exist a cycle with negative weight.
The algorithm consists of (from Wikipedia):
First, a new node q is added to the graph, connected by zero-weight edges to each of the other nodes.
Second, the Bellman–Ford algorithm is used, starting from the new vertex q, to find for each vertex v the minimum weight h(v) of a path from q to v. If this step detects a negative cycle, the algorithm is terminated.
Next the edges of the original graph are reweighted using the values computed by the Bellman–Ford algorithm: an edge from u to v, having length w(u, v), is given the new length w(u,v) + h(u) − h(v).
Finally, q is removed, and Dijkstra's algorithm is used to find the shortest paths from each node s to every other vertex in the reweighted graph.
If I understood your question correctly, which should have been as follows:
Is it possible knowing the final pair-wise distances matrix after running the Johnson Algorithm on a graph, to know if it originally had any negative-weight edges or not? Why?
As others commented here, we must first assume the graph has no negative weight cycles, since otherwise the Johnson algorithm halts and returns False (due to the internal Bellman-Form detection of negative weight cycles).
The answer then is that if any negative weight edge e = (u, v) exists in the graph, then the shortest weighted distance between u --> v cannot be > 0 (since at the worst case you can travel the negative edge e between those vertices).
Therefore, at least one of the edges had negative weight in the original graph iff any value in the final pair-wise distances is < 0
If the question is supposed to be interpreted as:
Is it possible, knowing the updated non-negative edge weights after running the Johnson Algorithm on a graph, to know if it originally had any negative-weight edges or not? Why?
Then no, you can't tell.
Running the Johnson algorithm on a graph that has only non-negative edge weights will leave the weights unchanged. This is because all of the shortest distances q -> v will be 0. Therefore, given the edge weights after running Johnson, the initial weights could have been exactly the same.

Find the lowest-weight cycle in a weighted, directed graph using Dijkstra's

Hi I am struggling with this question. It is the following:
Devise an algorithm to find the lowest-weight cycle(i.e. of all cycles in the graph, the one with the smallest sum of edge weights), in a weighted, directed graph G = (V,E). Briefly justify the runtime and space complexity. Assume all edges are non-negative. It should run in O(|V||E|log|V|) time.
Hint: Use multiple calls to Dijkstra's algorithm.
I have seen solutions that use Floyd-Warshall but I was wondering how we would do this using Dijkstra's and how to do it within the time constraint given.
I have a few points of confusion:
Firstly, how do we even know how many cycles are in the graph and how
to check those?
Also, why is it |E||V|log|V|? By my understanding you should traverse
through all the vertices therefore making it |V|log|V|.
This is for my personal learning so if anyone has an example they could use, it would greatly help me! I am not really looking for pseudo-code - just a general algorithm to understand how using the shortest path from one node to all nodes is going to help us solve this problem.
Thank you!
Call Dijkstra's algorithm from each vertex to find the shortest path to itself, if one exists. The shortest path from any vertex to itself is the smallest cycle.
Dijkstra's algorithm takes O(|E| log |V|), so total time is O(|V||E| log |V|).
Note that this time can be worse than Floyd-Warshall, because there can be O(|V|^2) edges in the graph.
Call Dijkstra's algorithm |V| times, using each vertex in V as the start vertex. Store the results in a matrix, where dist(u,v) is the shortest path length from u to v.
For each pair of vertices (u,v), dist(u,v) is the shortest parth from u to v and dist(v,u) is the shortest path from v to u. Therefore, dist(u,v) + dist(v, u) is the length of the lowest weight cycle containing u and v. For each pair of vertices, computer this value and take the minimum. This is the lowest weigtht cycle.

Multi-start and Multi-end shortest path set

I am having problem with shortest path in directed weighted graph. I know Dijkstra, BFS, DFS. However, I have a set of vertices S for starting points and a set of vertices E to end. S and E doesn't overlap. So how can I find the set of edges with minimal sum of edge weight? The edge set doesn't have to include all vertices in S, but have to reach all vertices in E. Should I start with Dijkstra on all permutation of {Si, Ei} and optimize or I miss any important algorithm I should know? Or even I am over-thinking....
If I understand you correctly, you want to find the tree of minimal weight in the graph that contains all the vertices of E and at least one vertex from S.
The problem is called general Steiner tree, and it is NP-hard. So the best you can probably hope for is an exponential-time algorithm or some kind of approximation (the minimum spanning tree of the whole graph comes to mind, maybe after removing some unneeded subtrees).
There is a simple DP solution that works in O(2^n * (n + m)): Let f(S) be the cost of the minimum tree in the graph that spans all the nodes in S. It can be shown that there is such a tree T such that the weight of T \ {x} is f(S \ {x}) for some x, so the transition can be done in O(n + m).

Why do all-pair shortest path algorithms work with negative weights?

I've been studying all-pair shortest path algorithms recently such as Floyd-Warshall and Johnson's algorithm, and I've noticed that these algorithms produce correct solutions even when a graph contains negative weight edges (but not negative weight cycles). For comparison, Dijkstra's algorithm (which is single-source shortest path) does not work for negative weight edges. What makes the all-pair shortest path algorithms work with negative weights?
Floyd Warshall's all pairs shortest paths algorithm works for graphs with negative edge weights because the correctness of the algorithm does not depend on edge's weight being non-negative, while the correctness of Dijkstra's algorithm is based on this fact.
Correctness of Dijkstra's algorithm:
We have 2 sets of vertices at any step of the algorithm. Set A consists of the vertices to which we have computed the shortest paths. Set B consists of the remaining vertices.
Inductive Hypothesis: At each step we will assume that all previous iterations are correct.
Inductive Step: When we add a vertex V to the set A and set the distance to be dist[V], we must prove that this distance is optimal. If this is not optimal then there must be some other path to the vertex V that is of shorter length.
Suppose this some other path goes through some vertex X in the set B.
Now, since dist[V] <= dist[X] , therefore any other path to V will be atleast dist[V] length, unless the graph has negative edge lengths.
Correctness of Floyd Warshall's algorithm:
Any path from vertex S to vertex T, will go through any other vertex U of the graph. Thus the shortest path from S to T can be computed as the
min( shortest_path(S to U) + shortest_path(U to T)) for all vertices U in the graph.
As you can see there is no dependence on the graph's edges to be non-negative as long as the sub calls compute the paths correctly. And the sub calls compute the paths correctly as long as the base cases have been properly initialized.
Dijkstra's Algorithm doesn't work for negative weight edge because it is based on the greedy strategy(an assumption) that once a vertex v was added to the set S, d[v] contains the minimum distance possible.
But if the last vertex in Q was added to S and it has some outgoing negative weight edges. The effects on the distance that caused by negative edges won't count.
However, all pairs shortest paths algorithm will capture those updates.

Computing another graph with edges of exactly length l from an unweighted, undirected graph

What is a method of making another graph featuring vertices that can be only gotten to with an edge length of l from every vertex V in the original unweighted (assume edges of length 1) and undirected graph G=(V,E). I came up with a solution that just searches through each branch from each V using depth-first search on each vertex until I found all the vertices of path length l from each vertex. This gives a runtime of O(V^(l+1)) so of course, this is not the optimal solution. Can anyone help me find a better solution with a better asymptotic runtime?
You could use the Floyd-Warshall algorithm that uses a matrix representation (as #Hammar suggested) but finishes in O(V^3) regardless of l. Instead of l matrix exponentiations, you determine all distances by sequentially inserting nodes and determining the effect on the shortest paths.

Resources