Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- <?php
- //
- // pg - print all PG and PG-13 movies of a given year
- //
- // include the common PHP code file
- require("a2.php");
- // PROGRAM BODY BEGINS
- $usage = "Usage: $argv[0] Year";
- $db = dbConnect(DB_CONNECTION);
- // Check arguments
- if (count($argv) != 3) exit("$usage\n");
- $queue = array();
- $visitedTable = array();
- $distanceTable = array();
- $predTable = array();
- $output = array();
- //Getting id of the src actor
- $getIDQuery = "select id from Actor where %s ilike actor.name;";
- $r = dbQuery($db, mkSQL($getIDQuery, $argv[1]));
- $row = dbNext($r);
- $firstActorID = $row[0];
- //Getting id of the dest actor
- $getIDQuery = "select id from Actor where %s ilike actor.name;";
- $r = dbQuery($db, mkSQL($getIDQuery, $argv[2]));
- $row = dbNext($r);
- $lastActorID = $row[0];
- array_push($queue, $firstActorID); // Add the starting actor to the QUEUE
- $visitedTable[$firstActorID] = 1; // Add the starting actor to the hash table
- $distanceTable[$firstActorID] = 0; // Add the starting distance
- //BFS
- while (!empty($queue)) {
- $actorVertice = array_shift($queue);
- if ($actorVertice == $lastActorID) {
- echo("found it bitch!\n");
- // allPaths($firstActorID, $lastActorID, "", $db, $output, $distanceTable);
- break;
- } else {
- //get the movie of the actorVertice,
- //and then the actors who are in that movie
- $neighbourQuery = "select actor_id from Acting
- where movie_id in (select movie_id from Acting where actor_id = %d)
- and actor_id <> %d;";
- $r = dbQuery($db, mkSQL($neighbourQuery, $actorVertice, $actorVertice));
- //all actors will be stored in $r
- while ($row = dbNext($r)) {
- $neighbourActor = $row[0];
- if (!isset($visitedTable[$neighbourActor])) {
- //checking that we have not visited
- //thus if we haven't add to visited and predecessor
- $visitedTable[$neighbourActor] = 1;
- $predTable[$neighbourActor] = $actorVertice;
- $distanceTable[$neighbourActor] = $distanceTable[$actorVertice] + 1;
- array_push($queue, $neighbourActor);
- }
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement