• FEATURED
• FEATURED
• FEATURED
• FEATURED
• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Adjacency matrix and dijkstra pathfining? (c++)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1suliman  Members

Posted 20 March 2017 - 01:44 AM

Hi

Im trying to understand dijkstra pathfinding. But i need to use it on a grid which im only used to doing on a star. Most implementations look something like this:

const int inf = 1 << 30;

// given adjacency matrix adj, finds shortest path from A to B
int dijk(int A, int B, vector< vector<int> > adj) {
vector<int> dist(n);
vector<bool> vis(n);

for(int i = 0; i < n; ++i) {
dist[i] = inf;
}
dist[A] = 0;

for(int i = 0; i < n; ++i) {
int cur = -1;
for(int j = 0; j < n; ++j) {
if (vis[j]) continue;
if (cur == -1 || dist[j] < dist[cur]) {
cur = j;
}
}

vis[cur] = true;
for(int j = 0; j < n; ++j) {
int path = dist[cur] + adj[cur][j];
if (path < dist[j]) {
dist[j] = path;
}
}
}

return dist[B];
}


What is A and B? Indexes to the start and goal node? It's a graph and not a grid I assume. Can I build a adjacency matrix from a grid and use the above implementation or would there be a completely different implementation for using dijkstra on a grid?
Lets assume my map looks like:

1 0 1
1 0 1
1 1 1

And "0" means impassable node. And I need to pathfind from topleft to topright. How would I setup "vector<vector<int>> adj" so it described this map? A and B? Or is it a stupid way of doing it? I need a minimal pathfind but they seem to always look like the one above.

E

Edited by suliman, 20 March 2017 - 01:45 AM.

### #2Mussi  GDNet+

Posted 20 March 2017 - 04:34 AM

First of all, don't use vector<vector<int>>. Your data will not be laid out contiguously in memory and it complicates identifying a node, which should be as simple as using it's index in the array. Two dimensional arrays are just one dimensional to the computer, so array[n][m] is the same as array[n*m] which means all you need is vector<int>. To compute the index of the 2D coordinate [x, y] you can use x * m + y.

What is A and B? Indexes to the start and goal node? It's a graph and not a grid I assume.

Yes, they're just indexes to the start and goal node. A grid is a type of graph, this doesn't matter to the algorithm.

Can I build a adjacency matrix from a grid and use the above implementation or would there be a completely different implementation for using dijkstra on a grid?

No need to build an adjacency matrix if you're using a grid, you can easily compute the indices of the adjacent nodes. The indices of the adjacent nodes are i +-1 and i +- m + <-1, 0, 1>.

How would I setup "vector> adj" so it described this map?

vector<int> { 1, 1, 1, 0, 0, 1, 1, 1, 1 }

I need a minimal pathfind but they seem to always look like the one above.

Minimal in what sense and why?

### #3Alberth  Members

Posted 20 March 2017 - 01:15 PM

Dijkstra is equivalent to A* with 0 for the estimate.

That estimate makes that the algorithm doesn't care about the estimate at all, it simply always expands the nearest unexpanded node. On an regular grid that makes it expand in all directions equally fast.

For a path from A to B, Dijkstra only makes sense if you cannot compute an estimate for the remaining distance to B. It is also useful for getting many distances from A (a single Dijkstra run gives all of them, a single A* run gives you just 1 distance).

### #4Khatharr  Members

Posted 20 March 2017 - 05:17 PM

array[n*m]

To be clear, this is the length of the array, not the index of an element.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #5suliman  Members

Posted 21 March 2017 - 09:57 AM

Im trying to change the code to understand what's going on. See below.

I try to make it to go from A=(0,0) to B=(2,0), is this correct? Where is the found path stored? I want the map to be described row by row. In my example below again:

1 0 1
1 0 1
1 1 1

So I need to go down, then right then up to start at (0,0) and reach the tile at position (2,0).

int dijk(int A, int B, const unsigned char* pMap, const int mapW, const int mapH) {
int n = mapW*mapH;
vector<int> dist(n);
vector<bool> vis(n);

for(int i = 0; i < n; ++i) {
dist[i] = inf;
}
dist[A] = 0;

for(int i = 0; i < n; ++i) {
int cur = -1;
for(int j = 0; j < n; ++j) {
if (vis[j]) continue;
if (cur == -1 || dist[j] < dist[cur]) {
cur = j;
}
}

vis[cur] = true;
for(int j = 0; j < n; ++j) {
int path = dist[cur] + pMap[cur+j*mapW];
if (path < dist[j]) {
dist[j] = path;
}
}
}

return dist[B];
}

int main() {
unsigned char pMap[] = {1, 0, 1, 1, 0, 1, 1, 1, 1};
int ans = dijk(0,2,pMap,3,3);
}


Edited by suliman, 21 March 2017 - 10:36 AM.

### #6Alberth  Members

Posted 21 March 2017 - 10:21 AM

Instead of reverse engineering the code, it may be useful to read the algorithm that it implements: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

### #7suliman  Members

Posted 21 March 2017 - 10:37 AM

I did read that, I understand better if I can follow the code, but I'm not yet quite there.

### #8suliman  Members

Posted 21 March 2017 - 11:34 PM

Is the dist-vector used to hold the path? It seems to just calculate costs (distances) for each tile (with very high distances for node [3] and [4]).

### #9Alberth  Members

Posted 21 March 2017 - 11:50 PM

Why would you need the path if you know the minimal distance to the starting point for all nodes?

From any node (with a non-zero distance) find the direct neighbour with the smallest distance.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.