instruction
stringlengths
0
29.8k
⌀
null
Here is a linear-time algorithm that decides whether a DAG contains at least one incomparable pair of nodes. 1. Do a topological sorting with a linear algorithm. (Yes, topological sorting is very helpful!). 2. For each pair of adjacent nodes in the topological order obtained, check if there is an edge between them....
null
**Two nodes $u$, $v$ are said to be incomparable if there is neither a path from $u$ to $v$ nor a path from $v$ to $u$.** I can't see a linear solution to this. We can do a DFS for each pair $(u,v)$ and $(v,u)$ as in [here][1]. The fact that it is a DAG hints topological order but is this useful? Can this be s...
|algorithms|dag|directed-graphs|topological-ordering|
I need to make a container in which I can store (x,y) as pairs, and for a given number 'a', I have to find a pair (p, q) such that p<=a and q is maximum possible. Note the constraints: x>=1 and x<= 10^7; y >=1 and y<=10^5. I am solving this problem in C++. I tried using a set that stores pairs of integers. I was...
Constructing a Container for the Given Situation
|algorithms|data-structures|sets|c++|
null
null
null
"A window" will also mean "an axis-aligned unit square". As observed in the question, there is an optimal window such that - either the bottom left corner is a given point. - or the left side contains a given point not on the bottom side and the bottom side contains a given point not on the left side. Hence w...
As far as I am aware, most functional programming languages today use a call-by-value eager evaluation strategy with some exceptions like Haskell. I am curious if it is possible for a language to have a mixed evaluation strategy: for example, a language where everything is evaluated lazily by default but some functions...
Is it possible for a language to have mixed evaluation strategies?
|compilers|type-theory|functional-programming|
null
Let $D \subseteq \{0,1\}^*$ be the class of all dyadic languages. Then by $P^D$, we denote the class of languages decidable in polynomial time by a Turing machine with an oracle for a Dyadic language. Given a dyadic string $x \in \{0,1\}^m$, denote by $B(x)$ the string $y$ such that $y_i = x_{2^i}$. Intuitively, yo...
I have a problem that I'm struggling to solve or even name, I'd really appreciate any help or pointer to potential existing solutions. Suppose there is a connected graph $G$ and we are trying to find a tree $M^*$ with the following property. Let $D_M(v)$ be the degree of node $v$ in an arbitrary tree $M$. The soluti...
Minimum spanning tree with dynamic edge cost based on degrees
|algorithms|graphs|trees|computer-networks|minimum-spanning-tree|
null
In many references I find that the simply typed lambda calculus was introduced because the Kleene–Rosser paradox showed that pure/untyped lambda calculus was inconsistent. However, since it is untyped, the usual definition of inconsistency (i.e., that every type is inhabited) does not apply. Then, in what sense is pure...
The Kleene–Rosser paradox and the inconsistency of lambda calculus
|lambda-calculus|
On most modern systems, peripheral devices do not speak or control DMA directly. Rather, they communicate over a bus protocol (e.g. SCSI or ATA-over-PCI in the case of storage devices), and a special piece of hardware manages both the bus and the mechanics of DMA. On PC hardware, this is known as the HBA (Host Bus A...
On most modern systems, peripheral devices do not speak or control DMA directly. Rather, they communicate over a bus protocol (e.g. SCSI or ATA-over-PCI in the case of storage devices), and a special piece of hardware manages both the bus and the mechanics of DMA. On PC hardware, this is known as the HBA (Host Bus A...
I need to make a container in which I can store (x,y) as pairs, and for a given number 'a', I have to find a pair (p, q) such that p<=a and q is maximum possible. Note the constraints: x>=1 and x<= 10^7; y >=1 and y<=10^5. I am solving this problem in C++. I tried using a set that stores pairs of integers. I was...
null
The [Hamiltonian Path problem][1] on undirected graphs can be reduced to your problem for $k = 2$ and $\beta \gg |V|$. Since the Hamiltonian Path problem is $\mathsf{NP}$-hard, your problem is $\mathsf{NP}$-hard as well. For the proof of the above statement, you just need to observe the fact that if any *spanning tr...
I am trying to write down the generalized form of the finite automata which accept strings which contain as a substring an arbitrary string. Here is what I have come up with — I was hoping someone could tell me if I've made any mistakes (i.e., if there are any "bugs" in my "implementation"). Let $\Sigma$ be an alpha...
Can someone check if the finite state automata I have designed does what I expect it to do?
|finite-automata|
null
Consider the staircase formed by the "left-dominant" points, i.e. those such that they have the maximum ordinate among the points to their left. (If I am right, this staircase is the analogue of the convex hull but for the Manhattan metric.) [![enter image description here][1]][1] A dynamic solution to this prob...
null
A lot of great answers have already been given, but I would like to name some additional aspects I find quite important as well: not having to define a main function as the entry point encourages less encapsulation and thus less clean, in general uglier (and sometimes also even unusable) code. This is because high ...
Almost every mainstream programming language mixes strict and eager evaluation. In particular, in almost all mainstream programming languages, subroutine calls, function applications, and operator applications are evaluated eagerly, *except* conditional operations. For example, in C, `foo ? x : y` will only evaluate...
I am seeking a linear algorithm to determine whether a directed acyclic graph (DAG) contains at least one pair of incomparable nodes. **Two nodes $u$, $v$ are said to be incomparable if there is neither a path from $u$ to $v$ nor a path from $v$ to $u$.** I can't see a linear solution to this. We can do a DFS for...
Linear algorithm for determining the presence of incomparable pairs in a directed acyclic graph (DAG)
I'am trying to prove the following integer programming problem NP hard. $$ \min \frac{\sum\limits_{i=1}^m(u_i-\bar u)^2}{m}\text{ , where }u_i=\sum\limits_{j=1}^{n}\lambda_{j}x_j\\ s.t. \begin{cases} Ax\leq b, \\Cx=d, \\ \forall j: x_{j}\in\{0,1\}, \lambda_{j}\in \mathbb{R}^+\end{cases} $$ - x is a n-di...
Is the following integer programming NP-Hard?
|computability|np-complete|np-hard|
null
I assume by "*equivalent*" you were talking about the language a TM accepts. Then the proof of the two definitions' being equivalent can be made. On one hand, simply adding a rejecting state to any TM without one does not change what it accepts. On the other hand, removing the rejecting state from a TM with it does ...
Let $A = \{a_1, \ldots, a_n\}$ be a set of numbers and $w$ - also some number. Define $p_w: \mathbb{R} \rightarrow \mathbb{N}$ as $p_w(r) = |\{i \in \{1, \ldots, n\} | r \leq a_i \leq r + w\}|$. Note that $p_w(\mathbb{R})$ - an image of the whole $\mathbb{R}$ - is finite. Define $MRT_w = \max p_w(\mathbb{R})$. Let's...
Data structure for querying maximum of a rolling total
I have a problem that I'm struggling to solve or even name, I'd really appreciate any help or pointer to potential existing solutions. Suppose there is a connected graph $G$ and we are trying to find a tree $M^*$ with the following property. Let $D_M(v)$ be the degree of node $v$ in an arbitrary tree $M$. The soluti...
I assume by "*equivalent*" you were talking about the language a TM accepts. Then the proof of the two definitions' being equivalent can be made. On one hand, simply adding a rejecting state to any TM without one does not change what it accepts. On the other hand, removing the rejecting state from a TM with it does ...
null
I have a problem that I'm struggling to solve or even name, I'd really appreciate any help or pointer to potential existing solutions. Suppose there is a connected graph $G$ and we are trying to find a spanning tree $M^*$ with the following property. Let $D_M(v)$ be the degree of node $v$ in an arbitrary tree $M$. T...
An application or program has a start. That's the "main" function. Many languages do not call it "main", and in many it is implicit (e.g. the function defined by the script as a whole). On the other hand, libraries are collection of functions (and code, data, resources, etc.), and do not have a starting point. You c...
null
I'am trying to prove the following integer programming problem NP hard. $$ \min \frac{\sum\limits_{i=1}^m(u_i-\bar u)^2}{m}\text{ , where }u_i=\sum\limits_{j=1}^{n}\lambda_{j}x_j\\ s.t. \begin{cases} Ax\leq b, \\Cx=d, \\ \forall j: x_{j}\in\{0,1\}, \lambda_{j}\in \mathbb{R}^+\end{cases} $$ - x is a n-di...
Your specification of the FSA is - not finished. The transitions where the symbol read is not in $S$ are not defined. - ambiguous. What happens if $s_4=s_6$? Are $q_4$ and $q_6$ the same state? With the best intention to interpret your specification, consider the following example. Let $S=aab$. The FSA wil...
I'am trying to prove the following integer programming problem NP hard. $$ \min \frac{\sum\limits_{i=1}^m(u_i-\bar u)^2}{m}\text{ , where }u=Bx,B\in\mathbb{R}^{m\times n}\\ s.t. \begin{cases} Ax\leq b, \ A\in\mathbb{R}^{m\times n},b\in\mathbb{R}^m \\Cx=d, \ C\in \mathbb{R}^{m\times n},d\in\mathbb{R}^m\\ \for...
I'am trying to prove the following integer programming problem NP hard. $$ \min \frac{\sum\limits_{i=1}^m(u_i-\bar u)^2}{m}\text{ , where }u=Q x,Q\in\mathbb{R}^{m\times n}\\ s.t. \begin{cases} Ax\leq b, \ A\in\mathbb{R}^{m\times n},b\in\mathbb{R}^m \\Cx=d, \ C\in \mathbb{R}^{m\times n},d\in\mathbb{R}^m\\ \fo...
I'am trying to prove the following integer programming problem NP hard. $$ \min \frac{\sum\limits_{i=1}^m(u_i-\bar u)^2}{m}\text{ , where }u=Q x,Q\in\mathbb{R}^{m\times n}\\ s.t. \begin{cases} Ax\leq b, \ A\in\mathbb{R}^{m\times n},b\in\mathbb{R}^m \\Cx=d, \ C\in \mathbb{R}^{m\times n},d\in\mathbb{R}^m\\ \fo...
> Why do we need the main function and can't we execute the code without it? It depends entirely on how the language is defined. Programming languages are man-made constructs anyway, so we can make them as we like. With that in mind, we don't "need" a main function, and we can well have a language that can read fro...
An important philosophical point you might want to consider: Is there really a fundamental difference between _declaring_ a place where the program starts, and simply having the program start at the top of a specific file? `main`, in all the languages that have such a thing, is really just a marker that says "start h...
I have a problem that I'm struggling to solve or even name, I'd really appreciate any help or pointer to potential existing solutions. Suppose there is a connected graph $G$ and we are trying to find a spanning tree $M^*$ with the following property. Let $D_M(v)$ be the degree of node $v$ in an arbitrary tree $M$. T...
Let $A = \{a_1, \ldots, a_n\}$ be a set of numbers and $w$ - also some number. Define $p_w^{A}: \mathbb{R} \rightarrow \mathbb{N}$ as $p_w^{A}(r) = |\{i \in \{1, \ldots, n\} | r \leq a_i \leq r + w\}|$. Note that $p_w^{A}(\mathbb{R})$ - an image of the whole $\mathbb{R}$ - is finite. Define $MRT_w(A) = \max p_w^{A}(\ma...
I am seeking a linear-time algorithm to determine whether a directed acyclic graph (DAG) contains at least one pair of incomparable nodes. Two nodes $u$, $v$ are said to be incomparable if there is neither a path from $u$ to $v$ nor a path from $v$ to $u$. I can't see a linear solution to this. We can do a DFS fo...
Linear-time algorithm for determining the presence of incomparable pairs in a directed acyclic graph (DAG)
null
I have a problem that I'm struggling to solve or even name, I'd really appreciate any help or pointer to potential existing solutions. Suppose there is a connected graph $G$ and we are trying to find a spanning tree $M^*$ with the following property. Let $D_M(v)$ be the degree of node $v$ in an arbitrary tree $M$. T...
null
null
Here is my code, which passes all the tests. It's based on Matthew C's comment. def graham_scan(points: list) -> list: p0 = find_leftmost(points) hull = [] points.sort(key=cmp_to_key(lambda p, q: orientation_cmp(p0, p, q))) # handle colinear points at the beginning ...
An important philosophical point you might want to consider: Is there really a fundamental difference between _declaring_ a place where the program starts, and simply having the program start at the top of a specific file? `main`, in all the languages that have such a thing, is really just a marker that says "start h...
### Remarks on your FSA Your specification of the FSA is - not finished. The transitions from states other than $q_n$ where the symbol read is not in $S$ are not defined. - ambiguous. - What happens if $s_1=s_3$? Are $q_1$ and $q_3$ the same state? - Is the FSA deterministic or nondeterministic? It looks n...
Show that $ALL-NFA$ = {$\langle M\rangle : M$ is $NFA$ and $L(M) = \Sigma^*$} is $PSPACE-complete$. I've manged o prove that it is $PSPACE$. It is easy to see that $\overline{ALL-NFA}\in NPSPACE$ (TM just nondeterministically guess the word and this process is bounded by an appropriate time). $NPSPACE = PSPACE$, w...
ALL-NFA is PSPACE-complete
|turing-machines|finite-automata|space-complexity|
We understand the exponential function to be constantly growing, which we consider bad for a problem. When we say that a problem takes exponentially long time, what we mean is that as we tweak the input parameters a little then the problem takes more time by a 'constant factor'. Then why did we choose polynomials as a ...
Why are polynomials a natural measure for easiness of computational problems?
|complexity-theory|polynomial-time|
null
**Preliminaries**. Let $n,m,i,j,p,c \in \mathbb{N}$ with $n,m,i,j,p,c \geq 1$. Let our alphabet be $\{0,1\}$, with non-empty languages $ L_i \subseteq \Sigma^n$ and $ L'_i \subseteq \Sigma^m$. The other preliminaries are the same as a [previous question](https://cs.stackexchange.com/questions/159828/counting-stat...
Counting States in the trim automaton for $\cup_{i=1}^{p} L_i \circ L'_i$
|regular-languages|finite-automata|
null
{"OriginalQuestionIds":[210],"Voters":[{"Id":755,"DisplayName":"D.W.","BindingReason":{"GoldTagBadge":"complexity-theory"}}]}
We want to implement a data structure, which can store pairs of $(key, value)$ (where $key$ and $value$ are some numbers) and has the following interface: 1. Add $(key, value)$ pair to a data structure (if such key is already in a data structure then ignore this operation). 2. Remove $key$ from a data structure. 3...
Segment queries over a key-value store
|algorithms|data-structures|
I have a problem that I'm struggling to solve or even name, I'd really appreciate any help or pointer to potential existing solutions. Suppose there is a connected graph $G$ and we are trying to find a spanning tree $M^*$ with the following property. Let $D_M(v)$ be the degree of node $v$ in an arbitrary tree $M$. T...
### Remarks on your FSA Your specification of the FSA is - not finished. The transitions from states other than $q_n$ where the symbol read is not in $S$ are not defined. - ambiguous. - What happens if $s_1=s_3$? Are $q_1$ and $q_3$ the same state? - Is the FSA deterministic or nondeterministic? It looks n...
Does my finite state automaton accept a string iff it contains the given string as a substring?
|regular-languages|finite-automata|substrings|
I am trying to write down the generalized form of the finite automata which accept strings which contain as a substring an arbitrary string. Here is what I have come up with — I was hoping someone could tell me if I've made any mistakes (i.e., if there are any "bugs" in my "design"). Let $\Sigma$ be an alphabet. ...
We want to implement a data structure, which can store pairs of $(key, value)$ (where $key$ and $value$ are some numbers) and has the following interface: 1. Add $(key, value)$ pair to a data structure (if such key is already in a data structure then ignore this operation). 2. Remove $key$ from a data structure. 3...
If you subdivide your two must-include edges creating a node right in the middle of the edge, you have an instance of requiring a simple path with two must-include nodes. The general problem (requiring $k$ must-include nodes) is studied here: https://utd-ir.tdl.org/bitstream/handle/10735.1/2637/ECS-TR-EE-Vardhan-310...
I am currently working through Sipser's Introduction to the Theory of Computation. In chapter 5, he defines that a Language $A$ is mapping reducible to language $B$, written $A\leq_m B$ if there is a computable function $f:\Sigma^*\rightarrow\Sigma^*$ where for every $w$, $w\in A \iff f(w)\in B$, where $f$ is called th...
Understanding reductions and notatoin
|complexity-theory|computability|reductions|notation|
We want to implement a data structure, which can store pairs of $(key, value)$ (where $key$ and $value$ are some numbers) and has the following interface: 1. Add $(key, value)$ pair to a data structure (if such key is already in a data structure then ignore this operation). 2. Remove $key$ from a data structure. 3...
null
I'am trying to prove the following integer programming problem NP hard. $$ \min \frac{\sum\limits_{i=1}^m(u_i-\bar u)^2}{m}\text{ , where }u=Q x,Q\in\mathbb{R}^{m\times n}\\ s.t. \begin{cases} Ax\leq b, \ A\in\mathbb{R}^{m\times n},b\in\mathbb{R}^m \\Cx=d, \ C\in \mathbb{R}^{m\times n},d\in\mathbb{R}^m\\ \fo...
|computability|np-complete|np-hard|integer-programming|
null