Bibi's DevLog ๐ค๐
[Swift] ์์ด๊ณผ ์กฐํฉ ๊ตฌํํ๊ธฐ Permutation & Combination ๋ณธ๋ฌธ
ํ๋ก๊ทธ๋๋ฐ/Data Structures ์๋ฃ๊ตฌ์กฐ
[Swift] ์์ด๊ณผ ์กฐํฉ ๊ตฌํํ๊ธฐ Permutation & Combination
๋น๋น bibi 2022. 10. 6. 15:08์ฐธ๊ณ ๋งํฌ
[์ฝ๋ฉํ
์คํธ ๋๋น] ์์ด(Permutation)๊ณผ ์กฐํฉ(Combination) ์๊ณ ๋ฆฌ์ฆ
์์ด ๊ตฌํํ๊ธฐ Permutation
/// ์์ด permutation
// ๊ธธ์ด๊ฐ n์ธ ๋ฐฐ์ด์์ r๊ฐ์ ์์๋ฅผ ์ฐจ๋ก๋๋ก ๋ฝ์ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ ๋ค.
// ๋ฐ๋ณต๋ฌธ : for๋ฌธ์ r๋ฒ ์ค์ฒฉ - i, j, k์ ์ค๋ณต์ ๋ฐฐ์ ํ๋ค
func permutation() {
// (n = 4, r = 3)๊ธธ์ด 4์ธ ๋ฐฐ์ด์์ 3๊ฐ์ ์์ ๋ฝ๊ธฐ
let array = [1, 2, 3, 4]
var answer: [[Int]] = []
for i in array.indices { // r๋ฒ ์ค์ฒฉ
for j in array.indices {
for k in array.indices {
if (i == j || j == k || k == i) {
continue
}
let arr = [array[i], array[j], array[k]] // ๊ธธ์ด r
answer.append(arr)
}
}
}
print(answer)
}
func getPermutation<T: Hashable> (array: [T],
count: Int,
tempArray: [T] = [], // ์์ด ์ค ํ๋๋ฅผ ์์ ์ ์ฅ
answer: [[T]] = [],
usingItems: Set<T> = []) -> [[T]] { // ์ด๋ค ์ซ์๊ฐ ์ฌ์ฉ์ค์ด๋ผ๋ฉด ์ฌ๊ธฐ์ ํฌํจ, ์๋๋ผ๋ฉด ๋บ๋ค
var currentTempArray = tempArray
var currentAnswer = answer
var currentUsingItems = usingItems
if currentTempArray.count == count { // ์์ด ์ค ํ๋๊ฐ ์์ฑ๋ ๊ฒฝ์ฐ
currentAnswer.append(currentTempArray)
print(currentAnswer)
return currentAnswer
}
// ์ฌ์ฉ์ค์ด์ง ์์ ์์ ์ค ํ๋๋ฅผ ์ฌ์ฉํด ์์ด์ ๊ตฌ์ฑ
for item in array {
if currentUsingItems.contains(item) {
continue
}
currentUsingItems.insert(item)
currentTempArray.append(item)
currentAnswer = getPermutation(array: array, count: count, tempArray: currentTempArray, answer: currentAnswer, usingItems: currentUsingItems)
currentTempArray.popLast()
currentUsingItems.remove(item)
}
return currentAnswer
}
// usingItems๋ฅผ ์ฐ์ง ์๋ ํ์ด - Set์ ์ฐ์ง ์์ผ๋ฏ๋ก ์๋๋ ์กฐ๊ธ ๋ ๋๋ฆฌ๋ค
// Set์ ์ฐ์ง ์๋ ๋์ tempArray์ ํด๋น ์์๊ฐ ํฌํจ๋์ด ์๋์ง(์ฌ์ฉ์ค์ธ์ง)๋ฅผ ์ฒดํฌํจ
func getPermutation2<T: Hashable> (array: [T],
count: Int,
tempArray: [T] = [],
answer: [[T]] = []) -> [[T]] {
var currentTempArray = tempArray
var currentAnswer = answer
if currentTempArray.count == count {
currentAnswer.append(currentTempArray)
return currentAnswer
}
for item in array {
if tempArray.contains(item) {
continue
}
currentTempArray.append(item)
currentAnswer = getPermutation2(array: array, count: count, tempArray: currentTempArray, answer: currentAnswer)
currentTempArray.popLast()
}
return currentAnswer
}
์กฐํฉ ๊ตฌํํ๊ธฐ Combination
/// ์กฐํฉ combination
// ์์๊ฐ ์ค์ํ์ง ์์.
// ๋ฐ๋ณต๋ฌธ์ผ๋ก ๊ตฌํ : for๋ฐ๋ณต๋ฌธ์ r๋ฒ ๋ฐ๋ณตํด ๊ตฌํ
func combination() {
let array = [1, 2, 3, 4]
var answer: [[Int]] = []
for i in 0..<array.count {
for j in i + 1..<array.count { // ์ง์ ๋ฐ๋ณต๋ฌธ์์ ์ฌ์ฉํ ๋ค์ ์์๋ฅผ ์ฌ์ฉํด ์ค๋ณต ํผํ๊ธฐ
for k in j + 1..<array.count {
answer.append([array[i], array[j], array[k]])
}
}
}
print(answer)
}
// ์ฌ๊ท๋ก ๊ตฌํ : ๋งค ์ธ๋ฑ์ค๊ฐ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ
func getCombination<T: Comparable>(array: [T],
count: Int,
tempArray: [T] = [],
answer: [[T]] = [],
lastIndex: Int = 0) -> [[T]] {
var currentTempArray = tempArray
var currentAnswer = answer
var currentLastIndex = lastIndex
// ์กฐํฉ ์ค ํ๋๊ฐ ์์ฑ๋ ๊ฒฝ์ฐ
if currentTempArray.count == count {
currentAnswer.append(currentTempArray)
return currentAnswer
}
for i in currentLastIndex..<array.count {
let item = array[i]
if tempArray.contains(item) {
continue
}
currentTempArray.append(item)
currentLastIndex += 1
currentAnswer = getCombination(array: array, count: count, tempArray: currentTempArray, answer: currentAnswer, lastIndex: currentLastIndex)
currentTempArray.popLast()
}
return currentAnswer
}