Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [combinatorics]

Combinatorics is a branch of mathematics concerning the study of finite or countable discrete structures.

49
votes
3answers
3k views

Analyzing Minesweeper Probabilities

Calculating probabilities in Minesweeper might sound like an easy task, but I've seen so many probability calculators that's either incorrect, horribly slow, or with ugly code (or all of them) so I ...
30
votes
5answers
44k views

Brute Force Algorithm in C

This is a simple brute force algorithm that I have programmed in C. All the program does is print out every possible combination of the given alphabet for the ...
5
votes
3answers
73k views

Calculate all possible combinations of given characters

I was asked in my textbook Lectures on Discrete Mathematics for Computer Science to construct a program that would take an alphabet ({a,b,c} or any combination of ...
30
votes
3answers
859 views

Advanced and Detailed Minesweeper Probabilities

In an earlier question, I showed my code for calculating the mine probability for each and every field in a Minesweeper board. But it doesn't stop there. In that very same Minesweeper Analyze project, ...
9
votes
1answer
294 views

Sequence-based enumeration of permutations in lexicographic order

This is follow-up on On Knuth's "Algorithm L" to generate permutations in lexicographic order, where I presented the following method to enumerate all permutations of array elements in ...
10
votes
2answers
454 views

A Specific Combination

This code is going to be included in my Minesweeper Probabilities project. (so far it only exists on the develop-branch) The purpose is to return a specific combination, let's say for example that ...
25
votes
7answers
7k views

Finding words of different lengths given six letters

I'm trying to find all the 3, 4, 5, and 6 letter words given 6 letters. I am finding them by comparing every combination of the 6 letters to an ArrayList of words ...
15
votes
2answers
324 views

Minimising the triangle

A triangle needs a good foundation. Every row in the triangle is derived from the sum of the two values below it. However, there can be no repeated values, if a value shows up more than once the ...
14
votes
3answers
2k views

On Knuth's “Algorithm L” to generate permutations in lexicographic order

I needed a method to generate all permutations of given elements, so I decided to implement "Algorithm L (Lexicographic permutation generation)" from Donald E. Knuth, "GENERATING ALL PERMUTATIONS" in ...
8
votes
1answer
734 views

Java application for finding permutations efficiently

I am an eighth grader with a school project of creating and application in Java that returns the total permutations of two given numbers. It needs to be light and efficient, for later deployment on a ...
6
votes
3answers
2k views

Optimizing unique partitions of integers

I am working on a project involving the unique partitioning of integers. I.e. I am looking to have all of the partitions of n = x1 + x2 + x3 + ... + xk, where xi = xj implies i = j. For example, if I ...
3
votes
1answer
149 views

Stable reversed Tartaglia's triangles

Inspired by Minimising the triangle I am writing a fully tested program to solve the following problem: A triangle needs a good foundation. Every row in the triangle is derived from the sum of the ...
12
votes
2answers
586 views

Finding a word within a pre-defined set using a search string with wildcards

The other day I was intrigued by this question (originally from here): Write a program that answers YES/NO search queries containing * placeholders. Example: if the data you have is (hazem, ahmed,...
6
votes
1answer
2k views

An iterator returning all possible permutations of a list in Java

I have this class that iterates over all permutations of an input list: PermutationIterable.java: ...
6
votes
2answers
491 views

Integer Partition using an iterator

I tried implementing Integer Partition (enumerating all possible sets of whole numbers whose sum is a given integer). Looking at @rolfl's answer, I reasoned that with a stack data structure, we ...
4
votes
1answer
239 views

Counting ways to fit bottles in crates

I'm calculating how many possibilities there are to fit \$n\$ identical bottles inside \$k\$ crates of various capacities. n = Number of bottles k = Number of crates K = List of the number of bottles ...
2
votes
1answer
311 views

3D bin packing in Java

I made some changes to the first code here I wrote and I want to know which one is more correct logically (before the changes or after the changes). It's really hard to manually test these kind of ...
61
votes
9answers
94k views

Generating all combinations of an array

I'm generating all combinations of an array, so for instance, ["a", "b", "c", "d"] will generate: ...
17
votes
6answers
28k views

Generate all possible combinations of letters in a word

This is a JavaScript function that returns ALL the possible combinations of whatever word the user enters. I am looking to clean this code up a bit...any and all suggestions are welcome! ...
17
votes
3answers
13k views

Finding all k-subset partitions

The following code generates all \$k\$-subsets of a given array. A \$k\$-subset of set \$X\$ is a partition of all the elements in \$X\$ into \$k\$ non-empty subsets. Thus, for ...
12
votes
3answers
10k views

Counting permutations without repetitions for a number or a string

Can I make the following code faster with minor modifications? It times out on CodeWars website. ...
7
votes
3answers
985 views

Find the total number of ways W, in which a sum S can be reached in N throws of a dice

I was solving this question: Given there is a 6 sided dice. Find the total number of ways W, in which a sum S can be reached in N throws. Example: S = 1, N = 6 => W = 0 S = 6, N = 6 => W ...
8
votes
3answers
2k views

Genetic algorithm for solving a puzzle

Edit. Version 2. I am working on a genetic algorithm in order to solve a little "puzzle." Given a text file with N rows with 4 int each, the idea is to ...
7
votes
1answer
833 views

Generate next combination in lexicographic order

C++'s standard library has a std::next_permutation algorithm but no next_combination. The reason behind this absence is, I guess,...
7
votes
2answers
900 views

Simplified Bin Packing using JavaScript

This code determines the maximum number of boxes (one size) that can fit into the back of a truck, checking all possible orientations to ensure maximum Bin Packing excitement. This is (hopefully) the ...
6
votes
2answers
867 views

An iterator returning all possible partitions of a list in Java

Given a list of \$n\$ objects and an integer \$k \in \{ 1, 2, \dots, n \}\$, this iterator generates all possible ways of partitioning the elements in the list into exactly \$k\$ disjoint, non-empty ...
3
votes
1answer
97 views

Improving Speeds of Tokenisation of large word list for distinct combinations

I'm having trouble with a current algorithm I'm writing that takes a corpa, a list of characters in sets (1 one or larger) with a frequency number attached to it. Against another list of character ...
3
votes
2answers
3k views

Subset sum problem implementation

Here is a recursive implementation of the Subset sum problem: ...
2
votes
3answers
4k views

Finding combinations of 3 numbers

What this code tries to do is find possible combinations of three numbers. For example, let's say I have the following numbers: 1, 2, 3, 4, 5, 6, 7 Some ...
1
vote
2answers
3k views

Combination generator in Java - 2nd iteration

I have refactored the previous combination generator, and now it is an iterator returning combinations as lists. It hides larger constant factors as the previous version, yet it is really easy to use: ...
6
votes
3answers
487 views

Korean word segmentation using frequency heuristic

This a continuation of a previous question. I want to thank Joe Wallis for his help with increasing the readability of my code. Although the changes made by Joe Wallis did increase the speed of the ...
6
votes
2answers
2k views

Consecutive Primes challenge at CodeEval.com and memory allocation issue

I finally solved this challenge with using recursion and figured out why the automatic scorer wasn't passing my previous solutions. I was using NSMutableArray to ...
6
votes
2answers
6k views

Brute-force string generator

I have created a brute-force algorithm class in C# and I was wondering if I could speed up the process of creating a new string. There are two methods inside the class: one returns the brute-force ...
5
votes
3answers
2k views

Finding the given word from jumbled letters

I have tried to write JavaScript code to check whether I can form a word from the given string (jumbled): ...
5
votes
3answers
3k views

Generating character permutations

I want to generate a 'dictionary' containing all 8 character permutations of upper-case letters such that the output file looks like: ...
4
votes
4answers
6k views

Staircase problem solved using recursion

Problem Question taken from here. I want to go up a flight of stairs that has n steps. I can either take 1 or 2 steps each time. How many different ways can I go up this flight of stairs? Write a ...
4
votes
5answers
768 views

Finding the smallest cube for which exactly five permutations of its digits are cube

(Project Euler #62) The digits of the cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which ...
4
votes
1answer
1k views

Combination generator in Java

(See the next iteration.) Given a positive \$N\$, the following class generates all possible non-empty sets \$A \in \mathcal{P}(\{0, 1, \dots, N - 1\})\$ in lexicographic order. Also, it provides a ...
3
votes
1answer
6k views

Generating all subsets of a given set

I am learning Java Collections Framework. Can someone look at this code for generating all subsets of a given set and tell me any issues with it. ...
3
votes
2answers
170 views

Determine if a word can be constructed from list of subsets - follow-up

I have reworked my code as suggested from my previous question: Determine if a word can be constructed from list of subsets. Please instruct me on the complexity along with a review feedback, as ...
3
votes
3answers
974 views

Check whether given a pair of arrays are permutation of each other

Does this implementation guarantee execution of \$O(n)\$ time? What is the additional space utilization for this implementation by excluding the original array size? Is it \$O(1)\$? ...
3
votes
2answers
3k views

Permutation and combination calculator

I wanted to make something for the Community Challenge, so I made a simple permutation and combination calculator. I'd like input on: The overall design - any suboptimal choices that could be ...
3
votes
2answers
1k views

3D bin packing algorithm using Java?

I wrote a 3D bin packing algorithm but I am still not sure if it is correct or not. I did not follow any code or pseudo-code that's why I would like to know if it is an efficient algorithm for the 3D ...
3
votes
2answers
174 views

Check is_permutation on a pair of same sized arrays - follow-up

After considering the suggestions of my previous question, Check whether given a pair of arrays are permutation of each other, I changed the code. Resolved the bug reported earlier In place of ...
2
votes
2answers
182 views

Genetic algorithm for solving a puzzle (v. 2)

Thanks to your wonderful comments regarding the first version of my code, I have been able to improve it. This new version is more readable (I guess). The problem is that it seems to me that there is ...
2
votes
2answers
176 views

Determine if a word can be constructed from list of subsets [closed]

The question is explained well by examples in the comments below. Also I request verifying complexity: O( n * n!), where n is the number of words in the subsets. Review my code for optimizations, ...
2
votes
2answers
229 views

Sequence-based enumeration of permutations with Heap's algorithm

Heap's algorithm is an algorithm to generate all permutations of a given array. It ... generates each permutation from the previous one by interchanging a single pair of elements; the other n−2 ...
2
votes
4answers
389 views

Printing all the permutations of a string in alphabetical order

It is a code eval challenge question https://www.codeeval.com/open_challenges/14/. My answer was accepted, and I modified my code again to improve it. Please let me know is it possible to improve it ...
1
vote
2answers
112 views

multirange iterator

I'm doing some boring math with lot of sums over lot of variables. So, instead of nesting for loops I have decided to use something like this: ...