javascript - Dijkstra's algorithm on three dimensional array -
i cannot figure out how implement in psuedo-code. user enters in this:
[ [ [0,1] ], [ [5,6],[7,8] ], [ [91,17],[18,42] ], [ [20,54] ] ]
basically path [0,1] maps ([5,6] , [7,8]) each of maps ([91,17] , [18,42]) , on, cost distance between points. starting point [0, 1] , ending point [20,54]. there 1 starting point , 1 ending point, , points in previous index map points in next index.
how implement dijkstra's algorithm kind of data structure?
this image may (not scale):
green start , red end.
notice given array 2 dimensional if considered entry in array pair (x, y)
.
the basic idea build graph, assign cost of edges, , apply standard dijkstra's algorithm.
building graph:
make 2 hash tables h
, q
h([x,y])
maps vertex (x,y)
number between 0
, n - 1
, , q
maps integer between 0
, n - 1
vertex (x, y)
. n
number of vertices in graph. can find n
looping on vertices in given 2d
array. let's call given array a
pseudo-code of hashing:
n = 0 for(i = 0; < length of ; i++) for(int j = 0; j < length of a[i]; j++) h[a[i][j]] = n q[n] = a[i][j] n++
notice a[i][j]
pair of integers, key of h
should pair of integers.
now can build graph considering vertices numbers between 0
, n - 1
. can represent graph adjacency list
psudo-code of constructing graph:
array g[n][] //-- adjacency list of graph for(int = 0; < length of - 1; i++) //-- notice "-1" for(int j = 0; j < length of a[i]; j++) for(int k = 0; k < length of a[i + 1]; k++) g[ h[a[i][j]] ].insert (h[ a[i + 1][k] ]) g[ h[ a[i + 1][k] ].insert( h[a[i][j]] ) //-- assuming graph undirected
by doing this, have built graph. can apply standard dijkstra's algorithm on graph g
. find cost of edge between 2 vertices u
, v
can use hash table q
in order coordinates of u
, v
. cost of edge euclidean distance between points q[u]
, q[v]
.
pseudo-code cost of edge between 2 vertices u
, v
cost(int u, int v) return euclidean_distance(q[u], q[v])
Comments
Post a Comment