## 4 Algorithm Overview
### Algorithm Overview  
The Hierarchical Stackelberg Fairness Algorithm (Algorithm 1) implements a Stackelberg-based fairness mechanism for traffic management involving emergency vehicles (EVs) and regular vehicles (RVs). This algorithm is designed to dynamically balance the needs of both types of vehicles using a hierarchical decision-making approach. The hierarchical Stackelberg algorithm iteratively maximizes the utility functions of the TMC, EVs, and RVs, balancing competing objectives through weight adjustments. ```
1:Initialize road network, traffic signals, vehicle positions
2:Set initial utility function weights (alpha, beta, gamma, delta, lambda, mu)
3:while traffic is active do
4: Input real-time traffic data
5: Update road network state
6: // Upper-Level Decision: TMC Optimization
7: Calculate expected travel times for EVs and RVs
8: Determine route priorities for minimizing EV travel time
9: Optimize traffic signals for \"green wave\" effect
10: Output updated signal timings and priorities
11: // Lower-Level Decision: EV and RV Strategy Optimization
12:for each EV do
13: Evaluate route options based on updated signal timings
14: Choose a route that minimizes travel time and maximizes priority
15: Output optimal route choices to TMC
16:endfor
17:for each RV do
18: Adjust route and speed based on EV decisions and signal states
19: Choose a route that minimizes travel time and avoids conflicts
20: Output adjusted routes and speeds
21:endfor
22: // Evaluate Fairness
23: Calculate Gini coefficient for travel times
24: Calculate skewness for travel time distribution
25:if fairness constraints are not met then
26: Adjust signal timings and route priorities
27: Continue optimization loop
28:else
29: Break
30:endif
31:endwhile
```  
**Algorithm 1** Hierarchical Stackelberg Fairness Algorithm  
A Stackelberg-based fairness mechanism is implemented using a hierarchical decision-making framework, dynamically prioritizing EVs while maintaining fair and efficient traffic flow for RVs. Iterative optimization ensures that fairness metrics, such as the Ginicoefficient and skewness, are kept within acceptable limits, which enables all road users' needs to be met. The Gini coefficient is calculated by combining the average travel times of EVs (emergency vehicles) and RVs (regular vehicles) into one dataset and using a similar approach to calculate waiting times. Let T\({}_{EV}\) be the EV travel time and T\({}_{RV}\) be the RV travel time. In the next step, these values are sorted ascending. The cumulative proportion of travel and waiting times is calculated for each value X\({}_{i}\) in the sorted data. Using Formula (4), we can calculate the Gini coefficient:  
\[G=\frac{\sum_{i=1}^{n}\sum_{j=1}^{n}\left|X_{i}-X_{j}\right|}{2n^{2}\bar{X}} \tag{4}\]  
where X\({}_{i}\) and X\({}_{j}\) are individual travel or waiting times, n is the total number of data points (both EVs and RVs), and \(\bar{X}\) is the mean of the travel or waiting times. The Gini coefficient, which ranges from 0 (perfect equality) to 1 (maximum inequality), measures the disparity between the two groups. In the context of traffic management, it indicates how fairly resources, such as signal timing, are distributed between emergency and regular vehicles. Calculating Skewness  
The skewness of a distribution helps determine whether the distribution of travel times or waiting times is symmetrical, positively skewed (indicating a longer tail on the right), or negatively skewed (indicating a longer tail on the left). To calculate skewness, the first step is to calculate the mean, where X\({}_{i}\) represents individual travel times, and n is the number of values. As a next step, the standard deviation is calculated to determine the degree of dispersion in the data.