Skip to content

Commit 35426b8

Browse files
committed
sort 추가
1 parent 3f95b65 commit 35426b8

File tree

6 files changed

+164
-4
lines changed

6 files changed

+164
-4
lines changed

algorithm/day2.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,10 @@ n개의 원소가 있는 배열에서 i번째 원소를 정렬한다고 가정
6262

6363
- 시간복잡도: O(nlogn) / 최악: O(n^2)
6464
- 공간복잡도: O(n)
65+
66+
# 다익스트라 알고리즘
67+
68+
단일 출발지에서 최단 경로를 찾는 알고리즘 이다.
69+
![](https://cdn.filepicker.io/api/file/11XplfRtRC2pGdQW6xav)
70+
71+
모든 버텍스의 최단 거리값을 Infinity(무한)으로 설정합니다.

data-structure-homework/aram/da.js

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
var Graph = (function() {
2+
function Vertex(key) {
3+
this.next = null;
4+
this.arc = null;
5+
this.key = key;
6+
this.inTree = null;
7+
}
8+
function Arc(data, dest, capacity) {
9+
this.nextArc = null;
10+
this.destination = dest;
11+
this.data = data;
12+
this.capacity = capacity;
13+
this.inTree = null;
14+
}
15+
function Graph() {
16+
this.count = 0;
17+
this.first = null;
18+
}
19+
Graph.prototype.insertVertex = function(key) {
20+
var vertex = new Vertex(key);
21+
var last = this.first;
22+
if (last) {
23+
while (last.next !== null) {
24+
last = last.next;
25+
}
26+
last.next = vertex;
27+
} else {
28+
this.first = vertex;
29+
}
30+
this.count++;
31+
};
32+
Graph.prototype.deleteVertex = function(key) {
33+
var vertex = this.first;
34+
var prev = null;
35+
while (vertex.key !== key) {
36+
prev = vertex;
37+
vertex = vertex.next;
38+
}
39+
if (!vertex) return false;
40+
if (!vertex.arc) return false;
41+
if (prev) {
42+
prev.next = vertex.next;
43+
} else {
44+
this.first = vertex.next;
45+
}
46+
this.count--;
47+
};
48+
Graph.prototype.insertArc = function(data, fromKey, toKey, capacity) {
49+
var from = this.first;
50+
var to = this.first;
51+
while (from && from.key !== fromKey) {
52+
from = from.next;
53+
}
54+
while (to && to.key !== toKey) {
55+
to = to.next;
56+
}
57+
if (!from || !to) return false;
58+
var arc = new Arc(data, to, capacity);
59+
var fromLast = from.arc;
60+
if (fromLast) {
61+
while (fromLast.nextArc != null) {
62+
fromLast = fromLast.nextArc;
63+
}
64+
fromLast.nextArc = arc;
65+
} else {
66+
from.arc = arc;
67+
}
68+
};
69+
Graph.prototype.deleteArc = function(fromKey, toKey) {
70+
var from = this.first;
71+
while (from !== null) {
72+
if (from.key === fromKey) break;
73+
from = from.next;
74+
}
75+
if (!from) return false;
76+
var fromArc = from.arc;
77+
var preArc;
78+
while (fromArc !== null) {
79+
if (toKey === fromArc.destination.key) break;
80+
preArc = fromArc;
81+
fromArc = fromArc.next;
82+
}
83+
if (!fromArc) return false;
84+
if (preArc) {
85+
preArc.nextArc = fromArc.nextArc;
86+
} else {
87+
from.arc = fromArc.nextArc;
88+
}
89+
};
90+
91+
Graph.prototype.shortest = function(startKey) {
92+
var from = this.first;
93+
while (from) {
94+
if (from.key === startKey) {
95+
break;
96+
}
97+
from = from.next;
98+
}
99+
console.log("시작점은 %s입니다", from.key);
100+
var temp = this.first;
101+
var current;
102+
var arc;
103+
while (temp) {
104+
// 모든 버텍스 최단거리를 Infinity로 초기화
105+
temp.distance = Infinity;
106+
temp = temp.next;
107+
}
108+
temp = this.first;
109+
temp.distance = 0;
110+
while (temp) {
111+
// 반복문을 돌며 최단 거리를 찾음
112+
current = temp;
113+
temp = temp.next;
114+
arc = current.arc;
115+
while (arc) {
116+
if (arc.destination.distance > current.distance + arc.data) {
117+
arc.destination.distance = current.distance + arc.data;
118+
}
119+
arc = arc.nextArc;
120+
}
121+
}
122+
temp = this.first;
123+
while (temp) {
124+
console.log("%s까지의 최단 거리는 %d입니다", temp.key, temp.distance);
125+
temp = temp.next;
126+
}
127+
};
128+
129+
return Graph;
130+
})();
131+
function insertTwoWayArc(graph, data, from, to) {
132+
graph.insertArc(data, from, to);
133+
graph.insertArc(data, to, from);
134+
}
135+
136+
var graph = new Graph();
137+
graph.insertVertex("A");
138+
graph.insertVertex("B");
139+
graph.insertVertex("C");
140+
graph.insertVertex("D");
141+
graph.insertVertex("E");
142+
graph.insertVertex("F");
143+
insertTwoWayArc(graph, 6, "A", "B");
144+
insertTwoWayArc(graph, 3, "A", "C");
145+
insertTwoWayArc(graph, 2, "B", "C");
146+
insertTwoWayArc(graph, 5, "B", "D");
147+
insertTwoWayArc(graph, 3, "C", "D");
148+
insertTwoWayArc(graph, 4, "C", "E");
149+
insertTwoWayArc(graph, 2, "D", "E");
150+
insertTwoWayArc(graph, 3, "D", "F");
151+
insertTwoWayArc(graph, 5, "E", "F");
152+
graph.shortest("A");

data-structure-homework/aram/member.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

datastructure/data-structure.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 이진검색트리
2+
3+
1. 어떤 노드 n을 기준으로 왼쪽 서브트리 노드의 모든 키 값은 노드 n의 키값보다 작다.
4+
2. 오른쪽 서브트리 노드의 모든 키값은 노드 n의 키값보다 크다.
5+
3. 같은 키값을 가지는 노드는 없다.

0 commit comments

Comments
 (0)