In this programming exercise you will implement two functions. The first function will prompt the user for a file containing the number of vertices and entries of the adjacency matrix of a graph. It will return a two-dimensional list (a list of lists) containing the adjacency matrix. The textfile is named inputfilename.txt  am trying to read for the matrix 8 0 1 2 3 100 100 100 100 1 0 2 100 3 4 100 100 2 2 0 4 4 100 5 100 3 100 4 0 100 100 4 100 100 3 4 100 0 3 3 3 100 4 100 100 3 0 100 1 100 100 5 4 3 100 0 2 100 100 100 100 3 1 2 0 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 (I need help creating  the adjacent matrix)     adjMat = []     (write the code here)     return adjMat testFile = input("Enter the name of the input file") graphMatrix = readMatrix(testFile) graphMatrix

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

In this programming exercise you will implement two functions. The first function will prompt the user for a file containing the number of vertices and entries of the adjacency matrix of a graph. It will return a two-dimensional list (a list of lists) containing the adjacency matrix.

The textfile is named inputfilename.txt  am trying to read for the matrix

8
0 1 2 3 100 100 100 100
1 0 2 100 3 4 100 100
2 2 0 4 4 100 5 100
3 100 4 0 100 100 4 100
100 3 4 100 0 3 3 3
100 4 100 100 3 0 100 1
100 100 5 4 3 100 0 2
100 100 100 100 3 1 2 0

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 (I need help creating  the adjacent matrix)
    adjMat = []
    (write the code here)
    return adjMat

testFile = input("Enter the name of the input file")

graphMatrix = readMatrix(testFile)
graphMatrix

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 4 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

hello i am getting this error when i run the code

[]
adjrow = []
n=rest.split("\n")
a=[]
for i in n:
adjMat =
a.append (i.split(" "))
for i in a:
adjrow=[]
for j in i:
if j=="100":
adjrow.append ("INF")
else:
adjrow.append (int(j))
adjMat.append (adjrow)
return adjMat
Test your function.
testFile = input ("Enter the name of the input file")
Enter the name of the input fileinputfilename.txt
graphMatrix = readMatrix(testFile)
graphMatrix
AttributeError
Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_16544/4153083134. py in <module>
1 graphMatrix = readMatrix(testFile)
2 graphMatrix
~\AppData\Local\Temp/ipykernel_16544/731974715. py in readMatrix(inputfilename)
adjMat = []
adjrow = []
n=rest.split("\n")
a=[]
for i in n:
20
21
22
23
24
AttributeError: 'list' object has no attribute 'split'
Transcribed Image Text:[] adjrow = [] n=rest.split("\n") a=[] for i in n: adjMat = a.append (i.split(" ")) for i in a: adjrow=[] for j in i: if j=="100": adjrow.append ("INF") else: adjrow.append (int(j)) adjMat.append (adjrow) return adjMat Test your function. testFile = input ("Enter the name of the input file") Enter the name of the input fileinputfilename.txt graphMatrix = readMatrix(testFile) graphMatrix AttributeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_16544/4153083134. py in <module> 1 graphMatrix = readMatrix(testFile) 2 graphMatrix ~\AppData\Local\Temp/ipykernel_16544/731974715. py in readMatrix(inputfilename) adjMat = [] adjrow = [] n=rest.split("\n") a=[] for i in n: 20 21 22 23 24 AttributeError: 'list' object has no attribute 'split'
Solution
Bartleby Expert
SEE SOLUTION
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY