Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 14, Problem 8PP
Program Plan Intro
Finding all permutations for a set
Program Plan:
- Include required file.
- Define the structure for node.
- Declare elements in “vector” type.
- Declare variable for next value in “NodeValue” type.
- Declare function for display permutations.
- Declare function for compute permutations with recursively.
- Declare function for display vector elements of set.
- Define main function.
- Call the function “displayPermutations” with one parameter.
- Define function “displayPermutations”.
- Create a pointer for node.
- Declare the set in “vector” type.
- Fill the set with first “n” whole elements.
- Call the function “displayVectorElements” to print the
vectors . - Then compute the permutation for given set by calling the function “recursivePermutations”.
- Performs “while” loop. This loop executes until the pointer is equal to “NULL”.
- Display the values in set by calling the function “displayVectorElements”.
- Then delete and move to the next value.
- Define function “recursivePermutations”.
- This function is used to returns a list holding all of the permutations of the given list of elements.
- In this function, first assign the pointer list to “NULL”.
- Then performs base case if the size of the vector element is “1”. Otherwise performs recursive case.
- Compute the permutations for smaller set of elements by recursively call the function “recursivePermutations”.
- Define function “displayVectorElements”.
- This function is used to display the elements of set.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
In chess, a walk for a particular piece is a sequence of legal moves for that piece, starting from a square of your choice, that visits every square of the board. A tour is a walk that visits every square only once. (See Figure 5.13.)
5.13 Prove by induction that there exists a knight’s walk of an n-by-n chessboard for any n ≥ 4. (It turns out that knight’s tours exist for all even n ≥ 6, but you don’t need to prove this fact.)
The Knapsack Problem is a famous computer science problem that is defined as follows: imagine you are carrying a knapsack with capacity to hold a total of weight C. You are selecting among n items with values A={a_1, a_2, ... , a_n} and associated weights W={w_1, w_2, ... , w_n}. Here the weights and values are all positive (but not necessarily unique). You wish to maximize the total value of the items you select not exceeding the given weight capacity, i.e. maximize sum_{a in A} such that sum_{w in W} <= C. Please note that you can only select your items once.
a) We can reformulate this as a 2D bottom-up dynamic programming problem as follows. Define T_{i,j} as the highest possible value sum considering items 1 through i and total weight capacity j (j <= C). What is the base case i.e. T_{0,j} for all j and T_{i,0} for all i?, and What is the loop statement?
Recursive filtering techniques are often used to reduce the computational complexity of
a repeated operation such as filtering. If an image filter is applied to each location in an
image, a (horizontally) recursive formulation of the filtering operation expresses the result
at location (x +1, y) in terms of the previously computed result at location (x, y).
A box convolution filter, B, which has coefficients equal to one inside a rectangular win-
dow, and zero elsewhere is given by:
w-1h-1
B(r, y,w, h) =
ΣΣΤ+ i,y + )
i=0 j=0
where I(r, y) is the pixel intensity of image I at (x, y). We can speed up the computation
of arbitrary sized box filters using recursion as described above. In this problem, you will
derive the procedure to do this.
(a) The function J at location (x,y) is defined to be the sum of the pixel values above
and to the left of (x,y), inclusive:
J(r, y) =
- ΣΣ14.0
i=0 j=0
Formulate a recursion to compute J(r, y). Assume that I(r, y) = 0 if r <0 or y < 0.
Hint: It may be…
Chapter 14 Solutions
Problem Solving with C++ (10th Edition)
Ch. 14.1 - Prob. 1STECh. 14.1 - Prob. 2STECh. 14.1 - Prob. 3STECh. 14.1 - Prob. 4STECh. 14.1 - Prob. 5STECh. 14.1 - If your program produces an error message that...Ch. 14.1 - Write an iterative version of the function cheers...Ch. 14.1 - Write an iterative version of the function defined...Ch. 14.1 - Prob. 9STECh. 14.1 - Trace the recursive solution you made to Self-Test...
Ch. 14.1 - Trace the recursive solution you made to Self-Test...Ch. 14.2 - What is the output of the following program?...Ch. 14.2 - Prob. 13STECh. 14.2 - Redefine the function power so that it also works...Ch. 14.3 - Prob. 15STECh. 14.3 - Write an iterative version of the one-argument...Ch. 14 - Prob. 1PCh. 14 - Prob. 2PCh. 14 - Write a recursive version of the search function...Ch. 14 - Prob. 4PCh. 14 - Prob. 5PCh. 14 - The formula for computing the number of ways of...Ch. 14 - Write a recursive function that has an argument...Ch. 14 - Prob. 3PPCh. 14 - Prob. 4PPCh. 14 - Prob. 5PPCh. 14 - The game of Jump It consists of a board with n...Ch. 14 - Prob. 7PPCh. 14 - Prob. 8PP
Knowledge Booster
Similar questions
- Computer Science java programarrow_forwardComplete the task using the return search algorithm: Task: The labyrinth is represented by a Boolean square matrix A8x8. The cell (i, j) is considered passable if the element ai, j is true and otherwise impassable. Write a program that checks if there is a path from adjacent horizontally and vertically passable cells of the maze, which starts in its upper left corner (0.0) and ends in its lower right corner (7.7). Operating instructions: 1) Compose a Boolean function Path (i, j), called with initial parameters (0,0); 2) If cell (i, j) is outside the matrix Path returns false; 3) If cell (i, j) coincides with (7,7) a path is found and Path returns true; 4) If cell (i, j) is impassable Path returns false (because it cannot exit the cell), otherwise (if the cell is passable) there is a path from it to cell (7,7), if there is a path from any of the cells adjacent to (i, j) next to cell (7,7). Adjacent cells per cell (i, j) are cells (i + 1, j), (i, j + 1), (i-1, j), (i, j-1).arrow_forwardCorrect answer will be upvoted else Multiple Downvoted. Don't submit random answer. Computer science. each individual has a rating chart portrayed by a variety of integers an of length n. You are currently refreshing the foundation, so you've made a program to pack these diagrams. The program functions as follows. Given an integer boundary k, the program takes the base of each adjoining subarray of length k in a. All the more officially, for a cluster an of length n and an integer k, characterize the k-pressure exhibit of an as a cluster b of length n−k+1, to such an extent that bj=minj≤i≤j+k−1ai For instance, the 3-pressure cluster of [1,3,4,5,2] is [min{1,3,4},min{3,4,5},min{4,5,2}]=[1,3,2]. A stage of length m is an exhibit comprising of m unmistakable integers from 1 to m in subjective request. For instance, [2,3,1,5,4] is a stage, however [1,2,2] isn't a change (2 shows up twice in the exhibit) and [1,3,4] is likewise not a stage (m=3 but rather there is 4 in the…arrow_forward
- Correct answer will be upvoted else downvoted. Computer science. You are given an exhibit a comprising of n (n≥3) positive integers. It is realized that in this exhibit, every one of the numbers with the exception of one are something very similar (for instance, in the cluster [4,11,4,4] all numbers aside from one are equivalent to 4). Print the list of the component that doesn't rise to other people. The numbers in the cluster are numbered from one. Input The main line contains a solitary integer t (1≤t≤100). Then, at that point, t experiments follow. The main line of each experiment contains a solitary integer n (3≤n≤100) — the length of the exhibit a. The second line of each experiment contains n integers a1,a2,… ,an (1≤ai≤100). It is ensured that every one of the numbers aside from one in the an exhibit are something very similar. Output For each experiment, output a solitary integer — the list of the component that isn't equivalent to other people.arrow_forwardConsider a divide-and-conquer algorithm that calculates the sum of all elements in a set of n numbers by dividing the set into two sets of n/2 numbers each, finding the sum of each of the two subsets recursively, and then adding the result. What is the recurrence relation for the number of operations required for this algorithm? Answer is f(n) = 2 f(n/2) + 1. Please show why this is the case.arrow_forwardPython: Graph Colouring A simple method to find a colouring of a graph, with vertices {0,1,…,?−1}, is the following greedy algorithm: For ? in 0, 1, 2, ..., n - 1: Find the smallest colour (positive integer) ? which is not a colour of any neighbour of ?. Assign ? as the colour of ?. i) Implement the method above as a function greedy_colouring which takes a networkx Graph G (with vertices {0,1,…,?−1}) and returns a list C of the colours of G, where C[i] is the colour of vertex ?. ii) As a test, the Hoffman-Singleton graph (nx.hoffman_singleton_graph()) should require 6 colours using this method. If you have a graph G, you can draw it with coloured vertices using the code below: ten_colours = ['#CC6677', '#332288', '#DDCC77', '#117733', '#88CCEE', '#882255', '#44AA99', '#999933', '#AA4499', '#DDDDDD'] nx.draw_networkx(G, node_color=[ten_colours[i - 1] for i in greedy_colouring(G)], with_labels=False) iii) One way of refining the…arrow_forward
- Execute the following using Array Implementation. Use diagrams in showing your final answer A ENQUEUE(A) ENQUEUE(B) ENQUEUE(C) DEQUEUE() ENQUEUE(A) ENQUEUE(B) ENQUEUE(C) DEQUEUE() DEQUEUE() ENQUEUE(A) DEQUEUE() ENQUEUE(B) ENQUEUE(C) B PUSH (x, S) PUSH (y, S) PUSH (z, S) POP (S) PUSH (x, S) PUSH (y, S) POP (S) POP (S) C INSERT(Alice, 1, ) INSERT(Bob, 2, L) INSERT(Karen, 3, L) INSERT(Ram, 4, L) DELETE( 2, L) What will happen to your array?arrow_forwardYou are given a list of randomly arranged numbers, for example (11,7,18,5,17,13). The triple (11,7, 5) is called the "inversion triple" because (5<7<11) in terms of value, while the index of 5 in the list is greater than the index of 7, and the index of 7 is greater than 11. Therefore, we can find 2 inversions in such list as follows: (11,7,5), and (18,17,13). Your main task is to find the total number of inversions in any given list. a) Design a brute-force algorithm to return the number of possible inversions, and analyse the complexity of your solution b) Develop a python code to implement your brute-force algorithm. [The marks depend on the correctness of the code, indentation, comments, test-case] c) Design a more efficient algorithm to do the same task with less complexity, and analyse the complexity of your solution. [Important instruction to be followed: Create an arbitrary unsorted list of 8 characters and use it to provide full explanation of how your proposed…arrow_forwardThere are m towns in a straight line, with a road joining each pair of consecutive towns. Legends say that an ordinary person in one of these towns will become a hero by completing a sequence of n quests. The first quest will be completed in their home town, but after each quest they may complete their next quest either in the same town or after moving to a neighbouring town.For example, if n = 5 and m = 4, a resident of town 2 may become a hero as follows:• begin in town 2 for quest 1,• then move to town 3 for quest 2,• stay in town 3 for quest 3,• return to town 2 for quest 4, and• move to town 1 for quest 5.Design an algorithm which runs in O(nm) time and finds the total number of waysto complete n quests.arrow_forward
- There is a legend about a magical park with N × N trees. The trees are positioned in a square grid with Nrows (numbered from 1 to N from north to south) and N columns (numbered from 1 to N from west to east).The height (in metres) of each tree is an integer between 1 and N × N, inclusive. Magically, the height ofall trees is unique.Bunga is standing on the northmost point of the park and wants to count the number of visible trees for eachcolumn. Similarly, Lestari is standing on the westmost point of the park and wants to count the number ofvisible trees for each row. A tree X is visible if all other trees in front of the tree X are shorter than the treeX.For example, let N = 3 and the height (in metres) of the trees are as follows.6 1 87 5 32 9 4• On the first column, Bunga can see two trees, as the tree on the third row is obstructed by the othertrees.• On the second column, Bunga can see all three trees.• On the third column, Bunga can see only the tree on the first row, as the…arrow_forwardWrite a function find_equivalent_matchings that takes a matching and returns all matchings with the same pattern (including the original one). Using this function, draw all matchings equivalent to {(0, 7), (1, 4), (2, 3), (5, 6)}. You might find it helpful to generate all permutations of a list using itertools.permutation (imported above) as in the following code template. perms=permutations (list_of_items) for p in perms:arrow_forwardA Queen on a chessboard can attack any piece in the same column, row or diagonal. The N-Queens problem is to place n queens on an x n chessboard such that no two queens threaten each other. a) Implement a one-dimensional integer array of Queen positions for an 8x8 board where indices represent rows and the values represent columns. For example, a "safe" solution would be (3,6,2, 7, 1, 4, 0,5} b) Implement a print function to display the board (see output example) c) Implement an isSafe function that: 1) Returns false if multiple queens share a column 2) Returns false if multiple queens share a diagonal 3) Returns true if all queens are safe d) Program should display if the Queens are safe or not safe. Example Output Testing: 1 4 2 3 5 7 60 Queens are not safe!arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education