# Energy-Efficient Distributed Edge Computing to Assist Dense Internet of Things
## 6 Optimized Resource Allocation Algorithm  
This section introduces the proposed resource allocation algorithm for managing tasks using MEC and fog computing in the proposed system structure. The algorithm aims to optimize resource utilization, minimize latency, and reduce energy consumption. Algorithm 1 provides the pseudo-code of the proposed scheme. The considered algorithm starts by initializing an allocation matrix A for all nodes that can process computing tasks. This matrix is introduced to keep track of task allocations. All values in A are set to zero initially, indicating that no tasks are allocated. The algorithm iterates over all possible fog nodes and MEC servers for each IoT device. It evaluates all potential fog nodes and MEC servers for task allocation for each IoT device. Each node is assessed based on its computational capacity and the suitability of its resources. Then, the algorithm checks whether the node computational capacity is sufficient to handle the allocated task. If sufficient, it calculates the cost of assigning the task to the node. For each node-task pairing, the algorithm computes the cost of assignment, which is defined as a weighted combination of latency and energy consumption. The cost function ensures a balanced trade-off between minimizing delays and energy use. The cost is a latency and energy consumption combination weighted by factors \(\alpha\) and \(\beta\), respectively. The algorithm then keeps track of the node that offers the minimum cost for each task. The algorithm identifies the node with the minimum cost for each task and updates the allocation matrix A. This ensures that tasks are distributed optimally across the network. The process iterates until all tasks are assigned, while conforming to the constraints indicated in the energy model. ```
1: Initialize allocation matrix A = {0} for all S E MS, f E FN, MEC E MECN
2: For each S in {MS}, do
3: Initialize Min\({}_{\text{cost}}\) = \(\infty\)
4: Initialize Optimal\({}_{\text{node}}\) = None
5: Initialize position of each node \(\text{x}_{\text{i}}\) (i = 1, 2, 3, \(\ldots\), n)
6: For each (f in {FN} & MEC in {M}) do
7: If (C\({}_{\text{v}}\)\(\geq\) C\({}_{\text{i}}\)), then
8: Calculate latency cost for handling task T\({}_{\text{Cost-i}}\) = \(\alpha^{\star}\) T\({}_{\text{i}}\) (T\({}_{\text{i}}\) = Processing time if the task is handled
9: at node i (T\({}_{\text{proc-i}}\)) + Communication time for sending tasks to node i (T\({}_{\text{comm-i}}\)))
10: Calculate energy cost E\({}_{\text{cost-i}}\) = \(\beta^{\star}\) E\({}_{\text{T-i}}\)
11: Calculate the total cost Total\({}_{\text{Cost-i}}\) = T\({}_{\text{Cost-i}}\) + E\({}_{\text{Cost-i}}\)
12: If (Total\({}_{\text{Cost-i}}\) < Min\({}_{\text{Cost}}\)), then
13: Min\({}_{\text{cost}}\) = Total\({}_{\text{Cost-i}}\)
14: Optimal\({}_{\text{node}}\) = v
15: End If
16: Else
17: Calculate latency cost for forwarding packets T\({}_{\text{Cost-i}}\) = T\({}_{\text{frw-i}}\) (T\({}_{\text{frw-i}}\) is the time required to communicate with the edge node to take the offloading decision)
18: Calculate energy cost (for sending requests and taking offloading decisions) E\({}_{\text{cost-i}}\) = \(\beta^{\star}\) E\({}_{\text{T-i}}\)
19: Calculate the total cost Total\({}_{\text{Cost-i}}\) = T\({}_{\text{Cost-i}}\) + E\({}_{\text{Cost-i}}\)
20: End For
21: If (Optimal\({}_{\text{node}}\)\(\
eq\) None), thenThe proposed optimization models demonstrate the following:

* The system effectively distributes computational and communication demands by applying energy consumption constraints on nodes and links, extending network lifetime and lowering operational expenses. * Tasks are allocated to nodes with sufficient computational capacity and minimal communication delays, ensuring that the latency constraint is met. This guarantees a QoS-compliant network environment. * The algorithm ensures a balanced distribution of tasks across fog nodes and MEC servers, avoiding bottlenecks and overloading, which could otherwise degrade system performance.