0

I have a question on one of the previous Stack Overflow post @ Knight's Shortest Path on Chessboard

I understand the reply on 'ok, it's a graph question, and its sparse matrix is like':

(a1,b3)=1,
(a1,c2)=1,
  .....

which describe existing edges. However i still don't know what Data Structure of this Graph should looks like (is it an adjacency matrix? stated as 'sparse matrix' above, or something else?), so that it can be readily used by Dijkstra's algorithm.

http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm.

From the algorithm description, it looks convenient if the graph data structure is a set of vertex, with neighbor vertex information available. But how do we achieve this?

How can I write out a sample data structure for this graph? I am seeking an understanding of how it can be linked conveniently to Dijkstra's algorithm.

halfer
  • 18,701
  • 13
  • 79
  • 158
user1559625
  • 2,382
  • 2
  • 27
  • 56

2 Answers2

2

The graph is very sparse (64 vertices, each vertex has at most 8 edges) thus an adjacency matrix is a waste IMO.

A better structure for this will be adjacency list:

v1->v2,v3,v4,v5
v2->v1,...
...

The idea is indeed to hold a Set<Vertex>, and for the Vertex type to have a field: List<Vertex> neighbors, which will contain all the vertex's neighboring vertices.

There is no need in this case for some additional weight information - since the graph is unweighted.

Also - Dijkstra's algorithm is redundant in here. Again, the graph is unweighted - so a simpler (to program and understand) and faster (run time) algorithm to find the shortest path is a BFS for unweighted graphs.

amit
  • 166,614
  • 24
  • 210
  • 314
  • Thanks amit. The data structure makes a lot of sense. And as you said, BFS is the right choice (i am actually using BFS, in the question i borrowed Dijkstra to raise data structure question:)). One more question, in my problem i need to find the path in addition to the length of shortest path. However, the 'previous[v]' idea stated in Dijkstra i am not sure how to implement it in BFS. Because i sort of create my adjacency list on the run of BFS, and not put intermediate vertex in a set, so those intermediate vertex are gone when algorithm is done. So what can i do? – user1559625 Aug 30 '12 at 11:17
  • @user1559625: I once addressed this issue in [this thread](http://stackoverflow.com/q/9590299/572670). Tell me if you have any difficulties in understanding it. – amit Aug 30 '12 at 11:23
  • Cool. Thanks Amit. It's a big help. Any recommendation on a good beginner/intermediate book on algorithm :)? I am weak on algorithm. Have a good day and appreciate all the assistance. – user1559625 Aug 30 '12 at 11:34
  • @user1559625 [CLRS Introduction to Algorithms](http://en.wikipedia.org/wiki/Introduction_to_Algorithms) is a good starter. But only a starter, experience is the main issue. Participate in programming contests, write programs, answer SO questions - those things will make you better programmer. – amit Aug 30 '12 at 11:38
0

Since there are only 64 tiles, you can conveniently put the adjacency matrix in an array of 64 integers of 64 bits each. Sure it's sparse, but it's also tiny, so the waste, if it exists at all (pointers are pretty big compared to single bits), will be small too.

Extracting a list of indices from a bitvector is also especially easy when it's sparse, but you don't even need that - you could let the front be a queue of bitvectors instead, and the "already visited" set could be a single bitvector.

edit: ok actually you may still need it if you use that trick, but then it remains that it just takes a couple of fast operations such as bitscan and x &= x -1.

harold
  • 53,069
  • 5
  • 75
  • 140