Why do we use this to deal with minimum value? Please, explain someone.
lowest_path_cost = float('inf')
Why do we use this to deal with minimum value? Please, explain someone.
lowest_path_cost = float('inf')
Let's say you have a deck of cards, and you want to find the one with the minimum value. What would you do? You probably remember the lowest card you have see so far while looking at each card. At the end that number is the minimum.
Example
min = nothing
.min = 6
.min = 6
min = 3
min = -10000000
You can code this reasoning in the following way:
deck = [6, 10, 3, -10000000, .....]
min = nothing
for card in deck:
if card < min:
min = card
print('The minimum card is', min)
This of course does not work, because 'nothing' does not exist in python. Could you write min = 0
? Not really, because when you find the 6 it's not lower than 0, so you would forget about it. The important thing is to use a number which is bigger than the first card of your deck, so that if card < min
is true and you correctly remember the first card.
But you do not know a priori (in general) what is the first card of your deck. It could be 1000000000 or -5 or 0 etc. So you must use a number that is bigger than any other number (again, this is necessary to make the first if card < min
be always true). What is bigger than any other number? Infinity!!
deck = [6, 10, 3, -10000000, ...]
min = float('inf')
for card in deck:
if card < min:
min = card
print('The minimum card is', min)