在networkx中添加和删除随机边缘

时间:2017-03-04 02:58:39

标签: python random graph networkx

我在python中使用NetworkX。给定任何无向和未加权的图形,我想循环遍历所有节点。对于每个节点,我想添加随机边缘和/或以概率p删除该节点的现有随机边缘。有一个简单的方法吗?非常感谢!

2 个答案:

答案 0 :(得分:1)

给定一个节点i,要添加没有重复的边,你需要知道(1)i已经存在哪些边,然后计算(2)那些没有重复的候选边的集合。 t来自i。对于删除,您已在注释中定义了一个方法 - 该方法仅基于(1)。 这是一个基于列表推导提供一轮随机添加和删除的函数

def add_and_remove_edges(G, p_new_connection, p_remove_connection):    
    '''    
    for each node,    
      add a new connection to random other node, with prob p_new_connection,    
      remove a connection, with prob p_remove_connection    

    operates on G in-place    
    '''                
    new_edges = []    
    rem_edges = []    

    for node in G.nodes():    
        # find the other nodes this one is connected to    
        connected = [to for (fr, to) in G.edges(node)]    
        # and find the remainder of nodes, which are candidates for new edges   
        unconnected = [n for n in G.nodes() if not n in connected]    

        # probabilistically add a random edge    
        if len(unconnected): # only try if new edge is possible    
            if random.random() < p_new_connection:    
                new = random.choice(unconnected)    
                G.add_edge(node, new)    
                print "\tnew edge:\t {} -- {}".format(node, new)    
                new_edges.append( (node, new) )    
                # book-keeping, in case both add and remove done in same cycle  
                unconnected.remove(new)    
                connected.append(new)    

        # probabilistically remove a random edge    
        if len(connected): # only try if an edge exists to remove    
            if random.random() < p_remove_connection:    
                remove = random.choice(connected)    
                G.remove_edge(node, remove)    
                print "\tedge removed:\t {} -- {}".format(node, remove)    
                rem_edges.append( (node, remove) )    
                # book-keeping, in case lists are important later?    
                connected.remove(remove)    
                unconnected.append(remove)    
    return rem_edges, new_edges    

要查看此功能的实际效果:

import networkx as nx
import random
import matplotlib.pyplot as plt

p_new_connection = 0.1
p_remove_connection = 0.1

G = nx.karate_club_graph() # sample graph (undirected, unweighted)
# show original
plt.figure(1); plt.clf()
fig, ax = plt.subplots(2,1, num=1, sharex=True, sharey=True)
pos = nx.spring_layout(G)
nx.draw_networkx(G, pos=pos, ax=ax[0])

# now apply one round of changes
rem_edges, new_edges = add_and_remove_edges(G, p_new_connection, p_remove_connection)

# and draw new version and highlight changes
nx.draw_networkx(G, pos=pos, ax=ax[1])
nx.draw_networkx_edges(G, pos=pos, ax=ax[1], edgelist=new_edges,
                       edge_color='b', width=4)
# note: to highlight edges that were removed, add them back in;
# This is obviously just for display!
G.add_edges_from(rem_edges)
nx.draw_networkx_edges(G, pos=pos, ax=ax[1], edgelist=rem_edges,
                       edge_color='r', style='dashed', width=4)
G.remove_edges_from(rem_edges)

plt.show() 

你应该看到这样的事情。 example output

请注意,您也可以使用邻接矩阵执行类似的操作, A = nx.adjacency_matrix(G).todense()(它是一个numpy矩阵,所以像A [i,:]。nonzero()这样的操作是相关的)。如果你有非常大的网络,这可能会更有效。

答案 1 :(得分:1)

在networkx

中创建新的随机边缘

让我们设置一个测试图:

import networkx as nx
import random
import matplotlib.pyplot as plt

graph = nx.Graph()
graph.add_edges_from([(1,3), (3,5), (2,4)])
nx.draw(graph, with_labels=True)
plt.show()

Figure 1

现在我们可以从图中的非边缘列表中选择一个随机边。还不完全清楚你提到的概率是多少。由于您添加了一条评论,表明您要使用random.choice,我会坚持这一点。

def random_edge(graph, del_orig=True):
    '''
    Create a new random edge and delete one of its current edge if del_orig is True.
    :param graph: networkx graph
    :param del_orig: bool
    :return: networkx graph
    '''
    edges = list(graph.edges)
    nonedges = list(nx.non_edges(graph))

    # random edge choice
    chosen_edge = random.choice(edges)
    chosen_nonedge = random.choice([x for x in nonedges if chosen_edge[0] == x[0]])

    if del_orig:
        # delete chosen edge
        graph.remove_edge(chosen_edge[0], chosen_edge[1])
    # add new edge
    graph.add_edge(chosen_nonedge[0], chosen_nonedge[1])

    return graph

用法例子:

new_graph = random_edge(graph, del_orig=True)

nx.draw(new_graph, with_labels=True)
plt.show()

Figure 2

如果需要,我们仍然可以在random.choice的边缘添加概率分布(例如,使用numpy.random.choice())。