# Help for Counting Connected Yellow Patch Clusters in a Termite Model

**URL:** <https://forum.netlogo.org/t/help-for-counting-connected-yellow-patch-clusters-in-a-termite-model/375>\
**Category:** Q&A\
**Tags:** en\
**Created:** [October 8, 2024, 12:55am UTC](https://forum.netlogo.org/t/help-for-counting-connected-yellow-patch-clusters-in-a-termite-model/375 "2024-10-08T00:55:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tari25](https://forum.netlogo.org/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@Tari25](https://forum.netlogo.org/u/Tari25)\
**Post date:** [October 8, 2024, 12:55am UTC](https://forum.netlogo.org/t/help-for-counting-connected-yellow-patch-clusters-in-a-termite-model/375/1 "2024-10-08T00:55:42Z")

</div>

Hello everyone,

I am working on a termite model in NetLogo and I am having difficulties correctly counting the connected clusters of yellow patches. What I want to achieve is to detect how many clusters (groups of connected yellow patches) there are in the world and plot that number as the simulation progresses.

So far, my code works, but what it is counting is simply the total number of yellow patches, not the connected clusters. After doing some research, I have tried to use a flood-fill function-based approach to identify the clusters, but I still can’t get it to work as it should.

Here is the code I have developed so far:

```netlogo
turtles-own [next-task steps]

patches-own [visited?] ; To keep track of patches that have already been visited

to setup
  clear-all
  set-default-shape turtles "bug"

  ; Initialize the graph for the number of clusters
  set-current-plot "Clusters"
  set-plot-x-range 0 100
  set-plot-y-range 0 50
  create-temporary-plot-pen "Clusters"

  ask patches [
    set visited? false ; Initialize all patches as not visited
    if random-float 100 < density [set pcolor yellow]
  ]

  create-turtles number [
    set color white
    setxy random-xcor random-ycor
    set steps 0
    set next-task "search-for-chip"
  ]

  reset-ticks
end

to go
  ask turtles [
    wiggle
    ifelse steps > 0 [
      set steps steps - 1
    ] [
      if next-task = "search-for-chip" [search-for-chip]
      if next-task = "find-new-pile" [find-new-pile]
      if next-task = "put-down-chip" [put-down-chip]
      if next-task = "get-away" [get-away]
    ]
  ]

  detect-clusters ; Detect and plot clusters
  tick
end

to wiggle
  forward 1
  right random 20
  left random 20
end

to search-for-chip
  if pcolor = yellow [
    set pcolor black
    set color orange
    set steps 20
    set next-task "find-new-pile"
  ]
end

to find-new-pile
  if pcolor = yellow [
    set next-task "put-down-chip"
  ]
end

to put-down-chip
  if pcolor = black [
    set pcolor yellow
    set color white
    set steps 20
    set next-task "get-away"
  ]
end

to get-away
  if pcolor = black [
    set next-task "search-for-chip"
  ]
end

; Function to detect and count clusters of yellow patches
to detect-clusters
  ask patches [set visited? false] ; Reset the visited status for all patches
  let num-clusters 0 ; Variable to store the number of clusters

  ; Search for unvisited yellow patch clusters
  ask patches with [pcolor = yellow and not visited?] [
    ; If we find an unvisited yellow patch, flood-fill (visit) the entire cluster
    flood-fill self
    set num-clusters num-clusters + 1 ; Increment the number of clusters only once per cluster
  ]

  ; Plot the number of detected clusters
  set-current-plot-pen "Clusters"
  plot num-clusters
end

; Recursive search to flood-fill the connected cluster of yellow patches
to flood-fill [p] ; 'p' is the current patch
  ask p [
    set visited? true ; Mark this patch as visited
    ask neighbors4 with [pcolor = yellow and not visited?] [
      flood-fill self ; Recursively call to flood-fill the rest of the cluster
    ]
  ]
end

```

**Problem:**  
What I want: To detect how many clusters of connected yellow patches there are and plot that number in real time.  
What I get: Instead of counting the clusters, the model is counting the total number of yellow patches.

**Question:**  
Is there any mistake in how I am implementing the flood-fill algorithm to detect clusters? Any suggestions or improvements would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![john-chen](https://forum.netlogo.org/user_avatar/forum.netlogo.org/john-chen/32/131_2.png) [@john-chen](https://forum.netlogo.org/u/john-chen)\
**Post date:** [October 8, 2024, 2:16am UTC](https://forum.netlogo.org/t/help-for-counting-connected-yellow-patch-clusters-in-a-termite-model/375/2 "2024-10-08T02:16:42Z")

</div>

Maybe try this:

```netlogo
; Search for unvisited yellow patch clusters
ask patches with [pcolor = yellow] [
  if not visited? [
    ; If we find an unvisited yellow patch, flood-fill (visit) the entire cluster
    flood-fill self
    set num-clusters num-clusters + 1 ; Increment the number of clusters only once per cluster
  ]
]

```
