Finding all full paths in directed graph with cycles - graph

I need to be able to find all paths in a directed graph, which can include cycles. Due to the nature of the graph, I need find the full path in the case of a cycle even after looping since I need to accumulate values along the path from src to dest. For example in the graph below, if asked for paths from 1 to 5, I'd like it to find 1,2,3,4,5 and 1,2,3,4,6,2,3,4,5.
1->2->3->4->5
^ |
| |
-6 <-

Related

Neo4j: Shortest path between known start node and multiple end nodes

I have a graph with ~10000 nodes, I'm trying to find the longest path between a known start node and an unknown end node but of a known type Reader.
When I had 1000 nodes this computed fine in 4 seconds but now it is taking a long time, presumably because of the large list of possible end nodes.
I'm wondering if there is a better algorithm to do this, my only possible thought is to add some filtering to p2 to shorten the size of the list however this is not ideal as I would like to match a lot of edge cases.
MATCH (p1:Book{id: '0532013000'})-[r*1..5]-(p2:Reader)
CALL apoc.algo.dijkstraWithDefaultWeight(
p1, p2, 'REL_ONE|REL_TWO|REL_THREE|REL_FOUR', 'mean', 1) YIELD path, weight
RETURN path, weight
ORDER by weight DESC
LIMIT 5

Volume identification from a graph where vertices refer to surfaces

