Get all links connecting turtles in a set of turtles effectively in NetLogo

Given the set of turtles nodes , I want to find all links having both ends in nodes . The best I've come up with so far:

 link-set [ my-links with [ member? other-end nodes ] ] of nodes 

This is exactly what I want. However, since member? works in linear time for non-generated sets, this is O(n*n*d) (where d is the maximum degree of nodes and n is the number of nodes). I could improve it using a hash set, but that would require either abusing the table extension, searching for a third-party extension, or creating my own. I could also create a sorted list of who nodes numbers and then do a binary search on it to go to O(n * log(n) * d) , but I hope for a simpler answer. Any ideas?

+5
source share
1 answer

Here is a wacky idea:

 links-own [ i ] to-report links-between [ nodes ] ask links [ set i 0 ] ask nodes [ ask my-links [ set ii + 1 ] ] report links with [ i = 2 ] end 

This is not the most elegant thing in the world, but it is quite simple and, in my opinion, only O (n * d).

+5
source

Source: https://habr.com/ru/post/1200446/


All Articles