From 7ca5562ee3355e3051600db7f6508a7dafcd5205 Mon Sep 17 00:00:00 2001 From: volkov-ilya-ip51 Date: Fri, 10 Mar 2017 18:13:10 +0200 Subject: [PATCH 1/4] Creating empty repository --- .eslintrc.yml | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++ .jshintrc | 4 ++ 2 files changed, 143 insertions(+) create mode 100644 .eslintrc.yml create mode 100644 .jshintrc diff --git a/.eslintrc.yml b/.eslintrc.yml new file mode 100644 index 0000000..ff4265f --- /dev/null +++ b/.eslintrc.yml @@ -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 + - windows + 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 diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..85ceb24 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,4 @@ +{ + "esversion": 6, + "node": true +} \ No newline at end of file From c8b41778ab3904457e78e94f1b92c226cd797bec Mon Sep 17 00:00:00 2001 From: volkov-ilya-ip51 Date: Sat, 11 Mar 2017 10:32:39 +0200 Subject: [PATCH 2/4] graph realisation added --- .eslintrc.yml | 2 +- JavaScript/1-DirectedGraph.js | 84 +++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 JavaScript/1-DirectedGraph.js diff --git a/.eslintrc.yml b/.eslintrc.yml index ff4265f..00e6090 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -15,7 +15,7 @@ rules: MemberExpression: 1 linebreak-style: - error - - windows + - linux quotes: - error - single diff --git a/JavaScript/1-DirectedGraph.js b/JavaScript/1-DirectedGraph.js new file mode 100644 index 0000000..3dc353c --- /dev/null +++ b/JavaScript/1-DirectedGraph.js @@ -0,0 +1,84 @@ +/* + Graph in constructor is represented like an array: + [[n, m], + [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) { + if (!checkGraphForm(graph)) throw new Error('Not a graph!'); + this.vertexNum = graph[0][0]; + this.edgesNum = graph[0][1]; + this.edges = []; + for (var i = 1; i <= this.edgesNum; i++) { + this.edges.push(graph[i]); + } + } + Output() { + console.dir('Number of vertexes: ' + this.vertexNum); + console.dir('Number of edges: ' + this.edgesNum); + for (var i = 0; i < this.edgesNum; i++) { + console.dir('Edge ' + (i+1) + ': ' + this.edges[i][0] + ' -> ' + this.edges[i][1]); + } + } + Incidence() { + let matrix = []; + for (var i = 0; i < this.vertexNum; i++) { + matrix.push([]); + for (var j = 0; j < this.edgesNum; j++) { + matrix[i].push(0); + } + } + for (var 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 matrix = []; + for (var i = 0; i < this.vertexNum; i++) { + matrix.push([]); + for (var j = 0; j < this.vertexNum; j++) { + matrix[i].push(0); + } + } + for (var j = 0; j < this.edgesNum; j++) { + matrix[this.edges[j][0] - 1][this.edges[j][1] - 1] = 1; + } + return matrix; + } +} + +function checkGraphForm(graph) { + if (graph instanceof Array && graph.length > 0) { + for (var i = 0; i < graph.length; i++) { + if (graph[i].length != 2 || !graph[i] instanceof Array) return false; + } + } + else { + return false; + } + for (var 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); +} \ No newline at end of file From 1530f4b7d2d54b5fa0a9b46df5dbbfb8da82d790 Mon Sep 17 00:00:00 2001 From: volkov-ilya-ip51 Date: Sat, 11 Mar 2017 10:53:35 +0200 Subject: [PATCH 3/4] Linter loves my program --- .eslintrc.yml | 2 +- JavaScript/1-DirectedGraph.js | 72 +++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index 00e6090..ddc22cb 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -15,7 +15,7 @@ rules: MemberExpression: 1 linebreak-style: - error - - linux + - unix quotes: - error - single diff --git a/JavaScript/1-DirectedGraph.js b/JavaScript/1-DirectedGraph.js index 3dc353c..9e383b0 100644 --- a/JavaScript/1-DirectedGraph.js +++ b/JavaScript/1-DirectedGraph.js @@ -11,46 +11,52 @@ 'use strict'; class DirectedGraph { - constructor(graph) { - if (!checkGraphForm(graph)) throw new Error('Not a graph!'); + 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 (var i = 1; i <= this.edgesNum; i++) { + for (i = 1; i <= this.edgesNum; i++) { this.edges.push(graph[i]); } - } - Output() { + } + output() { + let i; console.dir('Number of vertexes: ' + this.vertexNum); console.dir('Number of edges: ' + this.edgesNum); - for (var i = 0; i < this.edgesNum; i++) { - console.dir('Edge ' + (i+1) + ': ' + this.edges[i][0] + ' -> ' + this.edges[i][1]); + for (i = 0; i < this.edgesNum; i++) { + console.dir('Edge ' + (i + 1) + ': ' + this.edges[i][0] + ' -> ' + this.edges[i][1]); } } - Incidence() { + incidence() { + let i; + let j; let matrix = []; - for (var i = 0; i < this.vertexNum; i++) { + for (i = 0; i < this.vertexNum; i++) { matrix.push([]); - for (var j = 0; j < this.edgesNum; j++) { + for (j = 0; j < this.edgesNum; j++) { matrix[i].push(0); } } - for (var j = 0; j < this.edgesNum; j++) { + 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() { + adjacency() { + let i; + let j; let matrix = []; - for (var i = 0; i < this.vertexNum; i++) { + for (i = 0; i < this.vertexNum; i++) { matrix.push([]); - for (var j = 0; j < this.vertexNum; j++) { + for (j = 0; j < this.vertexNum; j++) { matrix[i].push(0); } } - for (var j = 0; j < this.edgesNum; j++) { + for (j = 0; j < this.edgesNum; j++) { matrix[this.edges[j][0] - 1][this.edges[j][1] - 1] = 1; } return matrix; @@ -58,27 +64,27 @@ class DirectedGraph { } function checkGraphForm(graph) { - if (graph instanceof Array && graph.length > 0) { - for (var i = 0; i < graph.length; i++) { - if (graph[i].length != 2 || !graph[i] instanceof Array) return false; - } - } - else { - return false; - } - for (var 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; + 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; } - return true; + } 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()); + 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); -} \ No newline at end of file +} From 421d90900e7b1a640af17a16d21019f6cab2941f Mon Sep 17 00:00:00 2001 From: volkov-ilya-ip51 Date: Sun, 12 Mar 2017 10:47:14 +0200 Subject: [PATCH 4/4] dynamic graph added --- JavaScript/2-DynamicGraph.js | 100 +++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 JavaScript/2-DynamicGraph.js diff --git a/JavaScript/2-DynamicGraph.js b/JavaScript/2-DynamicGraph.js new file mode 100644 index 0000000..aaa95c3 --- /dev/null +++ b/JavaScript/2-DynamicGraph.js @@ -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()); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy