# 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
4\(B\leftarrow\) Caching capacity of mining machine
5\(id\leftarrow\) Block ID
6\(N\leftarrow\) Maximum number of blocks
7\(C[id,b]\leftarrow\) Value of cell for table with \(id\)th block and \(b\) allocation
8\(b_{id}\leftarrow\) block size of \(id\)th block
9\(X[id,0]=0\)
10\(X[id,0]=0\)
11 // Initially, the first column is set to 0 value
12\(X[id,b]=0\)
13 // Initially, the first column is set to 0 value
14 end for
29 for\(b=0\) to \(B\)do
20\(X[id,b]=0\)
21\(X[id,b]=X[id-1,b]\)
22 end for
30
31 end for
32
33 end for
344
356 end for
```

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

The LBM has the information for caching the blocks of the mining machines and fills the cache of each mining machine by applying the \(0/1\) knapsack algorithm in such a way that a node with the minimum cache is filled up to a certain capacity. The LBM scrutinizes the data blocks before distributing them to the mining machines when the number of data blocks is greater than the caching capacity of the mining machines. It is assumed that the LBM already knows the number of blocks of data placed in the fog mining nodes' cache on a runtime basis. Suppose that there are three mining machines in a cluster, such as \(M_{a}\), \(M_{b}\), and \(M_{c}\), with the current caching capacity to store data blocks at any time instant \(T\), being \(D_{a}\), \(D_{b}\), and \(D_{c}\), respectively. If the available caching capacity of \(D_{a}\), \(D_{b}\), and \(D_{c}\) varies from the highest to the lowest, respectively, then the \(0/1\) knapsack allows the LBM to allocate those data blocks to mining machine \(M_{a}\), which has the maximum value. The caching capacity of the mining node \(D_{sack}\) must be greater than the difference between the most available cache capacities, i.e., \(D_{a}\) and \(D_{b}\), and less than the difference between the two extreme cache capacities of the mining machines, i.e., \(D_{a}\) and \(D_{c}\), as follows:

\[Opt_{2}\geq maxB_{j}\quad 1\leq j\leq N\]2

The \(0/1\) knapsack allocates data blocks to the mining machines by considering two constraints:

1. Fill the mining cache with data blocks, provided their sizes do not exceed the cache limit of the mining machine. If there are \(N\) nodes selected with varying block sizes \(B\), such that \[Opt_{2}\geq maxB_{j}\quad 1\leq j\leq N\]3
2. The value of all the selected data blocks must be maximum. \[Opt_{2}\geq maxB_{j}\quad 1\leq j\leq N\]4

The knapsack problem is resolved using a knapsack table. Suppose there are five nonce-requesting data blocks \(A\), \(B\), \(C\), \(D\), and \(E\), with block sizes of 1, 2, 4, 4, and 5 MB, respectively, and \(D_{sack}\) is 7 MB. The values calculated against each of these \(A\), \(B\), \(C\), \(D\), and \(E\) blocks is 200, 300, 100, 400, and 300, respectively. This means 16 MB are required to cache these data blocks to a mining machine and only 7 MBs are available. As the requested data block sizes are more than the available caching limit, the data blocks need to be scrutinized with optimal values. The knapsack table filled utilizing the above-mentioned example is shown in \[Opt_{2}\geq maxB_{j}\quad 1\leq j\leq N\]5

The table shows that the knapsack algorithm optimally selects data blocks \(A\), \(B\), and \(D\) in this example, with the maximum value calculated as 1600. \begin{table}
\begin{tabular}{c c c} \hline \hline  & **Data Blocks Allocation on Mining Machines** & **Knapsack Problem** \\ \hline \(D_{sack}\) & Caching capacity of mining machine & Carrying capacity of knapsack \\ \hline \(N\) & Data blocks to be allocated & Items to be packed \\ \hline \(B_{j}\) & Size of \(j\)th data block & Weight of an item \\ \hline \(V_{j}\) & Value of \(j\)th data block & Value of an item \\ \hline \hline \end{tabular}
\end{table} 
Table 2: Mapping of the data block allocation problem to a knapsack problem. ## 5 Results and Analysis

We implemented the proposed \(ESM_{HCS}\) technique in MATLAB and evaluated its performance.