## algorithms – How to close an open polygonal chain such that the resulting enclosed area includes polygon B and excludes polygon C?

I have an algorithmic question for you:

Given: 3D triangle surface mesh, open polygonal chain A, closed polygon B, closed polygon C.

Wanted: A line that closes A such that B lies within the enclosed area and C does not.

A correct solution would be the green line, a wrong solution would be the red line.

I am currently looking at it as a graph and walking through the graph with a shortest path algorithm. While I can guarantuee that the solution does not intersect polygons B and C by blocking the relevant edges, I have trouble finding a way to prescribe that they can/cannot be enclosed.

I realize I am asking two things at once, a] “how to include a given area?” and b] “how to exclude a given area?”. So if you have a solution for either one or even both questions, please let me know, I am grateful for any help! Posted on

## algorithms – Why is the time complexity of the Bit Manipulation solution to Binary Addition O(M + N)?

I am trying to understand why the time complexity of the Bit Manipulation solution (https://leetcode.com/problems/add-binary/solution/) to the Binary Addition problem is O(M + N), where M and N are the lengths of the input strings a and b, respectively.

From my understanding, the worst case scenario is that each column in the addition will result in a carry, and since there are max(N, M) columns, the time complexity should be O(max(N, M))?

Posted on

## approximation – Voice activation keywords algorithms on iot devices?

I’ve been scouting the internet for days and after a lot of digging, I decided to resort to my last hope and ask: I’m talking about the “hey Google”, “Alexa” and so on voice activations. All results I could find suggested recurrent neutral networks but I have some serious doubts about that for several reasons:

Running an rnn requires a lot of resources, both memory and CPU. None of which are available in both Alexa and Google home devices-they are really underpowered and have hardly any resources available to spare.

“Hey Google” is also available on pixel phones and while they have plenty of system resources, to my mind this would seriously impact battery life.

So my question is if anyone knows what might be going on here – we are looking at incredible accuracy on hardware that’s significantly less potent than a first gen raspberry pi?

Posted on

## algorithms – Seemingly simple path finding problem, but graph with travelling salesman or shortest path does not work

I have multiple lines, some of them are open, some are closed. On one side of each line there is “inside”, the other side “outside”. Inside is shown with small strokes next to the line. Now I have to close all open lines with connecting lines. Rules:

1. It is possible to connect multiple open lines together, or to connect an open line with itself.
2. The solution must be such that all areas that are defined through the lines are consistent on whether they are inside or outside.
3. All open lines must be closed in the final solution.

I am looking for an algorithm to compute this. At the moment, I am tackling it as a graph and looking for paths through the graph. I tried shortest path and travelling salesman/hamiltonian path, but both fail to comply with rule 2. Since the closed lines are needed to find the correct solution, and they are not part of the graph, possibly a graph is the wrong tool in the first place.

Any ideas?

Below are some images for clarification:    Posted on

## algorithms – If in a given text the frequency of the letter A is 0.5, then the number of bits encoding in the Hoffman code for the text is 1

Studying for my finals. I saw the following question:

Prove or disprove: If in a given text the frequency of the letter A is 0.5, then the number of bits encoding in the Hoffman code for the text is 1.

In the solution they said that this statement is true but didn’t explain why. I’m trying to prove this statement for myself but struggling how to “show it”. Why it’s true?

Posted on

## randomized algorithms – A so-called random variable not being well-defined

Consider this algorithm:

`````` RANDOMIZED-SELECT(A, p, r, i)
1 if p == r
2  return A(p)
3 q = RANDOMIZED-PARTITION(A, p, r)
4 k = q - p + 1
5 if i == k // the pivot value is the answer
6  return A(q)
7 elseif i < k
8  return RANDOMIZED-SELECT(A, p, q - 1, i)
9 else return RANDOMIZED-SELECT(A, q + 1, r, i - k)
``````

The book CLRS claims that if we set $$T(n)$$ to be the running time of the algorithm on an input $$A$$ of size $$n$$, then $$T$$ is a random variable. However, I think it’s not since if we define $$Omega$$
to be the set of all inputs of all sizes $$n$$ and set $$mathcal{F}$$ as $$mathcal{P}(Omega)$$ and let $$T$$ to be a function from $$Omega$$ to $$mathbb{R}$$, if $$n = m$$ then it doesn’t follow that $$T(n) = T(m)$$ since the algorithm might have different running times on arbitrary inputs of the same size. Also note that the algorithm is randomized. So why does it say that $$T$$ is a random variable?

Posted on

## algorithms – Reduction from Edge-Coloring and Vertex-Coloring to a new problem

I have a question from a test I did and failed, a question I failed to do.

In short:

the question is about reduction from Vertex-coloring and Edge-coloring, to a new problem they have defined.

The new problem is called Min-Monochromatic. Want to show that this problem belongs to $$NP$$ , and later make a reduction from Edge-Coloring and Vertex-Coloring to it.

The question has three parts, all parts are related to each other. The first part, which is the easiest of the three I was able to do in my opinion.

The second part shows a reduction from Vertex-Coloring to Min-Monochromatic, ie $$Vertex-Coloring leq_p Min-Monochromatic$$:
I could not do that properly.

The third part should show a reduction from Edge-Coloring to Min-Monochromatic, i.e. $$Edge-Coloring leq_p Min-Monochromatic$$ : I also could not do for the same reasons of the second part, and in addition the question is more complex, there is a reference between Edge-Coloring and Vertex- Coloring.

The question:

Let’s look at the Min-Monochromatic problem. In this problem, we are given 3 things:

• Undirected graph, G = (V, E).
• Natural number $$k in mathbb{N}$$ so that $$k> 0$$.
• Another natural number $$l in mathbb{N}$$ so that $$l> 0$$.

We are asked if there is a color function $$varphi : Vrightarrow begin{Bmatrix} 0,cdots,k-1 end{Bmatrix}$$ so that there are at most $$l$$ pairs of neighbors with the same color. Note that the color function $$varphi$$ is not necessarily valid (in this problem we are allowed to have neighbors of the same color, the goal is only to minimize the number of cases). We want to show that the problem is NP-complete. We will see this in parts.

Part One:

Show that $$Min-Monochromatic(G,k,l) in NP$$. You must explain the correctness of the validation algorithm and analyze the runtime.

Given an instance $$(G, k, l)$$ of the Min-Monochromatic problem. The witness who is a subgroup $$U subseteq V$$. Graph $$G_U = (U,E_U)$$ was built.

The validation algorithm will first count the different colors that the vertices in the graph have. If the number is less than or equal to k, we will continue.

After this we will define a counter c = 0. For each edge in the graph, if the color of the vertices to which it is connected is the same, we will increase the counter by 1.

If the counter is less than or equal to $$l$$, we return true

The algorithm is polynomial, we will go through each edge and each vertex exactly once.

Part Two:

Show that $$Min-Monochromatic$$ is $$NP-complete$$. You must:

• Build a reduction that will see it.
• Prove its correctness.
• Analyze the running time.

(Instruction: You can show $$Vertex-Coloring leq_p Min-Monochromatic$$ reduction and assume that $$Vertex-Coloring$$ is NP-complete.)

Here I could not continue. I was thinking of doing a reduction from $$Min-Monochromatic$$ to $$Vertex-Coloring$$ . You can add one more color, each time of adjacent vertices with the same color, delete the edge between them, and connect them to the new vertex. But in question they asked for the reduction and vice versa, so I could not continue.

Part Three:

Rely on the previous reduction and show $$Edge-Coloring leq_p Min-Monochromatic$$.

You must:

• Build a reduction that will see it.
• Prove its correctness.
• Analyze the running time.

In addition: Based solely on our conclusion from section B and this reduction, can it be determined to which class Edge-Coloring belongs? Explain and detail

I think show reduction: $$Edge-Coloring leq_p Vertex-Coloring$$.And because of the reduction from the second part it should work.

To show reduction from Edge-Coloring to Vertex-Coloring, it’s not complicated.

Reduction from Edge-Coloring to Vertex-Coloring, creates a vertex for each edge, and creates an edge $$(v_e, v_f)$$ between each pair of different e and f edges that share a vertex.

In my opinion it is not possible to know only by the reduction in Part B to which class Edge-Coloring belongs. Because Edge-Coloring does not participate in this reduction at all.

I would be happy to help regarding the second and third part, which I could not understand properly, and the second part I could not do.

Posted on

## algorithms – issue with a task scheduling problem asked during a recent software engineering interview

I recently went through a interview session for a SWE/CS role at a well known company.
It wasn’t specifically a “coding-round” but was titled a “domain interview” session, so I assumed the interviewer was interested in an discussion/high-level-solution related to the problem.

The problem:

You’re given a computation graph with nodes representing computations, edges representing dependence, and the values on edges representing the data-size in MB (input or output) at that edge.
You want to schedule this graph on a single-processor with limited memory `X` MB.
To execute a node/computation, both inputs and outputs data should fit within X memory of the processor.
It’s given for for each node sum of its inputs/outputs data <= X.
If at any time the data the total data that needs to be stored execeeds X, then there is extra cost per MB of fetching data from the disk. (it can happen with node n1 has produced an output that needs to be used by n2 but not all predecessors of n2 have run yet).
How would you schedule this graph?

My approach/What I discussed:

• I answered by saying that “in general” this problem is intractable(NP-complete) for multi-processor systems, and likely too for single-processor system due to the memory constraint.
• Then I suggested a greedy approach based on keeping track of all ‘ready’ nodes at a given time that can be executed as their predecessors have been executed.
• Which specific node to schedule next from several available/ready
ones can be done based on trying to minimize the cost associated with
memory constraint (i.e. seeking to schedule the one with the min cost
associated with data transfer).
• If there are several nodes with same minimum cost associated with
data transfer, the pick the node among them with the biggest
input+output mem requirement.

During the session, the interviewer didn’t suggest many things or gave any tips or direction on which directions he may have wanted me to lead to, so it turned out to be very open-ended.

He was almost neutral or very slight camera/tight-lipped smile taking notes most of the time.

I couldn’t gauge exactly what interviewer may have been looking for due to the intractableness of the problem — it’s hard to come up with a general optimal algo for such at the top of your head.

Later I came to know I received a ‘no’ decision for this session.

Issue:

This has me puzzled. I’ve been racking my brain on if I didn’t approach it correctly, or if the interviewer was expecting one to talk about something else that the things I mentioned.

Is there something optimal or direct I missed?

Could there be some specific algo that interviewer may have been looking for that generally one is expected to know?

During the session, I thought I was providing a reasonable solution given the constraints of the problem, and could have deep-dived into any direction that interviewer may have asked me to.

But didn’t get any push-back or suggestions from the interviewer during the session, i’m interested to know what i missed or how I should have approached this problem.

Posted on

## algorithms – Need the type of time complexity and its formula

If the complexity of my problem is $$O(f_n(n))$$ begins at $$n =4$$ and increases in this sequence:

At

$$n = 4$$ the number of operations = $$(n – 2)$$,

$$n = 5$$ the number of operations = $$((n – 2) (n-2)(n-3)/2)$$

$$n = 6$$ the number of operations = $$((n – 2) (n-2)(n-3) (n-3)(n-4)/4))$$

