I need to help to validate whether my understanding of when to use Dijkstra and when BFS are correct.
My way of understanding BFS and Dijkstra, when to use what, am I correct?
As I see it. Being given a graph, that can be imagined as N-ary tree, where edges have no special value (all are same length) and we just want to get from node A to node B.
- To solve this, a BFS can be used
- Dijkstra can be used as well, but it is wasting memory, space and OP
However, being given a graph, where edges do have different values:
Lets imagine three nodes: A, B, C. Length from A to B is 1 Length from A to B is 5 Length from A to C is 100 Length from B to C is 1
So, yes, you can have more paths from node A to B. We are interested in shortest path from A to C.
There is NO way to solve this using plain BFS, am I correct...? I have to use Dijkstra...
From the implementation side, can Dijskstra be understood like BFS, but with a difference that we are having a container WHITE from wich we take always node with shortest tmp path... (priority queue) ? Whereas in plain BFS we can for WHITE use a queue or deque..? So Dijsktra's only difference is the priority queue...(and maybe some small things in algo, I wrote both BFS and Dijkstra, can not recall from head)
About complexities, on wiki I just am not sure with it. For BFS it should be O(a) + O(b)
- a = Number of nodes
- b = number of edges
But is there also the backtrack...? On wiki is just what is shown up...so it is oncomplete...? backtracking is also part of algorithm...vital one..I can backtrack at n * log(n), n being number of elements that I saved during traversal...
For Dijkstra....O(a * a) + O(b * b)...? Because I can visit some twice...? plus O(a) for setting each node's distances, plus O(n * long(n) ) for backtrack...?