Can it be used for directed graphs

let the edges be 3
from 1 to 2(wt=4)
from 1 to 3(wt=1)
from 3 to 2(wt=1)
it fails to give the correct ans
but according to the dry run it should be correct
if we change it to bidirected then it works properly plzz give a detailed explanation as to why this happens

Hey Pooja,
In the context of Dijkstra’s algorithm , whether the graph is directed or undirected does not matter. Dijkstra’s algorithm simply references the adjacent vertices of a vertex. It is this adjacency list that you would have to modify if you were changing a graph from directed to undirected .
Can you tell between which pair it fails?

from 1 to 2(wt=4)
from 1 to 3(wt=1)
from 3 to 2(wt=1)

answer for 1 to 2 will be 2.
if its giving 4 then there must be error in your implementation!, make sure you don’t need any visited array as node already visited are visited again if cost to reach them becomes lower than what was before.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.