Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3.  
  4. //
  5. // pg - print all PG and PG-13 movies of a given year
  6. //
  7.  
  8. // include the common PHP code file
  9. require("a2.php");
  10.  
  11.  
  12.  
  13. // PROGRAM BODY BEGINS
  14.  
  15. $usage = "Usage: $argv[0] Year";
  16. $db = dbConnect(DB_CONNECTION);
  17.  
  18. // Check arguments
  19. if (count($argv) != 3) exit("$usage\n");
  20.  
  21. $queue = array();
  22. $visitedTable = array();
  23. $distanceTable = array();
  24. $predTable = array();
  25. $output = array();
  26.  
  27. //Getting id of the src actor
  28. $getIDQuery = "select id from Actor where %s ilike actor.name;";
  29. $r = dbQuery($db, mkSQL($getIDQuery, $argv[1]));
  30. $row = dbNext($r);
  31. $firstActorID = $row[0];
  32.  
  33. //Getting id of the dest actor
  34. $getIDQuery = "select id from Actor where %s ilike actor.name;";
  35. $r = dbQuery($db, mkSQL($getIDQuery, $argv[2]));
  36. $row = dbNext($r);
  37. $lastActorID = $row[0];
  38.  
  39. array_push($queue, $firstActorID); // Add the starting actor to the QUEUE
  40. $visitedTable[$firstActorID] = 1; // Add the starting actor to the hash table
  41. $distanceTable[$firstActorID] = 0; // Add the starting distance
  42.  
  43. //BFS
  44.  
  45. while (!empty($queue)) {
  46. $actorVertice = array_shift($queue);
  47. if ($actorVertice == $lastActorID) {
  48. echo("found it bitch!\n");
  49. // allPaths($firstActorID, $lastActorID, "", $db, $output, $distanceTable);
  50. break;
  51. } else {
  52. //get the movie of the actorVertice,
  53. //and then the actors who are in that movie
  54. $neighbourQuery = "select actor_id from Acting
  55. where movie_id in (select movie_id from Acting where actor_id = %d)
  56. and actor_id <> %d;";
  57. $r = dbQuery($db, mkSQL($neighbourQuery, $actorVertice, $actorVertice));
  58. //all actors will be stored in $r
  59. while ($row = dbNext($r)) {
  60. $neighbourActor = $row[0];
  61. if (!isset($visitedTable[$neighbourActor])) {
  62. //checking that we have not visited
  63. //thus if we haven't add to visited and predecessor
  64. $visitedTable[$neighbourActor] = 1;
  65. $predTable[$neighbourActor] = $actorVertice;
  66. $distanceTable[$neighbourActor] = $distanceTable[$actorVertice] + 1;
  67. array_push($queue, $neighbourActor);
  68. }
  69. }
  70. }
  71.  
  72. }
  73.  
  74. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement