Exemplo n.º 1
0
        echo $vertex['letter'];
        // output on screen
        foreach ($vertex['neighbours'] as $neighbour) {
            // Watch neighbours, which were not visited yet
            if (!$list[$neighbour]['visited']) {
                // mark vertex as visited
                $list[$neighbour]['visited'] = true;
                array_unshift($queue, $list[$neighbour]);
            }
        }
    }
}
// go through graph starting from 1st vertex
depthFirstSearch($incidenceList[1], $incidenceList);
echo "</br>";
breadthFirstSearch($incidenceList);
// Here is an example of breadth-first search (queue content step by step)
// Event       Queue
// Visit   A    A
// Visit   N    NA
// Remove  A    N
// Visit   R    RN
// Remove  N    R
// Visit   I    IR
// Visit   K    KIR
// Remove  R    KI
// Visit   L    LKI
// Remove  I    LK
// Remove  K    L
// Visit   V    VL
// Remove  L    V
Exemplo n.º 2
0
 function findPath($StringX, $StringY)
 {
     $Start = findWord($StringY);
     /*Check if the word given by the user even exist in the dictionary*/
     if ($Start == NULL) {
         print $Start;
         print " does not exist in the dictionary!";
     }
     $End = findWord($StringY);
     /*Check to see if second word given even exists in the dictionary*/
     if ($End == NULL) {
         print $End;
         print " does not exist in the dictionary!";
     }
     /* Run breadth first search on the start vertex*/
     breadthFirstSearch($Start);
     /*We will traverse backwards on the list from breadfirstSearch to find the path*/
     while ($End->pred != NULL) {
         /*If the predecessor for end is NULL means there is no neighbor for this word*/
         if ($End->pret == NULL) {
             print "No Path Found!";
         }
         /*Otherwise print out as it traverses*/
         print $end->word . " -> ";
         $End = $End->pred;
     }
     print $End->word;
 }