php - Pseudo-Apriori Algorithm

454

I have an array of objects with an ID attribute, and I need to link them together in a table.

I would like to insert the object IDs linked together into a 2-column MySQL table.

The table in question has 2 columns: A, and B. I want to link together the collection of objects without any overlap.

I am referring to it as pseudo-apriori because it resembles the candidate-generation process of the apriori algorithm.

For the example below, I have a five objects with an ID value of 1-5. So for example:$obj1->id == 1, and so on.

Example table:

Input IDs:
{1, 2, 3, 4, 5}

Output DB Table:
515

Answer

- | A | B |
364

Answer

- | 1 | 2 | | 1 | 3 | | 1 | 4 | | 1 | 5 | | 2 | 3 | | 2 | 4 | | 2 | 5 | | 3 | 4 | | 3 | 5 | | 4 | 5 |
388

Answer

-

Solution:

Really don't know what you're trying to do. But if the result must be as the table example, then you can do it with the following loop:

$arr = array(1, 2, 3, 4, 5);

for($i = 0; $i < count($arr); $i++) {
    for($j = $i+1; $j < count($arr); $j++) {
        // add db logic here
        $q = 'insert into table (' . $arr[$i] . ', ' . $arr[$j] . ')';
    }
}

Here is an jsFiddle example for the output: JsFiddle

552

Answer

Solution:

E.g. the pear package Math_Combinatorics can do this.

<?php
require 'Math/Combinatorics.php';
$c = new Math_Combinatorics;
foreach( $c->combinations(array(1,2,3,4,5), 2) as $k ) {
    echo join(', ', $k), "\n";  
}

prints

1, 2
1, 3
1, 4
1, 5
2, 3
2, 4
2, 5
3, 4
3, 5
4, 5
385

Answer

Solution:

please visit this address: Apriori Algorithm in PHP

Methods: setMaxScan(int), setMinSup(int), setMinConf(int), setDelimiter(string), getMinSup(void), getMinConf(void), getMaxScan(void), getDelimiter(void), process(string or array), printFreqItemsets(void), getFreqItemsets(void), printAssociationRules(void), getAssociationRules(void), saveFreqItemsets(string), saveAssociationRules(string)

People are also looking for solutions to the problem: php - how to form values in to database

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.