当前位置: 首页>>代码示例>>Python>>正文


Python Graph.has_edge方法代码示例

本文整理汇总了Python中networkx.Graph.has_edge方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.has_edge方法的具体用法?Python Graph.has_edge怎么用?Python Graph.has_edge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在networkx.Graph的用法示例。


在下文中一共展示了Graph.has_edge方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: edmondskarp

# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import has_edge [as 别名]
def edmondskarp(G: nx.Graph, s, t):

    RG = G.copy()

    for u,v in G.edges_iter():
        G.edge[u][v]['flow'] = 0

    path = isthereapath(RG,s,t)
    while len(path) > 0:
        path_cp = min([RG.edge[u][v]['capacity'] for u,v in path])
        for u,v in path:
            if G.has_edge(u,v):
                G.edge[u][v]['flow'] += path_cp
                RG.edge[u][v]['capacity'] -= path_cp
                if RG.edge[u][v]['capacity'] == 0:
                    RG.remove_edge(u,v)

                if RG.has_edge(v,u):
                    RG.edge[v][u]['capacity'] += path_cp
                else:
                    RG.add_edge(v,u,capacity=path_cp)
            else:
                # then this edge is a "cancelling" flow
                # residue should go up and cancelling "capacity" should go down
                G.edge[v][u]['flow'] -= path_cp
                RG.edge[v][u]['capacity'] += path_cp
                RG.edge[u][v]['capacity'] -= path_cp
                if RG.edge[u][v]['capacity'] == 0:
                    RG.remove_edge(u,v)
        path = isthereapath(RG,s,t)

    return RG
开发者ID:makslevental,项目名称:clrs,代码行数:34,代码来源:flownetworks.py

示例2: recalculate_edges

# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import has_edge [as 别名]
 def recalculate_edges(self, nodes=[]):
     """ Recalculate edges for given nodes or for all self.nodes().
     Edge between nodes n1 and n2 are added if both are
     ChannelType.in_comm_range of each other"""
     if(not nodes):
         nodes = self.nodes()
     for n1 in nodes:
         for n2 in self.nodes():
             if (n1 != n2):
                 if (self.channelType.in_comm_range(self, n1, n2)):
                     Graph.add_edge(self, n1, n2)
                 elif (Graph.has_edge(self, n1, n2)):
                     Graph.remove_edge(self, n1, n2)
开发者ID:engalex,项目名称:pymote,代码行数:15,代码来源:network.py

示例3: LangGraph

# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import has_edge [as 别名]
class LangGraph(object):
	"""
	A graph of all the relationships in a document and/or sentence
	"""
	def __init__(self, directed=False):
		"""
		Builds a graph out of the given document
		"""
		self.isDirected = directed

		#a graph that is meant to be full of class Instance
		if self.isDirected:
			self.graph = DiGraph()
		else:
			self.graph = Graph()
		
		self.start = None #an Instance

		#keep the graph also according to temporal, redundant probably needs
		#refactoring
		self.temporal = None 
		self.temporalMap = None

	def setStart(self, start):
		"""
		Sets the starting instance, also builds the temporal ordering
		of the graph
		"""
		self.start = start
		self.temporal = self.narrativeOrder()
		self.temporalMap = self.narrativeMapping()
			
	def indexToInst(self, index):
		"""
		Returns the instance corresponding to the given index
		"""
		result = index

		#if the index is an int, lookup the instance associated with it
		if type(index) == int:
			result = self.temporal[index]

		return result

	def instToIndex(self, instance):
		"""
		Return the index associated with the instance
		"""
		return self.temporalMap[instance]

	def narrativeOrder(self):
		"""
		Returns the instances in narrative order
		"""
		results = []
		node = self.start
		prev = None

		#while there are more nodes, keep adding them
		while node is not None:
			#record the current node
			results.append(node)
			
			#get the connected nodes
			fringe = [n for n in self.adj(node, WORD_EDGE) if n != prev]
			nextNode = fringe[0] if fringe else None

			#advance to the next node
			prev = node
			node = nextNode

		return results

	def narrativeMapping(self):
		"""
		Makes the mapping from instances to their narrative index
		"""
		return {inst:i for i,inst in enumerate(self.temporal)}

	def addNode(self, node):
		"""
		Adds a node to the graph
		"""
		self.graph.add_node(node)

	def addEdge(self, start, end, type):
		"""
		Adds an edge between the two instances
		"""
		#if the edge exists, just add the type
		if self.graph.has_edge(start, end):
			self.addType(start, end, type)
		else:
			self.graph.add_edge(start, end, TYPES=set([type]))

	def removeEdge(self, start, end, edgeType):
		"""
		Removes an edge with a given type from the edge type
		"""
		#remove the type
#.........这里部分代码省略.........
开发者ID:jworr,项目名称:ml_tools,代码行数:103,代码来源:docgraph.py


注:本文中的networkx.Graph.has_edge方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。