Permalink
Cannot retrieve contributors at this time
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?
5003CEM/adjacency_matrix_basics.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
64 lines (41 sloc)
1.93 KB
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
class Graph(object): | |
# number of vertices | |
numberOfVertices = 0 | |
# adjacency matrix | |
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 | |
#methods for (1) adding a vertex; (2) adding an edge; (3) removing an edge; and (4) printing the | |
#matrix should appear here | |
# this method is for adding vertex | |
def VertexAddition(self): | |
# increasing the number of vertices | |
self.numberOfVertices = self.numberOfVertices + 1 | |
# initializing the new elements to 0 | |
for i in range(0, self.numberOfVertices): | |
self.adjMatrix[i][self.numberOfVertices-1]= 0 | |
self.adjMatrix[self.numberOfVertices-1][i]= 0 | |
#This function adds an edge by accepting the adjacentMatrix, u and v as a parameter | |
def EdgeAddition(adjMatrix, u, v): | |
# To add i only append the values. | |
adjMatrix[u].append(v) | |
adjMatrix[v].append(u) | |
def EdgeDeletion(adjacentValues, u, v): | |
# Here am looping through the first section while at the same time removing the second element | |
for x in range(len(adjacentValues[u])): | |
if (adjacentValues[u][x] == v): | |
adjacentValues[u].pop(x); | |
break; | |
# Looping through the last section and removing the first element | |
for x in range(len(adjacentValues[v])): | |
if (adjacentValues[v][x] == u): | |
adjacentValues[v].pop(x); | |
break; | |
#remember list indexing - this is 1 out, unless we start the matrix at 0 (not a +ve integer) | |
def main(): | |
g = Graph(6) | |
if __name__ == '__main__': | |
main() |