2011-08-11 125 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