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

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
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
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning