sorting – How to prove that a binary heap can’t be sorted with complexity that better than O(nlogn)


Your privacy


By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.




What is an algorithm to do this binary image segmantation task?

What I need to do is go from a binary image like this:

to a segmented structure like this:

because I have a program that operates on those separated line contours after.
As you see, those contours are not perfect, they might be slightly bent but where they meet it’s always pretty much orthogonal, there are no 45 degree lines. The lines are between 3 and 20 pixels wide. The whole image is sometimes slightly rotated (less that 10 degrees) but if you find a solution that only works without rotation I could look into de-rotating stuff before. The inserted gap may be of any width (preferably 1px or 2px) as long as it entirely separates one line from another.

I thought one could do something like on page 15 of this paper (the description of what they do is on page 13). Essentially this would come down to finding one marker per line segment and then watershed, but I have no idea how to find that marker.

Just to be clear:

On any point of line intersection, this

is good, as well as this:

If easier to achieve,

this:

is acceptable too, although not preferred.

What’s not ok is this:

This is not only true for three-line t-sections but also two-line corner connections (= it doesn’t matter which of the two lines meeting gets shortened).

Sorry for the huge pictures, didn’t know how to prevent them from turning out this big.

I am working with python and cv2 so it would be a plus if you use that. On the other hand, even a link to a re-implementable algorithm or an idea how to find markers would be fine. Also, I’m looking for a fast and easy algorithm that works on binary (0 or 255) images, not any crazy machine-learning stuff (if avoidable). I have really time but lack the right idea. Thanks in advance.

plotting – How to plot clusters with binary matrix and coordinates?

I have to lists, xcor and ycor that contain the $x$-axis and $y$-axis coordinates of $N$ nodes. The nodes are divided into $C$ non overlapping clusters. The clustering information is provided in a binary matrix $Mmat$ of size $Ctimes N$, where $C$ is the number of clusters. If $M_{c,n}=1$, then node $n$ belongs to cluster $c$.

How can I show the clusters graphically?

xcor = {0.0667, 0.1667, 0.1667, 0.1667, 0.1667, 0.1667, 0.5000, 0.5000,
0.5000, 0.3000, 0.5000, 0.5000, 0.8333, 0.8333, 0.8333, 0.8333,
0.8333, 0.8333, 1.1667, 1.0667, 1.1667, 1.1667, 1.1067, 1.1667,
1.3000, 1.5000, 1.4000, 1.5000, 1.6000, 1.5000, 1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, {0, 0,
1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0,
0, 0, 1, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0}, {0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0}};

recursion – How to perform AND on binary “recursive repeating sequences”?

Suppose, we have a two binary sequences, encoded as “recursive repeating sequences” (I don’t know exactly how to name them). Each sequence can contain other sequences and has number related to how many times this sequence is repeated, or can contain a bit either 0 or 1. Following image describes it visually:

enter image description here

On this image repetition is denoted by lower index number nearby ending bracket. The bits need to be expanded are denoted by regular size font.

Each sequence can produce binary sequence (sequence of bits), when we expand it.

The questions are:

  1. Can we create a algorithm, that performs AND on two recursive repeating sequences, without expanding both of them? (The algorithm should produce third recursive repeating sequence, such when we expand it, it is AND on both expanded input sequences).
  2. If the answer for first question is true, how such algorithm will look (for example in pseudo code) ?

Note: In practice, the numbers responsible for repetition may be very large, so expansion of whole sequence is not practical in terms of computation – but if algorithm will need to do it partially, it may be accepted.

c++ – C++20 : N-dim matrix version 2, broadcasting binary operators

I’ve improved my previous N-dim Matrix class (C++20 : N-dimensional minimal Matrix class), adding some key feature from NumPy: Matrix broadcasting!

Broadcasting is a key required feature for taking inputs with possibly different shape in matrix binary operators.
https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md

Only binary addition has been implemented, other binary operators (sub, mul, div, …) are just similar boilerplates.

Since my code is getting bloated, I’ll post here the GitHub link of notable changes from the previous version:

https://github.com/frozenca/Ndim-Matrix/blob/main/ObjectBase.h

https://github.com/frozenca/Ndim-Matrix/blob/main/MatrixBase.h#L309

https://github.com/frozenca/Ndim-Matrix/blob/main/Matrix.h#L267

https://github.com/frozenca/Ndim-Matrix/blob/main/MatrixUtils.h#L56

https://github.com/frozenca/Ndim-Matrix/blob/main/MatrixUtils.h#L199

Simple broadcasting test:

#include "Matrix.h"
#include <iostream>
#include <iterator>
#include <numeric>

int main() {

    auto m6 = frozenca::ones<double, 3>({2, 3, 3});
    // {{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}}
    std::cout << m6 << 'n';

    auto m7 = frozenca::zeros<int, 2>({1, 3});
    std::iota(std::begin(m7), std::end(m7), 1);
    // {{1, 2, 3}}
    std::cout << m7 << 'n';

    auto m8 = m6 + m7; // type: Matrix<double, 3> with size (2, 3, 3)
    // {{{2, 3, 4}, {2, 3, 4}, {2, 3, 4}}, {{2, 3, 4}, {2, 3, 4}, {2, 3, 4}}}
    std::cout << m8 << 'n';
}

Feel free to comment anything!

Yet to come: Linear algebra stuffs (.dot(), .matmul(), SVD, inverse/pseudoinverse, etc)

c – Float to binary conversion

I wrote a program to convert float to it’s binary IEEE754 representation in C.

