100% found this document useful (1 vote)
346 views

Algorithms For Generating Combinatorial Objects

The document discusses algorithms for generating combinatorial objects such as permutations, subsets, and binary reflected Gray codes. It provides pseudocode for the Johnson-Trotter algorithm to generate permutations by iteratively finding the largest mobile element and swapping it with the next element. It also provides pseudocode for generating binary reflected Gray codes recursively by adding a bit in front of strings in the list and its reverse. An example is given for n=3 that outputs the 8 possible 3-bit strings in Gray code order.

Uploaded by

aM_zHree
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
346 views

Algorithms For Generating Combinatorial Objects

The document discusses algorithms for generating combinatorial objects such as permutations, subsets, and binary reflected Gray codes. It provides pseudocode for the Johnson-Trotter algorithm to generate permutations by iteratively finding the largest mobile element and swapping it with the next element. It also provides pseudocode for generating binary reflected Gray codes recursively by adding a bit in front of strings in the list and its reverse. An example is given for n=3 that outputs the 8 possible 3-bit strings in Gray code order.

Uploaded by

aM_zHree
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Algorithms for Generating Combinatorial Objects

Rizki Makarim Ilham Zuhri 5109100066 5109100108

Generating Permutations
Permutasi adalah penyusunan kembali suatu kumpulan objek dalam urutan yang berbeda dari urutan yang semula dengan memperhatikan urutan. Contoh: Ada sebuah kotak berisi 3 bola masingmasing berwarna merah, hijau dan biru. Jika seorang anak ditugaskan untuk mengambil 2 bola secara acak dan urutan pengambilan diperhatikan, ada berapa permutasi yang terjadi? Solusi: Ada 6 permutasi yaitu: M-H, M-B, H-M, H-B, B-M, B-H.

Johnson-Trotter algorithm
//Implements Johnson-Trotter algorithm for generating permutations //Input: A positive integer n //Output: A list of all permutations of {1,...,n} initialize the rst permutation with ... 1 2 while the last permutation has a mobile element do nd its largest mobile element k swap k with the adjacent element ks arrow points to reverse the direction of all the elements that are larger than k add the new permutation to the list

Johnson-Trotter algorithm
Jika kita memasukkan nilai n = 3, maka akan menghasilkan:
1 2 1 2 3 1 2 1 2 1 2 1

Generating Subsets

Binary Reected Gray code


//Generates recursively the binary reected Gray code of order n //Input: A positive integer n //Output: A list of all bit strings of length n composing the Gray code if n = 1make list L containing bit strings 0 and 1 in this order else generate list L1 of bit strings of size n 1 by calling BRGC(n 1) copy list L1 to list L2 in reversed order add 0 in front of each bit string in list L1 add 1 in front of each bit string in list L2 append L2to L1 to get list L return L

Contohnya, n = 3 maka kita akan mendapatkan hasil sebagai berikut : 000 001 011 010 110 111 101 100.

You might also like