Skip to content

์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ •๋ฆฌํ•ด๋‘์—ˆ์Šต๋‹ˆ๋‹ค.

Notifications You must be signed in to change notification settings

lemidia/Algorithm-and-Data-Structure

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

Algorithm

In mathematics and computer science, an algorithm is a finite sequence of well-defined, computer-implementable instructions, typically to solve a class of problems or to perform a computation. Algorithms are always unambiguous and are used as specifications for performing calculations, data processing, automated reasoning, and other tasks. - Wikipedia.

์ˆ˜ํ•™๊ณผ ์ปดํ“จํ„ฐ ๊ณผํ•™์—์„œ, ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์–ด๋–ค ์ง‘ํ•ฉ์˜ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•˜๊ฑฐ๋‚˜ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด, ์ž˜ ์ •์˜๋œ ํ•œ ์œ ํ•œ์ˆœ์„œ์ด์ž, ์ปดํ“จํ„ฐ๊ฐ€ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ง€์‹œ์‚ฌํ•ญ๋“ค ์ž…๋‹ˆ๋‹ค. ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํ•ญ์ƒ ๋ชจํ˜ธํ•˜์ง€ ์•Š๊ณ , ๊ณ„์‚ฐ์ด๋‚˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ, ์ž๋™ ์ถ”๋ก  ๋“ฑ๋“ฑ ์—ฌ๋Ÿฌ๊ณณ์—์„œ์˜ ๋ช…์„ธ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

DFS & BFS - ๊นŠ์ด์šฐ์„ ํƒ์ƒ‰ & ๋„ˆ๋น„์šฐ์„ ํƒ์ƒ‰

Binary Search - ์ด๋ถ„ํƒ์ƒ‰

Back Tracking + Brute Force - ๋ฐฑํŠธ๋ž˜ํ‚น + ์ „์ฒดํƒ์ƒ‰ ๋‹ค ํ•ด๋ณด๊ธฐ

  • Permutation - O(n!) ์ˆœ์—ด

  • Power Set Problem - O(2^n) ๋ชจ๋“  ์กฐํ•ฉ ๋‚˜์—ด

  • N Queen Problem - N x N ๊ฒฉ์žํŒ์— N๊ฐœ์˜ ํ€ธ์ด ์„œ๋กœ ๊ณต๊ฒฉํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๋†“์•„์ง€๊ฒŒ ํ•˜๋Š” ๋ฒ• ๊ตฌํ•˜๊ธฐ

  • Rat In A Maze Problem - ๋ฏธ๋กœ์ฐพ๊ธฐ ๋ฌธ์ œ

  • Sudoku - ์Šค๋„์ฟ , ์ˆซ์ž ํผ์ฆ ๋ฌธ์ œ

  • Sum Of Subset - ์ง‘ํ•ฉ์—์„œ์˜ ์›์†Œ๋“ค์„ ์กฐํ•ฉํ•ด์„œ ํŠน์ • ๊ฐ’์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€? O(2^n)

Tree Algorithm - ํŠธ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • Pre Order, In Order, Post Order - ์ „์œ„ ์ˆœํšŒ, ์ค‘์œ„ ์ˆœํšŒ, ํ›„์œ„ ์ˆœํšŒ | ๊ธฐ๋ณธ์ ์ธ ํŠธ๋ฆฌ์˜ ์ˆœํšŒ ๋ฐฉ๋ฒ•

  • Center of a Binary Tree - ํŠธ๋ฆฌ์˜ ์ค‘์‹ฌ(1๊ฐœ ๋˜๋Š” ์ตœ๋Œ€ 2๊ฐœ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค.)

  • Height of a Binary Tree - ํŠธ๋ฆฌ์˜ ๋†’์ด(๋ฃจํŠธ๋…ธ๋“œ๋กœ ๋ถ€ํ„ฐ ๊ฐ€์žฅ ๊นŠ์€ ์žŽ ๋…ธ๋“œ๋กœ์˜ ๊ฒฝ๋กœ์—์„œ์˜ ๋…ธ๋“œ ๊ฐœ์ˆ˜)

  • Diameter of a Binary Tree - ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„(ํŠธ๋ฆฌ์—์„œ ๊ฐ€์žฅ ๊ธด ๊ฒฝ๋กœ์˜ ๋…ธ๋“œ ๊ฐœ์ˆ˜)

  • Sum of Nodes - ํŠธ๋ฆฌ์—์„œ์˜ ๋…ธ๋“œ๋“ค์˜ ํ•ฉ

  • Sum of Leaf Nodes - ์žŽ ๋…ธ๋“œ๋“ค์˜ ํ•ฉ

Shortest Path - ์ตœ๋‹จ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜

Sorting - ์ •๋ ฌ

  • Quick sort - ๋น ๋ฅธ์ •๋ ฌ, Worst case: O(n^2), Average case: O(nlogn) where n is the number of item in an array

  • Merge sort - ๋ณ‘ํ•ฉ์ •๋ ฌ O(nlogn), Worst case: O(nlogn) where n is the number of item in an array

  • Counting sort - ์นด์šดํŒ… ์†ŒํŠธ, ๊ณ„์ˆ˜์ •๋ ฌ, O(kn) where k is upper bound number, n is the # of items in an array

  • Selection sort - ์„ ํƒ์ •๋ ฌ Worst case: O(n^2)

  • Bubble sort - ๋ฒ„๋ธ”์†ŒํŠธ Worst case: O(n^2)

  • Insertion sort - ์‚ฝ์ž…์ •๋ ฌ Worst case: O(n^2)

  • Heap sort - ํž™ ์ •๋ ฌ Worst case: O(nlogn)

  • Radix sort - ๊ธฐ์ˆ˜ ์ •๋ ฌ, Time Complexity: O(nw) n = ์ •๋ ฌ๋  ํ‚ค์˜ ๊ฐœ์ˆ˜, w = ์ •๋ ฌ๋  ํ‚ค ์ค‘์—์„œ ๊ฐ€์žฅ ํฐ ์ž๋ฆฟ์ˆ˜

  • Bucket sort - ๋ฒ„ํ‚ท ์†ŒํŠธ

Topological Sort - ์œ„์ƒ์ •๋ ฌ

