Note that there are some explanatory texts on larger screens.

plurals
  1. POdelete random edges of a node till degree = 1 networkx
    primarykey
    data
    text
    <p>If I create a bipartite graph G using random geomtric graph where nodes are connected within a radius. I then want to make sure all nodes have a particular degree (i.e. only one or two edges). My main aim is to take one of the node sets (i.e node type a) and for each node make sure it has a maximum degree set by me. So for instance if a take node i that has a degree of 4, delete random edges of node i until its degree is 1. </p> <p>I wrote the following code to run in the graph generator after generating edges. It deletes edges but not until all nodes have the degree of 1.</p> <pre><code>for n in G: mu = du['G.degree(n)'] while mu &gt; 1: G.remove_edge(u,v) if mu &lt;=1: break return G </code></pre> <p>full function below:</p> <pre><code>import networkx as nx import random def my_bipartite_geom_graph(a, b, radius, dim): G=nx.Graph() G.add_nodes_from(range(a+b)) for n in range(a): G.node[n]['pos']=[random.random() for i in range(0,dim)] G.node[n]['type'] = 'A' for n in range(a, a+b): G.node[n]['pos']=[random.random() for i in range(0,dim)] G.node[n]['type'] = 'B' nodesa = [(node, data) for node, data in G.nodes(data=True) if data['type'] == 'A'] nodesb = [(node, data) for node, data in G.nodes(data=True) if data['type'] == 'B'] while nodesa: u,du = nodesa.pop() pu = du['pos'] for v,dv in nodesb: pv = dv['pos'] d = sum(((a-b)**2 for a,b in zip(pu,pv))) if d &lt;= radius**2: G.add_edge(u,v) for n in nodesa: mu = du['G.degree(n)'] while mu &gt; 1: G.remove_edge(u,v) if mu &lt;=1: break return G </code></pre> <p>Reply to words like jared. I tried using you code plus a couple changes I had to make:</p> <pre><code>def hamiltPath(graph): maxDegree = 2 remaining = graph.nodes() newGraph = nx.Graph() while len(remaining) &gt; 0: node = remaining.pop() neighbors = [n for n in graph.neighbors(node) if n in remaining] if len(neighbors) &gt; 0: neighbor = neighbors[0] newGraph.add_edge(node, neighbor) if len(newGraph.neighbors(neighbor)) &gt;= maxDegree: remaining.remove(neighbor) return newGraph </code></pre> <p>This ends up removing nodes from the final graph which I had hoped it would not.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload