Last active
August 29, 2015 14:09
-
-
Save spellancer/c5ba18d4cdb046eee4ce to your computer and use it in GitHub Desktop.
lab3.js
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 pretty(input) { | |
return JSON.stringify(input, null, '\t'); | |
} | |
function pp(input) { | |
console.log(pretty(input)); | |
} | |
function output(input) { | |
$('.output').append('<p>' + input + '</p>'); | |
} | |
function copy(input) { | |
return JSON.parse(JSON.stringify(input)); | |
} | |
function printMatr(input, title) { | |
var result = '<h3 class="title">' + title + '</h3> ', | |
N = input.length; | |
for (var i = 1; i <= N; i++) { | |
result += '<span style="color:#999;display:inline-block;transform:rotate(-45deg);padding-bottom:4px">' + i + '</span> '; | |
if (i < 10) { | |
result += ' '; | |
} | |
} | |
result += '<br>' | |
input.forEach(function (row, i) { | |
result += '<span style="color:#999">' + (i+1) + '</span> '; | |
if (i < 9) { | |
result += ' '; | |
} | |
row.forEach(function (el) { | |
result += el + ' '; | |
if (el < 10) { | |
result += ' '; | |
} | |
if (el.length == 2) { | |
result += ' '; | |
} | |
if (el.length == 3) { | |
result += ' '; | |
} | |
}); | |
result += '<br>'; | |
}); | |
output(result); | |
} | |
function logicSum(a,b) { | |
if (a == 0 || a == 1) { | |
return b; | |
} | |
if (b == 0 || b == 1) { | |
return a; | |
} | |
return (a + b); | |
} | |
function logicMultiplication(a,b) { | |
if (a == 0 || b == 0) { | |
return 0; | |
} | |
if (a == 1) { | |
return b; | |
} | |
if (b == 1) { | |
return a; | |
} | |
return (a + b); | |
} | |
function compute(tree) { | |
/* | |
Расчет матрицы следования | |
*/ | |
var N = Object.keys(tree).length; | |
var matr = []; | |
for (var i = 1; i <= N; i++) { | |
var current = tree[i], | |
row = [], | |
conditionState = true; | |
for (var j = 0; j < N; j++) { | |
if (current[j + 1]) { | |
if (current.condition) { | |
row[j] = conditionState ? (i + 'T') : (i + 'F'); | |
conditionState = false; | |
} else { | |
row[j] = 1; | |
} | |
} else { | |
row[j] = 0; | |
} | |
} | |
matr.push(row); | |
} | |
matr = _.zip.apply(_, matr); | |
printMatr(matr, "Матрица следования: "); | |
matr = _.zip.apply(_, matr); | |
/* | |
Расчет матрицы следования с указанием весов | |
*/ | |
var values = []; | |
for (var i = 1; i <= N; i++) { | |
values.push(tree[i].value); | |
} | |
matr.push(values); | |
// Траспонирование матрицы | |
matr = _.zip.apply(_, matr); | |
printMatr(matr, "Матрица следования с указанием весов: "); | |
/* | |
Расчет матрицы следования с указанием транзитивных связей | |
*/ | |
// Извлечение последнего столбца(столбца с весами элементов) | |
matr = _.zip.apply(_, matr); | |
matr.pop(); | |
matr = _.zip.apply(_, matr); | |
for (var i = 0; i < N; i++) { | |
for (var j = 0; j < N; j++) { | |
if (matr[i][j] != 0) { | |
for (k = 0; k < j; k++) { | |
matr[i][k] = logicSum(logicMultiplication(matr[j][k], matr[i][j]), matr[i][k]); | |
} | |
} | |
} | |
} | |
printMatr(matr, "Матрица следования с транзитивными связями: "); | |
} | |
$(function () { | |
var tree = {1: {4: 3, 5: 2, value: 2}, 2: {6: 1, 7: 5, condition:[2.1,2.2], value: 3}, 3: {12: 6, value: 4}, | |
4: {8: 2, 9: 3, condition:[4.1,4.2], value: 3}, 5: {value: 2}, 6: {10: 4, 11: 5, value: 1}, 7: {12: 6, value: 5}, | |
8: {13: 1, 14: 3, value: 2}, 9: {value: 3}, 10: {15: 2, 16: 1, condition:[10.1,10.2], value: 4}, 11: {17: 4, value: 5}, | |
12: {18: 3, value: 6}, 13: {value: 1}, 14: {19: 3, 20: 4, condition[14.1,14.2], value: 3}, 15: {value: 2}, 16: {value: 1}, | |
17:{value: 4}, 18:{value: 3}, 19: {21: 7, value: 3}, 20: {22: 5, value: 4}, 21: {value: 7}, 22: {value: 5}} | |
compute(tree); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment