Python noob here.
I am trying to create a network object in Python that contains both node and edge objects. Edges are constructed from two nodes. Networks are constructed from lists of nodes and edges.
My problem arises where the network sometimes doesn't realise that an edge is connected to a specific node due to the implicit ordering of the nodes within the edge construction, thus giving the appearance that some nodes are 'isolated' when they shouldn't be.
I have 3 classes:
class Node:
def __init__(self,key):
self.key = key
class Edge:
def __init__(self, node1, node2):
self.p1 = node1
self.p2 = node2
class Network:
def __init__(self, nodes = [], edges = []):
self.nodes = nodes
self.edges = edges
def maximal_subnetwork(self, node):
nodes = {node}
traced = set()
while nodes:
node = nodes.pop()
traced.add(node)
for i in self.adjacent_nodes(node): # returns adhacent nodes
if i not in traced:
nodes.add(i)
traced = list(traced)
return Network(nodes = traced , edges = self.return_edges(*traced)) # returns the subset of edges in the network
Inevitably, due to how I constructed the classes, the latter 'finds' maximal subnetworks that depend entirely on where the edge originates.
I could try changing the classes, however, I want to keep it general enough that I can add properties such as directed edges and multiedges.
How could I make it such that an edge is indifferent to the nodes, without compromising the ability to add a direction to it or a duplicate?