flowchart TD
A[Build dist matrix] --> B[Set diagonal 0]
B --> C[Load direct edges]
C --> D[for k in vertices]
D --> E[for i in vertices]
E --> F[for j in vertices]
F --> G{dist i k dist k j dist i j}
G -- Yes --> H[Update dist cell]
G -- No --> I[No change]
H --> F
I --> F
F --> J{j done}
J -- Yes --> K{i done}
K -- Yes --> L{k done}
L -- Yes --> M[All pairs shortest paths]
sequenceDiagram
participant K as via k
participant I as source i
participant J as target j
participant D as distMatrix
K->>I: choose intermediary
I->>J: candidate path through k
J->>D: compare current vs candidate
D-->>J: min value stored
# [Floyd-Warshall 템플릿: 아키텍트 버전]# Use Case: 모든 쌍 최단 경로# Components: 2D Distance Matrix# Constraint: O(V³) 시간 복잡도, 정점 수 적을 때만 사용deffloyd_warshall(graph,n):# 1. 초기화 (Initialization Layer)# - 2차원 거리 행렬 생성INF=float('inf')dist=[[INF]*(n+1)for_inrange(n+1)]# 자기 자신으로 가는 거리 0foriinrange(1,n+1):dist[i][i]=0# 초기 간선 정보 입력foru,v,weightingraph:dist[u][v]=weight# 2. 3중 루프 (Triple Loop)# - k: 경유 노드# - i: 출발 노드# - j: 도착 노드forkinrange(1,n+1):# 경유점foriinrange(1,n+1):# 출발forjinrange(1,n+1):# 도착# 3. 갱신 로직 (Update Logic)# - k를 거쳐가는 것이 더 짧은지 확인ifdist[i][k]+dist[k][j]<dist[i][j]:dist[i][j]=dist[i][k]+dist[k][j]returndist