Recursively function generates all permutations set no.

Assignment Help Mathematics
Reference no: EM13767168

Some problem require finding all permutations (different orderings)of a set of items. For a set of n items there are n! permutations.

For example, given the set {1, 2, 3} there are six permutations:

{1, 2, 3} {2, 3, 1} {2, 1, 3} {3, 1, 2}{1, 3, 2} {3, 2, 1}

Write a recursively function that generates all the permutations of a set of numbers. Use the STL set class for all set operations and the STL linked list class to store and manipulate each individual permutation. When creating a set containing lists, make. The general outline of a solution is given as followings:

The program will require storing a set of permutations of numbers that  you can implement in many ways, (e.g., linked list, linked lists of vector, array, etc.)Your program should call the recursive function with sets of several different sizes, printing the resulting set of  permutation for each.

One solution is to first leave out the nth item in the set. Recursively find all permutations using the set of (n-1) items. If we insert the nth item into each position for all of these permutations,  then we get a new set of permutations that includes the nth item. The base case is when there is only one item in the set, in which case the  solution is simply the permutation with the single item.

For example, consider finding all permutations of {1, 2, 3}. We leave  the 3 out and recursively find all permutations of the set {1, 2}.
This consists of 2 permutations:

{1, 2} {2, 1}

Now we insert the 3 into every position for these permutations. For  the first permutation we insert the 3 in the front, between 1 and 2,
and after 2. For the second permutation we insert the 3 in the front, between 1 and 2, and after 1.

{3, 1, 2} {1, 3, 2} {1, 2, 3} {3, 2, 1}{2, 3, 1} {2, 1, 3}

When creating a set containing lists, make sure to place a space between the last two >'s or the compiler may get confused. For example, set <list<int>> defines a set where elements are linked lists containing elements of type int. the code set <list<int>> without a space will likely produce a compiler error.

Use the following function declaration.

// Uses the permutations function to print all permutations of

// the first n whole numbers

voidprint_permutations(int n);
// Recursive function that returns a list contains all of the
// permutations of the given set of numbers

set<list <int>>permutations(const set<int>& numbers);
// Helper function for printing the contents of a list
voidprint_list(const list<int>& v);

Sample Output

 

717: {6,5,4,2,1,3}
718: {6,5,4,2,3,1}
719: {6,5,4,3,1,2}
720: {6,5,4,3,2,1}

Permuations of {1,2,3,4,5} :
1: {1,2,3,4,5}
2: {1,2,3,5,4}
119: {5,4,3,1,2}
120: {5,4,3,2,1}

Permuations of {1,2,3,4} :
1: {1,2,3,4}
2: {1,2,4,3}
23: {4,3,1,2}
24: {4,3,2,1}

Permuations of {1,2,3} :
1: {1,2,3}
2: {1,3,2}
3: {2,1,3}
4: {2,3,1}
5: {3,1,2}
6: {3,2,1}

Permuations of {1,2} :
1: {1,2}
2: {2,1}

Permuations of {1}:
1: {1}

Permuations of {}:

Press any key to continue . . .

Reference no: EM13767168

Questions Cloud

Prepare the journal entries for geraths : The windows are delivered on September 1, 2014, Geraths completes installation on October 15, 2014, and the customer pays the balance due. Prepare the journal entries for Geraths in 2014.
Identify and discuss major criticisms of insanity defense : What are the insanity statutes being used in the state you live in? How often the insanity defense is used and how successful is it? Identify and discuss the major criticisms of the insanity defense
Write a argument essay on does technology impact society : Write a Classical Research Argument Essay on Does Technology Impact Society?
Contrast cash basis accounting and accrual basis accounting : Compare and Contrast Cash Basis Accounting and Accrual Basis Accounting. In your discussion define each method.
Recursively function generates all permutations set no. : Write a recursively function that generates all the permutations of a set of numbers. Use the STL set class for all set operations and the STL linked list class to store and manipulate each individual permutation. When creating a set containing li..
Examples that you can take from the supreme court decisions : When is this protected by the U.S. Constitution? What are 2 examples that you can take from the Supreme Court decisions that support your arguments
State your proposal and country focus : State your proposal and country focus and give a brief summary of major findings. SWOT analysis Micro issues: Introduction of the concept.
Balance in unearned revenue at the end of march : On March 1, the Unearned Revenue account had a credit balance of $4,000. During March, it sold 300 tickets at $20 each and 250 tickets were used during the month. What is the balance in Unearned Revenue at the end of March?
The quality of an lcd monitor or lcd screen : Several factors that affect the quality of an LCD monitor or LCD screen, including the specific resolution for which they are geared. How is this resolution described

Reviews

Write a Review

Mathematics Questions & Answers

  How fast is the area of the rectangle changing

How fast is the area of the rectangle changing when the width is 10 cm and the height is 15 cm? Is the area decreasing or increasing?

  Describe a real-life situation

What is the difference between domain and range? Describe a real-life situation that could be modeled by a function.

  Important information about binomial random variable

Important information about Binomial Random Variable, Four cards are selected, one at a time, from a standard deck of 52 cards. Let x represent the number of aces drawn in a set of 4 cards.

  Incremental analysis and project addition

Explain why the existing $310,000 of fixed costs is a sunk cost, while the $320,000 of fixed costs associated with the proposed addition is an out of pocket cost.

  Express the hydrostatic force against one side of the plate

Express the hydrostatic force against one side of the plate as an integral and evaluate it. (Give your answer correct to the nearest whole number.)

  Which set of vertices represents a square

Which set of vertices represents a square?

  What is the median of the reported blood pressure

What is the median of the reported blood pressure values?

  Use the p-value method

At a=0.05 does it appear that the average is lower than what was stated by the Tennis Industry Association? Use the P-Value method, and assume that the variable is approximately normally distributed.

  Find the volume of the solid obtained by rotating the region

Find the volume of the solid obtained by rotating the region bounded by the given curves about the specified line. Sketch the region, the solid, and a typical disk or "washer." (Do this on paper.)

  Maximal planar graph

Prove that there exists only one 4-regular maximal planar graph.

  How many of each is needed to complete the project

Fred is building a fence that is 6 feet tall and 96 feet long. He needs to make a list of supplies needed. Based on the specifications below, please write down how many of each is needed to complete the project.

  Nelson diebel swam 100 meters in 62 seconds and mike

during the breaststroke competitions of 1992 olympics nelson diebel swam 100 meters in 62 seconds and mike bowermen

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd