Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Josephus Problem

Rhythm Gupta
February 14, 2012

Josephus Problem

This PDF shows How to solve Josephus problem using queue , made up of two stacks.

Rhythm Gupta

February 14, 2012
Tweet

More Decks by Rhythm Gupta

Other Decks in Programming

Transcript

  1. € Implement a Stack ADT using linked lists. Implement a

    Queue ADT using two stacks. Use the Queue to solve the Josephus problem explained below. € The Josephus problem is a famous problem in algorithm design. It can best be explained as the solution to one- potato-two-potato: Children sometimes choose which of them is to be given a prize by what they think is a random process: They stand in a circle and count, "1 potato, 2 potato, 3 potato, 4; 5 potato, 6 potato, 7 potato, more; Y- O-U spells € you!". This is a count of 13 around the circle. Then they eliminate the child it ends on. This is repeated until only one child is left, which is the child who gets the prize.
  2. Linked List Stack € Created a Node. € Defined Methods

    of accesing the Node. € Created methods like Push, Pop and Top, i.e I created a stack with the help of a linked list.
  3. € For Queue Using Two Stacks, I created two Stacks.

    Dequeue is complex. If this box is not empty, pop. And push into right. Element 1 Element 2 For Insertion, Enqueue For deletion, DEQUEUE If yes, pop from left
  4. A B C D E Suppose Game starts from A

    and K=13. So the first person to leave is C
  5. A B D E The Algorithm is to enqueue and

    dequeue people from the queue After setting the counter k, and to remove the kth child. Don’t Enqueue It. Hence Its Done.