1 2 3上面是一个非常简单的无向图,其中有三个节点,第一个节点连接到第二个和第三个节点。 注意 :矩阵顶部和左侧的数字只是节点的标签。下面是同一件事的JavaScript实现。
------
1 | 0 1 1
2 | 1 0 0
3 | 1 0 0
var adjMat = [与邻接列表不同,矩阵的每个“行”必须具有与图中的节点相同数量的元素。这里我们有一个三乘三矩阵,这意味着我们的图中有三个节点。有向图看起来很相似。下面是第一节点具有指向第二节点的边缘,然后第二节点具有指向第三节点的边缘的图。
[0,1,1],
[1,0,0],
[1,0,0]
]。
var adjMatDirected = [图形的边缘也可以有权重 。到目前为止,我们有未加权的边缘,只有存在和缺少边是二进制(
[0,1,0],
[0,0,1],
[0,0,0]
]。
0
或1
)。根据您的应用,您可以拥有不同的重量。 undirectedAdjList
应该只包含五个节点。
testString: 'assert((adjMatUndirected.length === 5) && adjMatUndirected.map(function(x) { return x.length === 5 }).reduce(function(a, b) { return a && b }) , "undirectedAdjList
should only contain five nodes.");'
- text: 第一个和第四个节点之间应该有一条边。
testString: 'assert((adjMatUndirected[0][3] === 1) && (adjMatUndirected[3][0] === 1), "There should be an edge between the first and fourth node.");'
- text: 第一个和第三个节点之间应该有一条边。
testString: 'assert((adjMatUndirected[0][2] === 1) && (adjMatUndirected[2][0] === 1), "There should be an edge between the first and third node.");'
- text: 第三个和第五个节点之间应该有一条边。
testString: 'assert((adjMatUndirected[2][4] === 1) && (adjMatUndirected[4][2] === 1), "There should be an edge between the third and fifth node.");'
- text: 第四个和第五个节点之间应该有一条边。
testString: 'assert((adjMatUndirected[3][4] === 1) && (adjMatUndirected[4][3] === 1), "There should be an edge between the fourth and fifth node.");'
```