RIck C 130
RIck C 130

Reputation: 125

Heapify python method in a list of tuples

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?

Upvotes: 10

Views: 10789

Answers (1)

Yakov Dan
Yakov Dan

Reputation: 3347

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

Upvotes: 16

Related Questions