Algorithms – Removes the last x elements from an array and reinserts them before the y position

I am looking for an algorithm to move a sub-array before an element (which is not part of this sub-array) in the same array where the last element of the sub-array is the last element of the array with O (1) extra space and O (n) runtime.

for example. where * p1 = 5 and * p2 = 3:

1 2 5 6 7 3 4

bECOMES

1 2 3 4 5 6 7

That's what I have up to now (written in C programming language). Problems arise when p1 reaches p2.

void swap(long* p1, long* p2, long* array_end) {
long* p2_i = p2;
while (p1 < array_end) {
if (p2_i > array_end) {
p2_i = p2;
}

// swap *p1 and *p2_i
long* temp = p1;
*p1 = *p2_i;
*p2_i = temp;

++p1;
++p2_i;
}
}

Posted on

algorithms – Application of the sub-problem technique to permutations with grouping

I try to apply overlapping subproblems and dynamic programming to permutations.

Say, we have a set of $$n$$ elements in a chain. Each of these elements could be a $$1$$ or one $$0$$.

Given some strings, I try to count the number of valid permutations, where a valid setting is defined as an order without $$1$$ which is different from others 1.

Example of input string: $$0101$$

Example of valid arrangements: $$1100, 0011, 0110$$

Not valid: $$0101, 1010, 1001$$

EXPECTED PRODUCTION: $$3$$

For example:

Suppose our input string contains four 1s and two 0s.

I guess my difficulty comes with defining the sub-problem. Initially, I thought I could create a channel with only the $$0$$ elements with slots / bins in between. $$[] 0 [] 0 []$$, then continue using the combinatory to calculate the number of possible extra arrangements with each added 1.

The first possibilities 1: 0 because it will never have a 1 to accompany it.

Added the second possibility 1: 3, because the pair can go in one or the other of the 3 boxes.

Added the 3rd possibility 1: 0, because it must go with the 2 others, wherever they are.

Added the 4th 1: We have enough to make 2 groups of $$1$$Now … here is where I'm starting to be uncertain. I would think it would be 3 additional possibilities. $$binom {3} {1} + binom {3} {2} = 6$$ total

Would anyone have an idea if I am on the wrong track, or if not, how to proceed? This approach does not seem to work for larger values. Thank you!

Posted on

algorithms – Check the sum of subsets for a special array

I was trying to solve the following problem.

We are given N and A (0)

N <= 5000
A(0) <= 10^6 and even
if i is odd then
A(i) >= 3 * A(i-1)
if i is even
A(i)= 2 * A(i-1) + 3 * A(i-2)
element at odd index must be odd and at even it must be even.

We must minimize the sum of the table.

and we are given a number Q

Q <= 1000
X<= 10^18

We need to determine if it is possible to get the subset-sum = X from our array.

What I have tried,

Creating a minimum sum table is easy. Just follow the equations and the constraints.

The approach I know for subsets is dynamic programming that has the sum * sizeof (Array) time complexity, but since the sum can reach 10 ^ 18, this approach will not work.

Is there a relationship of equation that I miss?

Posted on

algorithms – Random isomorphism of rooted trees

I'm trying to solve the following problem on the random isomorphisms of trees – this comes from an old test that I found online. I state below the problem and my progress up to here. Any help is appreciated.

Problem:
A rooted tree is a tree in which a vertex is marked as root. Two rooted trees $$T_1$$ and $$T_2$$ they say to be isomorphic if there is one to one mapping (an isomorphism) $$f$$ summits of $$T_1$$ to those of $$T_2$$, such as $$f (root (T_1))$$ = $$Root (T_2)$$and for each vertex $$v$$ of $$T_1$$ with children $$v_1, ldots, v_k$$, children (in $$T_2$$) of $$f (v)$$ are exactly $$f (v_1), ldots, f (v_k)$$ (in any order).

