Figure 2:
Algorithm.