How to identify triangles in Netlogo

How to find all triangles in an undirected network in Netlogo, i.e. list all instances of AB, BC, CA?

Thank,

Thomas

+3


source to share


1 answer


Here's a pretty naive approach. If your network isn't too big, it might be good enough:

to-report find-triangles
  let triangles (list)
  ask turtles [
    let t1 self
    ; find all the triangles that the turtle is a part of
    ask link-neighbors [
      let t2 self
      ask link-neighbors [
        let t3 self
        if link-with t1 != nobody [
          set triangles lput (turtle-set t1 t2 t3) triangles
        ]
      ]
    ]
  ]
  report remove-duplicates triangles
end

      

Test it with a simple network:



to setup
  clear-all
  create-turtles 4
  ask turtle 0 [
    create-link-with turtle 1
    create-link-with turtle 2
  ]
  ask turtle 1 [
    create-link-with turtle 2    
  ]
  ask turtle 3 [
    create-link-with turtle 1
    create-link-with turtle 2
  ]
  ask turtles [ set label who ]
  repeat 30 [ layout-spring turtles links 0.2 5 1 ]
  show map sort find-triangles
end

      

In the command center, the result is:

observer> setup
observer: [[(turtle 1) (turtle 2) (turtle 3)] [(turtle 0) (turtle 1) (turtle 2)]]

      

+3


source







All Articles