[英]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.