I have a list of edges l=[(0,1),(0,2),(1,3)]
likewise, and I have list of weight of edges l1=[0.23,0.45,0]
. Now I wanted to store edges in min heap manner, so that I can get access to minimum weighted edge.
Asked
Active
Viewed 199 times
-1
-
4Use [`heapq`](https://docs.python.org/3/library/heapq.html)? – trincot Sep 18 '21 at 16:58
-
do you have a custom implementation of heap? post that so we can see where you're getting at, or otherwise use heapq module – mightyandweakcoder Sep 18 '21 at 17:07
-
No i don't have implementation of heap. I am not able find how can i use `heapq`. – Jaykishan Pipaliya Sep 18 '21 at 17:24
-
@mightyandweakcoder now i got it using heapq. – Jaykishan Pipaliya Sep 18 '21 at 18:01
1 Answers
0
Now i got it, using heapq
and do heapify
.
First create list
l = [[0.23, (0, 1)], [0.45, (0, 2)], [0, (1, 3)]]
Then call
heapq.heapify(l)
Returning:
[[0, (1, 3)], [0.23, (0, 1)], [0.45, (0, 2)]]
-
1Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 18 '21 at 18:32