本文整理汇总了Python中networkx.DiGraph.in_degree方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.in_degree方法的具体用法?Python DiGraph.in_degree怎么用?Python DiGraph.in_degree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.DiGraph
的用法示例。
在下文中一共展示了DiGraph.in_degree方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: contract_chains
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import in_degree [as 别名]
def contract_chains(g:nx.DiGraph, blockname='block'):
'''Contract chains with indegree=outdegree=1:
i.e. turns > - - - <
into [>---<]
The label of the chain is the label of its first element.
g.node[n][blockname] will hold list of dictionaries.
'''
from itertools import chain
starts = set(chain((n for n in g if g.in_degree(n) != 1),
chain.from_iterable(g.successors_iter(n) for n in g
if g.out_degree(n) > 1)))
blocks = nx.DiGraph()
for label in starts:
n = label
block = []
while True:
block.append(g.node[n])
if g.out_degree(n) != 1:
break
next_n = next(g.successors_iter(n))
if g.in_degree(next_n) != 1:
break
n = next_n
if label not in blocks:
blocks.add_node(label)
blocks.add_edges_from((label, suc) for suc in g.successors_iter(n))
blocks.node[label][blockname] = block
return blocks
示例2: filter_nodes
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import in_degree [as 别名]
def filter_nodes(self,thresh):
nodes=DiGraph.nodes(self)
for node in nodes:
if DiGraph.in_degree(self,node)<thresh:
DiGraph.remove_node(self,node)
nodes=DiGraph.nodes(self)
for node in nodes:
if DiGraph.in_degree(self,node)==0 and DiGraph.out_degree(self,node)==0:
DiGraph.remove_node(self,node)
示例3: filter_edges
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import in_degree [as 别名]
def filter_edges(self,thresh):
edges=DiGraph.edges(self,data='weight')
for edge in edges:
if edge[2]<thresh:
DiGraph.remove_edge(self,edge[0],edge[1])
if DiGraph.in_degree(self,edge[0])==0 and DiGraph.out_degree(self,edge[0])==0:
DiGraph.remove_node(self,edge[0])
if DiGraph.in_degree(self,edge[1])==0 and DiGraph.out_degree(self,edge[1])==0:
DiGraph.remove_node(self,edge[1])
示例4: splitG
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import in_degree [as 别名]
def splitG(self,minN):
contcmp=[]
ct_cliq=[]
cncp=list(strongly_connected_components(self))
for item in cncp:
#print(len(item))
if len(item)<=minN and len(item)>1:
#print("topic")
tmp=set()
tmp_cliq=[]
for each in item:
tmp=tmp.union(set(self.node[each]["keywords"]))
tmp_cliq.append(each)
DiGraph.remove_node(self,each)
contcmp.append(tmp)
ct_cliq.append(tmp_cliq)
if len(item)==1:
DiGraph.remove_node(self,list(item)[0])
nodes=DiGraph.nodes(self)
for node in nodes:
if DiGraph.in_degree(self,node)==0 and DiGraph.out_degree(self,node)==0:
DiGraph.remove_node(self,node)
return contcmp,ct_cliq
示例5: deterministic_topological_ordering
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import in_degree [as 别名]
def deterministic_topological_ordering(nodes, links, start_node):
"""
Topological sort that is deterministic because it sorts (alphabetically)
candidates to check
"""
graph = DiGraph()
graph.add_nodes_from(nodes)
for link in links:
graph.add_edge(*link)
if not is_directed_acyclic_graph(graph):
raise NetworkXUnfeasible
task_names = sorted(graph.successors(start_node))
task_set = set(task_names)
graph.remove_node(start_node)
result = [start_node]
while task_names:
for name in task_names:
if graph.in_degree(name) == 0:
result.append(name)
# it is OK to modify task_names because we break out
# of loop below
task_names.remove(name)
new_successors = [t for t in graph.successors(name)
if t not in task_set]
task_names.extend(new_successors)
task_names.sort()
task_set.update(set(new_successors))
graph.remove_node(name)
break
return result
示例6: is_in_1_out_1
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import in_degree [as 别名]
def is_in_1_out_1(graph: nx.DiGraph, node: object) -> bool:
in_degree = graph.in_degree(node)
out_degree = graph.out_degree(node)
return in_degree == 1 and out_degree == 1
示例7: in_out_balance
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import in_degree [as 别名]
def in_out_balance(graph: nx.DiGraph, node: object):
return graph.in_degree(node) - graph.out_degree(node)
示例8: GraphManager
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import in_degree [as 别名]
class GraphManager(object):
""" Generates and processes the graph based on packets
"""
def __init__(self, packets, layer=3, geo_ip=os.path.expanduser('~/GeoIP.dat')):
self.graph = DiGraph()
self.layer = layer
self.geo_ip = None
self.data = {}
try:
self.geo_ip = GeoIP(geo_ip)
except:
logging.warning("could not load GeoIP data")
if self.layer == 2:
edges = map(self._layer_2_edge, packets)
elif self.layer == 3:
edges = map(self._layer_3_edge, packets)
elif self.layer == 4:
edges = map(self._layer_4_edge, packets)
else:
raise ValueError("Other layers than 2,3 and 4 are not supported yet!")
for src, dst, packet in filter(lambda x: not (x is None), edges):
if src in self.graph and dst in self.graph[src]:
self.graph[src][dst]['packets'].append(packet)
else:
self.graph.add_edge(src, dst, {'packets': [packet]})
for node in self.graph.nodes():
self._retrieve_node_info(node)
for src, dst in self.graph.edges():
self._retrieve_edge_info(src, dst)
def get_in_degree(self, print_stdout=True):
unsorted_degrees = self.graph.in_degree()
return self._sorted_results(unsorted_degrees, print_stdout)
def get_out_degree(self, print_stdout=True):
unsorted_degrees = self.graph.out_degree()
return self._sorted_results(unsorted_degrees, print_stdout)
@staticmethod
def _sorted_results(unsorted_degrees, print_stdout):
sorted_degrees = OrderedDict(sorted(unsorted_degrees.items(), key=lambda t: t[1], reverse=True))
for i in sorted_degrees:
if print_stdout:
print(sorted_degrees[i], i)
return sorted_degrees
def _retrieve_node_info(self, node):
self.data[node] = {}
if self.layer >= 3 and self.geo_ip:
if self.layer == 3:
self.data[node]['ip'] = node
elif self.layer == 4:
self.data[node]['ip'] = node.split(':')[0]
node_ip = self.data[node]['ip']
country = self.geo_ip.country_name_by_addr(node_ip)
self.data[node]['country'] = country if country else 'private'
#TODO layer 2 info?
def _retrieve_edge_info(self, src, dst):
edge = self.graph[src][dst]
if edge:
packets = edge['packets']
edge['layers'] = set(list(itertools.chain(*[set(GraphManager.get_layers(p)) for p in packets])))
edge['transmitted'] = sum(len(p) for p in packets)
edge['connections'] = len(packets)
@staticmethod
def get_layers(packet):
return list(GraphManager.expand(packet))
@staticmethod
def expand(x):
yield x.name
while x.payload:
x = x.payload
yield x.name
@staticmethod
def _layer_2_edge(packet):
return packet[0].src, packet[0].dst, packet
@staticmethod
def _layer_3_edge(packet):
if packet.haslayer(IP):
return packet[1].src, packet[1].dst, packet
@staticmethod
def _layer_4_edge(packet):
if any(map(lambda p: packet.haslayer(p), [TCP, UDP])):
src = packet[1].src
dst = packet[1].dst
_ = packet[2]
return "%s:%i" % (src, _.sport), "%s:%i" % (dst, _.dport), packet
#.........这里部分代码省略.........