数据结构(七) 图
图结构由顶点和边构成,顶点V(Vertex)表示事物,边E(Edge)表示关系,
边可以有向的或无向的
图的特点
- 顶点
- 边
- 相邻顶点:一条边两头的顶点称为相邻顶点
- 度:一个顶点拥有相邻顶点的数量
- 路径:顶点V1,V2…Vn顶点构成的序列
- 无向图:边没有方向的图
- 有向图:边有方向的图
- 无权图:边带有权重的图
- 有权图:边没有权重的图
图的表示方法
邻接矩阵
邻接矩阵可以用一个二维数组表示顶点之间的连接
- 问题:(稀疏图)当顶点数量多而联系少时,因为二维数组会存储所有点之间的联系,所以会造成存储空间的大量浪费
邻接表
邻接表由图中每个顶点以及和顶点相邻的顶点列表组成
图的遍历
代码实现
class Graph {
constructor() {
this.vertex = []
this.edges = new hashTable()
}
addVertex(vertex) {
this.vertex.push(vertex)
this.edges.put(vertex, [])
}
addEdge(v1, v2) {
//无向图
this.edges.get(v1).push(v2)
this.edges.get(v2).push(v1)
}
initializeColor() {
let colors = {}
for (const v of this.vertex) {
colors[v] = 'white'
}
return colors
}
bfs(handler) {
//广度遍历
//1.初始化顶点颜色
let colors = this.initializeColor()
//2.创建队列
let queue = new Queue()
//3.将顶点加入到队列中
queue.add(this.vertex[0])
//4.遍历队列,当队列为空时退出队列
while (!(queue.size() == 0)) {
//取得队列元素
let vertex = queue.del()
//拿到vertex的所有相邻顶点
let edges = this.edges.get(vertex)
colors[vertex] = 'gary'
//遍历vertex所有的相邻顶点,碰到颜色为白色的顶点将其添加进队列
for (const v of edges) {
if (colors[v] == 'white') {
queue.add(v)
colors[v] = 'gray'
}
}
//打印顶点
handler(vertex)
//将顶点设置为黑色
colors[vertex] = 'black'
}
}
dfs(handler) {
//深度遍历
let colors = this.initializeColor()
this.dfsOrder(this.vertex[0], colors, handler)
}
dfsOrder(initV, colors, handler) {
//拿到顶点的相邻顶点
let edges = this.edges.get(initV)
colors[initV] = 'gray'
//处理顶点
handler(initV)
//处理过后顶点颜色设置为黑色
colors[initV] = 'balck'
//遍历相邻顶点,当顶点为白色时,递归调用自己
for (const v of edges) {
if (colors[v] == 'white') {
this.dfsOrder(v, colors, handler)
}
}
}
toString() {
let str = ''
for (const v of this.vertex) {
str += v + '->'
for (const e of this.edges.get(v)) {
str += e + ' '
}
str += '\n'
}
return str
}
}