Search code examples
algorithmgraphdijkstrapath-finding

Using Dijsktra's algorithm with unique distance between nodes


For a school project, my friends and I are learning what is pathfinding and how to exploit it via a simple exercise:

For a group of ants going from A to B, they need to travel through multiple nodes, one at a time.

I've read some explanation about Dijsktra's algoritm, and I wanted to know if I could use it in a graph where every distance between every node is 1 unit. Is it optimal ? Or is A* more appropriated to my case ?

EDIT:

Since I had knowledge on the graph, BFS was prefered because distance between node was pre-computed, where Djisktra is prefered when you have absolutely nothing about the graph itself. See this post for reference


Solution

  • If every edge has the same cost, then Dijkstra's algorithm is the same as a breadth-first search. In this case you might as well just implement BFS. It's easier.

    If you have a way to estimate how far a point is from the target, then you could use A* instead. This will find the best path faster in most cases.