2

Usually, Dijkstra's algorithm finds out the shortest distance to get from one node to another.

But can it work out the shortest distance to get from one node to another by going through every other single node. I need it to go to every other node before reaching the end node.

How would I apply this using Dijkstra's algorithm for an undirected graph, or is it not possible?

Newbie101
  • 501
  • 2
  • 6
  • 17
  • check this out (https://stackoverflow.com/questions/789159/how-do-i-find-the-shortest-path-that-covers-all-nodes-in-a-directed-cyclic-graph) – Alex Ciu May 02 '19 at 18:55
  • This is definitely the Traveling Salesman Problem, discussed in the linked question. – Rob Napier May 02 '19 at 19:35

1 Answers1

1

The problem described is a variation of the Travelling salesman problem (TSP). This problem is NP-hard. A brute force solution has time complexity of O(n!). Using dynamic programming, you can achieve a time complexity of O(n2 2n). There are various heuristics to achieve a near optimal solution in polynomial time.

Dijsktra's algorithm will pick the shortest path to each new node found which may not be the optimal solution for the travelling salesman problem. As counter example, consider a graph connecting nodes with very short paths to each node except the end node. Imagine the shortest path from the 2nd last node to the end node to be exceptionally large such that it is optimal to take an intermediate non-shortest path between intermediate nodes so as to reach the end node.

Mukul Gupta
  • 2,310
  • 3
  • 24
  • 39