Find All Cycle Bases In a Graph, With the Vertex Coordinates Given - graph

A similar question is posted here.
I have an undirected graph with Vertex V and Edge E. I am looking for an algorithm to identify all the cycle bases in that graph. An example of such a graph is shown below:
Now, all the vertex coordinates are known ( unlike previous question, and contrary to the explanation in the above diagram), therefore it is possible to find the smallest cycles that encompass the whole graph.
In this graph, it is possible that there are edges that don't form any cycles.
What is the best algorithm to do this?
Here's another example that you can take a look at:
Assuming that e1 is the edge that gets picked first, and the arrow shows the direction of the edge.

I haven't tried this and it is rather greedy but should work:
Pick one node
Go to one it's neighbors's
Keep on going until you get back to your starting node, but you're not allowed to visit an old node.
If you get a cycle save it if it doesn't already exist or a subset of those node make up a cycle. If the node in the cycle is a subset of the nodes in another cycle remove the larger cycle (or maybe split it in two?)
Start over at 2 with a new neighbor.
Start over at 1 with a new node.
Comments: At 3 you should of course do the same thing as for step 2, so take all possible paths.
Maybe that's a start? As I said, I haven't tried it so it is not optimized.
EDIT: An undocumented and not optimized version of one implementation of the algorithm can be found here: https://gist.github.com/750015. But, it doesn't solve the solution completely since it can only recognize "true" subsets.

Related

Algorithm to find cycles in a directed graph with some constraints

I have a graph with the given constraints:
1. The graph is directed
2. At most 2 edges emerge from a vertex
3. At most 2 edges lead into a vertex
In this graph, I want to find cycles, so that:
a) each vertex is only a part of one cycle
b) every vertex is part of some cycle
I've spent some time on this problem already, but as easy as it sounds,
It doesn't seem so trivial.
An extension of the question is as follows:
Each edge additionally has a span, two real numbers,
and a 'pivot' must be selected,
so that all edges that end up in the cycles
can 'fit' pivot into their span range.
I.e. for pivot 100 one can use edge [20, 100], because 100 is in [20, 120]
Your problem is called finding a vertex-disjoint cycle cover. It can be found in polynomial time as described in this answer
Your extended question is not clear to me. Are you given the pivot? Then you'd simply remove the non-fitting edges. Do you have to chose one? With luck you may be able to modify the perfect matching search to satisfy this. But careful, such additional conditions easily make the difference between a polynomial and NP-hard problem.

Finding nearest element to a point STL format

A short introduction:
I have a body that is expressed by the outer-surface, given in STL form (set of triangular elements and their outside-pointing normal vector).
I'm trying to detect if a point given by coordinates is inside or outside the body.
The problem:
How do I find the nearest element to a given point?
More specifically, say you have found the nearest vertex to the point and this vertex is shared by two (or more) elements. Which is is the "nearest" one?
Note that the end result is determining if the point is inside or outside the body. A simple normal distance (dot product with the normal) does not solve the problem and can lead to ambiguous result based on which of the elements, sharing the node is selected.
Using the centroid of the element is also problematic.
Any suggestions, ideas (especially from people who have been involved in this issue before) are most welcomed!
EDIT:
I'll make the issue slightly harder. Say there's an open surface (but it covers the whole domain so that every point is on one of two sides of the surface, either in or out, based on the direction relative to the normal.
This also needs to be answered using the same approach.
EDIT2:
Answer was found!
Hope this helps!
The problem was answered with a variation of the ray-intersection method. 1. Find the nearest vertex, using the L2 norm (squared). 2. Consider the elements which share the vertex. It is recommended to have a connectivity list and not map them every time. 3. Cast a ray is cast from the interest point to the centroid of the first element. 4. Check among the elements in <2> which intersect the ray and select the one with the shortest intersection distance 5. Use the dot product of the intersection vector with the element normal (negative sign = outside, else inside)
(This was added to the question post itself)

Take certain edges in Dijkstra (or DFS)

i have a question about Dijkstra and/or DFS.
Let's assume I have a graph, with several nodes and edges. Now i want to find a Path from Node A to Node B. On this way i have to take certain Edges, for example the Edge (C,D).
Edit:
Sorry if it was a little bit unclear. My question is : I want a path from A to B. Is there a Path so that all edges {a,b}, {b,c}... and so on are taken? I'm interested if that's possible with dfs. And if thats also possible with Dijkstra under the same constraint if i want the shortest path from A to B and some edges {a,b}, {b,c} in the graph are needed to be taken. Also the graph is directed.
Help would be really appreciated!
Create a list of the all the edges that you want to make sure must be navigated.
Perform a DFS starting from the source.
Everytime you pass though an edge that's in the list, mark is as "traversed".
When you reach the destination, check if all the edges in the list are marked as traversed.
a.) If yes, goal state reached.
b.) Else, backtrack and unmark each edge in the list while passing through them.

Searching for an atypical graph pathfinding algorithm

Graph structure
graph is oriented
each edge has an assigned value representing the cost of using this edge. The value can be positive or negative
Problem description
input: graph, starting node and initial value (I don't have a goal node)
both, nodes and edges can be used repeatedly
goal: change the initial value to zero by passing through the graph. The answer should be if it is possible to reach zero (exactly zero, without reaching a negative actual value in the process)
I don't need the final path as the result, just the information if it is possible is enough. I would be most interested in a name of algorithm that is designed for this problem.
It is clearly NP-Hard (subset-sum can be reduced to it by using an appropriate complete graph). Breadth-first search seems like a natural approach, though to get a decision-procedure out of it you would need to find an upper-bound on the length of the path.

Finding shortest paths to all desired points in a graph

I was asked the following question in an interview
You are given a 4 X 4 grid. Some locations on the grid contain
treasure. Your task is the visit all the locations that contain the
treasure and collect it. You are allowed to move on the four adjacent
cells (up, down, left, right). Each movement and the action of
"treasure collection" is of a single unit cost. You need to traverse
the entire grid, and collect all the treasure on the grid, minimizing
the cost taken.
If I can recall properly, here is a sample graph that was given:
U..X
..X.
X..X
..X.
Where, U is my current position and X marks the position of the treasure.
The solution that I presented was to use breadth first search traversing the graph and "collecting the treasure" while doing so. However, the interviewer insisted that there was a better way to minimize the cost. I hope you could help me in figuring it out.
You should have recognized that this is a Traveling Salesman Problem in a small disguise. Using breadth-first, you can determine the shortest way between the different vertices you have to visit which gives you a derived graph containing just those ways as weighted edges between the vertices. From then on, it's a classic TSP.
BFS alone is not going to solve it for you, because you cannot move in all directions at the same time. It is not a single-source shortest path problem, because once you collect the treasure, you start your path to the next one from your current spot, not from the original spot.
The time that it takes to collect all treasure depends on the order in which you visit the boxes with X. Since there are only five of them, you can try all 120 orderings, compute the cost, and pick the best one.
Note that if the order is fixed, the solution is trivial: you add up manhattan distances between the cells in order, and pick the smallest result.

Resources