$$n = 7$$ the number of operations = $$((n-2) (n-2)(n-3) (n-3)(n-4)(n-4)(n-5)/8)$$

$$n = 8$$ the number of operations = $$((n-2) (n-2)(n-3) (n-3)(n-4)(n-4)(n-5)(n-5)(n-6)/16 )$$ … etc.

1. How to formulate the $$f_n(n)$$ for all $$n$$?
2. What is the type of its time complexity?
Posted on

## algorithms – Reduction from IS problem to other problem

Given graph 𝐺 = (𝑉, 𝐸) it is said that it is a `star` if there is a vertex $$𝑣_0 ∈ 𝑉$$ so that all the other vertices are connected exclusively to it (and not to each other.)

That is, $$𝐸 =$${ $$(𝑣_𝑖, 𝑣_0) | 𝑣_𝑖 ∈ 𝑉$$ }. Also, given graph 𝐺 = (𝑉,𝐸) we will also look for its sub-graphs, which are a `star`. Which is the center of the star.) For example, the following graph: Is not a star, but it contains an induced sub-graph (above the vertices 𝑎, 𝑏, 𝑐, 𝑒, 𝑓) which is a star of size 5: In the optimized version of the problem, we will be interested in finding the largest induced sub-graph that is a star. In the decision version, we also receive as input a natural number 𝑘, and are asked “Is there an induced sub-graph of 𝐺 which is a star ≤ k.”?

