Dijkstra Algorithm
1. ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ด๋?
ํต์ฌ ๊ฐ๋
:
2. ๋์ ๋ฐฉ์(์ฐ์ ์์ ํ ์ฌ์ฉ ๋ฒ์ )
3. ์๋ฐ ๊ตฌํ (์ฐ์ ์์ ํ ๊ธฐ๋ฐ)
Class Edge implements Comparable<Edge> {
int to, weight;
Edge(int to, int weight) {
this.to = to;
this.weight = weight;
}
public int compareTo(Edge other) {
return this.weight - other.weight;
}
}
public void dijkstra(int start, List<List<Edge>> graph, int[] dist) {
int n = graph.size();
boolean[] visited = new boolean[n];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[start] = 0;
PriorityQueue<Edge> pq = new PriorityQueue<>();
pq.offer(new Edge(start, 0));
while(!pq.isEmpty()) {
Edge current = pq.poll();
int curNode = current.to;
if(visited[curNode]) continue;
visited[curNode] = true;
for(Edge e : graph.get(curNode)) {
int next = e.to;
int cost = dist[curNode] + e.weight;
if(cost < dist[next]) {
dist[next] = cost;
pq.offer(new Edge(next, cost));
}
}
}
}๊ทธ๋ํ ์ค๋น(์ธ์ ๋ฆฌ์คํธ)
4. ์๊ฐ ๋ณต์ก๋
๋ฒ์
์๊ฐ๋ณต์ก๋
์ค๋ช
5. ์ฌ์ฉ ์กฐ๊ฑด ์ ๋ฆฌ
์กฐ๊ฑด
๊ฐ๋ฅ ์ฌ๋ถ
6. ์์ - ๊ฐ๋จํ ์
๋ ฅ ์
๋ฌธ์
์ถ๋ ฅ(1์์ ๊ฐ ์ ์ ๊น์ง ์ต๋จ ๊ฑฐ๋ฆฌ)
7. Dijkstra ๋ก ์์ฃผ ๋์ค๋ ๋ฌธ์ ์ ํ
์ ํ
์ค๋ช
8. ๊ด๋ จ ์๊ณ ๋ฆฌ์ฆ
์๊ณ ๋ฆฌ์ฆ
์ฐจ์ด์
์์ฝ
Last updated