언젠가는 되어있겠지
데이터 공부 노트
언젠가는 되어있겠지
전체 방문자
오늘
어제
  • 분류 전체보기 (410)
    • Programming (14)
      • Python (6)
      • Algorithm (8)
    • OS (9)
      • Linux (9)
    • DBMS (2)
      • MySQL (2)
    • IoT Apps (6)
      • AppInventor (5)
      • Arduino (1)
    • LeetCode (151)
      • Easy (136)
      • Medium (13)
      • Hard (2)
    • Course (207)
      • [IBM] Data Science (9)
      • [IBM] Data Engineering (5)
      • [Inflearn] Pandas (12)
      • [Inflearn] Public Data Anal.. (8)
      • [Kaggle] Data Science (49)
      • [Youtube] Informations (2)
      • [Coursera] Machine Learning (40)
      • [Progrmiz] Data Structure A.. (15)
      • [DataQuest] Data Engineerin.. (67)
    • Data Engineering (20)
      • [Data Quest] Handling Datas.. (16)
      • [Data Quest] Data Pipeline (4)
    • Certificate (0)
      • 빅데이터 분석기사 (0)
    • SQLD (0)
    • 하고 싶은 이야기 (0)
    • 소설 (0)

블로그 메뉴

  • 홈
  • 태그

공지사항

인기 글

태그

  • Kaggle Course
  • Data Science

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
언젠가는 되어있겠지

데이터 공부 노트

[Introduction] Divide and Conquer Algorithm
Course/[Progrmiz] Data Structure Algorithm

[Introduction] Divide and Conquer Algorithm

2022. 8. 9. 22:12

1. What is Divide and Conquer?

A divide and conquer algorithm is a strategy of solving a large problem by

  1. breaking the problem into smaller sub-problems
  2. solving the sub-problems, and
  3. combining them to get the desired output.

To use the divide and conquer algorithm, recursion is used.

 

2. How Divide and Conquer Algorithms Work?

  1. Divide : Divide the given problem into sub-problems using recursion
  2. Conquer : Solve the smaller sub-problems recursively. If the subproblem is small enough, then solve it directly.
  3. Combine : Combine the solutions of the sub-problems that ar part of the recursive process to solve the actual problem.

 

3. Example : Merge Sort

1. Let the given array be :

2. Divide the array into two halves.

Again, divide each subpart recursively into two halves until we get individual elements.

3. Combine the individual elements in a sorted manner.

4. Time Complexity

We will apply master theorem to calculate the complexity of the divide and conquer algorithms.

 

T(n) = aT(n/b) + f(n),
where,
n = size of input
a = number of subproblems in the recursion
n/b = size of each subproblem. All subproblems are assumed to have the same size.
f(n) = cost of the work done outside the recursive call, which includes the cost of dividing the problem and cost of merging the solutions

T(n) = aT(n/b) + f(n)
     = 2T(n/2) + O(n)
Where, 
a = 2 (each time, a problem is divided into 2 subproblems)
n/b = n/2 (size of each sub problem is half of the input)
f(n) = time taken to divide the problem and merging the subproblems
T(n/2) = O(n log n) (To understand this, please refer to the master theorem.)

Now, T(n) = 2T(n log n) + O(n)
          ≈ O(n log n)

 

5. Divide and Conquer vs Dynamic Approach

The divide and conquer approach divides a problem into smaller subproblems; these subproblems are further solved recursively. The result of each subproblem is not stored for future reference, whereas, in a dynamic approach, the result of each subproblem is stored for future reference.

 

Use the divide and conquer approach when the same subproblem is not solved multiple times. Use the dynamic approach when the result of a subproblem is to be used multiple times in the future.

 

# Divide and Conquer 
def fib(n): 
    if n < 2 : 
    	return 1
    else : 
    	return f(n - 1) + f(n -2)
        
# Dynamic Approach 
mem = []
fib(n)
    If n in mem: return mem[n] 
    else,     
        If n < 2, f = 1
        else , f = f(n - 1) + f(n -2)
        mem[n] = f
        return f

'Course > [Progrmiz] Data Structure Algorithm' 카테고리의 다른 글

[DS1] Queue  (0) 2022.08.10
[DS1] Stack  (0) 2022.08.10
[Introduction] Asymptotic Notations  (0) 2022.08.08
[Introduction] Why Learn DSA?  (0) 2022.08.08
[Introduction] Data Structure and Types  (0) 2022.08.08
    'Course/[Progrmiz] Data Structure Algorithm' 카테고리의 다른 글
    • [DS1] Queue
    • [DS1] Stack
    • [Introduction] Asymptotic Notations
    • [Introduction] Why Learn DSA?
    언젠가는 되어있겠지
    언젠가는 되어있겠지

    티스토리툴바