# Using an Artificial _Physarum polycephalum_ Colony for Threshold Image Segmentation
## 4 Algorithm Steps of APPCA
### Step 2: Expansion
```
1: For \(Ite=1\):\(Ite\_max\)
2: //expansion algorithm
3: For hyphae \(k=1\):\((2+p_{j})MS\)
4: For hyphae \(s=1\):\((2+p_{j})MS\)-1
5: If fitness (s) \(>\) fitness(s + 1)
6: temp = fitness (s)
7: fitness (\(s\)) = fitness(\(s+1\))
8: fitness(\(s+1\)) = temp
9: End if
10: End for
11: End for
12: To store the optimal solution Temp_s = 1
13: To store the optimal fitness Temp_fitness = fitness(1)
14: To calculate the iterative error e=\(|\) fitness(1)(\(Ite\))- fitness(1)(\(Ite\)-1)\(|\)
15: To judge the error with end condition: \(T_{max}\), \(Ite\_max\), \(e_{th}\)
16: If the end condition does not fit
17: Select the top \(S\) best hyphae
18: Return to the expansion algorithm
19: Else
20: Exit
21: End for
22: Exit: output the optimal segmented image \(x^{1*}(Ite)\)
23: Exit: output the optimal fitness \(fitness(x^{1*}(Ite))\)
```
**Algorithm 3** Contraction algorithm with a triple-loop

As we can see from Algorithm 3, there is a triple-loop and a bubble sort algorithm, so the time complexity of the contraction algorithm is \(O(\left(2+p_{f}\right)^{2}M^{2}S^{2})\), the best time complexity is \(O(\left(2+p_{f}\right)MS)\), and the space complexity is \(O(MS)\). In the triple-loop of contraction, the APPC has \(M\) of _Physarum polycephalum_ and each has \(S\) hyphae. The APPC will rank the fitness of all hyphae using the bubble sort algorithm and select the top \(S\) best hyphae. If the end condition is fit, such as the error threshold \(e_{th}\), the maximum iterations \(Ite\_max\), and the maximum simulation time \(T_{max}\), the optimal solution(rank No.1) and the corresponding fitness (fitness of No.1) will be output. Otherwise, the top \(S\) best solutions will be selected from all solutions and returned to the expansion algorithm. The APPC contraction algorithm seems to have good sorting capabilities, but the computation is still too time-consuming. Similarly, the contraction algorithm with a triple-loop in Algorithm 3 can also be improved. Then, the quick sort is introduced here to replace the bubble sort in Algorithm 3, improving the algorithm's time performance and search efficiency. The improved contraction of APPC is shown in Algorithm 4. The problem scale and the _Physarum polycephalum_ colony are the same as the Algorithm 3, where there are a total of \(HV\) pixels and the maximum number \(S\) of hyphae. The maximum number \(M\) of _Physarum polycephalum_ is ignored. ```
1: For \(Ite=1\):\(Ite\_max\)
2://expansion algorithm
3: For hyphae \(k=1\):(\(2+p_{f}\)) \(S\)
4: QuickSort(s, 1, (\(2+p_{f}\)) \(S\))
5: To store the optimal solution Temp_\(s=1\)
6: To store the optimal fitness Temp_fitness = fitness(1)
7: End for
8: To calculate the iterative error e= | fitness(1)(\(Ite\))- fitness(1)(\(Ite\)-1) |
9: To judge the error with end condition: \(T_{max}\), \(Ite\_max\), \(e_{th}\)
10: If the end condition is not fit
11: Select the top \(S\) best hyphae
12: Return to the expansion algorithm
13: Else
14: Exit
15: End for
16: Exit: output the optimal segmented image \(x^{1*}(Ite)\)
17: Exit: output the optimal fitness \(fitness(x^{1*}(Ite))\)
```

**Algorithm 4** Contraction algorithm with a dual-loop

Compared with the contraction algorithm with a triple-loop in Algorithm 3, the improved contraction algorithm in Algorithm 4 employs the quick sort with better performance than the bubble sort, so the time complexity of the contraction algorithm is improved as \(O((2+p_{f})S\log_{2}[(2+p_{f})S])\), the best time performance is also \(O(\log_{2}[(2+p_{f})S)]\), and the space complexity is \(O((2+p_{f})S)\). In the dual-loop of contraction, the improved algorithm no longer distinguishes the source of hyphae, the time performance has been greatly improved, and the spatial performance is stable. The improved APPC contraction algorithm has higher computational efficiency. ### Step 4: End Judgment

The end conditions of the proposed APPC approach can be judged by a predefined error threshold \(e_{th}\) or the number of iterations \(Ite\_max\).