-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraphEdge.js
More file actions
41 lines (40 loc) · 886 Bytes
/
GraphEdge.js
File metadata and controls
41 lines (40 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import GraphVertex from './GraphVertex'
export default class GraphEdge {
/**
* 图的边
* @param {GraphVertex} startVertex
* @param {GraphVertex} endVertex
* @param {number} weight
*/
constructor(startVertex, endVertex, weight = 0) {
// 边的始点
this.startVertex = startVertex
// 边的终点
this.endVertex = endVertex
// 边的权重
this.weight = weight
}
/**
* 获得边的顶点
* @returns {srting}
*/
getKey() {
const startVertex = this.startVertex.getKey()
const endVertex = this.endVertex.getKey()
return `${startVertex}_${endVertex}`
}
/**
* 反转边的始点与终点
* @returns {GraphEdge}
*/
reverse() {
;[this.startVertex, this.endVertex] = [this.endVertex, this.startVertex]
return this
}
/**
* @returns {string}
*/
toString() {
return this.getKey()
}
}