I want to write a shortest path program. I know how the algorithm works, but I don't know where to start
Initially, I thought of using an adjacency matrix but then decided against it because of space. Now I think adjacency list would be better.
Can anyone suggest me a websites or tutorials how to start writing adjacency list to give the input to the program?
You might start with Boost::Graph, which will provide you both mechanisms for storing graph data and a structure for writing an algorithm that consumes that data.
Related
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'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.
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).
Im taking a course in algorithms and data structers, and my instructor wants me to implement several data structers (such as BST, stack etc.), and algorithms (such as quick search, DFS, etc.).
I want to belive that I understand the basics, but everytime Im starting to plan the code I have the same difficulty:
here's my current assigment: my instructor wants me to implement a DFS (depth first search) for a directed graph (using c++).
my question is- how do I suppose to implement the graph? should I use adjacency matrix? or should I use adjacency list? neither this nor that??
so I asked my instructor, and his answare was this: "think of the graph as a black box"...
more confused than before, I rashed to stackoverflow, and here i am posting this question...
I dont look for someone to tell me how to implement DFS (or any other algorithm- I can google too!)- I just need someone to explain what should I get as input, and what should I provide as output?
I'll appreciate any comment! thanks!
What he means by a black box is just that you cannot see the nodes and how they connect before you do your DFS. You will probably just get the root node and your algorithm with have to explore from there. As for what you should output- that depends on the assignment. Are you looking for specific data? if not, perhaps a detail of which nodes were visited in which order.
Is there any fast way to determine the size of the largest strongly connected component in a graph?
I mean, like, the obvious approach would mean determining every SCC (could be done using two DFS calls, I suppose) and then looping through them and taking the maximum.
I'm pretty sure there has to be some better approach if I only need to have the size of that component and only the largest one, but I can't think of a good solution. Any ideas?
Thanks.
Let me answer your question with another question -
How can you determine which value in a set is the largest without examining all of the values?
Firstly you could use Tarjan's algorithm which needs only one DFS instead of two. If you understand the algorithm clearly, the SCCs form a DAG and this algo finds them in the reverse topological sort order. So if you have a sense of the graph (like a visual representation) and if you know that relative big SCCs occur at end of the DAG then you could stop the algorithm once first few SCCs are found.