Given a directed acyclic graph G = (V,E) and two distinguished vertices s and t. Both the edges and vertices are assigned real-valued weights. The weight of a path is defined as the sum of all the edges and vertices on the path. The problem is to find a shortest weighted simple path from s to t.
(a) Design a dynamic programming algorithm and briefly describe it.
(b) Design a greedy algorithm and briefly describe it.
(c) Provide upper and lower bounds of one of your algorithms as tight as possible.
How do I do this? Can Dijkstra be used?
I think for greedy algorithm Dijkstra's can be used. I dont know about the rest, sorry mate.
Related
I have a directed graph and I want to find a path that visits every node exactly one time. I want to do this with a good complexity. Is this possible? And if yes, how?
You are searching for a Hamiltonian path, which is a simple open path that contains each node exactly once.
Finding a Hamiltonian path in a given graph is NP-complete. In fact, determining whether a given (directed or undirected) graph contains a Hamiltonian path is already NP-complete (proven via reduction from e.g. the vertex cover problem).
If you still want to code it, here is an implementation on github. If you want a fast solution, maybe a heuristic is sufficient (for instance inspired by DNA molecules, or a solution that works fast on a subset of graphs. For instance, if you have a DAG, you can do a topological sort and then check if successive vertices are connected. If so, the topological sort gives a Hamiltonian path.
I want an algorithm to be able to find an optimal path between two vertices on a graph (with positive int weights).The thing is my graph is relatively big (up to 100 vertices). I have considered the dijkstra algorithm but as I searched the net most implementions use the adjacency matrix which in my case will be 100x100.
If you could recommend me a certain source to read and learn from , or even better provide me with a c++ implementaion it will be great.
PS: The algorithm needs to output the required route and not just the shortest distance between two points.
Thank you for your time.
Have you looked into A*?
Here's a good article to start reading: http://www.redblobgames.com/pathfinding/a-star/introduction.html
I came across this question while finding a solution for a "critical edge" problem. The original (C++) problem, which I have already solved, was:
Consider a graph G=(V,E). Find how many edges belong to all MSTs, how many edges do not belong to any MST and how many edges belong to some MSTs, but not all.
Let's call "green", "red" and "yellow", respectively, the edges in the 3 above cases.
After conducting my research, I came across Find all critical edges of an MST, which solves the problem. One would run a modified version of Kruskal's algorithm: if two or more edges of the same weight connect the same components, thus forming a cycle, then all these are yellow edges, i.e., edges that could be included in the MST (or not). Edges that have been indisputably selected are "green" and edges that create a cycle in the same component are "red". So, the original problem has been solved.
The issue with the above algorithm is that it runs in O( |E| * log|V| ), which is the running time of Kruskal's algorithm (please correct me if I'm wrong). I was considering whether a modified version of Prim's algortihm could also be used, as it has a better amortized complexity of O( |E| + |V| log |V| ), if a Fibonacci heap is used.
My feeling is that a modified version of Prim's algorithm cannot be used here, since we are obliged to iterate all edges based on ascending weight; however, I cannot prove this. So, it is possible to further reduce the complexity of this problem?
This problem is easier than the problem of sensitivity analysis of minimum spanning trees, which is to determine how much each tree/nontree edge can increase/decrease in weight before the minimum spanning tree changes. The best known algorithm for MST sensitivity analysis appears to be due to Seth Pettie (2005, arXived 2014), with a running time of O(|E| log alpha(|E|, |V|)). This is very close to optimal (alpha is inverse Ackermann) but also still superlinear. Several randomized algorithms with linear expected running times are known.
I "found" an algorithm for finding maximum flow in undirected graph which I think isn't correct, but I can't find my mistake. Here is my algorithm:
We construct a new directed graph in the following way: for every edge ${u,v}$ we create edges $(u,v)$ and $(v,u)$ with $c((u,v))=c((v,u))=c({u,v})$. Then we apply Ford-Falkerson's algorithm on new graph. Now we make a flow in our first graph in the following way: Let's $f((u,v))\ge f((v,u))$, than we direct edge ${u,v}$ from $u$ to $v$ and take $f'((u,v))=f((u,v))-f((v,u))$. Now it will be maximum flow for our undirected graph, because otherwise we will construct a flow for corresponding directed graph, which is contradiction.
The reason that I think I have missed something is that there is an article on the Internet about this problem and I don't think anybody would wrote an article about such a trivial problem.
And this is the article: http://www.inf.ufpr.br/pos/techreport/RT_DINF003_2004.pdf
Thanks!
Ford-Fulkerson is not the best algorithm to find maximum flow in a directed graph, and in the undirected case it is possible to do much better (close to linear-time if I recall correctly).
You don't cite the article you are talking about, but it is most likely that it describe an algorithm which is much better than yours.
For many problems in optimization, it is not difficult to find an algorithm that gives an optimal solution; nevertheless there is a lot of work to find the most efficient ones.
This is driving me mad...
I have a directed graph with weighted edges between the nodes.
I need to find the shortest distance from node A and back to node A.
I've tried Djikstras algorithm and Floyd-Warshall but neither quite seem to do the trick. I'm not strong on the maths and most resources I found quickly get overly complex without explaining what I really need to do. Once I understand the steps I can code it, just struggling to find an understandable approach...
Can anyone help?
You want to find the shortest non-trivial cycle containing a particular point. You could do this by taking successive A^n, where A is the adjacency matrix, and finding the first nonzero value on the matrix diagonal corresponding to your point. This is illustrative, but a breadth-first search will work faster. I don't think there is a better way than the BF search, performance wise, though I can't prove that.
Here is some code that implements the search.
http://algs4.cs.princeton.edu/42directed/BreadthFirstDirectedPaths.java.html
You'll have to modify the termination condition, I think.
Alternatively, you could use Dijkstra's and "lift" the point in question into two different start/end points, but this isn't going to be faster for computing just one path, and makes a mess of the graph if you want many paths (point pairs).