Add graph references
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
# Adjacency Matrix representation in Python
|
||||
|
||||
|
||||
class Graph(object):
|
||||
|
||||
# Initialize the matrix
|
||||
def __init__(self, size):
|
||||
self.adjMatrix = []
|
||||
for _ in range(size):
|
||||
self.adjMatrix.append([0 for _ in range(size)])
|
||||
self.size = size
|
||||
|
||||
# Add edges
|
||||
def add_edge(self, v1, v2):
|
||||
if v1 == v2:
|
||||
print("Same vertex %d and %d" % (v1, v2))
|
||||
self.adjMatrix[v1][v2] = 1
|
||||
self.adjMatrix[v2][v1] = 1
|
||||
|
||||
# Remove edges
|
||||
def remove_edge(self, v1, v2):
|
||||
if self.adjMatrix[v1][v2] == 0:
|
||||
print("No edge between %d and %d" % (v1, v2))
|
||||
return
|
||||
self.adjMatrix[v1][v2] = 0
|
||||
self.adjMatrix[v2][v1] = 0
|
||||
|
||||
def __len__(self):
|
||||
return self.size
|
||||
|
||||
# Print the matrix
|
||||
def print_matrix(self):
|
||||
for row in self.adjMatrix:
|
||||
for val in row:
|
||||
print('{:4}'.format(val)),
|
||||
print
|
||||
|
||||
|
||||
def main():
|
||||
g = Graph(5)
|
||||
g.add_edge(0, 1)
|
||||
g.add_edge(0, 2)
|
||||
g.add_edge(1, 2)
|
||||
g.add_edge(2, 0)
|
||||
g.add_edge(2, 3)
|
||||
|
||||
g.print_matrix()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user