instruction
stringlengths
0
29.8k
⌀
You haven't really defined your computation model, so here is a suggestion. The input to the algorithm is a sorted array $A$ of length $n$ and an element $x$. The output is either a position $i$ such that $A[i] = x$, or $\bot$, which means that $x$ is not in the array. An algorithm is a decision tree whose lea...
#### the Idea Find all edges that are on one of the shortest paths from $s$ to $t$. Find the bridges in the graph that consists of those edges. #### the Approach 1. Use breadth-first-search to compute $d_s(u)$, the distance from $s$ to $u$ for each vertex $u$. If $t$ is found not reachable from $s$, return nothing...
Verify if array is orthogonal
null
[Orthogonal arrays][1] often appear in probabilistic algorithms. They can be efficiently constructed from, e.g., [BCH codes][2]. But is there an efficient algorithm that can verify if an array is orthogonal? I would be particularly curious about the strength-4 *binary* arrays. In my last edit I added that the arra...
Let p be a prime. Since there are infinitely many primes, p is followed by n composite numbers for some n dependent on p, and then by another prime q. After processing $a^p$ you are in a state S that is rejecting. In that state, if you process further a's, then the next n states are accepting, and the state after t...
I misunderstanding about some logarithm property in algorithm course: is it correct that we say following three term is equivalent? $O(\log a + \log b)$ $O(\log (ab))$ $O(\log (a+b))$
equivalency of some facts in $O$ notation
|algorithms|time-complexity|data-structures|asymptotics|discrete-mathematics|
I need to prove: reverse(push(as,bs)) = push(reverse(bs), reverse(as)) Where: def push[T](as: List[T], bs: List[T]): List [T] = as match {case Nil => bs case x::xs => x::push(xs, bs) } def reverse[T](ls: List[T]): List[T] = ls match { case Nil => Nil case x::xs => push(re...
Structural induction proof, search for a lemma
|programming-languages|tail-recursion|
null
I am confused with the terms inhabitant vs. subtyping. For example, 1. We usually think that "john is an inhabitant of Human". This sentence is correct, because john is an individual, and an individual belongs to the set Human (that reflects the meaning of inhabitant). However, if we think of john as a subset wi...
What is the difference between type inhabitant and subtyping?
|type-theory|formal-methods|ontologies|
Let p be a prime. Since there are infinitely many primes, p is followed by n composite numbers for some n dependent on p, and then by another prime q. After processing $a^p$ you are in a state S that is rejecting. In that state, if you process further a's, then the next n states are accepting, and the state after t...
|type-theory|formal-methods|knowledge-representation|ontologies|
Let $a>b>0$. From $\log(a+b)=\log(a)+\log\left(1+\dfrac ba\right)$, we draw $$\log(a)\le\log(a+b)\le \log(a)+\log(2)$$ and similarly for $b>a$.
null
Let p be a prime. Since there are infinitely many primes, p is followed by n composite numbers for some n dependent on p, and then by another prime q. After processing $a^p$ you are in a state S that is rejecting. In that state, if you process further a's, then the next n states are accepting, and the state after t...
I have the following problem: There are $n$ variables $x_i$, $i=1...n$, each can take integer values from 1 to $m$. For every set of values I can run a test which has a binary outcome ('Pass' or 'Fail'), and it is known that the test outcome only depends on a value of a single variable being larger than some thresho...
optimal search algorithm for finding parameters and thersholds
|algorithms|search-algorithms|search-problem|binary-search|
I have a large system of $3$-$XORSAT$ constraints (i.e. up to $3$ variables per constraint) and this can be represented in matrix form as a linear algebra problem $Ax=b$ $mod$ $2$. Solvability (i.e. the satisfiability of the $3$-$XORSAT$ system) can be tested by using Gaussian elimination which involves multiple rounds...
Can sb. explain to me what the :: indicates in the following predicate? $$(∀ i : P(i) : Q(i)) ≡ (∀ i :: P(i) ⇒ Q(i))$$
What does :: indicate in all-quantor predicates?
|logic|
Suppose you have an $n\times n$ matrix and you want to find a local minimum. To find it you scan the middle row and column and identify a minimum. If it is a local minimum, you're done; if not, you recursively search the sub-quadrant where the smaller value was found. Now I can analyze the runtime in two differe...
Find matrix local minimum - two analysis which seem to get contradictory runtimes
|algorithms|algorithm-analysis|master-theorem|
Suppose you have an $n\times n$ matrix and you want to find a local minimum. To find it you scan the middle row and column and identify a minimum. If it is a local minimum, you're done; if not, you recursively search the sub-quadrant where the smaller value was found. Now I can analyze the runtime in two differe...
I need to prove: reverse(push(as,bs)) = push(reverse(bs), reverse(as)) Where: def push[T](as: List[T], bs: List[T]): List [T] = as match { case Nil => bs case x::xs => x::push(xs, bs) } def reverse[T](ls: List[T]): List[T] = ls match { c...
I need to prove: reverse(push(as,bs)) = push(reverse(bs), reverse(as)) where: def push[T](as: List[T], bs: List[T]): List [T] = as match { case Nil => bs case x::xs => x::push(xs, bs) } def reverse[T](ls: List[T]): List[T] = ls match { c...
Structural induction proof for reverse(push(as, bs)) = push(reverse(bs), reverse(as))
|programming-languages|induction|tail-recursion|
In the first analysis note that, $T(n) = 2n + n + \frac n 2 + \frac n 4 +... + \frac n {2^k} \leq n (2+1+\frac 1 2 + \frac 1 4 +... ) \leq 4n =O(n) $
I think what would help a lot is proving first that `push(push(a, b), c) = push(a, push(b, c))`. Once you have that result, you can make an induction on `as` only in the equality `reverse(push(as,bs)) = push(reverse(bs), reverse(as))`. I will add some details if necessary.
null
In the first analysis note that, $T(n) = 2n + n + \frac n 2 + \frac n 4 +... + \frac n {2^{\lg n}} \leq n (2+1+\frac 1 2 + \frac 1 4 +... ) \leq 4n =O(n) $
(a || b) || (c && d)) Given the above, how do I derive the table below: | a | b |c | d| output | | - | - | - |- | -| | T | - | - | - |TRUE| |F|T|-|-|TRUE| |F|F|T|T|TRUE| |F|F|T|F|FALSE| F|F|F|-|FALSE| I'm told that this is short circuit evaluation, basically, we take away some branches/paths that always ...
How to come up with combination a short-circuit evaluation table?
|logic|boolean-algebra|decision-tree|
Can somebody explain to me the meaning of the :: indicates in the following predicate? $$(\forall i : P(i) : Q(i)) \equiv (\forall i :: P(i) \implies Q(i))$$
What does :: indicate in all-quantifier predicates mean?
|logic|first-order-logic|notation|
"::" is ":" followed by another ":", two separators. "$\forall i :: P(i) \implies Q(i)$" is the same as "$\forall i : \text{nothing}: P(i) \implies Q(i)$".
null
"::" is ":" followed by another ":", i.e., two separators. "$\forall i :: P(i) \implies Q(i)$" is the same as "$\forall i :    : P(i) \implies Q(i)$", i.e, there is no premise/second term. Or you can consider the second term as $\text{true}$ or $\top$. This situation is similar to the slice notation for lists i...
Can somebody explain to me the meaning of $::$ in the following predicate? $$(\forall i : P(i) : Q(i)) \equiv (\forall i :: P(i) \implies Q(i))$$
What does :: indicate in all-quantifier predicates?
I read a survey paper a few years ago that seems relevant, "[Successful SAT Encoding Techniques](https://content.iospress.com/articles/journal-on-satisfiability-boolean-modeling-and-computation/sat190085)" by Magnus Björk. Abstract: >This article identifies good practices for SAT encodings by analysing interviews...
null
Looking at your question again, I think you really may only need two simple rules. After reviewing these rules, then we can see how to build a table. Note that I will write $$(a || b) = (a \lor b)$$ $$(a \&\& b) = (a \land b)$$ --- When you have logical or equations: $$\tag{1}(a \lor b)$$ You should k...
Define the *load factor* of a hash table with open addressing to be $n/m$, where $n$ is the number of elements in the hash table and $m$ is the number of slots. It can be shown that the expected time for doing an insert operation is $\frac{1}{1-\alpha}$, where $\alpha$ is the load factor. If $\alpha$ is bounded to som...
null
Define the *load factor* of a hash table with open addressing to be $n/m$, where $n$ is the number of elements in the hash table and $m$ is the number of slots. It can be shown that the expected time for doing an insert operation is $\frac{1}{1-\alpha}$, where $\alpha$ is the load factor. If $\alpha$ is bounded to som...
optimal search algorithm for finding parameters and thresholds
null
No, it's not possible to use fewer steps than that. Each test gives you one bit of information about the solution. There are $nm$ possible solutions, so you need $\lg(nm)$ bits of information to uniquely identify the solution -- which means you need at least $\lg(nm)$ tests.
To solve this problem in $O(n\log n)$, we can use a min-heap. Consider $s_i = \Sigma_{k=i}^{n}{a_k}$ and $b_i = s_i + c_i - a_i$. You can see that if $b_i < s_j$ for $j < i$, by starting from $j$ and breaking every container from $j$ to $i$, container $i$ will break by the amount of water from the containers $j$ to ...
null
There is this certain slide in Coursera Computer Science: Algorithms, Theory, and Machines course: [![enter image description here][1]][1] [1]: https://i.stack.imgur.com/FuN50.png I think it is saying finding the optimal size of given data is undecidable. However, I thought there is a theorem in information th...
Undecidability in optimal data compression
|turing-machines|undecidability|information-theory|data-compression|
I just had an exam. There is the same question, we being ask to find all vertex that not part of cycle. What I did is, I run DFS search to the node not being visited, and in each DFS search I kept an array track parent so that each time I met a back edge, I will update it. If there is no cycle, I will left a no cycl...
A simple mental model of a PDA or NPDA is **a finite state machine plus a stack**. Both parts can be used to remember which kind has been the input taken by the automaton so far. The finite-state-machine part can remember bounded-many information. The stack can remember unbounded-many information (but for any parti...
null
null
Can’t a PDA get stuck in a cycle of blank transitions? Should the implementation detect such cycles and do something about them? That seems quite complex to consider all the edge cases. Does the answer vary for nondeterministic PDAs?
Why do PDAs always halt?
|formal-languages|automata|pushdown-automata|halting-problem|
Implementations will typically store the hash value inside the table - this will save lots of hash value calculations. For the hash value of the key being looked up, it depends on the caller how often that value is calculated. Hash values can be cached. I might have an object with a name, address, and age. And name...
The length of the shortest program to produce a given string is known as the [Kolmogorov complexity](https://en.wikipedia.org/wiki/Kolmogorov_complexity) of that given string. (Of course, some details are needed to give a formal definition, but this is the idea.) A proof of undecidability goes roughly as follows: - Th...
Some time go I had in one of the big tech interviews the following question that I still don't know how to approach it. You have a chains of reservations from AirBnb: reservations:[ {user = 1, res_id = 1001, checkin = 100, checkout 101 }, {user = 2, res_id = 1002, checkin = 104, checkout 105...
Show all chains per user
|algorithms|data-structures|
What are some examples of non-enumerable languages whose complement isn't either? I.e., a language L such that L is not Turning-recognizable and L’ is not Turing-recognizable either. Update: Found some examples: - https://cs.stackexchange.com/questions/80924/is-the-below-language-non-r-e/80947
What are some examples of non-enumerable languages whose complement isn't either?
|turing-machines|computability|turing-recognizable|recursively-enumerable|
null
Some time go I had in one of the big tech interviews the following question that I still don't know how to approach it. You have a chains of reservations from AirBnb: reservations:[ {user = 1, res_id = 1001, checkin = 100, checkout 101 }, {user = 2, res_id = 1002, checkin = 104, checkout 105...
Some time go I had in one of the big tech interviews the following question that I still don't know how to approach it. You have a chains of reservations from AirBnb: reservations:[ {user = 1, res_id = 1001, checkin = 100, checkout 101 }, {user = 2, res_id = 1002, checkin = 104, checkout 105...
Some time go I had in one of the big tech interviews the following question that I still don't know how to approach it. You have a chains of reservations from AirBnb: reservations:[ {user = 1, res_id = 1001, checkin = 100, checkout 101 }, {user = 2, res_id = 1002, checkin = 104, checkout 105...
null
I was watching this video https://www.youtube.com/watch?v=whjt_N9uYFI and at 1:04:15 the teacher says that a^(log[b]n) is the same as n^(log[b]a). Can someone please explain me why bringing the "n downstairs" and the "a" "upstairs" is possible???
Why in a^(log[b]n) is the same as n^(log[b]a)?
|asymptotics|recurrence-relation|
Take $\log_b$, and you get $\log_b n \cdot \log_b a$ on both sides.
$$p^{\log(q)}=e^{\log(p)\log(q)}=q^{\log(p)}$$ holds (also for other bases).
I need to write a function that gets an array of numbers $a$ as an input and returns an index $i$ such that $a[i]<a[i+1]$ if it exists, if such $i$ doesn't exist return $-1$. (return any index $i$ if theres multiple). <br> We know that: in $a$ there's atleast $2$ elements, $a$ IS NOT sorted and the first element in $a...
How can I apply binary search in this question? (unsorted array with extra information)
|algorithms|search-algorithms|arrays|binary-search|
null
I was watching this video https://www.youtube.com/watch?v=whjt_N9uYFI and at 1:04:15 the teacher says that $a^{log_b n}$ is the same as $n^{log_b a}$. Can someone please explain me why bringing the "n downstairs" and the "a" "upstairs" is possible???
Why in $a^{log_b n}$ is the same as $n^{log_b a}$?
null
Why is $a^{log_b n}$ the same as $n^{log_b a}$?
I was watching video [*Lec 2 | MIT 6.046J / 18.410J Introduction to Algorithms (SMA 5503), Fall 2005*](https://youtu.be/whjt_N9uYFI?t=3859), where professor Erik Demaine said that $a^{log_b n}$ is the same as $n^{log_b a}$. Can someone please explain me why bringing "the $n$ downstairs" and "the $a$ upstairs" is possib...
We can say some number $a = b^{\log_b(a)}$, because $\log_b(a)$ tells us to how much power we need to raise $x$ to get $a$ and then we actually raise it to get $a$. Then, $$a^k = (b^{\log_b(a)})^k = b^{k\log_b(a)}$$ If $k = \log_b(n)$, then $$a^{\log_b(n)} = b^{\log_b(n)\log_b(a)}$$ Notice that $n = b^{\log_b(...