5
Assignment No 01 Submitted By: Sunawar Khan Reg No: 813/FBAS/MSCS/F14 Topic: Johnson Trotter Algorithm(Permutations) Submitted To: Dr. R. Aewan Subject: Advance Algorithm Analysis Date: 5 October, 2015 Department: Computer Science International Islamic university islamabad

Johnson Trotter Algorithm(Permutation)

Embed Size (px)

Citation preview

Page 1: Johnson Trotter Algorithm(Permutation)

Assignment No 01

Submitted By: Sunawar Khan

Reg No: 813/FBAS/MSCS/F14

Topic: Johnson Trotter Algorithm(Permutations)

Submitted To: Dr. R. Aewan

Subject: Advance Algorithm Analysis

Date: 5 October, 2015

Department: Computer Science

International Islamic university islamabad

Page 2: Johnson Trotter Algorithm(Permutation)

Algorithm:

//input: the number n whose permutation are to be obtained.

//output: permutation of numbers “n”

Initialize the first permutation with 1L 2L 3L 4L

While (there exist a mobile number K) do{ Find the largest mobile number k Swap mobile integer k and the adjacent number its arrow point Reverse the direction of all integers that are the largest then K }

Problem: obtain all possible permutations using Johnson Trotter Algorithm for n=4

Solution: As n=4 we will consider a set{1, 2, 3, 4} for generation permutation

1:- 1L 2L 3L 4L

Initially 4, arrow of 4 point to 3, exchange 4 with 32:- 1L 2L 4L 3L

Exchange 2 & 43:- 1L 4L 2L 3L

Exchange 4 & 14:- 4L 1L 2L 3L

4 is no longer mobile as it is on the left most column pointing to the left. Swap 3 & 2 and change 4’s direction.

5:- 4R 1L 3L 2L

Exchange 4 & 16:- 1L 4R 3L 2L

Exchange 4 & 37:- 1L 3L 4R 2L

Exchange 4 & 2 8:- 1L 3L 2L 4R

4 is no longer mobile as it is on the rightmost column pending to the right. Swap 3 & 4. Change 4’s direction.

9:- 3L 1L 2L 4L

Exchange 2 & 40:- 3L 1L 4L 2L

Exchange 1 & 41:- 3L 4L 1L 2L

Exchange 3 & 4

2:- 4L 3L 1L 2L

Page 3: Johnson Trotter Algorithm(Permutation)

4 is no longer mobile as it is on the leftmost column pointing to left. 3 is no longer mobile now as there is no number smaller then itself on its direction. Swap 2 & 1. Change 4 and 3 direction.

3:- 4R 3R 2L 1L

Exchange 4 & 34:- 3R 4R 2L 1L

Exchange 4 & 25:- 3R 2L 4R 1L

Exchange 4 & 16:- 3R 2L 1L 4R

4 is no longer mobile as to its on the rightmost column pointing to the right. Swap 3 & 2. Change 4’s direction.

7:- 2L 3R 1L 4L

Exchange 4 & 18:- 2L 3R 4L 1L

Exchange 4 & 39:- 2L 4R 3R 1L

Exchange 4 & 20:- 4L 2L 3R 1L

4 is no longer mobile as it is on the leftmost column pointing to the left. Swap 3 & 1. And change 4’s direction.

1:- 4R 2L 1L 3R

Exchange 4 & 22:- 2L 4R 1L 3R

Exchange 4 & 13:- 2L 1L 4R 3R

4:- 2L 1L 3R 4R

No mobile number are more.

Total Permutations:- 4 != 24