为网络中最大的弱连接组件着色

Coloring the largest weakly connected component in a network

我基本上是在尝试为最大的弱连接组件着色。 nw:weak-component-clusters 正在返回网络中存在的所有组件的列表。我只想给最大的一个上色。我的代码为所有组件着色。

 let clusters nw:weak-component-clusters 
 print length(clusters)
 show clusters
 show sort (clusters)
 foreach clusters [ 
   set color pink
 ]

您可以使用 sort-by 按大小降序对您的集群列表进行排序,然后让第一个更改其颜色:

extensions [nw]

to setup
  clear-all
  create-turtles 100 [
    set color blue
    create-links-with n-of random 3 other turtles
  ]
  repeat 30 [ layout-spring turtles links 0.2 5 1 ]
  let clusters nw:weak-component-clusters 
  ; sort by descending size of cluster:
  let sorted-clusters sort-by [ [c1 c2] -> count c1 > count c2 ] clusters
  ask first sorted-clusters [ set color pink ]  
end