- 40 Algorithms Every Programmer Should Know
- Imran Ahmad
- 379字
- 2025-04-04 12:59:09
A practical example of pseudocode
Figure 1.3 shows the pseudocode of a resource allocation algorithm called SRPMP. In cluster computing, there are many situations where there are parallel tasks that need to be run on a set of available resources, collectively called a resource pool. This algorithm assigns tasks to a resource and creates a mapping set, called Ω. Note that the presented pseudocode captures the logic and flow of the algorithm, which is further explained in the following section:
1: BEGIN Mapping_Phase
2: Ω = { }
3: k = 1
4: FOREACH Ti∈T
5: ωi = RA(Δk,Ti)
6: add {ωi,Ti} to Ω
7: state_changeTi [STATE 0: Idle/Unmapped] → [STATE 1: Idle/Mapped]
8: k=k+1
9: IF (k>q)
10: k=1
11: ENDIF
12: END FOREACH
13: END Mapping_Phase
Let's parse this algorithm line by line:
- We start the mapping by executing the algorithm. The Ω mapping set is empty.
- The first partition is selected as the resource pool for the T1 task (see line 3 of the preceding code). Television Rating Point (TRPS) iteratively calls the Rheumatoid Arthritis (RA) algorithm for each Ti task with one of the partitions chosen as the resource pool.
- The RA algorithm returns the set of resources chosen for the Ti task, represented by ωi (see line 5 of the preceding code).
- Ti and ωi are added to the mapping set (see line 6 of the preceding code).
- The state of Ti is changed from STATE 0:Idle/Mapping to STATE 1:Idle/Mapped (see line 7 of the preceding code).
- Note that for the first iteration, k=1 and the first partition is selected. For each subsequent iteration, the value of k is increased until k>q.
- If k becomes greater than q, it is reset to 1 again (see lines 9 and 10 of the preceding code).
- This process is repeated until a mapping between all tasks and the set of resources they will use is determined and stored in a mapping set called Ω.
- Once each of the tasks is mapped to a set of the resources in the mapping phase, it is executed.