Help with recursion on one array algorithm in PHP
Although this sounds like a math/CS question, I am sure someone out there will be able to help me out with this.
I have two tables, similarityTable and items. The items table contains the following data:
itemID itemName
------ -----
1 A
2 B
3 C
4 D
5 E
and the similarityTable:
item1 item2
----- -----
1 2
1 3
2 1
2 开发者_如何学编程 3
3 1
3 4
4 1
4 2
From the above, it is seen that item1 of ID=1 is similar to item2 of ID 2,3. Item1 of ID=2 is similar to item2 of ID 1,3. That makes Item1 also similar to 3. Now, item1 of ID=3 is similar to 4. This means that item1 of ID1 is similar to 1,2,3,4 but not to 5.
I have attempted to do an algorithm on this idea. Full code posted below. It does not work. Anybody has enough grey matter to solve this?
<?php
$server = 'localhost:3306';
$username = 'root';
$password = '';
$databasename = "test";
mysql_connect($server, $username, $password) or die('Error connecting to MySQL');
mysql_select_db($databasename);
function getSimilarities ($inddex, $prepared_stack1) //this function returns the array of item2 given Item1
{
$link = mysqli_connect('localhost', 'root', '', 'test');
/* check connection */
if (! $link) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = mysqli_prepare($link,
"SELECT
items.itemName
FROM similarityTable
INNER JOIN items ON similarityTable.item2 = items.itemID
WHERE item1 = ?");
mysqli_stmt_bind_param($stmt, 'i', $inddex);
mysqli_stmt_execute($stmt);
$rows = array();
$row = new stdClass();
mysqli_stmt_bind_result($stmt, $row);
while (mysqli_stmt_fetch($stmt)) {
$rows[] = $row; //contains array of what we want
}
printArray($rows);
return X($rows, $prepared_stack1);
}
function X ($stack, $prepared_stack1) //This is my recursive function
{
if (empty($stack)) {}else{
$i = $stack[0];
echo $i;
$a = array_pop($stack);
if (in_array($i, $prepared_stack1)) {
echo "smthg";
} else {
array_push($prepared_stack1, $i);
X(getSimilarities($i), $prepared_stack1);
}
}
return $prepared_stack1;
}
function printArray($array){
foreach ($array as $value) {
$new1[] = $value;
}
$query = "(" . implode(",", $new1) . ")";
echo "<b>" . $query . "</b>";
}
/////////////////////////////
$prepared_stack = array();
$myArray = getSimilarities(1, $prepared_stack);
mysql_close();
?>
With the following data:
$data = array(
array(1, 2), // item1 = 1, item2 = 2
array(1, 3), // item1 = 1, item2 = 3
array(2, 1), // etc.
array(2, 3),
array(3, 1),
array(3, 4),
array(4, 1),
array(4, 2),
);
If you want to check what items each item is similar to, no need for recursion, simply do:
$similarity = array();
foreach ($data as $item) {
$id = $item[0];
if (isset($similarity[$id])) continue;
$array = array();
foreach ($data as $sim) {
list($item1, $item2) = $sim;
if ($item1 == $id) $current = $item2;
else if ($item2 == $id) $current = $item1;
else continue;
if (!in_array($current, $array)) $array[] = $current;
}
$similarity[$id] = $array;
}
This will give you an array with each key being an item ID, with an array:
Array
(
// item 1 is similar to 2,3,4
[1] => Array
(
[0] => 2
[1] => 3
[2] => 4
)
// item 2 is similar to 1,3,4
[2] => Array
(
[0] => 1
[1] => 3
[2] => 4
)
// and so on...
[3] => Array
(
[0] => 1
[1] => 2
[2] => 4
)
// etc.
[4] => Array
(
[0] => 3
[1] => 1
[2] => 2
)
)
精彩评论