์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch08. DFS, BFS ํ™œ์šฉ : ํ•ฉ์ด ๊ฐ™์€ ๋ถ€๋ถ„ ์ง‘ํ•ฉ (DFS, ์•„๋งˆ์กด ์ธํ„ฐ๋ทฐ)

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

728x90

 

https://hyejin.tistory.com/1265

 

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

https://hyejin.tistory.com/1264 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch07. Recursive, Tree, Graph(DFS, BFS ๊ธฐ์ดˆ) : ๊ฒฝ๋กœ ํƒ์ƒ‰ (์ธ์ ‘๋ฆฌ์ŠคํŠธ) DFS https://hyejin.tistory.com/1263 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch07. Recursive, Tree, G

hyejin.tistory.com

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

 

 

 

1. ํ•ฉ์ด ๊ฐ™์€ ๋ถ€๋ถ„์ง‘ํ•ฉ (DFS, ์•„๋งˆ์กด ์ธํ„ฐ๋ทฐ) 

์„ค๋ช…

N๊ฐœ์˜ ์›์†Œ๋กœ ๊ตฌ์„ฑ๋œ ์ž์—ฐ์ˆ˜ ์ง‘ํ•ฉ์ด ์ฃผ์–ด์ง€๋ฉด,

์ด ์ง‘ํ•ฉ์„ ๋‘ ๊ฐœ์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์œผ๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ ๋‘ ๋ถ€๋ถ„์ง‘ํ•ฉ์˜ ์›์†Œ์˜ ํ•ฉ์ด ์„œ๋กœ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•˜๋ฉด “YES"๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ”NO"๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๋‘˜๋กœ ๋‚˜๋‰˜๋Š” ๋‘ ๋ถ€๋ถ„์ง‘ํ•ฉ์€ ์„œ๋กœ์†Œ ์ง‘ํ•ฉ์ด๋ฉฐ, ๋‘ ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ํ•ฉํ•˜๋ฉด ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์›๋ž˜์˜ ์ง‘ํ•ฉ์ด ๋˜์–ด ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด {1, 3, 5, 6, 7, 10}์ด ์ž…๋ ฅ๋˜๋ฉด {1, 3, 5, 7} = {6, 10} ์œผ๋กœ ๋‘ ๋ถ€๋ถ„์ง‘ํ•ฉ์˜ ํ•ฉ์ด 16์œผ๋กœ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

์ž…๋ ฅ ์„ค๋ช…

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

๋‘ ๋ฒˆ์งธ ์ค„์— ์ง‘ํ•ฉ์˜ ์›์†Œ N๊ฐœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ์›์†Œ๋Š” ์ค‘๋ณต๋˜์ง€ ์•Š๋Š”๋‹ค.

 

 

์ถœ๋ ฅ ์„ค๋ช…

์ฒซ ๋ฒˆ์งธ ์ค„์— “YES" ๋˜๋Š” ”NO"๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

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

6
1 3 5 6 7 10

 

 

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

YES

 

 

 

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

public class SubsetsWithTheSameSum2
{
   static String answer = "NO";
   static int n, total = 0;
   boolean flag = false;
   
   public void DFS(int L, int sum, int[] arr)
   {
      if (flag) return;
      if (sum > total/2) return;
      if (L == n) {
         if ((total- sum) == sum){
            flag = true;
            answer = "YES";
         }
      }else {
         DFS(L + 1, sum + arr[L], arr);
         DFS(L + 1, sum, arr);
      }
   }
   public static void main(String[] args)
   {
      SubsetsWithTheSameSum2 subsetsWithTheSameSum2 = new SubsetsWithTheSameSum2();
      Scanner scanner = new Scanner(System.in);
      n = scanner.nextInt();
      int[] arr = new int[n];
      
      for (int i = 0; i <n; i++)
      {
         arr[i] = scanner.nextInt();
         total += arr[i];
      }
      
      
      subsetsWithTheSameSum2.DFS(0, 0, arr);
      System.out.println("answer = " + answer);
      
   }
}

๐Ÿ‘พ :  

https://hyejin.tistory.com/1258

 

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

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

hyejin.tistory.com

์ด์ „์— ํ’€์—ˆ๋˜ ๋ถ€๋ถ„์ง‘ํ•ฉ ์ฒ˜๋Ÿผ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด ๋œ๋‹ค๊ณ  ํ•œ๋‹ค. 

๋ฌธ์ œ์—์„œ ์›ํ•˜๋Š” ๊ฒƒ์ด ์ฃผ์–ด์ง„ ์ง‘ํ•ฉ์—์„œ ๋‘ ๊ฐœ์˜ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์œผ๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ ๋‘ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์˜ ์›์†Œ์˜ ํ•ฉ์ด ์„œ๋กœ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•˜๋ฉด yes๋ฅผ ๋ฆฌํ„ดํ•˜๋ผ๊ณ  ํ–ˆ๋‹ค. 

 

 

 

 

-> ๊ทธ๋ฆผ์—์„œ ์ฒ˜๋Ÿผ D(0,0) ์œผ๋กœ ์‹œ์ž‘ํ•ด์„œ, ๋‹ค์Œ ๋ ˆ๋ฒจ๋กœ ๋„˜์–ด๊ฐ€์„œ ์›์†Œ 1์„ ํฌํ•จํ•œ๋‹ค, ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค ํ•ด์„œ 

