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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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
Transcribed Image Text: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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Merge Sort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education