Area 2.2 : SPFA
Example :
Figure 4.1 : SPFA
SPFA(Shortest Path Faster Alogrithm)
is an algorithm sloving SSSP(single source shortst path) problem.
For the algorithm, we need an array and a queue.
For example, we need to find the distance between 1 and other points
Step 1 : push node 1 into queue, and set dist[1] = 0
0 | inf | inf | inf | inf |
queue q :
1 | – | – |
Table 4.1-a : SPFA(a)
Next, pop the node 1, and relax all node v that (1, v) is in E.
If the node v is relaxed, push v into q.
0 | inf | 8.44 | inf | inf |
queue q :
3 | – | – |
Table 4.1-b : SPFA(b)
Repeat again :
0 | inf | 8.44 | 14.38 | 13.72 |
queue q :
4 | 5 | – |
Table 4.1-c : SPFA(c)
Node 4 has no edges to relax, so we only pop the element.
5 | – | – |
Table 4.1-d : SPFA(d)
Node 3 can relax (5, 2), so push node 2 into q.
0 | 15.98 | 8.44 | 14.38 | 13.72 |
queue q :
2 | – | – |
Table 4.1-e : SPFA(e)
Node 2 has also no edges to relax, so we only pop the element.
– | – | – |
Table 4.1-f : SPFA(f)
Now, we got all the sp from 1 to any node.
This is SPFA.
Given Score :
Difficulty : 2
x Basic Score : 49
Score : 264
[Area 2.3 Unlocked.]
All Figures in the passage :
http://59.60.22.18:2500/wordpress/wp-content/uploads/2024/01/C02_Figures.zip