Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could you please take a look at def max_degree_nodes in the following python code? class Graph: # Do not modify def __init__(self, with_nodes_file=None, with_edges_file=None):

Could you please take a look at "def max_degree_nodes" in the following python code?

class Graph:

# Do not modify def __init__(self, with_nodes_file=None, with_edges_file=None): """ option 1: init as an empty graph and add nodes option 2: init by specifying a path to nodes & edges files """ self.nodes = [] self.edges = [] if with_nodes_file and with_edges_file: nodes_CSV = csv.reader(open(with_nodes_file)) nodes_CSV = list(nodes_CSV)[1:] self.nodes = [(n[0],n[1]) for n in nodes_CSV]

edges_CSV = csv.reader(open(with_edges_file)) edges_CSV = list(edges_CSV)[1:] self.edges = [(e[0],e[1]) for e in edges_CSV]

def add_node(self, id: str, name: str)->None: """ add a tuple (id, name) representing a node to self.nodes if it does not already exist The graph should not contain any duplicate nodes """ if(id, name) not in self.nodes: self.nodes.append((id, name)) #return NotImplemented

def add_edge(self, source: str, target: str)->None: """ Add an edge between two nodes if it does not already exist. An edge is represented by a tuple containing two strings: e.g.: ('source', 'target'). Where 'source' is the id of the source node and 'target' is the id of the target node e.g., for two nodes with ids 'a' and 'b' respectively, add the tuple ('a', 'b') to self.edges """ edge = (source, target) edge_oppo = (target, source) if edge not in self.edges and edge_oppo not in self.edges : self.edges.append(edge) #return NotImplemented

def total_nodes(self)->int: """ Returns an integer value for the total number of nodes in the graph """ return len(self.nodes)

def total_edges(self)->int: """ Returns an integer value for the total number of edges in the graph """ return len(self.edges)

def max_degree_nodes(self)->dict: """ Return the node(s) with the highest degree Return multiple nodes in the event of a tie Format is a dict where the key is the node_id and the value is an integer for the node degree e.g. {'a': 8} or {'a': 22, 'b': 22} """ return NotImplemented

def print_nodes(self): """ No further implementation required May be used for de-bugging if necessary """ print(self.nodes)

def print_edges(self): """ No further implementation required May be used for de-bugging if necessary """ print(self.edges)

# Do not modify def write_edges_file(self, path="edges.csv")->None: """ write all edges out as .csv :param path: string :return: None """ edges_path = path edges_file = open(edges_path, 'w', encoding='utf-8')

edges_file.write("source" + "," + "target" + " ")

for e in self.edges: edges_file.write(e[0] + "," + e[1] + " ")

edges_file.close() print("finished writing edges to csv")

# Do not modify def write_nodes_file(self, path="nodes.csv")->None: """ write all nodes out as .csv :param path: string :return: None """ nodes_path = path nodes_file = open(nodes_path, 'w', encoding='utf-8')

nodes_file.write("id,name" + " ") for n in self.nodes: nodes_file.write(n[0] + "," + n[1] + " ") nodes_file.close() print("finished writing nodes to csv")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago