带你读《图解算法小抄》七、图(4)https://developer.aliyun.com/article/1348214?groupCode=tech_library
GraphVertex
export default class GraphVertex { /** * @param {*} value */ // 构造函数,参数为顶点的值 constructor(value) { if (value === undefined) { throw new Error('图顶点必须有一个值'); } /** * @param {GraphEdge} edgeA * @param {GraphEdge} edgeB */ // 边的比较函数 const edgeComparator = (edgeA, edgeB) => { if (edgeA.getKey() === edgeB.getKey()) { return 0; } return edgeA.getKey() < edgeB.getKey() ? -1 : 1; }; // 通常你会储存一个字符串作为顶点的名称,但实际上可以储存任何类型的对象 this.value = value; this.edges = new LinkedList(edgeComparator); } /** * @param {GraphEdge} edge * @returns {GraphVertex} */ // 添加边 addEdge(edge) { this.edges.append(edge); return this; } /** * @param {GraphEdge} edge */ // 删除边 deleteEdge(edge) { this.edges.delete(edge); } /** * @returns {GraphVertex[]} */ // 获取邻居顶点 getNeighbors() { const edges = this.edges.toArray(); /** @param {LinkedListNode} node */ // 邻居转换器 const neighborsConverter = (node) => { return node.value.startVertex === this ? node.value.endVertex : node.value.startVertex; }; // 返回起始或者结束顶点 // 对于无向图,当前顶点可能是结束顶点 return edges.map(neighborsConverter); } /** * @return {GraphEdge[]} */ // 获取边 getEdges() { return this.edges.toArray().map((linkedListNode) => linkedListNode.value); } /** * @return {number} */ // 获取顶点的度(相连的边的数量) getDegree() { return this.edges.toArray().length; } /** * @param {GraphEdge} requiredEdge * @returns {boolean} */ // 检查顶点是否有指定的边 hasEdge(requiredEdge) { const edgeNode = this.edges.find({ callback: (edge) => edge === requiredEdge, }); return !!edgeNode; } /** * @param {GraphVertex} vertex * @returns {boolean} */ // 检查顶点是否有指定的邻居 hasNeighbor(vertex) { const vertexNode = this.edges.find({ callback: (edge) => edge.startVertex === vertex || edge.endVertex === vertex, }); return !!vertexNode; } /** * @param {GraphVertex} vertex * @returns {(GraphEdge|null)} */ // 查找与指定顶点相连的边 findEdge(vertex) { const edgeFinder = (edge) => { return edge.startVertex === vertex || edge.endVertex === vertex; }; const edge = this.edges.find({ callback: edgeFinder }); return edge ? edge.value : null; } /** * @returns {string} */ // 获取顶点的键(即顶点的值) getKey() { return this.value; } /** * @return {GraphVertex} */ // 删除所有的边 deleteAllEdges() { this.getEdges().forEach((edge) => this.deleteEdge(edge)); return this; } /** * @param {function} [callback] * @returns {string} */ // 将顶点转换为字符串 toString(callback) { return callback ? callback(this.value) : `${this.value}`; }}
2. 参考
∙ Introduction to Graphs on YouTube
∙ Graphs representation on YouTube