Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GraphAdjacencyMatrix.py #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

keshav549
Copy link

class Vertex:
def init(self, node):
self.id = node
# Mark all nodes unvisited
self.visited = False

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

def getConnections(self, G):
    return G.adjMatrix[self.id]

def getVertexID(self):
    return self.id

def setVertexID(self, id):
    self.id = id

def setVisited(self):
    self.visited = True

def __str__(self):
    return str(self.id)

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)

def setVertex(self, vtx, 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
    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  
	
def getVertices(self):
    vertices = []
    for vertxin in range(0, self.numVertices):
        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)	

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

if name == 'main':
G = Graph(5)
G.setVertex(0, 'a')
G.setVertex(1, 'b')
G.setVertex(2, 'c')
G.setVertex(3, 'd')
G.setVertex(4, 'e')
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())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant