Click here to Skip to main content
15,890,897 members
Home / Discussions / Algorithms
   

Algorithms

 
AnswerRe: Square root of positive integer using binary search. Pin
Patrice T7-Apr-18 21:01
mvePatrice T7-Apr-18 21:01 
Question3D Points to 2D Points Pin
Member 1368222216-Feb-18 15:34
Member 1368222216-Feb-18 15:34 
AnswerRe: 3D Points to 2D Points Pin
Ralf Meier17-Feb-18 0:35
mveRalf Meier17-Feb-18 0:35 
GeneralRe: 3D Points to 2D Points Pin
jschell17-Feb-18 11:41
jschell17-Feb-18 11:41 
GeneralRe: 3D Points to 2D Points Pin
Ralf Meier17-Feb-18 23:02
mveRalf Meier17-Feb-18 23:02 
QuestionNode discovery in a truly decentralized system Pin
stopthespying15-Feb-18 6:40
stopthespying15-Feb-18 6:40 
AnswerRe: Node discovery in a truly decentralized system Pin
Gerry Schmitz16-Feb-18 5:21
mveGerry Schmitz16-Feb-18 5:21 
QuestionFinding strongly connected component in a directed Graph using PHP Pin
Member 136573131-Feb-18 21:22
Member 136573131-Feb-18 21:22 
I have a problem with my homework.

Description:

1. In the group of people we define the relationship: A does not like B. This relation is not symmetrical.
If there is a series of relationships A1 does not like A2, A2 does not like A3 ... Ak does not like A1,
all people in this cycle belong to one group of "disliking".
For the given pair specifying the relations, determine the maximum division
groups of "disliking". Provide complexity and justify the correctness of your solution.

2. There are some animosities in the group of naughty children. The preschool teacher decided to
set the children in a row. If A does not like B, he can not stand in a row in front of B (not to throw papers in it).
a) Specify if a group of children can be set in one row.
b) If not, enter the minimum number of rows to set children in above configuration.
Provide complexity and justify the correctness of your solution.
c) Enter the minimum number of rows necessary to set the children, if:
Child A does not like child B, it must stand in a row with a lower number.

I know that in the first task I have to find strongly connected components. Actually I did it using Tarjan's algorithm in PHP implementations. I wonder that is it correct approach and how to provide complexity of my solution. Unfortunately I don't have any information how to solve second task. Maybe someone has an idea and could give a clue.

Thanks for advice Smile | :)

Code of first task:

PHP
const GROUPS_COUNTER = 10;
const MIN_ASCII = 65;
const MAX_ASCII = 69;

/*
 * generate children pairs.
 */
function generateGroups($n) {
    $matrix = [];
    $exist = [];
    echo "************************\n";

    for ($i = 0; $i < $n; $i++) {
        $a = chr(rand(MIN_ASCII, MAX_ASCII));
        $b = chr(rand(MIN_ASCII, MAX_ASCII));

        if ($a == $b || isset($exist[$a][$b]) || isset($exist[$b][$a])) {
            $i--;
            continue;
        }

        $matrix[$i][0] = $a;
        $matrix[$i][1] = $b;
        $exist[$a][$b] = true;

        echo $a . " " . $b . "\n";
    }

    echo "************************\n";

    return $matrix;
}

/*
 * find all children which are not like by parent.
 */
function findGroups($groups) {
    $dislikeList = [];
    $checked = [];

    foreach ($groups as $groupParent) {
        if (isset($checked[$groupParent[0]])) {
            continue;
        }

        $groupChildList = [];

        foreach ($groups as $groupChild) {
            if ($groupParent[0] == $groupChild[0]) {
                array_push($groupChildList, $groupChild[1]);
            }
        }

        echo $groupParent[0] . " => " . implode(", ", $groupChildList) . "\n";
        $dislikeList[$groupParent[0]] = $groupChildList;
        $checked[$groupParent[0]] = true;
    }

    echo "************************\n";

    return $dislikeList;
}

/*
 * phpTarjanEntry() iterates through the graph array rows, executing phpTarjan().
 */
function phpTarjanEntry($groups) {
    global $cycles;
    global $marked;
    global $markedStack;
    global $pointStack;

    $cycles = array();
    $marked = array();
    $markedStack = array();
    $pointStack = array();

    $marked = array();

    foreach ($groups as $key => $value) {
        $marked[$key] = FALSE;
    }

    foreach ($groups as $key => $value) {
        phpTarjan($key, $key);

        while (!empty($markedStack)) {
            $marked[array_pop($markedStack)] = FALSE;
        }
    }

    $cycles = array_keys($cycles);

    return $cycles;
}