Formally:

STAR = { $$left langle G,k right rangle |$$There is a $$Usubseteq V$$subgroup so the induced sub-graph $$G_U = (U, E_U)$$ which is a star ≤ k in size. }

We want to show that the problem is NP-complete. We will see this in parts.

section A

Show that 𝑆𝑇𝐴𝑅 (𝐺, 𝑘) ∈ 𝑁𝑃. You must explain the correctness of the validation algorithm and analyze its runtime.

section B

Show that 𝑆𝑇𝐴𝑅 is NP-complete. You must:

• Build a reduction that you will see,
• prove its correctness,
• and analyze its running time.

(Instruction: You can show a reduction 𝑆𝑇𝐴𝑅 ≤𝑃 𝐼𝑆 and assume that 𝐼𝑆 is NP-complete)

`The direction I was thinking of is to first show that STAR belongs to NP, to say that there is a graph G and a natural number k. The witness will give us an example of the graph and then it will be possible to verify or reject if the graph is STAR.`

`After this, show a reduction from IS to STAR, say that there is a graph `$$G_{IS}$$`, which belongs to IS, and from it make a reduction to STAR, what can be done is to add one more vertex to IS, and then it will be STAR. `

`In terms of correctness, it should be seen that if you add a vertex to IS it becomes STAR, and if you remove a vertex that is connected to all the other vertices in STAR, it is IS. `

