ThomasC
ThomasC

Reputation: 881

How to identify triangles in Netlogo

How can I find all triangles in an undirected network in Netlogo, that is, list all instances of A-B, B-C, C-A?

Thank you,

Thomas

Upvotes: 3

Views: 96

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

Here is a fairly naive approach. If your network is not too big, it could 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

Let's 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

From the command center, the result is:

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

Upvotes: 3

Related Questions