I was recently given the challenge of drawing a house with an x in the middle without lifting my pen, and without retracing any lines. Link to problem
The link above begins to dive into some of the graph theory related to the problem, however there is no mention of how one might go about solving this problem using graph theory algorithms.
What algorithms could be used here, and what would be the correct way to formulate this problem using graph theory language?
Two specific algorithms for constructing an Eulerian path are mentioned in the Wikipedia article on Eulerian paths. These are Fleury's algorithm and Hierholzer's algorithm.
Note that an algorithm that only finds an Euler cycle can also find an Euler trail by augmenting the graph with another edge that joins the 2 vertices that have an odd degree, then rotating the solution so that the added edge is first or last, then removing this edge from the solution found.
Related
So I have the following layout:
graph representation
The objective is to collect all the yellow blocks by moving the white ball around. I'm trying to come up with an algorithm that will calculate an efficient path however I'm not too sure where to start.
Initially I thought about path finding algorithms like Djikstra and A* but they don't seem to fit with my goal. I've also thought about hamiltonian paths which is closer to what I want but still doesn't seem to solve the problem.
Any suggestions on what sort of algorithm can be used would be appreciated.
Your problem has a classic name in the litterature, it is the minimum hamiltonian walk problem. Beware not to mistake it with the minimum hamiltonian path problem, its 'cousin', because it is much more famous, and much, much harder (finding a hamiltonian walk can be done in polynomial time, finding a hamiltonian path is NP-complete). The traveling salesman problem is the other name of the minimum hamiltonian path problem (path, not walk).
There are very few ressources on this problem, but nevertheless you can have a look at an article called 'An algorithm for finding a short closed spanning walk in a graph' by Takamizawa, Nishizeki and Saito from 1980. They provide a polynomial algorithm to find such a path.
If the paper is a bit hard to read, or the algorithm too complex to implement, then I'll suggest that you go for the christofides algorithm, because it runs in polynomial time, and is somehow efficient (it is a 2-approximation if I remember well).
Another possible approach would be to go for a greedy algorithm, like a nearest unvisited neigbor algorithm (start somewhere, go to the nearest node that is not in the walk yet, repeat until everyone is in the walk).
Acutally, I think the easiest and maybe best simple solution is to go greedy.
Assume that I have set of points scattered on the XY plane, and i have two points say start and end point any where in XY plane. I want to find the shortest path between start and end point without touching scattered points. The path has to maintain certain offset ( i.e assume path has some width ).
How to approach this kind of problems in programming, Are there any algorithms in machine learning.
So you need a greedy algorithm for the shortest path?
Try Dijsktra's Algorithm.
http://www.geeksforgeeks.org/greedy-algorithms-set-6-dijkstras-shortest-path-algorithm/
The shortest solution for the lowest price.
You can also consider the A* algorithm.
This finds the same solution as Dijkstra's algorithm, but often at a lower computational cost (which might be important in your case, since after the space discretization you might end up with a large grid).
This is because A* uses a heuristic to bias the search, so that it looks into more promising directions first (e.g. moving towards the target is in principle a good idea, so this is attempted first).
You can see some visualizations of A* running here and (side by side with Dijkstra's algorithm - thanks #Thrawn for the link), here.
This is not a machine learning problem but an optimization problem.
So you need a greedy algorithm for the shortest path
Indeed it could be solved this way but the challenge is to represent your grid as a graph...
For example, decomposing the grid in a n x n matrix. In your shortest path algorithm, a node is an element of your matrix (so you exclude the elements of the matrice that contains the scattered points) and the weight of the arcs are the distance.
However n must be small since shortest path algotithms are np-hard problems...
Maybe other algorithms exist for this specific problem but I'm not aware of.
Like others already stated: this is not a typical "Artificial Intelligence" problem. It is kind of a path planning problem.
There are different algorithms available. If your path doesn't neet to satisfy any constraints like .g. smoothness, you can use an A*-Algorithm with distance as heuristic.
You have to represent your XYZ-space as a Graph where each node has a coordinate. Further you need to take into account, that no nodes lie near the points you want to avoid.
If your path needs to satisfy constraints, this turns into a more complicated path planning problem where you could apply optimization or RRTs.
I have a large graph of connected vertices (a connected component) and I am looking for a path that goes through all of them, but never goes through one vertex twice. This isn't always possible. For instance, in the following example from wikipedia, it is obvious that there is no path that visits every vertex where no vertex is visited more than once:
But if it was tweaked slightly, so that it has more edges (connections), then there are some paths that can go through every vertex exactly once. I've tweaked it and numbered the vertices to give one such path:
My graph is like this one, where I know that there is a possible path. However, it is quite large (20,000 vertices, each with anywhere between 2 and 11 edges). I implemented depth-first search and breadth-first search but the graph is simply too big to find a path through (it will take too long to compute).
So my question is: Is there another algorithm that can solve this problem, specifically more efficiently than depth-first or breadth-first search?
It is a little bit like the traveling salesman problem except that cities are only reachable from specific other cities, and distance between those is equal.
The problem you're describing is called the Hamiltonian path problem (a Hamiltonian path is one that goes through every node once and exactly once). This problem is, unfortunately, known to be NP-hard, so there are no known polynomial-time algorithms for solving this problem. As a result, you are unlikely to find a solution that works efficiently using a simple breadth-first search or depth-first search.
There is a somewhat famous dynamic programming algorithm for solving this problem. If you search online for "Hamiltonian path DP," you should find some good links on the subject.
I want to convert a cyclic graph into an acyclic graph. Is there an pseudocode that can do this? I did try to search but most of them returned where mathematical based on markov chain or research articles.
I want to write a program to do it and any directions will be useful. for example consider this below graph.
A->B
B->C
C->A
I saw a solution in an MIT lecture but the lecture skimmed over it with reference to something taught earlier and couldn't catch it. In short it kind of replicates the nodes in layers in a way that the end graph denotes a DAG but conveys the same path information.
[See 46:59]
https://www.youtube.com/watch?v=OQ5jsbhAv_M
Edit:
There is an explanation of turning cyclic graphs into dags in this MIT recitation at 36:57
https://youtu.be/IFrvgSvZA0I
Se also Wikipedia : cycle and forest
Edit:
I want to apply dynamic programming over a problem which is a cyclic graph e.g) say shortest path problem Delta(S,D) where S-> Source node and D->Destination node. Since DP over cyclic graph is infinite algorithm, we first need to convert the cyclic graph into acyclic graph and then apply the dynamic programming technique over it.
I believe you pointed out the wrong video, here it is (46:59): https://www.youtube.com/watch?v=OQ5jsbhAv_M
The idea exposed here, is to make several copies of the graph and to arrange them into layers. Each one represents the state of the graph at a given time, and for every edge traversed you go down by one layer.
I did not find pseudo-code for this, but the way of doing this is detailed here: https://cstheory.stackexchange.com/questions/14591/combinatorics-of-bellman-ford-or-how-to-make-cyclic-graphs-acyclic
I have n points and I need to connect all of them minimizing the final distance. The image above represents an algorithm that in each node it connects to the nearest one but the final output might be really of.
I've been searching a lot, I know some pathfinding algos but unaware of one that solves exactly this case. I found a question on Math Stackexchange but the answer is not providing any algorithm - https://math.stackexchange.com/a/581844/156584.
Is there any algorithm that solves exactly this problem? Otherwise I can bruteforce it.
Edit: Some clarification regarding the result I'm expecting: each node can be connected to 2 other nodes, creating a continuous path (like taking a pen and without ever lifting it, connect the nodes minimizing the final distance). I don't want to create a cycle (that being the travelling salesman problem).
PS: this question can also be translated to "complete graph with n vertices, and wanting to choose the set of edges such that the graph is connected, but the sum of the edge weights is minimized"
This problem is known as the shortest Hamiltonian path problem and it is NP-hard. So if the number of points is small, you can use backtracking or dynamic programming to find an optimal solution. If the number of points is large, you can use heuristics and/or approximations to obtain a relatively good answer(it is not always possible to find the best one in this case, though).