Where i need help has multiple parenthesis beside them thank you. def readMatrix(inputfilename): ''' Returns a two-dimentional array created from the data in the given file. Pre: 'inputfilename' is the name of a text file whose first row contains the number of vertices in a graph and whose subsequent rows contain the rows of the adjacency matrix of the graph. ''' # Open the file f = open(inputfilename, 'r') # Read the number of vertices from the first line of the file n = int(f.readline().strip()) # Read the rest of the file stripping off the newline characters and splitting it into # a list of intger values rest = f.read().strip().split() # Create the adjacency matrix adjMat = [] (I need help creating the adjacency matrix)()() ... # Return the matrix return adjMat testFile = input("Enter the name of the input file") graphMatrix = readMatrix(testFile) graphMatrix def Prim(m): ''' Runs Prim's algorithm on the graph G with adjacency matrix 'm'. POST: The list of edges in a minimum spanning tree of G and the total weight of the spanning tree has been returned. PRE: 'm' is the adjacency matrix of a connected graph. ''' # Initialize dictionaries to hold the nearest and distance vectors n = len(m) # the number of vertices is equal to the number of rows in the matrix distance = {} for i in range(1,n): distance[i] = m[0][i] nearest = {} for i in range(1,n): nearest[i] = 0 # Implement Prim's algorithm (i need help implementing the prim's algorithm)()() ... # Create the list of edges and calculate the total weight (i need help Creating the list of edges and calculate the total weight)()() ... return list_of_edges, weight edgeList, totalWeight = Prim(graphMatrix) print(f'The list of edges in the spanning tree is: {edgeList}') print(f'The total weight of the spanning tree is: {totalWeight}')
Where i need help has multiple parenthesis beside them thank you.
def readMatrix(inputfilename):
''' Returns a two-dimentional array created from the data in the given file.
Pre: 'inputfilename' is the name of a text file whose first row contains the
number of vertices in a graph and whose subsequent rows contain the rows of
the adjacency matrix of the graph. '''
# Open the file
f = open(inputfilename, 'r')
# Read the number of vertices from the first line of the file
n = int(f.readline().strip())
# Read the rest of the file stripping off the newline characters and splitting it into
# a list of intger values
rest = f.read().strip().split()
# Create the adjacency matrix
adjMat = [] (I need help creating the adjacency matrix)()()
...
# Return the matrix
return adjMat
testFile = input("Enter the name of the input file")
graphMatrix = readMatrix(testFile)
graphMatrix
def Prim(m):
''' Runs Prim's
POST: The list of edges in a minimum spanning tree of G and the total weight
of the spanning tree has been returned.
PRE: 'm' is the adjacency matrix of a connected graph. '''
# Initialize dictionaries to hold the nearest and distance vectors
n = len(m) # the number of vertices is equal to the number of rows in the matrix
distance = {}
for i in range(1,n):
distance[i] = m[0][i]
nearest = {}
for i in range(1,n):
nearest[i] = 0
# Implement Prim's algorithm (i need help implementing the prim's algorithm)()()
...
# Create the list of edges and calculate the total weight (i need help Creating the list of edges and calculate the total weight)()()
...
return list_of_edges, weight
edgeList, totalWeight = Prim(graphMatrix)
print(f'The list of edges in the spanning tree is: {edgeList}')
print(f'The total weight of the spanning tree is: {totalWeight}')
Step by step
Solved in 4 steps with 4 images