Graph implementation using STL for competitive programming | Set 2 ( Weighted graph ) | To add an edge ; Print adjacency list representaion ot graph ; Driver code
def addEdge ( adj , u , v , wt ) : NEW_LINE INDENT adj [ u ] . append ( [ v , wt ] ) NEW_LINE adj [ v ] . append ( [ u , wt ] ) NEW_LINE return adj NEW_LINE DEDENT def printGraph ( adj , V ) : NEW_LINE INDENT v , w = 0 , 0 NEW_LINE for u in range ( V ) : NEW_LINE INDENT print ( " Node " , u , " makes ▁ an ▁ edge ▁ with " ) NEW_LINE for it in adj [ u ] : NEW_LINE INDENT v = it [ 0 ] NEW_LINE w = it [ 1 ] NEW_LINE print ( " TABSYMBOL Node " , v , " with ▁ edge ▁ weight ▁ = " , w ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT V = 5 NEW_LINE adj = [ [ ] for i in range ( V ) ] NEW_LINE adj = addEdge ( adj , 0 , 1 , 10 ) NEW_LINE adj = addEdge ( adj , 0 , 4 , 20 ) NEW_LINE adj = addEdge ( adj , 1 , 2 , 30 ) NEW_LINE adj = addEdge ( adj , 1 , 3 , 40 ) NEW_LINE adj = addEdge ( adj , 1 , 4 , 50 ) NEW_LINE adj = addEdge ( adj , 2 , 3 , 60 ) NEW_LINE adj = addEdge ( adj , 3 , 4 , 70 ) NEW_LINE printGraph ( adj , V ) NEW_LINE DEDENT