출발점에서 목표점까지의 최단 경로를 구하는 알고리즘
A노드에서 출발해 F노드로 가는 최단 경로를 구한다.
이 때, 현재 노드에서 인접한 노드의 거리가 가장 가까운 순으로 방문한다.
distance 는 A -> N 까지 계산된 최단 거리이다.
visited 로 방문체크를 한다.
확인되지 않은 거리는 전 부 무한으로 잡는다(INF)
이미지 출처 : 제로베이스
반복문 - O(V²)
static class Node {
int to;
int weight;
public Node(int to, int weight) {
this.to = to;
this.weight = weight;
}
}
static void dijkstra(int v, int[][] data, int start) {
ArrayList<ArrayList<Node>> graph = new ArrayList<>();
for (int i = 0; i <= v; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < data.length; i++) {
graph.get(data[i][0]).add(new Node(data[i][1], data[i][2]));
}
int[] dist = new int[v + 1];
for (int i = 1; i <= v; i++) {
dist[i] = Integer.MAX_VALUE;
}
dist[start] = 0;
boolean[] visited = new boolean[v + 1];
for (int i = 0; i < v; i++) {
int minDis = Integer.MAX_VALUE;
int curIdx = 0;
for (int j = 1; j <= v; j++) {
if (!visited[j] && dist[j] < minDis) {
minDis = dist[j];
curIdx = j;
}
}
visited[curIdx] = true;
for (int j = 0; j < graph.get(curIdx).size(); j++) {
Node adjNode = graph.get(curIdx).get(j);
if (dist[adjNode.to] > dist[curIdx] + adjNode.weight) {
dist[adjNode.to] = dist[curIdx] + adjNode.weight;
}
}
}
for (int i = 1; i <= v; i++) {
if (dist[i] == Integer.MAX_VALUE) System.out.print("INF ");
else System.out.print(dist[i]+" ");
}
System.out.println();
}
우선순위 큐 - O(ElogV)
static class Node {
int to;
int weight;
public Node(int to, int weight) {
this.to = to;
this.weight = weight;
}
}
static void dijkstra(int v, int[][] data, int start) {
ArrayList<ArrayList<Node>> graph = new ArrayList<>();
for (int i = 0; i <= v; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < data.length; i++) {
graph.get(data[i][0]).add(new Node(data[i][1], data[i][2]));
}
int[] dist = new int[v + 1];
for (int i = 1; i <= v; i++) {
dist[i] = Integer.MAX_VALUE;
}
dist[start] = 0;
PriorityQueue<Node> pq = new PriorityQueue<>((x, y) -> x.weight - y.weight);
pq.offer(new Node(start, 0));
while (!pq.isEmpty()) {
Node cur = pq.poll();
if (dist[cur.to] < cur.weight) {
continue;
}
for (int i = 0; i < graph.get(cur.to).size(); i++) {
Node adj = graph.get(cur.to).get(i);
if (dist[adj.to] > cur.weight + adj.weight) {
dist[adj.to] = cur.weight + adj.weight;
pq.offer(new Node(adj.to, dist[adj.to]));
}
}
}
for (int i = 1; i <= v; i++) {
if (dist[i] == Integer.MAX_VALUE) System.out.print("INF ");
else System.out.print(dist[i]+" ");
}
System.out.println();
}