I've done a bit of research online about this solution and I'm seeing a lot of conflicting information, some stating it's exponential, some stating it's linear. I'm wondering what is the worst case scenario's time complexity of flattening an array utilizing recursion inside of a loop to flatten deeply nested structures?
This is my code:
const flattenDeep = (array) => {
const flat = [];
for (let element of array) {
Array.isArray(element)
? flat.push(...flattenDeep(element))
: flat.push(element);
}
return flat;
}
CodePudding user response:
No, the code you've shown has neither exponential nor linear time complexity.
Before we can determine the complexity of any algorithm, we need to decide how to measure the size of the input. For the particular case of flatting an array, many options exist. We can count the number of arrays in the input, the number of array elements (sum of all array lengths), the average array length, the number of non-array elements in all arrays, the likelyhood that an array element is an array, the average number of elements that are arrays, etc.
I think what makes the most sense to gauge algorithms for this problem are the number of array elements in the whole input - let's call it e
- and the average depth of these elements - let's call it d
.
Now there are two standard approaches to this problem. The algorithm you've shown
const flattenDeep = (array) => { const flat = []; for (let element of array) { Array.isArray(element) ? flat.push(...flattenDeep(element)) : flat.push(element); } return flat; }
does have a time complexity of O(e * d)
. It is the naive approach, also demonstrated in the shorter code
const flattenDeep = x => Array.isArray(x) ? x.flatMap(flattenDeep) : [x];
or in the slightly longer loop
const flattenDeep = (array) => {
const flat = [];
for (const element of array) {
if (Array.isArray(element)) {
flat.push(...flattenDeep(element))
} else {
flat.push(element);
}
}
return flat;
}
Both of them have the problem that they are more-or-less-explicit nested loops, where the inner one loops over the result of the recursive call. Notice that the spread syntax in the call flat.push(...flattenDeep(element))
amounts to basically
for (const val of flattenDeep(element)) flat.push(val);
This is pretty bad, consider what happens for the worst case input [[[…[[[1,2,3,…,n]]]…]]]
.
The second standard approach is to directly put non-array elements into the final result array - without creating, returning and iterating any temporary arrays:
function flattenDeep(array) {
const flat = [];
function recurse(val) {
if (Array.isArray(val)) {
for (const el of val) {
recurse(el);
}
} else {
flat.push(val);
}
}
recurse(array);
return flat;
}
This is a much better solution, it has a linear time complexity of O(e d)
.
CodePudding user response:
the exploration of a regular structure can be done, however, for models that use addressing it is necessary to control the exploration, otherwise it may have infinite recursion.. i.e:
var a = [ 1, 2, 3 ]; var t = [ ]; t.push(a); t.push(t);
console.log(t);
- action: check if your array value already explored.