## algorithms – Minimum vertex cover and odd cycles

Suppose we have a graph $$G$$. Consider the minimum vertex cover problem of $$G$$ formulated as a linear programming problem, that is for each vertex $$v_{i}$$ we have the variable $$x_{i}$$, for each edge $$v_{i}v_{j}$$ we have the constraint $$x_{i}+x_{j}geq 1$$, for each variable we have $$0leq x_{i}leq 1$$ and we have the objective function $$min sumlimits_{1}^{n}{x_{i}}$$. We say such a linear programming problem LP. Note that it is NOT an integer linear programming problem.

We find a half integral optimal solution of LP that we say $$S_{hi}$$. For each variable $$x_{i}$$ that takes value 0 in $$S_{hi}$$, we add the constraint $$x_{i}=0$$ to LP.

For each odd cycle of $$G$$, add to LP the constraint $$x_{a}+x_{b}+x_{c}+…+x_{i}geq frac{1}{2}(k+1)$$ where $$x_{a},x_{b},x_{c},…,x_{i}$$ are the vertices of the cycle and $$k$$ is the number of vertices of the cycle. We find a new optimal solution of LP that we say $$S$$.

If $$x_{i}$$ is a variable that takes value $$0.5$$ in $$S_{hi}$$ and value $$gt 0.5$$ in $$S$$, can we say that there is at least a minimum vertex cover of $$G$$ that contains the vertex associated to $$x_{i}$$?

The idea behind the question: in an odd cycle $$c$$ with $$k$$ vertices, the number of vertices needed to cover the cycle is $$frac{1}{2}(k+1)$$, therefore for each odd cycle we add to LP the constraint $$x_{a}+x_{b}+x_{c}+…+x_{i}geq frac{1}{2}(k+1)$$. If in $$S_{hi}$$ the sum of the variables of $$c$$ is $$frac{k}{2}$$ (that is all the variables of $$c$$ take value $$frac{1}{2}$$), then in $$S$$ at least a variable $$x_{i}$$ of $$c$$ takes vale $$gt frac{1}{2}$$ and the vertex associated to $$x_{i}$$ belongs to at least a minimum vertex cover of the given graph.

## algorithms – How does Backtracking work in this Sudoku solver? How is the next “path” chosen

I have this solve function.

From my understanding the backtracking part of this algorithm is the line after the recursive solve().
I would think that it would leave 0’s in the final board.
Say if I get to a spot on the board where it needs to backtrack, wont it assign `grid(y)(x) = 0` then advance to the next possible value `n`? How is the next “path” (next `n` value) chosen when backtracking?

``````   def solve():
for y in range(9) :
for x in range(9) :
if grid(y)(x) == 0 :
for n in range(1,10) :
if possible(y,x,n) :
grid(y)(x) = n
solve()
grid(y)(x) = 0
return
pprint(grid)
``````

## C++ Practice on STL templates and algorithms

I have implemented the following program below for an assignment for practice on STL templates and algorithms. All I’m doing is implementing the codes for the printing of empty files, un-empty files, etc.. I would love to know if there is any way to make the code more optimized.

Note: From the requirements of my school (Must be adhered to):

Codes with comment as “helper function” should not be modified. These are `empty_check()`, `split()`, and `print_filename()`.

Functions commented as “Can modify” means the codes within that function can be modified.

`int main()` should not be modified

No use of lambda expressions.

No use of operators:

. (member access); -> (member access via a pointer); * (dereference).

No use of explicit iteration (for, while, do while) or selection (if, switch, ?:) statements or operators.

No use of keyword auto.

No use of std::cout, std::cerr or any other functions that perform printing of the text. I’ll have to use the provided helper function to do it.

``````#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <functional>
#include <utility>

using name_file = std::string;
using sizeFile = size_t;
using record_in_file = std::pair<name_file, sizeFile>;
using file_datas = std::map<name_file, sizeFile>;

bool empty_check( //helper function
const record_in_file& dataRecord,
bool true_if_its_not
)
{
sizeFile size;
std::tie(std::ignore, size) = dataRecord;
bool result = (size == 0);
if (true_if_its_not)
{
result = !result;
}
return result;
}

name_file split(const record_in_file& dataRecord) //helper function
{
name_file name;
std::tie(name, std::ignore) = dataRecord;
return name;
}

void print_filename(const name_file& name1) //helper function
{
std::cout << " * " << name1 << std::endl;
}

void file_names_print(const file_datas& map1) //can modify
{
std::vector<name_file> files;
std::transform(std::begin(map1), std::end(map1), std::back_inserter(files), split);
std::for_each(std::begin(files), std::end(files), print_filename);
}

