instruction
stringlengths
0
29.8k
⌀
In [Introduction to Algorithms](https://en.wikipedia.org/wiki/Introduction_to_Algorithms), when talking about prefix code for Huffman encoding, it says that: > Although we won’t prove it here, a prefix code can always achieve the optimal data compression among any character code... How can we prove such a claim? ...
Optimality of prefix codes
|huffman-coding|
Yes. You can just compute which is the smallest between $n^kk^2$ and $2^{n/4}$ and run the correponding algorithm. Alternatively, you could run both algorithms in parallel (alternating one step of each) and stop when one finishes.
I have a problem set about NP Completeness proofs and I'm struggling to approach this problem: An organizer would like to arrange all the participants in a circle where neighboring two students must have taken the same class last semester. How to prove that this is an NP complete problem? Any hints/tip would...
How to prove that this problem is NP Complete
|np-complete|np-hard|np|traveling-salesman|vertex-cover|
Given a Directed Graph G, and some subsets of vertices T1,T2,..Tn(These subset can intersect) , is there a path in this graph such that it is acyclic and contains exactly 3 vertices from each Ti. I'm not sure , but I have an intuition that this is a problem that can be reduced from hamiltonian path problem , just can't...
To Prove NP-Completeness
|np-complete|reductions|hamiltonian-path|
Given a Directed Graph G, and some subsets of vertices T1,T2,..Tn(These subset can intersect) , is there a path in this graph such that it is acyclic and contains exactly 3 vertices from each Ti. I'm not sure , but I have an intuition that this is a problem that can be reduced from hamiltonian path problem , just can't...
{"OriginalQuestionIds":[155750],"Voters":[{"Id":755,"DisplayName":"D.W."}]}
- Same class last semester: an edge between two vertices. - Students in a circle: an hamiltonian cycle. I think you should be able to do the rest.
Yes. You can just compute which is the smallest between $n^kk^2$ and $2^{n/4}$ and run the corresponding algorithm. Alternatively, you could run both algorithms in parallel (alternating one step of each) and stop when one finishes.
Suppose you have a dependency graph of "packages" registered in the ecosystem of a given programming language. We can model each package as a tuple `(name, version)` where `name` is the package name (like `Plots`) and `version` is a semantic version number (like `1.2.3`). Each package has a set of dependencies, whi...
Asymptotic analysis is used mainly for two reasons - because of technical difficulties one can meet when evaluating some functions (for instance, we can't easily evaluate $\sum_{i=1}^n\frac1n$ but we know that it is well approximated by $\log(n)+\gamma$); - because when we want to compare functions, we prefer to ...
Is there any instance where a problem $A$ can be reduced to a problem $B$ where $B$ is easier to solve than $A$? I've been learning about NP-Hardness recently and seems that the answer is no. Whenever we show $ A \leq_p B$ its also said that $B$ must therefore be at least as hard as $A$. If this is always the case ...
Reducing problems to solve easier problems
|np-complete|reductions|np-hard|np|
null
Roughly speaking, no. One way to solve A is by using the method for solving B combined with the reduction from A to B. So if that reduction is efficient, then solving A can't be too much harder than solving B. If I'm pedantic, yes, B might be a little bit easier to solve than A. If the reduction is very expensive...
Let $E_1, E_2, \ldots, E_n$ be boolean-valued expressions, each with an associated evaluation cost and probability of returning `true`. For simplicity, assume these probabilities are independent, so that $\mathbb{P}(E_i \cap E_j) = \mathbb{P}(E_i) \, \mathbb{P}(E_j)$ for $i \ne j$. Suppose we want to compute the ex...
Optimal expression ordering with short-circuit evaluation
|dynamic-programming|
Let $E_1, E_2, \ldots, E_n$ be boolean-valued expressions, each with an associated evaluation cost and probability of returning `true`. For simplicity, assume these probabilities are independent, so that $\mathbb{P}(E_i \cap E_j) = \mathbb{P}(E_i) \, \mathbb{P}(E_j)$ for $i \ne j$. Suppose we want to compute the ex...
|dynamic-programming|greedy-algorithms|
{"OriginalQuestionIds":[35502],"Voters":[{"Id":755,"DisplayName":"D.W."}]}
null
I want to write a regular grammar that follows the C language. I almost wrote the grammar, but was not able to resolve how to define a variable. Def: A variable can be any combination of characters, but it should not be an existing keyword. I tried writing something like this but not sure how to write it: ...
Regular grammar such that it rejects keywords
null
Assuming I have two relations: Supplier [ID (PK), Name, City, Country] and Item [ID (PK), Name, Department, Price, Stock, Supplier_ID (FK)] What would be the easiest way to determine the following statement both in domain and tuple relational calculus: "List the IDs and names of those suppliers, who have deliv...
Relational calculus: "Find all X with at least y amount of Z"
|logic|databases|relational-calculus|tuple-relational-calculus|
null
I'm stuck on an exercise that ask the approximation factor of a MAX-SAT approximated algorithm generalized from a MAX-3SAT algorithm MAX-3SAT: set every variable with a random value (0 or 1 each with 1/2 probability) check how many clauses are satisfied This algorithm has a 7/8-approximation factor si...
MAX-SAT approximation factor
|approximation|maxsat|
I'm stuck on an exercise that ask the approximation factor of a MAX-SAT approximated algorithm generalized from a MAX-3SAT algorithm MAX-3SAT: set every variable with a random value (0 or 1 each with 1/2 probability) check how many clauses are satisfied This algorithm has a 7/8-approximation factor si...
Let $P$ be a set of finite points in $\mathbb{R}^n$. I want to know if there is an algorithm that can find the most optimal set of $n$-spheres $S$ that follows these properties: - Each $n$-sphere in $S$ overlaps with at least one of the other $n$-spheres in $S$, - For all $p \in P$, there exists $s \in S$ with c...
From abstract data types in wiki, I'm reading about two types: **collection** and **container**. Can anyone supply a better detailed difference between the difference between the two? [https://en.wikipedia.org/wiki/Container_(abstract_data_type)][1] [https://en.wikipedia.org/wiki/Collection_(abstract_data_type)][2] ...
What's the difference between the two abstract data types collection vs container?
|data-structures|abstract-data-types|
Let $P$ be a non-empty, finite set of points in $\mathbb{R}^n$. I want to know if there is an algorithm that can find the most optimal set of $n$-spheres $S$ that follows these properties: - Each $n$-sphere in $S$ overlaps with at least one of the other $n$-spheres in $S$, - For all $p \in P$, there exists $s \i...
null
As "all parameters have to be compared with each other at best once*, but not less than once", you must perform at least $\frac12 n(n-1)$ comparisons and you can do it in the systematic way, like in your example. There is no shortcut. *If you perform the same comparisons several times, there are two possibilities: ...
An example: You have 10 processors, and 1000 items that can be handled by any processor. So you let any processor handle 100 items, and you are finished after 100 time units. With a single processor, that processor would have to do all thousand tasks and take 1000 time units, 10 times more. Now you have another kin...
null
This problem can be split into two steps. 1. For each index $\ell$ with $a_\ell>0$, find the largest index $r$ such that $\sum_{k=\ell}^ra_k>0$. Denote that $r$ by $c[\ell]$, which reads "the companion index of $\ell$". $(\ell, c[\ell])$ is the longest "positive interval" that starts with $a_\ell$. We ca...
Given something similar to this. [![enter image description here][1]][1] Where you have **blocks** (the squares) and **entries** (the circles). Each block has a rating (the number inside the blocks) and is connected to other blocks. This topology is always fixed and **cannot** change, the ratings neither. An ...
Algorithm to distribute group of connected nodes in a graph
|algorithms|graphs|optimization|linear-programming|
Here is an $O(n)$-time solution that consists of the following two parts. #### Compute the prefix sums as well the "milestones" among them. 1. Compute the prefix sum array $s$, $s_i=\sum_{k=1}^ia_i$ for all $0\le i\le n$. This can be done by setting $s_0=0$ and looping $s_{i}=s_{i-1}+a_i$ for $i$ from $1$ to ...
Given something similar to this. [![enter image description here][1]][1] Where you have **blocks** (the squares) and **entries** (the circles). Each block has a rating (the number inside the blocks) and is connected to other blocks. This topology is always fixed and **cannot** change, the ratings neither. An ...
Given something similar to this. [![enter image description here][1]][1] Where you have **blocks** (the squares) and **entries** (the circles). Each block has a rating (the number inside the blocks) and is connected to other blocks. This topology is always fixed and **cannot** change, the ratings neither. An ...
Given something similar to this. [![enter image description here][1]][1] Where you have **blocks** (the squares) and **entries** (the circles). Each block has a rating (the number inside the blocks) and is connected to other blocks. This topology is always fixed and **cannot** change, the ratings neither. An ...
Given something similar to this. [![enter image description here][1]][1] Where you have **blocks** (the squares) and **entries** (the circles). Each block has a rating (the number inside the blocks) and is connected to other blocks. This topology is always fixed and **cannot** change, the ratings neither. An ...
null
An example: You have 10 processors, and 1000 items that can be handled by any processor. So you let any processor handle 100 items, and you are finished after 100 time units. With a single processor, that processor would have to do all thousand tasks and take 1000 time units, 10 times more. Now you have another kin...
There are many different aspects to this. It is quite common that you can perform an algorithm on one processor doing an amount x of work, and you can distribute it to several processors but do more work. Which is fine if your machine is otherwise idle. Unless it is a mobile device where a single core at lowest pos...
I think the problem might be $\mathsf{NP}$-difficult. Consider the decision version: > Input: an undirected graph $G = (V, E)$, a value function $f:V\to \mathbb{N}$, a subset $E = \{e_1, …, e_m\} \subseteq V$ of entries, vertices of degree $1$ and a rational $q$. > > Question: does there exists $m$ disjoint paths ...
About the language $L=\{<M>|M~is~a~TM~and~L(M)=\{0^n1^n|n\ge0\}\}$ I want to determine if it is in RE / coRE or neither. I think that I found a mapping reduction from $\overline{A_{TM}}$ to $L$, where $\overline{A_{TM}}=\{<M,w>|M~is~a~TM~and~M~does~not~accept~w\}$: Define $f(<M>)=<N_{M,w}>$ as follows: $N_...
$L=\{<M>|M~is~a~TM~and~L(M)=\{0^n1^n|n\ge0\}\}$
|complexity-theory|turing-machines|computability|automata|reductions|
null
Is it always the case that ${n \choose k} = O(n^k)$? If it is, then why does the comment from Clement C. in [this post][1] say it may not be when $k$ is a function of $n$? If it is not, then why is the asymptotic time complexity of the brute force algorithm for the Clique decision problem denoted as ${n \choose...
Some questions regarding asymptotic notation of ${n \choose k}$
|time-complexity|asymptotics|combinatorics|
Is it always the case that ${n \choose k} = O(n^k)$? If it is, then why does the comment from Clement C. in [this post][1] state it is only the case when $k$ is a constant? If it is not, then why is the asymptotic time complexity of the brute force algorithm for the Clique decision problem denoted as ${n \choos...
It is always true that $\binom{n}{k} =\mathcal{O}(n^k)$, even if $k$ depends on $n$. As a proof: $$\binom{n}{k} = \frac{n!}{k!(n-k)!} = \frac{(n-k+1)(n-k+2)…(n-1)n}{k!}\leqslant \frac{n^k}{k!}\leqslant n^k$$ Since the algorithm you talk of is an upper bound, the majoration is correct. What is wrong, however...
In terms of $n$, $$\binom nk=\frac{n(n-1)\cdots(n-k+1)}{k!}$$ is indeed a polynomial in $n$ of degree $k$ (and not of degree $\min(k,n-k)$). In terms of $k$ (and constant $n$), there is no asymptotic expression as $k\le n$ is required. If both parameters are variable, the expression is complicated: $$O\left(\f...
It is always true that $\binom{n}{k} =\mathcal{O}(n^k)$, even if $k$ depends on $n$. As a proof: $$\binom{n}{k} = \frac{n!}{k!(n-k)!} = \frac{(n-k+1)(n-k+2)…(n-1)n}{k!}\leqslant \frac{n^k}{k!}\leqslant n^k$$ Since the complexity of the algorithm you talk of is an upper bound, the majoration is correct. What...
In terms of $n$, $$\binom nk=\frac{n(n-1)\cdots(n-k+1)}{k!}$$ is indeed a polynomial in $n$ of degree $k$ (and not of degree $\min(k,n-k)$). In terms of $k$ (and constant $n$), there is no asymptotic expression as $k\le n$ is required. If both parameters are variable, the expression is complicated: $$O\left(\f...
$L_d$ is general reducible to $L_u$ but not map reducible to $L_u$. To show a language is not decidable, use general reduction is good enough. To show a language is RE or non-RE, you need to apply map reduction. In general reduction, $A\leq \bar{A}, A\leq B \Rightarrow A\leq \bar{B}, etc.$ However, in map reduction, $A...
$L_d$ is general reducible to $L_u$ but not map reducible to $L_u$. To show a language is not decidable, use general reduction is good enough. To show a language is RE or non-RE, you need to apply map reduction. In general reduction, $A\leq \bar{A}, A\leq B \Rightarrow A\leq \bar{B}, etc.$ However, in map reduction, $A...
I'm trying to come up with a performant layout algorithm for [web resources timings](https://developer.mozilla.org/en-US/docs/Web/API/Resource_Timing_API/Using_the_Resource_Timing_API). For now, all that is relevant is that resource timings include a `startTime` timestamp and a `duration` and the goal of this layout al...
Layout algorithm for resource timings
|algorithms|dynamic-programming|
It is always true that $\binom{n}{k} =\mathcal{O}(n^k)$, even if $k$ depends on $n$. As a proof: $$\binom{n}{k} = \frac{n!}{k!(n-k)!} = \frac{(n-k+1)(n-k+2)…(n-1)n}{k!}\leqslant \frac{n^k}{k!}\leqslant n^k$$ Since the complexity of the algorithm you talk of is an upper bound, the majoration is correct. What...
Here is a linear-time solution that consists of the following two parts. #### Compute the prefix sums and the "milestones" among them. 1. Compute the prefix sum array $s$, $s_i=\sum_{k=1}^ia_i$ for all $0\le i\le n$. This can be done by setting $s_0=0$ and then $s_{i}=s_{i-1}+a_i$ for $i$ from $1$ to $n$. 2....
One standard approach for package managers these days seems to be to use a SAT solver (or ILP solver). That seems like a plausible solution. All of your requirements can be expressed in SAT (or ILP). If you use SAT, you will need to constrain the number of packages selected, using a cardinality constraint: see ht...
The problem of minimizing the number of lanes is the problem of graph coloring (with the fewest number of colors) of an [interval graph](https://en.wikipedia.org/wiki/Interval_graph), namely, the graph with one vertex per resource timing and an edge between each pair of resource timings whose times overlap. Wikipedia ...
I'm trying to come up with a performant layout algorithm for [web resources timings](https://developer.mozilla.org/en-US/docs/Web/API/Resource_Timing_API/Using_the_Resource_Timing_API). For now, all that is relevant is that resource timings include a `startTime` timestamp and a `duration` and the goal of this layout al...
I'm trying to come up with a performant layout algorithm for [web resources timings](https://developer.mozilla.org/en-US/docs/Web/API/Resource_Timing_API/Using_the_Resource_Timing_API). For now, all that is relevant is that resource timings include a `startTime` timestamp and a `duration` and the goal of this layout al...
I'm trying to come up with a performant layout algorithm for [web resources timings](https://developer.mozilla.org/en-US/docs/Web/API/Resource_Timing_API/Using_the_Resource_Timing_API). For now, all that is relevant is that resource timings include a `startTime` timestamp and a `duration` and the goal of this layout al...
I have designed an algorithm (up to making a pseudocode) that accepts an array as input and finds in $O(n)$ time if there's a pair of elements (integers) in the array that have a sum zero. What my algorithm basically does is: 1. sort the array using merge sort (since it's quite efficient and takes only $O(logn)$) ...
How do I prove correctness of my algorithm that finds a pair of integers in an array that have a sum of 0?
|algorithms|search-algorithms|correctness-proof|search|program-correctness|
While this problem can be represented as interval graphs, it is not necessary to solve it. Moreover, an efficient algorithm to solve the problem directly on interval graphs is LexBFS, but it is quite hard to implement. Of course, it is simpler if you already know the corresponding intervals set, but in this case, worki...
I have designed an algorithm (up to making a pseudocode) that accepts a sorted array as input and finds in $O(n)$ time if there's a pair of elements (integers) in the array that have a sum zero. What my algorithm basically does is: 1. set index $i$ = 0 2. set index $j$ = length(array) - 1 3. while $i<j$ : if $i+...
I have designed an algorithm (up to making a pseudocode) that accepts a sorted array as input and finds in $O(n)$ time if there's a pair of elements (integers) in the array that have a sum zero. What my algorithm basically does is: 1. set index $i$ = 0 2. set index $j$ = length(array) - 1 3. while $i<j$ : if $a[...
You can prove this property using a [loop invariant](https://en.wikipedia.org/wiki/Loop_invariant). Naming the array $a$ and $n = |a|$, consider the following property $(P)$: > There exist $(k,\ell)\in \{0, …, n-1\}, k\neq \ell$, such that $a[k] + a[\ell] = 0$ if and only if There exist $(k,\ell)\in \{i, …, j\}, k\n...
Here is a linear-time solution that consists of the following two parts. #### Compute the prefix sums and the "milestones" among them. 1. Compute the prefix sum array $s$, $s_i=\sum_{k=1}^ia_i$ for all $0\le i\le n$. This can be done by setting $s_0=0$ and then $s_{i}=s_{i-1}+a_i$ for $i$ from $1$ to $n$. 2....
Let $S$ be language $S=${$\langle M\rangle |(\forall x \in \Sigma^*)[x \in L(M) \iff x^R \in L(M)]$}. How can I show that $L_U \le_m S$ and $L_U \le_m \bar S$ where $L_U$ is universal language and $\le_m$ means it is m-reducible (or simply reducible)? Can it be done via Rice theorem?
How to reduce universal language to language of all turing machines that deduce all palindromes?
|turing-machines|np-complete|
While I was initialy convinced by the two previous answers, a colleague of mine showed me the light about this exercise. He claims that the maximum value of the counter is $110$, not $20$. As proof, consider the following execution : - Thread $t$ reads $i = 0$; `counter = 0` - Thread $u$ executes $10$ loops i...
#### Show $L_U \le_m S$ Let $v=\langle T, w\rangle$, where $T$ is a Turing machine and $w$ is an input string. Fix two different input symbol $a$ and $b$. Construct Turing machine $M_v$ that upon input $x$, check whether $x$ is $awb$. - If it is, accept. - Otherwise, check whether $x$ is $bwa$. - If i...
{"Voters":[{"Id":79440,"DisplayName":"Nathaniel"}]}
While I was initialy convinced by the two previous answers, a colleague of mine showed me the light about this exercise. He claims that the maximum value of the counter is $65$, not $20$. As proof, consider the following execution : - Thread $t$ reads $i = 0$; `counter = 0` - Thread $u$ executes $10$ loops in...