instruction stringlengths 0 29.8k ⌀ |
|---|
I was looking at the Advent of Code 2023 [day 11](https://adventofcode.com/2023/day/11) problem and misread it to think that the question ended up asking something akin to what's below (after transforming the positions of the "galaxies"), and I'm wondering what kind of optimization problem this is. See [nearly equivale... |
Choose edges in fully connected weighted graph that minimize sum of weights |
|graphs|set-cover| |
Write an algorithm that takes an arbitrary natural number and returns a boolean of whether the number is a perfect square. Sketch a flowchart of the algorithm.
My approach is that a natural number $n$ is a perfect square if and only if we are able to find a natural number $i$ where $i < n$ such that $n / i - i = 0$ ... |
Write an Algorithm that Outputs Whether a Number is a Perfect Square |
null |
You're right. It is a sort of 0-1 knapsack problem. It is NP-complete in general. So you will need to settle for approximate solutions or heuristics or algorithms that only work for short strings or make other compromises.
You probably don't want to find all such solutions, as there could be exponentially many. ... |
we are looking into sampling for the traversable areas of a 2D map where new nodes can be inserted efficiently and it is less costly to extract nodes from a built tree structure. Our requirements:
1. Sampling in a dense 2D map with mostly non-empty areas
2. Efficient node insertion and data structure update
3. ... |
Optimal graph data structure for 2D map sampling that allows dynamic updates |
|graphs|trees|c++|robotics| |
I believe you may be looking for the dominator tree of the CFG. Some useful resources:
- section 4 of Recursive Translation of Unstructured Control Flow to Structured Control
https://dl.acm.org/doi/pdf/10.1145/3547621
- https://www.csd.uwo.ca/~mmorenom/CS447/Lectures/CodeOptimization.html/node6.html |
I believe you may be looking for the dominator tree of the CFG. Some useful resources:
- section 4 of Recursive Translation of Unstructured Control Flow to Structured Control
https://dl.acm.org/doi/pdf/10.1145/3547621
- https://www.csd.uwo.ca/~mmorenom/CS447/Lectures/CodeOptimization.html/node6.html
It could ... |
I believe that a good starting point is the following list compiled by Codechef:
https://www.codechef.com/certification/data-structures-and-algorithms/prepare
It contains resources and problems for all relevant topics.
It's also important to participate in competitions along the way - you can check both Codeforc... |
null |
Build a Turing machine that will count the units written in a row (without omissions) (their number does not exceed n) and write their number in the number system with the base n +1, here n=3+[N](mod 13) and N =(your number in the group list)+(your number groups).Build a Turing machine that will count the units recorde... |
Build a Turing machine that will count the units written in a row (without omissions) |
|turing-machines| |
The intuitive reasoning is "there are more problems than algorithms, so it cannot be the case that, for each problem, there exists an algorithm that solves it". More formally, this can be expressed as follows:
The fact that the two sets have different cardinality implies that there is no surjective map $f$ from th... |
null |
No, it is not possible to write a code that would "run forever" without a loop. |
null |
null |
Merging $n$ sorted numbers and $log\ n$ unsorted numbers asks you to do two things:
1. Sort the $log\ n$ unsorted numbers in $O(log\ n\ log\ log\ n)$
2. Merge $n$ sorted numbers and $log\ n$ sorted numbers in $O(n + log\ n)$
This yields the total time complexity of $O(n + log\ n + log\ n\ log\ log\ n)$ = $O(n)... |
As many others have mentioned in the comments, it would depend on what you define as a loop. In any programming language with recursion, it is possible to define a theoretically non-terminating program by doing something like:
```python
def f (x):
return f(x)
f(5)
```
Which does not contain any explicit lo... |
In an image processing system, an algorithm processes an "n x n" image (the image is
represented by an n by n matrix of pixels). Each pixel is a number in
the 0-255 range. The system finds the median "M" in the image and converts every pixel to a
binary number using the following rule:
Every pixel will be one: if t... |
would the answer be o(nlogn) or o(n^2logn) |
|algorithms| |
Given a binary $n \times n$ matrix, we would like to decide whether there is a row or a column which consists entirely of $1$s. The caveat is that after we read an entry of the matrix, it is erased. How much space do we need in order to solve this task?
If entries do not get erased, the task can be solved in space $... |
Read-once complexity of a matrix problem |
|algorithms|complexity-theory|space-complexity|lower-bounds| |
The computation of the erasing machine can be expressed as a *read-once branching program*.
A branching program is a DAG with a unique source and two sinks, labelled "Yes" and "No". Each non-sink is labelled by an input variable, and has exactly two outgoing edges, labelled $0$ and $1$. Essentially, this is a compr... |
No, it is not possible to write a code that would "run forever" without a loop.
A program that runs forever basically means that it always has some statement to execute. In absence of all kinds of loops (recursion , etc.), you will have to write infinite number of statements so that your program can continue running... |
No, it is not possible to write a code that would "run forever" without a loop.
A program that runs forever basically means that it always has some statement to execute. In absence of all kinds of loops (while(1), for(;;), recursion, etc.), you will have to write infinite number of statements so that your program ca... |
The answer is yes! Fix your favorite flavour of Turing machines (for instance: non-deterministic Turing machines with a single tape), and fix both the input and output alphabets. Then for each $s \in \mathbb{N}$, there are finitely many machines with $s$ states (up to relabeling the states). Also, for each $n \in \math... |
Chack battery if there is the provision to do so (as suggested by greybeard)
Check you have latest firmware.
Get a quote from third party repair technician (not always possible with medical equipment).
Pay the money to BEMER.
Attempt repair yourself – there are no youtube videos to help you.
If you fail... |
Given a sorted array of positive integers that is guaranteed to have some unique entry that occupies more than $1/3$ of the entries, is there an algorithm to determine this entry in $O(1)$ time?
Some remarks:
(1) If we replaced $1/3$ with $1/2$, then by checking the first, middle, and last entries of the array, w... |
Element that occurs more than one third of the time in a sorted array - constant time? |
|algorithms|arrays|constant-time| |
> Additionally, I have a bit of trouble understanding the notion of encoding a whole algorithm as a single binary string —is it just an encoding of the finite-tuple-of-finite-sets Turing machine into a single binary string/number?
Yes.
First of all, the word "binary" in that sentence is irrelevant. If you can enc... |
If you just want any polynomial-time algorithm, you can solve this with a [maximum weight matching](https://en.wikipedia.org/wiki/Maximum_weight_matching) algorithm.
Let's first solve the problem where you require that it include all $N$ points. Draw a graph with a vertex per point, and the weight on an edge is the... |
I think it is more difficult. If your array has 3n elements then you could have n times x and n+1 times y, and the solution is y. So even if you find x and y, you then need to prove that y occurs n+1 and not n times, and x occurs n and not n+1 times. That's probably done with binary search, taking O (log n) steps. |
Easily with recursion. For example the C function
void f() { f (); f (); }
will run forever, given arbitrary amounts of memory. |
It is O(n^2).
Since there are only 256 values, you count how often each pixel value occurs in n^2 time and find the median in at most 256 steps. Then you check each of n^2 pixels again in n^2 steps. |
null |
null |
I was looking at the Advent of Code 2023 [day 11](https://adventofcode.com/2023/day/11) problem and misread it to think that the question ended up asking something akin to what's below (after transforming the positions of the "galaxies"), and I'm wondering what kind of optimization problem this is. See [nearly equivale... |
I was looking at the Advent of Code 2023 [day 11](https://adventofcode.com/2023/day/11) problem and misread it to think that the question ended up asking something akin to what's below (after transforming the positions of the "galaxies"), and I'm wondering what kind of optimization problem this is. See [nearly equivale... |
I have the following problem, and would like to figure out whether or not it's NP-hard - primarily to know that searching for a polynomial algorithm for it is futile. Approximations are possible, and in practice sufficient, but I'm still interested in how hard the exact problem is.
Given is a DAG $(V, E)$, where eve... |
Finding highest value/weight ratio in dependency graph: NP-hard? |
|optimization|np-complete|knapsack-problems|dag| |
I have the following problem, and would like to figure out whether or not it's NP-hard - primarily to know that searching for a polynomial algorithm for it is futile. Approximations are possible, and in practice sufficient, but I'm still interested in how hard the exact problem is.
Given is a DAG $(V, E)$, where eve... |
So I am given the language *qr* where *q* is any combination of a's and b's. *r* is then the reverse of whatever *q* is. For example, abba is in the language because we can make a *q* = ab and *r* = ba
I have to prove, using the Pumping Lemma, that this language is **not** regular.
I can't make any sense of this,... |
Proving a language is not regular using Pumping Lemma |
|regular-languages|automata|finite-automata|pumping-lemma| |
No, there is no $O(1)$ time algorithm.
In the $1/2$ case, we can obtain information when the following occurs:
1. the middle element is equal to the first element
2. the middle element is equal to the last element
3. the middle element is equal to neither the first element nor the last element
In the $1/3$ cas... |
I am trying to solve a competitive-programming-style problem which I've boiled down to the following: given two arrays $a, b$ of length $n$, where $a_i, b_i > 0$ for all $i=1\ldots n$, compute
$$\max_{i, j : i\neq j} \quad a_ib_i + a_jb_j + \max \{ a_ib_j, a_jb_i \}. $$
Of course, this can be solved by brute force... |
How to do better than brute-force for this problem? |
|algorithms| |
In the case of the pumping lemma, it is not good for intuition or practice to consider specific strings, such as $abba$. The statement of the pumping lemma says that if a language $L$ is regular, then *every single* string $w \in L$ of length at least $p$ can be written as $w = xyz$ satisfying
1. $|y| \geq 1
2. $|x... |
In the case of the pumping lemma, it is not good for intuition or practice to consider specific strings, such as $abba$. The statement of the pumping lemma says that if a language $L$ is regular, then *every single* string $w \in L$ of length at least $p$ can be written as $w = xyz$ satisfying
1. $|y| \geq 1$
2. $|... |
null |
null |
This problem is actually a bit simpler than first thought, and I think you had the right idea about sorting the numbers.
In a sorted list, we know that ai * bi is maximum when we just pick the final index/number.
So the only real hard part is how the ai bj or aj bi affects the equation. And the answer is that it ... |
What I would try: Sort the arrays by $a_i \cdot b_i$. Combine them starting with the smallest indices, and you should be able to show that any smaller product needs components of different sizes, because you want to combine a large a_i with a large b_j.
So if the largest products are say about 100, a product 50 x 1... |
null |
I was just sitting down, and I realized this problem if you convert all (ai, bi) to coordinates on a graph, I believe this can be solved similarly to the, [closest pairs problem](https://www.geeksforgeeks.org/closest-pair-of-points-onlogn-implementation/). Which has a nlogn solution, related to comparing all coordinate... |
I was looking at the Advent of Code 2023 [day 11](https://adventofcode.com/2023/day/11) problem and misread it to think that the question ended up asking something akin to what's below (after transforming the positions of the "galaxies"), and I'm wondering what kind of optimization problem this is. See [nearly equivale... |
null |
If you just want any polynomial-time algorithm, you can solve this with a [maximum weight matching](https://en.wikipedia.org/wiki/Maximum_weight_matching) algorithm.
Draw a graph with a vertex per point, and the weight on an edge $(i,j)$ is $C-D_{ij}$. Here $C$ is a very large constant, chosen to be bigger than $N ... |
I have the following problem, and would like to figure out whether or not it's NP-hard - primarily to know that searching for a polynomial algorithm for it is futile. Approximations are possible, and in practice sufficient, but I'm still interested in how hard the exact problem is.
Given is a DAG $(V, E)$, where eve... |
I have the following problem, and would like to figure out whether or not it's NP-hard - primarily to know that searching for a polynomial algorithm for it is futile. Approximations are possible, and in practice sufficient, but I'm still interested in how hard the exact problem is.
Given is a DAG $(V, E)$, where eve... |
Okay, so I think I found *a bit* of an optimization?
---
Firstly, since arrays are accessed by the same index, - you aren't dealing with 2 arrays, you're dealing with array of 2-tuples
and 2-tuples can represent points on 2D plane: $(x_i, y_i)$
And for brevity, let's call what we optimize over as function $... |
If you just want any polynomial-time algorithm, you can solve this with a [maximum weight matching](https://en.wikipedia.org/wiki/Maximum_weight_matching) algorithm.
Draw a graph with a vertex per point, and the weight on an edge $(i,j)$ is $C-D_{ij}$. Here $C$ is a very large constant, chosen to be bigger than $N ... |
We're looking to model the following problem as a standard optimization problem (or even a non-standard one). We can come close, but nothing seems to fit exactly. We have a working algorithm coded, but the performance is unacceptable.
**Problem statement**
The problem is set up in two parts. First, there are fixe... |
How can I model this optimization problem? |
|optimization|linear-programming|constraint-programming|independent-sets| |
If you convert all (ai, bi) to coordinates on a graph, I believe this can be solved similarly to the, [closest pairs problem](https://www.geeksforgeeks.org/closest-pair-of-points-onlogn-implementation/). Which has a nlogn solution, related to comparing all coordinates together to find some max or min value. |
only one way to construct this CFG is that make DFA of the given problem then convert DFA to Grammar that will be a regular grammar and we know every regular grammar is CFG also[**By chomsky classification**].
So following will be the production of the required grammar:
S => aS/bA/a/b
A => aA/bB/a/b
B => aB/bC/... |
I was staring at this question for too long thinking "what's the catch?". But I think I got it
---
If input array contains 2 entries such that one occupies n/3 entries and the other n/3-1 - then the only way to distinguish between them would be determining _exact_ borders of their intervals - and that's $O(\log n... |
If we ignore the upper bound $m$, this is the problem of finding a [maximum weight clique](https://en.wikipedia.org/wiki/Clique_problem) in a weighted graph. Delete all unavailable objects, and create a graph, where each object is a vertex, and you draw an edge between each pair of compatible objects. Then you want ... |
null |
"""In a doubly-linked list, the time complexity for inserting and deleting an element is O(1)."""
The above statement is not correct for all cases. I have explained it below.
I am assuming that you don't want a sorted linked list.
Then, inserting an element in both singly linked list and doubly linked list is ... |
"""In a doubly-linked list, the time complexity for inserting and deleting an element is O(1)."""
The above statement is not correct for all cases. I have explained it below.
I am assuming that you don't want a sorted linked list.
Then, inserting an element in both singly linked list and doubly linked list is ... |
"""In a doubly-linked list, the time complexity for inserting and deleting an element is O(1)."""
The above statement is not correct for all cases. I have explained it below.
I am assuming that you don't want a sorted linked list.
Then, inserting an element in both singly linked list and doubly linked list is ... |
"""In a doubly-linked list, the time complexity for inserting and deleting an element is O(1)."""
The above statement is not correct for all cases. I have explained it below.
I am assuming that you don't want a sorted linked list.
Then, inserting an element in both singly linked list and doubly linked list is ... |
Calculate the square root of n, round to the nearest integer, and check whether the square equals n. |
null |
Your reduction is incorrect.
The problem is with your claim "Each clique in $G'$ corresponds to a vertex in $G$". This is false, because there could be a clique associated to two different vertices.
For example, consider a 3-clique $G = (\{v_1, v_2, v_3\}, \{v_1v_2, v_2v_3, v_1v_3\})$. The corresponding graph $G'... |
Your reduction is incorrect.
The problem is with your claim "Each clique in $G'$ corresponds to a vertex in $G$". This is false, because there could be a clique associated to two different vertices.
For example, consider a 3-clique $G = (\{v_1, v_2, v_3\}, \{v_1v_2, v_2v_3, v_1v_3\})$. The corresponding graph $G'... |
Strong Exponential Time Hypothesis states that general SAT, where clauses are not limited in length, can't be solved in time $o(2^n)$ (and each k+1-SAT requires exponentially more time to solve than k-SAT). It's proven that DPLL algorithm requires $\Omega(2^n)$ time in the worst case.
CDCL algorithms, however, use m... |
null |
Only one way to construct this CFG is that make DFA of the given problem then convert DFA to Grammar that will be a regular grammar and we know every regular grammar is CFG also [**By Chomsky classification**].
So following will be the production of the required grammar:
S => aS/bA/a/b
A => aA/bB/a/b
B => a... |
null |
I have been trying to figure out why this problem is supposedly not in $P$. Eager to learn where my reasoning is wrong!
**Problem:**
Given a pair of integers ($n$, $m$) in binary, does there exist $2 \leq k \leq m$ such that $k$ divides $n$?
**Reasoning:**
In order to find a potential $k$ we need to loop ov... |
Why is this problem not in $P$? |
|complexity-theory| |
Strong Exponential Time Hypothesis states that general SAT, where clauses are not limited in length, can't be solved in time $o(2^n)$. It's proven that DPLL algorithm requires $\Omega(2^n)$ time in the worst case.
CDCL algorithms, however, use more sophisticated approaches to SAT solving, primarily by using partial ... |
Every integer other than 1 is the sum of primes. So it’s either 0 or >= 2 symbols.
eps + (a+b)(a+b)(a+b)*
|
I can suggest a simple algorithm. The psuedocode is below.
```
for (i=0; ((i*i) < n); i++); // this for loop will exit when i*i becomes >= n
if ((i*i) == n) {
return 1; // true
} else {
return 0; // false
}
``` |
I can suggest a simple algorithm. The psuedocode is below.
```
for (i=0; ((i*i) < n); i++); // this for loop will exit when i*i becomes >= n
if ((i*i) == n) {
return 1; // true
} else {
return 0; // false
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.