here's a question that's been bothering me for the last couple of days - my colleague believes this is a well-researched problem, but I couldn't find any papers online (perhaps the key words I used were incorrect).
Given a graph where each vertex corresponds to a surface and an edge exists between two vertices if the corresponding surfaces meet at a line, how would I go about identifying the surfaces that combine to form closed volumes?
So for instance, the adjacency list for a cube (with surfaces numbered 1 to 6) might look something like this:
1 -> [4,2,5,6]
2 -> [1,3,5,6]
3 -> [2,4,5,6]
4 -> [3,1,5,6]
5 -> [1,2,3,4]
6 -> [1,2,3,4]
In addition to this information, I also know if some surfaces have free-edges (i.e those edges are not shared with any other surface) which means I can immediately exclude these surfaces since a surface with a free-edge cannot be part of a cavity boundary. Also, if two surfaces meet, they will meet cleanly at a boundary - no glancing blows etc.
What I hope to be able to do is not just identify that a cavity exists but also to output a mapping between surfaces and the cavities they contain. For instance, in the case of two cubes which meet at an edge (not enough reputation to post an image, so here's a side view):
-----------
| |
| |
| |
---------------------
| |
| |
| |
-----------
...this would be the desired output assuming surfaces 1-6 make up cube ONE and 7-12 make up cube TWO:
Volume 1 -> [1,2,3,4,5,6]
Volume 2 -> [7,8,9,10,11,12]
Note that, in this case, some surfaces have SIX neighbors in the graph while others have only FOUR neighbors.
Any help would be much appreciated!
Yes, this area is known as computational topology. Its central objects are algebraic groups of loops known as homology groups (H1 when loops are formed by 1-dim edges and H2 when we talk about 2-dim faces).
One can analyze such group by forming a homology group matrix. For some queries both H1 and H2 matrices are needed.
When matrix can be re-arranged into independent blocks it means shapes are not interacting. When a single-block homology matrix has ranking N it means shape has N cavities (holes). Simple shape like cube has 0 holes, torus has 1,etc.
One can find C++ and MATLAB open-source packages for these computations.
Hope it gives some helpful keywords.

Cut Sets in a graph

I have a question regarding the maximum flow in a network. I was trying to find a cut set in a graph that could disconnect the source and the destination. I explored all the edge independent paths in the graph from a source to the destination. Next I picked an edge from each of these paths and grouped them together. So basically I enumerated all the possible combinations of taking one edge from each path.
So I have a set of such groups. Does this mean that I have eventually found the cut sets of the network for that particular source and destination? Is this an efficient method?
This sounds like it has exponential complexity. I can't say exactly, because I don't know what "all the edge independent paths" means. For example:
A
|
B
/ \
C D
\ /
E
There are two paths from A to E, but they're not edge independent.
A maximum flow on a graph is dual to a minimum cut, and there are plenty of standard algorithms that can find one in (small) polynomial time. If you're satisfied with any cut at all, just remove all the edges -- this runs in time O(E).
What are your constraints?

Sequence of number of vertices in a graph

I want to generate a sequence of the number of vertices in all graphs which each edge has the same number of leaving edges. I dont have to generate the whole sequence. Let's say the first 50 if exists.
I want:
Input: the number of edges leaving each vertex
Output: a sequence of the number of vertices
So far, I have looked at complete graphs. Complete graphs with n vertices always have n-1 edges leaving each vertex. But there are other kinds of graphs that have this property. For example, some polyhedrons, such as snub dodecahedron and truncated icosidodecahedron have this property.
How should I approach my problem?
I think you mean regular graphs:
http://en.wikipedia.org/wiki/Regular_graph
http://mathworld.wolfram.com/RegularGraph.html
I made a regular graph generator which isn't flawless by the way:
once you generate the nodes, say from 1 to n. You want regularity r.
For node 1, make connections to the following nodes until you reach degree r for node 1.
For node 2 you already have degree 1 (because of node 1), you connect again to further nodes until you reach degree r for node 2 too. And this way till the last node.
The flaw is that you can't define an r-regular graph for any number of nodes. The algorithm mentioned doesn't detect that, so some errors may occur. Also, this isn't a random r-regular graph generator, but instead give one possible solution.
I'm not much of an explainer, so please ask if the description lacks somewhere.

How can I find all 'long' simple acyclic paths in a graph?

Let's say we have a fully connected directed graph G. The vertices are [a,b,c]. There are edges in both directions between each vertex.
Given a starting vertex a, I would like to traverse the graph in all directions and save the path only when I hit a vertex which is already in the path.
So, the function full_paths(a,G) should return:
- [{a,b}, {b,c}, {c,d}]
- [{a,b}, {b,d}, {d,c}]
- [{a,c}, {c,b}, {b,d}]
- [{a,c}, {c,d}, {d,b}]
- [{a,d}, {d,c}, {c,b}]
- [{a,d}, {d,b}, {b,c}]
I do not need 'incomplete' results like [{a,b}] or [{a,b}, {b,c}], because it is contained in the first result already.
Is there any other way to do it except of generating a powerset of G and filtering out results of certain size?
How can I calculate this?
Edit: As Ethan pointed out, this could be solved with depth-first search method, but unfortunately I do not understand how to modify it, making it store a path before it backtracks (I use Ruby Gratr to implement my algorithm)
Have you looked into depth first search or some variation? A depth first search traverses as far as possible and then backtracks. You can record the path each time you need to backtrack.
If you know your graph G is fully connected there is N! paths of length N when N is number of vertices in graph G. You can easily compute it in this way. You have N possibilities of choice starting point, then for each starting point you can choose N-1 vertices as second vertex on a path and so on when you can chose only last not visited vertex on each path. So you have N*(N-1)*...*2*1 = N! possible paths. When you can't chose starting point i.e. it is given it is same as finding paths in graph G' with N-1 vertices. All possible paths are permutation of set of all vertices i.e. in your case all vertices except starting point. When you have permutation you can generate path by:
perm_to_path([A|[B|_]=T]) -> [{A,B}|perm_to_path(T)];
perm_to_path(_) -> [].
simplest way how to generate permutations is
permutations([]) -> [];
permutations(L) ->
[[H|T] || H <- L, T <- permutations(L--[H])].
So in your case:
paths(A, GV) -> [perm_to_path([A|P]) || P <- permutations(GV--[A])].
where GV is list of vertices of graph G.
If you would like more efficient version it would need little bit more trickery.

Resources