distance
,visited
와, Previous
를 따로 만들어 지속적으로 확인해야한다.distacne
라는 객체를 만들고, 인접리스트의 key값(vertex들)을 이용한다.infinity
를 가진다.infinity
를 가진다.previous
라는 객체를 만들고, 각 vertex를 추가한다.null
의 값을 가진다.null
로 만드는 것priority queue
를 기준으로 루프가 실행된다.priority queue
에서 dequeue
한다.dequeue
되는 vertex가 ending vertex
라면 종료한다.starting vertex
와 dequeue
되는 vertex의 distance를 구한다.enqueue
한다.class PriorityQueue {
constructor(){
this.values = [];
}
enqueue(val, priority) {
this.values.push({val, priority});
this.sort();
};
dequeue() {
return this.values.shift();
};
sort() {
this.values.sort((a, b) => a.priority - b.priority);
};
}
// 간단한 우선순위 큐 구현
Dijkstra(start, finish) {
const nodes = new PriorityQueue();
const distance = {};
const previous = {};
// bulid up initail state.
for(let vertex in this.adjacencyList) {
if(vertex === start) {
distance[vertex] = 0;
nodes.enqueue(vertex, 0);
} else {
distance[vertex] = Infinity;
nodes.enqueue(vertex, Infinity);
// 우선순위 큐 만들어주기.
// 니중에 우선순위를 줄여주어서 방문할 값을 만들어 줄것임.
}
previous[vertex] = null;
}
// distance
{
A: 0,
B: Infinity,
C: Infinity,
D: Infinity,
E: Infinity,
F: Infinity
}
// previous
{ A: null, B: null, C: null, D: null, E: null, F: null }
// priorityQueue
PriorityQueue {
values: [
{ val: 'A', priority: 0 },
{ val: 'B', priority: Infinity },
{ val: 'C', priority: Infinity },
{ val: 'D', priority: Infinity },
{ val: 'E', priority: Infinity },
{ val: 'F', priority: Infinity }
]
}
// as long as there is something to visit
while(nodes.values.length){
smallest = nodes.dequeue().val;
if(smallest === finish){
//WE ARE DONE
//BUILD UP PATH TO RETURN AT END
while(previous[smallest]){
path.push(smallest);
smallest = previous[smallest];
}
break;
}
if(smallest || distances[smallest] !== Infinity){
for(let neighbor in this.adjacencyList[smallest]){
//find neighboring node
let nextNode = this.adjacencyList[smallest][neighbor];
//calculate new distance to neighboring node
let candidate = distances[smallest] + nextNode.weight;
let nextNeighbor = nextNode.node;
if(candidate < distances[nextNeighbor]){
//updating new smallest distance to neighbor
distances[nextNeighbor] = candidate;
//updating previous - How we got to neighbor
previous[nextNeighbor] = smallest;
//enqueue in priority queue with new priority
nodes.enqueue(nextNeighbor, candidate);
}
}
}
}
class PriorityQueue {
constructor(){
this.values = [];
}
enqueue(val, priority) {
this.values.push({val, priority});
this.sort();
};
dequeue() {
return this.values.shift();
};
sort() {
this.values.sort((a, b) => a.priority - b.priority);
};
}
class WeightedGraph {
constructor() {
this.adjacencyList = {};
}
addVertex(vertex){
if(!this.adjacencyList[vertex]) this.adjacencyList[vertex] = [];
}
addEdge(vertex1,vertex2, weight){
this.adjacencyList[vertex1].push({node:vertex2,weight});
this.adjacencyList[vertex2].push({node:vertex1, weight});
}
Dijkstra(start, finish){
const nodes = new PriorityQueue();
const distances = {};
const previous = {};
let path = [] //to return at end
let smallest;
//build up initial state
for(let vertex in this.adjacencyList){
if(vertex === start){
distances[vertex] = 0;
nodes.enqueue(vertex, 0);
} else {
distances[vertex] = Infinity;
nodes.enqueue(vertex, Infinity);
}
previous[vertex] = null;
}
// as long as there is something to visit
while(nodes.values.length){
smallest = nodes.dequeue().val;
if(smallest === finish){
//WE ARE DONE
//BUILD UP PATH TO RETURN AT END
while(previous[smallest]){
path.push(smallest);
smallest = previous[smallest];
}
break;
}
if(smallest || distances[smallest] !== Infinity){
for(let neighbor in this.adjacencyList[smallest]){
//find neighboring node
let nextNode = this.adjacencyList[smallest][neighbor];
//calculate new distance to neighboring node
let candidate = distances[smallest] + nextNode.weight;
let nextNeighbor = nextNode.node;
if(candidate < distances[nextNeighbor]){
//updating new smallest distance to neighbor
distances[nextNeighbor] = candidate;
//updating previous - How we got to neighbor
previous[nextNeighbor] = smallest;
//enqueue in priority queue with new priority
nodes.enqueue(nextNeighbor, candidate);
}
}
}
}
return path.concat(smallest).reverse();
}
}
var graph = new WeightedGraph()
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("C");
graph.addVertex("D");
graph.addVertex("E");
graph.addVertex("F");
graph.addEdge("A","B", 4);
graph.addEdge("A","C", 2);
graph.addEdge("B","E", 3);
graph.addEdge("C","D", 2);
graph.addEdge("C","F", 4);
graph.addEdge("D","E", 3);
graph.addEdge("D","F", 1);
graph.addEdge("E","F", 1);
graph.Dijkstra("A", "E");
// ["A", "C", "D", "F", "E"]
이 문서는 유데미에서 다음 강의를 듣고 기록한 문서입니다.
부족한 내용이 있을 수 있습니다. 🥲
【글로벌 Best】 JavaScript 알고리즘 & 자료구조 마스터클래스 (한글자막)