|
| 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"); |
0 commit comments