์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch07. Recursive, Tree, Graph(DFS, BFS ๊ธฐ์ดˆ) : ๋ถ€๋ถ„์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ (DFS)

2023. 11. 22. 10:18ใ†์ธํ”„๋Ÿฐ/์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„

728x90

 

https://hyejin.tistory.com/1257

 

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch07. Recursive, Tree, Graph(DFS, BFS ๊ธฐ์ดˆ) : ์ด์ง„ ํŠธ๋ฆฌ ์ˆœํšŒ

https://hyejin.tistory.com/1256 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch07. Recursive, Tree, Graph(DFS, BFS ๊ธฐ์ดˆ) : ํ”ผ๋ณด๋‚˜์น˜ ์žฌ๊ท€ (๋ฉ”๋ชจ์ด์ œ์ด https://hyejin.tistory.com/1255 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch07. Recursive, Tree,

hyejin.tistory.com

-> ์ด์ „ ๋ฌธ์ œ ํ’€์ด 

 

 

6. ๋ถ€๋ถ„์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ 

์„ค๋ช…

์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์ง€๋ฉด 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ์›์†Œ๋ฅผ ๊ฐ–๋Š” ์ง‘ํ•ฉ์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ž…๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N(1<=N<=10)์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

 

์ถœ๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ ๊ฐ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ์•„๋ž˜์™€ ์ถœ๋ ฅ์˜ˆ์ œ์™€ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

๋‹จ ๊ณต์ง‘ํ•ฉ์€ ์ถœ๋ ฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ ์ž…๋ ฅ 1 

3

 

์˜ˆ์‹œ ์ถœ๋ ฅ 1

1 2 3
1 2
1 3
1
2 3
2
3

 

 

๋ฌธ์ œ ํ’€์ด 1

public class FindingASubset
{
   static int n; // ์ง‘ํ•ฉ ์›์†Œ์˜ ๊ฐœ์ˆ˜
   static int[] ch; // ๋ถ€๋ถ„์ง‘ํ•ฉ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค, ํ•˜์ง€ ์•Š๋Š”๋‹ค ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด์„œ
   
   public void DFS(int L)
   {
      if (L == n + 1) {
         String tmp = "";
         for (int i = 1; i <= n; i++)
         {
            if (ch[i] == 1) {
               tmp += i + " ";
            }
         }
         if (tmp.length() > 0) {
            System.out.println(tmp);
         }
      }else {
         ch[L] = 1;
         DFS(L + 1); // ํŠธ๋ฆฌ์˜ ์™ผ์ชฝ (์‚ฌ์šฉํ•œ๋‹ค)
         ch[L] = 0;
         DFS(L + 1); // ํŠธ๋ฆฌ์˜ ์˜ค๋ฅธ์ชฝ (์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.)
      }
   }
   public static void main(String[] args)
   {
      FindingASubset findingASubset = new FindingASubset();
      Scanner scanner = new Scanner(System.in);
      int L = scanner.nextInt();
      n = L;
      ch = new int[n + 1];
      findingASubset.DFS(1);
   }
}

๐Ÿ‘พ : DFS ๊ด€๋ จ ๋ฌธ์ œ ๋‚˜์˜ค๋ฉด ์–ด๋ ต๋‹ค.. ๊ทธ๋ž˜์„œ ๊ฒฐ๊ตญ ์ด๋ฒˆ์—๋„ ํ’€์ง€ ๋ชปํ•˜๊ณ ... ๊ฐ•์‚ฌ๋‹˜์˜ ๊ฐ•์˜๋ฅผ ๋“ค์—ˆ๋‹ค. 

์‚ฌ์‹ค ์ด๋ฒˆ์— DFS ์— ๋Œ€ํ•ด์„œ ์ฒ˜์Œ ๊ณต๋ถ€ํ•˜๊ธฐ๋„ ํ•˜๋‹ˆ.. ๊ฐ•์˜ ํ™•์‹คํžˆ ๋“ฃ๊ณ , ๋‚˜์ค‘์— ๊ด€๋ จ ๋ฌธ์ œ ๋‚˜์˜ฌ ๋•Œ ์‘์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ตํ˜€๋‘ฌ์•ผ ๊ฒ ๋‹ค. 

 

๋ฌธ์ œ๋Š” ์šฐ์„  DFS(1) ์—์„œ DFS(2), DFS(2) ๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ๋Š”๋ฐ ์ด๋•Œ ์™ผ์ชฝ DFS(2)๋Š” ํ•ด๋‹น ๊ฐ’์„ ๋ถ€๋ถ„์ง‘ํ•ฉ์— ํฌํ•จํ•œ๋‹ค๋Š” ๋œป(ch[2] = 1) ์ด๊ณ , ์˜ค๋ฅธ์ชฝ DFS(2)๋Š” ํ•ด๋‹น ๊ฐ’์„ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์—์„œ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๋œป์ด๋‹ค. (ch[2] = 0)

์ด๋Ÿฐ์‹์œผ๋กœ ๊ฐ€์ง€ ์น˜๊ธฐ๋ฅผ ํ•ด์„œ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์„ ๊ตฌํ•ด์ค„ ๊ฒƒ์ด๋‹ค. 

 

 

1๏ธโƒฃ

static int n; // ์ง‘ํ•ฉ ์›์†Œ์˜ ๊ฐœ์ˆ˜
static int[] ch; // ๋ถ€๋ถ„์ง‘ํ•ฉ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค, ํ•˜์ง€ ์•Š๋Š”๋‹ค ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด์„œ

์ด๋ฒˆ์—๋Š” ์ „์—ญ ๋ณ€์ˆ˜ n๊ณผ ch intํ˜• ๋ฐฐ์—ด์„ ์„ ์–ธํ–ˆ๋‹ค. 

n์€ ์ง‘ํ•ฉ ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ , ch๋Š” n ๋งŒํผ์˜ ๋ฐฐ์—ด์„ ์„ ์–ธํ•ด์„œ ํ•ด๋‹น ๊ฐ’์„ ํฌํ•จํ• ์ง€ (1) ์•ˆํ• ์ง€(0) ๋ฅผ ์ง€์ •ํ•ด์ค„ ๊ฒƒ์ด๋‹ค. 

 

2๏ธโƒฃ

public static void main(String[] args)
{
   FindingASubset findingASubset = new FindingASubset();
   Scanner scanner = new Scanner(System.in);
   int L = scanner.nextInt();
   n = L;
   ch = new int[n + 1];
   findingASubset.DFS(1);
}

๋‹ค์Œ์—๋Š” ์ž…๋ ฅ ๊ฐ’ L ์„ ๋ฐ›๊ณ , ์œ„์—์„œ ์„ ์–ธํ•œ ์ „์—ญ๋ณ€์ˆ˜ n์— L ๊ฐ’์„ ๋Œ€์ž…ํ•ด์ฃผ๊ณ , ch ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” n + 1๋กœ ์„ ์–ธํ•ด์ค€๋‹ค. 

n+1๋กœ ์„ ์–ธํ•˜๋Š” ์ด์œ ๋Š” ch[0]์€ ์ œ์™ธํ•˜๊ณ  ํ•  ์˜ˆ์ •์ด๋ผ์„œ... 

๊ทธ๋ฆฌ๊ณ  DFS ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ ๋งจ ์ฒ˜์Œ 1๋ถ€ํ„ฐ ๋Œ€์ž…ํ•ด์„œ ์‹œ์ž‘ํ•œ๋‹ค. 

 

3๏ธโƒฃ

public void DFS(int L)
{
   if (L == n + 1) {
      String tmp = "";
      for (int i = 1; i <= n; i++)
      {
         if (ch[i] == 1) {
            tmp += i + " ";
         }
      }
      if (tmp.length() > 0) {
         System.out.println(tmp);
      }
   }else {
      ch[L] = 1;
      DFS(L + 1); // ํŠธ๋ฆฌ์˜ ์™ผ์ชฝ (์‚ฌ์šฉํ•œ๋‹ค)
      ch[L] = 0;
      DFS(L + 1); // ํŠธ๋ฆฌ์˜ ์˜ค๋ฅธ์ชฝ (์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.)
   }
}

 

DFS ํ•จ์ˆ˜์—์„œ๋Š” L ๊ฐ’์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š”๋ฐ ์šฐ์„  1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค. 

๊ทธ๋Ÿผ DFS(1) ์ธ๋ฐ ์ด๋•Œ if-else๋กœ ๋งŒ์•ฝ L์ด n+1 ์ด๋˜๋ฉด ๊ทธ๋• ๋ฐฐ์—ด์˜ ๊ฐ’ ์ค‘ 1์ธ ์ธ๋ฑ์Šค ๊ฐ’์„ ์ถœ๋ ฅํ•ด์ค„ ๊ฒƒ์ด๋‹ค. 

L์ด n+1์ด ์•„๋‹ˆ๋ผ๋ฉด ์ด์ œ ch[L]์— ์šฐ์„  1 ๊ฐ’์„ ๋Œ€์ž…ํ•ด์ฃผ๊ณ , ๊ทธ ๋‹ค์Œ DFS(2) ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. 

... ์ด๋Ÿฐ์‹์œผ๋กœ ๋‚˜์•„๊ฐ€๋‹ค๊ฐ€ L์ด n+1 ์ฆ‰, ์—ฌ๊ธฐ์„  4๊ฐ€ ๋˜๋ฉด ch ๋ฐฐ์—ด์—์„œ 1์ธ ๊ฐ’์˜ ์ธ๋ฑ์Šค๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ๊ณ ,  -> 1 2 3 ์ถœ๋ ฅ 

๋‹ค์‹œ DFS(3) ์œผ๋กœ ๋Œ์•„์˜จ๋‹ค. 

 

 

 

๋‹ค์‹œ DFS(3) ์œผ๋กœ ๋Œ์•„์˜จ๋‹ค. DFS(3) ์—์„œ ch[3] = 0์œผ๋กœ ๊ฐ’์„ ์„ค์ •ํ•ด์ฃผ๊ณ , ๊ทธ ๋‹ค์Œ DFS(4)๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. 

๊ทธ๋Ÿฌ๋ฉด DFS(4)๋Š” n+1 ์ด๋ฏ€๋กœ, ch ๋ฐฐ์—ด์˜ ๊ฐ’์ด 1์ธ ์ธ๋ฑ์Šค๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. -> 1 2 ์ถœ๋ ฅ 

๋‹ค์Œ DFS(3) ์ด ๋๋‚ฌ์œผ๋‹ˆ DFS(2)๋กœ ๋Œ์•„์˜จ๋‹ค. 

 

 

DFS(2)๋กœ ๋Œ์•„์™€์„œ ch[2] = 0 ์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๊ณ , DFS(3) ์„ ํ˜ธ์ถœํ•œ๋‹ค. 

๊ทธ๋Ÿฌ๋ฉด ๋˜ ๋ฐ˜๋ณตํ•ด์„œ DFS(3) ์€ ch[3] = 1 ํ•˜๊ณ , DFS(4) ํ˜ธ์ถœํ•ด์„œ ch๊ฐ€ 1์ธ ์ธ๋ฑ์Šค ๊ฐ’ ํ˜ธ์ถœํ•œ๋‹ค. -> 1 3 ํ˜ธ์ถœ 

๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ DFS(3)์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค. 

 

 

๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ DFS(3)์œผ๋กœ ๋Œ์•„์˜ค๋ฉด ์ด์ œ ch[3] = 0 ์„ ๋Œ€์ž…ํ•˜๊ณ , DFS(4)๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋‹ค์‹œ ch์˜ ๊ฐ’์ด 1์ธ ์ธ๋ฑ์Šค๋ฅผ ํ˜ธ์ถœํ•ด์ค€๋‹ค. -> 1 ์ถœ๋ ฅ 

 

... ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฐ˜๋ณตํ•˜๋ฉด ๋ถ€๋ถ„ ์ง‘ํ•ฉ์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

+ ์ถ”๊ฐ€๋กœ ๋” ํ•ด๋ณด์ž๋ฉด... 

DFS(2) ๋„ ๊ทธ๋Ÿฌ๋ฉด ๋๋‚ฌ์œผ๋ฏ€๋กœ ๋‹ค์‹œ DFS(1)๋กœ ๋Œ์•„์™€์„œ ch[1] = 0 ์„ ์„ค์ •ํ•œ ๋‹ค์Œ, DFS(2) ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. 

๊ทธ๋Ÿผ DFS(2)๋Š” ๋‹ค์‹œ ch[2] =1 ํ•˜๊ณ , DFS(3) ํ˜ธ์ถœํ•˜๊ณ , ch[3] = 1 ์„ค์ •ํ•œ ๋‹ค์Œ, DFS(4) ๋ฅผ ๋งŒ๋‚˜๋ฉด ch์˜ ๊ฐ’์ด 1์ธ ์ธ๋ฑ์Šค ๊ฐ’์„ ํ˜ธ์ถœํ•œ๋‹ค . -> 2 3 ์ถœ๋ ฅ 

๊ทธ๋Ÿฌ๊ณ  ๋‹ค์‹œ DFS(3) ์œผ๋กœ ๋Œ์•„์˜ค๋ฉด ch[3] = 0 ์ด๊ณ  DFS(4) ๋ฅผ ํ˜ธ์ถœํ•ด ch ๋ฐฐ์—ด์˜ ์›์†Œ ๊ฐ’์ด 1์ธ ์ธ๋ฑ์Šค ๊ฐ’์„ ํ˜ธ์ถœํ•œ๋‹ค. 

-> 2 ์ถœ๋ ฅ 

๊ทธ๋Ÿฌ๋ฉด DFS(3)๋„ ๋๋‚ฌ๊ณ , ๋‹ค์‹œ DFS(2)๋กœ ๋Œ์•„์™€์„œ ch[2] = 0 ์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ๋‹ค์‹œ DFS(3)์„ ํ˜ธ์ถœํ•œ๋‹ค. 

๊ทธ๋Ÿฌ๋ฉด DFS(3) ์€ ch[3] = 1 ํ•˜๊ณ , DFS(4) ํ˜ธ์ถœํ•˜๋ฏ€๋กœ ch ๋ฐฐ์—ด์˜ ์›์†Œ ๊ฐ’์ค‘์— 1์ธ ์ธ๋ฑ์Šค ๊ฐ’์„ ํ˜ธ์ถœํ•˜๋ฉด -> 3 ์ถœ๋ ฅ 

๊ทธ ๋‹ค์Œ DFS(3)์œผ๋กœ ๋Œ์•„์˜ค๊ณ , ch[3] = 0 ์„ ์„ค์ •ํ•œ ๋‹ค์Œ, DFS(4) ํ˜ธ์ถœํ•˜๋ฉด ์ด์ œ ๋ชจ๋“  ๊ฐ’์ด 000์ด๋ฏ€๋กœ ์ถœ๋ ฅํ•˜๋Š”๊ฒŒ ์—†์ด ๋๋‚˜๊ฒŒ ๋œ๋‹ค ! 

 

 

728x90