instruction
stringlengths
0
29.8k
⌀
I've got homework to design NFA to accept a set of strings over {a,b,c} in which each string of the language satisfies: "cac" is a substring and "cc" is not a substring and the length of the string is even. I tried to approach the problem by looking at the shortest strings of the language and building a template for...
Desgining NFA under certain constraints
|regular-languages|automata|finite-automata|
null
The problem is as follows: > The input is an array $A$ of $n$ natural numbers such that: > > (1) if the maximum occurs in $A[p]$ for an index $p$, then $A[1..p] \leq A[p]$ and $A[p] \geq A[p+1..n]$; > > (2) if $A[i]=A[j]=x$ then $A[k]=x$ for $i \leq k \leq j$. (Forgive me for the abuse of notation in (1), I m...
The problem is as follows: > The input is an array $A$ of $n$ natural numbers such that: > > (1) if the maximum occurs in $A[p]$ for an index $p$, then $$A[1] \leq \ldots \leq A[p-1] \leq A[p]$$ and $$A[p] \geq A[p+1] \ldots \geq A[n];$$ > > (2) if $A[i]=A[j]=x$ then $A[k]=x$ for $i \leq k \leq j$. The goal i...
The problem is as follows: > The input is an array $A$ of $n$ natural numbers such that: > > (1) if the maximum occurs in $A[p]$ for an index $p$, then $$A[1] \leq \ldots \leq A[p-1] \leq A[p]$$ and $$A[p] \geq A[p+1] \ldots \geq A[n];$$ > > (2) if $A[i]=A[j]=x$ then $A[k]=x$ for $i \leq k \leq j$. The goal i...
null
null
The problem is as follows: > The input is an array $A$ of $n$ natural numbers such that: > > (1) if the maximum occurs in $A[p]$ for an index $p$, then $$A[1] \leq \ldots \leq A[p-1] \leq A[p]$$ and $$A[p] \geq A[p+1] \ldots \geq A[n];$$ > > (2) if $A[i]=A[j]=x$ then $A[k]=x$ for $i \leq k \leq j$. The goal i...
I'm surprised that no one mentioned it, but there is a very good metric to measure the running time of the insertion sort: it's the number of inversions, i.e. the pairs $(i,j)$ such that $i<j$ and $a_i > a_j$. Denoting the number of inversions as $I$, insertion sort does $I$ swaps and $O(n) + I$ comparisons. On the...
Given a bipartite graph, find a set of nodes on one side that has greater cardinality than the set of its neighbours on the other side. This is a conceptually simple problem, but I suspect it is actually quite difficult.
Algorithm to find a set of nodes with a smaller set of neighbours in a bipartite graph
|algorithms|graphs|bipartite-graph|
Assume we have a deterministic infinite automaton. $DIA = (Q, δ, q_0, F)$. Meaning there is no limit to the number of states. I want to prove or disprove that this module equals a normal DFA. **Attempt** \ I tried to disprove it using the language L = {$a^nb^n|0 < n ∈ N$}, but I am not sure if this is enough to dis...
Deterministic infinite automaton equals a normal DFA?
|formal-languages|regular-languages|automata|
The hypothesis is correct, and the greedy approach is indeed optimal. Here's why: Let's say Karl has m machines and w workers, and in one pass he can make m*w candies. Now, assume Karl decides to spend some of his candies to either buy a new machine or hire a new worker. Let's consider two cases: Case 1: Karl buy...
Let $A,B$ be the two sides of the bipartite graph, and $A$ the side we are intereseted in (we are looking for a subuset of $A$). Hall's characterization of perfect matchings states that $G$ admits a perfect matching saturating $A$ if and only if for each subset $X\subseteq A$ it holds that $|N(X)|\geq |X|$. If the g...
null
That approach indeed works. It is well known that $L = \{a^n b^n \mid n \in \mathbb N\}$ is not regular and hence not recognized by any "normal" DFA, so it suffices to show that $L$ can be recognized by a DIA. I'll leave the details to you, but using infinite states we can remember the number of $a$ we read so far and ...
Yes, since we already know that $L = \{a^n b^n\}$ is not regular (using the pumping lemma), and that a language is regular if and only if it admits a DFA, it suffices indeed to show the inequivalency by definining a $DIA$ for $L$. Intuitively, $L$ is able to keep arbitrary amount of information about history using addi...
Yes, since we already know that $L = \{a^n b^n\}$ is not regular (using the pumping lemma), and that a language is regular if and only if it admits a DFA, it suffices indeed to show the inequivalency by definining a $DIA$ for $L$. Intuitively, $L$ is able to keep arbitrary amount of information about history using addi...
Let us consider the recurrence $$T(n)=3T\left(\left\lfloor\frac n3\right\rfloor\right)+n.$$ We can show that the function $T$ is strictly growing because by complete induction, if $T$ is increasing up to $3m$, $$T(3m)=3T(m)+3m\\ <T(3m+1)=3T(m)+3m+1\\ <T(3m+2)=3T(m)+3m+2\\ <T(3m+3)=3T(m+1)+3m+3$$ so that ...
Partial answer: The problem admits an $O(\log n)$ algorithm. The algorithm $\mathcal A$ works as follows: 1. Query $x = A[n/3]$ and $y = A[2n/3]$. 2. If $x = y$, return $\max \{x, \mathcal A(A[1, n/3]), \mathcal A(A[2n/3, n])\}$ 3. If $x < y$, return $\max \{\mathcal A(A[n/3, 2n/3]), \mathcal A(A[2n/3, n])\}...
I'm reading a book which says that to transform a number from decimal to binary with fixed point representation you have to follow these steps: 1. Take the integer part (the one before the point) of the number and transform it in binary like you normally do with integer numbers: that is the integer part of the bina...
null
null
**computation that halts…** “the Turing machine will halt whenever it enters a final state” (Linz:1990:234) **Every "rebuttal" simply ignores this key fact**<br> ⟨Ĥ⟩ ⟨Ĥ⟩ correctly simulated by embedded_H cannot possibly reach its own simulated final state of ⟨Ĥ.qn⟩ and halt in any finite number of steps of correct ...
null
If you want IEEE 64 but floating point numbers, you need 53 mantissa bits. If the integer part is non-zero and has k bits, you need 53-k bits from the fractional part. If the integer part is zero, then you ignore the initial zero bits from the fractional part, and you need 53 bits starting with the first non-zero bit. ...
No, you have not done anything wrong. However, you have not done enough. You have done step 2 for 18 times. You will have to repeat step 2 for another 82 times. Then you will observe that the "obtained numbers start to repeat". The binary representation of $0.264$ is $$0.\underline{01000011100101011000000100000...
Compare bubblesort with a selection sort that looks for the largest element and moves it to the end. You will see that both actually make the exact same comparisons, finding the largest array element. Bubblesort also moves elements around so the indices it compares constantly change, but the values compared are the...
I'm reading a book which says that to transform a number from decimal representation to binary representation you have to follow these steps: 1. Take the integer part (the one before the point) of the number and transform it in binary like you normally do with integer numbers: that is the integer part of the binary...
How does Decimal to Binary conversion algorithm work?
|binary|number-formats|base-conversion|
If you want IEEE 64 bit floating point numbers, you need 53 mantissa bits. If the integer part is non-zero and has k bits, you need 53-k bits from the fractional part. If the integer part is zero, then you ignore the initial zero bits from the fractional part, and you need 53 bits starting with the first non-zero bit. ...
Let’s say with k bits we have n values. If we add a bit, then we have n values where the added bit is set, and n values where the added bit is not set. Total 2n values. Every bit doubles the number of possible values. 0 bits: one possible value of nothing. 1 bit: twice as many values, that is two. 2 bits:...
Given a set of `n` points, and a number `k` decide whether there exist `k` straight lines such that all of the points are covered. Here a point is *covered* by a line if that point lies on that line. I can formulate an ILP for the same. The problem seems to be hard. I want to formally prove its NP-Completeness. N...
Multi-line fitting problem
|np-complete|computational-geometry|
I was going through the [main idea][1] of solution to this [problem][2]. I could not assimilate the idea of reversing the edges and then using that graph to check if node 1 is reachable from all others. How come reversing the graph not affect the original conditions and is applicable to be used for the intended pur...
How reversing the edges of the graph not altering the original conditions?
|algorithms|graphs|data-structures|connected-components|
null
Fix some vertex $u$. Consider the following observations: * A path that ends at vertex $u$, which originates from some other vertex $v$ will be a path that originates from $u$ and ends at $v$ in the reversed graph. * You can test if a vertex $u$ reaches all other vertices $v$ via DFS starting at $u$ in the origin...
For $k=2$, consider all pairs of points in turn and remove all points that are collinear with them. Then solve the $k=1$ problem for the remaining points. This takes time $O(n^4)$. More generally, $$T(n;k)\le n^3T(n;k-1)$$ which is polynomial in $n$.
I have very large sets $A,B$ of 4-tuples of integers, and I would like to find $(x_1,y_1,z_1,w_1)\in A,(x_2,y_2,z_2,w_2)\in B$ such that $$x_1x_2 + y_1y_2 + z_1z_2 + w_1w_2 = 0.$$ Of course the naive $|A||B|$-time algorithm is too slow for me. Is there a better algorithm? In 2D you can normalize all the vectors ...
Find orthogonal (integer) vectors in set
|algorithms|number-theory|linear-algebra|
Consider all pairs of points in turn and detect all those that are collinear with them. This takes time $O(n^3)$ and will generate up to $\dfrac n2$ subsets. This reduces the question to a set cover decision problem.
Consider all pairs of points in turn and detect all those that are collinear with them. This takes time $O(n^3)$ and will generate up to $\dfrac{(n-1)n}2$ subsets. This reduces the question to a set cover decision problem. For $k=2$, the brute force solution takes $O(n^5)$.
**Applying a Simulating Halt Decider to the Linz Halting Problem Proof** A simulating (at least partial) halt decider correctly predicts whether or not its correctly simulated input can possibly reach its own final state and halt. It does this by correctly recognizing several non-halting behavior patterns in a finit...
The general case of this problem is NP-complete, as shown by Meggido and Tamir [N. Megiddo and A. Tamir. _On the complexity of locating linear facilities in the plane._ Oper. Res. Lett., 1:194–197, 1982.]. Their reduction is from 3SAT, in the usual way: some points in the construction represent clauses, others variable...
Let $A,B$ be the two sides of the bipartite graph, and $A$ the side we are intereseted in (we are looking for a subuset of $A$). Hall's characterization of perfect matchings states that $G$ admits a perfect matching saturating $A$ if and only if for each subset $X\subseteq A$ it holds that $|N(X)|\geq |X|$. If the g...
Consider all pairs of points in turn and detect all those that are collinear with them. This takes time $O(n^3)$ and will generate up to $\dfrac{(n-1)n}2$ subsets. This reduces the question to a set cover decision problem. For $k=2$, the largest subset must have a size at least $\dfrac n2$ (because two subsets have ...
Consider all pairs of points in turn and detect all those that are collinear with them. This takes time $O(n^3)$ and will generate up to $\dfrac{(n-1)n}2$ subsets. This reduces the question to a set cover decision problem. For $k=2$, the largest subset must have a size at least $\dfrac n2$ (because two subsets may h...
null
null
null
null
null
I am watching MIT's [Introduction to Data Structure][1] and there is something I'm not understanding at the 14:48 mark Why does the machine word size, w have to be >= lg(n) where n is the length of a static array? As I understand a static array, `[1,2,3]` is stored consecutively in the RAM like: $$|*|*|1|2|3|*...
Why does the machine word size need to be bigger than clog(n)
|algorithms|time-complexity|data-structures|
Let's say I have a set of non-repeating arcs, e.g. `(0,1), (3,0), (0,3), (1,2), (2,3)`, ("set" because they don't repeat). Note that the path might not be unique (and that's fine). And I don't care where we start in the path, as long as all arcs in the set are traversed. Given a directed adjacency matrix, ho...
How to recover path, given unordered set of non-repeating arcs for directed graph?
|graphs|
Maybe the lecture is assuming the [Transdichotomous RAM Model](https://en.wikipedia.org/wiki/Transdichotomous_model). Under this assumption and with respect to algorithms involving arrays, you can access elements in $O(1)$, since this allows storing of indices in a single machine word. This $O(1)$ access time is quite ...
Maybe the lecture is assuming the [Transdichotomous RAM Model](https://en.wikipedia.org/wiki/Transdichotomous_model). Under this assumption and with respect to algorithms involving arrays, you can access elements in $O(1)$, since this allows storing of indices in a single machine word. Because there are $n$ possible el...
The [combinatorial number system][1] shows that there is a bijection between the natural numbers less than $n \choose k$ and $n\choose k$ combinations. There is a [greedy algorithm][2] for [unranking][3] combinations, which one may calculate the worst-case run time for. I'm curious: is this the fastest algorithm in ter...
What is the worst case time complexity of unranking n choose k combinations (combinatorial number system, combinadics)
|algorithms|time-complexity|runtime-analysis|combinatorics|rank|
In the [counting number of islands problem](https://leetcode.com/problems/number-of-islands/description/), I noticed that one of the solution from internet is as follows ``` def visitIslandDFS(grid, i, j): if (i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0])): return # return, if it is not a v...
Number of islands
|algorithms|python|
null
The above two cases are redundant if one try to solve the problem by looping over all possible grids and running dfs . Another approach to solve the problem is by storing all the unvisited grids into a queue, iteratively running a dfs until the queue is empty, in this case we can't be sure about the order in which th...
You can use a "counting bloom filter" which uses integers instead of bits for each item stored. Increase integer when adding, decrease integer when deleting. Uses more storage, of course. https://en.wikipedia.org/wiki/Counting_Bloom_filter
Imagine having an island of the form: ``` ..X X.X XXX ``` The solution starts looking at the island in the top right cell (smallest $i$). If you never go left or up, you would only visit the three right cells during the first DFS and count them as the first island, and later when you do a second DFS you fi...
null
Take for example a word size of 32 bits. A word can hold values from 0 to $2^{32}-1$. Now you want an array with $2^{32}+1$ elements. According to the formula, the word size is too small or the array size is too large. And you can see that you need an index of $2^{32}$ to access the last array element, which do...
As far as I understand, when doing this operation, we first need to identify the subnormal value, normalise it, adjust the exponent, and then multiply the significands. Wouldn't it be advantageous to just multiply the significands and use a leading-zero counter on the result? It seems like removing some steps could spe...
Multiplication of subnormal and normal numbers under IEEE 754
|floating-point|
You can understand that $n=2^b$ numbers are stored at addresses $0$ to $n-1=2^b-1$, which can be represented with $b$ bits. (Though indeed $n$ takes $b+1$ bits). But what matters is that the word size is a $\Theta(\log(n))$ function.
null
You understand that $n=2^b$ numbers are stored at addresses $0$ to $n-1=2^b- What matters in this discussion is that the word size is a $\Theta(\log(n))$ function.
The general case of this problem is NP-complete, as shown by Meggido and Tamir [N. Megiddo and A. Tamir. _On the complexity of locating linear facilities in the plane._ Oper. Res. Lett., 1:194–197, 1982.]. Their reduction is from 3SAT, in the usual way: some points in the construction represent clauses, others variable...
You have to check not only for denormalised numbers but also for zeroes because inf x denormalised = inf, but inf x 0 = NaN. Someone might have tried this and rejected it. Someone might actually implementing it and tells nobody. You’d have to talk to some engineers at Intel, AMD, Arm, ibm, apple to find out. Bu...
If you want IEEE 64 bit floating point numbers, you need 53 mantissa bits. If the integer part is non-zero and has k bits, you need 53-k bits from the fractional part. If the integer part is zero, then you ignore the initial zero bits from the fractional part, and you need 53 bits starting with the first non-zero bit. ...
You have to check not only for denormalised numbers but also for zeroes because inf x denormalised = inf, but inf x 0 = NaN. And you can use the usual method for division as well, where it is extra valuable. Much better to use the exact same first part of the hardware, you need to design it only once. Someone might...
It’s the other way round: it’s not the array size determining the word size. It’s the word size determining the maximum possible array index and therefore the maximum array size. Take for example a word size of 32 bits. A word can hold values from 0 to $2^{32}-1$. Now you want an array with $2^{32}+1$ elements....
If you avoid the up and left displacements, the algorithm will only fill down and right of the seed pixel and can fail to cover whole blobs. E.g. from the top left pixel . x . x x x x x will be filled as . o . x x o o o
null
Either the rightmost node of the left tree or the leftmost node of the right tree can be used as a common root.
Let's say I have a set of non-repeating arcs, e.g. `(0,1), (3,0), (0,3), (1,2), (2,3)`, ("set" because they don't repeat). Here's an example of a graph for which this series of arcs would form a valid path: [![Graph][1]][1] And the valid path would be `(0,1), (1,2), (2,3), (3,0), (0,3)`. Note that the path ...
Let's say I have a set of non-repeating arcs, e.g. `(0,1), (3,0), (0,3), (1,2), (2,3)`, ("set" because they don't repeat). Here's an example of a graph for which this series of arcs would form a valid path: [![Graph][1]][1] And a valid path would be `(0,1), (1,2), (2,3), (3,0), (0,3)`. Note that the path mi...
Let $E$ be the set of your edges, and let $V$ be the set of all the edge's endpoints. Consider the graph $G=(V,E)$. You're asking for an [Eulerian trail of $G$][1]. [One possible way][2] to so so, is the following: assume that $G$ is connected (otherwise there is no Eulerian trail) and that all vertices have even ...
Let $E$ be the set of your edges, and let $V$ be the set of all the edge's endpoints. Consider the graph $G=(V,E)$. You're asking for an [Eulerian trail of $G$][1]. [One possible way][2] to so so, is the following: assume that $G$ is connected (otherwise there is no Eulerian trail) and that all vertices have even ...