You are tasked to define methods of a graph class implementation to be able to create a graph from: (a) the number of vertices, (b) a list of directed edges, and/or (o) from a set of graph methods which can add and/or remove vertices and/or edges. Specifically you need to define the following Graph methods. • A. add_vertex • B. add_edge () . C. initialize_graph) • D. remove_edge (0) • E. remove_vertex() Grading: • [25%) Test Case 0 and 1 makes sure that you have defined the Graph methods add_vertex() and add_edge(). . [25%] Test Cases 2 and 3 make sure that you have: (a) properly defined the function add_edge() to ignore Invalid input, and (b) defined the Graph method initialize_graph() properly, • [25%] Test Cases 4 and 5 make sure that you have properly defined the Graph method remove_edge() and make sure it ignores invalid input. . [25%] Test Cases 6 and 7 make sure that you have properly defined the Graph method remove_vertex () and make sure it ignores invalid input. Input Format The first line of the input contains three non-negative integers V. E, and C separated by white spaces. Vis the number of vertices of the graph to be added (initially): E is the number of undirected edges to be added (Initially), and is the number of graph methods to be executed after initializing the graph. IfE > 0, the next E lines will each contain three non-negative integers ul, u2, cost corresponding to undirected edges; where ul is the first vertex, v2 is the second vertex, and cost is the weight associated with the undirected edge between veretices vi to vertex u2. If C > 0, the next C lines will each contain a graph method to be called. Constraints • Do not create additional class attributes, helper methods, or helper functions. Do not modify formal parameters. Just make use of the existing methods and attributes. • For reduced difficulty, the method remove_vertex() will only be tested to remove the last existing vertex. This is to remove the need to shift the vertex values in the adjacency list when vertices are removed. Graph methods will ignore invalid input arguments, i.e. do nothing to the graph. .
Output Format The output is a formatted string that is generated by the method print_graph () of the graph class. See sample outputs for more information. Sample Input o O @4 add_vertex add_vertex) add_vertex() add_vertex Sample Output 0 JE - IV = 4, [1] => I [2] => [3] => 0 [4] => 0 Sample Input 1 OG 9 add_vertex() add_vertex add_vertex add_vertex) add_edge(1, 2, 5) add_edge(1, 3, 7) add_edge (2, 3, 11) add_edge (2, 4, 13) add_edge (3, 4, 17) Sample Output 1 TV - 4, TET - 5 [1] -> [(2, 5), (3, 7)] [2] -> ((1, 5), (3, 11), (4, 13)] (3] => [(1,7), (2, 11), (4, 17)] [4] => [(2, 13), (3, 17)]
Sample Input 2 330 125 137 2 3 11 Sample Output 2 V = 3, E = 3 [1] -> [(2, 5), (3, 7)] [2] -> [(1, 5), (3, 11)] [3] => [(1,7), (2, 11)] Sample Input 3 2 3 3 125 1 37 2 3 11 add_vertex) add_edge (1, 3, 13) add_edge (2, 3, 17) Sample Output 3 IV = 3, E = 3 [1] => [(2, 5), (3, 13)] [2] => [(1, 5), (3, 17)] [3] => [(1, 13), (2, 17)]
Python 3 111 4 7 8 9 # NNN # 1 - class Graph (object): 2 3 This graph implements an undirected graph using an adjacency list. Note that the implementation assumes that vertex identifiers are positive 5 numbers; thus the first element of the adjacency list (which has index B) 6 is not used and has an arbitrary placeholder None. DO NOT MODIFY CODE BELOW def _init__(self): YO 11 Initializer method that initializes the member variables of the class. 12 13 self.v = 0 # number of vertices 14 self. E = 0 # number of undirected edges (Note: This corresponds to two separate 15 entries in the adjacency list.) 16 self.adjacency_list = [None] + index 8 is used as a placeholder 17 18 def print_graph(self): 19 20 This function prints a formatted adjacency list representation of 21 the weighted graph. 22 23 print('|V = {self.v}. 1E| = {self.E}') 24 for n in range(1, self.V+1): 25 print('[{n}] => {self.adjacency.list[n]}') 26 DO NOT MODIFY CODE ABOVE 27 28 def add_vertex (self): 29 30 This function adds a vertex in the graph. 31 32 INSERT CODE BELOW 39 # A. Add a vertex by modifying the appropriate graph attribute/s 34 35 36 37 38 39 INSERT CODE ABOVE *** # 56. WENNGANNNNSO # 40
40 41 42 def add_edge (self, vi, v2, cost): This function adds an undirected edge in the graph. 44 Parameters 47 49 50 51 vl : int first vertex v2: int second vertex cost : int weight associated with undirected edge between the vertices vi and v2 53 54 INSERT CODE BELOW # B. Add an edge by modifying the appropriate graph attribute/s The function does nothing if the edge cannot be added. 57 + 60 INSERT CODE ABOVE DANNNN88888888985KWA 63 64 def initialize_graph (self, V, edge_list): This function initializes an empty graph using the number of vertices and the list of undirected edges by making use of available methods. 66 67 68 69 70- Parameters 72- 73 Vint number of vertices to be added to the graph edge list: list of tuples list of undircted edges with format (v1, v2, cost) to be added to the graph INSERT CODE BELOW # C. Initialize the graph using the given parameters FIF 75 76 # 78 79 81 83 INSERT CODE ABOVE 86
88- def remove_edge (self, vi, v2): This function removes the undirected edge between vertex vi to vertex v2. Parameters vl : int first vertex of undirected edge to be removed v2 : int second vertex of undirected edge to be removed INSERT CODE BELOW # D. Remove the specified undirected edge. This function does nothing if the specified edge does not exist or is not valid. # # INSERT CODE ABOVE 89 90 91 92 93 94 95 96 97 99 99 100 101 102 103 104 105 106 107 108 109 110- 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 125 127 128 129 def remove_vertex (self, v): This function removes a vertex in the graph and all of its associated edges. Parameters Vint vertex to be removed # INSERT CODE BELOW # E. Remove the specified vertex. This function does # nothing if the specified vertex does not exist. INSERT CODE ABOVE
name == DO NOT MODIFY CODE BELOW main': # Reads the first line of the input V, E, C = input('').rstrip('\r\n').split() V, E, C = int(V), int(E), int(C) # Create a graph instance 8 = Graph() 130 131 # 132.if 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1.56 157 159 159 160 161 # Parse the remaining input into two lists edge_list = [] for e in range(E): vi, v2, cost = input().rstrip('\r\n').split() edge_list.append((int(v1), int(v2), int(cost))) command_list = [] forn in range(C): command_list.append(input().rstrip('\r\n')) # Initialize the graph using the value of V and/or the edge_list if V > O or E > 8: g. initialize_graph (V, edge_list) # Runs the elements in command_list as python statements if C > 0: for command in command_list: exec('.' + command) # Generate the required formatted output 8.print_graph) DO NOT MODIFY CODE ABOVE Line: 1 Col: 1
You are tasked to define methods of a graph class implementation to be able to create a graph from: (a) the number of ve
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
You are tasked to define methods of a graph class implementation to be able to create a graph from: (a) the number of ve
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!