D(1,1) -> ๋ ˆ๋ฒจ 1๋กœ ๋„˜์–ด๊ฐ€์„œ ์›์†Œ 1์„ ํฌํ•จํ•ด์„œ ์ดํ•ฉ์ด 0 +1 ์ด๋ผ๋Š” ๋œป์ด๋‹ค.

D(1,0) -> ๋ ˆ๋ฒจ 1๋กœ ๋„˜์–ด์™”๋Š”๋ฐ ์›์†Œ 1์„ ํฌํ•จํ•˜์ง€ ์•Š๊ณ , ์ดํ•ฉ์ด 0 ์ด๋ผ๋Š” ๋œป์ด๋‹ค. 

 

D(2,4) -> ๋ ˆ๋ฒจ 2๋กœ ๋„˜์–ด์™€์„œ ์›์†Œ 3์„ ํฌํ•จํ•˜๊ณ , ์ดํ•ฉ์ด 0 + 1 + 3 ์ด๋ผ๋Š” ๋œป์ด๋‹ค.

D(2,1) -> ๋ ˆ๋ฒจ 2๋กœ ๋„˜์–ด์™€์„œ ์›์†Œ 3์„ ํฌํ•จํ•˜์ง€ ์•Š๊ณ , ์ดํ•ฉ์ด 0 + 1 ์ด๋ผ๋Š” ๋œป์ด๋‹ค. 

 

... ์ด๋ ‡๊ฒŒ ๋ฐ˜๋ณตํ•ด์„œ ๋‘ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์˜ ํ•ฉ์ด ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค๋ฉด YES ๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค. 

 

static String answer = "NO";
static int n, total = 0;
boolean flag = false;

์ด ๋ฌธ์ œ์—์„œ๋Š” ์šฐ์„  n, total, answer๋ฅผ static ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  flag์—์„œ๋Š” ๋‘ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์˜ ํ•ฉ์ด ๊ฐ™์„ ๊ฒฝ์šฐ true๋กœ ๋ฐ”๊พธ๊ณ , flag๊ฐ€ true์ด๋ฉด DFS๋ฅผ ์ข…๋ฃŒํ•˜๋„๋ก ํ•œ๋‹ค. 

 

public void DFS(int L, int sum, int[] arr)
{
   if (flag) return;
   if (sum > total/2) return;
   if (L == n) {
      if ((total- sum) == sum){
         flag = true;
         answer = "YES";
      }
   }else {
      DFS(L + 1, sum + arr[L], arr);
      DFS(L + 1, sum, arr);
   }
}

-> L : ๋ ˆ๋ฒจ์„ ๋œปํ•˜๊ณ , sum ์€ ์›์†Œ์˜ ์ด ํ•ฉ, arr์€ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์ด๋‹ค. (์—ฌ๊ธฐ์„  ์™œ ๊ฐ‘์ž๊ธฐ ๋ฐฐ์—ด์„ DFS์— ํฌํ•จ์‹œ์ผฐ๋Š”์ง€๋Š”.. ๋ชจ๋ฅด๊ฒ ๋‹ค ใ…‹) 

DFS์ด๋ฏ€๋กœ if-else๋กœ ์‹œ์ž‘ํ•ด์ค€๋‹ค. ๋จผ์ € L์ด n์ด๋ฉด ์›์†Œ๋ฅผ ๋‹ค ๋Œ์•˜๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ ์ด์ œ ๊ฒ€์ฆ์„ ํ•˜๋Š”๋ฐ ์ด๋•Œ 
์ดํ•ฉ total - sum์ด sum ๊ณผ ๊ฐ™๋‹ค๋ฉด YES ๋กœ answer ๋ฅผ ๋ฐ”๊ฟ”์ฃผ๊ณ , flag๋ฅผ true๋กœ ์„ค์ •ํ•ด์ค€๋‹ค. 

 

L์ด n์ด ์•„๋‹ˆ๋ผ๋ฉด DFS ์žฌ๊ท€ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ํ†ตํ•ด DFS(L+ 1, sum + arr[L] , arr) ๊ณผ DFS(L + 1, sum, arr) ๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ๋Š”๋ฐ 

์•ž์— DFS ํ˜ธ์ถœ์€ ํ•ด๋‹น ์›์†Œ๋ฅผ ํฌํ•จํ•˜์—ฌ ๋”ํ•œ ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๊ณ , ๋’ค DFS ํ˜ธ์ถœ์€ ํ•ด๋‹น ์›์†Œ๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๊ณ  ๋”ํ•œ ๊ฐ’์„ ๋„˜๊ฒจ์ค€๋‹ค. 

 

if (flag) return;
if (sum > total/2) return;

DFS ํ˜ธ์ถœ ํ›„ flag๊ฐ€ true์ด๋ฉด ์ด๋ฏธ ๊ฐ’์„ ๊ตฌํ–ˆ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ตฌํ•ด๋ณผ ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋Œ€๋กœ return ํ•ด์ฃผ๊ณ , 

๋งŒ์•ฝ sum์ด total/2 ๊ฐ’์„ ๋„˜๊ฒจ๋ฒ„๋ ธ๋‹ค๋ฉด ๊ตฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ’์ด ์•„๋‹ˆ๋ฏ€๋กœ ๊ทธ๋ƒฅ return ํ•ด์ค€๋‹ค. 

 

728x90