Graph implementation using STL for competitive programming | Set 2 ( Weighted graph ) | To add an edge ; Print adjacency list representaion ot graph ; Driver code
function addEdge ( adj , u , v , wt ) { adj [ u ] . push ( [ v , wt ] ) ; adj [ v ] . push ( [ u , wt ] ) ; return adj ; } function printGraph ( adj , V ) { let v = 0 , w = 0 ; for ( let u = 0 ; u < V ; u ++ ) { document . write ( " " + u + " " ) ; for ( let it = 0 ; it < adj [ u ] . length ; it ++ ) { v = adj [ u ] [ it ] [ 0 ] ; w = adj [ u ] [ it ] [ 1 ] ; document . write ( " " + v + " " + w + " " ) } } } let V = 5 ; let adj = new Array ( V ) ; for ( let i = 0 ; i < V ; i ++ ) { adj [ i ] = [ ] ; } adj = addEdge ( adj , 0 , 1 , 10 ) adj = addEdge ( adj , 0 , 4 , 20 ) adj = addEdge ( adj , 1 , 2 , 30 ) adj = addEdge ( adj , 1 , 3 , 40 ) adj = addEdge ( adj , 1 , 4 , 50 ) adj = addEdge ( adj , 2 , 3 , 60 ) adj = addEdge ( adj , 3 , 4 , 70 ) printGraph ( adj , V ) ;