algorithm – C# knapSack problem, i want to display all included numbers

I have a problem with displaying used numbers. I’m using knapSack algorithm and I want to display all numbers that I used to get highest value. So there is my code:

static int max(int a, int b)
         {
             int c = (a > b) ? a : b;
            Console.WriteLine(c);
             return (a > b) ? a : b;
         }

    // Returns the maximum value that can 
    // be put in a knapsack of capacity W            
    int knapSack(int() r, int() wt, int n,  int W)
     {

        if (W < 0)
             return Int32.MinValue;
         if (n < 0 || W == 0)
             return 0;
        int include = r(n) + knapSack(r, wt, n, W - wt(n));
        int exclude = knapSack(r, wt, n - 1, W);
        int V = max(include, exclude);         
        return V;
     }
    int() r = new int() { 3, 4, 8, 5, 6 };
     int() wt = new int() { 2, 2, 3, 4, 7 };
    int W = 11;
    int z = W;
     int n1 = r.Length;
    stopwatch.Start();
    int keik = knapSack(r, wt, n1 - 1, W);
    stopwatch.Stop();

answer of this is 28, but I need to display all r numbers that was included in this. I know that for this array used numbers are 8 8 8 and 4, so I need somehow to get these numbers and display to the console.

timebased scoring algorithm for game

i am developing a game where user has to find target number using 5 given numbers and 4 operators. the score depends on the type of operators and number of operators used and time.

score = typeOperators + noOfOperators + timeScore

where typeOperators is the sum of score for each type of operator, 1 pt for +,* 2 pt for -, and 3 pt for /.

if all the three operators are used once, typeOperators will be 13.
the game should avoid the situation where the user finding target number with worst solution but with minimum time gets higher scores.
so the more weightage should be on the typeOperators rather than timeScore.

currently, i am thinking of

timeScore = max(0, x - timespent)

can there be any better approach?

Choosing genetic algorithm population size as function of input size

Is it true that the population size in genetic algorithm can be a function of chromosome size? i.e. the more genes the chromosome has, the more chromosomes I need to have (to have more variety in generation)?

Other than that, what functional dependencies can exist between population size and input?

complexity theory – What about problems that are fixed parameter tractable with an algorithm that does not inspects the parameter

A parameterized problem is a subset $L subseteq Sigma^* times mathbb N$, where $Sigma$ is a finite alphabet. A parameterized problem is fixed parameter tractable, if it could be decided in time $f(k)|x|^{O(1)}$ for some $(x,k) in L$.

In terms of the problem definition, a Turing machine for $L$ has access to the parameter. I noted that for some algorithms, for example the kernelization of vertex cover, the algorithm indeed must have access to it, whereas for others, for example clique paramterized by maximal degree, it does not need access to the parameter. The algorithm for the latter simply looks into the neighbourhood of every vertex if it contains a clique, hence it runs in time $O(2^{Delta} |x|)$, where $(x, Delta) in L$.

Was this ever noted? And if so, is there some notion like FPT without access to the parameter, i.e., a problem $Lsubseteq Sigma^*$ is in “FPT without access to the parameter”, if it runs in time $f(k)|x|^{O(1)}$ for $x in L$ and some number $k$ that depends on $x$ (I avoid the term parameter, as it is not given as input)? Or would it not make sense to consider such a class?

algorithm – Is a random number secure (enough) for card numbers and pins?

I think is a reasonable approach, the most important aspect will be how hard the pin numbers are to guess – a 6 digit pin will take more effort than a 3 digit pin for example. The larger you can make the length of the pin number without compromising usability the better, I’d suggest no less than 6 digits.

The maths is fairly simple: 3 digits means 1000 combinations so 1/1000 chance of guessing correctly. 6 digits means 1,000,000 combinations so 1/1,000,000

You will also need to rate limit how many times a pin can be guessed and decide what to do when that rate limit is reached. If the serial number is visible then you may want to consider a delay before the pin can be tried again rather than locking out the card to prevent someone deliberately locking all cards they can see.

As for not allowing certain patterns, I’d say this isn’t entirely necessary, however you may want to restrict the most guessable ones such as ‘000000’, ‘999999’, ‘123456’ etc.

number theory – Continued Fraction Factorization Algorithm please explain with Code

Thanks for contributing an answer to Computer Science Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

To learn more, see our tips on writing great answers.

algorithm – How to find the smallest distance between two 4-sided objects in 3D space?

I was wondering if there is a way to calculate the smallest distance between two 4-sided objects in space. In this specific scenario, the only information I know about both of them are the 4 points that make up their vertices (so 8 points in total). Is there a way to achieve this?

Also, I’m considering that both shapes have a non-zero volume and that they do not intersect each other.

graphs – Algorithm to calculate shortest path when updating the heaviest edge in a path

For a given graph G=(V,E) and path p= v1->v2->…->vk

w*(p) represents the weight of the path between v1 and vk excluding max_edge.
enter image description here

For a given vertex s in V

I would like to find an algorithm that calculates using Dijkstra the shortest path with value w*(p) between s and every v in G.

I’m guessing, I should start off by changing the original graph G. Maybe duplicating an edge with value (-1*max edge) .. or maybe making the heaviest edge zero

I’m not sure, so anything would be appreciated.

Thank you advance.

Algorithm to turn gray-scale text into legible binary black and white text

I have an image with gray-scale anti-aliased text produced by Chromium and need to convert this into pure binary black and white text. Applying a simple binary threshold pass over the image causes artifacts, either missing edges or weird protrusions and text being too bold depending on the chosen threshold.

Below an example of the input, the perfect outcome (as this is how the text would be rendered without AA) and the current outcome based on binary threshold (I think this is a 0.6 threshold). Sadly, it’s not possible to instruct Chromium to render the text without anti-aliasing, so a conversion is the only option.

I was wondering if there was an established algorithm for this kind of problem or in what direction I would have to look to find something that could be adapted to work here. Any algorithm would need to be able to run in semi-realtime, no offline solutions.

text rendering example

bipartite matching – Efficient algorithm to map two differently-sized sets of numbers as closely as possible?

The problem

I have two sets of numbers and need to find a mapping between those two sets, so that the total distance between two mapped numbers is as small as possible. Two numbers must not be mapped if they are farther apart then 0.18. As many numbers should get mapped as possible.

Also, the sets are not necessarily the same size. So, consequently, some numbers of the larger set won’t get any mapping.

Example:

Example

Is there a reasonably efficient algorithm that finds a mapping like this? Or, is there a term for this specific problem so that I can research algorithms on my own?

My research

Through googling I encountered this question, which led me to the term “Euclidean Bipartite Matching Problem” which seems to be the term for a problem very similar to mine. However, my problem is slightly different than the Euclidean Bipartite Matching Problem.

So basically, I’m looking for an efficient algorithm for the 1-dimensional Euclidean Bipartite Matching Problem except that the two sets of numbers can be of differing size, and the distance between two numbers must not exceed 0.18.

My attempt

I’ve already coded my own implementation, however… it doesn’t work properly and is pretty complicated too so that I’m not even sure why it doesn’t work.

As for the basic idea behind my implementation: let’s call the first set the red numbers and the second set the blue numbers (apparently that’s the terminology used in the Euclidean Bipartite Matching Problem). Now;

  1. go through all red numbers, and for each:
    1. find the closest blue number within a ±0.18 range
    2. if the blue number is already assigned to a different red number:
      • if the existing assigned red number is nearer than our red number, skip this blue number
    3. assign our red number to the blue number
    4. if we overwrote a previously-assigned red number in the process, make the red number find itself a new blue number (i.e. make the red number go through steps 1-4 again)

(I’m doubtful that this implementation is even correct) but yeah, this is what I tried so far.


Are there well-known algorithms to do this task, so that I don’t have to create a wonky, non-functioning, slow implementation myself? Or in general, is there a term for this specific problem? Then I could google for that term and find what I need.

I’ll be happy about any answer or pointers 🙂