Created
February 12, 2021 07:45
-
-
Save spiterman/8b44dae4a6279ddcea5c1eb2a5fdd6b7 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
| function addNode(graph, nodeToAdd) { | |
| if(graph[nodeToAdd] === undefined) { | |
| graph[nodeToAdd] = new Set() | |
| } | |
| return graph | |
| } | |
| function addConnection(graph, origin, destination) { | |
| addNode(graph, origin) | |
| addNode(graph, destination) | |
| graph[origin].add(String(destination)) | |
| return graph | |
| } | |
| function deleteConnection(graph, origin, destination){ | |
| graph[origin].delete(String(destination)) | |
| return graph | |
| } | |
| function deleteNode(graph, nodeToDelete) { | |
| delete graph[nodeToDelete] | |
| for(let node in graph) { | |
| deleteConnection(graph, node, nodeToDelete) | |
| } | |
| return graph | |
| } | |
| let graph = {}; | |
| addNode(graph, 'v1') | |
| addNode(graph, 'v2') | |
| addNode(graph, 'v3') | |
| addNode(graph, 'v4') | |
| addNode(graph, 'v5') | |
| addNode(graph, 'v5') | |
| addConnection(graph, 'v1', 'v2') | |
| addConnection(graph, 'v1', 'v6') | |
| addConnection(graph, 'v2', 'v4') | |
| addConnection(graph, 'v2', 'v5') | |
| addConnection(graph, 'v2', 'v6') | |
| addConnection(graph, 'v3', 'v2') | |
| addConnection(graph, 'v4', 'v3') | |
| addConnection(graph, 'v5', 'v2') | |
| addConnection(graph, 'v6', 'v5') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment