๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
5๏ธโƒฃ CS/Algorithm

[Java Algorithm Note] ๋ถ€๋ถ„์ง‘ํ•ฉ (feat. ์žฌ๊ท€)

by seolhee2750 2022. 8. 16.

๋“œ๋””์–ด ์ˆœ์—ด, ์กฐํ•ฉ, ๋ถ€๋ถ„์ง‘ํ•ฉ ์‹œ๋ฆฌ์ฆˆ ์ค‘ ๋งˆ์ง€๋ง‰์ธ ๋ถ€๋ถ„์ง‘ํ•ฉ ๋“ฑ์Ÿ

(์ˆœ์—ด, ์กฐํ•ฉ ์ •๋ฆฌ๋Š” ์•„๋ž˜๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”! ๐Ÿ‘‡)

https://seolhee2750.tistory.com/229

 

[Java Algorithm Note] ์ˆœ์—ด (feat. ์žฌ๊ท€)

์ˆœ์—ด, ์กฐํ•ฉ, ๋ถ€๋ถ„์ง‘ํ•ฉ ์ด ์„ธ๊ฐ€์ง€์— ๋Œ€ํ•ด์„œ ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐ์„ ๋ชปํ–ˆ๋Š”๋ฐ, ์ด๋ฒˆ์— ๋ฐฐ์›Œ๋ณด๋‹ˆ ์ •๋ง ๋„ˆ๋ฌด๋„ˆ๋ฌด ์ค‘์š”ํ•œ ๊ฒƒ๋“ค์ด์–ด๋”ฐ..!! ์ด๊ฒƒ๋“ค์„ ๊ณต๋ถ€ํ•˜๊ณ ๋‚˜๋‹ˆ ๋ฌธ์ œ์—์„œ๋„ ๋‹ค์–‘ํ•˜๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Œ ๊ทธ๋ž˜

seolhee2750.tistory.com

https://seolhee2750.tistory.com/230

 

[Java Algorithm Note] ์กฐํ•ฉ (feat. ์žฌ๊ท€)

์ €๋ฒˆ ์ˆœ์—ด์— ์ด์–ด ์ด๋ฒˆ์—๋Š” ์กฐํ•ฉ์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๋ ค ํ•œ๋‹ค. (์ˆœ์—ด ์ •๋ฆฌ๋Š” ์•„๋ž˜ ์ฐธ๊ณ ! ๐Ÿ‘‡) https://seolhee2750.tistory.com/229 [Java Algorithm Note] ์ˆœ์—ด (feat. ์žฌ๊ท€) ์ˆœ์—ด, ์กฐํ•ฉ, ๋ถ€๋ถ„์ง‘ํ•ฉ ์ด ์„ธ๊ฐ€์ง€์— ๋Œ€ํ•ด์„œ..

seolhee2750.tistory.com

 

๐Ÿ“Ž ๋ถ€๋ถ„์ง‘ํ•ฉ

๋ถ€๋ถ„์ง‘ํ•ฉ์€ ์ค‘๋ณต๋„ ๊ฐ€์ง€์ง€ ์•Š๊ณ , ์ˆœ์„œ๋„ ๊ฐ€์ง€์ง€ ์•Š์œผ๋ฉฐ ์ตœ๋Œ€ r๊ฐœ๋ฅผ ๋ฝ‘์•„ ๋‚˜์—ดํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

 

์ˆœ์—ด๊ณผ ์กฐํ•ฉ์€ ๋ชจ๋‘ ์ •ํ•ด์ง„ ๊ฐœ์ˆ˜๋งŒํผ๋งŒ ๋ฝ‘์•„ ๋‚˜์—ดํ•˜์˜€๋‹ค.

ํ•˜์ง€๋งŒ ๋ถ€๋ถ„์ง‘ํ•ฉ์˜ ๊ฒฝ์šฐ ์›ํ•˜๋Š” ๊ฐœ์ˆ˜'๊นŒ์ง€'์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ฝ‘์•„ ๋‚˜์—ดํ•œ๋‹ค.

(3๊ฐœ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด 0๊ฐœ์ผ ๋•Œ๋ถ€ํ„ฐ 3๊ฐœ์ผ ๋•Œ๊นŒ์ง€๋ฅผ ๋ชจ๋‘ ๊ณ ๋ ค!)

 

์˜ˆ๋ฅผ ๋“ค์–ด {1, 2, 3}์ด ์ฃผ์–ด์ง€๋ฉด

{}, {1}, {2}, {3}, {1, 2}, ..., {1, 2, 3} ์ด๋ ‡๊ฒŒ!!


๐Ÿ“Ž ๋ถ€๋ถ„์ง‘ํ•ฉ ๊ตฌํ˜„

 

public class SubSet {
	
	static char[] src = { 'a', 'b', 'c', 'd' };
	
	/*
     * ๋ถ€๋ถ„ ์ง‘ํ•ฉ: ์ค‘๋ณต X, ์ˆœ์„œ X
     * @param toCheck - ์„ ํƒ๋  ์ˆ˜ ์žˆ๋Š” ์š”์†Œ์˜ ์ธ๋ฑ์Šค
     * @param checked - ์„ ํƒ๋œ ์š”์†Œ๋“ค์˜ ์ž๋ฆฌ๋ฅผ true๋กœ ํ‘œํ˜„
     */
	static void powerSetDupPer(int toCheck, boolean[] checked) {
        if(toCheck == checked.length) { // ๊ธฐ์ € ์กฐ๊ฑด: choosed๋ฅผ ๋‹ค ์ฑ„์› ๋‹ค๋ฉด ๋
        	print(checked);
        	return;
        }
        
        // toCheck์— ํ•ด๋‹นํ•˜๋Š” ์š”์†Œ๋ฅผ ์„ ํƒํ•  ๊ฒƒ์ธ์ง€ ์•ˆํ•  ๊ฒƒ์ธ์ง€
        checked[toCheck] = true;
        powerSetDupPer(toCheck+1, checked);
        checked[toCheck] = false;
        powerSetDupPer(toCheck+1, checked);
    }

    static void print(boolean[] temp) {
    	List<Character> selected = new ArrayList<>();
    	List<Character> unselected = new ArrayList<>();
    	for(int i = 0; i < temp.length; i++) {
    		if(temp[i]) selected.add(src[i]);
    		else unselected.add(src[i]);
    	}
        System.out.println(selected + " : " + unselected);
    }
    
	public static void main(String[] args) {
		System.out.println("๋ถ€๋ถ„์ง‘ํ•ฉ");
        powerSetDupPer(0, new boolean[3]); // src๋กœ ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ 3๊ฐœ์˜ ๋ชจ๋“  ๋ถ€๋ถ„์ง‘ํ•ฉ ์ถœ๋ ฅ
	}

}

checked ๋ฐฐ์—ด์— ์„ ํƒํ•œ ์š”์†Œ๋Š” true๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ์–ด ์„ ํƒํ–ˆ์Œ์„ ํ‘œ์‹œํ•˜๋Š” ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

booleanํ˜•ํƒœ์˜ checked ๋ฐฐ์—ด์„ ์™„์„ฑํ•˜๋ฉด, print ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ง‘ํ•ฉ์„ ์ถœ๋ ฅํ–ˆ๋‹ค.


๐Ÿ‘€ ์ •๋ฆฌ

๋ถ€๋ถ„ ์ง‘ํ•ฉ์€ ์„ ํƒํ•œ ๊ฐœ์ˆ˜ ๋‚ด์—์„œ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์กฐํ•ฉ์„ ์˜๋ฏธํ•œ๋‹ค.

๋ถ€๋ถ„ ์ง‘ํ•ฉ ๋˜ํ•œ ์ˆœ์—ด๊ณผ ์กฐํ•ฉ์ฒ˜๋Ÿผ ์žฌ๊ท€๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ,

0๊ฐœ๋ถ€ํ„ฐ n๊ฐœ๊นŒ์ง€์˜ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ์ฒดํฌํ•ด์ฃผ์–ด์•ผ ํ•˜๋ฏ€๋กœ,

๋ฐ”๋กœ ๋ฐฐ์—ด์— ๊ฐ’์„ ๋‹ด์ง€ ์•Š๊ณ  boolean์œผ๋กœ ์ฒดํฌํ•ด์ค€ ๋’ค ๊ฐ’์„ ๋”ฐ๋กœ ์ถœ๋ ฅํ•˜๋Š” ์‹์œผ๋กœ ์ž‘์„ฑํ–ˆ๋‹ค.

๋Œ“๊ธ€