Computer Science/Discrete mathmatics

[이산수학] Combinatorics and Probability 조합론과 확률 3주차

dev seon 2023. 9. 13. 20:58

학습목표

 

- 문제를 계산할 때 표준 조합 세팅 사용하기
- 카운팅 문제를 표준 조합 세팅으로 분류하기
- 표준 조합 세팅을 통한 개체 수 세기
- 여러 조합 세팅을 결합해 계산 문제 해결하기

 

복습

() parentheses(소괄호) 순서 있는 집합{} braces(중괄호) 순서 없는 집합

튜플 : 순서 있는 선택, 중복 가능

from itertools import product

for t in product('abc', repeat=2):
    print(*t, sep='', end=' ')

순열 permutation : 순서 있는 선택, 중복 불가능 n!/(n-k)!

from itertools import permutations

for t in permutations('abc', 2):
    print(*t, sep='', end=' ')

조합 combination : 순서 없는 선택, 중복 불가능 n!/k!(n-k!)

from itertools import combinations

for t in combinations('abc', 2):
    print(*t, sep='', end=' ')

중복조합 combination with repetition : 순서 없는 선택, 중복 가능

from itertools import combinations_with_replacement

for t in combinations_with_replacement('abc', 2):
    print(*t, sep='', end=' ')

 

 

문제)샐러드 조합하기(3개의 다른 재료 중 4개 고르기)

해설) 경우의 수 확인하기

토마토가 4개 - 1

토마토가 3개 - 2

토마토가 2개 - 3

토마토가 1개 - 4

토마토가 0개 - 5

from itertools import combinations_with_replacement

for salad in combinations_with_replacement('TBL', 4):
    print(*salad)

얇은 바와 굵은 바로 종류를 구분하여 6개의 객체가 있다고 보았을 때 6개 중에 바 위치 2개 고르는 조합으로 바꿀 수 있음

 

중복조합 계산법

 

coursera Introduction to Discrete Mathematics for Computer Science 과정 중

 Combinatorics and Probability 3주차 내용 정리