check for negative - weight cycles . The above step guarantees shortest distances if graph doesn 't contain  negative weight cycle. If we get a  shorter path, then there is a cycle.
for ( $ i = 0 ; $ i < $ E ; $ i ++ ) { $ x = $ graph [ $ i ] [ 0 ] ; $ y = $ graph [ $ i ] [ 1 ] ; $ weight = $ graph [ $ i ] [ 2 ] ; if ( $ dis [ $ x ] != PHP_INT_MAX && $ dis [ $ x ] + $ weight < $ dis [ $ y ] ) echo " Graph ▁ contains ▁ negative ▁ weight ▁ cycle ▁ STRNEWLINE " ; } echo " Vertex ▁ Distance ▁ from ▁ Source ▁ STRNEWLINE " ; for ( $ i = 0 ; $ i < $ V ; $ i ++ ) echo $ i , " TABSYMBOL TABSYMBOL " , $ dis [ $ i ] , " STRNEWLINE " ; }