Algorithms For Generating Combinatorial Objects
Algorithms For Generating Combinatorial Objects
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
Contohnya, n = 3 maka kita akan mendapatkan hasil sebagai berikut : 000 001 011 010 110 111 101 100.