`It is a polynomial run time that is passed over each edge once, and the number of edges can be, as the number of vertices squared.`

This is the solution I’m thinking of, but I do not know if it is a
correct solution, there are a number of things that need to be done
and proven, and I do not know if I do it right and list it properly.

For the first part I think I did it right, for the second part is not
complicated, but I do not know if I explained enough

section A:

Given a graph 𝐺 = (𝑉, 𝐸) and a natural number 𝑘 ∈ ℕ our witness will be a subset of vertices 𝑈 ⊆ 𝑉. We initialize an array C by the size of the number of vertices V ← 0.
We will go through each pair of different 𝑢1, 𝑢2 ∈ 𝑈 vertices and check whether (𝑢1, 𝑢2 ∈ 𝐸)?
If so, we will increase the content in the position of the vertices in the array by 1. If there is a vertex whose counter is equal to | V | -1 and all the other vertices of their counter are 1, we will return true, otherwise we will return false.

correctness:

(𝐺, 𝑘) ∈ STAR ⇔ There is a group 𝑈 ⊆ 𝑉 almost independent in size 𝑘 ≥ | 𝑈 | ⇔ There is one vertex with a degree of | V | -1 and the other vertices with a degree of 1 ⇔ have 𝑈 ⊆ 𝑉 in size 𝑘 ≥ | 𝑈 | For which the verification algorithm returns truth ⇔ there is a witness for whom the verification algorithm will return truth ⇔ the verification algorithm gets the (𝐺, 𝑘).

section B

We will reduce $$IS leq _p STAR$$, given the instance $$(G, k)$$ of the IS problem. We will create a new vertex x. We will define $$widetilde{V} = 𝑉 cup {𝑥, 𝑦}$$. Also, $$widetilde{E} = E cup begin{Bmatrix} (x,e_1),cdots,(x,e_{|v|}) end{Bmatrix}$$. That is, the vertex is connected to the other vertices finally $$widetilde{G} = (widetilde{V}, widetilde{E})$$. We will define our reduction function: $$f(G,k) = (widetilde{G},k+1)$$.

The reduction performs a fixed amount of operations, and is therefore polynomial.

We will see its correctness:

$$(G,k) in IS Leftrightarrow (widetilde{G},k+1) in STAR$$

$$Rightarrow$$

Suppose that $$(G,k) in IS$$

We can add vertex x, to subgroup U of vertices V. and | U | Greater than or equal to k. Because we add one vertex, we will turn a set of vertices that have no edge between them, into a set of vertices that are all connected only to vertex x.

That is $$(widetilde{G},k+1) in STAR$$

$$Leftarrow$$

Suppose that $$(widetilde{G},k + 1) in STAR$$. There is a subset $$widetilde{U}$$ of the vertices $$widetilde{V}$$. and | $$widetilde{U}$$ | Greater than or equal to k + 1.

We know that there is an x vertex to which all the other vertices are connected, we will delete that vertex. We get a set of vertices in size | V | Of IS, and with vertices not connected to each other

That is $$(G,k) in IS$$

`Not sure if I answered the question properly, I also have a hard time showing the correctness of section 2, I do not know if my correctness covers all cases. I'm not sure if I answered it correctly.`

Posted on