java - Finding the shortest path in a maze graph using adjacency matrix -
i have troubles solving of particular problem find shortest path in maze graph. probably, i'm stucked because of fact maze initialized 4 dimensional array adjacent = new boolean[height][width][height][width];
first , second pair of indices specify location in graph in row/column formation. graph looks this:
xxxxxxxxxxxxx ..........x.x x.xxx.xxx.x.x x.x.x...x.x.x x.x.xxx.xxx.x x...x.....x.. xxxxxxxxxxxxx
the arraylist must hold locations of vertices in path, in order start end inclusive.
i've written constructor , connection method; however, have trouble finding shortest path method. here example of how create maze graph:
final int edges[][] = {{1, 0, 1, 1}, {1, 1, 1, 2}, {1, 1, 2, 1}, {1, 2, 1, 3}, {1, 3, 1, 4}, {1, 4, 1, 5}, {1, 5, 1, 6}, {1, 5, 2, 5}, {1, 6, 1, 7}, {1, 7, 1, 8}, {1, 8, 1, 9}, {1, 9, 2, 9}, {1, 11, 2, 11}, {2, 1, 3, 1}, {2, 5, 3, 5}, {2, 9, 3, 9}, {2, 11, 3, 11}, {3, 1, 4, 1}, {3, 3, 4, 3}, {3, 5, 3, 6}, {3, 6, 3, 7}, {3, 7, 4, 7}, {3, 11, 4, 11}, {4, 1, 5, 1}, {4, 3, 5, 3}, {4, 7, 5, 7}, {4, 11, 5, 11}, {5, 1, 5, 2}, {5, 2, 5, 3}, {5, 5, 5, 6}, {5, 6, 5, 7}, {5, 7, 5, 8}, {5, 8, 5, 9}, {5, 11, 5, 12}}; mazegraph maze = new mazegraph(13, 7); (int[] edge : edges) maze.connect(new location(edge[0], edge[1]), new location(edge[2], edge[3]));
first of all, this
adjacent = new boolean[height][width][height][width];
contradicts this:
the first , second pair of indices specify location in graph in row/column formation.
it column/row, not row/column.
dijkstra's algorithm should implemented matrix. quote:
let node @ starting called initial node. let distance of node y distance initial node y. dijkstra's algorithm assign initial distance values , try improve them step step.
assign every node tentative distance value: set 0 our initial node , infinity other nodes.
set initial node current. mark other nodes unvisited. create set of unvisited nodes called unvisited set.
for current node, consider of unvisited neighbors , calculate tentative distances. compare newly calculated tentative distance current assigned value , assign smaller one. example, if current node marked distance of 6, , edge connecting neighbor b has length 2, distance b (through a) 6 + 2 = 8. if b marked distance greater 8 change 8. otherwise, keep current value.
when done considering of neighbors of current node, mark current node visited , remove unvisited set. visited node never checked again.
if destination node has been marked visited (when planning route between 2 specific nodes) or if smallest tentative distance among nodes in unvisited set infinity (when planning complete traversal; occurs when there no connection between initial node , remaining unvisited nodes), stop. algorithm has finished.
otherwise, select unvisited node marked smallest tentative distance, set new "current node", , go step 3.
Comments
Post a Comment