instruction stringlengths 0 29.8k ⌀ |
|---|
The Master Theorem is not deemed to be used with $b<1$. But you can recast the recurrence as
$$T(2n)=\frac12T(n)-\frac n2$$
or
$$T(n)=\frac12T\left(\frac n2\right)-\frac n4.$$
So yes, the Master Theorem can be used. |
null |
I'm struggling in finding a correct way to approach this, I'm aware that this problem is solvable using dynamic programming, and this problem somehow relates to the "max non-contiguous subarray" problem, yet I cannot make the connection.
Given an array of integers, $A=(a_1,\ldots , a_n)$ for which $\sum_{i=1}^{n}a_i... |
Here is a linear-time solution that consists of the following two parts.
#### Preparation: 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... |
Here is a linear-time solution that consists of the following two parts.
#### Preparation: 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 is done by setting $s_0=0$ and then $s_{i}=s_{i-1}+a_i$ for $i$ from $1$ to... |
null |
Assume an array of $n$ elements, containing $k$ groups with equidistant pairs, of decreasing difference, each including $m_k$ pairs.
The first version performs $n-1$ comparisons and $k$ array initializations and $\displaystyle\sum_{i=1}^k m_i$ `push_back`. The worst case is $n-1$ comparisons and $k$ initializations ... |
I'm trying to apply the [master method](https://en.wikipedia.org/wiki/Master_theorem_(analysis_of_algorithms)) to the following recurrence:
$$T(n) = 2 \cdot T(2n)+n.$$
We have $a=2$ and $b=1/2$.
Also,
$f(n)=n$
and
$n^{\log_b a} = n^{\log_{1/2} 2} = n^{-1}$ since $\log_{1/2} 2 = -1$.
So, case 3 applies si... |
Does T(n) = 2 · T(2n) + n apply to Master method? |
Given a collection of tuples $X=\{(x_1,y_1),\dots,(x_n,y_n)\}$, where elements
$x_i, y_i \in R_{\geq 0}$ are non-negative real values. The collection $X$ is
sorted if $x_i \leq x_{i+1}$ and $y_i \leq y_{i+1}$ for all $i \in [n-1]$. Sorting $X$ is not
always possible for instance if the given input has two tuples $(x... |
Sorting a collection of tuples using merge rearrangements |
|optimization|sorting|np-hard|combinatorics| |
Given a collection of tuples $X=\{(x_1,y_1),\dots,(x_n,y_n)\}$, where elements
$x_i, y_i \in R_{\geq 0}$ are non-negative real values. The collection $X$ is
sorted if $x_i \leq x_{i+1}$ and $y_i \leq y_{i+1}$ for all $i \in [n-1]$. Sorting $X$ is not
always possible for instance if the given input has two tuples $(x... |
null |
Given a collection of tuples $X=\{(x_1,y_1),\dots,(x_n,y_n)\}$, where elements
$x_i, y_i \in R_{\geq 0}$ are non-negative real values. The collection $X$ is
sorted if $x_i \leq x_{i+1}$ and $y_i \leq y_{i+1}$ for all $i \in [n-1]$. Sorting $X$ is not
always possible for instance if the given input has two tuples $(x... |
I want to generate spatial scale-free networks for my project. Are there any python libraries that enable it?
I read about the BA model ([https://www.science.org/doi/pdf/10.1126/science.286.5439.509][1]) which seems like the pioneer of scale-free networks but I am not sure it takes the spatial aspect into the accoun... |
How to generate spatial scale-free nwtworks? |
|algorithms|graphs|sampling|network-topology| |
I searched online. Also checked several articles/papers on poset topological sorting and [linear extension of poset](https://en.wikipedia.org/wiki/Partially_ordered_set#Linear_extension). No name has been found for that relation.
$E(P)$ can be described as the set of ordered pairs compatible to $P$, the set of order... |
> Or is it just the fact, that any clique must be contained in some bag
> of a tree/path decomposition?
To prove the claim one should prove, both, that the pathwidth is at most the maximum clique size - 1 (e.g., by construction), and that there does not exists a clique not contained in a bag as you observed. For in... |
null |
Self-answer: after a follow-up research on my own, I discovered that, while my *is-a* & *has-a* weights do form a star semiring with idempotent meet operator, Floyd-Warshall algorithm does not work because it does not take cycles into account.
If I understood it correctly, Floyd-Warshall algorithm is only valid if p... |
Is there a standard terminology for the topological sort count over a partial order? I went with magnitude of a poset rather than dimension as this is too close to linear algebra terminology. I wonder if there is a generally accepted choice. |
Terminology for number of topological sorts |
|partial-order| |
Answer by Clayton Gotberg [1], modified:
> If $\textbf{A}$ is a symmetric matrix and $\textbf{A}_{LT}$ is the
> lower triangular part of the matrix and $\textbf{A}_{UT}$ is the upper triangular
> part of the matrix:
>
> $\textbf{A}_x = \textbf{A}_{LT}x + \textbf{A}_{UT}x - diag(\textbf{A})
\cdot x$
>
> wher... |
null |
Given a collection of tuples $X=\{(x_1,y_1),\dots,(x_n,y_n)\}$, where elements
$x_i, y_i \in R_{\geq 0}$ are non-negative real values. The collection $X$ is
sorted if $x_i \leq x_{i+1}$ and $y_i \leq y_{i+1}$ for all $i \in [n-1]$. Sorting $X$ is not
always possible for instance if the given input has two tuples $(x... |
How can I convert a 1D index into the lower-left triangle of a grid into a row and column?
For example, consider this table of 1D indices, indexed by row and column
// 0 1 2 3 4
// +--------------- ...
// 0 | 0
// 1 | 1 2
// 2 | 3 4 5
// 3 | 6 7 8 9
/... |
Convert lower-left matrix triangle 1D index to row, column |
|arrays| |
null |
|data-structures|arrays|matrices| |
How can I convert a 1D index in the lower-left triangle of a grid into a row and column?
For example, consider this table of 1D indices, indexed by row and column
// 0 1 2 3 4
// +--------------- ...
// 0 | 0
// 1 | 1 2
// 2 | 3 4 5
// 3 | 6 7 8 9
// ... |
How can I convert a 1D index in the lower-left triangle of a grid into a row and column?
For example, consider this table of 1D indices, indexed by row and column
// 0 1 2 3 4
// +--------------- ...
// 0 | 0
// 1 | 1 2
// 2 | 3 4 5
// 3 | 6 7 8 9
// ... |
I think this is the solution. Works better with 1-based indices.
// 1 2 3 4 5
// +--------------- ...
// 1 | 1
// 2 | 2 3
// 3 | 4 5 6
// 4 | 7 8 9 10
// 5 | 11 12 13 14 15
// ...
Let `i` be the 1D index. Then we have `i < (row^2 + row... |
I think this is the solution. Works better with 1-based indices.
// 1 2 3 4 5
// +--------------- ...
// 1 | 1
// 2 | 2 3
// 3 | 4 5 6
// 4 | 7 8 9 10
// 5 | 11 12 13 14 15
// ...
Let `i` be the 1D index. Then we have `i < (row^2 + row... |
"Number of linear extensions" is probably the most popular one.
Google scholar returned 1000+ matches for `"number of linear extensions"` while `"number of topological order"` or `"number of topological sort"` return less than 10 matches. `"magnitude of a poset"` has only one match with seemingly different meaning.
... |
null |
In each iteration, with probability $p$ you create an edge, and otherwise a new vertex.
When creating edges, you consider the degree of the two vertices, and scale by $1/f(\text{dist}(v,u))$, where dist is typically the Euclidean distance.
When adding a new vertex, you put it randomly in $n$-dimensional space and... |
The $r$ first rows include $T_r=\dfrac{r(r+1)}2$ elements and the linear index corresponding to $(r,c)$, with $0\le c\le r$, is $T_r+c$. The $T_r$ are called the *triangular numbers*.
|
The $r$ first rows include $T_r=\dfrac{r(r+1)}2$ elements and the linear index corresponding to $(r,c)$, with $0\le c\le r$, is $i:=T_r+c$.
The $T_r$ are called the *triangular numbers*.
----------
Retrieving the row and column from the linear index is a little more tricky.
For given $i$, we have
$$\frac... |
As far as I know, there is no better way than the standard matrix-vector accumulation, taking $O(n^2)$ operations. Anyway, you need to take into account the packed storage of the matrix, and you need to process the matrix elements following an L shape. |
Determining a faster/better approach for finding the pairs with minimum difference in an array |
I have a large array of bits of length $N$.
The query $f(k, m)$ means "find $kth$ zero in the array and the next $m-1$ zeros after it", $k \in [0, N-1], m \ll N$
Currently I use a segment tree over the bit array and my approasches were:
- use [kth zero search procedure](https://cp-algorithms.com/data_structur... |
I have the following problem: Given an undirected graph with n vertices v1,…,vn, a positive integer weight on each edge, and a n×n symmetric matrix Rij. The objective is to find a subset S of the edges such that the total cost of the edges in S is minimized and for every pair of vertices vi and vj there exist at least ... |
How to prove that this is NP complete |
|graphs|np-complete|np-hard|np|correctness-proof| |
I think this is a solution. Works better with 1-based indices.
// 1 2 3 4 5
// +--------------- ...
// 1 | 1
// 2 | 2 3
// 3 | 4 5 6
// 4 | 7 8 9 10
// 5 | 11 12 13 14 15
// ...
Let `i` be the 1D index. Then we have `i < (row^2 + row /... |
null |
You can reduce from the minimum Steiner tree problem. The graph and the weights are the same. Build $R$ as follows: for each pair $v_i, v_j$ of terminals set $R_{i,j}=1$, then set all other entries of $R$ to $0$.
The set of edges of any Steiner tree is clearly also a solution to your problem (and the costs coincide)... |
The problem cannot be NP complete since it is not a decision problem, and hence it does not belong to NP. The decision version of your problem (given an edge-weighted graph $G$, the matrix $R$, and an integer $k$, is there a set of edges that satisfies the constraints in your question and has a total cost at most $k$?)... |
This problem's been stumping me for the better part of a week:
You're given a set of triplets of variables. The variables are all distinct and ordered. Each triplet $a,b,c$ means that either $a<b<c$ or $c<b<a$. The problem is to find an ordering of the variables satisfying all constraints.
For example, the set $\... |
Is there a sub-NP algorithm to satisfy or prove unsatisfiable a set of a<b<c OR c<b<a constraints |
|algorithms|integers|ordering| |
I understand that the problem of whether a PDA accepts all strings is undecidable. However that doesn't mean such PDAs exist. To start, I'm working under the assumption that a PDA must read it's entire input, have an empty stack, and be in an accept state in order to accept. I am also working with the convention that a... |
What characteristics would a PDA $A$ where $L(A)=\Sigma^*$ have? |
|formal-languages|finite-automata|undecidability|pushdown-automata| |
{"Voters":[{"Id":155618,"DisplayName":"kim120"}]} |
Your mouse is plugged into a USB port, yes? And you're using a pretty normal PC?
First off, USB devices never send signals by themselves. They're only allowed to respond to questions they get from the computer (formally, the "host"). The computer is asking the mouse, typically 125 times per second, whether it's move... |
I found many solution online on how to reduce Subgraph Isomorphism problem to Clique, but how do I prove that it is NP complete by reduction from independent set?
I'm struggling to figure out this proof through independent set. Any hints? |
Subgraph Isomorphism Problem NP complete? |
|np-complete|np|graph-isomorphism|subgraphs| |
null |
I searched online. Also checked several articles/papers on poset topological sorting and [linear extension of poset](https://en.wikipedia.org/wiki/Partially_ordered_set#Linear_extension). No name has been found for that relation.
$E(P)$ can be described as the set of ordered pairs compatible to $P$ <!-- depending on... |
null |
A *symbolic link* is simply a *special file* which contains a *path*. There is nothing special or magic about this path. The path has to be looked up in exactly the same way as any other path.
For example, if the symbolic link contains the path `foo/bar/../../../baz/./quux`, then the OS has to look up that path.
... |
None of the above, I am afraid. There is nothing that distinguishes a PDA for $\Sigma^*$, except that it has an accepting computation for every string.
Of course a PDA may have all those properties, just build a deterministic finite state automaton and convert it to a PDA by ignoring the stack. But we can avoid all... |
null |
**Problem**: find all anagrams in a string such that one substring can be formed from another substring by rearranging its letters. Given a string $s$, find the number of pairs of substrings of the string that are anagrams of each other.
Below is the traditional solution to this problem that uses $O(n^2\times n\lo... |
Finding All Anagrams in a String |
|algorithms|time-complexity| |
I want to remove ambiguity from the grammar below:
S → ABC
A → abA | ab
B → b | BC
C → c | cC
I have removed left recursion and left factoring and obtained the grammar as follows:
S → ABC
B → bB'
B' → CB' | ε
A → abA'
A' → A | ε
C → cC'
C' → ε | C
Now, how can I remove ambiguity... |
How to remove ambiguity from the grammar? |
|formal-grammars|compilers| |
I want to remove ambiguity from the grammar below:
S → ABC
A → abA | ab
B → b | BC
C → c | cC
I have removed left recursion and left factoring and obtained the grammar as follows:
S → ABC
A → abA'
A' → A | ε
B → bB'
B' → CB' | ε
C → cC'
C' → ε | C
Now, how can I remove ambiguity... |
null |
Lines $1-12$ are pretty straightforward, finding all substrings, sorting them and updating the counter for each occurrence of a substring and its permutation accordingly.
Now, $3$ substrings $BAC$, $ACB$ and $CBA$ . Sorting them lexicographically gives $ABC$. And in the hashmap $anagrams$ count of $ABC$ would be $3$... |
Lines $1-12$ are pretty straightforward, finding all substrings, sorting them and updating the counter for each occurrence of a substring and its permutation accordingly.
Now, $3$ substrings $BAC$, $ACB$ and $CBA$ . Sorting them lexicographically gives $ABC$. And in the hashmap $anagrams$ count of $ABC$ would be $3$... |
null |
null |
Wondering which approach is faster in terms of time complexity. In short, is having a single loop that clears out the vector every time there's a difference lesser than the minimum so far faster than having two separate loops: first determines the minimum difference, second appends the corresponding elements with minim... |
null |
The problem cannot be NP complete since it is not a decision problem, and hence it does not belong to NP. The decision version of your problem (given an edge-weighted graph $G$, the matrix $R$, and an integer $k$, is there a set of edges that satisfies the constraints in your question and has a total cost at most $k$?)... |
null |
null |
null |
Given a collection of tuples $X=\{(x_1,y_1),\dots,(x_n,y_n)\}$, where elements
$x_i, y_i \in R_{\geq 0}$ are non-negative real values. The collection $X$ is
sorted if $x_i \leq x_{i+1}$ and $y_i \leq y_{i+1}$ for all $i \in [n-1]$. Sorting $X$ is not
always possible for instance if the given input has two tuples $(x... |
null |
My lecture notes for minimum spanning trees say that:
> A graph can have several minimum spanning trees but if the edge weights are distinct then the minimum spanning tree is unique. Without loss of generality, we can
assume that the edge weights are distinct
I'm trying to convince myself that the distinct edge ... |
Distinct edge weight assumption in MST algorithms |
|algorithms|graphs|minimum-spanning-tree| |
The real question is: if we assign arbitrary priorities to edges of equal weights, can that break an MST algorithm ? Though this can lead to different MST's, neither the tree-ness property, nor the spanning property nor the minimum weight will be invalidated.
Assigning priorities can be done
- a priori, for insta... |
null |
null |
Given a collection of tuples $X=\{(x_1,y_1),\dots,(x_n,y_n)\}$, where elements
$x_i, y_i \in R_{\geq 0}$ are non-negative real values. The collection $X$ is
sorted if $x_i \leq x_{i+1}$ and $y_i \leq y_{i+1}$ for all $i \in [n-1]$. Sorting $X$ is not
always possible for instance if the given input has two tuples $(x... |
Global variables are given fixed addresses in main memory by the C compiler, called static memory allocation. Function local variables are created on the stack, this is called automatic memory allocation.
Why is it done this way? |
automatic memory allocation |
|c|assembly| |
I am trying to understand stacks a little better.
I don't understand why the content of the program counter is saved on the stack for the function call (call), but not for a jump (jmp)?
can someone please explain it to me?
thanks in advance! |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.