47 lines
2.1 KiB
Markdown
47 lines
2.1 KiB
Markdown
---
|
||
title: Graphs
|
||
localeTitle: 图表
|
||
---
|
||
## 图表
|
||
|
||
图表是可用于解决路由问题的数据结构,例如“这两个组件是否已连接?”和“从a点到b点的最短路径是什么?”
|
||
|
||
图由节点和边组成。 节点(即顶点)是图形中的对象。 节点可以包含诸如节点名称和附加到哪些边缘之类的信息。 边是连接两个节点的链接。 边缘可以包含诸如边缘的重量之类的信息。 如果两个节点通过边连接,则它们是邻居(即相邻)。
|
||
|
||
根据问题,您可以使用双向(无向)或单向(定向)边。 如果你有一个从a到b的无向边,那么从b到a也有一种方法。 如果从a到b有一个有向边,则b和a之间不一定有边。
|
||
|
||
您可以使用图表来制定以下情况:
|
||
|
||
* 地理地图
|
||
* 您所在国家/地区的每个城市都是节点
|
||
* 如果两个城市通过公路连接,则它们之间存在边缘 \*道路可以是单向或双向(有向和无向边) \*重量可以是道路的长度
|
||
* 水流
|
||
* 每个闸门都是一个节点
|
||
* 每条运河都是边缘
|
||
* 水只会沿一个方向流动,因此边缘是直的
|
||
* 重量可以是流量的最大水容量
|
||
|
||
示例:一个图表,其中包含北欧国家首都的节点,以及(无向)边缘到通过直接道路连接的城市的行车距离。
|
||
```
|
||
. +---------+
|
||
. |Reykjavik|
|
||
. +---------+
|
||
.
|
||
.
|
||
. 529 km +---------+ 1760 km +--------+
|
||
. +------------+|Stockholm|+---------+|Helsinki|
|
||
. | +---------+ +--------+
|
||
. + +
|
||
. +----+ 1991 km |
|
||
. |Oslo|+-------------------------------------+
|
||
. +----+
|
||
. +----------+
|
||
. |Copenhagen|
|
||
. +----------+
|
||
```
|
||
|
||
#### 更多信息:
|
||
|
||
[广度优先搜索(BFS)](https://github.com/freecodecamp/guides/tree/master/src/pages/algorithms/graph-algorithms/breadth-first-search/index.md)
|
||
|
||
[深度优先搜索(DFS)](https://github.com/freecodecamp/guides/tree/master/src/pages/algorithms/graph-algorithms/depth-first-search/index.md) |