public double [ ] dijkstra ( int s , boolean weighted ) { double [ ] wt = new double [ n ] ; Arrays . fill ( wt , Double . POSITIVE_INFINITY ) ; PriorityQueue queue = new PriorityQueue ( wt ) ; for ( int v = 0 ; v < n ; v ++ ) { queue . insert ( v ) ; } wt [ s ] = 0.0 ; queue . lower ( s ) ; while ( ! queue . empty ( ) ) { int v = queue . poll ( ) ; if ( ! Double . isInfinite ( wt [ v ] ) ) { for ( int w = 0 ; w < n ; w ++ ) { if ( graph [ v ] [ w ] != 0.0 ) { double p = weighted ? wt [ v ] + graph [ v ] [ w ] : wt [ v ] + 1 ; if ( p < wt [ w ] ) { wt [ w ] = p ; queue . lower ( w ) ; } } } } } return wt ; }
Calculates the shortest path by Dijkstra algorithm .