zerohertzLib.algorithm.graph ¶
Functions:
| Name | Description |
|---|---|
bellman_ford | Graph에서 시작 node로부터 모든 다른 node까지의 최단 경로 거리 계산 |
bfs | BFS를 수행하기 위한 function |
dfs | DFS를 수행하기 위한 function |
dijkstra | Graph에서 시작 node로부터 모든 다른 node까지의 최단 경로 거리 계산 |
floyd_warshall | Graph에서 모든 node 쌍 간의 최단 경로 거리 계산 |
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
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]]