Given a directed graph with edges having -ve or +ve weights, what is the algorithms to find the smallest weight edges of all paths from vertex s to vertes d?
From Wikipedia
You are describing the Single Source shortest path problem. Which can be solved using Dijkstra's if the edges are only positive or Bellman-Ford if the edges are allowed to be negative as well.
The most important algorithms for solving this problem are:
Dijkstra's algorithm solves the single-source shortest path problem.
Bellman–Ford algorithm solves the single-source problem if edge weights may be negative.
A* search algorithm solves for single pair shortest path using heuristics to try to speed up the search.
Floyd–Warshall algorithm solves all pairs shortest paths.
Johnson's algorithm solves all pairs shortest paths, and may be faster than Floyd–Warshall on sparse graphs.
Viterbi algorithm solves the shortest stochastic path problem with an additional probabilistic weight on each node.
I would find all reachable from s, e.g. by depth first search. Then find all nodes that can reach d (equivalently, all nodes reachable from d in the graph with directions reversed). Now you want the smallest weight edges that start in the first set and end in the second set.
Related
I could only think of a trivial solution to find all cycles in the graph and then find the number of edges in each cycle and then return the one with maximum edges.
How do I find a longest cycle using a greedy algorithm?
"The longest simple cycle problem is the problem of
finding a cycle of maximum length in a graph. As a
generalization of the Hamiltonian cycle problem, it is NPcomplete on general graphs and, in fact, on every class of graphs
that the Hamiltonian cycle problem is NP-complete. The longest
simple cycle problem may be solved in polynomial time on
the complements of comparability graphs. It may also be solved
in polynomial time on any class of graphs with bounded tree
width or bounded clique-width, such as the distance-hereditary
graphs. However, it is NP-hard even when restricted to split
graphs, circle graphs, or planar graphs. In this paper a heuristic
algorithm is proposed which can solve the problem in polynomial
time. To solve the longest simple cycle problem using adjacency
matrix and adjacency list by making a tree of given problem to
find the longest simple cycle as the deepest path in tree following
reconnect the leaf node of deepest path with root node. The result
resolves the open question for the complexity of the problem on
simple unweighted graphs. The algorithm is implemented on a
simple labeled graph without parallel edges and without selfloop. The worst case time complexity for the proposed algorithm
is O(V+E).
The Longest Simple Cycle Algorithm
In the Proposed Algorithm, the input graph considered to be a
simple graph (i.e. without self-loop and without parallel
edges), the algorithm for Longest Simple Cycle in simple
graph is summarized below:
Enumerate all the nodes to calculate degree of each node to
find the node with highest degree.
Assign the node with highest degree as the root for tree.
Construct a tree T of the given graph G considering the
adjacent nodes as successor and predecessors accordingly
for each vertex using adjacency matrix.
Do apply the proposed LSC algorithm to find the longest
path.
Join the leaf node of the longest path with root and retrieve
the path considering it as the longest cycle in graph."
A Heuristic Algorithm for Longest Simple Cycle Problem
Assume that we have an undirected graph G=(V,E) and we have two nodes S and X.
Can we come up with an algorithm such that the largest weight of the edge on the path from S to X is minimized? Note that it is not the shortest path algorithm since we are not interested in minimizing their sum.
What is the complexity of this algorithm?
Is the minimum spanning tree algorithm (such as Prim) is a solution for the problem?
I don't know if minimum spanning tree will solve it or not, but it's certainly solvable by making some modifications to Dijkstra's algorithm.
Define the "length" of a path as the maximum edge in that path. Now find the shortest path from S to X using Dijkstra's algorithm. This is the path you are looking for.
Complexity is O((N+M)log N) if you use a binary heap and O(N * log N + M) with a Fibonacci heap.
Note that for this new definition of path length, if the length of a path is l, then adding an edge to the end of the path will not decrease it's length, since the maximum edge in that path can only increase. This property is necessary for Dijkstra's algorithm to work correctly.
For instance, if you were looking for the path with the shortest edge, then Dijkstra's algorithm will fail just like it fails when there are negative edges in the graph.
You can use minimum spanning tree (Prim's algorithm) to solve this problem. You will start with vertex S, then continue to build the tree using Prim's algorithm until you find X. Complexity will be O((V+E)*logV).
It will work because in the prim's algorithm you always choose the edge with minimum weight first.
Given: undirected weighted connected graph. s,t are vertices.
Question: Find an algorithm as efficient as possible that returns a path from s to t. In that path, the edge that has the highest weight, will has the least weight as possible. So if we have 5 paths from s,t and for every path we have the heaviest edge, so the minimum edge of these 5.
What I've tried:
Use some algorithm to find the shortest path between s and t.
Delete all the edges that are not part of the shortest paths we've found
Use BFS with some modification, We run BFS depending on the number of paths from s to t. Every time we find a maximum edge and store it in an array, then we find the minimum of the array.
I'm struggling to find an algorithm that can be ran in (1), Bellman ford won't work - because it has to be directed graph. Dijkstra won't work because we don't know if it has negative circles or negative edges. And Prim is for finding MST which I'm not aware of how it can help us in finding the shortest path. Any ideas?
And other from that, If you have an algorithm in mind that can solve this question, would be much appreciated.
You can solve this with Kruskal's algorithm. Add edges as usual and stop as soon as s and t are in the same cluster.
The idea is that at each stage of the algorithm we have effectively added all edges below a certain weight threshold. Therefore, if s and t are in the same cluster then there is a route between them consisting entirely of edges with weight less than the threshold.
You can solve it by converting into a MST problem, basically the path from s to t in the MST would be the path which has the least possible maximum weight
find the most negative edge in the graph
add that (weight+1) to every edge.
Now all edge are positive so you can apply Dijkstra's algorithm
you can get the shortest_path between source and destination
Now count the number of edges between source and destination (say x)
Real shortest path will be: shortest_path - x * (weight+1)
What is the most efficient shortest path algorithm performed on a graph that is not directed and only has positive edges out of these five algorithms?
BFS
DAG
Dijkstra
Floyd-Warshall
Bellman-Ford
So I know Dijkstra's can't be used on negative edges and has a running time of O(E * logV) where E is the number of edges and V is the number of vertices, so this would be my best guess. Is this correct?
If you need to find the shortest path in an unweighted graph, BFS would be the best option, however if there are weights on the edges, and you only need to find the optimal route between a single source and one or many other nodes, Dijkstra would be the best option. If you need to find the shortest path between any two pairs of nodes(i.e. have multiple sources), the best option is Floyd-Warshall.
I was wondering if there is an algorithm which would find shortest paths in graph.
Let's say that I have a graph where there are couples of path from one vertex to another. Two or more of these paths have the same cost. How can I mark, find etc all shortest paths between these vertices ? As far as I know Dijkstra or Bellman-Ford algorithms will find shortest path but they "choose" only one.
Dijkstra's algorithm gives you the cost to all the possible intermediate nodes, and the cost of the shortest path to the sink. You can get all the paths from source to sink by doing a depth first search from the sink to the source (going backwards), where you traverse an edge (backwards) only if the cost of that edge is equal to the difference between cost of the shortest path from the source to the two nodes. Of course you get the paths in reverse order, but reversing them is easy.
.
Take a look at Floyd-Warshall.
In computer science, the
Floyd–Warshall algorithm (sometimes
known as the WFI
Algorithm or
Roy–Floyd algorithm) is a graph
analysis algorithm for finding
shortest paths in a weighted graph
(with positive or negative edge
weights). A single execution of the
algorithm will find the lengths
(summed weights) of the shortest paths
between all pairs of vertices though
it does not return details of the
paths themselves. The algorithm is an
example of dynamic programming.