zerohertzLib.algorithm ¶
Algorithm
Algorithm과 관련된 다양한 함수들
Modules:
| Name | Description |
|---|---|
bisect | |
collections | |
fft | |
graph | |
prime | |
sort | |
Classes:
| Name | Description |
|---|---|
DisjointSet | Vanilla disjoint set |
DisjointSetRank | Disjoint set (union by rank) |
DisjointSetSize | Disjoint set (union by size) |
Functions:
| Name | Description |
|---|---|
bellman_ford | Graph에서 시작 node로부터 모든 다른 node까지의 최단 경로 거리 계산 |
bfs | BFS를 수행하기 위한 function |
bisect_left | Binary Search (left) |
bisect_right | Binary Search (right) |
bubble_sort | Bubble Sort Algorithm: 연속된 값들을 비교하여 가장 큰 값을 배열의 끝으로 이동시키는 방식으로 정렬 |
counting_sort | Counting Sort Algorithm: 각 숫자의 개수를 세어 정렬 |
dfs | DFS를 수행하기 위한 function |
dijkstra | Graph에서 시작 node로부터 모든 다른 node까지의 최단 경로 거리 계산 |
floyd_warshall | Graph에서 모든 node 쌍 간의 최단 경로 거리 계산 |
heap_sort | Heap Sort Algorithm: 배열 요소들을 heap으로 구성한 다음, 최대 heap 속성을 이용하여 정렬 |
insertion_sort | Insertion Sort Algorithm: 각 값들을 이미 정렬된 부분의 올바른 위치에 삽입하는 방식으로 정렬 |
merge_sort | Merge Sort Algorithm: 분할 정복 방법을 사용하여 배열을 절반으로 나누고, 각 부분을 정렬한 다음 합치는 방식으로 정렬 |
quick_sort | Quick Sort Algorithm: Pivot을 선택하여 이보다 작은 요소는 왼쪽, 큰 요소는 오른쪽에 위치시키는 방식으로 분할 정복을 사용하여 정렬 |
radix_sort | Radix Sort Algorithm: 각 자릿수에 대해 개별적으로 정렬 |
selection_sort | Selection Sort Algorithm: 배열에서 가장 작은 값을 찾아 해당 값을 배열의 앞부분으로 이동시키는 방식으로 정렬 |
soe | Sieve of Eratosthenes |
__all__ module-attribute ¶
__all__ = ['bfs', 'dfs', 'soe', 'fft', 'bisect_right', 'bisect_left', 'bubble_sort', 'selection_sort', 'insertion_sort', 'merge_sort', 'quick_sort', 'heap_sort', 'counting_sort', 'radix_sort', 'dijkstra', 'floyd_warshall', 'bellman_ford', 'DisjointSet', 'DisjointSetRank', 'DisjointSetSize']
DisjointSet ¶
Vanilla disjoint set
Note
Time Complexity:
- Without path compression:
- Worst: \(O(V)\)
- Average: \(O(V)\)
-
With path compression:
- Worst: \(O(\log{V})\)
- Average: \(O(\alpha(V))\)
-
\(V\): Node의 수
- \(\alpha(V)\): Ackermann function 의 역함수 (\(O(\alpha(V))\simeq O(1)\))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size | int | Node의 수 | required |
compression | bool | Path compression 여부 | False |
Attributes:
| Name | Type | Description |
|---|---|---|
parent | Node에 따른 부모 node의 index |
Examples:
>>> disjointset = zz.algorithm.DisjointSet(5)
>>> disjointset.union(0, 1)
>>> disjointset.union(2, 3)
>>> disjointset.union(1, 2)
>>> disjointset.parent
[0, 0, 0, 2, 4]
>>> disjointset = zz.algorithm.DisjointSet(5, True)
>>> disjointset.union(0, 1)
>>> disjointset.union(2, 3)
>>> disjointset.union(1, 2)
>>> disjointset.parent
[0, 0, 0, 2, 4]
Methods:
| Name | Description |
|---|---|
find | 목표 node에 대한 root node 탐색 |
union | 두 node 연결 |
Source code in zerohertzLib/algorithm/collections.py
find ¶
목표 node에 대한 root node 탐색
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node | int | 목표 node의 index | required |
Returns:
| Type | Description |
|---|---|
int | 목표 node에 대한 root node의 index |
Source code in zerohertzLib/algorithm/collections.py
DisjointSetRank ¶
DisjointSetRank(size: int)
Bases: DisjointSet
Disjoint set (union by rank)
Note
Time Complexity:
- Worst: \(O(\log{V})\)
-
Average: \(O(\alpha(V))\)
-
\(V\): Node의 수
- \(\alpha(V)\): Ackermann function 의 역함수 (\(O(\alpha(V))\simeq O(1)\))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size | int | Node의 수 | required |
Attributes:
| Name | Type | Description |
|---|---|---|
parent | Node에 따른 부모 node의 index | |
rank | Node에 따른 rank |
Examples:
>>> disjointset = zz.algorithm.DisjointSetRank(5)
>>> disjointset.union(0, 1)
>>> disjointset.union(2, 3)
>>> disjointset.union(1, 2)
>>> disjointset.parent
[0, 0, 0, 2, 4]
>>> disjointset.rank
[2, 0, 1, 0, 0]
Methods:
| Name | Description |
|---|---|
union | 두 node 연결 |
Source code in zerohertzLib/algorithm/collections.py
union ¶
두 node 연결
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node1 | int | 목표 node의 index | required |
node2 | int | 목표 node의 index | required |
Returns:
| Type | Description |
|---|---|
None |
|
Source code in zerohertzLib/algorithm/collections.py
DisjointSetSize ¶
DisjointSetSize(size: int)
Bases: DisjointSet
Disjoint set (union by size)
Note
Time Complexity:
- Worst: \(O(\log{V})\)
-
Average: \(O(\alpha(V))\)
-
\(V\): Node의 수
- \(\alpha(V)\): Ackermann function 의 역함수 (\(O(\alpha(V))\simeq O(1)\))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size | int | Node의 수 | required |
Attributes:
| Name | Type | Description |
|---|---|---|
parent | Node에 따른 부모 node의 index | |
size | Node에 따른 size |
Examples:
>>> disjointset = zz.algorithm.DisjointSetSize(5)
>>> disjointset.union(0, 1)
>>> disjointset.union(2, 3)
>>> disjointset.union(1, 2)
>>> disjointset.parent
[0, 0, 0, 2, 4]
>>> disjointset.size
[4, 1, 2, 1, 1]
>>> [disjointset.size[disjointset.find(i)] for i in range(5)]
[4, 4, 4, 4, 1]
Methods:
| Name | Description |
|---|---|
union | 두 node 연결 |
Source code in zerohertzLib/algorithm/collections.py
union ¶
두 node 연결
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node1 | int | 목표 node의 index | required |
node2 | int | 목표 node의 index | required |
Returns:
| Type | Description |
|---|---|
None |
|
Source code in zerohertzLib/algorithm/collections.py
bellman_ford ¶
Graph에서 시작 node로부터 모든 다른 node까지의 최단 경로 거리 계산
Note
Time Complexity: \(O(VE)\)
- \(V\): Node의 수
- \(E\): 간선의 수
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph | list[list[tuple[int, int]]] | Index (간선의 시작 node)에 따른 간선의 도착 node와 가중치 정보 | required |
start | int | 최단 경로 거리가 계신될 시작 node | required |
Returns:
| Type | Description |
|---|---|
list[int] | None |
|
Examples:
>>> graph = [[(1, 4), (2, 2), (3, 7)], [(0, 1), (2, 5)], [(0, 2), (3, 4)], [(1, 3)]]
>>> zz.algorithm.bellman_ford(graph, 0)
[0, 4, 2, 6]
>>> zz.algorithm.bellman_ford(graph, 1)
[1, 0, 3, 7]
>>> zz.algorithm.bellman_ford(graph, 2)
[2, 6, 0, 4]
>>> zz.algorithm.bellman_ford(graph, 3)
[4, 3, 6, 0]
>>> zz.algorithm.bellman_ford([[(1, -1)], [(0, -1)]], 0) is None
True
Source code in zerohertzLib/algorithm/graph.py
bfs ¶
BFS를 수행하기 위한 function
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
maps | list[list[int]] | 입력 graph | required |
start | int | Graph의 시작 지점 | required |
Returns:
| Type | Description |
|---|---|
list[int] | 방문 순서 |
Examples:
Source code in zerohertzLib/algorithm/graph.py
bisect_left ¶
Binary Search (left)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sorted_list | list[int | float] | 정렬된 list | required |
value | int | float | 찾고자하는 값 | required |
Returns:
| Type | Description |
|---|---|
int |
|
Examples:
>>> zz.algorithm.bisect_left([1, 3, 5], 2.7)
1
>>> zz.algorithm.bisect_left([1, 3, 5], 3)
1
>>> zz.algorithm.bisect_left([1, 3, 5], 3.3)
2
Source code in zerohertzLib/algorithm/bisect.py
bisect_right ¶
Binary Search (right)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sorted_list | list[int | float] | 정렬된 list | required |
value | int | float | 찾고자하는 값 | required |
Returns:
| Type | Description |
|---|---|
int |
|
Examples:
>>> zz.algorithm.bisect_right([1, 3, 5], 2.7)
1
>>> zz.algorithm.bisect_right([1, 3, 5], 3)
2
>>> zz.algorithm.bisect_right([1, 3, 5], 3.3)
2
Source code in zerohertzLib/algorithm/bisect.py
bubble_sort ¶
Bubble Sort Algorithm: 연속된 값들을 비교하여 가장 큰 값을 배열의 끝으로 이동시키는 방식으로 정렬
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr | list[int] | 정렬할 정수 list | required |
Returns:
| Type | Description |
|---|---|
list[int] | 오름차순으로 정렬된 list |
Examples:
Source code in zerohertzLib/algorithm/sort.py
counting_sort ¶
Counting Sort Algorithm: 각 숫자의 개수를 세어 정렬
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr | list[int] | 정렬할 정수 list | required |
Returns:
| Type | Description |
|---|---|
list[int] | 오름차순으로 정렬된 list |
Examples:
Source code in zerohertzLib/algorithm/sort.py
dfs ¶
DFS를 수행하기 위한 function
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
maps | list[list[int]] | 입력 graph | required |
start | int | Graph의 시작 지점 | required |
Returns:
| Type | Description |
|---|---|
list[int] | 방문 순서 |
Examples:
Source code in zerohertzLib/algorithm/graph.py
dijkstra ¶
Graph에서 시작 node로부터 모든 다른 node까지의 최단 경로 거리 계산
Note
Time Complexity: \(O((V+E)\log{V})\)
- \(V\): Node의 수
- \(E\): 간선의 수
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph | list[list[tuple[int, int]]] | Index (간선의 시작 node)에 따른 간선의 도착 node와 가중치 정보 | required |
start | int | 최단 경로 거리가 계신될 시작 node | required |
Returns:
| Type | Description |
|---|---|
list[int] |
|
Examples:
>>> graph = [[(1, 4), (2, 2), (3, 7)], [(0, 1), (2, 5)], [(0, 2), (3, 4)], [(1, 3)]]
>>> zz.algorithm.dijkstra(graph, 0)
[0, 4, 2, 6]
>>> zz.algorithm.dijkstra(graph, 1)
[1, 0, 3, 7]
>>> zz.algorithm.dijkstra(graph, 2)
[2, 6, 0, 4]
>>> zz.algorithm.dijkstra(graph, 3)
[4, 3, 6, 0]
Source code in zerohertzLib/algorithm/graph.py
floyd_warshall ¶
Graph에서 모든 node 쌍 간의 최단 경로 거리 계산
Note
Time Complexity: \(O(V^3)\)
- \(V\): Node의 수
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph | list[list[tuple[int, int]]] | Index (간선의 시작 node)에 따른 간선의 도착 node와 가중치 정보 | required |
Returns:
| Type | Description |
|---|---|
list[list[int]] | None | 모든 node 쌍에 대한 최단 경로 거리 (음의 cycle을 가질 시 None return) |
Examples:
>>> graph = [[(1, 4), (2, 2), (3, 7)], [(0, 1), (2, 5)], [(0, 2), (3, 4)], [(1, 3)]]
>>> zz.algorithm.floyd_warshall(graph)
[[0, 4, 2, 6], [1, 0, 3, 7], [2, 6, 0, 4], [4, 3, 6, 0]]
Source code in zerohertzLib/algorithm/graph.py
heap_sort ¶
Heap Sort Algorithm: 배열 요소들을 heap으로 구성한 다음, 최대 heap 속성을 이용하여 정렬
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr | list[int] | 정렬할 정수 list | required |
Returns:
| Type | Description |
|---|---|
list[int] | 오름차순으로 정렬된 list |
Examples:
Source code in zerohertzLib/algorithm/sort.py
insertion_sort ¶
Insertion Sort Algorithm: 각 값들을 이미 정렬된 부분의 올바른 위치에 삽입하는 방식으로 정렬
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr | list[int] | 정렬할 정수 list | required |
Returns:
| Type | Description |
|---|---|
list[int] | 오름차순으로 정렬된 list |
Examples:
Source code in zerohertzLib/algorithm/sort.py
merge_sort ¶
Merge Sort Algorithm: 분할 정복 방법을 사용하여 배열을 절반으로 나누고, 각 부분을 정렬한 다음 합치는 방식으로 정렬
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr | list[int] | 정렬할 정수 list | required |
Returns:
| Type | Description |
|---|---|
list[int] | 오름차순으로 정렬된 list |
Examples:
Source code in zerohertzLib/algorithm/sort.py
quick_sort ¶
Quick Sort Algorithm: Pivot을 선택하여 이보다 작은 요소는 왼쪽, 큰 요소는 오른쪽에 위치시키는 방식으로 분할 정복을 사용하여 정렬
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr | list[int] | 정렬할 정수 list | required |
Returns:
| Type | Description |
|---|---|
list[int] | 오름차순으로 정렬된 list |
Examples:
Source code in zerohertzLib/algorithm/sort.py
radix_sort ¶
Radix Sort Algorithm: 각 자릿수에 대해 개별적으로 정렬
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr | list[int] | 정렬할 정수 list | required |
Returns:
| Type | Description |
|---|---|
list[int] | 오름차순으로 정렬된 list |
Examples:
Source code in zerohertzLib/algorithm/sort.py
selection_sort ¶
Selection Sort Algorithm: 배열에서 가장 작은 값을 찾아 해당 값을 배열의 앞부분으로 이동시키는 방식으로 정렬
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr | list[int] | 정렬할 정수 list | required |
Returns:
| Type | Description |
|---|---|
list[int] | 오름차순으로 정렬된 list |
Examples:
Source code in zerohertzLib/algorithm/sort.py
soe ¶
Sieve of Eratosthenes
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_max | int | 구하고자 하는 소수 범위의 최댓값 | required |
Returns:
| Type | Description |
|---|---|
list[int] | N까지 존재하는 소수 list |
Examples: