I am pretty knew to the boost graph library. I need to implement a visitor that would be able to revisit the same vertices multiple times. For the undirectedS graphs (or directed with 2 edges) this would result in the infinite search depth, however I am planning to put a finite constraint on the maximum depth.
I am considering to use breadth_first_search(), and I think what I need to do is to un-mark visited vertices in vis.black_target()? Would you know how to do it?
Thanks!
EDIT: Technically I would like to explore all possible paths of a constant depth from a starting vertex. (even if some of them revisit the same vertices several times)
Related
I need to find an optimal solution for a Travelling Salesman Problem on graphs with the small number of vertices (< 10). Since this is an NP hard problem I am ready to do the brute force approach, for the small number of vertices it should be doable in a very small time.
I have a slightly modified conditions for 2 problems:
(A)
The graph is bidirectional, with different weights in each direction.
All vertices are connected to all.
(Nice to have condition) You can visit the same vertices more than once, and travel the same paths more than once (however for eventual completeness you should not loop infinitely)
(B)
In addition to conditions of (A), here you need to visit a subset of vertices, while you still allow to travel through all other vertices of a graph. (given that it is a better solution).
A while back I have implemented a brute force solution and some heuristics like Lin–Kernighan (using simple matrix of weights), however I never used Graph data structures like in boost. And I was wounding if there is an existing implementation that I could use or a set of algorithms that could help me out to get optimal solution. Also I would appreciate if you could advise on how to get the part (B) right.
Thanks!
I have a large graph (the number of vertices can be in the range of 50,000-100,000, the adjacency matrix need not be sparse). Edges in the graph can be removed/added, and I want to update the resulting connected components structure after such changes. I have implemented this in a straightforward fashion with a BFS search myself in C++ (keeping track of unordered_maps of vertices to connected component ids and updating them), but I am wondering if there is a more efficient way to do this using Boost's graph library.
I was able to find some questions similar to this here in Stackoverflow, and came to know of filtered_graph (and the connected_components function) but I am worried about the overhead involved in creating such filtered instances, every time we add or remove an edge. (Or should this be a concern at all?!)
I believe your solution is essentially the best possible. If you are only allowed to add edges, then I believe the algorithm can be improved by keeping track of connected components in terms of vertices included, and then when an edge is included you check to see if the two vertices belong to different connected components, in which case you merge the two connected components. This will reduce the complexity from quadratic to best-case per edge added. However, if you are allowed to insert and delete edges, I don't see any asymptotically faster way to solve the problem other that what you described.
There are algorithms for maintaining connectivity under edge insertions and deletions that are faster than recalculating. This is called "dynamic graph connectivity". Here is a paper on experimental evaluations (some newer theoretical results have been found since, but it is unclear whether they have practical relevance).
My program starts out by creating a graph (~1K-50K vertices) that usually consists of a few hundred connected components.
The program only needs to be able to manipulate and visualize individual components (using force-directed layout algorithm).
It would be great (but not essential) to have the capability of further splitting each connected component into connected subcomponents (by removing edges or vertices).
So my question is, can I use use subgraph or filtered_graph class templates to achieve the required functionality (maintain a collection of component graphs that can be individually manipulated and possibly further subdivided by removing edges/vertices)? Or is there an another, better approach?
I apologize if this question is too basic. I've just started to learn BGL and not comfortable with this library yet. Thanks in advance!
Use connected_components to assign a unique number to each component, storing it in a property of the nodes. Then you can use that property in the filtered_graph predicate to decide whether a given component belongs to the currently active graph or not. The vertex predicate would be straight-forward, whereas the edge predicate could simply look at either endpoint to make its choice. The number of the subgraph would be stored in the predicate objects themselves.
Whether a different approach would be beter depends on your use case. If the components don't change much, and you have to perform a lot of operations which iterate over all nodes, then having separate graph objects might be better. You could create them as copies of filtered graphs constructed as described above. If the graph gets modified a lot, some approach which will not have to scan the whole graph to update connected components would be useful. If no edges get removed, incremental_components might do the trick.
I need to write a graph using C++ and I have a little problem. My graph should be directed or undirected, weighted or unweighted, based on matrix or list all on user's choice. And distinguishing matrix from list graph is not a big deal, since it's two different classes, I got some problem with other parameters. The most obvious way to distinguish them is to make two bool variables and check them on every adding and deleting of vertex. It is quite obvious and easy to understand, but I doubt it's efficiency, because every time I add or delete vertex I have to do additional if. I also could write subclasses for it, but I seriously doubt if it's worth it.
Every library is okay to use, if it's not representing graph itself.
For directed and undirected best case is using bool variable for your graph, however You can assume your graph is weighted and directed, but for undirected edges add one edge from a→b and one edge from b→a. Also if there isn't weight function set its weight to 1.
But if you looking for graph library it depends to your programming language, but I'd suggest graph boost library which implemented fully in c++, and too many other people implement it partially in other languages.
the depth-first algorithm implemented in the boost library visits each vertex just one time.
Is there any work around to deactivate this option. I want that the vertexes can be visited whenever there is a branch in any vertex.
any suggestion...
EDIT: The graph is acyclic.
If you want to enumerate all paths in an acyclic graph, then I don't think you can easily modify depth-first search to do that. There are algorithms specifically designed for this purpose, in particular: Rubin, F.; , "Enumerating all simple paths in a graph," Circuits and Systems, IEEE Transactions on , vol.25, no.8, pp. 641- 642, Aug 1978.
If you know the Floyd-Warshall algorithm, you can easily modify it to compute a list of paths in each element of the matrix, instead of min distance, which will do the job. The above article uses some bit operations to make this run a bit faster.
want that the vertexes can be visited
whenever there is a branch in any
vertex.
What do you propose that an iterator do when it reaches a branch at a vertex?
Depth first search is just one answer this question. Here are some others.
But you have to choose something. It's not a matter of turning off DFS.
I think that is impossible by design. Because if your graph contains cycles (and you have them there, when you say, that the vertex can be visited more than once), the algorithm will end up in endless loop.