instruction
stringlengths
0
29.8k
⌀
Given a set of nodes $N$ on an undirected, weighted graph $G$ and a query node $n$, what is a fast algorithm for finding the node in $N$ that is closest to $n$? Furthermore, say we are doing many of these queries, so have many $n$'s, is there some data structure that could speed up this computation? Thank you for...
Given a set of nodes $N$ on an undirected, weighted graph $G$ and a query node $n$, what is the fastest algorithm for finding the node in $N$ that is closest to $n$? Furthermore, say we are doing many of these queries, so have many $n$'s, is there some data structure that could speed up this computation? Thank yo...
null
As suggested in your comment, the task can be done directly without having to modify values to ints. Input: A tuple `A` of `n` rows and `m` columns. Output: Whether there is matching $4$ "O"'s or 4 "X"'s on a diagonal, horizontal, or vertical line. Procedure: ``` # horizontal lines For i in range(n): F...
As suggested in comments, the task can be done directly without having to modify values to ints or change the structure of the input. Input: A tuple `A` of `n` rows and `m` columns. Output: Whether there is matching $4$ "O"'s or 4 "X"'s on a diagonal, horizontal, or vertical line. Procedure: ``` # horizontal...
My algorithm homework revolves around matching 4 `"O"`'s or 4 `"X"`'s on a diagonal, horizontal, or vertical. Hopefully that makes sense... This is what the data looks like below (2D tuples) where I need to recreate a function that returns `"O"` or `"X"` or `False` depending on what is found. xwins = ((None, ...
Algorithm that finds 4 "O"s or 4 "X"s on a diagonal, horizontal, or vertical
|algorithms|arrays|python|
I was reading the proof of time-complexity for the Edmonds-Karp algorithm here (https://brilliant.org/wiki/edmonds-karp-algorithm/). Everything in the first part of the proof (The section Monotonically increasing path length) makes sense. However, the last part of it is not very convincing (the part I have highlight...
shortest path increases monotonically => a bound on the length of one iteration of Edmons-Karp is then O(E) ... Convince me this is true
|algorithms|time-complexity|correctness-proof|network-flow|
There is, in fact, a solution. Convert the name to a binary number such that A=1 and B=0. Then, either subtract each person’s name from infinite 1111111 or just remove all 0s before the first 1. Either way, the set becomes all binary numbers from 0 -> infinity. At that point, just add 1 and convert to decimal. T...
An infinite string is uniquely identified by a function $s:\mathbb{N}\rightarrow \{A,B\}$, such that $s(n)$ is the $n$'th character in the string. The number of such functions is known to be: $|\{A,B\}|^{|\mathbb{N}|}=2^{\aleph_0}$. Now, its also known that $2^{\aleph_0}>\aleph_0$, and hence there is no way to as...
Let's say we want to find the smallest positive integer *x* for which some property *A* holds. We know that such an integer exists. However, we have no knowledge about the scale of *x* (i.e. *x* could be 7 or 5'142 or 17 Quadrillion or whatever). We have a function *f* that returns true when evaluated at values larg...
Efficient sampling from all positive integers
|complexity-theory|sampling|
Suppose I have a Turing machine that takes as input any string of length $n$, where $n$ is odd, and the Turing machine returns the middle character of the string. What time complexity class is this in? Is it in $O(n/2)$ because the tape head must scan $n/2$ places to the right, counting as it goes, or is there a ...
Turing machine read time
|time-complexity|turing-machines|runtime-analysis|
null
There is a way, with a little pre-processing that you can do each test in a constant $O(1)$ number of operations, assuming that you have large enough numbers that can store many bits. --- You can actually store the values as bits: - Store X's as 11 - Store O's as 00 - Store None as 01. We will store th...
If you want the exact value of x, you’d find an interval of width 2^k containing x, then k steps of binary search at a cost of roughly x^2 find x. Total cost about x^2 log x. If you want a rough estimate for x, you’d evaluate at x = c^n for a suitable c > 1. The cost is multiplied by c^2 for each n, so the total co...
null
I realized that you can actually detect a line of _exactly_ $n$ "X" values in time $O \left( \log{(n)}^2 \right)$ operations, and the same with $n$ "O" values. Also, in the rare case that $n$ is a power of two, you can detect a sequence of "X" or "O" in $\Theta \left( \lceil \log{(n)} \rceil \right)$ operations. --...
I realized that you can actually detect a line of at least $n$ "X" values in time $O \left( \log{(n)}^2 \right)$ operations, and the same with $n$ "O" values. Also, in the rare case that $n$ is a power of two, you can detect a sequence of "X" or "O" in $\Theta \left( \lceil \log{(n)} \rceil \right)$ operations. ---...
I want to know the major methods to prove the correctness of a program/algorithm. Recommendations on books are appreciated too. This is a general question. TIA.
What are the methods to prove the correctness of an algorithm?
|algorithms|
I realized that you can actually detect a line of at least $n$ "X" values in $\Theta \left( \lceil \log{(n)} \rceil \right)$ operations, and the same with $n$ "O" values. --- The idea is to keep two "tables", and you perform tests by bit shifts. In one table, you record a bit 1 for every "X" value, and a 0 for...
The normal approach is to evaluate $f(1)$, $f(2)$, $f(4)$, $f(8)$, $f(16)$, $f(32)$, $f(64)$, etc., until you find the first power of two for which $f(2^k)=\text{true}$ (and thus $f(2^{k-1})=\text{false}$). Then, you use binary search in the range $[2^{k-1},2^k]$ to find the smallest value for which f returns true. ...
It is no wonder why that you doubt that "the shortest path increases monotonically in the residual graph" implies a "bound on of one iteration of Edmonds-Karp algorithm to $O(|E|)$". The time bound of $O(|E|)$ has nothing to do with the fact that "the shortest path increases monotonically in the residual graph". <!-...
Linear search is $0,1,2,3,4,\cdots n,\cdots$. ($O(n)$ time.) Exponential search is $1,2,4,8,16,\cdots2^n,\cdots$. ($O(\lg(n))$ time.) Doubly exponential search is $2,4,16,256,65536,\cdots2^{2^n},\cdots$. ($O(\lg(\lg(n)))$ time.) And so on. After you have found a $\text{true}$, you can continue with a dicho...
Linear search is $0,1,2,3,4,\cdots k,\cdots$. ($O(n)$ steps to pass $n$.) Exponential search is $1,2,4,8,16,\cdots2^k,\cdots$. ($O(\lg(n))$ steps to pass $n$.) Doubly exponential search is $2,4,16,256,65536,\cdots2^{2^k},\cdots$. ($O(\lg(\lg(n)))$ steps to pass $n$.) And so on. After you have found a $\tex...
null
null
null
I want to solve a problem of object tracking along time. The problem is - I have a sequence of images, and I need to find and track the creation of the objects, than their movement, and than their disappearance. There can be up to 3 objects overall, and sometimes there are less, or none. Another limitation is that for ...
Viterbi algorithm for object tracking
|algorithms|hidden-markov-models|
null
I want to know the major methods to prove the correctness of a program/algorithm. Recommendations on books are appreciated too. This is a general question. TIA. Edit: I'm not looking to detailed answer of each and every method. Instead, solicit recommendations/topics to start reading up my own. Name-dropping of met...
null
If is unclear what your 0/1 represent (characters in a string or bits in integers). Anyway, extracting the symbols in sequence takes $O(n)$ operations for $n$ symbols. There is no particular difficulty in counting the lengths of the continuous runs of zeroes (this is an $O(n)$ task) and accumulating the correspondin...
Let there is an algorithm whose running time is $O(n^2)$. Suppose we apply a preprocessing step on the algorithm in $O(n)$ so that it reduces the input size to $O(\sqrt{n})$ but doesn't effect the true answer of the algorithm. It is just for optimization. Now is it true to say that the running time of the algorithm is ...
Let there is an algorithm whose running time is $O(n^2)$. Suppose we apply a preprocessing step on the algorithm in $O(n)$ so that it reduces the input size to $O(\sqrt{n})$ but doesn't effect the true answer of the algorithm. It is just for optimization. Now is it true to say that the running time of the algorithm is ...
The MIU system, famous from Douglas Hofstadter, is a semi-thue system with the following rules: Xi → Xiu mX → mXX XiiiY → XuY XuuY → XY and a start axiom "mi" I have tried to find a formal _grammar_ that generates all words that can be derived from the semi-thue system, but did not succeed. Is there a...
Formal grammar of MIU system
|formal-languages|formal-grammars|
Suppose I have a Turing machine that takes as input any string of length $n$, where $n$ is odd, and the Turing machine returns the middle character of the string. What time complexity class is this in? Is it in $O(n)$ because the tape head must scan $n/2$ places to the right, counting as it goes, or is there a qu...
null
I realized that you can actually detect a line of at least $n$ "X" values in $\Theta \left( \lceil \log{(n)} \rceil \right)$ operations, and the same with $n$ "O" values. --- The idea is to keep two "tables", and you perform tests by bit shifts. In one table, you record a bit 1 for every "X" value, and a 0 for...
The problem can be solved in $\Theta(n)$ time, meaning that there exists a Turing machine that solves it using $O(n)$ steps and that all Turing machines must use $\Omega(n)$ steps. The running time of your particular Turing machine depends on the details of the machine itself (but must be $\Omega(n)$, see also Yves ...
The problem can be solved in $\Theta(n)$ time, meaning that there exists a Turing machine that solves it using $O(n)$ steps and that all Turing machines must use $\Omega(n)$ steps. The running time of your particular Turing machine depends on the details of the machine itself (but must be $\Omega(n)$, see also Yves ...
I am looking for elementary cellular automata rules and initial conditions that give rise to interesting cycles (Cyclic boundary conditions, such that the leftmost entry is the neighbour of the rightmost entry). After running a rule with some starting state, the state of the CA might (will?) start repeating in "cycles...
Strategy for searching for elementary cellular automata (cyclic boundary conditions) that repeat
|search-problem|cellular-automata|
I have a set of strings, lets call them _RULES_. I have a function _F_ which given 2 strings deterministically returns boolean value. Given one string, lets call it _QUERY_, what is the fastest way to find longest prefix (lets call it _P_) of _QUERY_ which is a member of _RULES_ for which _F(P,QUERY)_ returns true? ...
Finding longest prefix of a given string in set of strings that satisfies some property
null
TL,DR: it's not the same $n$. That's where the confusion comes from. When we say that an algorithm's complexity is $O(f(n))$ (i.e. $O(n)$ or $O(n^2)$ or whatever), it's implicit that $n$ is the size of the input. There is an implicit assumption here that there is a single numerical parameter that is “the size of the...
Without the addendum, you could do $O(|V|+|E|)$-time preprocessing (a single BFS, starting from the set $N$) that would let you solve each query in $O(1)$ time. With the addendum, this seems much harder. I would suggest exploring dynamic shortest-paths. See, e.g., https://cs.stackexchange.com/q/48729/755, https://...
I'm doing an exercise about the MIPS pipeline with the following characteristics: Branches and Brach targets are calculated in the E-stage. -There is forward logic from the output to the input of the E-stage. You can read an operand from the register file only the cycle after it has been written to it. This is the...
Why does it create that stall?
|cpu-pipelines|mips|
It has same meaning as the 1d Fourier transform: the magnitude and phase of waves that, when superimposed, reconstruct your original signal. In one dimension, 1. the signal is (usually) a function of time, $f(t)$, 2. the Fourier transform takes you into the frequency domain, a kind of "inverse time", and 3. each fu...
For Turing machines with at least two tapes the problem can be solved in $\Theta(n)$ time, meaning that there exists a Turing machine that solves it using $O(n)$ steps and that all Turing machines must use $\Omega(n)$ steps (see also Yves Daoust's comment). For Turing machines with one tape, the time needed to solve...
I'm doing an exercise about the MIPS pipeline with the following characteristics: -Branches and Branch targets are calculated in the E-stage. -There is forward logic from the output to the input of the E-stage. -You can read an operand from the register file only the cycle after it has been written to it. The t...
Why does it have that amount of stalls
For Turing machines with at least two tapes the problem can be solved in $\Theta(n)$ time, meaning that there exists a Turing machine that solves it using $O(n)$ steps and that all Turing machines must use $\Omega(n)$ steps (see also Yves Daoust's comment). For Turing machines with one tape, the time needed to solve...
One protection against hacker attacks is “address space randomisation”. Basically code is always loaded to a different address, making it harder to attack. So on a modern device it’s very unlikely to be loaded to a fixed place. There are two methods to support this: “relocatable code” can be modified at load time t...
As Hendrik Jan remarked, the MIU-rules as a string rewriting system is not [a semi-Thue system](https://en.wikipedia.org/wiki/Semi-Thue_system#Definition). Each string rewriting rule in a semi-Thue system does not care about context. A step of rewriting in a semi-Thue system consists of looking for a substring $s$, rem...
[![enter image description here][1]][1] [1]: https://i.stack.imgur.com/gVuZ4.jpg I don’t understand this state diagram. What if $n=0$? Then our input string is the empty string. We begin at $q_1$, push \$ onto the string, move to $q_2$ but then fail. So the language recognized by this PDA is $n \geq 1$. If, h...
Is this PDA correct or an erratum? (Sipser example 2.14)
|automata|
[![enter image description here][1]][1] [1]: https://i.stack.imgur.com/gVuZ4.jpg I don’t understand this state diagram. What if $n=0$? Then our input string is the empty string. We begin at $q_1$, push \$ onto the string, move to $q_2$ but then fail. So the language recognized by this PDA is $n \geq 1$. If, h...
null
Your given is a non deterministic PDA. It can either stop at $q_1$ or move to $q_2$ at the start. So, for empty input, it will still accept since $q_1$ is an accept state.
For now, assume that P != NP. Is there a "complete" class of problems between P and NP-hard, and if so, what is it called? The two key words here are 1. between 2. complete By between, I mean that it runs in super-polynomial time (not in P), but sub-exponential time (NP-hard is believed to take exponential tim...
Is there a hypothesized "complete" class of problems between P and NP-hard?
|complexity-theory|np-complete|np-hard|np|factoring|
null
This is a purely theoretical question: among the known [flood fill](https://en.wikipedia.org/wiki/Flood_fill) algorithms, there is one which does not require any dynamically-sized data structures, explicit or implicit: the so-called [Walk-based filling][1], expressed there in *structured English*, e.g. ``` ... while...
One could reasonably defend a Yes or No answer. Yes: consider, e.g., the class of [GI-complete problems](https://en.wikipedia.org/wiki/Graph_isomorphism_problem#Complexity_class_GI). Similarly, for any [NP-intermediate](https://en.wikipedia.org/wiki/NP-intermediate) problem Q, you can define the class of problems t...
Yes, you can do it in approximately $O(\mathcal{A} + \mathcal{P} + n \log n)$ time, where $\mathcal{A}$ is the area of $P$, $\mathcal{P}$ is the perimeter of $P$, and $n$ is the number of vertices, using a [sweep-line algorithm](https://en.wikipedia.org/wiki/Sweep_line_algorithm). Sweep a vertical line, from left to...
I would be glad for help to solve this question: Given the code: int A[1024,1024],B[1024,1024],C[1024,1024] for(int i=0;i<1024;i++){ for(int j=0;j<1024;j++){ for(int k=0;k<1024;k++){ A[i,j]+=B[i,k]+C[k,j]; } } } That runs on pipelin...
compute cache miss rate by given code
|computer-architecture|cpu-cache|memory-access|
null
I have been trying to solve questions like this before but I have stumbled in difficulty to track the the spaceof the cache. so there is an example of a question like this: Given the code: int A[1024,1024],B[1024,1024],C[1024,1024] for(int i=0;i<1024;i++){ for(int j=0;j<1024;j++){ ...
I have been trying to solve questions like this before but I have stumbled in difficulty to track the the spaceof the cache. so there is an example of a question like this: Given the code: int A[1024,1024],B[1024,1024],C[1024,1024] for(int i=0;i<1024;i++){ for(int j=0;j<1024;j++){ ...
I have been trying to solve questions like this before but I have stumbled in difficulty to track the the space of the cache. so there is an example of a question like this: Given the code: int A[1024,1024],B[1024,1024],C[1024,1024] for(int i=0;i<1024;i++){ for(int j=0;j<1024;j++){ ...
null
I have one LP problem (LP1) to solve, where a term in a constraint is to be substituted after solving another LP problem (LP2) (with a different variable vector). Sippose I call the dimension of the variable vector in LP1 as $n$ and in LP2 as $m$. I know that the complexity of solving an LP problem is $O(n^3L)$ ($L$ is...
Complexity of solving two different LP problems
|complexity-theory|time-complexity|
Yes, all initial states under all CA rules on a finite grid will eventually lead to a cycle (or a fixed state, which can be viewed as a cycle of length 1). More generally, iterating any fixed deterministic map on a finite domain of $n$ elements from any initial value in the domain will inevitably lead to a cycle in at...
null
null
I am currently reading up about esoteric programming languages and came across a COS theory question stated as follows: >Esoteric programming languages can be categorised in a variety of different ways. For each of the following categories identify one advantage and one disadvantage associated with languages falling...
What are Funges?