Shortest path with constraints on traversing edges - algorithm

While working on a project I've stumbled upon a graph algorithms problem I
haven't been able to solve. The problem is as follows:
You have a directed, weighted graph and want to find the shortest path between a
start node and an end node while visiting specified nodes (very much like
Find the shortest path in a graph which visits certain nodes).
However, along with nodes and edges, this graph also has the notion of "items",
which reside at nodes and you "pick up" when you enter that node. Now there is an extra constraint that edges can only be
traversed if you have obtained the necessary items, I, for that particular edge.
Think of it as a key for a door; you need to obtain a key before being able to
pass through the door.
I can only think of brute-force methods that blow up exponentially. Can anyone think of anything better or point me to a place where this problem is solved? Or maybe convince me that this is "hard" (computationally speaking)? Thanks for any help!

This problem is NP-HARD to solve optimally. There's a simple reduction from the Hamiltonian path problem:
Put unique items on each vertex of the original graph. Construct an sink vertex connected only to the destination vertex. Let the edge between these two vertices require all of the items.

You can try a modified version of the saving algorithm. It's a heuristic to solve the vehicle routing problem. Maybe you can reverse it and create a pick up function for the wanted keys. It's use for a delivery and shortest path problem.

Related

Algorithm for finding an entity in a maze without prior knowledge

I have a grid like weighted maze and I need to find the shortest path to an entity without having any prior knowledge of the maze.
Algorithms like A* expect priot knowledge and 'jump' around when looking around, but that's not possible when I have a robot for example.
My first thought would be to initially explore the whole maze using BFS and then apply A* on the explored to find the shortest considering the weights as well. But that seems naive.
Can anyone point me to some algorithms that could be a good fit for this problem?
I think the most applicable algorithm for this sort of problem would be Dijkstra's algorithm.
In short, the algorithm starts at some root node, scans all neighbors and choose the node with the shortest path from the root to visit.
A table is kept that contains every node's
Shortest path from the root
The last node in the path to get to this node
The shortest path to a node is updated in the table whenever a shorter path is discovered.
When your entity is visited, its shortest path will be the shortest path from root->entity, and backtracking through its parents will yield the actual node path.
(Here's another video if you're confused).

How to find a shortest path in a directed graph which must pass through specific nodes?

I have a directed graph with less than 600 nodes, and each node's edge number is less than 8.
Now I need to find a path in this graph which must pass through some given nodes(<50). The order of passing given nodes is free.
I know it's a NPC problem, but I don't know how to solve it.
An approximate solution is also acceptable.
Thank you!
Compute the shortest ways between all pairs of the specific nodes. Then create a new graph that only contains those nodes, with the length of the shortest paths as distances. Now, the problem is "reduced" to Travelling Salesman.
(TSM has a fast 3/2-approximation that utilitizes minimal spanning trees and a matching, if that is good enough - the 50! possibilities are too much in any case)

Route between A and B with stations between

i´m obviously missing the forest through the trees ...
i know about the traveling salesman problem, but is there any other algorithm/problem which better fits my needs/description? I need to describe my problem with the help of such a mathematical description.
I have up to five points with known start- and endpoint. So i just need to calculate the shortest way to visit all the three points between that two. Dijkstra and similar algorithms try to find the shortest path between two points, so here they probably won´t visit all points between. Or is there a algorithm which finds shortest way and visit all points between two points?
You are overthinking it. There are only six (3*2*1) possible paths through the three intermediate nodes. Just check them all.
For larger instances, you could reduce your problem to the TSP as follows:
If s is the starting node and t is the final node, add a zero-weight edge between s and t and an infinitely-heavy edge between s and every other node, and between t and every other node.
The problem is NP-hard, but is extremely well-researched. There is a plethora of exact and approximate algorithms that you could explore.

How can I find the shortest path in a graph, with adding the least number of new nodes?

I need to find the shortest path in a graph with the least number of added nodes. The start and end nodes are not important. If there is no path in a graph just between specified n-nodes, I can add some nodes to complete the shortest tree but I want to add as few new nodes as possible.
What algorithm can I use to solve this problem?
Start with the start node.
if it is the target node, you are done.
Check every connected node, if it is the target node. If true you are done
Check if any of the connected nodes is connected to the target node. If true you are done.
Else add a node that is connected to start and end node. done.
I recommend you to use genetic algorithm. More information here and here.
Quickly explaining it, GA is an algorithm to find exact or approximate solutions to optimization and search problems.
You create initial population of possible solutions. You evaluate them with fitness function in order to find out, which of them are most suitable. After that, you use evolutionary algorithms that use techniques inspired by evolutionary biology such as inheritance, mutation, selection, and crossover.
After several generations, you'll find the most suitable (read shortest) solution to the problem.
You want to minimize the number of nodes in the path (instead of the sum-of-weight as in general algorithms).
If that is the case, assign equal weight to all the edges and find the shortest path (using the generic algorithms). You will have what you needed.
And if there is no path, just add that edge to the graph.
Sands.
PS: If you give a value of 1 for each edge, the number of nodes in the path would be the weight-1 (excluding the source and destination nodes)

Find the shortest Path between two nodes (vertices)

I have a list of interconnected edges (E), how can I find the shortest path connecting from one vertex to another?
I am thinking about using lowest common ancestors, but the edges don't have a clearly defined root, so I don't think the solution works.
Shortest path is defined by the minimum number of vertexes traversed.
Note: There could be a multi-path connecting two vertices, so obviously breadth first search won't work
Dijkstra's algorithm will do this for you.
I'm not sure if you need a path between every pair of nodes or between two particular nodes. Since someone has already given an answer addressing the former, I will address the latter.
If you don't have any prior knowledge about the graph (if you do, you can use a heuristic-based search such as A*) then you should use a breadth-first search.
The Floyd-Warshall algorithm would be a possible solution to your problem, but there are also other solutions to solve the all-pairs shortest path problem.
Shortest path is defined by the minimum number of vertexes treversed
it is same as minimum number of edges plus one.
you can use standard breadth first search and it will work fine. If you have more than one path connecting two vertices just save one of them it will not affect anything, because weight of every edge is 1.
Additional 2 cents. Take a look at networkx. There are interesting algos already implemented for what you need, and you can choose the best suited.

Resources