조합
from itertools import combinations ( fiic )
arr = ['A', 'B', 'C', 'D']
- list(combinations(arr, 2))
- #['AB', 'AC', 'AD', 'BC', 'BD', 'CD']
순열
from itertools import permutations
- list(permutations(arr, 2))
- #['AB', 'AC', 'AD', 'BA', 'BC', 'BD', 'CA', 'CB', 'CD', 'DA', 'DB', 'DC']
디렉토리
a = {}
- 키
→ list(a.keys())
- 값
→ list(a.values())
- 정렬
→ a = sorted(a.items(), key=lambda x : x[1])
정렬 2개 기준
array.sort(key=lambda x : (x[0].lower(), (int(x[1]))))
ord chr
- x = ord('a') # 97
- y = chr(97) # a
깊은복사
import copy
temp = copy.deepcopy(abc)
신장트리(크루스칼 알고리즘)
- 모든 노드를 포함하면서
- 사이클이 존재하지 않는 부분그래프
find_parent(parent, x)
union_parent(parent, a, b)
위상정렬(각 차수 계산), Deque
- 순서가 있는 정렬
indegree = [0]*(n+1) #차수 계산
deque (from collections import deque) # 차수가 0인것. 큐에삽입
최단경로
1. 다익스트라 A→Z (한곳에서 다른한곳) [홍ghd]
1. heapq (가장 짧은 노드 꺼내기)
- q = []
- heapq.heappush(q, (0, start)) # A
- heapq.heappop(q)
2. graph
- [[], [(2, 2), (3, 5), (4, 1)], [(3, 3), (4, 2)], [(2, 3), (6, 5)], [(3, 3), (5, 1)], [(3, 1), (6, 2)], []]
3. distance
- 무한으로 초기화
플로이드 워셜 (모든 지점)
for k in range(1, n+1):
for a in range(1, n+1):
for b in range(1, n+1):
graph[a][b] = min(graph[a][b], graph[a][k]+graph[k][b])
BFS (Deque)
- 너비우선탐색 / 가까운 노드부터
- 최단경로, 수수께끼
2 3 8 → 2 popleft → 3 8 1 7 (1, 7추가됨 2번째의)
DFS (재귀)
- 나눠진 구역
댓글