size_t files_un_empty_print(const file_datas& map1) //can modify
{
std::vector<record_in_file> files;
std::copy_if(std::begin(map1), std::end(map1), std::back_inserter(files),
std::bind(empty_check, std::placeholders::_1, true));
std::vector<name_file> file_names;
std::transform(std::begin(files), std::end(files), std::back_inserter(file_names),
split);
std::for_each(std::begin(file_names), std::end(file_names), print_filename);

return std::count_if(std::begin(map1), std::end(map1),
std::bind(empty_check, std::placeholders::_1, true));
}

size_t files_empty_print(const file_datas& map1) //can modify
{
std::vector<record_in_file> files;
std::copy_if(std::begin(map1), std::end(map1), std::back_inserter(files),
std::bind(empty_check, std::placeholders::_1, false));

std::vector<name_file> file_names;
std::transform(std::begin(files), std::end(files), std::back_inserter(file_names),
split);

std::for_each(std::begin(file_names), std::end(file_names), print_filename);

return std::count_if(std::begin(map1), std::end(map1),
std::bind(empty_check, std::placeholders::_1, false));
}

std::tuple<file_datas&>  get_param(file_datas& map1) //can modify
{
return std::forward_as_tuple<file_datas&>(map1);
}

void empty_removal(file_datas& map1) //can modify
{
std::vector<record_in_file> files;
std::copy_if(std::begin(map1), std::end(map1), std::back_inserter(files),
std::bind(empty_check, std::placeholders::_1, true));

file_datas n_map{ std::begin(files),std::end(files) };

std::swap(map1, n_map);
}

int main()
{
file_datas map = {
{"main.exe", 10000},
{"save.bak", 0},
{"library.dll", 1243},
{"0.res", 121100},
{"1.res", 121100},
{"2.res", 115600},
{"errors.log", 0}
};

std::cout << "Files:" << std::endl;
file_names_print(map);
std::cout << std::endl;

std::cout << "Files that are not empty:" << std::endl;
size_t Count_of_unemptyFiles = files_un_empty_print(map);
std::cout
<< " There are "
<< Count_of_unemptyFiles
<< " non-empty files.n"
<< std::endl;

std::cout << "Files that are empty:" << std::endl;
size_t Count_of_emptyFiles = files_empty_print(map);
std::cout
<< " There are "
<< Count_of_emptyFiles
<< " empty files.n"
<< std::endl;

std::cout << "Files after removing the empty ones:" << std::endl;
auto parameters = get_param(map);
std::apply(empty_removal, parameters);
file_names_print(map);
}
``````

## algorithms – Counting a number of sequences where the item is the most frequent one

Consider an array $$a$$, where elements are numbers. Consider all subsequence of $$a$$. For each subsequence $$s$$, we find an element $$k$$ with the largest number of occurrences in $$s$$ (if there are several options, choose the smallest such $$k$$).

Problem: for each number $$k$$, find the number of subsequences of $$a$$ for which $$k$$ is the chosen number.

Example:

``````Input: (1, 2, 2, 3)
Output:
1 -> 6 ((1), (1, 2), (1, 2'), (1, 3), (1, 2, 3), (1, 2', 3))
2 -> 8 ((2), (2'), (2, 3), (2', 3), (2, 2'), (1, 2, 2'), (2, 2', 3), (1, 2, 2', 3))
3 -> 1 ((3))
``````

My idea is that, I make a map $$cnt$$ where $$cnt(x)$$ is number of times $$x$$ occurred in array $$x$$. I am trying to find the answer for $$k$$ then i will create a map $$temp$$ where $$temp(i)=min(cnt(k), cnt(i))$$. Then consider the value of $$cnt(k)$$ to be $$m$$ then in a subset element $$k$$ can occur from $$0$$ to $$m$$ times and using this i will find answer for all of its occurrences from $$0$$ to $$m$$ which is a simple number of sub sets problem and finally i will have my answer.

But the complexity of my algorithms is bad as it is quadratic or worse. Can it be improved?

## algorithms – Counting a property of every sub-sets

Consider a array $$a$$ of length $$n$$ and $$a_i and a array of length $$n$$ ,$$b=(0,0….(n$$ $$times))$$.Consider every sub-set of array $$a$$ and denote it by $$s$$ i need to find a number $$k$$ with the largest number of occurrences or the most frequent one, If there are several options, choose the smallest one and then increase $$b(k)$$ by one .I need to final $$b$$ after considering all sub sets.

