Created
September 4, 2015 07:41
-
-
Save tomhennigan/30345f6e4be9bef3ac8c to your computer and use it in GitHub Desktop.
matrix representation of a graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mkmatrix(size): | |
matrix = [] | |
for _ in xrange(size): | |
matrix.append([False] * size) | |
return matrix | |
def addedge(matrix, nodea, nodeb): | |
matrix[nodea][nodeb] = True | |
matrix[nodeb][nodea] = True | |
def hasedge(matrix, nodea, nodeb): | |
return matrix[nodea][nodeb] | |
matrix = mkmatrix(10) | |
node1 = 1 | |
node2 = 2 | |
node3 = 3 | |
addedge(matrix, node1, node2) | |
print hasedge(matrix, node1, node2) | |
print hasedge(matrix, node1, node3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment