instruction
stringlengths
0
29.8k
⌀
null
Just to complement Mario Cervera's [answer][1]. Let's consider a hash function given by `hash(k) = k mod 12`, so our hash table has 12 buckets. First, we need to understand how collisions occurr in hash tables. Two numbers with keys $k_1$ and $k_2$ are said to collide when `hash(k_1)=hash(k_2)` which is $k_1 \...
Just to complement Mario Cervera's [answer][1]. Let's consider a hash function given by `hash(k) = k mod 12`, so our hash table has 12 buckets. First, we need to understand how collisions occurr in hash tables. Two numbers with keys $k_1$ and $k_2$ are said to collide when `hash(k_1)=hash(k_2)` which is $k_1 \...
null
With the following recursive code to count leaf nodes of a binary tree, is there any way to make it faster or dynamic or parallel-computing optimized in time? Python code - (mag(P) = number of leaf nodes of tree P depth H, left(P) = produces left subtree and right(P)= produces right subtree, not given here, reduce ...
fastest algorithm to count leaf nodes
|dynamic-programming|trees|parallel-computing|counting|backtracking|
Which algorithm do I use and how much does it cost if I have to: Calculate all distances in an undirected and unweighted graph from two sources to all nodes. I think the most appropriate algorithm is a BFS(G,s) with cost T(n)= O(V+E) where V= (number of nodes) and E=(number of arcs). The algorithm is the one in t...
Calculate all distances in an undirected and unweighted graph
|algorithms|graphs|graph-traversal|
I was wondering about a solution for the following problem: > Given a directed graph $G=(V,E)$ and a subset of vertices $U \subseteq V$ suggest an algorithm that finds if there is a directed path (the path can revisit vertices/edges) that visits every node in U I was thinking about running DFS\BFS from each verte...
>[![enter image description here][1]][1] > > $$(\lambda x. x \text{ } x ) ( \lambda x . x \text{ } x )= (\lambda x. x \text{ } x) ( \lambda x . x \text{ } x)$$ > > [Source](https://www.youtube.com/watch?v=9T8A89jgeTI&ab_channel=Computerphile) I am a bit confused on how this composition was done. When I do it, I...
Confusion when composing functions in Lambda expression
|lambda-calculus|
I am trying to understand how operating systems work because I would like to write my own version of Unix. Anyway, I have some doubts about some mechanisms that allow the applications to communicate with the kernel. In particular, according the several sources on the web, when we compile a program, it is OS dependent a...
How do programs communicate with the kernel?
|compilers|os-kernel|
null
I am really confused about it. I think 32 bits = 4 bytes but 32 bit address is only 1 bit.
Why does a 32 bit address only contain 1 byte, when 32 bits = 4 bytes?
|computer-architecture|memory-hardware|memory-allocation|
`L1` and `L2` are two lists which only consist of `X`'s and `Y`'s. `L1` and `L2` have the same length. How many sublists of `L1` contain the same number of `X`'s as the sublist of `L2` in the same position? The empty sublist is not counted. For example: 1. `L1` = `X` `L2` = `Y` => return `0` 2. `L1`...
number of substrings in same position with same contents
|algorithms|time-complexity|substrings|linear-complexity|
You drop the $\lambda x$ because that's just how beta reduction in defined: $(\lambda v.M)(N) \triangleright_\beta M[v/N]$. In your example, $v = x$, $M = xx$, and $N = \lambda x. xx$.
The address indicates *where* in memory a piece of data is found. This piece of data can be of any size. The page number in a book is made of two digits. That does not mean that the page only contains two digits, does it ?
null
You drop the $\lambda x$ because that's just how beta reduction in defined: $(\lambda v.M)(N) \triangleright_\beta M[v/N]$. In your example, $v$ is $x$, $M$ is $xx$, and $N$ is $\lambda x. xx$. The intuition is that once you applied the function to an argument, then slot is filled and no longer open for substitut...
You are right, there is an $O(n)$ time algorithm for this. Let $L(i,j)$ be the substring of the string $L$ from index $i$ to $j$, and let $|L|_X$ be the number of $X$'s in $L$. Consider that the number of substrings ending on an index $j$ with the same number of $X$'s in both strings is equal to the number of indice...
null
You drop the $\lambda x$ because that's just how beta reduction in defined: $(\lambda v.M)(N) \triangleright_\beta M[v/N]$. In your example, $v$ is $x$, $M$ is $xx$, and $N$ is $\lambda x. xx$. The intuition is that once you applied the function to an argument, then the slot is filled and no longer open for subst...
With the following recursive code to count leaf nodes of a binary tree, is there any way to make it faster or parallel-computing optimized in time? Python code - (mag(P) = number of leaf nodes of tree P depth H, left(P) = produces left subtree and right(P)= produces right subtree, not given here, reduce the tree P ...
|trees|parallel-computing|counting|backtracking|subtree|
The following is how I decode this program. The $R_\sqcup$ is a macro for the machine that moves right until it reads a blank. I deduce that $R^2_\sqcup$ repeats this process, locating the second blank to the right. Now the machine shutles right and left moving one non-blank $a$ to the right each cycle. The positio...
You drop the $\lambda x$ because that's just how beta reduction in defined: $(\lambda v.M)(N) \triangleright_\beta M[v/N]$. In your example, $v$ is $x$, $M$ is $xx$, and $N$ is $\lambda x. xx$. The intuition is that once you applied the function to an argument, then the slot is filled and no longer open for subst...
The following is how I decode this program. The $R_\sqcup$ is a macro for the machine that moves right until it reads a blank. I deduce that $R^2_\sqcup$ repeats this process, locating the second blank to the right. Now the machine shutles right and left moving one non-blank $a$ to the right each cycle. The positio...
You drop the $\lambda x$ because that's just how beta reduction is defined: $(\lambda v.M)(N) \triangleright_\beta M[v/N]$. In your example, $v$ is $x$, $M$ is $xx$, and $N$ is $\lambda x. xx$. The intuition is that once you applied the function to an argument, then the slot is filled and no longer open for subst...
The question is confusingly worded, but if the question is "why a pointer of 32 bits (4 bytes) points to only one byte of storage", that is just how a particular computer architecture (like x86) is designed. It's designed with byte-addressable storage, even though the address space itself is larger than 8 bits, and...
The question is confusingly worded, but if the question is "why a pointer of 32 bits (4 bytes) points to only one byte of storage", that is just how a particular computer architecture (like x86) is designed. It's designed with byte-addressable storage, even though the address space itself is larger than 8 bits, and...
fastest algorithm to count leaf nodes (i.e. terminal nodes)
null
I implemented the $O(n^2)$ convex hull algorithm. That is: * Find a triangle known to be in the hull (by finding the lowest point, a point connected to it in the 2D convex hull, and the point that makes the widest angled triangle relative to all other triangles with thsoe 2 points). * Add the 3 edges and the tria...
How to handle coplanarity in convex hull?
|algorithms|data-structures|computational-geometry|topology|
I have to express the solution of the recurrence equation T(n) = T(an) + n where a is a constant, 0 < a < 1 in terms of θ using the iteration method. I am unsure of how I calculate the cost of each level and the number of levels because of the constant a.
How do I solve this recurrence equation?
|algorithm-analysis|asymptotics|recurrence-relation|
I have to express the solution of the recurrence equation T(n) = T(an) + n where a is a constant, 0 < a < 1, in terms of θ using the iteration method. I am unsure of how I calculate the cost of each level and the number of levels because of the constant a.
**Hint:** $$T(n)=T(an)+n=T(a^2n)+an+n=T(a^3n)+a^2n+an+n=\cdots$$ You can stop the expansion when $a^kn=1$ and use the geometric sum formula.
There is a simple linear-time algorithm that determines whether it is possible or find a possible way. #### The algorithm for the decision problem The idea is to collect the choices at the rightmost cell of each region, scanning regions from the leftmost region, i.e. region $1$ to the rightmost region, i.e. regio...
I'm currently investigating the computational complexity of a modified one-dimensional Suguru puzzle. The general Suguru puzzles were recently proven to be NP-complete (see [here][1]). My investigation considers a modified one-dimensional Suguru puzzle played in a $1 \times N$ grid of cells. These cells are grouped int...
On top of what everyone else has said, computers tend to be byte-addressable because text is an important use case. Of course, modern CPUs don't actually read a byte from RAM. They read a *cache line*, which might be 32-128 bytes in size, and then extract bytes or words from that. Every so often, a new generation...
Your approach is mostly correct. The coverage of $S_i$ is $n ( 1 - 1 /k)^{i-1}$ and the sets $S_1, \ldots, S_k$ are disjoint, so the total coverage is $$ \sum_{i=1}^{k} n (1 - 1 / k)^{i-1} = n \left( \frac{1 - (1 - 1/k)^k}{1 - (1-1/k)} \right) = kn \left( 1 - (1 - 1/k)^k \right) = \mathrm{OPT} \cdot \left( 1 - (1 ...
Most constructs / schemes in programming serve an essential purpose (sometimes more than one), for instance : - Structs in essentially C's solution to the problem of providing user defined data types. - Classes in C, Python, generalize the concept and allow for OO programming. - Templates or type parameters or ...
Lets assume `dir a` has `dir b` which has `dir c`.<br> In a recursive deletion of these directories we can't "just delete" `dir a`. We must first delete `dir c` (and any file it has, if it has), then `dir b` (and any file it has, if it has) and only then could we delete `dir a`. This seems to me "from bottom to top...
Is recursiveness always from bottom to top?
|recursion|
null
Macros indeed enable a powerful form of metaprogramming. In particular they allow - to substitute any kind of piece of code, - anywhere, not necessarily in identifiers. They are quite useful to implement compile-time variants of the code, to ensure portability across platforms, as shorthands for repeating fragmen...
Random number generators commonly found in default libraries are actually pseudo-random number generators. Nevertheless, more sophisticated options exist to gather entropy from "truly"-random sources such as atmospheric noise, thermal noise, electromagnetic and quantum phenomena. Speed of random number generation i...
Are there applications in which random number generation quality is the bottleneck?
|randomness|pseudo-random-generators|random-number-generator|
There have certainly been situations where the periodicity of some pseudo-random number generators along with simplifications during use and exceptional observers have caused issues -- the obvious one that comes to mind is the guy who could predict where the "randomized" Press Your Luck gameboard would land. As far ...
Given a matrix A, let Aij denote the element of the i'th row and j'th column. 0 <= Aij <= 1 Find the number of sub-matrices with all ones. **1 <= #rows, #columns <= 150**
Given a binary matrix, find the number of sub-matrices with all ones
|algorithms|
There are papers that describe how generalized algebraic datatypes (GADTs) are encoded in core Haskell (System FC)[1][2], but I could not find any documentation on how OCaml implements/formalizes/encodes GADTs and how it compares to Haskell GADTs. How are GADTs implemented/formalized/encoded in core OCaml and how do...
How do GADTs work in OCaml?
|programming-languages|functional-programming|data-types|
Given a matrix $A \in \{0,1\}^{n\times n}$, let $A_{i,j}$ denotes the element of the i'th row and j'th column. $$ A_{i,j}\in \{0, 1\}$$ Find the number of sub-matrices with all ones. **1 <= #rows, #columns <= 150**
|algorithms|matrices|
Given a matrix A, let Aij denote the element of the i'th row and j'th column. $$ A_{i,j}\in \{0, 1\}$$ Find the number of sub-matrices with all ones. **1 <= #rows, #columns <= 150** P.S. This apparently can be solved in O(#rows x #columns) using dynamic programming and stack. I just can't wrap my head around ...
I am building an analyzer that takes in text and numbers the lines; taking in ``` hello how are you i'm fine thanks ``` as input and using the code: ``` %{ int i=0; %} %option noyywrap %% .* {i++;printf("[%d] %s \n",i,yytext);} %% void main(){ yylex(); } ``` this yields: ``` [1] hello how...
Confusion regarding FLEX(regular expressions)
|formal-languages|regular-languages|compilers|
null
Limited in this context means that the processor can only execute certain combinations of instructions in parallel. It may not even have more than one pipeline and may use some other mechanism for instruction-level parallelism. Asymmetric means that the two pipelines are not identical. For example, one pipeline cou...
I figured out that there is a problem with the convex hull of a raster image, namely the fact that it is essentially constructed of quads, which gives an ambiguity. For example, consider the example below: ``` 0 1 1 0 ``` This could be interprted like ``` 0 -- 1 | / | | / | 1 -- 0 ``` ``` 0...
null
I'm trying to figure out the pumping length of (a+b)(a+b)* From what I understand, this means that there is some A or B followed by any number of either A's or B's e.g ABBBB or AAAAA but AAAABA wouldnt be in the language. Firstly I'm not sure if my assumption above is correct. Using the decomposition...
Pumping length of (a+b)(a+b)*
|regular-languages|pumping-lemma|
I'm trying to figure out the pumping length of (a+b)(a+b)* From what I understand, this means that there is some A or B followed by any number of either A's or B's e.g ABBBB or AAAAA but AAAABA wouldnt be in the language. Firstly I'm not sure if my assumption above is correct. Using the decomposition...
I'm trying to figure out the pumping length of (a+b)(a+b)* From what I understand, this means that there is some A or B followed by any number of either A's or B's e.g ABBBB or AAAAA but AAAABA wouldnt be in the language. Firstly I'm not sure if my assumption above is correct. Using the decomposition...
There are two things you need to know about Lex (and hence Flex, which follows the same semantics): 1. It buffers a line of input at a time. 2. The dot `.` symbol includes the new line/carriage return ad the end of a line. The first point is the important one. It means that Lex tokens cannot span a line break....
There are two things you need to know about Lex (and hence Flex, which follows the same semantics): 1. It buffers a line of input at a time. 2. The dot `.` symbol includes the new line/carriage return at the end of a line. The first point is the important one. It means that Lex tokens cannot span a line break....
|programming-languages|functional-programming|data-types|ocaml|
null
**Approach**: 1. Run $BFS(vertex_{1})$. 2. Run $BFS($vertex_{2})$. **Complexity:** $O(|V|+|E|) + O(|V|+|E|) = O(|V|+|E|)$ As long as you have a constant $c$, then $c\times f(n) = O(f(n)) $.
null
Being C a complexity class, if C is contained in its complementary class, does it imply that the C = coC? So far I tried to prove $C \subseteq coC \implies C \subseteq coC \land coC \subseteq C$. I can assume that $C \subseteq coC$, but how can I prove that $coC \subseteq C$? --------- I came up with thi...
Complexity Class theorization
|complexity-theory|complexity-classes|
Let us iterate over all columns of the matrix from left to right. For each column let us iterate over all cells in this column and for each cell $A[i,j]$ let us compute the number of submatrices of ones, having $A[i,j]$ as the bottom-right corner. Let $\mathcal{T}[i,j]$ be the set of all these submatrices, and $T[i,j] ...
As the question suggest. I want an algorithm that runs in linear time which finds the longest prefix of a substring that is a sub-prefix of the same string. **Formally:** ___ Given a string $S$ of length $n$, we define a function $\delta_{S}:$ $[n]\rightarrow[n]$ as follows. For each integer $w$, $1\le w\le n$...
null
null
Here is a solution in $O(n\cdot m)$, where $n, m$ are the number of rows and columns respectively. Let us iterate over all columns of the matrix from left to right. For each column let us iterate over all cells in this column and for each cell $A[i,j]$ let us compute the number of submatrices of ones, having $A[i,j]...
I need to create a DFA for a *sequential* order of states e.g. A -> B -> C -> B -> A and so on, where 'A' is the start and finish state 1 is a transition to the next state and 0 just loops back to the current state. 1 and 0 are the only inputs The only way I've found I can do it in this order is by having...
Can a DFA have multiple of the same state?