Last active
December 5, 2025 22:49
-
-
Save davidystephenson/ad544a2f0bb59d61757a10033fe63538 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const roads = [ | |
| 'a-b', // ['a', 'b'] | |
| 'a-c', // ['a', 'c'] | |
| 'a-d', // ['a', 'd'] | |
| 'b-a', // ['b', 'a'] | |
| 'b-d', | |
| 'b-e', | |
| 'c-a', | |
| 'c-d', | |
| 'd-a', | |
| 'd-e', | |
| 'e-b', | |
| 'e-d' | |
| ] | |
| const example = { | |
| a: ['b', 'c', 'd'], | |
| b: ['a', 'd', 'e'], | |
| c: ['a', 'd'], | |
| d: ['a', 'e'], | |
| e: ['b', 'd'] | |
| } | |
| function createGraph (roads) { | |
| let graph = {} | |
| for (let x of roads) { | |
| let array = x.split('-') | |
| // v1 | |
| // let start = array[0] | |
| // let end = array[1] | |
| // v2 | |
| // let [start, end] = array | |
| if (start in graph) { | |
| graph[start].push(end) | |
| } else { | |
| graph[start] = [end] | |
| } | |
| } | |
| return graph | |
| } | |
| const graph = createGraph(roads) | |
| console.log('graph', graph) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment