-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Primitive graph realisation #1
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
parserOptions: | ||
ecmaVersion: 6 | ||
env: | ||
node: true | ||
extends: 'eslint:recommended' | ||
rules: | ||
indent: | ||
- error | ||
- 2 | ||
- SwitchCase: 1 | ||
VariableDeclarator: | ||
var: 2 | ||
let: 2 | ||
const: 3 | ||
MemberExpression: 1 | ||
linebreak-style: | ||
- error | ||
- unix | ||
quotes: | ||
- error | ||
- single | ||
semi: | ||
- error | ||
- always | ||
eqeqeq: | ||
- error | ||
- always | ||
no-loop-func: | ||
- error | ||
strict: | ||
- error | ||
- global | ||
block-spacing: | ||
- error | ||
- always | ||
brace-style: | ||
- error | ||
- 1tbs | ||
- allowSingleLine: true | ||
camelcase: | ||
- error | ||
comma-style: | ||
- error | ||
- last | ||
comma-spacing: | ||
- error | ||
- before: false | ||
after: true | ||
eol-last: | ||
- error | ||
func-call-spacing: | ||
- error | ||
- never | ||
key-spacing: | ||
- error | ||
- beforeColon: false | ||
afterColon: true | ||
mode: minimum | ||
keyword-spacing: | ||
- error | ||
- before: true | ||
after: true | ||
overrides: | ||
function: | ||
after: false | ||
max-len: | ||
- error | ||
- code: 80 | ||
ignoreUrls: true | ||
max-nested-callbacks: | ||
- error | ||
- max: 5 | ||
new-cap: | ||
- error | ||
- newIsCap: true | ||
capIsNew: true | ||
properties: true | ||
new-parens: | ||
- error | ||
no-lonely-if: | ||
- error | ||
no-trailing-spaces: | ||
- error | ||
no-unneeded-ternary: | ||
- error | ||
no-whitespace-before-property: | ||
- error | ||
object-curly-spacing: | ||
- error | ||
- always | ||
operator-assignment: | ||
- error | ||
- always | ||
operator-linebreak: | ||
- error | ||
- after | ||
semi-spacing: | ||
- error | ||
- before: false | ||
after: true | ||
space-before-blocks: | ||
- error | ||
- always | ||
space-before-function-paren: | ||
- error | ||
- never | ||
space-in-parens: | ||
- error | ||
- never | ||
space-infix-ops: | ||
- error | ||
space-unary-ops: | ||
- error | ||
- words: true | ||
nonwords: false | ||
overrides: | ||
typeof: false | ||
no-unreachable: | ||
- error | ||
no-global-assign: | ||
- error | ||
no-self-compare: | ||
- error | ||
no-unmodified-loop-condition: | ||
- error | ||
no-constant-condition: | ||
- error | ||
- checkLoops: false | ||
no-console: | ||
- off | ||
no-useless-concat: | ||
- error | ||
no-useless-escape: | ||
- error | ||
no-shadow-restricted-names: | ||
- error | ||
no-use-before-define: | ||
- error | ||
- functions: false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"esversion": 6, | ||
"node": true | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Graph in constructor is represented like an array: | ||
[[n, m], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not good idea to put n and m as first pair to array |
||
[a, b], | ||
[c, d], | ||
... ] | ||
where n - number of vertexes, m - number of edges | ||
pairs [a, b], [c, d] - edges (a, b, c, d, ... - numbers of each vertex) | ||
*/ | ||
|
||
'use strict'; | ||
|
||
class DirectedGraph { | ||
constructor(graph) { | ||
let i; | ||
if (!checkGraphForm(graph)) throw new Error('Not a graph!'); | ||
this.vertexNum = graph[0][0]; | ||
this.edgesNum = graph[0][1]; | ||
this.edges = []; | ||
for (i = 1; i <= this.edgesNum; i++) { | ||
this.edges.push(graph[i]); | ||
} | ||
} | ||
output() { | ||
let i; | ||
console.dir('Number of vertexes: ' + this.vertexNum); | ||
console.dir('Number of edges: ' + this.edgesNum); | ||
for (i = 0; i < this.edgesNum; i++) { | ||
console.dir('Edge ' + (i + 1) + ': ' + this.edges[i][0] + ' -> ' + this.edges[i][1]); | ||
} | ||
} | ||
incidence() { | ||
let i; | ||
let j; | ||
let matrix = []; | ||
for (i = 0; i < this.vertexNum; i++) { | ||
matrix.push([]); | ||
for (j = 0; j < this.edgesNum; j++) { | ||
matrix[i].push(0); | ||
} | ||
} | ||
for (j = 0; j < this.edgesNum; j++) { | ||
matrix[this.edges[j][0] - 1][j] = 1; | ||
matrix[this.edges[j][1] - 1][j] = -1; | ||
if (this.edges[j][0] === this.edges[j][1]) matrix[this.edges[j][0] - 1][j] = 2; | ||
} | ||
return matrix; | ||
} | ||
adjacency() { | ||
let i; | ||
let j; | ||
let matrix = []; | ||
for (i = 0; i < this.vertexNum; i++) { | ||
matrix.push([]); | ||
for (j = 0; j < this.vertexNum; j++) { | ||
matrix[i].push(0); | ||
} | ||
} | ||
for (j = 0; j < this.edgesNum; j++) { | ||
matrix[this.edges[j][0] - 1][this.edges[j][1] - 1] = 1; | ||
} | ||
return matrix; | ||
} | ||
} | ||
|
||
function checkGraphForm(graph) { | ||
let i; | ||
if (graph instanceof Array && graph.length > 0) { | ||
for (i = 0; i < graph.length; i++) { | ||
if (graph[i].length !== 2 || !(graph[i] instanceof Array)) return false; | ||
} | ||
} else { | ||
return false; | ||
} | ||
for (i = 1; i < graph.length; i++) { | ||
if (graph[i][0] > graph[0][0] || graph[i][1] > graph[0][0]) return false; | ||
if (graph[i][0] < 1 || graph[i][1] < 1) return false; | ||
if (graph.length !== graph[0][1] + 1) return false; | ||
} | ||
return true; | ||
} | ||
|
||
try { | ||
const graph = new DirectedGraph([[5, 7], [2, 1], [5, 2], [4, 1], [1, 3], [5, 1], [3, 4], [3, 3]]); | ||
graph.output(); | ||
console.dir(graph.incidence()); | ||
console.dir(graph.adjacency()); | ||
} catch (E) { | ||
console.dir(E.message); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you use try/except here? Throw error will print message to console, why do you use console.dir instead? |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
'use strict'; | ||
|
||
function DirectedGraph() { | ||
this.edges = []; | ||
this.vertexes = new Map(); | ||
} | ||
|
||
DirectedGraph.prototype.addVertex = function(data) { | ||
if (data === undefined) return false; | ||
this.vertexes.set(this.vertexes.size + 1, data); | ||
return true; | ||
}; | ||
|
||
DirectedGraph.prototype.deleteVertex = function(vertexNum) { | ||
const data = this.vertexes.get(vertexNum); | ||
this.vertexes.delete(vertexNum); | ||
return data; | ||
}; | ||
|
||
DirectedGraph.prototype.getVertex = function(vertexNum) { | ||
const data = this.vertexes.get(vertexNum); | ||
return data; | ||
}; | ||
|
||
DirectedGraph.prototype.addEdge = function(vertexNum1, vertexNum2) { | ||
if (this.vertexes.get(vertexNum1) === undefined) return false; | ||
if (this.vertexes.get(vertexNum2) === undefined) return false; | ||
this.edges.push([vertexNum1, vertexNum2]); | ||
return true; | ||
}; | ||
|
||
DirectedGraph.prototype.deleteEdge = function(edgeNum) { | ||
if (typeof edgeNum !== 'number') return false; | ||
if (edgeNum < 1 || edgeNum > this.edges.length) return false; | ||
this.edges.splice(edgeNum - 1, 1); | ||
return true; | ||
}; | ||
|
||
DirectedGraph.prototype.getEdge = function(edgeNum) { | ||
if (typeof edgeNum !== 'number') return undefined; | ||
if (edgeNum < 1 || edgeNum > this.edges.length) return undefined; | ||
return this.edges[edgeNum - 1]; | ||
}; | ||
|
||
DirectedGraph.prototype.outputAll = function() { | ||
console.dir('Vertexes:'); | ||
for (var [key, value] of this.vertexes) { | ||
console.dir(key + ' : ' + value); | ||
} | ||
console.dir('Edges:'); | ||
for (var i = 0; i < this.edges.length; i++) { | ||
console.dir(this.edges[i][0] + ' -> ' + this.edges[i][1]); | ||
} | ||
}; | ||
|
||
DirectedGraph.prototype.incidence = function() { | ||
let i; | ||
let j; | ||
let matrix = []; | ||
for (i = 0; i < this.vertexes.size; i++) { | ||
matrix.push([]); | ||
for (j = 0; j < this.edges.length; j++) { | ||
matrix[i].push(0); | ||
} | ||
} | ||
for (j = 0; j < this.edges.length; j++) { | ||
matrix[this.edges[j][0] - 1][j] = 1; | ||
matrix[this.edges[j][1] - 1][j] = -1; | ||
if (this.edges[j][0] === this.edges[j][1]) { | ||
matrix[this.edges[j][0] - 1][j] = 2; | ||
} | ||
} | ||
return matrix; | ||
}; | ||
|
||
DirectedGraph.prototype.adjacency = function() { | ||
let i; | ||
let j; | ||
let matrix = []; | ||
for (i = 0; i < this.vertexes.size; i++) { | ||
matrix.push([]); | ||
for (j = 0; j < this.vertexes.size; j++) { | ||
matrix[i].push(0); | ||
} | ||
} | ||
for (j = 0; j < this.edges.length; j++) { | ||
matrix[this.edges[j][0] - 1][this.edges[j][1] - 1] = 1; | ||
} | ||
return matrix; | ||
}; | ||
|
||
|
||
const graph = new DirectedGraph(); | ||
|
||
console.dir(graph.addVertex(null)); | ||
console.dir(graph.addVertex('111')); | ||
console.dir(graph.addEdge(1, 2)); | ||
graph.outputAll(); | ||
console.dir(graph.incidence()); | ||
console.dir(graph.adjacency()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add newline at the end of file