A shortest path problem can be reduced to a matching problem, for example like this:
https://tobi.rocks/2013/09/example-reduction-spp-assignment/
But I was wondering if one could also reduce a matching problem to a shortest path problem.
With matching problem I mean the object is to create a perfect matching between 2 equally sized groups, where every matching between two individuals of these groups has a certain weight and we want to minimize this weight.
Related
I have a question about the BSF algorithm implementation in Memgraph. Are shortest path calculations optimized to work with dynamic and streaming graphs? In my data set, the number of nodes and edges will grow. I need to maintain a table of shortest path hops from a set of nodes. If I use Memgraph, will BFS be re-run each time or does Memgraph only traverse nodes relevant for shortest path updates?
Memgraph has a bunch of stuff on the streaming side, but the current implementation of BFS is not incremental/dynamic. But, you can easily compose the solution by using BFS (https://memgraph.com/docs/memgraph/reference-guide/built-in-graph-algorithms#breadth-first-search), triggers (https://memgraph.com/docs/memgraph/how-to-guides/set-up-triggers), and query modules (https://memgraph.com/docs/memgraph/how-to-guides/query-modules)
It's not BSF, but there is something very similar in the streaming example
(https://github.com/memgraph/example-streaming-app/blob/main/memgraph/queries/create_update_neighbors_trigger.cypher)
I have a list of cities and an energy expenditure between each of them. I want to find the "best" (shortest) path between some specific pairs, that leads to the least energy loss. All roads are two-way roads, same energy expenditure from one city to another in a pair, but in the "best" path, each city should be visited only once to prevent looping around the same city.
I've tried making a directed adjacency list graph and using Bellman Ford but i am indeed detecting negative cycles, making the shortest path non-existent, but can't figure out how to make the algorithm go through each node only once to prevent looping. I've thought about using something like BFS to just print all the possible paths from a source node to a destination and somehow skip going through the same node (summing up the weights afterwards perhaps). Any ideas of how I would possibly solve this, either modding the Bellman Ford or the BFS or using something else?
The problem of finding the lowest-cost simple path (a path that doesn’t repeat nodes) in a graph containing negative cycles is NP-hard, which means that at present we don’t have any efficient algorithms for the problem and it’s possible that none exists. (You can prove NP-hardness by a reduction from the Hamiltonian path problem: if you assign each edge in a graph cost -1, then there’s a Hamiltonian path from a node u to a node v if and only if the lowest-cost simple path between them has length n-1, where n is the number of nodes in the graph.)
There are some techniques to find short paths using at most a fixed number of hops. Check out the “Color Coding” algorithm for an example of one of these.
add the absolute value of the most negative weight to EVERY weight.
Apply the Dijkstra algorithm. https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
Subtract absolute value of the most negative weight times number of hops from cost of optimal path found in 2
Disclaimer: this problem came from my past AI final exam. And I found it very interesting but I couldn't figure it out.
There is the description:
Given a maze you are free to move between adjacent white cells, but black cells are blocked. You can try to move UP, DOWN, LEFT, RIGHT. If you are not blocked in that direction, you successfully move. If you are blocked, you stay in the same place.
a) Find the shortest possible sequence of moves that guarantee you will end up at G regardless of where you started.
Before making any moves, you may be in any position. After each move, your set of possible positions will shift, and may shrink when moving from some possible positions would hit a wall. The set of possible positions will never increase in size, though.
You may assume a directed graph, with a vertex for each set of positions that may arise, and an edge connecting each set to the 4 sets that would follow from moving left, right, up, or down.
Your task, then, is to find the shortest path from the vertex for the set of all positions to the vertex for the singleton set containing only the target position.
My first attempt would by to run A* on this graph using an appropriate metric. The number of possible sets is extremely large, though, so this is not guaranteed to be feasible. I would use my human intelligence to try to pick a metric that works quickly.
If you can choose a metric that gets A* to completion in a reasonable amount of time, and conforms to the rules that A* puts on metrics, then that will prove that the path you found is the shortest one.
Off the top of my head, I would first try the length of the shortest path to the target from the furthest position in the set. Maybe in combination with the total number of non-empty rows and columns.
So this is what I would do, but I am not an AI expert. I am guessing that there is probably something you learned in class that can improve upon this procedure.
I'm trying to find the shortest path between two Vertices (fromNode and toNode) in Tinkerpop 3, with the limitation that I need to stop searching after at some depth N. Without this limitation I'll never finish as the graph is too large.
So far this the best I can come up with:
titanGraph.traversal().V(fromNode)
.repeat(out().simplePath())
.times(N)
.emit(hasId(toNode)).path();
The problem that this has, and all other ways I've tried, is that the paths of length N are returned as results, even when they don't reach toNode.
I need to return all paths between fromNode and toNode of length N or under.
You were on the right track
titanGraph.traversal().V(fromNode)
.repeat(out().simplePath())
.times(N)
.emit(hasId(toNode)).hasId(toNode).path();
I have read about Levenshtein distance about the calculation of the distance between the two distinct words.
I have one source string and i have to match it with all 10,000 target words. The closest word should be returned.
The problem is I have given a list of 10,000 target words, and input source words is also huge.... So what shortest and efficient algorithm to apply here. Levenshtein distance calculation for each n every combination(brute force logic) would be very time consuming.
Any hints, or ideas are most welcome.
I guess it depends a little on how the words are structured. For example this guy improved the implementation based on the fact that he processes his words in order and does not repeat calculations for common prefixes. But if all your 10,000 words are totally different that won't do you much good. It's written in python so might be a bit of work involved to port to C.
There are also some kinda homebrew algorithms out there (with which I mean there is no official paper written about it) but that might do the trick.
There's two common approaches for this, and I've blogged about both. The simpler one to implement is BK-Trees - a tree datastructure that speeds lookup based on levenshtein distance by only searching relevant parts of the tree. They'll probably be perfectly sufficient for your use-case.
A more complicated but more efficient approach is Levenshtein Automata. This works by constructing an NFA that recognizes all words within levenshtein distance x of your target string, then iterating through it and the dictionary in lockstep, effectively performing a merge join on them.