Question

Solve the pancake sorting problem with the initial state of 2,1,3,2 and the final state of...

Solve the pancake sorting problem with the initial state of 2,1,3,2 and the final state of 1,2,2,3. Note that there are two pancakes of size 2 to reduce the number of states needed. The arc-cost is the number of pancakes flipped.

a. Apply A* search using the heuristic h1 = the index of the largest pancake that is out of place.
b. Apply A* search using the heuristic h2 = the number of pancakes out of position.
c. Apply UCS

Use graph search (keeping track of closed nodes) wheneverapplicable; that is, in your work, do not show children that are found in the set of already closed nodes.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Pancake Sorting Algorithm ,unlike a traditional sorting algorithm, helps to sort the arrays with the fewest comparisons possible.  The main aim is to sort the sequence in as few reversals as possible.

Basic steps to be followed for this algorithm are :

1) Let given array be arr[] and size of array be n. let currentsize be equal to n initially. keep reducing the currentsize by 1 while it is greater than 1.

2) while (currentsize is greater than 1)
……a) Find index of the maximum element in arr[0..curr_szie-1]. Let the index be ‘mi’
……b) Call a function flip(arr,mi) which will reverse the array elements from 0 to mi.
……c) Call a function flip(arr,mi) which will reverse the array elements from 0 to (currentsize-1).

 static int pancakeSort(int arr[], int n) { for (int curr_size = n; curr_size > 1; --curr_size) { int mi = findMax(arr, curr_size); if (mi != curr_size-1) { flip(arr, mi); flip(arr, curr_size-1); } } return 0; } static void flip(int arr[], int i) { int temp, start = 0; while (start < i) { temp = arr[start]; arr[start] = arr[i]; arr[i] = temp; start++; i--; } } static int findMax(int arr[], int n) { int mi, i; for (mi = 0, i = 0; i < n; ++i) if (arr[i] > arr[mi]) mi = i; return mi; } 

for the given intial states : 2 1 3 2 , n=4 and currsize=4, [ ] this signifies the current state of elements in array.

1) maxindex=2, hence flip elements of array from (0,2) [ 3 1 2 2 ] and since currsize=4 ,again flip array elements from (0,3) , [ 2 2 1 3] and currsize=currsize-1=3;

Array : [ 2 2 1 3]

2) maxindex=0, hence flip elements of array from (0,0) [ 2 2 1 3 ] and since currsize=3 ,again flip array elements from (0,2) , [ 1 2 2 3] and currsize=currsize-1=2;

Array : [1 2 2 3] // array is sorted here itself with two flips.

3) maxindex=1, hence flip elements of array from (0,1) [ 2 1 2 3 ] and since currsize=2 ,again flip array elements from (0,1) , [1 2 2 3] and currsize=currsize-1=1;

Array : [ 1 2 2 3]

Hence the array is sorted.

Add a comment
Know the answer?
Add Answer to:
Solve the pancake sorting problem with the initial state of 2,1,3,2 and the final state of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Solve the pancake sorting problem with the initial state of 2,1,3,2 and the final state of...

    Solve the pancake sorting problem with the initial state of 2,1,3,2 and the final state of 1,2,2,3. Note that there are two pancakes of size 2 to reduce the number of states needed. The arc-cost is the number of pancakes flipped. Apply A* search using the heuristic h1 = the index of the largest pancake that is out of place. Apply A* search using the heuristic h2 = the number of pancakes out of position. Apply UCS Use graph search...

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* s...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. Please include pictures that the code runs and shows the different states as it reaches goal state please. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class....

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A*...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class. For more information, please read the wiki page of 15-puzzle problem at https://en.wikipedia.org/wiki/15_puzzle, and the wiki page of...

  • Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as...

    Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as a Constraint Satisfaction Problem, with the following guidelines: 1. Since 3 x 3 puzzles are too trivial for a computer, your program should use 4 x 4 puzzles (also known as Super Sudoku puzzles; see Figure 2 for an example). 2. The program should read a Sudoku puzzle from a text file. The user should be able to browse the file system to select...

  • Problem 4.1 - Odd Bound States for the Finite Square Well Consider the finite square well...

    Problem 4.1 - Odd Bound States for the Finite Square Well Consider the finite square well potential of depth Vo, V(x) = -{ S-V., –a sx sa 10, else In lecture we explored the even bound state solutions for this potential. In this problem you will explore the odd bound state solutions. Consider an energy E < 0 and define the (real, positive) quantities k and k as 2m E K= 2m(E + V) h2 h2 In lecture we wrote...

  • JAVA Write a program which will read a text file into an ArrayList of Strings. Note...

    JAVA Write a program which will read a text file into an ArrayList of Strings. Note that the given data file (i.e., “sortedStrings.txt”) contains the words already sorted for your convenience. • Read a search key word (i.e., string) from the keyboard and use sequential and binary searches to check to see if the string is present as the instance of ArraryList. • Refer to “SearchInt.java” (given in “SearchString.zip”) and the following UML diagram for the details of required program...

  • Playgrounds and Performance: Results Management at KaBOOM! (A) We do this work because we want to...

    Playgrounds and Performance: Results Management at KaBOOM! (A) We do this work because we want to make a difference in the world; how can we go further faster? - Darell Hammond, CEO and co-founder, KaBOOM! Darell Hammond stepped onto the elementary school playground and took a long, slow look around. It was 8 a.m. on an unusually warm fall day in 2002 and the playground was deserted, but Hammond knew the children would start arriving soon to admire their new...

  • Mountain Paths (Part 1) Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e...

    Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e. parallel arrays … called multiple arrays in the zyBook) Transform data Read from files Write to files structs Code Requirements Start with this code: mtnpathstart.zip Do not modify the function signatures provided. Do not #include or #include Program Flow Read the data into a 2D array Find min and max elevation to correspond to darkest and brightest color, respectively Compute the shade of...

  • SECTION A (50) Read the case study below and answer the questions. SHORT RUN STABILIZATION AND...

    SECTION A (50) Read the case study below and answer the questions. SHORT RUN STABILIZATION AND LONG RUN COMPETITIVENESS: THE LAVITAN CASE Growth of a young country Latvia – a small, young country on the east coast of the Baltic Sea – has recently earned the title of a ‘‘tiger’’. After gaining its independence from the Soviet Union in 1991, the country embarked upon a challenging road of transitioning from a planned to a market economy. The first decade proved...

  • please help with a detailed, fully explained answer for Question 2. thank you Read the case...

    please help with a detailed, fully explained answer for Question 2. thank you Read the case study below and answer the questions. SHORT RUN STABILIZATION AND LONG RUN COMPETITIVENESS: THE LAVITAN CASE Growth of a young country Latvia - a small, young country on the east coast of the Baltic Sea -has recently earned the title of a "tiger". After gaining its independence from the Soviet Union in 1991, the country embarked upon a challenging road of transitioning from a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT