Adjacency Matrix

6 views
Skip to first unread message

Michael Hite

unread,
Feb 26, 2014, 9:47:55 AM2/26/14
to cs...@googlegroups.com
Anyone have any advice for creating the adjacency matrices.  For some reason the way I've been trying to make them when inputting ones into the matrix they don't show up in the correct locations.  Any advice is appreciated.
Thanks,
Mike

amshahan

unread,
Feb 26, 2014, 12:27:49 PM2/26/14
to cs...@googlegroups.com
For me the problem was the initial creation of the matrix itself. Originally, I tried graph.m = graphLength * [graphLength * [0]] in order to create the matrix, but that didn't seem to work quite right, it ended up setting the entire row to a value.

Later, I found that the correct way to initialize the matrix was
graph.m = [[0 for x in range(graphLength)] for x in range(graphLength)]

and then just accessing it by graph.m[val1][val2] = 1 seemed to work correctly by placing just one value.

Hope this helps,
Austin

Michael Hite

unread,
Feb 26, 2014, 12:30:48 PM2/26/14
to cs...@googlegroups.com

Ok well I have the matrix set up almost identical to that but my problem lies in inputting the one into the val1, val2 space. I think the way that I'm iterating through the nodes is wrong.
Thanks for the help.

--
You received this message because you are subscribed to the Google Groups "cs310" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cs310+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

amshahan

unread,
Feb 26, 2014, 12:35:03 PM2/26/14
to cs...@googlegroups.com
I'm pretty sure the way thatwe iterated through them was

for node in graph.nodes:
    for option in node.edge:
        #Then in order to access the nodes, you have both the "here node" and "there node" in option, and the "here node" is also equivalent to node initialized in the outer loop


On Wednesday, February 26, 2014 9:47:55 AM UTC-5, mhite1 wrote:

Nithin Uppalapati

unread,
Feb 26, 2014, 12:37:53 PM2/26/14
to cs...@googlegroups.com
So get the node "there" from edge. For each node loop through node.out , and get "there" nodes. Then get of "there" node call it dest. 

Then 

AdjM[node.id][dest]=1


--
You received this message because you are subscribed to the Google Groups "cs310" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cs310+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


--
Sent from Gmail Mobile

Michael Hite

unread,
Feb 26, 2014, 12:38:18 PM2/26/14
to cs...@googlegroups.com

And node.Edge would be each edge the node connects to correct?

--

Nithin Uppalapati

unread,
Feb 26, 2014, 12:39:01 PM2/26/14
to cs...@googlegroups.com
Small correction:

Get id of "there" node call it dest

Quinn Jones

unread,
Feb 26, 2014, 12:41:04 PM2/26/14
to cs...@googlegroups.com

I also found it helpful to to create a dictionary of nodes to their index in the matrices so you can easily relate the two.  This makes it super easy to find the coordinates of where to put your 1's in the matrix given just a here node and a there node.

--Quinn Jones
qjo...@mix.wvu.edu

On Feb 26, 2014 12:35 PM, "amshahan" <amsh...@mix.wvu.edu> wrote:
--
Reply all
Reply to author
Forward
0 new messages