Skip to content

Commit 4af8cb0

Browse files
authored
Update Dijkstras-Algorithm.md
1 parent 1a0e398 commit 4af8cb0

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

Notes/Dijkstras-Algorithm.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ while ( !visited[ destination ] )
109109
}
110110
}
111111

112-
// If we were unable to find another vertex to visit...
112+
113+
// If we were unable to find another vertex to visit...
113114
if ( index == -1 )
114115
{
115116
// Break out of the loop
@@ -160,3 +161,75 @@ class Vertex implements Comparable<Vertex>
160161
}
161162
}
162163
```
164+
165+
Remember that when we used `dist[]` to keep track of distances, we had to look through _all_ tentative distances to see which vertex to visit; with this method, we just remove the next vertex from the priority queue and it will be guaranteed to have the smallest distance.
166+
167+
Now that we have our `Vertex` class setup, how do we use it to implement Dijkstra's algorithm?
168+
169+
Let's start by creating all of the vertices of a graph and storing them into an array.
170+
171+
```java
172+
Vertex[] graph = new Vertex[ N ];
173+
174+
// For each vertex in the graph...
175+
for ( int i = 0; i < N; i++ )
176+
{
177+
// Initialize the Vertex object corresponding to the vertex
178+
graph[ i ] = new Vertex( i );
179+
}
180+
181+
graph[ i ].neighbors.add( j ); // Add vertex j as a neighbor of vertex i
182+
```
183+
184+
We know the tentative distance of our source vertex is zero, so we can assign that value and add the vertex to the priority queue.
185+
186+
```java
187+
vertex[ source ].dist = 0;
188+
priorityQueue.add( vertex[ source ] );
189+
```
190+
191+
Just like BFS, we are repeating the steps of the algorithm until the queue is empty. This makes sense for Dijkstra's algorithm since we want to keep repeating the process until we either
192+
- run out of vertices (queue is empty), or
193+
- reach our destination vertex
194+
195+
Now that we have our stopping condition, we can implement the rest of the algorithm
196+
197+
```java
198+
// While the queue still has vertices left to visit...
199+
while ( !priorityQueue.isEmpty() )
200+
{
201+
// Get the current vertex
202+
Vertex current = priorityQueue.remove();
203+
204+
// If the current vertex is our destination...
205+
if ( current.id == destination )
206+
{
207+
// Break from the loop
208+
break;
209+
}
210+
211+
// Get the neighbors of the current vertex
212+
ArrayList<Integer> neighbors = current.neighbors;
213+
214+
// For each of the neighbors...
215+
for ( Integer j : neighbor )
216+
{
217+
// Get the Vertex representation of the neighbor
218+
Vertex neighbor = graph[ j ];
219+
220+
// Calculate the new distance to the neighbor
221+
int newDistance = current.dist + cost[ current.id ][ neighbor.id ];
222+
223+
// If the new distance we calculated is smaller than the tentative distance of the neighbor...
224+
if ( newDistance < neighbor.dist )
225+
{
226+
// Remove the neighbor from the priority queue so we can update the value
227+
priorityQueue.remove( neighbor );
228+
// Update the tentative distance of the neighbor
229+
neighbor.dist = newDistance;
230+
// Add the neighbor into the priority queue so that it can be sorted among the other vertices
231+
priorityQueue.add( neighbor );
232+
}
233+
}
234+
}
235+
```

0 commit comments

Comments
 (0)