Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
class Graph(object):
def __init__(self, size):
self.adjMatrix = []
for i in range(size):
self.adjMatrix.append([0 for i in range(size)])
print(self.adjMatrix)
self.size = size
def add_vertex(self):
'''Method for adding a vertex into graph.'''
for i in range(self.size): # iterate through vertices without the added one
self.adjMatrix[i].append(0) # add zero for every vertex
# this zero represents connection with the added vertex
self.size += 1 # increase the size of the graph
self.adjMatrix.append([0 for i in range(self.size)]) # add zeros for added vertex
print('Vertex added successfully.') # print info message
def add_edge(self, vertex1, vertex2):
'''Method for adding an edge between two vertices in graph.'''
if vertex1 == vertex2: # if the vertices are same
print('Both vertices are same!') # print info message
elif self.adjMatrix[vertex1][vertex2] == 1: # if the vertices already have edge
print('The edge already exists.') # print info message
else: # the vertices have no edge and they are different
self.adjMatrix[vertex1][vertex2] = 1 # add edge between the vertices
self.adjMatrix[vertex2][vertex1] = 1 # add edge between the vertices also from the other side
print('Edge is added successfully.') # print info message
def remove_edge(self, vertex1, vertex2):
'''Method for removing an edge between two vetices in graph.'''
if vertex1 == vertex2: # if the vertices are same
print('Both vertices are same!') # print info message
elif self.adjMatrix[vertex1][vertex2] == 0: # if the vertices have no edge
print('There is no edge to be removed') # print info message
else: # if the vertices are different and have an edge
self.adjMatrix[vertex1][vertex2] = 0 # remove the edge between vertices
self.adjMatrix[vertex2][vertex1] = 0 # remove the edge between vertices from the other side
print('Edge is removed successfully.') # print info message
def print_matrix(self):
'''https://thispointer.com/python-how-to-pad-strings-with-zero-space-or-some-other-character/ - ADAPTED
Method for printing the graph in a matrix form.'''
header = '' # create empty string variable
divLine = '--' # create line format
for i in range(1, self.size+1): # iterate through the vertices
header = header + ' ' + str(i) # add numbers of the vertices into header
print(" ", header) # print the header
print(' ', divLine.ljust(len(header),'-')) # print the dividing line
i=1 # set index to one
for row in self.adjMatrix: # iterate through vertices
print(i,'|',end=" ") # print number of the vertex and dividing line
i+=1 # increment the index
for val in row: # iterate through edges in every vertex
print(val, end=" ") # print the edge value
print('\n') # jump to a new line
def main():
g = Graph(4)
g.add_edge(0,1)
g.add_edge(0,2)
g.add_edge(1,2)
g.add_edge(2,3)
g.add_edge(1,3)
g.remove_edge(1,3)
g.add_vertex()
g.add_vertex()
g.print_matrix()
if __name__ == '__main__':
main()