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_Tasks/STD_3 - Adjacency Matrix.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
74 lines (55 sloc)
3.15 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
import sys | |
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 | |
#v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v | |
'''Adjacency Matrix | |
input: edges | |
output: unweighted matrix of values | |
Implements a method to add, remove, and display edges of an adjacency matrix | |
''' | |
def edge(self, vertex1, vertex2, task): #takes the values passed in the section of code below | |
if task == "add": | |
print("adding edge ",vertex1+1,vertex2+1) #shows the user what is happening | |
self.adjMatrix[vertex1][vertex2] = 1 #sets the value of the matrix to 1 at the location entered | |
self.adjMatrix[vertex2][vertex1] = 1 #undirected, so there will be symmetry in the matrix | |
elif task == "del": | |
print("removing edge",vertex1+1,vertex2+1) | |
self.adjMatrix[vertex1][vertex2] = 0 | |
self.adjMatrix[vertex2][vertex1] = 0 | |
def dispMatrix(self): #this section lays out the matrix and spaces everything so its more readable | |
rowCount = 1 #the edges are entered starting from 0, but we want the matrix to be displayed from 1 onwards | |
xStart = 1 #also want the columns to be 1 or higher | |
xLim = self.size #will change the number of columns based on how many values exist | |
xLine = self.size * 3 #multiplied by 3 as a hyphen is roughly a third of the length of a letter | |
print() | |
print(end=' ') #keeps the printing on the same line | |
for i in range(0,xLim): #will increase the number of columns until the maximum is reached | |
print("",xStart, end=' ') | |
xStart += 1 | |
print() | |
print(" ","-"*xLine) #prints dividing line for readability | |
for row in self.adjMatrix: #will print each row in the matrix | |
print(rowCount, "|", row) | |
rowCount += 1 | |
print(end='') | |
#^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ | |
#remember list indexing - this is 1 out, unless we start the matrix at 0 (not a +ve integer) | |
def main(): | |
g = Graph(6) | |
#v v v v v v v v v v v v v v v v v v v v v v | |
g.edge(0,0,"add") | |
g.edge(0,2,"add") | |
g.edge(1,2,"add") | |
g.edge(2,5,"add") | |
g.edge(4,4,"add") | |
g.edge(3,5,"add") #these simply populate the matrix with values, starting from 0 | |
g.edge(2,0,"del") #this removes one of the values from the matrix | |
g.dispMatrix() #calls the function to display the matrix | |
#^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ | |
if __name__ == '__main__': | |
main() | |