Graph Theory37 sections · 1633 units
Open in Course

Infinity Handling

(Avoiding overflow)

To represent infinity, use a large constant (e.g., 10910^9 or 101810^{18}). Do not use the maximum integer value (e.g., 23112^{31}-1), because adding two infinities can cause overflow. After Floyd-Warshall, if dist[i][j] is still infinity, there is no path from ii to jj.

Choose your infinity constant based on the problem constraints. If weights are at most 10610^6 and paths have at most nn edges, use infinity =109= 10^9.