/*
 * Recursive function to detect strongly connected components (cycles, loops).
 */
function phpTarjan($s, $v) {
    global $G;
    global $cycles;
    global $marked;
    global $markedStack;
    global $pointStack;

    $f = FALSE;
    $pointStack[] = $v;
    $marked[$v] = TRUE;
    $markedStack[] = $v;

    foreach ($G[$v] as $w) {
        if ($w < $s) {
            $G[$w] = array();
        } else if ($w == $s) {
            $cycles[implode('|', $pointStack)] = TRUE;
            $f = TRUE;
        } else if (isset($marked[$w]) && $marked[$w] === FALSE) {
            $g = phpTarjan($s, $w);

            if (!empty($f) OR ! empty($g)) {
                $f = TRUE;
            }
        }
    }

    if ($f === TRUE) {
        while (end($markedStack) != $v) {
            $marked[array_pop($markedStack)] = FALSE;
        }

        array_pop($markedStack);
        $marked[$v] = FALSE;
    }

    array_pop($pointStack);
    return $f;
}

$mixGroups = generateGroups(GROUPS_COUNTER);
$res = findGroups($mixGroups);
$G = $res;
$cycles = phpTarjanEntry($res);

echo '<p>Cycles found: ' . count($cycles);
print_r($cycles);

QuestionA very interesting question for you all Pin
Member 1364484725-Jan-18 14:28
Member 1364484725-Jan-18 14:28 
AnswerRe: A very interesting question for you all Pin
Richard MacCutchan25-Jan-18 22:26
mveRichard MacCutchan25-Jan-18 22:26 
AnswerRe: A very interesting question for you all Pin
jschell8-Feb-18 13:33
jschell8-Feb-18 13:33 
AnswerRe: A very interesting question for you all Pin
Luc Pattyn8-Feb-18 14:59
sitebuilderLuc Pattyn8-Feb-18 14:59 
Questionpython code on Effect of order of accuracy on the numerical differentiation Pin
Member 1362564316-Jan-18 1:32
Member 1362564316-Jan-18 1:32 
GeneralRe: python code on Effect of order of accuracy on the numerical differentiation Pin
harold aptroot17-Jan-18 23:28
harold aptroot17-Jan-18 23:28 
QuestionCompare products in Cart against min/max limitations Pin
Rico6419-Dec-17 7:07
Rico6419-Dec-17 7:07 
AnswerRe: Compare products in Cart against min/max limitations Pin
Gerry Schmitz21-Dec-17 11:04
mveGerry Schmitz21-Dec-17 11:04 
GeneralRe: Compare products in Cart against min/max limitations Pin
Rico6422-Dec-17 0:42
Rico6422-Dec-17 0:42 
AnswerRe: Compare products in Cart against min/max limitations Pin
Richard MacCutchan21-Dec-17 21:40
mveRichard MacCutchan21-Dec-17 21:40 
QuestionWriting a Sorting Algorithm -- NEED HELP! Pin
Member 135671429-Dec-17 11:36
Member 135671429-Dec-17 11:36 
AnswerRe: Writing a Sorting Algorithm -- NEED HELP! Pin
Gerry Schmitz10-Dec-17 1:09
mveGerry Schmitz10-Dec-17 1:09 
AnswerRe: Writing a Sorting Algorithm -- NEED HELP! Pin
Richard MacCutchan10-Dec-17 1:22
mveRichard MacCutchan10-Dec-17 1:22 
GeneralRe: Writing a Sorting Algorithm -- NEED HELP! Pin
Jc Christ5-Jun-20 1:57
Jc Christ5-Jun-20 1:57 
AnswerRe: Writing a Sorting Algorithm -- NEED HELP! PinPopular
Richard Deeming11-Dec-17 1:58
mveRichard Deeming11-Dec-17 1:58 
AnswerRe: Writing a Sorting Algorithm -- NEED HELP! Pin
Member 1488614010-Jul-20 3:04
Member 1488614010-Jul-20 3:04 
GeneralRe: Writing a Sorting Algorithm -- NEED HELP! Pin
Dave Kreskowiak23-Jul-20 3:39
mveDave Kreskowiak23-Jul-20 3:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.