Any improvements to this is appreciated

#include <stdio.h>
#include <stdlib.h>
void binary(float num) {
    int n = *(int *) &num; //Evil bit hack from Quake3 Q_sqrt function
    int size = sizeof(n) * 8; 
    char *s = malloc(1 + size); //A string which can hold 32 characters
    s(size) = '';
    int index = size - 1;
    while(index >= 0) {
        if (n & 1)
            *(s + index) = '1';
        else
            *(s + index) = '0';
        n >>= 1;
        index--;
    }
    printf("%sn", s);
    free(s);
}

int main(void) {
    float num;
    scanf("%f", &num);
    binary(num);
}

c – BINARY TO DECIMAL CONVERSION

I know how to find the decimal equivalent from the binary number. E.g.
a general method of finding decimal equivalent of binary number (101.01) is (2^2)*1 + (2^1)*0 + (2^0)*1 + (2^-1)*0 + (2^-2)*1 .

My doubt is how can we find the decimal equivalent of lengthy binary numbers whose decimal equivalent range is from ±3.4×10^38 . By lengthy I mean which contain large number of bits like 0.00000000000100100100010100010010101 etc.

I tried this with the general method given by me in starting, but using that method here leads to much time consumption and probability of getting wrong answers is also almost sure.

For the same I tried using calculator and online binary to decimal converter’s, they are giving the approximate result but i need accurate result.

If using some software I can do that then please share that, my ultimate aim is to find the way to find the 100 % accurate decimal equivalent of range ±3.4×10^38 from lengthy binary numbers.

PLEASE HELP ME IN GETTING THE PROPER SOLUTION OF IT.

algorithms – How can we prove that in binary search, low – high ≤ 1

Let’s denote the indices by $l,h,m$. If $l leq h$ and $h-l$ is even then $m = frac{l+h}{2}$ and so in the following iteration, the new values $l’,h’$ will either be $frac{l+h}{2}+1,h$ or $l,frac{l+h}{2}-1$. In the first case
$$
l’-h’ = frac{l-h}{2}+1 leq 1,
$$

and in the second case
$$
l’-h’ = frac{l-h}{2}+1 leq 1.
$$

If $l leq h$ and $h-l$ is odd then $m=frac{l+h-1}{2}$ and so either $l’,h’ = frac{l+h+1}{2},h$ or $l’,h’ = l,frac{l+h-3}{2}$. In the first case
$$
l’ – h’ = frac{l-h+1}{2} leq frac{1}{2}
$$

and so $l’-h’ leq 0$, since $l’-h’$ is an integer. In the second case
$$
l’ – h’ = frac{l-h+3}{2} leq frac{3}{2},
$$

and so $l’-h’ leq 1$, since $l’-h’$ is an integer.

Put together, we have shown:

If $l leq h$ then $l’-h’ leq 1$.

Now let’s use it to show that $l-h leq 1$ always holds.

We first prove by induction on $t$ that if the loop executes at least $t$ times, then after $t$ iterations of the loop, $l-h leq 1$. The base case $t = 0$ is by assumption. Now suppose that after $t$ iterations, we have $l-h leq 1$. If $l > h$ then the loop terminates, and so there is nothing to prove. Otherwise, $l leq h$, and so $l-h leq 1$ at the end of the iteration, as shown above.

This shows that when the loop terminates (if it terminates — which has to be shown separately), $l-h leq 1$ necessarily holds. In fact, we can say more: since the loop terminated, we must have $l > h$, and so $l-h = 1$.

file permissions – Cxxdroid can execute c++ binary in any internal storage directory, but Termux cannot

I was testing some c++ executable in my old smartphone (Android 8, no root). Here’s the description of the problem:

I have both Termux and Cxxdroid installed in that phone. And in termux, I have clang installed.

Let’s enter Termux.

cd /storage/emulated/0/c++    # current dir is now c++ which sits in the internal storage
ls    # this prints 'hello.cpp', a simple hello world program. let's compile it.
clang++ hello.cxx -o hello
ls    # this prints two files now, hello and hello.cxx. let's execute hello.
./hello    # prints: bash: ./hello: Permission denied. let's grant permission.
chmod +x hello
./hello    # prints: bash: ./hello: Permission denied

As you can see, there’s no way I can execute hello in the c++ directory. I know that if I move hello in $HOME and run it, it would work right away. But that’s not the case.

Now, let’s enter Cxxdroid.
Enter ‘Terminal’ from left menu. Let’s execute our hello binary.

cd /storage/emulated/0/c++
ls    # contains two files. hello.cxx and hello (which is the binary generated from Termux clang). let's run it.
./hello    # prints 'Hello World'

See? The same binary does not run in Termux but runs right away in Cxxdroid. Why? What technique is Cxxdroid using under the hood? Permission of the hello binary for both Termux and Cxxdroid is:

ls -l hello    # prints: -rw-rw---- 1 root everybody 115588 Apr 25 00:32 hello

Any help is appreciated.

python – My binary search function returns none when target not found despite me specifiying return value to be -1

def binary_search_recursive(array, target, start_index, end_index):
middle_index=(start_index+end_index)//2
if start_index>end_index:
    return -1
if array(middle_index)==target:
    return middle_index
if array(middle_index)>target:
    binary_search_recursive(array,target,start_index,middle_index-1)
else: binary_search_recursive(array,target,middle_index+1,end_index)

This function returns None instead of -1 when it doesnt find target, why is that? how to solve it?