Given an algorithm A that computes the longest path from a source vertex s in a DAG G with non-negative edge weights. What is the minimum number of times required to run the algorithm A to find the longest path in a DAG G?
One way is to figure out the multiple source vertices, this can be achieved in O(|Edges|). And then run Algorithm A with each of these vertices as a source vertex. This will require running algorithm A NumberOfSourceVertices times.
Can we do better ?
Yes, we can do better. Add a new node z to G. For every identified source vertex s, add an edge (z, s, 0) (zero edge weight) to G.
Run A once on the modified G.
Related
I am struggling to perform this in O(|E|log(|V|)) worst-case time complexity where E is the list of edges and V is the vertices in the graph.
So far I tried to use an adjacency list. I first sort the graph from starting vertex u using Kahn's topology algorithm.
I then look for the next target in the sorted topology and push the minimum distance of going from vertex u to v while traversing through the target into a min-heap. I did this by using Dijkstra's algorithm to find the minimum distance from u to the target requiring minimum overall distance. I also find the minimum distance from the target to vertex v.
Edit: Solution for directed graph
I found a solution by creating a copied graph with all the edges in reversee. I run dikjistra from source to destination then from run dikjstra again from destination to source with the graph with all reversed edges. I find overall distancing involving traversing at least one target along the way then I reuse the distance I got from the previous dikjistra executions to find the shortest path.
let DistSrc[i] denote the shotest distance of node i from source(here u) and DistDes[i] denote the shotest distance of node i from destination.
Both DistSrc and DistDes can be found by dijkstra algorithm.
iterate over all target nodes (say ti) and and take the minimum of (DistSrc[ti]+DistDes[ti]) over all ti.
Given a undirected graph that it has ordinary edges and specific edges, our goal is to find the sum of the shortest path's weight between two vertices(start vertex to end vertex) with only walk through specific edge equal or less than one time. In other words, there are multiple specific edges, and only at most one of them can be used.
This is a problem that I faced in my Data-Structure homework, and I stuck at the first step of the way to storage the weights of the edge in Graph. Because there are two kinds of edge in Graph, I have no idea that how to solve this problem.
I know that I can obtain the shortest path by using Dijkstra’s Algorithm, but during the process, how can I modify the Algorithm to meet the requirement of the restriction?
Thanks a lot for answering my question!
The solution is to duplicate the graph as follows:
Duplicate the vertices, such that for each original vertex A, you have an A and an A'.
If in the original graph there is a normal edge between A and B, then in the new graph, place an edge between A and B and also between A' and B'
If in the original graph there is a specific edge between A and B, then in the new graph place a (directed) edge from A to B' (not the inverse!) and from B to A' (again: not the inverse!). These edges should be directed.
If now the task was to find the shortest path between S and D, then solve in the new graph the problem of finding the shortest path between S and D or S and D', which ever is shortest. You can use a standard implementation of Dijkstra's algorithm for that, starting in S and ending when you find either D or D'.
Given n specific edges run Dijkstra's search n times.
On each run, one of the n nodes (let call it node i) should be set to its real weight and all other n-1 nodes to an infinite value.
At the end of each run store the shortest path and step i
At the end of all runs select from the stored paths the shortest one.
set all n edges weight to infinity
for i=0; i < n ; i++ {
set edge i to it real weight
run run Dijkstra's search
store path
set all n edges weight to infinity
}
select the shortest path from the stored paths.
I have a graph with about 6500 vertices, some of which have the label 'c'. I need to devise an algorithm that finds the shortest path between any two of the vertices that includes AT LEAST ONE of these 'c' vertices. This is simple enough, but the problem is that the required complexity is O(ElogV) where E is the number of edges, and V is the number of vertices. I have already implemented Dijkstra's Algorithm using a min-heap, so I can find the general shortest path in O(ElogV), but I am having trouble extending the problem. Any suggestions?
Note that calling Dijkstra's from source to c + c to destination iteratively does not fall within the complexity restraints, as it ends up being O(CElogV)
Let G be your graph, with vertices v1...vn.
Make a new graph that consists of two copies of your original vertices: v1..vn, v1'..vn'. In this new graph, let there be an edge between vi and vj or vi' and vj' if there's an edge between vi and vj in your original graph. Also let there be an edge between vi and vj' if there's an edge between vi and vj in your original graph, and vj' is labelled c.
Then, given two vertices vi, vj, the shortest path between vi and vj' in the new graph is the shortest path between vi and vj in the original graph that passes through at least one vertex labelled c.
Because the number of vertices in the new graph is doubled, and the number of edges at most tripled, the complexity doesn't change from O(VlogE) (where V and E are the number of vertices/edges in the original graph).
If you have an undirected graph:
Say you're searching for a shortest path between S and T.
find all the shortest paths from S to any node using Dijkstra's algorithm
find all the shortest paths from T to any node (same algorithm)
iterate over all marked nodes c and find the shortest path S to c combined with c to T using previously calculated shortest paths.
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.
Is there a graph algorithm that given a start(v) and an end(u) will find a shortest path through the given set of edges, but if u is a disconnected vertex, it will also determine the shortest path to add missing edges until u is no longer disconnected?
I have a pixel matrix where lines are made of 255's(black) and 0's(white). lines(255) can have breaks or spurs and I must get rid of both. I could have a pixel matrix forest with say 7 or so trees of black pixels. I need to find the true end points of each tree, find the single shortest path of each tree, then union all skew trees together to form 1 single line(ie, a single shortest path from the furthest 2 end points in the original matrix). all edge weights could be considered 1.
Thanks
How about running Dijkstra's algorithm and if disconnected, connect v and u? What's your criteria for "best place to add a missing edge?" Do edges have weights (like distance)?
Edit:
For one idea of "the best place," you could try the path that has minimal sum of shortest paths between all connected pairs. Floyd–Warshall algorithm can be used to find shortest paths between all pairs. So, run Floyd-Warshall for each node in v's tree and u.
Your problem isn't well defined for disconnected graphs. I can always add and edge between v and u.
If you meant that given an acyclic undirected disconnected graph, actually known as a forest, and given a subset of edges as a subgraph, can you find the shortest path between vertices, than this problem is trivial since if there is a path in the full graph, there is one path only.
If this is a general graph G, and you are talking about a forest subgraph G', than we need more info. Is this weighted? Is it only positive weights? If it is unweighted, do some variant of Dijksta. Define the diameter of a tree to be the length of the longest path between two leaves (this is well defined in a tree, since ther is only one such path). Let S be the sum of the diameters of all the trees in G', then set the weight all edges out of G' to 2S, then Dijkstra's algorithm will automatically prefer to step through G', stepping outside G' only when there is no choice, since walking through G' would always be cheaper.
Here you have following scenarios:
case:1. (all edges>0)
Negate all edges
find the longest path in graph using this:
https://www.geeksforgeeks.org/longest-path-undirected-tree/
negate the final result
case2: edges might be or might not be negative
Read Floyd–Warshall algorithm for this