nested boxes algorithm

I am given a set of 3D boxes {$ B_1$ , $ B_2$ $ B_n$ } and each box have length, width and height. But these values are interchangeable since I can rotate the box. I need to find out the maximum sequence of nested boxes.

I have tried this problem using sort. But I still get stuck. I used built in sort function to sort all boxes by length in ascending order. I read the reference about this question and learned that people used radix sort. I have know idea how radix sort is applied here because all sorts I am familiar is comparison sort.

Performing crossover on trees in genetic algorithm

I’m using genetic algorithm for solving a problem. Each chromosome is a B* tree (where each node has only 2 child nodes).

I’m wondering how to perform the crossover. I found an example which says that crossover points are fixed in parents, at which the tree is sliced, and the child is constructed from sliced sub-trees.

But I know I need to consider more things. Like, what if the child tree is not balanced? Or, what if the child contains duplicate items?

Any advice how I should proceed with these questions in mind? Thanks in advance.

Algorithm to Minimize a Regular Expression

I am referring to regular expressions with alphabet {$ 0$ , $ 1$ }. We want to minimize them so that they have the least possible number of symbols and operators. Is there an algorithm to do this?

For instance, what is done on this page in the accepted answer:

Is there a formal algorithm to explain the process that the answer went through?

What Is The Meaning Of Google Broad Core Algorithm ?

what is the meaning of google Broad Core Algorithm ?

Web Design & Development Company in Bangalore | Website Designing Company in Bangalore | Website Designing Companies in Bangalore | Web Designing Companies in Bangalore | Website Design Companies in Bangalore | Web Design Company in…

What Is The Meaning Of Google Broad Core Algorithm ?

Finding multiples of each element of the array in the left-sub array. How can I make my algorithm more efficient

Problem Statement: You are given an array/sequence of positive numbers $ a_1,a_2,a_3,\cdots,a_n$ and you need to execute q queries on the array and in each query you will take a positive number as an input and find out all the multiples of the number in the given array and print it.

Input: 2 4 9 15 21 20 q1 = 2 q2 = 3 q3 = 5  Output: 3 3 2 

To solve this problem I thought of an algorithm which works as explained below:

  • Create an array name freq_data[] whose length will be equal to the maximum element of the array, and it stores the count of each and every number occurred in the input array.
    For Example:

      array[] = {2, 4, 9, 15, 21, 20}   max_element = 21   freq_data[] = {0,1,0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,1}  
  • Create an array name multiples[] whose length will also be equal to maximum element encountered in the array. This array will store all the multiples of the numbers between $ [1,\text{maximum value}]$ .

  • Initialise the multiples[0] = (size of arr[] - 1) because every number in the array is divisible by 1.

  • If the query entered is $ (\gt \text{maximum value})$ in the array, then the answer will be zero because there will be no element in the array which will be divisible by arr[i] because for the number to be multiple of another, the number should be either equal to greater than the divisor i.e $ (\geq arr[i])$ .

  • By creating a multiples[] I will be able to answer each query in constant-time by printing the value at multiples[q - 1].

  • Time Complexity: $ O(max \times log(max))$
    Space Complexity: $ O(max)$

Using all this facts I wrote the code in C Programming Language as follows:

#include<stdio.h> #include<stdlib.h> #include<inttypes.h> #include<string.h> #include<assert.h>  uint32_t* find_all_multiples(uint32_t[],uint32_t,uint32_t);  int main(void) {     uint32_t n,max_val = 0;     printf("Enter the size of the sequence\n");     scanf("%"SCNu32,&n);     uint32_t data[n];     printf("Enter the data\n");     for(uint32_t i = 0; i < n; ++i) {         scanf("%"SCNu32,&data[i]);         assert(data[i] > 0);         if(data[i] > max_val) {             max_val = data[i];         }     }     uint32_t *const multiples = find_all_multiples(data,n,max_val);     uint32_t query;     printf("Enter the number of queries\n");     scanf("%"SCNu32,&query);     while(query--) {         uint32_t num;         scanf("%"SCNu32,&num);         if(num > max_val) {             printf("0\n");         } else {             printf("%"PRIu32"\n",multiples[num - 1]);         }     }     free(multiples);     return 0; }  uint32_t* find_all_multiples(uint32_t data[],uint32_t n,uint32_t max_val) {     uint32_t *const freq_data = calloc(max_val,sizeof(uint32_t));     for(uint32_t i = 0; i < n; ++i) {         ++freq_data[data[i] - 1];     }     uint32_t *const multiples = calloc(max_val,sizeof(uint32_t));     multiples[0] = n - 1;     for(uint32_t i = 2; i <= max_val; ++i) {         for(uint32_t j = i; j <= max_val; (j += i)) {             multiples[i - 1] += freq_data[j - 1];         }     }     for(uint32_t i = 0; i < max_val; ++i) {         printf("%"PRIu32" ",multiples[i]);     }     printf("\n");     free(freq_data);     return multiples; } 

