Point to point path in a Graph - c++

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

Related

Ford-Falkerson's algorithm for undirected graphs (What am I missing?)

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.

C++ generate random graphs suitable for TSP

I'm testing various TSP models/algorithms. Right now I'm using a full adjacency matrix, filled with random values from 1 to 100, which represents a complete directed graph.
I'm searching for a more rigorous approach that would allow me to try different kinds of random graphs, like Erdos-Renyi, small world networks and scale free networks.
I know I may have to switch to adjacency lists for the new graphs.
My approach would be generating a random graph and then ensuring there is the Hamiltonian path necessary for the problem to be a valid TSP instance. Is it possible, or is it cheaper to just try and solve the unsolvable instance (assuming all methods will terminate on such instance)?
BTW I was thinking of using the Boost Graph Library, but I'm not familiar with it, and maybe there's something more appropriate. Suggestions for alternatives are welcome, but should not be considered the main scope of this question.
I don't need a TSP solver, I need something to aid in the generation of acceptable problems.
Thanks.
You can try a monotone gray code, a.k.a a hilbert curve. It can help find a hamiltonian path:http://en.m.wikipedia.org/wiki/Gray_code.
I'm searching for a more rigorous approach that would allow me to try
different kinds of random graphs,
Check Mathematica. It has a built-in predicate to test whether a given graph has hamiltonian path or not. It also, has a predicate to generate random Hamiltonian Graphs.
In addition, If you have not try it yet, TSBLIB contains (hard and easy) instances that you may find them useful.

Shortest distance from a node back to itself in a weighted, directed graph

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).

How to implement TSP with dynamic in C++

Recently I asked a question on Stack Overflow asking for help to solve a problem. It is a travelling salesman problem where I have up to 40,000 cities but I only need to visit 15 of them.
I was pointed to use Dijkstra with a priority queue to make a connectivity matrix for the 15 cities I need to visit and then do TSP on that matrix with DP. I had previously only used Dijkstra with O(n^2). After trying to figure out how to implement Dijkstra, I finally did it (enough to optimize from 240 seconds to 0.6 for 40,000 cities). But now I am stuck at the TSP part.
Here are the materials I used for learning TSP :
Quora
GeeksForGeeks
I sort of understand the algorithm (but not completely), but I am having troubles implementing it. Before this I have done dynamic programming with arrays that would be dp[int] or dp[int][int]. But now when my dp matrix has to be dp[subset][int] I don't have any idea how should I do this.
My questions are :
How do I handle the subsets with dynamic programming? (an example in C++ would be appreciated)
Do the algorithms I linked to allow visiting cities more than once, and if they don't what should I change?
Should I perhaps use another TSP algorithm instead? (I noticed there are several ways to do it). Keep in mind that I must get the exact value, not approximate.
Edit:
After some more research I stumbled across some competitive programming contest lectures from Stanford and managed to find TSP here (slides 26-30). The key is to represent the subset as a bitmask. This still leaves my other questions unanswered though.
Can any changes be made to that algorithm to allow visiting a city more than once. If it can be done, what are those changes? Otherwise, what should I try?
I think you can use the dynamic solution and add to each pair of node a second edge with the shortest path. See also this question:Variation of TSP which visits multiple cities.
Here is a TSP implementation, you will find the link of the implemented problem in the post.
The algorithms you linked don't allow visiting cities more than once.
For your third question, I think Phpdna answer was good.
Can cities be visited more than once? Yes and no. In your first step, you reduce the problem to the 15 relevant cities. This results in a complete graph, i.e. one where every node is connected to every other node. The connection between two such nodes might involve multiple cities on the original map, including some of the relevant ones, but that shouldn't be relevant to your algorithm in the second step.
Whether to use a different algorithm, I would perhaps do a depth-first search through the graph. Using a minimum spanning tree, you can give an upper and lower bound to the remaining cities, and use that to pick promising solutions and to discard hopeless ones (aka pruning). There was also a bunch of research done on this topic, just search the web. For example, in cases where the map is actually carthesian (i.e. the travelling costs are the distance between two points on a plane), you can exploit this info to improve the algorithms a bit.
Lastly, if you really intend to increase the number of visited cities, you will find that the time for computing it increases vastly, so you will have to abandon your requirement for an exact solution.

Finding all paths of a certain length in a weighted undirected graph

I need to generate all paths that are lesser than or equal to a specified length in a graph (the graph is undirected and it's possible to have cycles). I tried using BFS while keeping track of the distance already traversed but I'm not sure how I would ensure that every path is different.
Note: I know this probably has a very high computational complexity, but I'm not worrying about that for now.
Using BFS is kind of the right way to do so.
But you additionally have to keep track of already found nodes.
There is a simple algorithm from Dijkstra solving this for you