Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions src/chapter09graphs/GraphAdjacencyMatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, node):
self.visited = False

def addNeighbor(self, neighbor, G):
G.addEdge(self.id, neighbor)
G.addEdge(self.id, neighbor)

def getConnections(self, G):
return G.adjMatrix[self.id]
Expand All @@ -36,49 +36,50 @@ class Graph:
def __init__(self, numVertices, cost=0):
self.adjMatrix = [[-1] * numVertices for _ in range(numVertices)]
self.numVertices = numVertices
self.vertices = []
for i in range(0, numVertices):
newVertex = Vertex(i)
self.vertices.append(newVertex)
self.vertices = []
for i in range(0, numVertices):
newVertex = Vertex(i)
self.vertices.append(newVertex)

def setVertex(self, vtx, id):
if 0 <= vtx < self.numVertices:
self.vertices[vtx].setVertexID(id)
if 0 <= vtx < self.numVertices:
self.vertices[vtx].setVertexID(id)

def getVertex(self, n):
for vertxin in range(0, self.numVertices):
if n == self.vertices[vertxin].getVertexID():
return vertxin
return -1
if n == self.vertices[vertxin].getVertexID():
return vertxin
else:
return -1

def addEdge(self, frm, to, cost=0):
if self.getVertex(frm) != -1 and self.getVertex(to) != -1:
self.adjMatrix[self.getVertex(frm)][self.getVertex(to)] = cost
# For directed graph do not add this
self.adjMatrix[self.getVertex(to)][self.getVertex(frm)] = cost
self.adjMatrix[self.getVertex(frm)][self.getVertex(to)] = cost
# For directed graph do not add this
self.adjMatrix[self.getVertex(to)][self.getVertex(frm)] = cost

def getVertices(self):
vertices = []
vertices = []
for vertxin in range(0, self.numVertices):
vertices.append(self.vertices[vertxin].getVertexID())
vertices.append(self.vertices[vertxin].getVertexID())
return vertices

def printMatrix(self):
for u in range(0, self.numVertices):
row = []
for v in range(0, self.numVertices):
row.append(self.adjMatrix[u][v])
print row
for u in range(0, self.numVertices):
row = []
for v in range(0, self.numVertices):
row.append(self.adjMatrix[u][v])
print (row)

def getEdges(self):
edges = []
for v in range(0, self.numVertices):
for u in range(0, self.numVertices):
if self.adjMatrix[u][v] != -1:
vid = self.vertices[v].getVertexID()
wid = self.vertices[u].getVertexID()
edges.append((vid, wid, self.adjMatrix[u][v]))
return edges
for v in range(0, self.numVertices):
for u in range(0, self.numVertices):
if self.adjMatrix[u][v] != -1:
vid = self.vertices[v].getVertexID()
wid = self.vertices[u].getVertexID()
edges.append((vid, wid, self.adjMatrix[u][v]))
return edges

if __name__ == '__main__':
G = Graph(5)
Expand All @@ -87,12 +88,12 @@ def getEdges(self):
G.setVertex(2, 'c')
G.setVertex(3, 'd')
G.setVertex(4, 'e')
print 'Graph data:'
print ('Graph data:')
G.addEdge('a', 'e', 10)
G.addEdge('a', 'c', 20)
G.addEdge('c', 'b', 30)
G.addEdge('b', 'e', 40)
G.addEdge('e', 'd', 50)
G.addEdge('f', 'e', 60)
print G.printMatrix()
print G.getEdges()
print (G.printMatrix())
print (G.getEdges())