Related questions
Concept explainers
How would you solve this by using Python 3.8 version
class Graph:
def __init__(self, num_nodes):
self.num_nodes = num_nodes
# Initialize a 2D matrix with zeros
self.adj_matrix = [[0] * num_nodes for _ in range(num_nodes)]
def add_edge(self, node1, node2):
# Add an undirected edge between node1 and node2
self.adj_matrix[node1][node2] = 1
self.adj_matrix[node2][node1] = 1
def remove_edge(self, node1, node2):
# Remove the edge between node1 and node2
self.adj_matrix[node1][node2] = 0
self.adj_matrix[node2][node1] = 0
def num_edges(self):
num_edges = 1
for i in range(self.num_nodes):
for j in range(i+1, self.num_nodes):
if self.adj_matrix[i][j] == 1:
num_edges += 1
return num_edges
def num_edges_in_set(self, node, subset):
# count the number of edges joining the given node and nodes in subset.
# example input: 1, {0, 2}
num_edges = 0
for s in subset:
if self.adj_matrix[node][s] == 1:
num_edges += 1
return num_edges
def edges_between_sets(self, setX, setY):
# TODO: return the edges that have one end in setX, another end in setY
pass
def edge_cut(self, setX):
# TODO: return the edge cut of a given subset of vertices
pass
def is_even(self):
# TODO: return true is the graph is even, false otherwise
pass
def count_components(self):
# return the number of components in graph.
def dfs(node, visited):
visited[node] = True
for neighbor, is_edge in enumerate(self.adj_matrix[node]):
if is_edge and not visited[neighbor]:
dfs(neighbor, visited)
visited = [False] * self.num_nodes
num_components = 0
for node in range(self.num_nodes):
if not visited[node]:
# If the node hasn't been visited, it's a new component
dfs(node, visited)
num_components += 1
return num_components
def is_connected(self):
# TODO: return true is the graph is connected, false otherwise
pass
def is_bridge(self, edge):
# TODO: return true if the given edge is an bridge, false otherwise
# edge is given as a python pair.
pass
def is_adjacent(self, node1, node2):
if self.adj_matrix[node1][node2] == 1:
return True
else:
return False
def print_graph(self):
# Print the adjacency matrix
for row in self.adj_matrix:
print(" ".join(map(str, row)))
if __name__ == "__main__":
# use case
test_graph = Graph(3)
test_graph.add_edge(1,0)
test_graph.add_edge(1, 2)
print("Is even: ", test_graph.is_even())
x = {1}
print("Edge cut is ", test_graph.edge_cut(x))
print("Is connected: ", test_graph.is_connected())
print("Is bridge: ", test_graph.is_bridge((1, 2)))
from Graph import *
import copy
def Fleury(g, u):
# TODO: implement Fleury's
# input: a graph and a vertex of the graph,
# return: an array of vertices representing the Euler tour.
# Return empty array if g is not even or not connected.
if not g.is_even() or not g.is_connected():
return []
# Run the algorithm when g is even and connected
W = [u]
x = u
F = copy.deepcopy(g)
edge_cut = F.edge_cut({x})
current_edge = (-1, -1)
pass
# test
test_graph = Graph(5)
test_graph.add_edge(0,3)
test_graph.add_edge(0,2)
test_graph.add_edge(1,3)
test_graph.add_edge(1,2)
test_graph.add_edge(2,3)
test_graph.add_edge(2,4)
test_graph.add_edge(3,4)
print(Fleury(test_graph, 1))
Step by stepSolved in 2 steps
- PLEASE HELP, PYTHON THANK YOU class Graph(object): def __init__(self, graph_dict=None): ################# # Problem 1: # Check to see if the graph_dict is none # if so, create an empty dictionary and store it in graph_dict ################## #DELETE AND PUT IN THE IF AND ASSIGNMENT STATEMENTS self.__graph_dict = graph_dict ################# # Problem 2: # Create a method called vertices and pass in self as the parameter ################## #DELETE AND PUT IN THE METHOD DEFINITION """ returns the vertices of a graph """ return list(self.__graph_dict.keys()) def edges(self): """ returns the edges of a graph """ ################# # Problem 3: # Return the results of the __generate_edges ################## #DELETE AND PUT IN THE RETURN STATEMENTSarrow_forwardPLEASE HELP, PYTHON THANK YOU def add_vertex(self, vertex): if vertex not in self.__graph_dict: self.__graph_dict[vertex] = [] def add_edge(self, edge): edge = set(edge) (vertex1, vertex2) = tuple(edge) ################# # Problem 4: # Check to see if vertex1 is in the current graph dictionary ################## #DELETE AND PUT IN THE IF STATEMENTS self.__graph_dict[vertex1].append(vertex2) else: self.__graph_dict[vertex1] = [vertex2] def __generate_edges(self): edges = [] ################# # Problem 5: # Loop through all of the data in the graph dictionary and use the variable vertex for the loop'ed data ################## #DELETE AND PUT IN THE LOOP STATEMENTS for neighbour in self.__graph_dict[vertex]: if {neighbour, vertex} not in edges: edges.append({vertex, neighbour}) return edges...arrow_forwardJava language Use arrays in creating your class. Stack Interface (LIFO) void push(Object) Object pop() String toString() boolean isEmpty() boolean equals(Object) getIndexOf get remove private static void arrayListTests() { System.out.println("ArrayList Tests"); // todo: make more tests here ArrayList a = new ArrayList(); System.out.println("Check empty array isEmpty:" + a.isEmpty()); a.insert('B', 0); a.insert('a', 0); a.insert('t', 1); System.out.println("Check non-empty array isEmpty:" + a.isEmpty()); System.out.println(a.toString()); while (a.isEmpty() == false) { System.out.println(a.remove(0)); } // Fill over initial capacity and check that it grows for (int i = 0; i < 110; i++) { a.append(new Integer(i)); } System.out.println("Size of array after 110 adds: "+ a.size()); System.out.println("Value of last element: "+ a.get(a.size()-1)); System.out.println("Insert past end of list"); a.insert('z', 200); System.out.println("Insert negative index"); a.insert('z', -3);...arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education