Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heapify python method in a list of tuples

Tags:

python-3.x

I'm tryng to figure out how I can use heapify function in a list of tuples. i want it for djikstra algorithm so my list will look like this.

[(node,distance)(node2,distance2)....(node-n,distance-n]

I just wanna know how heapify can order my list just checking the distance and not the node in the tuple.

As I know, if i have a list'l' like:

[8,4,7,2]

and I use

heapify(l)

It will return:

[2,4,7,8]

So how I can use this function only ordering by distance?

like image 937
RIck C 130 Avatar asked Mar 27 '26 13:03

RIck C 130


1 Answers

Heapify will work with lists of tuples such that the first element of each tuple is the value, so use (distance, node) instead

like image 159
Yakov Dan Avatar answered Mar 30 '26 10:03

Yakov Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!