In this problem, you will design an efficient random algorithm to check if two given rooted trees are isomorphic. Given a rooted tree $$T$$, define a polynomial $$P_v$$ for each summit $$v$$ of $$T$$ recursively as follows. For each sheet $$v$$, to define $$P_v equiv x_0$$. (Thus, all leaves have the same trivial polynomial.) For each non-leaf top $$v$$ height $$h$$ with children $$v_1, ldots, v_k$$, to define $$P_v equiv (x_h – P_ {v_1}) (x_h-P_ {v_2}) … (x_h-P_ {v_k})$$.

(a) For a tree $$T$$ height $$h$$ with $$m$$ leaves, how many variables are there in the $$P_ {Root (T)}$$, the polynomial at the root of $$T$$? And what is the degree of this polynomial?

(B) Show that if two trees rooted $$T_1$$ and $$T_2$$ are isomorphic, so $$P_ {Root (T_1)} equiv P_ {Root (T_2)}$$. (hint: Use induction on the height of the tree. You can assume that, if $$f$$ is an isomorphism of $$T_1$$ at $$T_2$$ and $$v$$ is a peak of $$T_1$$, then the sub-trees rooted in $$v$$ and $$f (v)$$ are also isomorphic.)

(vs) Show that if $$T_1$$ and $$T_2$$ are rooted trees and $$P_ {Root (T_1)} equiv P_ {Root (T_2)}$$then $$T_1$$ and $$T_2$$ are isomorphic. (hint: Again, use the induction on the height of the tree. You can assume that, if a polynomial $$Q equiv (x – alpha_1) ldots (x – alpha_k)$$, where the $$alpha$$does not depend on $$x$$Such a factorization is therefore unique, up to the order of the factors.)

(re) Give an efficient algorithm to test if two given rooted trees, $$T_1$$ and $$T_2$$ are isomorphic. If they are isomorphic, the result should be "yes"; if they are not, it should be "no" with at least one probability $$1/2$$.

Attempt:

(a): There is $$h$$ variables in $$P_ {Root (T)}$$ (one variable per level in the tree). The polynomial is degree $$m$$.

(b) and (c): I struggle with these two.

(d): With (b) and (c) we know that $$P_ {Root (T_1)} equiv P_ {Root (T_2)} iff T_1$$ and $$T_2$$ are isomorphic. This means that we can answer "yes" or "no" by asking $$P_ {Root (T_1)} equiv P_ {Root (T_2)}?$$. Of course, this means that we can perform polynomial identity tests to check whether the two polynomials match. It is an application of the Schwartz-Zippel lemma. The algorithm would work as well as the multivariate polynomial identity test algorithm that satisfies the requirements of the question.

Posted on

Algorithms – How to solve the following tree problem with a number assigned to each node and respond to queries?

I have a tree whose nodes have numbers that were originally assigned to them. A series of $$Q$$ questions are asked from $$n = 0$$ at $$n = Q-1$$ seconds. At the end of each second, the numbers of a node (which are not sheets) are deleted and are transferred to each direct child node. is possible that a new number be added to a node. How to solve this problem effectively?

Example: Suppose I have a tree 1 —– 2 —— 3 ——- 4 (1 is parent). Suppose at the beginning that the numbers on the nodes are 1,2,3,4 respectively.

At zero, the second end 5 is added to the zero node. At the end of 0 seconds, the number on the node is 5.1.2.3 (note 1 is not added to 5 because it is removed ).

At a second end, 1 is added to node 4, then the numbers are 0.5,1,3. The fourth node is number three since it has received 2 of the parent node and 1 is added.

Now, suppose that, two seconds from the end, I am asked to say the number on the second node?

I mean that in each query, a number can be added on a node or a question can be asked as – what is the number on $$ith$$ node?
How to solve this problem in less than $$O (nQ)$$. Or $$n$$ is the number of nodes and $$Q$$ is the number of requests?

Posted on

algorithms – How to encode accessibility in a graph with walls as SAT problem

