This is a Java applet. Make sure you have Java enabled in your browser. If the model does not start, download the NetLogo desktop application and the model file: RAndPrefAttachment.nlogo. Then open the model file from within the NetLogo application.


This is a model of network growth. Nodes appear one by one, and when they arrive, they add edges to existing nodes.

Most typically this could be an academic paper citation network, or a network of court cases (that is, the edges are only added from the very newest node to the nodes that were there before).


The model starts with (m + 1) nodes, which are all connected to each other. m is the number of edges each new node comes in with.

Then nodes are added one by one, and connect to previously added nodes.


Pressing the GO ONCE button adds one new node. To continuously add nodes, press GO.

The LAYOUT? switch controls whether or not a spring layout is applied to the network.

The PLOT? switch turns off the plots which speeds up the model.

The RESIZE-NODES button will make all of the nodes take on a size representative of their degree distribution. If you press it again the nodes will return to equal size.

If you want the model to run faster, you can turn off the LAYOUT? and PLOT? switches and/or freeze the view (using the on/off button in the control strip over the view). The LAYOUT? switch has the greatest effect on the speed of the model.

If you have LAYOUT? switched off, and then want the network to have a more appealing layout, press the REDO-LAYOUT button which will run the layout-step procedure until you press the button again. You can press REDO-LAYOUT at any time even if you had LAYOUT? switched on and it will try to make the network easier to see.


The networks that result from running this model are often called “scale-free” or “power law” networks. These are networks in which the distribution of the number of connections of each node is not a normal distribution – instead it follows what is a called a power law distribution. Power law distributions are different from normal distributions in that they do not have a peak at the average, and they are more likely to contain extreme values (see Barabasi 2002 for a further description of the frequency and significance of scale-free networks). Barabasi originally described this mechanism for creating networks, but there are other mechanisms of creating scale-free networks and so the networks created by the mechanism implemented in this model are referred to as Barabasi scale-free networks.

You can see the degree distribution of the network in this model by looking at the plots. The top plot is a histogram of the degree of each node. The bottom plot shows the same data, but both axes are on a logarithmic scale. When degree distribution follows a power law, it appears as a straight line on the log-log plot. One simple way to think about power laws is that if there is one node with a degree distribution of 1000, then there will be ten nodes with a degree distribution of 100, and 100 nodes with a degree distribution of 10.


Let the model run a little while. How many nodes are “hubs”, that is, have many connections? How many have only a few? Does some low degree node ever become a hub? How often?

Turn off the LAYOUT? switch and freeze the view to speed up the model, then allow a large network to form. What is the shape of the histogram in the top plot? What do you see in log-log plot? Notice that the log-log plot is only a straight line for a limited range of values. Why is this? Does the degree to which the log-log plot resembles a straight line grow as you add more node to the network?


To refer to this model in academic publications, please use: Wilensky, U. (2005). NetLogo Preferential Attachment model. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.

In other publications, please use: Copyright 2005 Uri Wilensky. All rights reserved. See for terms of use.


globals [
    new-node  ;; the last node we created
    degrees   ;; this is an array that contains each node in
              ;; proportion to its degree

to setup
  set degrees []   ;; initialize the array to be empty

  set-default-shape turtles "circle"

  ; initially create a fully-connected clique of (m+1) nodes
  crt (m + 1)
  ask turtles [
    repeat m [set degrees lput self degrees] ; insert nodes into array
    ask other turtles with [not link-neighbor? myself] [
      create-link-with myself
  repeat 3 [layout]

;;; Main Procedures ;;;

to go
  ;; new edge is green, old edges are gray
  ask links [ set color gray ]
  let partner nobody

  crt 1
    set new-node self ;; set the new-node global
    repeat m [
      ifelse (random-float 1 < prob-pref) ; if pref attachment
        set partner new-node
        while [partner = new-node] [
          set partner one-of (degrees) 
          ask partner [
            if link-neighbor? self [  ; we can't link to someone we already share an edge with
              set partner new-node    ; so set to self so we redo
     [  ; if not pref attachment
     set partner one-of turtles with [(not link-neighbor? self) and (self != myself)]
    move-to partner
       fd 1
    create-link-with partner [set color green]
      set degrees lput new-node degrees
      set degrees lput partner degrees
  if layout? [ repeat 3 [layout] 
  if plot? [ do-plotting ]


;; connects the two nodes
to make-edge [node1 node2]
  ask node1 [
    ifelse (node1 = node2) 
      show "error: self-loop attempted"
      create-link-with node2 [ set color green ]
     ;; position the new node near its partner
       move-to new-node
       fd 8
      set degrees lput node1 degrees
      set degrees lput node2 degrees

;;; Plotting ;;;

to do-plotting ;; plotting procedure
  let max-degree max [count link-neighbors] of turtles

  set-current-plot "Degree Distribution"
  plot-pen-reset  ;; erase what we plotted before
  set-plot-x-range 1 (max-degree + 1)  ;; + 1 to make room for the width of the last bar
  histogram [count link-neighbors] of turtles

  ;; for this plot, the axes are logarithmic, so we can't
  ;; use "histogram-from"; we have to plot the points
  ;; ourselves one at a time
  set-current-plot "Degree Distribution (log-log)"
  plot-pen-reset  ;; erase what we plotted before
  ;; the way we create the network there is never a zero degree node,
  ;; so start plotting at degree one
  let degree 1
  while [degree <= max-degree]
    let matches turtles with [count link-neighbors = degree]
    if any? matches
      [ plotxy log degree 10
               log (count matches) 10 ]
    set degree degree + 1

;;; Layout ;;;

to resize-nodes
    ;; modified from function written by Uri Wilensky
    ;; node size is proportional to degree 
    ;; (smallest size of 1 is assigned to nodes with m neighbors)
    ask turtles [ set size sqrt (count link-neighbors - m + 1)]


to layout
  ;; modified from function written by Uri Wilensky
  ;; the number 3 here is arbitrary; more repetitions slows down the
  ;; model, but too few gives poor layouts
  repeat 3 [
    ;; the more turtles we have to fit into the same amount of space,
    ;; the smaller the inputs to layout-spring we'll need to use
    let factor sqrt (count turtles / m)
    ;; numbers here are arbitrarily chosen for pleasing appearance
    layout-spring turtles links  (0.5 / m) (m * 100 / factor) (2 / factor)
    display  ;; for smooth animation