繁体   English   中英

在networkx图中,如何找到没有出边的节点?

[英]In a networkx graph, how can I find nodes with no outgoing edges?

我正在做一个类似于随机游走的项目,我目前正在尝试找出是否有可能,如果有,如何找出有向 networkx 图中的节点是否“悬空”,即如果它与其他节点没有边。

import collections
import networkx as nx
import numpy as np
import random as rand
from collections import Counter


def randomSurf(G, moves): #with G as a directed graph and moves as the amount of "random walks"
    #rand.seed(15) #Random seed for testing consistency
    starter = rand.choice(list(G.nodes))
    currNode = starter
    m = 0.15
    #Construct list of dangling nodes
    list_of_dangl = [] #<- this is where I'm struggling.
    list_of_nodes = [starter] #List of all visited nodes, with the starting node already in it.
    for step in range(moves-1):
        if rand.random() <= m: #If the probabilty of going to a random node is hit
            currNode = rand.choice(list(G.nodes))
            list_of_nodes.append(currNode)
        else: #If the probability of going to a random node is not hit
            neighbours = list(G.edges(currNode))
            count = 0
            for _ in G.edges(currNode):
                count +=1
            tempRandom = rand.randint(0, count-1)
            currNode = neighbours[tempRandom][1]
            list_of_nodes.append(currNode)
            
    return list_of_nodes

有没有一种方法可以检查节点是否在有向图中没有传出链接? 或者是否有其他任何人都可以推荐的方法,不包括使用 networkx pagerank 方法?

叶子的出度为零,因此:

list_of_dangl = [node for node in G.nodes if G.out_degree(node) == 0]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM