참조: https://www.youtube.com/watch?v=LFMrrTqqt4M&t=154
참조: http://contents2.kocw.or.kr/KOCW/document/2015/pukyong/kwonoeum/6.pdf
참조: https://code-lab1.tistory.com/13
그래프(Graph): 정점과 간선으로 이루어진 구조를 나타내는 수학적인 개념 (G = (V, E))
단순 그래프(Simple Graph): '루프'와 '다중 간선'이 없는 그래프
- 이 그래프에서는 4개의 정점과 4개의 간선으로 이루어진 그래프이다.
- V = V(G) = {v1, v2, v3, v4}
- E = E(G) = {(v1, v2), (v2, v1), (v1, v3), (v3, v1), (v2, v3), (v3, v2), (v3, v4), (v4, v3)}
해당 표기는 엣지에 방향이 있는 그래프가 좀 더 적합하다.
무방향 그래프(Nondirected Graph): 간선을 통해서 양 방향으로 이동할 수 있는 그래프
참조: https://junstar92.tistory.com/196
방향 그래프(Directed Graph): 간선의 방향성이 존재하는 그래프
참조: https://junstar92.tistory.com/196
완전 그래프(Complete Graph): 모든 정점이 서로 연결되어 있는 그래프
다중 그래프(Multi Graph): '루프' 및 '다중 간선'이 존재하는 그래프
연결 그래프(Connected Graph): 무방향 그래프에서 모든 정점이 직, 간접적으로 연결되어 있는 그래프
가중치 그래프(Weighted Graph): 간선에 숫자로 된 가중치를 각 간선에 부여하는 그래프
참조: https://junstar92.tistory.com/198
인접 행렬(Adjacency Matrix): 2차원 배열을 사용하여 그래프를 표현하며 모든 정보를 저장한다.
한 행의 원소를 모두 더하면 out-degree(한 정점에서 나가는 엣지)가 된다.
한 열의 원소를 모두 더하면 in-degree(한 정점으로 들어오는 엣지)가 된다.
인접 행렬을 제곱하면 해당 그래프의 경로 길이에 대한 정보를 얻을 수 있다.
대각선에 대해서 대칭 행렬이 되며, 특히 단순 그래프의 인접 행렬은 Binary Matrix가 된다.
import sys
input = sys.stdin.readline
vertex, edge = map(int, input().split())
adj = [[0 for _ in range(vertex)] for _ in range(vertex)]
for _ in range(edge):
src, dest = map(int, input().split())
adj[src][dest] = 1
adj[dest][src] = 1
인접 리스트(Adjacency List): 연결 리스트들의 배열로 표현하며 갈 수 있는 곳만 정보를 저장한다.
import sys
input = sys.stdin.readline
vertex, edge = map(int, input().split())
adj = [ [] for _ in range(vertex)]
for _ in range(edge):
src, dest = map(int, input().split())
adj[src].append(dest)
adj[dest].append(src)