1Here is an example of an
---
1 | 1
2 | 0
3 | 1
incidence matrix with 4 edges and 4 nodes. Remember, the columns are the edges and rows are the nodes themselves.
1 2 3 4Below is a JavaScript implementation of the same thing. ```js var incMat = [ [0, 1, 1, 1], [1, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 0] ]; ``` To make a directed graph, use
--------
1 | 0 1 1 1
2 | 1 1 0 0
3 | 1 0 0 1
4 | 0 0 1 0
-1 for an edge leaving a particular node and 1 for an edge entering a node.
```js
var incMatDirected = [
[ 0, -1, 1, -1],
[-1, 1, 0, 0],
[ 1, 0, 0, 1],
[ 0, 0, -1, 0]
];
```
Graphs can also have weights on their edges. So far, we have unweighted edges where just the presence and lack of edge is binary (0 or 1). You can have different weights depending on your application. A different weight is represented as numbers greater than 1.
incMatUndirected should only contain five nodes.
testString: assert((incMatUndirected.length === 5) && incMatUndirected.map(function(x) { return x.length === 4 }).reduce(function(a, b) { return a && b }) , 'incMatUndirected should only contain five nodes.');
- text: There should be a first edge between the first and second node.
testString: assert((incMatUndirected[0][0] === 1) && (incMatUndirected[1][0] === 1), 'There should be a first edge between the first and second node.');
- text: There should be a second edge between the second and third node.
testString: assert((incMatUndirected[1][1] === 1) && (incMatUndirected[2][1] === 1), 'There should be a second edge between the second and third node.');
- text: There should be a third edge between the third and fifth node.
testString: assert((incMatUndirected[2][2] === 1) && (incMatUndirected[4][2] === 1), 'There should be a third edge between the third and fifth node.');
- text: There should be a fourth edge between the second and fourth node.
testString: assert((incMatUndirected[1][3] === 1) && (incMatUndirected[3][3] === 1), 'There should be a fourth edge between the second and fourth node.');
```