|
-
Nov 8th, 2011, 01:11 AM
#1
Thread Starter
Frenzied Member
Could anyone explain a few things to me about Dijkstra's algorithm?
I need to calculate the shortest path to a destination in a 2D int array I have. I've tried implementing this into my own code but without understanding of certain things I'm unable to do so. I've looking around and found Dijkstra's algorithm which seems to be what I'm after.
This (gif) image on the wiki link above is what I'm trying to achieve:

Code:
1 function Dijkstra(Graph, source):
2 for each vertex v in Graph: // Initializations
3 dist[v] := infinity ; // Unknown distance function from source to v
4 previous[v] := undefined ; // Previous node in optimal path from source
5 end for ;
6 dist[source] := 0 ; // Distance from source to source
7 Q := the set of all nodes in Graph ;
// All nodes in the graph are unoptimized - thus are in Q
8 while Q is not empty: // The main loop
9 u := vertex in Q with smallest distance in dist[] ;
10 if dist[u] = infinity:
11 break ; // all remaining vertices are inaccessible from source
12 end if ;
13 remove u from Q ;
14 for each neighbor v of u: // where v has not yet been removed from Q.
15 alt := dist[u] + dist_between(u, v) ;
16 if alt < dist[v]: // Relax (u,v,a)
17 dist[v] := alt ;
18 previous[v] := u ;
19 decrease-key v in Q; // Reorder v in the Queue
20 end if ;
21 end for ;
22 end while ;
23 return dist[] ;
24 end Dijkstra.
I'm unclear on most of this and have consequently guessed. I've got a 2D int array with 0's denoting a walkable path and e.g 1's denoting a non-walkable path, and I want to calculate the fastest route to a specific coordinate (let's say 0,0) starting from (8, 14) just like the example (gif) image in the link above.
So, here's a few things I need clearing up:
- The graph, is it my 2D int array?
- What is a vertex? Is it coordinates like 2,1 and 5,7, etc.
- What are nodes? These are vertices, yes?
- When it says q should be the set of all nodes in graph, does that mean I add all the possibly vertices in my graph to q which is a one dimensional array?
- How is it calculating the distance between u and v if it's a vertice
- Is this written for a 1D graph? Is that why it only has one dimension, or is it a placeholder?
- Assuming a vertex is a coordinate (which is what I think it is), I'm unsure how to use it in an array like dist[v]. My first thought is dist[x][y] but I tried that and decided to give up and try another possible solution. (The above question may answer this somewhat.)
Would appreciate any help here! I've not really dealt with algorithms and I need to know this information is accurate or not so much. Cheers!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|