freeCodeCamp/curriculum/challenges/chinese/08-coding-interview-prep/data-structures/incidence-matrix.chinese.md

3.8 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d8256367417b2b2512c79 Incidence Matrix 1 发生率矩阵

Description

表示图形的另一种方式是将其置于关联矩阵中。 入射矩阵是二维2D阵列。一般而言关联矩阵在其两个维度之间涉及两个不同类别的对象。这种矩阵类似于邻接矩阵。但是行和列在这里意味着其他东西。在图表中我们有边缘和节点。这些将是我们的“两类不同的对象”。该矩阵将使行为节点列为边。这意味着我们可以拥有不均匀的行数和列数。每列将代表一个独特的边缘。此外每个边连接两个节点。要显示两个节点之间存在边缘您将在特定列的两行中放置1。下面是一个3节点图节点1和节点3之间有一条边。
1
---
1 | 1
2 | 0
3 | 1
以下是具有4个边和4个节点的incidence matrix的示例。请记住,列是边,行是节点本身。
1 2 3 4
--------
1 | 0 1 1 1
2 | 1 1 0 0
3 | 1 0 0 1
4 | 0 0 1 0
下面是同一件事的JavaScript实现。
var incMat = [
[0,1,1,1]
[1,1,0,0]
[1,0,0,1]
[0,0,1,0]
]。
要制作有向图,请使用-1表示离开特定节点的边,使用1作为边进入节点。
var incMatDirected = [
[0-1,1-1]
[-1,1,0,0]
[1,0,0,1]
[0,0-1,0]
]。
图形的边缘也可以有权 。到目前为止,我们有未加权的边缘,只有存在和缺少边是二进制( 01 。根据您的应用您可以拥有不同的重量。不同的权重表示为大于1的数字。

Instructions

创建具有五个节点和四个边的无向图的关联矩阵。该矩阵应该是多维数组。这五个节点在关系之后具有关系。第一边缘在第一和第二节点之间。第二个边缘位于第二个和第三个节点之间。第三个边缘位于第三个和第五个节点之间。并且四个边缘在第四和第二节点之间。所有边权重均为1边缘顺序很重要。

Tests

tests:
  - text: <code>incMatUndirected</code>应该只包含五个节点。
    testString: 'assert((incMatUndirected.length === 5) && incMatUndirected.map(function(x) { return x.length === 4 }).reduce(function(a, b) { return a && b }) , "<code>incMatUndirected</code> should only contain five nodes.");'
  - text: 第一个和第二个节点之间应该有第一条边。
    testString: 'assert((incMatUndirected[0][0] === 1) && (incMatUndirected[1][0] === 1), "There should be a first edge between the first and second node.");'
  - text: 第二个和第三个节点之间应该有第二条边。
    testString: 'assert((incMatUndirected[1][1] === 1) && (incMatUndirected[2][1] === 1), "There should be a second edge between the second and third node.");'
  - text: 第三个和第五个节点之间应该有第三条边。
    testString: 'assert((incMatUndirected[2][2] === 1) && (incMatUndirected[4][2] === 1), "There should be a third edge between the third and fifth node.");'
  - text: 第二个和第四个节点之间应该有第四条边。
    testString: 'assert((incMatUndirected[1][3] === 1) && (incMatUndirected[3][3] === 1), "There should be a fourth edge between the second and fourth node.");'

Challenge Seed

var incMatUndirected = [

];

Solution

// solution required