Seeking for smallest(minimum) and second smallest value of set of 2D functions

37 Views Asked by At

I have an algorithm in which I generate $$f^i(x,y)$$ in a loop running for n times. I want to calculate the smallest($f_{min1}$) and the second smallest ($f_{min2}$) values of all $f^i$s in all $(x, y)$ points. A brute-force way to do so is too keep all the $f^i$s and calculate the desired functions ($f_{min1}$) and ($f_{min2}$) having all the values for all $f^i$s. However, this method is unfortunately very time and memory consuming (saving all values and sorting all of the functions at all of the points and ...).

I am seeking for a way to do this iteratively. i.e. that I want to calculate the $f^j_{min1}$ and $f^j_{min2}$ by having the values of functions $f^j$, $f_{min1}^{j-1}$ and $f_{min2}^{j-1}$ . ($j$ is the iterator counter and $f^j_{min1}$ is the value of the minimum considering $f^i$ for $i = 0$ to $j$)

This approach for calculating $f_{min1}^{j}$ is feasible using:

$$ f_{min1}^j = \min(f_{min1}^{j-1}, f^j) \ \ \ \ \ \ \ (*) $$

However, I could not find such a formulation for updating $f_{min2}^j$. I would really appreciate it if someone can help find a way to calculate $f_{min2}$ similar to (*) without the need of keeping all the $f^i$s in the memory. thanks

2

There are 2 best solutions below

1
On

You don’t need $j$ on $f_{\min 1}$ and $f_{\min 2}$. The $=\min()$ update is just shorthand for an IF-THEN-ELSE expression in which you compare the current $f^j$ to the incumbent $f_{\min 1}$ and replace $f_{\min 1}$ if $f^j$ is better. To generalize to $f_{\min 2}$, use a nested IF in which the outer IF compares to $f_{\min 2}$, and the inner IF compares to $f_{\min 1}$ and replaces one or both.

4
On

I think I have got the answer now. So in my iterative scheme, I might have one of these three situations for my $f^j(x,y)$

  1. $f^j(x,y) >= f_{min2}(x, y)$

    -in this case neither $f_{min1}(x, y)$ nor $f_{min2}(x, y)$ updates.

  2. $f_{min1}(x, y) < f^j(x,y) < f_{min2}(x, y)$

    -in this case $f_{min2}(x, y)$ gets replaced by $f^j$

  3. $f^j(x,y) <= f_{min1}(x, y)$

    -in this case $f_{min2}(x, y)$ gets replaced by $f_{min1}(x, y)$ and $f_{min1}(x, y)$ gets replaced by $f^j$