๐์์ด, ์กฐํฉ, ๋ถ๋ถ์งํฉ
1. ์์ด(Permutation)
: ์์์ ๋ฐ๋ผ ๋ฐฐ์ด๋ ์์๋ค์ ๋ชจ๋ ๊ฐ๋ฅํ ์กฐํฉ(์์ O, ์ค๋ณต X) - ์ฃผ์ด์ง ์์๋ค์ ๋์ดํ๋ ๊ฒฝ์ฐ์ ์ ๊ณ์ฐ
2. ์กฐํฉ(Combination)
: ์์์ ์๊ด์์ด ์ ํ๋ ์์๋ค์ ๋ชจ๋ ๊ฐ๋ฅํ ์กฐํฉ(์์X, ์ค๋ณตX) - ์ฃผ์ด์ง ์์๋ค์ ๋ฝ๋ ๊ฒฝ์ฐ์ ์ ๊ณ์ฐ
3. ๋ถ๋ถ์งํฉ(Subset)
: ์ด๋ค ์งํฉ์ ์ผ๋ถ ์์๋ก ์ด๋ฃจ์ด์ง ์งํฉ - ์ฃผ์ด์ง ์งํฉ์ ๋ชจ๋ ๋ถ๋ถ์งํฉ ๊ณ์ฐ
๐์์ด ๊ตฌํ
1. ์ค๋ณต์์๋ฅผ boolean ๋ฐฐ์ด์ ์ด์ฉํด์ ํ์ธ
import java.util.Arrays;
// ์ค๋ณต ์์๋ฅผ ๋ฐฐ์ด์ ์ด์ฉํด์ ์ฒดํฌํ๊ธฐ
// 9P9 : 0.01 (์์ )
// 10P10 : 0.1์ด ์ปท (์ฝ๊ฐ ์ํ)
public class PermutationTest_flag {
static int N,R; //nPr
static int[] numbers; //๊ธฐ์กด data
static int[] perms; //๋ด๊ฐ ๋ง๋ ์์ด ์กฐํฉ
static boolean[] isSelected; //๋ฐฐ์ด์ ์ด์ฉํด์ ์ค๋ณต ์์ ๊ด๋ฆฌ
////////////count
static long tc; //์์ด ๊ฐ์
static long cnt; //๋ฐ๋ณต ํ์
public static void perm(int depth){
if(depth>=R){
tc++;
//System.out.println(Arrays.toString(perms));
return;
}
for(int i=0;i<N;i++){
cnt++;
if(isSelected[i]) continue;
perms[depth]=i;
isSelected[depth]=true;
perm(depth+1);
isSelected[depth]=false;
}
}
public static void main(String[] args){
numbers = new int[] {1,2,3,4,5,6,7,8,9,10};
N = numbers.length;
R = numbers.length;
perms = new int[R];
isSelected = new boolean[N];
long start = System.currentTimeMillis();
perm(0);
long end = System.currentTimeMillis();
System.out.printf("tc: %d count:%d time:%dms%n", tc, cnt, end-start);
}
}
2. ์ค๋ณต์์๋ฅผ ๋นํธ๋ง์คํน์ ์ด์ฉํด์ ํ์ธ
import java.util.Arrays;
// ์ค๋ณต ์์๋ฅผ bitmask๋ฅผ ์ด์ฉํด์ ์ฒดํฌํ๊ธฐ
// 9P9 : 0.009 (์์ )
// 10P10 : 0.1์ด ์ปท (์ฝ๊ฐ ์ํ) tc: 3628800 count:62353010 time:173ms
public class PermutationTest_bitmasking {
static int N,R; //nPr
static int[] numbers; //๊ธฐ์กด data
static int[] perms; //๋ด๊ฐ ๋ง๋ ์์ด ์กฐํฉ
static int bitmasking; //๋นํธ๋ง์คํน์ ์ด์ฉํ ์ค๋ณต ์์ ๊ด๋ฆฌ
////////////count
static long tc; //์์ด ๊ฐ์
static long cnt; //๋ฐ๋ณต ํ์
public static void perm(int depth){
if(depth>=R){
tc++;
//System.out.println(Arrays.toString(perms));
return;
}
for(int i=0;i<N;i++){
cnt++;
if((bitmasking & (1<<i))!=0) continue;
perms[depth]=i;
bitmasking |= (1<<i);
perm(depth+1);
bitmasking &= ~(1<<i);
}
}
public static void main(String[] args){
//numbers = new int[] {1,2,3};
numbers = new int[] {1,2,3,4,5,6,7,8,9,10};
N = numbers.length;
R = numbers.length;
perms = new int[R];
bitmasking = 0;
long start = System.currentTimeMillis();
perm(0);
long end = System.currentTimeMillis();
System.out.printf("tc: %d count:%d time:%dms%n", tc, cnt, end-start);
}
}
3. swap ์์ด
- ๋ฐฐ์ด์ ์ฒซ ๊ฐ๋ถํฐ ์์๋๋ก ํ๋์ฉ ๋ฐ๊พธ๋ฉฐ ๋ชจ๋ ๊ฐ์ ํ๋ฒ์ฉ swapํ๋ค.
- depth๋ฅผ ๊ธฐ์ค ์ธ๋ฑ์ค๋ก ํ์ฌ depth๋ณด๋ค ์์ ๊ฐ๋ค์ ๊ทธ๋๋ก ๊ณ ์ ํ๊ณ depth ์ด์์ ์ธ๋ฑ์ค๋ค๋ง swap์ ์งํํ๋ค.
import java.util.Arrays;
// 10P10 : 0.02์ด ์ปท (์์ ) tc: 3628800 count:0 time:47ms
public class PermutationTest_swap {
static int N,R; //nPr
static int[] numbers; //๊ธฐ์กด data
////////////count
static long tc; //์์ด ๊ฐ์
static long cnt; //๋ฐ๋ณต ํ์
public static void perm(int depth){
if(depth>=R){
tc++;
//System.out.println(Arrays.toString(numbers));
return;
}
for(int i=depth;i<N;i++){
swap(depth, i);
perm(depth+1);
swap(depth, i);
}
}
public static void swap(int i, int j){
int tmp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = tmp;
}
public static void main(String[] args){
//numbers = new int[] {1,2,3};
numbers = new int[] {1,2,3,4,5,6,7,8,9,10};
N = numbers.length;
R = numbers.length;
long start = System.currentTimeMillis();
perm(0);
long end = System.currentTimeMillis();
System.out.printf("tc: %d count:%d time:%dms%n", tc, cnt, end-start);
}
}
4. Next Permutation์ผ๋ก ์์ด ๊ตฌํ
์์ด์ ์ฌ์ ์์ผ๋ก ๋์ดํ ๋, ํ์ฌ ์์ด์ ๋ค์์ผ๋ก ์ค๋ ์์ด์ ์ฐพ๋ ์๊ณ ๋ฆฌ์ฆ
1) ๋ค์ชฝ๋ถํฐ ํ์ํ์ฌ p[i-1]<p[i]๋ฅผ ๋ง์กฑํ๋ ๊ฐ์ฅ ํฐ i๊ฐ ์ฐพ๊ธฐ. ์ด๋, i=0์ด๋ฉด ํ์์ข ๋ฃ
2) ๋ค์ชฝ๋ถํฐ ํ์ํ์ฌ p[i-1]<p[j]๋ฅผ ๋ง์กฑํ๋ ๊ฐ์ฅ ํฐ j๊ฐ์ ์ฐพ์ swap
3) index i~N๊น์ง swap
=>๋ฐ๋ณต
Next Permutation์ ๊ตฌํ์ด ์ด๋ ต์ง๋ง, O(N)์ ์๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋ฉฐ ๊ตฌํ๋ ์ง๊ด์ ์ด๊ณ ์ดํดํ๊ธฐ ์ฝ๋ค.
import java.util.Arrays;
// 10P10 : 0.02์ด time : 29ms count:5880473 tc:3628800
// 11P11 : 0.15์ด์ปท (์ํ)
public class Permutation_NP {
static int[] numbers; //๊ธฐ์กด data
////////////count
static long tc=0; //์์ด ๊ฐ์
static long cnt=0; //๋ฐ๋ณต ํ์
public static boolean np(int[] p){
int N = p.length-1;
int i = N;
while(i>0 && p[i-1]>=p[i]){
//๊ฐ์ฅ ํฐ p[i]<p[i+1]๋ฅผ ์ฐพ๊ธฐ
cnt++;
--i;
}
if(i==0) return false;
int j = N;
while(p[i-1]>p[j]){
cnt++;
--j;
}
swap(p,i-1,j);
int k = N;
while(i<k){
cnt++;
swap(p,i++,k--);
}
return true;
}
public static void swap(int[] p, int i, int j){
int tmp = p[i];
p[i] = p[j];
p[j] = tmp;
}
public static void main(String[] args){
numbers = new int[] {1,2,3,4,5,6,7,8,9,10};
long start = System.currentTimeMillis();
do{
tc++;
//System.out.println(Arrays.toString(numbers));
}while(np(numbers));
long end = System.currentTimeMillis();
System.out.printf("time : %dms count:%d tc:%d%n", end-start, cnt, tc);
}
}
๐์กฐํฉ ๊ตฌํ
import java.util.Arrays;
/**
* 25C12,13 => 35ms ์์ ์กฐํฉ์ : 5,200,300
* 26C8 => 110ms ์ํ ์กฐํฉ์ : 10,400,600 ==> ๋ฐฑํธ๋ ํน์ ์๋
* 27C14 => 241ms ์ํ ==> ๋ฐฑํธ๋ ํน์ ์๋
* 30C15 => 1.2์ด ์ปท ์๋จ 1์ต 5์ฒ
*/
public class CombinationTest {
static int N,R; //nCr
static int[] numbers; //๊ธฐ์กด data
static int[] combis; //๋ด๊ฐ ๋ฝ์ ์กฐํฉ
////////////count
static long tc=0; //์กฐํฉ ๊ฐ์
static long cnt=0; //๋ฐ๋ณต ํ์
public static void combi(int depth, int start){
if(depth>=R){
tc++;
//System.out.println(Arrays.toString(combis));
return;
}
for(int i=start;i<N;i++){
cnt++;
combis[depth]=i;
combi(depth+1,i+1);
}
}
public static void main(String[] args){
N = 26;
R = 13;
numbers = new int[N];
for(int i=0;i<N;i++) numbers[i] = i;
combis = new int[R];
long start = System.currentTimeMillis();
combi(0,0);
long end = System.currentTimeMillis();
System.out.printf("time : %dms count:%d tc:%d%n", end-start, cnt, tc);
}
}
๐๋ถ๋ถ์งํฉ ๊ตฌํ
// 25powser : 75ms ๊ฒฝ์ฐ์ ์ : 33,554,432
// ์ด ๊ฒฝ์ฐ์ ์ : 33554432 time:146
// 26powser : 140ms ๊ฒฝ์ฐ์ ์ : 67,108,863 ์ํ => BT ํ์
// ์ด ๊ฒฝ์ฐ์ ์ : 67108864 time:290
public class SubsetTest {
static int N; //data length
static int[] numbers; //๊ธฐ์กด data
static boolean[] isSelected; //๋ด๊ฐ ๋ฝ์ ๋ถ๋ถ์งํฉ
////////////count
static long tc=0; //๋ถ๋ถ์งํฉ ๊ฐ์
public static void subset(int depth){
if(depth>=N){
tc++;
//for(int n : numbers) System.out.print((char)(n+'a')+" ");
//System.out.println();
return;
}
// ๋ถ๋ถ์งํฉ์ ํ์ฌ ์์๋ฅผ ์ ํ
isSelected[depth]=true;
subset(depth+1);
isSelected[depth]=false;
// ๋ถ๋ถ์งํฉ์ ํ์ฌ ์์๋ฅผ ์ ํ X
subset(depth+1);
}
public static void main(String[] args){
N = 25;
numbers = new int[N];
for(int i=0;i<N;i++) numbers[i]=i;
isSelected = new boolean[N];
long start = System.currentTimeMillis();
subset(0);
long end = System.currentTimeMillis();
System.out.printf("์ด ๊ฒฝ์ฐ์ ์ : %d time:%d%n",tc, end-start);
}
}
๊ธฐ์ฃฝ์ง ๋ง์ฅ ..! ํ์ดํ ๐ฅน
'์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค]๋ฉ๋ด ๋ฆฌ๋ด์ผ-java (0) | 2024.11.21 |
---|---|
[์๊ณ ๋ฆฌ์ฆ]์ต์์ ์ฅํธ๋ฆฌ: kruskal,prim์๊ณ ๋ฆฌ์ฆ - Java๋ก ๊ตฌํํ๊ธฐ (1) | 2024.02.24 |
[์๊ณ ๋ฆฌ์ฆ]DynamicProgramming (0) | 2022.04.23 |
[์๊ณ ๋ฆฌ์ฆ]Sorting Algorithm_RadixSort (0) | 2022.04.23 |
[์๊ณ ๋ฆฌ์ฆ]Sorting Algorithm_HeapSort (0) | 2022.04.23 |