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.
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
Given a weighted directed graph with n vertices where edge weights are integers (positive, zero, or negative), determining whether there are paths of arbitrarily large weight can be performed in time -
O(n)
O(n.log(n)) but not O(n)
O(n^1.5) but not O(nlogn)
O(n^3) but not O(n^1.5)
O(2n) but not O(n^3)
I'm not understanding what algorithm to use as finding the longest path is a NP Hard problem. Though, the answer given is O(n^3)
Briefly, you have to negate weights and then run the Floyd-Warshall algorithm. It takes O(n^3).
As mentioned here,
The graphs must be acyclic, otherwise paths can have
arbitrarily large weights. We can find longest paths just by negating all of the
edge weights, and then using a shortest path algorithm. Unfortunately, Dijk
stra’s algorithm does not work if edges are allowed to have negative weights.
However, the Floyd-Warshall algorithm does work, as long as there are no
negative weight cycles, and so it can be used to find longest weight paths in
acyclic graphs.
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.
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.
The graph is unweighted and undirected.
Given two vertices s and t, and stride k, I want to find the shortest path between them that is:
Of length divisible by k
Every k "steps" in the path, counting from the first, are a simple path.
Meaning, the shortest non-simple path that can only "jump" k vertices at every step (exactly k), and every "jump" must be a simple sub-path.
I think this problem is equal to constructing a second graph, G', where (u, v) are an edge in G' if there exists a path of length k in G, because a BFS scan would give the required path -- but I haven't been able to construct such a graph in reasonable time (apparently it's an NP problem). Is there an alternative algorithm?
The problem you're describing is NP-hard overall because you can reduce the Hamiltonian path problem to it. Specifically, given an n-node graph and a pair of nodes s and t, you can determine whether there's a Hamiltonian path from s to t by checking if the shortest (n-1)-stride path from s to t has length exactly n-1, since in that case there's a simple path from s to t passing through every node once and exactly once. As a result, if k is allowed to be large relative to n, you shouldn't expect there to be a particularly efficient algorithm for solving this problem.
In case it helps, there are some fast algorithms for finding long simple paths in graphs that might work really well for reasonable values of k. Look up the color-coding technique, in particular, as an example of this.