## How Dijkstra’s algorithm forms shortest path tree when multiple nodes have same shortest path length

I came across following problem:

Consider below graph: What will be the shortest path tree starting with node $$A$$ returned by Dijkstra’s algorithm, if we assume priority queue is implemented as binary min heap?

My solution:

I referred Dijkstra from CLRS: With A as a starting node, we will have priority queue bin min heap as follows:

A{root, 0}     | Rest all:{∅,∞} 

(Notation: {parent in SPT, shortest path weight})

It will extract A from priority queue and add it to SPT and will relax AC and AB:

    B{A:5}      /  \ C{A:6}  Rest all:{∅,∞}  

It will extract B from priority queue and and add it to SPT:

   C{A:6}       | Rest all:{∅,∞}  

and will relax BE:

            C{A:6}              /   \ Rest all:{∅,∞}   E{B,6} 

Next it will extract C and so one. Thus the SPT will be: But not: Q1. Am I correct with above?
Q2. CLRS algo does not dictate which node to add to SPT if multiple of them have same shortest path weight. Hence its dependent on how priority queue is implemented. If no information was given about how priority queue was implemented then we cannot tell how SPT will be formed. Am I right?

Posted on Categories proxies

## Expected length of a random walk on a line

I am given the following randomized algorithm for SAT,

• Input: A satisfiable CNF-formula $$\varphi$$.
• Output: An assignment $$\rho$$, such that $$\rho \models \varphi$$.

The algorithm works as follow:

1. Choose an arbitrary assignment $$\rho$$.
2. As long as $$\rho \not \models \varphi$$
1. Choose a clause in $$\varphi$$ not satisfied by $$\rho$$ uniformly at random.
2. Choose a variable $$x\in_{u.a.r}\operatorname{VAR}(C)$$.
3. Flip the value of $$\rho(x)$$ (set $$\rho(x) = \overline{\rho(x)}$$).

We have to prove that for a 2-CNF Formula, the algorithm has polynomial expected running time.

I have proven that for a fixed assignment $$\alpha$$, such taht $$\alpha \models \varphi$$, with probability $$p \geq 1/2$$, after each iteration the number of variables that are assigned different values in $$\alpha$$ and $$\rho$$ decrease by one. With probability $$1-p$$, the assignments $$\rho$$ and $$\alpha$$ differ at one extra variable.

Now I have to prove, that the algorithm finished in expected polynomial number of steps. I was able to add one more step of abstraction. Let $$X_i$$ be the random variable that indicates the number of steps needed to make $$\rho = \alpha$$, when $$\rho$$ and $$\alpha$$ differ by exactly $$i$$ variables. Then it holds that $$E[X_i] = 1 + p E[X_{i-1}] + (1-p) E[X_{i+1}],$$ and $$X_i \leq X_{i+1}$$ for all $$i$$ and $$E[X_0]$$ is equal to 0. We need to find a polynomial bound for $$E[X_i]$$.

Since $$p\geq 1/2$$ and $$X_i \leq X_{i+1}$$, the following must hold $$E[X_i] \leq 1 + \frac{E[X_{i-1}] + E[X_{i-1}]}{2}$$

Now this can bee seen as walking on the integer line, in each step we move either one step to the left or one step to the right and the probability of moving to the left is at least one half. We have to prove that in expected polynomial many steps (polynomial in the starting position), we reach the number $$0$$ on the line.

Any help on this problem is very appreciated 🙂

## HLSL : Offset and length were out of bounds

