Skip to content

Commit

Permalink
Dijkstra algorithm (#2)
Browse files Browse the repository at this point in the history
* Get the f'off

* done with dijkstra algorithm

* refactoring

Co-authored-by: bson <[email protected]>
  • Loading branch information
sonck201 and tbvbson authored Feb 26, 2021
1 parent 4dd4e6f commit 2696ce0
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 831 deletions.
15 changes: 14 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@
"protocol": "inspector",
"request": "attach",
"type": "node"
}
},
{
"name": "Algorithm dijkstra",
"program": "${workspaceFolder}/src/dijkstra.ts",
"request": "launch",
"runtimeArgs": [
"-r",
"ts-node/register/transpile-only",
"-r",
"tsconfig-paths/register",
],
"type": "pwa-node",
"outputCapture": "std",
},
],
"version": "0.2.0"
}
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
"javascript",
"typescript"
],
"files.exclude": {
"**/.DS_Store": true,
"**/.git": true,
"**/coverage": true,
"**/node_modules/": true
},
"search.exclude": {
"**/*.lock": true,
"**/coverage": true,
"**/dist": true,
"**/node_modules": true
Expand Down
18 changes: 1 addition & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,13 @@
"url": "https://github.com/sonck201/algorithm"
},
"scripts": {
"build": "yarn ts:build",
"clean": "rimraf coverage dist logs",
"debug": "yarn build && yarn watch:debug",
"lint": "yarn lint-ts",
"lint-ts": "tsc --noEmit && eslint \"**/*.{js,ts}\" --ignore-path .gitignore .",
"lint-ts:fix": "yarn lint-ts --quiet --fix",
"lint:fix": "yarn lint-ts:fix",
"node:watch": "nodemon --signal SIGHUP -r ts-node/register/transpile-only -r tsconfig-paths/register dist/server.js",
"prebuild": "yarn clean",
"serve": "node -r ./tsconfig-paths-bootstrap.js dist/server.js",
"serve:debug": "nodemon --signal SIGHUP -r ts-node/register/transpile-only -r tsconfig-paths/register --inspect dist/server.js",
"start": "yarn serve",
"ts:build": "tsc",
"ts:watch": "tsc -w",
"watch": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"cyan.bold,green.bold\" \"yarn ts:watch\" \"yarn node:watch\"",
"watch:debug": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"cyan.bold,green.bold\" \"yarn ts:watch\" \"yarn serve:debug\""
"lint:fix": "yarn lint-ts:fix"
},
"dependencies": {
"bluebird": "^3.5.5",
"camelcase-keys": "^6.2.2",
"winston": "^3.2.1"
},
"devDependencies": {
Expand All @@ -42,15 +29,12 @@
"@types/node": "^14.14.25",
"@typescript-eslint/eslint-plugin": "^4.14.2",
"@typescript-eslint/parser": "^4.14.2",
"concurrently": "^5.0.0",
"eslint": "^7.19.0",
"eslint-config-prettier": "^7.0.0",
"eslint-import-resolver-typescript": "^2.3.0",
"eslint-plugin-import": "^2.21.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.4",
"faker": "^5.3.1",
"nodemon": "^2.0.4",
"prettier": "^2.0.5",
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
Expand Down
98 changes: 98 additions & 0 deletions src/dijkstra.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
type INodeVertex = {
[vertex: string]: number
}

class Graph {
private _startNode = ''
private _vertexes = {}
private _nodes: any = {}
private _dist: any = {}
private _prev: any = {}

set startNode(startNode: string) {
this._startNode = startNode
}

addEdge(fromVertex: string, nodes: INodeVertex): void {
for (const toVertex in nodes) {
if (!this._nodes[fromVertex]) {
this._nodes[fromVertex] = {}
}

this._nodes[fromVertex][toVertex] = nodes[toVertex]

this._vertexes = { ...this._vertexes, ...{ [fromVertex]: true, [toVertex]: true } }
}
}

private dijkstra(): void {
for (const vertex in this._vertexes) {
this._dist[vertex] = Infinity
this._prev[vertex] = undefined
}

this._dist[this._startNode] = 0

const Q = Object.keys(this._nodes)
while (Q.length > 0) {
let u = ''

for (const min of Q) {
if (u === '' || (this._dist[min] && this._dist[min] < this._dist[u])) {
u = min
}
}

if (this._dist[u] === Infinity) {
break
}

Q.splice(Q.indexOf(String(u)), 1)

for (const v in this._nodes[u]) {
const alt = this._dist[u] + this._nodes[u][v]
if (alt < this._dist[v]) {
this._dist[v] = alt
this._prev[v] = u
}
}
}
}

printPath(dest: string) {
if (this._prev[dest] != undefined) {
this.printPath(this._prev[dest])
}
console.log(`> ${dest}`)
}

getShortestPath(startNode: string): void {
this.startNode = startNode

this.dijkstra()

console.log(`Source: ${startNode}`)
for (const dest of this.getVertexes()) {
console.log(`\nTarget: ${dest}`)
this.printPath(dest)
if (this._dist[dest] != Infinity) {
console.log(`Distance: ${this._dist[dest]}`)
} else {
console.log('No path')
}
}
}

getVertexes() {
return Object.keys(this._vertexes)
}
}

const g = new Graph()

g.addEdge('A', { B: 5, C: 3 })
g.addEdge('B', { C: 4, D: 3, E: 4 })
g.addEdge('C', { B: 2, D: 5, E: 6 })
g.addEdge('E', { D: 2 })

g.getShortestPath('A')
32 changes: 19 additions & 13 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
{
"compilerOptions": {
"target": "es6",
"allowSyntheticDefaultImports": true,
"baseUrl": "./src",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"lib": [
"es5",
"es6",
"dom",
"dom.iterable"
],
"module": "commonjs",
"sourceMap": true,
"outDir": "./dist",
"strict": true,
"noImplicitAny": true,
"moduleResolution": "node",
"baseUrl": "./src",
"noImplicitAny": true,
"outDir": "./dist",
"paths": {
"*": [
"./node_modules/*",
"./src/types/*"
],
"@src/*": [
"./*"
],
"@utils/*": [
"./utils/*"
],
"*": [
"./node_modules/*",
"./src/types/*"
]
},
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
"sourceMap": true,
"strict": true,
"target": "es6"
},
"include": [
"src/**/*"
Expand Down
Loading

0 comments on commit 2696ce0

Please sign in to comment.