I have this problem and I came out with a solution but what is the best way to solve this problem ? Thank you in advance.
Given an array of strings and an array of tasks you need to return a sorted array with tasks and dependencies tasks ids.
Example:
$inputs = ['A'];
$tasks = [
['id' => 1, 'type' => 'A', 'dependencies' => ['B']],
['id' => 2, 'type' => 'A', 'dependencies' => ['C']],
['id' => 3, 'type' => 'B', 'dependencies' => ['C']],
['id' => 4, 'type' => 'C', 'dependencies' => ['A', 'F']],
['id' => 5, 'type' => 'D', 'dependencies' => []],
['id' => 6, 'type' => 'D', 'dependencies' => ['F']],
['id' => 7, 'type' => 'E', 'dependencies' => ['D']],
['id' => 8, 'type' => 'F', 'dependencies' => []]
];
Expected output:
[1, 2, 3, 4, 8]
// Comments.
Id 1 of tasks['type'] = A - [1]
Id 2 of tasks['type'] = A - [1, 2]
Tasks id 1 has dependencies B then
Id 3 of tasks['type'] = B - [1, 2, 3]
Tasks id 3 has dependencies C then
Id 4 of tasks['type'] = C - [1, 2, 3, 4]
Tasks id 4 has dependencies A and F. Take F then
Id 8 of tasks['type'] = F - [1, 2, 3, 4, 8]
So far this is the solution I built, but I would like to know if I am in the right path.
print_r(getId($inputs, $tasks));
function getId($inputs, $tasks){
$id = [];
$deps = [];
$values = [];
$result = [];
foreach($inputs as $input){
foreach($tasks as $task){
if ($task['type'] == $input){
$result[$task['id']] = $task['id'];
foreach($task['dependencies'] as $dependencies){
if($dependencies != $input){
$deps[] = $dependencies;
}
}
}
else
{
$values[$task['type']]['id'][] = $task['id'];
$values[$task['type']]['deps'] = $task['dependencies'];
}
}
foreach($deps as $d){
if(count($values[$d]['deps']) > 0)
{
foreach($values[$d]['deps'] as $nDeps)
{
if($nDeps != $input){
$result[] = $values[$nDeps]['id'];
}
}
}
}
}
foreach( $result as $res){
if (is_array($res)){
foreach( $res as $r){
$id[] = $r;
}
}
else {
$id[] = $res;
}
}
return array_unique($id);
}
3. Besides, the code is pretty verbose. There is scope to make it more concise.