Now I was wondering what if I changed the question like instead of taking the queries from the user, I am now interested in finding for each arr[i] how many multiples are present in the left-sub array with respect to arr[i] and I wrote the code for solving this problem also but I am curious about, can I do better.
For Example:

Input: arr[] = {2 6 3}  Output:   no_of_multiples[] = {0,0,1} // For 2 , 6 there are no multiples present but for 3 there is one multiple present i.e. 6   

The algorithm which I designed is basically based on the previous algorithm but rather than updating the freq_data[] at once I will first check how many multiples of the arr[i] is present in the freq_data[] and after finding all the multiples, I will increment the value at ++freq_data[data[i] - 1]

You can see the source-code below:

#include<stdio.h> #include<stdlib.h> #include<inttypes.h> #include<stdbool.h> #include<string.h> #include<assert.h>   struct multiples_array {     uint32_t value;     uint32_t no_of_multiples; }; typedef struct multiples_array multiples_array_t;  void take_input(multiples_array_t *const,uint32_t); void generate_multiples(multiples_array_t *const,uint32_t); const uint32_t find_maximum(multiples_array_t *const,uint32_t);  int main(void) {     uint32_t n;     printf("Enter the size of the array\n");     scanf("%"SCNu32,&n);     assert(n > 0);     multiples_array_t *const data = calloc(n,sizeof(multiples_array_t));     if(data) {         take_input(data,n);         generate_multiples(data,n);         free(data);     } else {         fprintf(stderr,"Memory not allocated to *data pointer!\n");     }     return 0; }  void take_input(multiples_array_t *const data,uint32_t n) {     for(uint32_t i = 0; i < n; ++i) {         scanf("%"SCNu32,&(data[i].value));         data[i].no_of_multiples = 0;         assert((data[i].value) > 0);     } }  void generate_multiples(multiples_array_t *const data,uint32_t n) {     uint32_t max_val = find_maximum(data,n);     uint32_t *const freq_data = calloc(max_val,sizeof(uint32_t));     if(freq_data) {         for(uint32_t i = 0; i < n; ++i) {             if((data[i].value) > max_val) {                 max_val = data[i].value;             }             if(data[i].value == 1) {                 data[i].no_of_multiples = i;             }             else if(data[i].value <= max_val) {                 for(uint32_t j = 1; ((data[i].value) * j) <= max_val; ++j) {                     (data[i].no_of_multiples) += freq_data[((data[i].value) * j) - 1];                 }             }             ++freq_data[(data[i].value) - 1];         }         for(uint32_t i = 0; i < n; ++i) {             printf("%"PRIu32" ",(data[i].no_of_multiples));         }         printf("\n");         free(freq_data);     } else {         fprintf(stderr,"Memory not allocated to *freq_data pointer!\n");     } }  const uint32_t find_maximum(multiples_array_t *const data,uint32_t n) {     uint32_t max = 0;     for(uint32_t i = 0; i < n; ++i) {         if((data[i].value) > max) {             max = data[i].value;         }     }     return max; } 

Time Complexity: $ O(n \times log(max))$
Space Complexity: $ O(max)$

My question is: Is there any better algorithm to solve the second part i.e to find all the multiples of arr[i] in the left-sub array $ \forall i$ ?

How does this recursive algorithm work?

One question from the Grokking Algorithms book:

Implement a max([]int) function, returning the biggest element in the array.

Here’s my solution in Golang (adapted from the Python solution in the book):

func maxNumber(arr []int) (r int) {     // base case     if len(arr) == 2 {         if arr[0] > arr[1] {             return arr[0]         } else {             return arr[1]         }     }      // recursive case     subMax := maxNumber(arr[1:])      if arr[0] > subMax {         fmt.Println("arr[0] > subMax")         return arr[0]     } else {         fmt.Println("arr[0] < subMax")         return subMax     } } 

It works, but I can wrap my head around it. How is the third block called if every time maxNumber(arr[1:]) is called it executes the function again, passing the array with one last element? If so, at some point in time it will hit the first block (base case) and return or the first element or the second.

I know the function works, but I’m missing the how.

Could someone help me around, explain like I’m 5 styles haha


Algorithm for coherent motion. Which bus is app user on?

I am currently working on an app with a map of the city, with markers for each bus. As a feature, the phone should show which bus the user is on.

To achieve this I am working on building a function that consumes a stream of a set of buses and their positions (Stream<Set<Tuple2<BusId, Location>>>), and a stream of phone location, to produce a Stream of bus predictions. The prediction should contain a confidence level.

The function should return the current prediction in real-time, and handle scenarios where the user changes the bus.

How could this be accomplished?

Both streams contain very precise locations at a rate of once every second.