# Efficient Resource Allocation in Blockchain-Assisted Health Care Systems
## 4 Proposed Technique
### Allocation of Data Blocks to Mining Machines
#### 4.2.1 Algorithm for Load Balancing
```
1\(b\leftarrow\) Current block size
2\(B\leftarrow\) Caching capacity of mining machine
3\(id\leftarrow\) Block ID
4\(N\leftarrow\) Maximum number of blocks
5\(C[id,b]\leftarrow\) Value of cell for table with \(id\)th block and \(b\) allocation
6\(b_{id}\leftarrow\) block size of \(id\)th block
7 Knapsack table filling process:
8 for\(id=1\) to \(N\)do
9\(X[id,0]=0\)
10 // Initially, the first column is set to 0 value
11 end for
12 for\(b=0\) to \(B\)do
13\(X[0,b]=0\)
14 // Initially, the first row is set to 0 value
15
16 end for
17 for\(id=1\) to \(N\)do
18 for\(b=0\) to \(B\)do
19 If\(b_{id}\leq b\) If\(b_{id}+X[id-1,b-b_{id}]>X[id-1,b]\)
20\(X[id,b]=b_{id}+X[id-1,b-b_{id}]\)
21 else
22\(X[id,b]=X[id-1,b]\)
23
24 EndIf
25
26 end for
27 Initialize id and w:
28\(N\gets id\)
29\(Y\gets m\)
30 end for
31 Data Selection for task execution:
32 while\(id>1\) and \(b>1\)do
33 If\(B[id,b]>B[id-1,b]\)
34\(id\)th block is allocated in optimal solution
35\(id=id-1\)
36\(b=b-b_{id}\)
37 Else
38\(id=id-1\)
39 EndIf
40 end
```

**Algorithm 2**Knapsack-based selection of data blocks for computing

#### 4.2.2 Knapsack Optimization Algorithm

The knapsack algorithm (0/1 type) can be used to fill a sack with items that are of the highest value, such that the total capacity of the sack is exceeded. In this work, the scrutinized problem was formulated as a knapsack problem, as we needed to allocate the most valuable data blocks to the mining machines, within their caching capacity. The mapping of our problem as a 0/1 knapsack problem is shown in Table 2 and its algorithm is given in Algorithm 2. ```
1\(b\leftarrow\) Current block size
2\(B\leftarrow\) Caching capacity of mining machine
3\(id\leftarrow\) Block ID
4\(N\leftarrow\) Maximum number of blocks
5\(C[id,b]\leftarrow\) Value of cell for table with \(id\)th block and \(b\) allocation
6\(b_{id}\leftarrow\) block size of \(id\)th block
7 Knapsack table filling process:
8 for\(id=1\) to \(N\)do
9\(X[id,0]=0\)
10 // Initially, the first column is set to 0 value
11 end for
12 for\(b=0\) to \(B\)do
13\(X[0,b]=0\)
14 // Initially, the first row is set to 0 value
15 end for
16 for\(id=1\) to \(N\)do
17 for\(b=0\) to \(B\)do
18 if\(b_{id}\leq b\) If\(b_{id}+X[id-1,b-b_{id}]>X[id-1,b]\)
19\(X[id,b]=b_{id}+X[id-1,b-b_{id}]\)
20 Else
21\(X[id,b]=X[id-1,b]\)
22
23 Else
24 Else
25\(X[id,b]=X[id-1,b]\)
26
27 EndIf
28
29 end for
30 Initialize id and w:
31\(N\gets id\)
32\(Y\gets m\)
33
34 end for
35 Data Selection for task execution:
36 while\(id>1\) and \(b>1\)do
37 If\(B[id,b]>B[id-1,b]\)
38\(id\)th block is allocated in optimal solution
39\(id=id-1\)
40\(b=b-b_{id}\)
41
42 Else
43\(id=id-1\)
44
45 EndIf
46
47 end for
48
49 end for
50
510 end for
```

**Algorithm 3**Knapsack-based selection of data blocks for computing

#### 4.2.3 Knapsack Optimization Algorithm

The knapsack algorithm (0/1 type) can be used to fill a sack with items that are of the highest value, such that the total capacity of the sack is exceeded. In this work, the scrutinized problem was formulated as a knapsack problem, as we needed to allocate the most valuable data blocks to the mining machines, within their caching capacity. The mapping of our problem as a 0/1 knapsack problem is shown in Table 2 and its algorithm is given in Algorithm 2.