0

Given groups of pairs in each level of the structure below, I want to find if there's a "loop" of connecting points. (if "b" on level 0 has a match on "a" on the next level).

I'm using a recursive function to test it and it's working fine. But when I test the return value it fails:

The code:

        $tracker=array();
        array_push($tracker, 0);

        if (findloop(1, $candidates[0]['pairs'][0]['b'])){
            echo "<h1>a path has been found</h1><pre>";
                print_r($tracker);
            echo "</pre>";
        }else{
            echo "<h3>no loop found</h3><pre>";
                print_r($tracker);
            echo "</pre>";
        }

The function:

function findloop($level, $value){  // so far.. only progression...
    echo "<p>level $level, $value</p>";
    global $candidates;
    global $tracker;
    foreach($candidates[$level]['pairs'] as $key=>$pair){
        if($pair['a']==$value){
            array_push($tracker, $key);
            if($level==sizeof($candidates)-1){ 
                echo "omgggggg";
                return true; 
            }else{
                findloop($level+1, $pair['b']);
            }
        }
    }

}

The result:

level 1, 19
level 2, 15
level 3, 18
omgggggg
no loop found    < - - - - - - - function does (?) return true but it fails

Array
(
    [0] => 0
    [1] => 1
    [2] => 0
    [3] => 1
)

The structure:

Array
(
    [0] => Array
        (
            [angle] => 41.7
            [pairs] => Array
                (
                    [0] => Array
                        (
                            [a] => 6
                            [b] => 19
                        )
                    [1] => Array
                        (
                            [a] => 19
                            [b] => 6
                        )
                )
        )
    [1] => Array
        (
            [angle] => 11.8
            [pairs] => Array
                (
                    [0] => Array
                        (
                            [a] => 15
                            [b] => 19
                        )
                    [1] => Array
                        (
                            [a] => 19
                            [b] => 15
                        )
                )
        )
    [2] => Array
        (
            [angle] => 14.3
            [pairs] => Array
                (
                    [0] => Array
                        (
                            [a] => 15
                            [b] => 18
                        )
                    [1] => Array
                        (
                            [a] => 16
                            [b] => 17
                        )
                    [2] => Array
                        (
                            [a] => 17
                            [b] => 16
                        )
                    [3] => Array
                        (
                            [a] => 18
                            [b] => 15
                        )
                )
        )
    [3] => Array
        (
            [angle] => 29.5
            [pairs] => Array
                (
                    [0] => Array
                        (
                            [a] => 6
                            [b] => 18
                        )
                    [1] => Array
                        (
                            [a] => 18
                            [b] => 6
                        )
                )
        )
)
Paulo Bueno
  • 2,499
  • 6
  • 42
  • 68

1 Answers1

1

The initial return is fine but the calls further up the chain aren't getting this result. In your else you need to return the result:

    if($level==sizeof($candidates)-1){ 
        echo "omgggggg";
        return true; 
    }else{
        return findloop($level+1, $pair['b']);
    }
Jim
  • 22,354
  • 6
  • 52
  • 80