BiSarfraz
BiSarfraz

Reputation: 479

How to identify nodes which are adjacent to at least two members of a list?

I want to find list of nodes which are adjacent to (that is, has a link with) at least two members of a given 'generating list' of agents. For example, the generating list contains [turtle2, turtle11, turtle9, turtle10, turtle7]. If turtle13 has a link with both turtle2 and turtle9, then it should be in the returned list. But nodes that are link-neighbors with only one member of the list should not be returned.

The following code sets up the generating list (called maxDegList). But I don't have any code (or an algorithm) for finding the desired list. The network has about 14,000 nodes and 26,000 links so efficiency is a concern. However, the generating list will be small (fewer than 20 nodes).

    to setup
      ca
      crt 60 [fd random 15]

      ask turtles with [color = red] [create-links-to other turtles with [color = blue]]
      ask turtles with [color = blue] [create-links-from other turtles with [color = yellow]]

      maxdeg
    end

    to maxdeg
      let maxDegList reverse sort-on [ count my-links ] max-n-of 4 turtles [ count my-links ]
        show-inf maxDegList "Top ten turtles using max-n-of:"
      foreach maxDegList [
        ask myself [
        ask other nw:turtles-in-radius 2 [set shape "circle"]  
      ]]
    end
    to show-inf [ turtle-list maxD ]
      print maxD
      foreach turtle-list [ t -> ask t [ show count my-links ] ]
    end

Upvotes: 1

Views: 158

Answers (1)

JenB
JenB

Reputation: 17678

Okay, this is a complete program that will get you started. I have done it as agentsets rather than lists, but if you really want a list, you can do foreach instead of ask and make other changes, the logic will be the same. You would also need to make some changes if you are working with a directed network.

The bit you asked for is the common-neigbors procedure, which takes the generating agentset and a number. In your case the number is 2, but it was just as easy to write this in such a way that you could also ask for a different number of adjacent nodes.

I took advantage of the fact that the generating agentset is small, so that's where the iteration occurs. First it constructs an agentset (called adjacents) of the link-neighbors of the generating agentset, because any node that has at least 2 neighbors that are members of a set must obviously be a member of the neighbors of that set. Then it looks at each agent in the adjacents set and simply counts how many of its network neighbours are in the original generating set. If that count is high enough, then the agent is added to the agentset that is eventually reported.

extensions [nw]

to setup
  clear-all
  nw:generate-random turtles links 40 0.1 [ set color red ]
  repeat 10 [ layout-spring turtles links 0.2 15 1 ]
  let maxDegNodes max-n-of 4 turtles [ count my-links ]
  show-inf maxDegNodes "Central turtles using max-n-of:"
  print "Relevant nodes:"
  let wanted common-neighbors maxDegNodes 2
  ask wanted
  [ set color blue
    show link-neighbors
  ]
end

to-report common-neighbors [ in-turtles min-links ]
  let adjacents nobody
  ask in-turtles [ set adjacents (turtle-set adjacents link-neighbors) ]
  let out-set nobody
  let intersection nobody
  ask adjacents
  [ set intersection link-neighbors with [ member? self in-turtles ]
    if count intersection >= min-links [ set out-set (turtle-set out-set self) ]
  ]
  report out-set
end

to show-inf [ in-turtles title ]
  print title
  ask in-turtles
  [ show count my-links
    set size 2
    set color yellow
  ]
end

Upvotes: 2

Related Questions