Applying Dijkstra Algorithm: Solving a Problem

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Algorithm
In summary, the Dijkstra algorithm is used to find the shortest path between two nodes in a graph. The algorithm can be applied to a directed or undirected graph and it finds the minimum cost path.
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Wave)

I want to apply the Dijkstra algorithm at an example.

Code:
Dijkstra(G,s,v)
1. InitialValues(G,s)
2.S<-∅
3.Q<-V // priority queue,with key the field d[]
4. while Q  ≠ ∅
5.         u<-Extraction_of_Minimum(Q)
6.          S<-S U {u}
7.          for each v ∈ Adj[u]
8.               Relaxation(u,v,w)

Code:
InitialValues(G,s)
 for each v ∈ V
      d[v]<-oo
      p[v]<-∅
 d[s]<-0

Code:
Relaxation(u,v,w)
 if d[v]>d[u]+w(u,v)
    d[v]<-d[u]+w(u,v)
    p[v]<-u

This is the given graph:

View attachment 3127

Executing the algorithm,I found the following:

$$p[a]=s$$
$$p[c]=s$$
$$p=c$$
$$p[d]=a$$

So,the tree,that we get from the Dijkstra's algorithm would be:

View attachment 3128

So,the weight would be $23$.

But...I found the following tree,with less weight:

View attachment 3129

Have I done something wrong,applying the algorithm? (Thinking)
 

Attachments

  • dijkstra.png
    dijkstra.png
    6.2 KB · Views: 76
  • dijkstra.png
    dijkstra.png
    3.2 KB · Views: 76
  • dijkstra.png
    dijkstra.png
    3.6 KB · Views: 80
Last edited:
Physics news on Phys.org
  • #2
According to definition, the minimum spanning tree is applied on a connected undirected graph.

So first convert the directed graph to undirected then apply the alogrithm to find the minimum spanning tree.

I'm not sure if there is a version for a directed graph because if you look at the last graph if you move from \(\displaystyle s\) then you are stuck.
 
  • #3
ZaidAlyafey said:
According to definition, the minimum spanning tree is applied on a connected undirected graph.

So first convert the directed graph to undirected then apply the alogrithm to find the minimum spanning tree.

I'm not sure if there is a version for a directed graph because if you look at the last graph if you move from \(\displaystyle s\) then you are stuck.

I am sorry..I don't want to find the minimum spanning tree.. (Blush)

I wanted to apply the Dijkstra's algorithm at the above example..

So,I have to find a path,that contains all the vertices , such that the sum of the weights of its constituent edges is minimized..right?

So..have I done something wrong? (Thinking)
 
  • #4
Dijkstra's algorithm is used to determine the lowest cost path from a given node (the source) $s$ to every other node in the graph. (The graph may be directed or undirected, since an undirected path can be viewed as two opposing directed path with the same weight.)

In the last graph provided, your directed path tree from $s$ can only reach $c$. The cost going from $s$ to $a$ in that path tree in fact is $\infty$ as it is not reachable, but that's not the case.
 
  • #5
magneto said:
Dijkstra's algorithm is used to determine the lowest cost path from a given node (the source) $s$ to every other node in the graph. (The graph may be directed or undirected, since an undirected path can be viewed as two opposing directed path with the same weight.)

In the last graph provided, your directed path tree from $s$ can only reach $c$. The cost going from $s$ to $a$ in that path tree in fact is $\infty$ as it is not reachable, but that's not the case.

A ok..so is the first graph right? (Thinking)
 
  • #6
evinda said:
A ok..so is the first graph right? (Thinking)

Yes. It gives the shortest path of $\enclose{circle}{s}$ to each of the other nodes. (Wasntme)
 
  • #7
I like Serena said:
Yes. It gives the shortest path of $\enclose{circle}{s}$ to each of the other nodes. (Wasntme)

Nice...thank you very much! (Mmm)
 

FAQ: Applying Dijkstra Algorithm: Solving a Problem

What is the Dijkstra algorithm and how does it work?

The Dijkstra algorithm is a widely used graph search algorithm that finds the shortest path between two nodes in a graph. It works by assigning a tentative distance to each node and then updating the distance as it finds a shorter path. The algorithm continues until the shortest path to the destination node is found.

What are some real-world applications of the Dijkstra algorithm?

The Dijkstra algorithm has many real-world applications, including GPS navigation systems, network routing protocols, airline route planning, and robot navigation. It can also be used in academic fields such as molecular biology and chemistry for finding the shortest paths between molecules.

How does Dijkstra's algorithm handle negative edge weights?

Dijkstra's algorithm cannot handle negative edge weights as it assumes that all edge weights are positive. If there are negative edge weights, the algorithm may produce incorrect results or get stuck in an infinite loop. To handle negative edge weights, a modified version of the algorithm called the Bellman-Ford algorithm can be used.

Can Dijkstra's algorithm be used for finding the shortest path between multiple nodes?

Yes, Dijkstra's algorithm can be modified to find the shortest path between multiple nodes by using a priority queue to store the distances of all the nodes and their corresponding predecessors. The algorithm can be run multiple times, starting from each node, to find the shortest path to all the other nodes.

What are some advantages and disadvantages of using Dijkstra's algorithm?

One advantage of Dijkstra's algorithm is that it always finds the shortest path between two nodes. It is also efficient for finding the shortest path in sparse graphs. However, the algorithm does not work for graphs with negative edge weights and it can be computationally expensive for large graphs. Additionally, it may not work well in cases where there are multiple shortest paths between two nodes.

Similar threads

Replies
2
Views
2K
Replies
8
Views
2K
Replies
11
Views
3K
Replies
13
Views
3K
Replies
2
Views
2K
Replies
2
Views
1K
Back
Top