[Solved]-Python Class Graph 2 Def Init Self Nodes 3 Selfnodes Nodes 4 Selfedges 5 Selfnodes 6 Selfe Q37300422



python
class Graph:
2
def __init__(self,nodes):
3
self.nodes = nodes
4
self.edges = {}
5
for i in self.nodes:
6
self.edges[i] = []
7
def add_edge(self, pair):
8
start,end = pair
9
self.edges[start].append(end)
10
def children(self,node):
11
return self.edges[node]
12
def nodes(self):
13
return str(self.nodes)
14
def __str__(self):
15
return str(self.edges)
1
import numpy as np
2
a = np.zeros ((4,4),dtype = int)
3
a[0][1] = 1
4
a[1][2] = 1
5
a[2][3] = 1
6
print(a)
7
a = np.dot (a,a) + a
8
print(a)
9
a = np.dot (a,a) + a
10
print(a)
11
12
for i in range(0,4):
13
for j in range(0,4):
14
if not i == j:
15
print(“{0} reaches {1}: {2}”.format(i+1,j+1,bool(a[i][j])))
In class we implemented a graph class. To instantiate the graph, we had to know the nodes a priori. We didn’t have a way to mange the graph either-to delete information. We also learned how to use linear algebra to find when one node is reachable from another 1 class Graph: 2 3 def init__(self,nodes) self.nodes nodes self.edges fori in self.nodes: self.edges[i][] 6 def add edge(self, pair): start,end-pair self.edges[start].append(end) return self.edges[node] return str(self.nodes) return str(self.edges) 10 def children(self,node) def nodes(self) def-str-(self): 12 13 15 Here’s a small example to reinforce the concept. 2 Figure 4: A graph of order 4 1 import numpy as np 2 anp.zeros ((4,4),dtype int) 3 a[0]01]-1 4 a[1][2] -1 5 a[2]3]1 6 print(a) 7 anp.dot (a,a 8 print(a) 9 a np . dot (a,a) + a 10 print(a) 12 for i in range(0,4): 13 for j in range(0,4) if not i-j: print(“10 reaches 12}”.format(i+1,j+1,bool(a[i][j]))) 15 Session Output 1 C0 1 0 0] [0 01 0] [0 00 1] [0 0 0 0]] C0 1 1 0] [0 01 1] [0 00 1] [0 0 0 0]] C0 1 2 2] [0 01 2] [0 00 1] [0 0 0 0]] 1 reaches 2: True 1 reaches 3:True 1 reaches 4: True 2 reaches 1: False 2 reaches 3: True 2 reaches 4:True 3 reaches 1: False 3 reaches 2: False 3 reaches 4:True 4 reaches 1: False 4 reaches 2: False 4 reaches 3: False Change Allow the user add edges (x.y) when x is not currently in the graph. Return 1 if successful and -1 if the edge already exists. . Add a method add_node(n) that simply adds a new node. Return 1 if successful and -1 if the node already exists. Add a method del_node(n) that deletes a node and any edges associated with it. Return 1 if successulf and -1 if no node existed to delete . Add a method del_edge((x.y)) that deletes the edge. Return 1 if successful and -1 if no edge existed to delete. Add a method reachable(x.y) that returns True if there exists a path from node x to node y, and False if no path exists. Use the transitive closure operation to determine this. You can use the numpy dot method or use your own matrix multiplication function you previously wrote Put your code in a module named bestgraph.py At this point, don’t worry about the size of the graphs-they won’t be more than 20 or so nodes. For extra credit, use a table to save your graph. You should be able to open the table and create the graph as well. Show transcribed image text In class we implemented a graph class. To instantiate the graph, we had to know the nodes a priori. We didn’t have a way to mange the graph either-to delete information. We also learned how to use linear algebra to find when one node is reachable from another 1 class Graph: 2 3 def init__(self,nodes) self.nodes nodes self.edges fori in self.nodes: self.edges[i][] 6 def add edge(self, pair): start,end-pair self.edges[start].append(end) return self.edges[node] return str(self.nodes) return str(self.edges) 10 def children(self,node) def nodes(self) def-str-(self): 12 13 15 Here’s a small example to reinforce the concept. 2 Figure 4: A graph of order 4 1 import numpy as np 2 anp.zeros ((4,4),dtype int) 3 a[0]01]-1 4 a[1][2] -1 5 a[2]3]1 6 print(a) 7 anp.dot (a,a 8 print(a)
9 a np . dot (a,a) + a 10 print(a) 12 for i in range(0,4): 13 for j in range(0,4) if not i-j: print(“10 reaches 12}”.format(i+1,j+1,bool(a[i][j]))) 15 Session Output 1 C0 1 0 0] [0 01 0] [0 00 1] [0 0 0 0]] C0 1 1 0] [0 01 1] [0 00 1] [0 0 0 0]] C0 1 2 2] [0 01 2] [0 00 1] [0 0 0 0]] 1 reaches 2: True 1 reaches 3:True 1 reaches 4: True 2 reaches 1: False 2 reaches 3: True 2 reaches 4:True 3 reaches 1: False 3 reaches 2: False 3 reaches 4:True 4 reaches 1: False 4 reaches 2: False 4 reaches 3: False
Change Allow the user add edges (x.y) when x is not currently in the graph. Return 1 if successful and -1 if the edge already exists. . Add a method add_node(n) that simply adds a new node. Return 1 if successful and -1 if the node already exists. Add a method del_node(n) that deletes a node and any edges associated with it. Return 1 if successulf and -1 if no node existed to delete . Add a method del_edge((x.y)) that deletes the edge. Return 1 if successful and -1 if no edge existed to delete. Add a method reachable(x.y) that returns True if there exists a path from node x to node y, and False if no path exists. Use the transitive closure operation to determine this. You can use the numpy dot method or use your own matrix multiplication function you previously wrote Put your code in a module named bestgraph.py At this point, don’t worry about the size of the graphs-they won’t be more than 20 or so nodes. For extra credit, use a table to save your graph. You should be able to open the table and create the graph as well.
Expert Answer
Answer to python class Graph: 2 def __init__(self,nodes): 3 self.nodes = nodes 4 self.edges = {} 5 for i in self.nodes: 6 self.ed… . . .
OR

