0

Imagine that you have a set of nodes (1 2 3) and that these nodes are connected through arcs (1,2), (1,3) and (2,3). Together representing a network.

How can I create a subset of nodes, containing all neighboring nodes? i.e. I wan't the following subset to be something like:

NeighborNode
1 2 3
2 1 3
3 1 2 

This Python code is far off, but maybe you get the idea:

def NNode_rule(model,i):
for i in model.Nodes:
    model.NNodes[i].add(model.ToNode[i]) if model.Nodes[i]==model.FromNode[i]
model.NNodes = Set(model.Nodes, initialize=NNode_rule)
Martin K
  • 1
  • 1

1 Answers1

0

Do you know what Object-oriented programming is?

I think the easiest solution is to create a simple class (e.g. Node) that has an attribute neighbors which is a list of other nodes.

You also need a method that adds an edge between two nodes, something like this:

def add_edge(self, other_node):
    self.neighbors.append(other_node)
    other_node.neighbors.append(self)

Then every Node holds the information which neighbors it has.

Hope this helps ;)

Felix
  • 6,131
  • 4
  • 24
  • 44