//Vertex Shader Constants  float4x4 World; float4x4 View; float4x4 Projection; float4x4 WorldViewIT;  //Color Texture  texture Texture;  //Normal Texture texture NormalMap;  //Specular Texture  texture SpecularMap;  //Albedo Sampler  sampler AlbedoSampler = sampler_state  {      texture = <Texture>;     MINFILTER = LINEAR;       MAGFILTER = LINEAR;       MIPFILTER = LINEAR;       ADDRESSU = WRAP;       ADDRESSV = WRAP;  };  //NormalMap Sampler  sampler NormalSampler = sampler_state  { texture = <NormalMap>;   MINFILTER = LINEAR;   MAGFILTER = LINEAR;   MIPFILTER = LINEAR;   ADDRESSU = WRAP;   ADDRESSV = WRAP;  };  //SpecularMap Sampler sampler SpecularSampler = sampler_state {  texture = <SpecularMap>;   MINFILTER = LINEAR;   MAGFILTER = LINEAR;   MIPFILTER = LINEAR;   ADDRESSU = WRAP;   ADDRESSV = WRAP;  };  //Vertex Input Structure  struct VSI {  float4 Position : POSITION0;   float3 Normal : NORMAL0;   float2 UV : TEXCOORD0;   float3 Tangent : TANGENT0;   float3 BiTangent : BINORMAL0; };  //Vertex Output Structure  struct VSO {  float4 Position : POSITION0;  float2 UV : TEXCOORD0;   float3 Depth : TEXCOORD1; float3x3 TBN : TEXCOORD2; };  //Vertex Shader  VSO VS(VSI input) { //Initialize Output  VSO output;  //Transform Position   float4 worldPosition = mul(input.Position, World); float4 viewPosition = mul(worldPosition, View); output.Position = mul(viewPosition, Projection); //Pass Depth   output.Depth.x = output.Position.z; output.Depth.y = output.Position.w; output.Depth.z = viewPosition.z;  //Build TBN Matrix   output.TBN = normalize(mul(input.Tangent, (float3x3)WorldViewIT)); output.TBN = normalize(mul(input.BiTangent, (float3x3)WorldViewIT)); output.TBN = normalize(mul(input.Normal, (float3x3)WorldViewIT));  //Pass UV   output.UV = input.UV;  //Return Output   return output; }  //Pixel Output Structure  struct PSO { float4 Albedo : COLOR0; float4 Normals : COLOR1; float4 Depth : COLOR2; };  //Normal Encoding Function  half3 encode(half3 n) { n = normalize(n);  n.xyz = 0.5f * (n.xyz + 1.0f);  return n; }  //Normal Decoding Function  half3 decode(half4 enc) { return (2.0f * enc.xyz - 1.0f); }    //Pixel Shader  PSO PS(VSO input) {  //Initialize Output   PSO output;  //Pass Albedo from Texture   output.Albedo = tex2D(AlbedoSampler, input.UV); //Pass Extra - Can be whatever you want, in this case will be a Specular Value   output.Albedo.w = tex2D(SpecularSampler, input.UV).x;  //Read Normal From Texture   half3 normal = tex2D(NormalSampler, input.UV).xyz * 2.0f - 1.0f;  //Transform Normal to WorldViewSpace from TangentSpace  normal = normalize(mul(normal, input.TBN));  //Pass Encoded Normal  output.Normals.xyz = encode(normal); //Pass this instead to disable normal mapping  //output.Normals.xyz = encode(normalize(input.TBN));    //Pass Extra - Can be whatever you want, in this case will be a Specular Value  output.Normals.w = tex2D(SpecularSampler, input.UV).y;  //Pass Depth(Screen Space, for lighting)  output.Depth = input.Depth.x / input.Depth.y;  //Pass Depth(View Space, for SSAO)  output.Depth.g = input.Depth.z;  //Return Output   return output; }  //Technique  technique Default { pass p0 {     VertexShader = compile vs_3_0 VS();     PixelShader = compile ps_3_0 PS(); } } 

This is my current shader… it is producing a “Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection” argumente axeception… I don’t understand what’s wrong with it….

Any help is appreciated.

## Understanding PDA for odd length string with middle symbol 0

I came across this pdf, which describes the language of odd length string with middle symbol 0 as follows: Doubts:

1. I dont understand the transition labels. In standard resources like books by Ullman et al, Linz and in wikipedia, the transition labels take following form:

• $$a,b/ab$$ means if next input symbol is $$a$$ and current stack top is $$b$$, then push $$a$$ on $$b$$
• $$a,b/\epsilon$$ means if next input symbol is $$a$$ and current stack top is $$b$$, then pop $$b$$
• $$a,b/a$$ means if next input symbol is $$a$$ and current stack top is $$b$$, then pop $$b$$ and push $$a$$

I dont get meaning of transition labels in diagram $$a,b\rightarrow c$$. Some one explained me that its, if next next input symbol is $$a$$, pop $$b$$ and push $$c$$. I feel, if this interpretation is correct, then this notation is insufficient as it will describe both $$a,b/ab$$ and $$a,c/ac$$ as $$a,\epsilon\rightarrow a$$. Am I right with this, or I understood the notation incorrectly?

