두개의 노드 사이에서 가장 짧은 경로를 찾는 알고리즘
단일 노드 ~ 단일 노드에 대한 최단 거리
모든 노드 쌍에 대한 최단 거리
static class Node implements Comparable<Node> {
// 노드 번호, 해당 노드까지의 가중치
int idx, dist;
public Node(int idx, int dist) {
this.idx = idx;
this.dist = dist;
}
@Override
public int compareTo(Node o) {
return Integer.compare(this.dist, o.dist);
}
}
// 각 정점에서 이동할 수 있는 정점에 대한 가중치를 담은 2차원 배열
static int[][] graph;
public static int[] dijkstra(int from) {
PriorityQueue<Node> pq = new PriorityQueue<Node>();
int[] dist = new int[N + 1];
boolean[] visited = new boolean[N + 1];
// 나머지 정점은 최대값으로 초기화
Arrays.fill(dist, INF);
// 시작 정점인 자기 자신에 대한 거리는 0
dist[from] = 0;
pq.add(new Node(from, dist[from]));
// 우선순위 큐를 사용하여 아직 선택되지 않은 노드 중 최단 거리가 가장 짧은 노드 선택
while (!pq.isEmpty()) {
int here = pq.peek().idx;
int cost = pq.peek().dist;
pq.poll();
if (cost > dist[here])
continue;
for (int i = 1; i <= N; i++) {
// 방문할 수 있는 노드이고, 아직 방문 전이라면
if (graph[here][i] != INF && !visited[i]) {
// (이미 계산되어있는)다음 노드로 갈 수 있는 이동 값이
// 현재 위치한 노드의 경로 + 다음 노드로의 dist 보다 더 크다면 갱신
// next로 가는 최소 값이 (지금 있는 노드 + next로 가는 가중치)보다 크면 최솟값으로 갱신
if (dist[i] > (dist[here] + graph[here][i])) {
dist[i] = dist[here] + graph[here][i];
pq.add(new Node(i, dist[i]));
}
}
}
visited[here] = true;
}
return dist;
}
false
를 반환해주고 아니라면 true
를 반환static class Node {
// 출발노드 / 끝노드 / 간선 가중치
int end, cost;
public Node(int end, int cost) {
this.end = end;
this.cost = cost;
}
}
static int INF = 987654321;
static ArrayList<Node>[] map;
public static void main(String[] args) throws IOException {
// 전체 노드의 개수
int N;
// 간선 정보의 개수
int M;
// 그래프 정보
map = new ArrayList<Node>[];
for (int i = 0; i <= N; i++) {
map[i] = new ArrayList<Node>();
}
for (int i = 0; i < M; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int start = Integer.valueOf(st.nextToken());
int end = Integer.valueOf(st.nextToken());
int cost = Integer.valueOf(st.nextToken());
// 양방향 그래프 초기화
map[start].add(new Node(end, cost));
map[end].add(new Node(start, cost));
}
}
static int[] bellmanFord(int start, int N) {
int[] dist = new int[N+1];
Arrays.fill(dist, INF);
// 그래프 시작점 0으로 초기화
dist[start] = 0;
// "전체 노드 개수 - 1" 만큼 대로 최단거리 초기화 반복
for (int i = 1; i < N; i++) {
// 최단거리 초기화
// 간선의 개수대로 모두 반복
for (int j = 1; j <= N; j++) {
for (Node node : map.get(j)) {
if (dist[j] == INF)
continue;
if (dist[road.end] > dist[j] + node.cost)
dist[road.end] = dist[j] + node.cost;
}
}
}
return dist;
}
int INF = 987654321;
// 최소 비용 담은 배열
int[][] dist = new int[][];
// 1) 배열 초기화
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i = j)
dist[i][j] = 0;
else
dist[i][j] = INF;
}
}
// 2) 그래프 연결 정보 입력
for (int[] info : list) {
int start = info[0];
int end = info[1];
int dist = info[2];
dist[start][end] = dist;
}
// 3) 플로이드 와샬
// 경유 노드
for (int k = 0; k < n; k++) {
// 시작 노드
for (int i = 0; i < n; i++) {
// 도착 노드
for (int j = 0; j < n; j++) {
dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}