## 语法

``A = adjacency(G)``
``A = adjacency(G,'weighted')``
``A = adjacency(G,weights)``

## 说明

``A = adjacency(G)` 返回图 `G` 的稀疏邻接矩阵。如果 `(i,j)` 是 `G` 中的一条边，则 `A(i,j) = 1`。否则，`A(i,j) = 0`。`
``A = adjacency(G,'weighted')` 返回加权邻接矩阵，其中对于每条边 `(i,j)`，值 `A(i,j)` 包含边的权重。如果图没有边权重，则 `A(i,j)` 设置为 1。对于此语法，`G` 必须是简单图，从而使 `ismultigraph(G)` 返回 `false`。`
``A = adjacency(G,weights)` 返回加权邻接矩阵，其边权重由向量 `weights` 指定。对于 `G` 中的每条边 `(i,j)`，邻接矩阵的值 `A(i,j) = weights(findedge(G,i,j))`。对于此语法，`G` 必须是简单图，从而使 `ismultigraph(G)` 返回 `false`。`

## 示例

```s = [1 1 1 2 2 3]; t = [2 3 4 5 6 7]; G = digraph(s,t)```
```G = digraph with properties: Edges: [6x1 table] Nodes: [7x0 table] ```
`A = adjacency(G)`
```A = (1,2) 1 (1,3) 1 (1,4) 1 (2,5) 1 (2,6) 1 (3,7) 1 ```

`A = [0 5 3 0;0 0 1 2; 0 0 0 11; 0 0 0 0]`
```A = 4×4 0 5 3 0 0 0 1 2 0 0 0 11 0 0 0 0 ```
`G = graph(A,'upper')`
```G = graph with properties: Edges: [5x2 table] Nodes: [4x0 table] ```
`G.Edges`
```ans=5×2 table EndNodes Weight ________ ______ 1 2 5 1 3 3 2 3 1 2 4 2 3 4 11 ```

`B = adjacency(G)`
```B = (2,1) 1 (3,1) 1 (1,2) 1 (3,2) 1 (4,2) 1 (1,3) 1 (2,3) 1 (4,3) 1 (2,4) 1 (3,4) 1 ```

```G = digraph([1 1 1 2 3 4],[2 3 4 4 2 3],[5 6 7 8 9 10]); G.Edges```
```ans=6×2 table EndNodes Weight ________ ______ 1 2 5 1 3 6 1 4 7 2 4 8 3 2 9 4 3 10 ```

`A = adjacency(G)`
```A = (1,2) 1 (3,2) 1 (1,3) 1 (4,3) 1 (1,4) 1 (2,4) 1 ```

`A = adjacency(G,'weighted')`
```A = (1,2) 5 (3,2) 9 (1,3) 6 (4,3) 10 (1,4) 7 (2,4) 8 ```

`B = full(A)`
```B = 4×4 0 5 6 7 0 0 0 8 0 9 0 0 0 0 10 0 ```

## 提示

• `adjacency` 返回的稀疏邻接矩阵中，权重为零的边不可见。这意味着加权邻接矩阵仅在没有权重为零的边时才能表示加权图。