Related questions
Concept explainers
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 with 1 images
- JAVA programming languagearrow_forwardFor any element in keysList with a value greater than 50, print the corresponding value in itemsList, followed by a comma (no spaces). Ex: If the input is: 32 105 101 35 10 20 30 40 the output is: 20,30, 1 #include 2 3 int main(void) { const int SIZE_LIST = 4; int keysList[SIZE_LIST]; int itemsList[SIZE_LIST]; int i; 4 6 7 8 scanf("%d", &keysList[0]); scanf ("%d", &keysList[1]); scanf("%d", &keysList[2]); scanf("%d", &keysList[3]); 10 11 12 13 scanf ("%d", &itemsList[0]); scanf ("%d", &itemsList[1]); scanf("%d", &itemsList[2]); scanf ("%d", &itemsList[3]); 14 15 16 17 18 19 /* Your code goes here */ 20 21 printf("\n"); 22 23 return 0; 24 }arrow_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_forward
- This data structure provides a variety of benefits over others, such as a linked list or tree.arrow_forwardJava coding Can you create a remove method that removes and returns an element from an index in an array? Thanks.arrow_forwardPlease answer in python with showing the code. (this is a 1 ques in 3 part please answer the ques) i. Create a python dictionary to organize the following info: a) The capital of the USA is Washington. The capital of France is Paris. The capital of India is New Delhi. b) Print the dictionary. ii. Create the dictionary to organize the following info: a) Our 2 students are Dembe and Kate. Dembe’s major is History. Kate’s major is Anthropology. Dembe’s hobbies are chess, judo, and drawing. Kate’s hobbies are bird-watching, stained-glass, poetry, and skating. b) Print the dictionaries. iii. Create a list of dictionaries to organize the following state info: a) Alaska’s capital is Juneau. Alaska’s population is 731,545. Its size is 665,000 square miles b) Hawaii’s capital is Honolulu. Hawaii’s population is 1,415,872. Its size is 10,931 square miles. c)Print the listarrow_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