2. Assuming above interpretation to be correct, loop on $$q_1$$ pushes all input symbols, be it 1 or 0. Then for $$0$$ at any position (not necessarily middle position), it transits to $$q_2$$. Loop at $$q_2$$ pops all symbols. I dont get how above PDA forces middle symbol to be $$0$$. Also I dont get how it ensures length of $$w$$ is odd.

3. If given PDA is incorrect, can we prepare correct one by re-labelling as follows:

• Loop at $$q_0$$: $$\{(1, /1);(0, /1);(0,0/00);(0,1/01);(1,0/10);(0,1/01)\}$$
• Transition $$q_0-q_1$$: $$\{(0,0/0);(0,1/1)\}$$
• Loop at $$q_2$$: $$\{(0,0/\epsilon);(0,1/\epsilon);(1,0/\epsilon);(1,1/\epsilon)\}$$

So, its CFL not deterministic CFL, right?

Posted on Categories proxies

## Length of strings accepted by DFA

Problem: Given a DFA $$D$$, find all possible lengths of strings accepted by the $$D$$.

It makes sense that these lengths can be represented as $$a_i+kb_i$$. What might be the algorithm to find all such pairs $$(a_i, b_i)$$?

## Effects of parity bit on odd code length regarding its size after alternation

I am trying to understand code distance, but I am not sure regarding the following scenario:

Assume that you have an information word M with m bits, that You code into a coding word using the following procedure:

1. You take M and code it using a code you know nothing about. But you do know that its distance(code’s distance) is k, and that k>2.
2. You add to the word obtained in the last step a parity bit.

If you know that k is odd, does it necessarily mean that the distance of the code constructed by those two steps is necessarily k+1?

Well, I am not sure here. Code distance is basically the number of different bits, and the meaning of the second step in the procedure(adding a parity bit) is, according to my understanding, allowing the detection of any odd number of errors. Does it necessarily mean that the code constructed by those two steps is necessarily k+1?

Posted on Categories proxies

## finding amount of errors that can be fixed based on code length [closed]

i tried to look online and search this site and others but haven’t found any good explanation to the following simple question: how many errors can a code with length k(k>2)fix?

Posted on Categories proxies

## Is checking if the length of a C program that can generate a string is less than a given number decidable?

I was given this question:

Komplexity(S) is the length of the smallest C program that generates the string S as an output. Is the question “Komplexity(S) < K” decidable?

With respect to decidability, I only know about the Halting Problem and just learned about Rice’s Theorem while searching online (though I don’t think it can be applied here?). I couldn’t reduce the problem to any undecidable problem I know about. Thanks in advance for any help

Posted on Categories proxies

## Dynamic length of union of segments (1d Klee’s measure problem)

Finding the length of union of segments (1-dimensional Klee’s measure problem) is a well-known algorithmic problem. Given a set of $$n$$ intervals on the real line, the task is to find the length of their union. There is a simple $$O(n \log n)$$ solution involving sorting endpoints of all intervals and then iterating over them while keeping track of some counters.

Now let’s look at a dynamic variant of this problem: we are receiving $$n$$ segments one after another and the task is to find the length of the current union of segments after each new segment arrives. It seems like a natural generalization, however, I cannot find much information about this version of the problem. I wonder if it is still possible to solve this problem in $$O(n \log n)$$ time in an offline setting, i.e. when we are given all segments beforehand and can preprocess them in some way before giving an answer for each segment.

I feel like a modification of a segment tree might be useful here. In every node we need to keep track of both the total sum of lengths of segments (or their parts) corresponding to this node and the length of their union. However, I cannot figure out how to implement this modification without performance of one update degrading to linear time in some cases. Maybe using a segment tree is not the right approach and a better way exists.

Posted on Categories proxies

## Should user input be validated/checked for it’s length in PHP (server side) as a security measure?

important to note that this user input is something that after validation & sanitation – will be inserted into a database, and later on be shown to other users on the same web site. (example: a forum) I’m referring to both a case when I know in advanced what’s the length I should expect from the user and a case in which I don’t but know vaguely that’s not more than 100 length. I’m trying to figure out if there is any security advantages for checking user input length in PHP. taking into account I’m already validation & sanitation user input based on the type of content I’m expecting using regex. I know this differs from language to language to I want to refer to PHP this time, but any referring to other language like Java, .NET, python etc. would be fine.

Posted on Categories proxies