My idea is that ,I make a array $$val$$ of length $$n$$ where $$val(i)$$ is number of times $$i$$ occurred in array $$a$$ .Consider that i am trying to find value for $$b(k)$$ then i will create a array $$temp$$ of length where $$temp(i)=min(val(k),val(i))$$.Then consider the value of $$val(k)$$ to be $$m$$ then in a subset element $$k$$ can occur from $$0$$ to $$m$$ times and using this i will find answer for all of its occurrences from $$0$$ to $$m$$ which is a simple number of sub sets problem and finally i will have my answer.

But my algorithms complexity is bad as it quadratic or worse.Could anyone help me.

## algorithms – Calculate the area of the shape created by multiple paths

I’m trying to write an algorithm to calculate the area created by multiple paths that can be overlapping or not. Here is an example:

Basics

• 4 separate paths (A,B,C,D) which are a collection of vertices (A1,A2,…)
• Area desired is represented by green

Edge Cases

• As shown with B, a path might have segments that don’t contribute to a filled shape
• As shown with C, a path might be completely enclosed by other paths and therefore should basically be ignored.
• As shown with D, paths may create independent shapes
• As shown with A and B, it should be a union of all the shapes

My first question is if an algorithm for this already exists. If it does, it would save me a lot of effort :). I tried searching around but I don’t even know how to describe this problem concisely.

Assuming one doesn’t exist for this exact purpose I have to move on to figuring it out myself. I’m assuming the right data structure for the job is a graph. I’m thinking I will add points for each intersection (highlighted in red) as I insert paths into the graph.

Then “all I need” is an algorithm for tracing around the outside of each shape because calculating the area of those irregular polygons will be simple. Does something like that already exist? My primary hangups when I think about how to do this are:

• What vertex do I “start” at?
• How do I account for multiple shapes (D as well as A,B,C)?
• How do I account for the parts of shapes like formed by A1,A5,A5 where I’ll be visiting that intersection point multiple times?

I’m not necessarily looking for a complete solution, I’d love thoughts on if you think I’m approaching this the best way so far and if you have any ideas/suggestions on how to achieve this.

## algorithms – Find Index In Sorted Array Such That A[i] = C1 * i + C2

Finding i such that A[i] = i, where A is a sorted array, needs to check every single array element in the worst case. Choose an array where A[i] = i-1 for every i except that A[k] = k for one single k. If you looked at any number of array elements other than A[k], every single one of the remaining elements could be the one you are looking for.

Now replace every array element with A[i] * C1 + C2, and unless C1 = 0, you have exactly the same problem.

Of course you can be optimistic, and often you will find i a lot quicker. Let’s say you have array elements a[1] to a[1000], and you find that a[500] = 770. You now know that 1 <= i <= 499 or 770 <= i <= 1000. You just significantly reduced the number of choices. Of course my first example would have a[500] = 499, which tells you nothing apart from i ≠ 500.

## algorithms – Maximum number of occurrences of a element in every sub sequence

Consider there is a array $$a$$ with $$n$$ elements .i need to consider all sub-sequences and find element that occurs maximum number of times in that sub-sequence and if there are ties i consider the minimum element.Since there are $$2^n-1$$ sequences brute forcing is not possible.you may consider that elements of array are $$a_i<=n$$ and $$n<10^5$$.

Example :
$$a=[2,1,3]$$

$$1$$ occurs maximum number of times in $$4$$ sub-sequences and $$2$$ occurs in $$2$$ sub sequences and $$3$$ in one sub-sequence.Output can be given in form of number $$i$$ occurs maximum number of times in $$k$$ sub sequences.

I think this can be solved with Dynamic Programming. But I can’t form a recursive equation for Dynamic Programming.

## algorithms – Latin-1 Supplement Unicode Characters

The unicode characters under the “Basic Latin” unicode block from U+0001 to U+007F are the characters that characters we use in English. However, the “Latin-1 Supplement” block from U+0080 to U+00FF is an extension of the “Basic Latin” block and includes characters that are used in some languages. For example, the Spanish language use all the characters from U+0080 to U+00BF as well as the letter “Ñ/ñ” (U+00D1 and U+00F1), which is pronounced like the “ny” in “canyón”. So, how did some languages get the characters under the Latin-1 supplement block?

## algorithms – Is looping a sorted array considered O(n^2)?

Often when I’m working with a sorted array, I write code along these lines:

``````function operation (array) {
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length; j++) {
// do something with array, i, and j
}
}
}
``````

While the runtime of this code (starting `j` at `i + 1`) is clearly faster than a normal double-nested loop, I’m struggling to figure out if that’s just a matter of a smaller coefficient or if it should be represented by a different big O.

Is there a better big O for the above function than n squared?