2011-08-11 126 views
2

在一个阵列算法递归虽然这听起来像一个数学/ CS的问题,我相信有人在那里将能帮助我与此有关。帮助与PHP

我有两个表,similarityTable和物品。项目表包含以下数据:

itemID itemName 
------ ----- 
    1 A 
    2 B 
    3 C 
    4 D 
    5 E 

和similarityTable:

item1 item2 
----- ----- 
    1 2 
    1 3 
    2 1 
    2 3 
    3 1 
    3 4 
    4 1 
    4 2 

根据上述内容,可以看出,ITEM1 ID = 1的相似,ID 2,3的ITEM2。 ID = 2的Item1类似于ID 1,3的item2。这使得项目1也类似于3.现在,ID的物品1 = 3类似于4。这意味着ITEM1 ID1的类似于1,2,3,4而不是5

我试图做这个想法的一个算法。完整的代码发布如下。 它不起作用。任何人都有足够的灰色物质来解决这个问题?

<?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(); 
?> 
+0

简单的想法(可能不是很有效):善待你的相似性表的条目为图表中的边缘,并使用弗洛伊德 - 沃肖尔计算顶点之间的最短距离。通过这种方式,您可以轻松看到“正在类似”的传递性关闭。 –

+0

你想要什么样的输出? – netcoder

回答

1

数据如下:

$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), 
); 

如果要检查什么项目每个项目类似,不需要递归,简单地做:

$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; 
} 

这会给你与每个键是项目ID的数组,一个数组:

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 
     ) 
) 
+0

非常感谢你的努力。碰巧,你的提议并没有完全解决问题。然而,你的代码给了我一个很好的提示,如何着手解决这个问题。我使用了列表函数,事实证明它非常有用。我发布了我的代码,以便它对其他人有用。非常感谢! – shailenTJ