views:

268

answers:

3

Given graph, how could i represent it using adj matrix?. I have read lots of tutorials, post, slides, etc but i cant get my head round it, i just need that little push.

alt text

+3  A: 

Every letter-number combination is one node in your graph, i.e., from A0, A1, A2, ... to F5, F6, F7. Your graph has 48 (8 columns times 6 rows in your maze) nodes, so you'll need a 48x48 matrix. If you treat it as boolean, you'll set all fields to false except the ones where there is a connection between two nodes, e.g. A0 to A1 would mean that the A0 row has a true value in the A1 column, and vice versa (because your graph is undirected).

Robert Kosara
Ok, thanks a lot. Do you reckon it will be better to use adjacency list in this case. Cuz 48x48 seems a bit redundant (mirror image) and inefficient.
Carlucho
And this matrix, by its nature, is symmetrical, so any manipulation (and storage, of course) can be done on one of the halves only.
ysap
The degree of each vertex is at most 4, so adjacency list would be A LOT more efficient.
polygenelubricants
@polygenelubricants: Thanks buddy
Carlucho
True, but it's a small graph and a matrix is generally easier to deal with in a program than a list.
Robert Kosara
Many languages (Matlab for example) also have efficient sparse-matrix storage
BlueRaja - Danny Pflughoeft
@Carlucho, for graphs 25x25 it really doesn't matter. But generally, for mazes, where there's a lot of cells (vertexes) and just a few edges (connections), it's better off with adjacency lists.
Pavel Shved
+3  A: 

Here's my attempt for the first horizontal line of the maze:

   A0 A1 A2 A3 A4 A5 A6 A7
A0 0  1  0  0  0  0  0  0
A1 1  0  0  0  0  0  0  0
A2 0  0  0  1  0  0  0  0
A3 0  0  1  0  0  0  0  0
A4 0  0  0  0  0  1  0  0
A5 0  0  0  0  1  0  0  0
A6 0  0  0  0  0  0  0  0
A7 0  0  0  0  0  0  0  0

So you can see from this that your going to end up with a symmetrical matrix due to the undirected nature of your edges and that its going to be sparsely populated.

EDIT: Matrix vs Lists

The wikipedia entry for adjacency list has some good points on the algorithmic benefits of each.

EDIT:

Wikipedia entry for Adjacency Matrix :+)

Binary Nerd
thanks for your effort buddy. I was confused because the way i was doing it was not symmetrical so i was pretty sure it was wrong. But when i thought of the idea A1....F7 i just couldn't believe it. As i asked to Robert; what do you think will be better in this case matrix or adjacency list?
Carlucho
Due to the nature of your graph, low degree and undirected, i would have thought an edge list would be a good bet. The algorithms you want to apply to the data structure should also influence your decision though.
Binary Nerd
hey buddy sorry by mistake i down-voted u it doesn't let me cancel the vote for some reason i guess because edited, can u edit anything in your post that way i can give u back the rep/vote. Thanks and sorry.
Carlucho
@Carlucho - No problem. Added a small edit.
Binary Nerd
@Binary Nerd: Nice link ;P.... By the way, if you like have a look at a new question i posted related to this, maybe you can give me some tips
Carlucho
Thanks for the tip. Looks like you got some good answers.
Binary Nerd
+1  A: 

Another way would be to have 2 boolean matrices named Hor and Ver to track the possibility of horizontal and vertical movement respectively.

Hor Matrix: Dimension:6x9
[X,YZ] represents the possiblity of a horizontal movement from [X,Y] to [X,Z] on the real board.
-1 represents the boundary

example: [A,01] is true and so is [F,-10]. But [B,23] is false.

   -10 01 12 23 34 45 56 67 7-1
A
B
C
D
E
F

Similarly

Ver Matrix: Dimension: 7x8
[XY,Z] represents the possiblity of a vertical movement from [X,Z] to [Y,Z] on the real board.
Capital o in the row represent boundary.
example: [DE,0] is true and so is [BC,7]. But [CD,0] is false.

    0 1 2 3 4 5 6 7

OA
AB
BC
CD
DE
EF
FO
codaddict