No Dijkstra and no BFS? What else is there?

157 Views Asked by At

So, this is the scenario I have a graph with 7 points (say A to G) all interconnected (full mesh), and I want the best path to traverse all points starting from A and ending on G, but there are a couple of... caveats:

1.- The weight from A to B is not necessarily the same as the weight from B to A, and

2.- Some weights can be negative

Is there an algorithm that could help solve this problem?

2

There are 2 best solutions below

2
On

Unfortunately, you are dealing with an $\mathsf{NP}$-hard problem, which is called the Traveling Salesman Problem. This means there is no efficient way to solve your problem.

Thankfully, the graph is relatively small, so brute force can be done in realistic time.

A reduction which you need to do is make all the edges' weights nonnegative. This can be done by adding a constant to all edges.

0
On

If you introduce a dummy node adjacent to only A and G, you have an asymmetric traveling salesman problem on a directed graph with 8 nodes, which can be converted to a symmetric traveling salesman problem on an undirected graph with 16 nodes.

For directed or undirected TSP, you can have negative weights.

If you post the data, I'll find an optimal solution.