Permutations of items with no adjacent duplicates

In summary: The initial values for Lg(1) and Lf(1) are easy to compute by hand.In summary, This conversation discusses a problem involving calculating the number of different ways characters in a string can be arranged, with the constraint that no two adjacent characters can be the same. The problem statement provides an example and the person has tried to find a solution using permutations and factorials, but is struggling with the calculation for strings with duplicates. They are seeking resources or hints on how to approach this problem, with suggestions including researching algorithmic combinatorics and using recursion or mathematical induction to find a solution.
  • #1
alandberg
1
0
Hello

I have recently come across a problem in my programming exercises that I haven't been able to solve, even after several days of hard thinking and tinkering.

I have found that this problem may be solved by means of permutations using factorials, but there is a specific permutation that I cannot calculate.

The problem statement is that a string of n characters contains 3 types of different characters, e.g. A, B and C, where each character can have duplicates within the string. Calculate the number of different ways the characters in the string can be arranged. No two adjacent characters can be the same, where the first and last character are also adjacent.
For example: ABBA can be arranged in two ways: ABAB and BABA.

So far, I have tried to find a permutation for the string AAABBC. My method is as follows:

Calculate all permutations under consideration of duplicates as 6! / 3! x 2!
Then, subtract all permutations that contain an occurrence of AAA, AA, BB
The issue with this is that each of these three permutations will also include a subset of the other two, which means I will need to add back permutations that contain both AAA and AA, AAA and BB, and AA and BB. Finally I will need to subtract the intersection of all three sets.

This method may not even be correct, I am getting lost in trying to calculate permutations that contain duplicates. Can someone point me to a post / exercises where I can study this? Can someone please give me a hint on how to approach this problem.

Thanks and regards

Andy
 
Physics news on Phys.org
  • #2
alandberg said:
I have recently come across a problem in my programming exercises

I haven't solved the problem. These are just my random thoughts.

Is the solution to this problem to be given in the form of a computer program? If so it might expect an algorithm as an answer rather than solving this by finding a mathematical formula and simply writing the expression for that formula.

If the answer is to be a program, you should look up the subject of "algorithmic combinatorics". There is an online book about it that is probably overkill for your problem, but you might find it handy in your future work: http://www.1stworks.com/ref/RuskeyCombGen.pdf

If the answer is to be a single mathematical formula, I hope some other forum member will give you a hint. I've never enjoyed thinking about such problems. Algorithmic combinatorics is more interesting.

Algorithms that enumerate all possible arrangements of things can often be written in a recursive style. Computer science textbooks seem to like recursion. For practical programming, I prefer to implement recursion using stacks that I can monitor for overflows.

That might suggest a slick approach using just mathematics. Induction resembles recursion. If you knew the answer to the problem for a string of length n, could you find the answer for a string of length n+1?

A list of the arrangements for a string of length n would contain only "legal" initial substrings for the arrangements of length n+1. It would be also be missing some legal initial substrings. You could consider how the legal substrings that are present could be used to form other legal substrings and how the missing initial substrings could be added back.

I think the algorithmic solution to the above idea taxes my brain less than a mathematical solution. If someone gave you all the legal strings of length n, could your write an algorithm to form all the legal strings of length n+1? If you can write such a function then it can be used recursively to solve the problem.

If S is legal string of length n then we can append any any character to it except its first and last character. That seems straightforward to program. How do we account for the missing strings? A string of length 4 like ABCA will be missing from the list because its first and last characters match, but it could be used as a legal initial substring in a string of length 5.

Perhaps the best way to program this is to write one function G that computes a list Lg(n) of all strings of length n plus the ones that are illegal only because the first and last characters match. Then write another function F that computes a list Lf(n) of legal strings by pruning the list Lg(n) of all the strings whose first and last characters match. To find the legal strings of length n+1, apply G to the Lg(n) to produce Lg(n+1). Then apply F to Lg(n+1) to produce Lf(n+1).
 

Related to Permutations of items with no adjacent duplicates

What are permutations?

Permutations refer to the different ways in which a set of items can be arranged or ordered. For example, if we have three items A, B, and C, the possible permutations are ABC, ACB, BAC, BCA, CAB, and CBA.

What does "no adjacent duplicates" mean in permutations?

"No adjacent duplicates" means that in a permutation, no two identical items can be placed next to each other. For example, in the permutation ABC, there are no adjacent duplicates because A, B, and C are all different items.

Why is it important to consider permutations with no adjacent duplicates?

In some cases, it may be important to avoid adjacent duplicates in permutations to maintain uniqueness or avoid repetition. For example, when creating passwords or codes, we want to ensure that there are no adjacent characters that are the same for security purposes.

How many permutations with no adjacent duplicates are possible for a given set of items?

The number of permutations with no adjacent duplicates for a given set of items can be calculated using the formula n! / (n-r)! where n is the total number of items and r is the number of items in each permutation. For example, if we have 5 items and we want to create permutations of 3 items with no adjacent duplicates, the number would be 5! / (5-3)! = 5! / 2! = 60.

How can we generate permutations with no adjacent duplicates?

There are various algorithms and methods that can be used to generate permutations with no adjacent duplicates. One common approach is to use backtracking, where we start with a base permutation and then recursively generate all possible permutations by adding one item at a time while ensuring that no adjacent duplicates are created. Other methods include dynamic programming, recursion, and Heap's algorithm.

Similar threads

  • Precalculus Mathematics Homework Help
Replies
8
Views
446
  • Programming and Computer Science
Replies
4
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
20
Views
1K
  • Calculus and Beyond Homework Help
Replies
18
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
10K
  • Calculus and Beyond Homework Help
Replies
6
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
4K
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
6
Views
2K
  • Programming and Computer Science
2
Replies
37
Views
9K
Back
Top