Skip to content

Commit 0918c8d

Browse files
committed
Added adjacency matrix output.
1 parent ead7f54 commit 0918c8d

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Graph Creator",
3-
"version": "0.1.2",
3+
"version": "0.2.2",
44
"main": "index.html",
55
"window": {
66
"height": 1024,

src/fileWrite.js

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Handles the writing of multiline adjacency lists after the graph is completed.
33
Also handles general file IO.
44
*/
55
var fs = require('fs'),
6-
pathMod = require('path'),
76
dialog = require('nw-dialog');
87
//**************************************************************************
98
//WRITE GRAPHS
@@ -38,7 +37,7 @@ function makeAdjlist(nodeList){
3837
return adjlist;
3938
}
4039

41-
function writeGraph(){
40+
function writeGraphAdjlist(){
4241
/*
4342
Writes a multiline adjlist of the user made graph to the specified filename.
4443
*/
@@ -53,6 +52,65 @@ function writeGraph(){
5352
});
5453
}
5554

55+
function makeMatrix(nodeList){
56+
/*
57+
Given a list of nodes in a graph, creates an adjacency matrix.
58+
Note that the dimensions of the matrix are the first line of the file
59+
and are formatted n,n.
60+
61+
INPUT
62+
nodeList: A list of nodes in the user's graph
63+
64+
OUTPUT
65+
matrix: A string with the first line being the dimensions of a matrix,
66+
and then a jagged array to represent the matrix.
67+
*/
68+
69+
var matrix = nodeList.length.toString() + ',' + nodeList.length.toString()
70+
+ '\r\n';
71+
matrix = matrix + '[';
72+
73+
for(var i=0; i<nodeList.length; i++){
74+
console.log('here');
75+
var edges = nodes[i].adjacencies;
76+
77+
//Define our row and fill it with zeros
78+
var matrixRow = new Array(nodeList.length);
79+
matrixRow.fill(0);
80+
81+
//Increment every index which has an edge by one
82+
for(var j=0; j<edges.length; j++){
83+
matrixRow[edges[j].index]++;
84+
}
85+
86+
matrix = matrix + '[' + matrixRow.toString() + ']' + ',\r\n';
87+
}
88+
89+
//Remove the last ',\r\n'
90+
matrix = matrix.split(',\r\n');
91+
matrix.pop();
92+
matrix = matrix.join(',\r\n');
93+
94+
matrix = matrix + ']';
95+
96+
return matrix;
97+
}
98+
99+
function writeGraphMatrix(){
100+
/*
101+
Writes an adjacency matrix for the usermade graph to the specified filename.
102+
*/
103+
104+
dialog.setContext(document);
105+
dialog.saveFileDialog(function(fileName){
106+
var matrixFile = makeMatrix(nodes);
107+
108+
fs.writeFile(fileName, matrixFile, function(err){
109+
if(err) throw err;
110+
});
111+
});
112+
}
113+
56114
//**************************************************************************
57115
//WRITE POSITIONS
58116
//**************************************************************************

src/menu.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ menu.append(new nw.MenuItem({
1515
var graphSubmenu = new nw.Menu();
1616
graphSubmenu.append(new nw.MenuItem({
1717
label: 'Save as Multiline Adjlist',
18-
click: writeGraph
18+
click: writeGraphAdjlist
19+
}));
20+
21+
graphSubmenu.append(new nw.MenuItem({
22+
label: 'Save as Adjacency Matrix',
23+
click: writeGraphMatrix
1924
}));
2025

2126
menu.append(new nw.MenuItem({

0 commit comments

Comments
 (0)