Dynamic Programming - ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ (์ž˜ ์•Œ๋ ค์ง„ ๋ฌธ์ œ๋“ค)

  • Fibonacci - Bottom up Manner - n๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด ๊ตฌํ•˜๊ธฐ(๋ฐ˜๋ณต์  ๋ฐฉ๋ฒ•), Iterative

  • Fibonacci - Top Down Manner - n๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด ๊ตฌํ•˜๊ธฐ(์žฌ๊ท€์  ๋ฐฉ๋ฒ•), Recursive + Memoization

  • Maximum Sum Sub array(Kadene's Algorithm) - ๋ฐฐ์—ด์—์„œ์˜ ์—ฐ์†๋œ ๋ถ€๋ถ„๋ฐฐ์—ด์˜ ์ตœ๋Œ€ ํ•ฉ, O(n)

  • Coin Change Problem 1 - ์ฃผ์–ด์ง„ ์ข…๋ฅ˜์˜ ์ฝ”์ธ์œผ๋กœ ํŠน์ • ๊ธˆ์•ก์„ ๋งŒ๋“œ๋Š”๋ฐ ๋“œ๋Š” ๊ฐ€๋Šฅํ•œ ์ตœ์†Œ์˜ ๋™์ „ ์ˆ˜ (๋™์ „์˜ ๊ฐœ์ˆ˜๋Š” ๋ฌดํ•œ)

  • Coin Change Problem 2 - ์ฃผ์–ด์ง„ ์ข…๋ฅ˜์˜ ์ฝ”์ธ์œผ๋กœ ํŠน์ • ๊ธˆ์•ก์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜(์กฐํ•ฉ์˜ ์ˆ˜) (๋™์ „์˜ ๊ฐœ์ˆ˜๋Š” ๋ฌดํ•œ)

  • 0 1 Knapsack - ๋ฌผ๊ฑด์˜ ์ˆ˜๋Ÿ‰์ด ์ตœ๋Œ€ 1๊ฐœ์ธ ๋ฐฐ๋‚ญ ๋ฌธ์ œ

  • Unbounded Knapsack - ๋ฌผ๊ฑด์˜ ์ˆ˜๋Ÿ‰์ด ์ œํ•œ ์—†๋Š” ๋ฐฐ๋‚ญ ๋ฌธ์ œ

  • LCS(Longest Common Subsequence) - ์ตœ์žฅ ๊ณตํ†ต ๋ถ€๋ถ„ ์ˆœ์„œ(Compare with 3 Strings), Bottom Up Manner O(n^3)

  • LCS(Longest Common Substring) - ์ตœ์žฅ ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด(Compare with 2 Strings), Bottom Up Manner O(n^2)

  • LIS(Longest Increasing Subsequence) - ์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„์ˆœ์„œ, Bottom Up Manner O(n^2)

  • Maximum Sub Square Matrix of 1 - 1๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜• ๋ถ€๋ถ„ ํ–‰๋ ฌ ๊ตฌํ•˜๊ธฐ

  • ๊ฒฉ์ž ๊ฒฝ๋กœ ๋ฌธ์ œ

  • Stair Case Problem N๋ฒˆ ๊ณ„๋‹จ ๊นŒ์ง€ ์˜ฌ๋ผ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜

  • Tiling Dominoes - ํƒ€์ผ๋ง ๋ฌธ์ œ, e.g 2 x N or 3 x N and so on

  • Edit Distance - ํŽธ์ง‘๊ฑฐ๋ฆฌ

  • Longest Palindrome Subsequence - ์ตœ์žฅ ํŒฐ๋ฆฐ๋“œ๋กฌ ๋ถ€๋ถ„์ˆœ์„œ

  • Josephus Problem - ์กฐ์„ธํผ์Šค ๋ฌธ์ œ

Dynamic Programming - ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ (์ถ”๊ฐ€)

Prime - ์†Œ์ˆ˜

GCD, LCM - ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜, ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜

  • GCD - ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜

  • LCM - ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜

Permutation & Combination - ์ˆœ์—ด๊ณผ ์กฐํ•ฉ

Minimum Spanning Tree Algorithm - ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • Kruskal's Algorithm - ๊ฐ€๋Šฅํ•œ ๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ„์„ ์œผ๋กœ ์‹œ์ž‘ํ•ด N-1๊ฐœ์˜ ๊ฐ„์„ ์„ ์„ ํƒํ•˜๋Š” Greedy Algorithm

  • Prim's Algorithm

Network Flow - ๋„คํŠธ์›Œํฌ ์œ ๋Ÿ‰

  • Ford-Fulkerson method - ์ตœ๋Œ€์œ ๋Ÿ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • Edmonds Karp Algorithm - ์ตœ๋Œ€์œ ๋Ÿ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜, Ford Fulkerson method์—์„œ์˜ ํƒ์ƒ‰๋ฐฉ๋ฒ•์„ BFS๋กœ ์ ์šฉ

  • Min Cost Max Flow - ์ตœ์†Œ๋น„์šฉ ์ตœ๋Œ€์œ ๋Ÿ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ตœ์†Œ๋น„์šฉ์—๋Š” SPFA Algorithm, ์ตœ๋Œ€๋น„์šฉ์—๋Š” Edmonds Karp Algorithm ์ ์šฉ

Bipartite Matching - ์ด๋ถ„๋งค์นญ

Strongly Connected Component - ๊ฐ•ํ•œ ์—ฐ๊ฒฐ ์š”์†Œ

Cut Edge and Articulation Point - ๋‹จ์ ˆ์„ ๊ณผ ๋‹จ์ ˆ์ 

Cycle Detection Algorithm in a graph - ๊ทธ๋ž˜ํ”„์—์„œ์˜ ์‚ฌ์ดํด ํƒ์ง€

  • Cycle Detection in a Directed Graph - ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ์˜ ์‚ฌ์ดํด ํƒ์ง€, DFS ์‚ฌ์šฉ - O(V+E)

  • Cycle Detection in an UnDirected Graph - ๋น„๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ์˜ ์‚ฌ์ดํด ํƒ์ง€, ์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ ์‚ฌ์šฉ - O(V+E)

LCA - ์ตœ์†Œ ๊ณตํ†ต ์กฐ์ƒ

String Pattern Matching - ๋ฌธ์ž์—ด ํŒจํ„ด ๋งค์นญ

  • KMP Pattern Matching Algorithm - KMP(Knuth, Morris, Pratt) ํŒจํ„ด ๋งค์นญ ์•Œ๊ณ ๋ฆฌ์ฆ˜, O(n+m) where n is pattern matching and m is LPS construction (LPS : Longest Proper Prefix which is also Suffix)

  • Boyer Moore Algorithm - Using Bad Character Rule

  • Rabin Karp Algorithm

Other Algorithm

  • Count Inversions In An Array - ์ธ๋ฑ์Šค ์œ„์น˜๊ฐ€ i < j ์ด๋ฉด์„œ A[i] > A[j]์ธ ์›์†Œ๋“ค์„ ์—ญ์ „๊ด€๊ณ„๋ผ๊ณ  ํ•œ๋‹ค. ์—ญ์ „๊ด€๊ณ„์˜ ์›์†Œ๋“ค์ด ํ•ด๋‹น ๋ฐฐ์—ด์•ˆ์— ๋ช‡๊ฐœ๋‚˜ ์žˆ๋Š”์ง€ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜. ๋จธ์ง€์†ŒํŠธ๋ฅผ ์ด์šฉํ•œ๋‹ค. O(nlogn), Naive Approach : O(n^2)

Data structure

In computer science, a data structure is a data organization, management, and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data. - Wikipedia

Queue - ํ

Stack - ์Šคํƒ

Linked List - ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

Graph - ๊ทธ๋ž˜ํ”„

Set - ์ง‘ํ•ฉ (์ค‘๋ณต๋œ ์›์†Œ๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ์ž๋ฃŒ๊ตฌ์กฐ)

Priority Queue / Heap - ์šฐ์„ ์ˆœ์œ„ ํ / ํž™

Indexed Priority Queue / Indexed Heap - ์ธ๋ฑ์Šค ์šฐ์„ ์ˆœ์œ„ ํ / ์ธ๋ฑ์Šค ํž™

  • [Implementation]

Dynamic Array - ๋™์  ๋ฐฐ์—ด

Disjoint Set - Union Find by Rank with Path Compression - ์„œ๋กœ์†Œ ์ง‘ํ•ฉ

Tree - ํŠธ๋ฆฌ

Binary Search Tree - ์ด์ง„ํƒ์ƒ‰ํŠธ๋ฆฌ

Segment Tree - ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ

Fenwick Tree or Binary Indexed Tree - ํŽœ์œ…ํŠธ๋ฆฌ

  • Fenwick Tree - Range Sum Query - Construction : O(n), Update: O(logn), Sum Query : O(logn)

Sparse Table

Trie or Prefix Tree - ํŠธ๋ผ์ด, ์ ‘๋‘์‚ฌ ํŠธ๋ฆฌ

Bit Manipulation - ๋น„ํŠธ ์กฐ์ž‘

Releases

No releases published

Packages

No packages published

Languages