python - Networkx creating a graph with node attributes to determine which edges are connected -


i trying create connected graph each node has attributes determine other nodes connected to. network circular space make easy establish links (there 1000 nodes).

the way network works node has both neighbors (the ones immediate left/right - i.e. node 3 has neighbors 1 , 2) , k long distance links. way node picks long distance links randomly picks nodes clockwise direction (i.e. node 25 might have 200 long distance link instead of 15).

here sample image of might looks like: http://i.imgur.com/pkyk5bz.png given symphony network implementation simplification of that.

i partially implemented in java(via linked list holding arraylist) lost on how in networkx. confused on how add these specific node attributes node find k long links after k not accept more links. there specific built in graph in networkx suited towards model or graph acceptable long have correct node attributes?

it's simplification of more complicated network no node leaves , no edge dissapears.

any or link example appreciated on this.

this approximates need:

import networkx nx import matplotlib.pyplot plt import random  n = 20 # number of nodes k = 3 # number of "long" edges  g = nx.cycle_graph(n)  node in g.nodes():     while len(g.neighbors(node)) < k+2:         # add k neighbors each node         # (each node has 2 neighbors cycle)         valid_target_found = false         while not valid_target_found:             # caution             # loop not terminate             # if k high relative n             target = random.randint(0,n-1)             # pick random node             if (not target in g.neighbors(node)                 , len(g.neighbors(target)) < k+2):                 # accept target if (a) not                 # connected source , (b) target                 # has less k long edges                 valid_target_found = true         g.add_edge(node, target)  nx.draw_circular(g) plt.show() 

it creates graph below. there improvements made, example, more efficient selection of target nodes long edges, gets started, hope.

cycle graph "long" edges


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

c# - Using multiple datasets in RDLC -