Suppose we have a graph representing a grid of cells. We are given a cell to start and a cell that is the destination. There are cells in which we can not enter and we call them walls. Finally, we can only move in adjacent cells (diagonal movements are not allowed).

My question is: how to code this problem in CNF so that it is solved by a SAT solver?

For example, suppose the graph represents the following 2 x 3 grid:

0 1 0
0 0 0

where 0 cells are cells in which we can be or move, and cells 1 are walls. Suppose we leave (0, 0) and want to go to (0, 2). Then there is a path.

On the contrary, consider another 2 x 3 grid with the same starting and destination positions:

0 1 0
0 1 0

Then, this graphic has no path from the beginning to the destination.

Clearly, we can run BFS on the graph and if there is a path, we can code it as a trivially satisfying CNF file or, if there is not one, encode as a non-unsatisfactory CNF file. For example, $$A$$, or, $$A land neg A$$. However, the purpose of my exercise is to code the existence of the path in CNF.

This is actually part of my homework. I'm stuck for a while now. Help on this problem would be greatly appreciated.

Posted on

Algorithms – Improve the time of the next largest Next function with the same Javascript digits

I've created a function that gives a number n by taking its numbers and looking for the next larger number than the one that can be formed …
Examples:

1. n = 513 back to 531,
2. n = 9219 return 9912,
3. n = 123 back 213

not being able to form the number pq n = 5 (there are no numbers to form others), n = 321 .. the function returns -1 .. The problem is that this function simple is the brute force and does not pass the speed test because what he does is to increase the number of 1 until he finds a number that has the same numbers in that part he does numerous comparisons in which he transforms the number, passes it by orders, etc. How can you improve the part where you compare if the numbers of the number are the same as those of my number?

function nextBigger(n) {
const digitos = `\${n}`.split('').sort((a, b) => a - b).join('');
if (n <= 11 || n == mayorPosible) return -1
else {
do {
n++
} while (digitosNumero(n) != digitos)
}
return n
}

function mayorPosible(n) {
return Number(`\${n}`.split('').sort((a, b) => b - a).join(''))
}

function digitosNumero(n) {
return `\${n}`.split('').sort((a, b) => a - b).join('')
}

console.log(nextBigger(513));
console.log(nextBigger(2063));
console.log(nextBigger(1497));
Posted on

algorithms – Java Dictionary implementation

I'm doing a dictionary in java using a separate string. It uses a configuration of X and O and empty spaces on the table ex. "XXOOOX X" for a tic-tac-toe game that evaluates the board through a string related to the arrangement and records the score. I use an "ArrayList". A list of table lists. My problem is for the remove function and the functions get that a given configuration can hash to the same value, that is why a separate chaining obviously occurs, but when the get or remove functions only take param, c & e Is a String configuration, when I try to delete or get the value in this distinct string I struggle with there are several values ​​associated with the unique value how can I identify the correct value in the string ??

Posted on

algorithms – find a pair of coprimes

we are given a table

Size of the array <= 10 ^ 4.

0 <= A [i] <= 15

We must divide it into 4 parts. Take xor from each game and add these xors.

find the maximum amount.

The approach is very expensive.

there are only 16 different values ​​of xor, so for each value of xor = X one can search in array is it possible to partition the array so as to obtain xor = X.

algorithms – Fuse 3D-Points in Bundel Adjustment?

I am setting up my own Pose-Estimate / – and-Refinement pipeline. To this end, I use a moving mono camera. Then I take the consecutive images to estimate the pose and triangulate the points (nothing special). In the last step, I fine-tune the poses and 3D dots with a beam-fitting approach.

The generation of 3D points with triangulation from consecutive image pairs will give me several estimates for a real 3D point. In fact, all estimates refer to the same point. For my understanding, these estimates of the same 3D points must be merged in one way or another. Otherwise, the poses were no longer linked by a common point (see also the picture below). Do I understand this right or do I have to use a different set of 3D points for each camera view?