Read the question in the picture first and then solve the question please. def readFile(): # reading file from as input # change the file name according to yours f = open("graph.txt", "r") # first line of input contains the number of vertices in the graph n = f.readline() # strip() gets rid of the new line # try printing n without strip() print(n.strip()) n = n.strip() print(type(n)) # n is of type string. we need to convert it to int n=int(n) print(type(n)) # the second line of the file contains the number of connections c = f.readline() c = c.strip() c = int(c) print(c) buildGraphUsingDictionary(c,f) buildGraphUsingListofLists(c,f) # we want to build an adjacency list like the following # A -> B,C # One vertex can be connected to multiple vertices # which means multiple values are associated with one vertex # one data structure that can be used is a dictionary of lists # {A:[B,C]} def buildGraphUsingDictionary(c,f): # creating a dictionary graph = {} # the following lines of the file contain the connections # creating a directed graph (a,b means a is connected to b) counter = 0 while (counter 2,4 # 2 -> 4 # 3 -> 1,4 # 4 -> 2 # notice this method takes both the graphs as parameters # this means you have print the same output in the same style for both the datastructures # if graph is none then print from listGraph # if listGraph is none then print from graph def printGraph(graph,listGraph): # Your code return # delete this line # TO DO
Read the question in the picture first and then solve the question please.
def readFile():
# reading file from as input
# change the file name according to yours
f = open("graph.txt", "r")
# first line of input contains the number of vertices in the graph
n = f.readline()
# strip() gets rid of the new line
# try printing n without strip()
print(n.strip())
n = n.strip()
print(type(n))
# n is of type string. we need to convert it to int
n=int(n)
print(type(n))
# the second line of the file contains the number of connections
c = f.readline()
c = c.strip()
c = int(c)
print(c)
buildGraphUsingDictionary(c,f)
buildGraphUsingListofLists(c,f)
# we want to build an adjacency list like the following
# A -> B,C
# One vertex can be connected to multiple vertices
# which means multiple values are associated with one vertex
# one data structure that can be used is a dictionary of lists
# {A:[B,C]}
def buildGraphUsingDictionary(c,f):
# creating a dictionary
graph = {}
# the following lines of the file contain the connections
# creating a directed graph (a,b means a is connected to b)
counter = 0
while (counter<c):
line = f.readline() # reading each libe
a,b = line.split(",") # splitting the vertices
b = b.strip() # getting rid of \n from the end
# we first search if the value inside variable a exists in the dictionary or not
if(a in graph):
# if yes, then append() the value in b to a
graph[a].append(b)
else:
# create a new list in graph with a as the key and b as the value
graph[a] = [b]
print(a)
print(b)
counter+=1
print(graph)
printGraph(graph, None)
# TO DO
# This method must be completed by you
# You should code in such a way that the output should be
# 1 -> 2,4
# 2 -> 4
# 3 -> 1,4
# 4 -> 2
# notice this method takes both the graphs as parameters
# this means you have print the same output in the same style for both the datastructures
# if graph is none then print from listGraph
# if listGraph is none then print from graph
def printGraph(graph,listGraph):
# Your code
return # delete this line
# TO DO
# I have shown you how to build a graph using a dictionary of list
# now your job is to build a graph using list of lists [[E,B],[C,D]]
# it means A -> E,B and B -> C,D
def buildGraphUsingListofLists(c,f):
listGraph = [] # do not change the name of the variable
# your code
printGraph(None,listGraph)
return # delete this line
# ======================Program starts here.========================
# read file using the readFile() method
readFile()
![You are given a piece of python code and a txt file named
graph.txt. The input file contains the number of vertices in the
first line, n, followed by the number of connections, c. Then
there are c numbers of lines mentioning the connection between a
pair of vertices in the form a,b -a is connected to b. The
graph given is a directed graph.
If you look at the python file you will see that the methods
readFile() and buildGraphUsing Dictionary() have been done for
you. Not only that, comments have been added for you to
understand how each line is working and why it has been used.
Read the comments very carefully and study the code and of
course run the code to match your input with the output. Your
job is to complete the buildGraphUsingListofLists() method and
the printGraph() method. Google "list of lists in python" to aid
you complete the tasks.
Input:
5
6
1,2
3,1
1,4
2,4
3,4
4,2](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F63d9f7fe-3368-4fbf-b1d4-630dadf551c1%2F7c22839c-c2a7-481b-a1b9-3667516270e4%2Fb42fcb_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)