本文整理汇总了Python中Graph.Graph.add_node方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.add_node方法的具体用法?Python Graph.add_node怎么用?Python Graph.add_node使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph.Graph
的用法示例。
在下文中一共展示了Graph.add_node方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: topology_to_network
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_node [as 别名]
def topology_to_network(nodes, edges):
"""(nodes, edges) -> Graph
Given a simple topology description, generate a graph network
of the computers and cable types created by the network
network_nodes is a list of computer addresses
network cables is a list of tuples of the form
(cabletype, computer1, computer2)"""
network = Graph()
nodeLookup = {}
for name in nodes:
node = Node(name)
nodeLookup[name] = node
network.add_node(node)
for cabletype, computer1, computer2 in edges:
node1 = nodeLookup[computer1]
node2 = nodeLookup[computer2]
edge = Edge(cabletype)
network.add_edge(edge, node1, node2)
return network
示例2: open
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_node [as 别名]
from Graph import Graph
from Node import Node
thaum_data = open('research.thaum')
counter = 0
primal_amount = int(thaum_data.readline())
print "Creating Primal Instances"
graph = Graph()
while counter<primal_amount:
#create Nodes
element_name = thaum_data.readline()
element = Node(element_name.strip())
graph.add_node(element)
counter += 1
# print counter
# print primal_amount
#create secondary aspects
counter = 0
constructed_amount = int(thaum_data.readline())
print "Creating Constructed Instances"
while counter<constructed_amount:
#create nodes
element_data = thaum_data.readline().strip().split(" ")
does_elem_exist = False
element_name = element_data[0]
element_builder_name_1 = element_data[1]
element_builder_name_2 = element_data[2]
# print("Current line is: "+ element_name)
for elem in graph.nodes:
if elem.name == element_name:
does_elem_exist = True
示例3: Node
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_node [as 别名]
from Graph import Graph
from Node import Node
from Edge import Edge
node1 = Node('darkstar.wi.mit.edu')
node2 = Node('fang.harvard.edu')
node3 = Node('www.wi.mit.edu')
edge1 = Edge('t3')
edge2 = Edge('t1')
network = Graph()
network.add_node(node1)
network.add_node(node2)
network.add_node(node3)
network.add_edge(edge1, node1, node2)
network.add_edge(edge2, node1, node3)
print "*"*44
print "current graph"
network.dump()
print
print "*"*44
print "now removing", edge2
network.remove_edge(edge2)
network.dump()
print "*"*44
print "now removing", node1
network.remove_node(node1)
示例4: read_connection_table
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_node [as 别名]
def read_connection_table(file):
"""file->graph"""
line = file.readline()
if not line:
return None
try:
numnodes, numedges = map(int, line.split())
except ValueError:
raise AssertionError("Can't parse header into integer values")
except IndexError:
raise AssertionError("Header has wrong number of elements")
result = Graph()
nodes = []
# read the nodes
for i in range(numnodes):
line = file.readline()
if not line:
raise AssertionError("Need %s nodes"%numnodes)
# just get the machine name
machine = line.strip()
node = Node(machine)
result.add_node(node)
nodes.append(node)
# read the edges
edges = []
for i in range(numedges):
line = file.readline()
if not line:
raise AssertionError("Need %s edges!"%numedges)
line = line.strip()
groups = line.split()
if len(groups) < 3:
raise AssertionError("Format for edge is cable node_index1 node_index2\ngot %s"%
line)
if len(groups) > 3:
raise AssertionError("Too many columns for edge")
cable, index1, index2 = groups
try:
index1 = int(index1)
except ValueError:
raise AssertionError("%s is not an integer for edge %s"%(index1, line))
try:
index2 = int(index2)
except ValueError:
raise AssertionError("%s is not an integer for edge %s"%(index2, line))
node1 = nodes[index1-1]
node2 = nodes[index2-1]
edge = Edge(cable)
result.add_edge(edge, node1, node2)
return result
示例5: __init__
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import add_node [as 别名]
class Molecules:
# constructor sets up the class
# It takes an event queue that will contain the posey assembly events, an
# initial list of molecule graphs and two hooks into the gui, one to set the
# list of elements displayed when a new posey graph is produced, and one to force
# update of the currently displayed molecule.
def __init__ (self, event_queue, isomorphism_list, set_gui_list, update_display):
# initialize various fields
# list of possible molecules
self.isomorphism_list = isomorphism_list
# a copy of the original possible molecules for backtracking on
# destroy or disconnect events
self.original_isomorphism_list = isomorphism_list[:]
# part data
self.part_library = Part_Library( hub_class=Hub,
strut_class=Strut )
# posey graph
self.iso_graph = Graph()
# information on currently connected pieces to help update the graph
self.node_dict = {}
self.socket_dict = {}
self.strut_dict = {}
#setting gui hook
self.set_gui_list = set_gui_list
self.update_display = update_display
# print initial moledcules
print "Initial possible molecules (%d):" % len(self.isomorphism_list)
self.isomorphism_list.sort(key=self.molecule_key)
self.set_gui_list(self.isomorphism_list)
for el in self.isomorphism_list:
print "%s,"% el[0],
print ""
print "\n"
#start thread to process assembly events
t = threading.Thread(target=self.event_wait,args=(event_queue,))
t.setDaemon(1)
t.start()
# We sort molecules by size
def molecule_key (self, m):
return m[1].adj_matrix.shape[0]
# Used with map: for each graph in our list, try to
# match the posey graph to it.
def map_isomorphisms( self, large ):
if self.iso_graph.adj_matrix.shape[0] == 0:
# the graph is empty
return (large[0], large[1], {})
if self.iso_graph.adj_matrix.shape[0] > large[1].adj_matrix.shape[0]:
# return if we are larger than the molecule we are testing against
return (large[0], large[1], None)
gm = GM.Graph_Matcher(large[1], self.iso_graph)
iso_map = gm.get_isomorphism()
return (large[0], large[1], iso_map)
# Used with filter to get rid of failed matches.
def filter_isomorphisms( self, triple ):
return triple[2] is not None
# wait for events, update the iso graph
def event_wait (self, event_queue):
while 1:
self.update_isomorphisms(event_queue.get())
# process assembly events; update graph; update list of molecules
def update_isomorphisms( self, event ):
#create event
if event["type"] == "create":
#find out what node this is
gn = Graph_Node(self.part_library[event["hub"]].label)
print "Received create event: %s(%d)." % (gn.label, gn.unique)
# add it to dictionary of nodes in our graph
self.node_dict[event["hub"]] = gn
# add it to the graph
self.iso_graph.add_node(gn)
#destroy
elif event["type"] == "destroy":
# find out what node it is
try:
gn = self.node_dict[event["hub"]]
print "Received destroy event: %s(%d)" % (gn.label, gn.unique)
# remove node from graph
self.iso_graph.remove_node(gn)
# remove node from dictionary
del self.node_dict[event["hub"]]
# replace molecule list with original list
# This requires some explanation. Since we filter out unmatching graphs,
# our list of matching molecules gets smaller monotonically. When we
# need to make our list larger on destroy or disconnect, we refilter the
# whole list.
#.........这里部分代码省略.........