-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeDegree.js
More file actions
40 lines (30 loc) · 758 Bytes
/
nodeDegree.js
File metadata and controls
40 lines (30 loc) · 758 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var nodes = {}
var fs = require('fs')
function applyLink(a, b) {
if (!nodes.hasOwnProperty(a) || nodes[a].constructor != Array) {
nodes[a] = []
}
if (nodes[a].indexOf(b) < 0) {
nodes[a].push(b)
}
if (!nodes.hasOwnProperty(b) || nodes[b].constructor != Array) {
nodes[b] = []
}
if (nodes[b].indexOf(a) < 0) {
nodes[b].push(a)
}
}
var file = fs.openSync('./nodes.txt', 'r')
fs.readFile(file, 'utf8',function(e, d){
if (e) throw (e)
var input = d.split('\n'),
count = parseInt(input[0]),
pairs = input.slice(1, input.length)
pairs.forEach(function(v, i, a){
pair = v.split(' ')
applyLink(pair[0], pair[1])
})
Object.keys(nodes).forEach(function(k, i, o){
console.log('Node '+k+' has a degree of '+nodes[k].length)
})
})