diff --git a/README.md b/README.md index 51abd25..4b838f2 100644 --- a/README.md +++ b/README.md @@ -1 +1,146 @@ -# javascript-subway-final \ No newline at end of file +# ๐Ÿš‡ ์ง€ํ•˜์ฒ  ๋…ธ์„ ๋„ ๊ฒฝ๋กœ ์กฐํšŒ ๋ฏธ์…˜ +- ๋“ฑ๋ก๋œ ์ง€ํ•˜์ฒ  ๋…ธ์„ ๋„์—์„œ ๊ฒฝ๋กœ๋ฅผ ์กฐํšŒํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•œ๋‹ค. + +## ๐Ÿš€ ๊ธฐ๋Šฅ ์š”๊ตฌ์‚ฌํ•ญ +> ํ”„๋ฆฌ์ฝ”์Šค 3์ฃผ์ฐจ ๋ฏธ์…˜์—์„œ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•ด๋„ ๋ฌด๊ด€ํ•˜๋‹ค. + +### ์ดˆ๊ธฐ ์„ค์ • +- ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘ ์‹œ ์—ญ, ๋…ธ์„ , ๊ตฌ๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ดˆ๊ธฐ ์„ค์ • ํ•ด์•ผ ํ•œ๋‹ค. +- ๊ฑฐ๋ฆฌ์™€ ์†Œ์š” ์‹œ๊ฐ„์€ ์–‘์˜ ์ •์ˆ˜์ด๋ฉฐ ๋‹จ์œ„๋Š” km์™€ ๋ถ„์„ ์˜๋ฏธํ•œ๋‹ค. +- ์•„๋ž˜์˜ ์‚ฌ์ „ ๋“ฑ๋ก ์ •๋ณด๋กœ ๋ฐ˜๋“œ์‹œ ์ดˆ๊ธฐ ์„ค์ •์„ ํ•œ๋‹ค. + +``` +1. ์ง€ํ•˜์ฒ ์—ญ์œผ๋กœ ๊ต๋Œ€, ๊ฐ•๋‚จ, ์—ญ์‚ผ, ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„, ์–‘์žฌ, ์–‘์žฌ์‹œ๋ฏผ์˜์ˆฒ, ๋งค๋ด‰ ์—ญ ์ •๋ณด๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ๋‹ค. +2. ์ง€ํ•˜์ฒ  ๋…ธ์„ ์œผ๋กœ 2ํ˜ธ์„ , 3ํ˜ธ์„ , ์‹ ๋ถ„๋‹น์„ ์ด ๋“ฑ๋ก๋˜์–ด ์žˆ๋‹ค. +3. ๋…ธ์„ ์— ์—ญ์ด ์•„๋ž˜์™€ ๊ฐ™์ด ๋“ฑ๋ก๋˜์–ด ์žˆ๋‹ค.(์™ผ์ชฝ ๋์ด ์ƒํ–‰ ์ข…์ ) + - 2ํ˜ธ์„ : ๊ต๋Œ€ - ( 2km / 3๋ถ„ ) - ๊ฐ•๋‚จ - ( 2km / 3๋ถ„ ) - ์—ญ์‚ผ + - 3ํ˜ธ์„ : ๊ต๋Œ€ - ( 3km / 2๋ถ„ ) - ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„ - ( 6km / 5๋ถ„ ) - ์–‘์žฌ - ( 1km / 1๋ถ„ ) - ๋งค๋ด‰ + - ์‹ ๋ถ„๋‹น์„ : ๊ฐ•๋‚จ - ( 2km / 8๋ถ„ ) - ์–‘์žฌ - ( 10km / 3๋ถ„ ) - ์–‘์žฌ์‹œ๋ฏผ์˜์ˆฒ +``` + +### ๊ฒฝ๋กœ ์กฐํšŒ ๊ธฐ๋Šฅ + + +- ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ์„ ์ž…๋ ฅ๋ฐ›์•„ ๊ฒฝ๋กœ๋ฅผ ์กฐํšŒํ•œ๋‹ค. +- ๊ฒฝ๋กœ ์กฐํšŒ ์‹œ ์ด ๊ฑฐ๋ฆฌ, ์ด ์†Œ์š” ์‹œ๊ฐ„์„ ํ•จ๊ป˜ ์ถœ๋ ฅํ•œ๋‹ค. +- ๊ฒฝ๋กœ ์กฐํšŒ ์‹œ `์ตœ๋‹จ ๊ฑฐ๋ฆฌ` ๋˜๋Š” `์ตœ์†Œ ์‹œ๊ฐ„` ์˜ต์…˜์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. + +### ์˜ˆ์™ธ ์ฒ˜๋ฆฌ +- ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ์€ 2๊ธ€์ž ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค. +- ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์—ญ์„ ์ถœ๋ฐœ์—ญ ๋˜๋Š” ๋„์ฐฉ์—ญ์œผ๋กœ ์ž…๋ ฅํ•  ์ˆ˜ ์—†๋‹ค. +- ๊ฒฝ๋กœ ์กฐํšŒ ์‹œ ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ์ด ๊ฐ™์„ ์ˆ˜ ์—†๋‹ค. +- ๊ฒฝ๋กœ ์กฐํšŒ ์‹œ ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ์ด ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์œผ๋ฉด ๊ฒฝ๋กœ๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์—†๋‹ค. +- ๊ทธ ์™ธ ์ •์ƒ์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์ด ์ˆ˜ํ–‰๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ `alert`์œผ๋กœ ์—๋Ÿฌ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. + +
+ +## ๐Ÿ’ป ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์‹คํ–‰ ๊ฒฐ๊ณผ +### ๊ฒฝ๋กœ ์กฐํšŒ + + + +## โœ… ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ์‚ฌํ•ญ +### ๊ธธ์ฐพ๊ธฐ ๊ด€๋ จ ๊ธฐ๋Šฅ +- ์ถœ๋ฐœ์—ญ์„ ์ž…๋ ฅํ•˜๋Š” input ํƒœ๊ทธ๋Š” `departure-station-name-input` id ์†์„ฑ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. +- ๋„์ฐฉ์—ญ์„ ์ž…๋ ฅํ•˜๋Š” input ํƒœ๊ทธ๋Š” `arrival-station-name-input` id ์†์„ฑ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. +- ์ตœ๋‹จ๊ฑฐ๋ฆฌ, ์ตœ์†Œ์‹œ๊ฐ„์„ ์„ ํƒํ•˜๋Š” radio๋Š” `search-type` name ์†์„ฑ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. + - **radio option์˜ default ๊ฐ’์€ ์ตœ๋‹จ๊ฑฐ๋ฆฌ์ด๋‹ค.** +- ๊ธธ์ฐพ๊ธฐ ๋ฒ„ํŠผ์€ `search-button` id ์†์„ฑ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. +- ๐Ÿ“ ๊ฒฐ๊ณผ๋Š” `table`์„ ์ด์šฉํ•˜์—ฌ ๋ณด์—ฌ์ค€๋‹ค. + +## โ—๏ธํžŒํŠธ +## ๋ฐ์ดํ„ฐ ์ดˆ๊ธฐํ™” +์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘์— ํ•˜๋‚˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด data๋ฅผ `export`ํ•˜๊ณ , `import`ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. + +```javascript +export const users = [ + { + name: 'Alt' + }, + { + name: 'Jamie' + }, + { + name: 'Sony' + } +] + +export const courses = [ + { + name: 'frontend', + }, + { + name: 'backend', + }, + { + name: 'iOS', + }, + { + name: 'Android', + } +] + +``` +์œ„์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ `export`ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ `import` ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. +```javascript +import { users, courses } from './data.js' + +function App() { + this.users = users + this.courses = courses +} +``` + +## ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ +- `utils/Dijkstra.js` ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๊ฐ„ํŽธํ•˜๊ฒŒ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. +- ์ •์ (Vertex)๊ณผ ๊ฐ„์„ (Edge), ๊ทธ๋ฆฌ๊ณ  ๊ฐ€์ค‘์น˜ ๊ฐœ๋…์„ ์ด์šฉ + - ์ •์ : ์ง€ํ•˜์ฒ ์—ญ + - ๊ฐ„์„ : ์ง€ํ•˜์ฒ ์—ญ ์—ฐ๊ฒฐ์ •๋ณด + - ๊ฐ€์ค‘์น˜: ๊ฑฐ๋ฆฌ or ์†Œ์š” ์‹œ๊ฐ„ +- ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ธฐ์ค€ ์กฐํšŒ ์‹œ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฑฐ๋ฆฌ๋กœ ์„ค์ • +- ์ตœ์†Œ ์‹œ๊ฐ„ ๊ธฐ์ค€ ์กฐํšŒ ์‹œ ๊ฐ€์ค‘์น˜๋ฅผ ์‹œ๊ฐ„์œผ๋กœ ์„ค์ • + +```javascript +import Dijkstra from "./utils/Dijkstra.js"; +const dijkstra = new Dijkstra() + +//dijkstra.addEdge("์ถœ๋ฐœ์—ญ", "๋„์ฐฉ์—ญ", ๊ฑฐ๋ฆฌ ๋˜๋Š” ์‹œ๊ฐ„); +dijkstra.addEdge("V1", "V2", 2); +dijkstra.addEdge("V2", "V3", 2); +dijkstra.addEdge("V1", "V3", 100); + +const result = dijkstra.findShortestPath("V1", "V3"); +// result = ["V1", "V2", "V3"] +``` + +#### ํ…Œ์ŠคํŠธ์„ค๋ช… + + +- ์—ญ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ V1->V3 ๊ฒฝ๋กœ๊ฐ€ ์ตœ๋‹จ ๊ฒฝ๋กœ +- ์—ญ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ ๋ คํ•  ๊ฒฝ์šฐ V1->V3 ๊ฒฝ๋กœ์˜ ๊ฑฐ๋ฆฌ๋Š” 100km, V1->V2->V3 ๊ฒฝ๋กœ์˜ ๊ฑฐ๋ฆฌ๋Š” 4km์ด๋ฏ€๋กœ ์ตœ๋‹จ ๊ฒฝ๋กœ๋Š” V1->V2->V3 + +
+ +### ์š”๊ตฌ์‚ฌํ•ญ +- ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ์ž…๋ ฅ ๊ฐ’์„ ์ž‘์„ฑํ•œ ๊ฒฝ์šฐ `alert`์„ ์ด์šฉํ•ด ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ , ์žฌ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค. +- ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(jQuery, Lodash ๋“ฑ)๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ์ˆœ์ˆ˜ Vanilla JS๋กœ๋งŒ ๊ตฌํ˜„ํ•œ๋‹ค. +- **์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ ์ปจ๋ฒค์…˜์„ ์ง€ํ‚ค๋ฉด์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ** ํ•œ๋‹ค + - [https://google.github.io/styleguide/jsguide.html](https://google.github.io/styleguide/jsguide.html) + - [https://ui.toast.com/fe-guide/ko_CODING-CONVENSION/](https://ui.toast.com/fe-guide/ko_CODING-CONVENTION) +- **indent(์ธ๋ดํŠธ, ๋“ค์—ฌ์“ฐ๊ธฐ) depth๋ฅผ 3์ด ๋„˜์ง€ ์•Š๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. 2๊นŒ์ง€๋งŒ ํ—ˆ์šฉ**ํ•œ๋‹ค. + - ์˜ˆ๋ฅผ ๋“ค์–ด while๋ฌธ ์•ˆ์— if๋ฌธ์ด ์žˆ์œผ๋ฉด ๋“ค์—ฌ์“ฐ๊ธฐ๋Š” 2์ด๋‹ค. + - ํžŒํŠธ: indent(์ธ๋ดํŠธ, ๋“ค์—ฌ์“ฐ๊ธฐ) depth๋ฅผ ์ค„์ด๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์€ ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์†Œ๋“œ)๋ฅผ ๋ถ„๋ฆฌํ•˜๋ฉด ๋œ๋‹ค. +- **ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์†Œ๋“œ)์˜ ๊ธธ์ด๊ฐ€ 15๋ผ์ธ์„ ๋„˜์–ด๊ฐ€์ง€ ์•Š๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค.** + - ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์†Œ๋“œ)๊ฐ€ ํ•œ ๊ฐ€์ง€ ์ผ๋งŒ ์ž˜ ํ•˜๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. +- ๋ณ€์ˆ˜ ์„ ์–ธ์‹œ [var](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/var) ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. [const](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/const) ์™€ [let](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/let) ์„ ์‚ฌ์šฉํ•œ๋‹ค. +- [import](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/import) ๋ฌธ์„ ์ด์šฉํ•ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ชจ๋“ˆํ™”ํ•˜๊ณ  ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ ๋‹ค. +- [template literal](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Template_literals)์„ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ์™€ html string์„ ๊ฐ€๋…์„ฑ ์ข‹๊ฒŒ ํ‘œํ˜„ํ•œ๋‹ค. + +
+ +## ๐Ÿ“ ๋ฏธ์…˜ ์ €์žฅ์†Œ ๋ฐ ์ง„ํ–‰ ์š”๊ตฌ์‚ฌํ•ญ + +- ๋ฏธ์…˜์€ [https://github.com/woowacourse/javascript-subway-path-precourse](https://github.com/woowacourse/javascript-subway-path-precourse) ์ €์žฅ์†Œ๋ฅผ fork/cloneํ•ด ์‹œ์ž‘ํ•œ๋‹ค. +- **๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์ „์— javascript-subway-path-precourse/docs/README.md ํŒŒ์ผ์— ๊ตฌํ˜„ํ•  ๊ธฐ๋Šฅ ๋ชฉ๋ก**์„ ์ •๋ฆฌํ•ด ์ถ”๊ฐ€ํ•œ๋‹ค. +- **git์˜ commit ๋‹จ์œ„๋Š” ์•ž ๋‹จ๊ณ„์—์„œ README.md ํŒŒ์ผ์— ์ •๋ฆฌํ•œ ๊ธฐ๋Šฅ ๋ชฉ๋ก ๋‹จ์œ„๋กœ ์ถ”๊ฐ€**ํ•œ๋‹ค. +- [ํ”„๋ฆฌ์ฝ”์Šค ๊ณผ์ œ ์ œ์ถœ](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse) ๋ฌธ์„œ ์ ˆ์ฐจ๋ฅผ ๋”ฐ๋ผ ๋ฏธ์…˜์„ ์ œ์ถœํ•œ๋‹ค. diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..13907b1 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,291 @@ +# ๊ธฐ๋Šฅ ๊ตฌํ˜„ ๋ชฉ๋ก + +1. ๋ฐ์ดํ„ฐ ์ดˆ๊ธฐํ™” + + 1. data.js์— ์ •๋ฆฌํ•˜๊ธฐ + 2. ์ง€ํ•˜์ฒ  ์—ญ: ๊ต๋Œ€, ๊ฐ•๋‚จ, ์—ญ์‚ผ, ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„, ์–‘์žฌ, ์–‘์žฌ์‹œ๋ฏผ์˜์ˆฒ, ๋งค๋ด‰ + 3. ์ง€ํ•˜์ฒ  ๋…ธ์„ : 2ํ˜ธ์„ , 3ํ˜ธ์„ , ์‹ ๋ถ„๋‹น์„  + - 2ํ˜ธ์„ : ๊ต๋Œ€ - (2km / 3๋ถ„) - ๊ฐ•๋‚จ - (2km / 3๋ถ„) - ์—ญ์‚ผ + - 3ํ˜ธ์„ : ๊ต๋Œ€ - (3km / 2๋ถ„) - ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„ - (6km / 5๋ถ„) - ์–‘์žฌ - (1km / 1๋ถ„) - ๋งค๋ด‰ + - ์‹ ๋ถ„๋‹น์„ : ๊ฐ•๋‚จ - (2km / 8๋ถ„) - ์–‘์žฌ - (10km / 3๋ถ„) - ์–‘์žฌ์‹œ๋ฏผ์˜์ˆฒ + +2. ๊ฒฝ๋กœ ์กฐํšŒ + + 1. `getFromStation`: ์ถœ๋ฐœ์—ญ ์ž…๋ ฅ ๋ฐ›๊ธฐ ํ•จ์ˆ˜ + 2. `getToStation`: ๋„์ฐฉ์—ญ ์ž…๋ ฅ ๋ฐ›๊ธฐ ํ•จ์ˆ˜ + 3. `getOption`: ์ตœ๋‹จ๊ฑฐ๋ฆฌ or ์ตœ์†Œ์‹œ๊ฐ„ ์„ ํƒ ํ•จ์ˆ˜ + +3. ์˜ˆ์™ธ ์ฒ˜๋ฆฌ (๋ฐ‘์ค„์˜ ๊ฒฝ์šฐ์—๋งŒ ๋กœ์ง ์ฒ˜๋ฆฌ) + + 1. `vaildStationNameLength`: ์ถœ๋ฐœ์—ญ/๋„์ฐฉ์—ญ ์ด๋ฆ„์ด 2๊ธ€์ž ์ด์ƒ์ธ์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜ + (true: 2๊ธ€์ž ์ด์ƒ, false: 2๊ธ€์ž ๋ฏธ๋งŒ) + 2. `notExsistStation`: ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์—ญ ์—ฌ๋ถ€ ํ™•์ธ ํ•จ์ˆ˜ + (true: ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์—ญ, false: ์กด์žฌํ•˜๋Š” ์—ญ) + 3. `sameStation`: ์ถœ๋ฐœ์—ญ/๋„์ฐฉ์—ญ ์ด๋ฆ„์ด ๊ฐ™์€์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜ + (true: ๊ฐ™์Œ, false: ๊ฐ™์ง€ ์•Š์Œ) + 4. `connected`: ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ์ด ์—ฐ๊ฒฐ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜ + (true: ์—ฐ๊ฒฐ๋จ, false: ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์Œ) + 5. `alertError`: ๊ทธ ์™ธ ์ •์ƒ์ ์ด์ง€ ์•Š์€ ํ”„๋กœ๊ทธ๋žจ ์ˆ˜ํ–‰ ์•Œ๋ฆผ ํ•จ์ˆ˜ + +4. ๊ธธ์ฐพ๊ธฐ ๊ด€๋ จ ๊ธฐ๋Šฅ + + 1. ์ถœ๋ฐœ์—ญ input ํƒœ๊ทธ id ์†์„ฑ๊ฐ’: `departure-station-name-input` + 2. ๋„์ฐฉ์—ญ input ํƒœ๊ทธ id ์†์„ฑ๊ฐ’: `arrival-station-name-input` + 3. ์ตœ์†Œ์‹œ๊ฐ„/์ตœ๋‹จ๊ฑฐ๋ฆฌ ์„ ํƒ radio์˜ name ์†์„ฑ๊ฐ’: `search-type` + - radio option์˜ ๊ธฐ๋ณธ๊ฐ’: `์ตœ๋‹จ๊ฑฐ๋ฆฌ` + 4. ๊ธธ์ฐพ๊ธฐ ๋ฒ„ํŠผ id ์†์„ฑ๊ฐ’: `search-button` + 5. ๊ฒฐ๊ณผ ์ถœ๋ ฅ: `table` + +5. ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ: `utils/Dijkstra.js`์— ์ •๋ฆฌ๋˜์–ด ์žˆ์Œ + + ```javascript + export default function Dijkstra() { + + // Node: ์—ญ์—๋Œ€ํ•œ ์ •๋ณด, ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ–๋Š” ๋…ธ๋“œ + const Node = { + init: function (val, priority) { + this.val = val; + this.priority = priority; + }, + }; + + // ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ธฐ๋ฐ˜ ์ž๋ฃŒ๊ตฌ์กฐ, ์šฐ์„ ์ˆœ์œ„ ํ + const PriorityQueue = { + // ์šฐ์„ ์ˆœ์œ„ ํ์˜ ์ดˆ๊ธฐ๊ฐ’์€ ๋นˆ ๋ฐฐ์—ด + init: function () { + this.values = []; + }, + + // ์šฐ์„ ์ˆœ์œ„ ํ์— ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑ(create & init)ํ•ด์„œ ํ์— ๋„ฃ๋Š”๋‹ค. + enqueue: function (val, priority) { + const newNode = Object.create(Node); + newNode.init(val, priority); + + this.values.push(newNode); + + let idxOfNewNode = this.values.length - 1; + + // ์šฐ์„ ์ˆœ์œ„ ํ์— ๋…ธ๋“œ๊ฐ€ ๋“ค์–ด๊ฐ”์„ ๋•Œ, ์šฐ์„ ์ˆœ์œ„ ์กฐ์ •ํ•ด์ฃผ๋Š” ๋ถ€๋ถ„ + while (idxOfNewNode > 0) { + const idxOfParentNode = Math.floor((idxOfNewNode - 1) / 2); + + const parentNode = this.values[idxOfParentNode]; + + if (priority < parentNode.priority) { + this.values[idxOfParentNode] = newNode; + this.values[idxOfNewNode] = parentNode; + idxOfNewNode = idxOfParentNode; + continue; + } + break; + } + // enqueue ๋กœ์ง์ด ๋๋‚˜๋ฉด ์šฐ์„ ์ˆœ์œ„๋Œ€๋กœ ์ •๋ ฌ๋œ values๋ฅผ ๋ฆฌํ„ด + return this.values; + }, + + // ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ ๋…ธ๋“œ ์‚ญ์ œ + dequeue: function () { + // ๋งŒ์•ฝ ์šฐ์„ ์ˆœ์œ„ ํ์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’์ด ์—†์œผ๋ฉด ์‚ญ์ œํ•  ์ˆ˜ ์—†์Œ + if (this.values.length == 0) { + return; + } + + // ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๋Š” shift() + const dequeued = this.values.shift(); + // ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๊ทธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” pop() + const lastItem = this.values.pop(); + // ๋งˆ์ง€๋ง‰ ์š”์†Œ๊ฐ€ ์•„๋‹ˆ๋ฉด ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฆฌํ„ด + if (!lastItem) { + return dequeued; + } + // lastItem์„ ๋ฐฐ์—ด์˜ ๋งจ ์•ž์ชฝ์— ์ถ”๊ฐ€ํ•˜๊ณ , ์ƒˆ๋กœ์šด ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” unshift() + this.values.unshift(lastItem); + + let idxOfTarget = 0; + + while (true) { + let idxOfLeftChild = idxOfTarget * 2 + 1; + let idxOfRightChild = idxOfTarget * 2 + 2; + let leftChild = this.values[idxOfLeftChild]; + let rightChild = this.values[idxOfRightChild]; + + function swap(direction) { + const idxOfChild = + direction == "left" ? idxOfLeftChild : idxOfRightChild; + const child = direction == "left" ? leftChild : rightChild; + this.values[idxOfChild] = this.values[idxOfTarget]; + this.values[idxOfTarget] = child; + idxOfTarget = idxOfChild; + } + + if (!leftChild) { + return dequeued; + } + + if (!rightChild) { + if (leftChild.priority < lastItem.priority) { + swap.call(this, "left"); + continue; + } + return dequeued; + } + + if (leftChild.priority == rightChild.priority) { + swap.call(this, "left"); + continue; + } + + if ( + leftChild.priority < rightChild.priority && + leftChild.priority < lastItem.priority + ) { + swap.call(this, "left"); + continue; + } + + if ( + rightChild.priority < leftChild.priority && + rightChild.priority < lastItem.priority + ) { + swap.call(this, "right"); + continue; + } + } + }, + }; + + // ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„ + const WeightedGraph = { + init: function () { + this.adjacencyList = {}; + this.length = 0; + }, + + // ๋…ธ๋“œ(vertex) ์ถ”๊ฐ€ + addVertex: function (vertex) { + if (!this.adjacencyList.hasOwnProperty(vertex)) { + this.adjacencyList[vertex] = {}; + this.length++; + } + }, + + // ๊ฐ„์„ (edge) ์ถ”๊ฐ€ + addEdge: function (vertex1, vertex2, weight) { + this.addVertex(vertex1); + this.addVertex(vertex2); + this.adjacencyList[vertex1][vertex2] = weight; + this.adjacencyList[vertex2][vertex1] = weight; + return this.adjacencyList; + }, + + // ๊ฐ„์„  ์ œ๊ฑฐ + removeEdge: function (vertex1, vertex2) { + if (!this.adjacencyList.hasOwnProperty(vertex1)) { + return `There's no ${vertex1}`; + } + if (!this.adjacencyList.hasOwnProperty(vertex2)) { + return `There's no ${vertex2}`; + } + + function removeHelper(v1, v2) { + if (!this.adjacencyList.hasOwnProperty(v1)) { + return `There's no edge between ${v1} and ${v2}`; + } + delete this.adjacencyList[v1][v2]; + if (Object.keys(this.adjacencyList[v1]).length == 0) { + delete this.adjacencyList[v1]; + } + } + + removeHelper.call(this, vertex1, vertex2); + removeHelper.call(this, vertex2, vertex1); + + return this.adjacencyList; + }, + + // ๋…ธ๋“œ ์ œ๊ฑฐ + removeVertex: function (vertex) { + if (!this.adjacencyList.hasOwnProperty(vertex)) { + return `There's no ${vertex}`; + } + const edges = this.adjacencyList[vertex]; + for (const key in edges) { + this.removeEdge(key, vertex); + } + return this.adjacencyList; + }, + findShortestRoute: function (start, end) { + if (!start || !end) { + throw Error("์ถœ๋ฐœ์ง€์™€ ๋„์ฐฉ์ง€๋ฅผ ๋ชจ๋‘ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + const distance = {}; + const previous = {}; + const pq = Object.create(PriorityQueue); + pq.init(); + pq.enqueue(start, 0); + const visited = {}; + + const hashOfVertex = this.adjacencyList; + for (const vertexName in hashOfVertex) { + const priority = vertexName == start ? 0 : Infinity; + distance[vertexName] = priority; + previous[vertexName] = null; + } + + while (true) { + let current = pq.dequeue(); + if (!current?.val) { + return; + } + current = current.val; + if (current == end) { + break; + } + const neighbors = hashOfVertex[current]; + + for (const vertexName in neighbors) { + if (visited.hasOwnProperty(vertexName)) { + continue; + } + const distFromStart = distance[current] + neighbors[vertexName]; + + if (distFromStart < distance[vertexName]) { + pq.enqueue(vertexName, distFromStart); + distance[vertexName] = distFromStart; + previous[vertexName] = current; + } + } + visited[current] = true; + } + + let node = end; + + const route = []; + while (node) { + route.unshift(node); + node = previous[node]; + } + + return route; + }, + }; + + this.addEdge = (source, target, weight) => { + WeightedGraph.addEdge(source, target, weight); + }; + + this.findShortestPath = (source, target) => { + return WeightedGraph.findShortestRoute(source, target); + }; + + this.addVertex = (vertex) => { + WeightedGraph.addVertex(vertex); + }; + + WeightedGraph.init(); + } + + ``` + +6. +7. \ No newline at end of file diff --git a/images/dijkstra_example.png b/images/dijkstra_example.png new file mode 100644 index 0000000..7c75197 Binary files /dev/null and b/images/dijkstra_example.png differ diff --git a/images/path_result.gif b/images/path_result.gif new file mode 100644 index 0000000..ee394bd Binary files /dev/null and b/images/path_result.gif differ diff --git a/images/path_result.jpg b/images/path_result.jpg new file mode 100644 index 0000000..40a4bed Binary files /dev/null and b/images/path_result.jpg differ diff --git a/index.html b/index.html new file mode 100644 index 0000000..6b939ea --- /dev/null +++ b/index.html @@ -0,0 +1,17 @@ + + + + + ์ง€ํ•˜์ฒ  ๊ธธ์ฐพ๊ธฐ + + +
+
+
+
+ +
+
+ + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..aead43d --- /dev/null +++ b/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} \ No newline at end of file diff --git a/src/data.js b/src/data.js new file mode 100644 index 0000000..7161623 --- /dev/null +++ b/src/data.js @@ -0,0 +1,35 @@ +export const stationList = ["๊ต๋Œ€", "๊ฐ•๋‚จ", "์—ญ์‚ผ", "๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„", "์–‘์žฌ", "๋งค๋ด‰", "์–‘์žฌ์‹œ๋ฏผ์˜์ˆฒ"] + +export const pathWithDistance = [ + ["๊ต๋Œ€", "๊ฐ•๋‚จ", 2], + ["๊ต๋Œ€", "๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„", 3], + ["๊ฐ•๋‚จ", "๊ต๋Œ€", 2], + ["๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„", "๊ต๋Œ€", 3], + ["๊ฐ•๋‚จ", "์—ญ์‚ผ", 2], + ["์—ญ์‚ผ", "๊ฐ•๋‚จ", 2], + ["๊ฐ•๋‚จ", "์–‘์žฌ", 2], + ["์–‘์žฌ", "๊ฐ•๋‚จ", 2], + ["๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„", "์–‘์žฌ", 6], + ["์–‘์žฌ", "๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„", 6], + ["์–‘์žฌ", "๋งค๋ด‰", 1], + ["๋งค๋ด‰", "์–‘์žฌ", 1], + ["์–‘์žฌ", "์–‘์žฌ์‹œ๋ฏผ์˜์ˆฒ", 10], + ["์–‘์žฌ์‹œ๋ฏผ์˜์ˆฒ", "์–‘์žฌ", 10] +] + +export const pathWithTime = [ + ["๊ต๋Œ€", "๊ฐ•๋‚จ", 2], + ["๊ต๋Œ€", "๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„", 3], + ["๊ฐ•๋‚จ", "๊ต๋Œ€", 2], + ["๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„", "๊ต๋Œ€", 3], + ["๊ฐ•๋‚จ", "์—ญ์‚ผ", 2], + ["์—ญ์‚ผ", "๊ฐ•๋‚จ", 2], + ["๊ฐ•๋‚จ", "์–‘์žฌ", 2], + ["์–‘์žฌ", "๊ฐ•๋‚จ", 2], + ["๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„", "์–‘์žฌ", 6], + ["์–‘์žฌ", "๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„", 6], + ["์–‘์žฌ", "๋งค๋ด‰", 1], + ["๋งค๋ด‰", "์–‘์žฌ", 1], + ["์–‘์žฌ", "์–‘์žฌ์‹œ๋ฏผ์˜์ˆฒ", 10], + ["์–‘์žฌ์‹œ๋ฏผ์˜์ˆฒ", "์–‘์žฌ", 10] +] \ No newline at end of file diff --git a/src/handler.js b/src/handler.js new file mode 100644 index 0000000..50d3a65 --- /dev/null +++ b/src/handler.js @@ -0,0 +1,18 @@ +import {stationList} from "./data.js" + +export function validStationNameLength(name) { + return name.length > 1 +} + +export function notExistStation(station) { + return stationList.includes(station) +} + +export function sameStation(departure, arrival) { + return (departure === arrival) +} + +// function connected(departure, arrival) { +// // TODO: ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ์ด ์—ฐ๊ฒฐ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋กœ์ง -> Dijkstra? +// return +// } \ No newline at end of file diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..1658c72 --- /dev/null +++ b/src/index.js @@ -0,0 +1,105 @@ +import {validStationNameLength, notExistStation, sameStation} from "./handler.js" +import Dijkstra from "./utils/Dijkstra.js" +import {pathWithDistance, pathWithTime} from "./data.js" + +function init() { + const dijkstraDistance = new Dijkstra(); + const dijkstraTime = new Dijkstra(); + // TODO 1: ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๋กœ์ง + for (let i=0; i 0) { + const idxOfParentNode = Math.floor((idxOfNewNode - 1) / 2); + + const parentNode = this.values[idxOfParentNode]; + + if (priority < parentNode.priority) { + this.values[idxOfParentNode] = newNode; + this.values[idxOfNewNode] = parentNode; + idxOfNewNode = idxOfParentNode; + continue; + } + break; + } + return this.values; + }, + dequeue: function () { + if (this.values.length == 0) { + return; + } + const dequeued = this.values.shift(); + const lastItem = this.values.pop(); + if (!lastItem) { + return dequeued; + } + this.values.unshift(lastItem); + + let idxOfTarget = 0; + + while (true) { + let idxOfLeftChild = idxOfTarget * 2 + 1; + let idxOfRightChild = idxOfTarget * 2 + 2; + let leftChild = this.values[idxOfLeftChild]; + let rightChild = this.values[idxOfRightChild]; + + function swap(direction) { + const idxOfChild = + direction == "left" ? idxOfLeftChild : idxOfRightChild; + const child = direction == "left" ? leftChild : rightChild; + this.values[idxOfChild] = this.values[idxOfTarget]; + this.values[idxOfTarget] = child; + idxOfTarget = idxOfChild; + } + + if (!leftChild) { + return dequeued; + } + + if (!rightChild) { + if (leftChild.priority < lastItem.priority) { + swap.call(this, "left"); + continue; + } + return dequeued; + } + + if (leftChild.priority == rightChild.priority) { + swap.call(this, "left"); + continue; + } + + if ( + leftChild.priority < rightChild.priority && + leftChild.priority < lastItem.priority + ) { + swap.call(this, "left"); + continue; + } + + if ( + rightChild.priority < leftChild.priority && + rightChild.priority < lastItem.priority + ) { + swap.call(this, "right"); + continue; + } + } + }, + }; + + const WeightedGraph = { + init: function () { + this.adjacencyList = {}; + this.length = 0; + }, + addVertex: function (vertex) { + if (!this.adjacencyList.hasOwnProperty(vertex)) { + this.adjacencyList[vertex] = {}; + this.length++; + } + }, + addEdge: function (vertex1, vertex2, weight) { + this.addVertex(vertex1); + this.addVertex(vertex2); + this.adjacencyList[vertex1][vertex2] = weight; + this.adjacencyList[vertex2][vertex1] = weight; + return this.adjacencyList; + }, + removeEdge: function (vertex1, vertex2) { + if (!this.adjacencyList.hasOwnProperty(vertex1)) { + return `There's no ${vertex1}`; + } + if (!this.adjacencyList.hasOwnProperty(vertex2)) { + return `There's no ${vertex2}`; + } + + function removeHelper(v1, v2) { + if (!this.adjacencyList.hasOwnProperty(v1)) { + return `There's no edge between ${v1} and ${v2}`; + } + delete this.adjacencyList[v1][v2]; + if (Object.keys(this.adjacencyList[v1]).length == 0) { + delete this.adjacencyList[v1]; + } + } + + removeHelper.call(this, vertex1, vertex2); + removeHelper.call(this, vertex2, vertex1); + + return this.adjacencyList; + }, + removeVertex: function (vertex) { + if (!this.adjacencyList.hasOwnProperty(vertex)) { + return `There's no ${vertex}`; + } + const edges = this.adjacencyList[vertex]; + for (const key in edges) { + this.removeEdge(key, vertex); + } + return this.adjacencyList; + }, + findShortestRoute: function (start, end) { + if (!start || !end) { + throw Error("์ถœ๋ฐœ์ง€์™€ ๋„์ฐฉ์ง€๋ฅผ ๋ชจ๋‘ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + const distance = {}; + const previous = {}; + const pq = Object.create(PriorityQueue); + pq.init(); + pq.enqueue(start, 0); + const visited = {}; + + const hashOfVertex = this.adjacencyList; + for (const vertexName in hashOfVertex) { + const priority = vertexName == start ? 0 : Infinity; + distance[vertexName] = priority; + previous[vertexName] = null; + } + + while (true) { + let current = pq.dequeue(); + if (!current?.val) { + return; + } + current = current.val; + if (current == end) { + break; + } + const neighbors = hashOfVertex[current]; + + for (const vertexName in neighbors) { + if (visited.hasOwnProperty(vertexName)) { + continue; + } + const distFromStart = distance[current] + neighbors[vertexName]; + + if (distFromStart < distance[vertexName]) { + pq.enqueue(vertexName, distFromStart); + distance[vertexName] = distFromStart; + previous[vertexName] = current; + } + } + visited[current] = true; + } + + let node = end; + + const route = []; + while (node) { + route.unshift(node); + node = previous[node]; + } + + return route; + }, + }; + + this.addEdge = (source, target, weight) => { + WeightedGraph.addEdge(source, target, weight); + }; + + this.findShortestPath = (source, target) => { + return WeightedGraph.findShortestRoute(source, target); + }; + + this.addVertex = (vertex) => { + WeightedGraph.addVertex(vertex); + }; + + WeightedGraph.init(); +}