Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 | 16x 394x 394x 394x 394x 394x 394x 640x 394x 394x 394x 394x 394x 394x 43x 43x 394x 394x 394x 394x 394x 394x 394x 1020x 394x 850x 394x 21x 21x 394x 394x 6x 2x 4x 6x 394x 394x 394x 394x 394x 1024x 1024x 362x 1x 362x 662x 662x 56x 56x 56x 26x 56x 662x 2648x 662x 662x 394x 11x 4x 394x 394x 37x 1x 394x 88x 67x 67x 46x 394x 36x 36x 394x 37x 36x 36x 36x 3x 1x 2x 35x 27x 35x 35x 35x 35x 35x 35x 35x 394x 16x 16x 13x 13x 11x 2x 3x 1x 2x 1x 394x 53x 53x 394x 111x 111x 394x 22x 1x 21x 394x 5x 3x 2x 394x 13x 13x 13x 13x 18x 12x 13x 6x 3x 13x 4x 7x 7x 1x 7x 4x 7x 13x 8x 5x 2x 5x 5x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 8x 394x 7x 3x 4x 7x 394x 373x 373x 373x 373x 11x 11x 362x 1x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 394x 17x 394x 40x 93x 93x 40x 394x 31x 394x 108x 394x 61x 10x 10x 10x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 10x 394x 1x 394x 394x 55x 55x 54x 1x 394x 83x 83x 82x 1x 394x 35x 1x 34x 16x 394x 394x 12x 12x 10x 2x 394x 12x 12x 10x 2x 394x 6x 394x 394x | import { edgeArgsToId, isFunction } from '../util';
import { GraphEnum } from './enum';
import { decrementOrRemoveEntry, edgeArgsToObj, edgeObjToId, incrementOrInitEntry } from '../util';
import { read, write } from './toJSON';
export interface GraphOption {
/**
* @description Is the graph directed or not
* @description.zh-CN 这个图是否是有向图
* @default true
*/
directed?: boolean;
/**
* @description Is this graph contains more than one graph data
* @description.zh-CN 这个图是否包含多个图
* @default false
*/
multigraph?: boolean;
/**
* @description Is this graph a compound graph;
* @description.zh-CN 这个图是否是复合图(包含嵌套节点的图)
* @default false
*/
compound?: boolean;
}
const defaultOption: GraphOption = {
compound: false,
multigraph: false,
directed: true,
};
export interface DefaultEdgeType<NodeIDType, EdgeType> {
/**
* @description the node where this edge start
* @description.zh-CN 边开始的节点
*/
v: NodeIDType;
/**
* @description the node where this edge end
* @description.zh-CN 边结束的节点
*/
w: NodeIDType;
/**
* @description The name used to distinguish the multilateral relationship between two nodes
* @description.zh-CN 用来区分两点之间的多边关系的名称
*/
name?: string;
/**
* @description The value of the edge
* @description.zh-CN 边的值
*/
value?: EdgeType;
}
type EdgeID = string;
export default class Graph<
NodeIDType = string,
NodeType = Record<string, any>,
EdgeType = Record<string, any>,
GraphType = string,
> {
// Graph option or basic props
private directed: boolean = true;
private multigraph: boolean = false;
private compound: boolean = false;
private GRAPH_NODE = GraphEnum.GRAPH_NODE as unknown as NodeIDType;
/**
* @description Label for this graph itself
* @description.zh-CN 图本身的标签(label)
* @default undefined
*/
label?: GraphType;
/**
* @description Number of nodes in the graph
* @description.zh-CN 节点的数量
* @default 0
*/
private nodeCountNum = 0;
/**
* @description Number of edges in the graph
* @description.zh-CN 节点的数量
* @default 0
*/
private edgeCountNum = 0;
/**
* @description return node label with its id
* @description.zh-CN 返回节点的默认的标签
*/
private defaultNodeLabelFn: (v: NodeIDType) => NodeType | undefined = () => undefined;
/**
* @description return edge label with its id
* @description.zh-CN 返回边的默认的标签
*/
private defaultEdgeLabelFn: (
v: NodeIDType,
w: NodeIDType,
name?: string,
) => EdgeType | undefined = () => undefined;
constructor(options: GraphOption = {}) {
const resultOptions = {
...defaultOption,
...options,
};
this.compound = resultOptions.compound!;
this.directed = resultOptions.directed!;
this.multigraph = resultOptions.multigraph!;
if (this.compound) {
this.parentMap = new Map();
this.childrenMap = new Map();
}
}
// Map for graph
/**
* @description Map for parent relationship
* @description.zh-CN 父子关系的映射
*/
private parentMap?: Map<NodeIDType, NodeIDType>;
/**
* @description Map for children relationship
* @description.zh-CN 子孙关系的映射
*/
private childrenMap?: Map<NodeIDType, Map<NodeIDType, boolean>>;
private nodesLabelMap = new Map<NodeIDType, NodeType | undefined>();
/**
* @description Map for edges
* @description.zh-CN 边的映射
*/
private inEdgesMap = new Map<NodeIDType, Map<EdgeID, DefaultEdgeType<NodeIDType, EdgeType>>>();
private outEdgesMap = new Map<NodeIDType, Map<EdgeID, DefaultEdgeType<NodeIDType, EdgeType>>>();
/**
* @description Map for predecessors
* @description.zh-CN 前驱节点的映射
*/
private predecessorsMap = new Map<NodeIDType, Map<NodeIDType, number>>();
/**
* @description Map for successors
* @description.zh-CN 后继节点的映射
*/
private successorsMap = new Map<NodeIDType, Map<NodeIDType, number>>();
/**
* @description Map for edge object
* @description.zh-CN 边的映射
*/
private edgesMap = new Map<string, DefaultEdgeType<NodeIDType, EdgeType>>();
/**
* @description Map for edge label
* @description.zh-CN 边的标签的映射
*/
private edgesLabelsMap = new Map<string, EdgeType | undefined>();
/**
* @description Is the graph directed or not
* @description.zh-CN 这个图是否是有向图
* @default true
*/
isDirected = () => this.directed;
/**
* @description Is this graph contains more than one graph data
* @description.zh-CN 这个图是否包含多个图
* @default false
*/
isMultigraph = () => this.multigraph;
/**
* @description Is this graph a compound graph;
* @description.zh-CN 这个图是否是复合图(包含嵌套节点的图)
* @default false
*/
isCompound = () => this.compound;
/**
* @description Set Graph label (Identity for graph)
* @description.zh-CN 设置图的标识符
* @param label
* @returns
*/
setGraph = (label?: GraphType) => {
this.label = label;
return this;
};
/**
* @description Get Graph label (Identity for graph)
* @description.zh-CN 获取图的标识符
* @returns stirng | undefined
*/
graph = () => this.label as GraphType;
/**
* @description Set function that generate default label for node, if param is non-function value then default label will always be this value;
* @description.zh-CN 设置默认获取节点Label的方法,如果传入不是函数的,那么默认label 的值只会是传入值
* @param newDefault (node) => label | label
* @returns this
*/
setDefaultNodeLabel = (newDefault: any) => {
if (isFunction(newDefault)) {
this.defaultNodeLabelFn = newDefault;
} else {
this.defaultNodeLabelFn = () => newDefault;
}
return this;
};
/**
* @description Count the nodes in graph
* @description.zh-CN 计算图中所有节点的数量
* @returns number
*/
nodeCount = () => this.nodeCountNum;
/**
* @description get node label
* @description.zh-CN 获取节点的标签
*/
node = (n: NodeIDType) => this.nodesLabelMap.get(n);
/**
* @description Return all nodes in graph
* @description 返回图中所有节点
* @returns
*/
nodes = () => Array.from(this.nodesLabelMap.keys());
/**
* @description Return all source nodes in graph
* @description 返回图中所有源头节点(入度为0)
* @returns
*/
sources = () => this.nodes().filter((n) => !this.inEdgesMap.get(n)?.size);
/**
* @description Return all sink nodes in graph
* @description 返回图中所有终点节点(出度为0)
* @returns
*/
sinks = () => this.nodes().filter((n) => !this.outEdgesMap.get(n)?.size);
/**
* @description Set Node label in graph if node not in graph then create it
* @description.zh-CN 设置节点的label,如果这个节点不在图中,则在图中创建这个节点
* @param node
* @param value
* @returns
*/
setNode(node: NodeIDType, value?: NodeType) {
const {
nodesLabelMap,
defaultNodeLabelFn,
isCompound,
parentMap,
childrenMap,
inEdgesMap,
outEdgesMap,
predecessorsMap,
successorsMap,
} = this;
// 如果节点不在图中,则创建节点
if (nodesLabelMap.has(node)) {
if (value !== undefined) {
nodesLabelMap.set(node, value);
}
return this;
}
nodesLabelMap.set(node, value || defaultNodeLabelFn(node));
// 如果是复合图,则创建节点的子节点
if (isCompound()) {
parentMap?.set(node, this.GRAPH_NODE);
childrenMap?.set(node, new Map());
if (!childrenMap?.has(this.GRAPH_NODE)) {
childrenMap?.set(this.GRAPH_NODE, new Map());
}
childrenMap?.get(this.GRAPH_NODE)?.set(node, true);
}
[inEdgesMap, outEdgesMap, predecessorsMap, successorsMap].forEach((map) =>
map.set(node, new Map()),
);
this.nodeCountNum += 1;
return this;
}
/**
* @description Set nodes or add nodes in batch
* @description.zh-CN 批量设置或者创建节点
* @param nodes
* @param value
* @returns
*/
setNodes = (nodes: NodeIDType[], value?: NodeType) => {
nodes.map((node) => this.setNode(node, value));
return this;
};
/**
* @description Is the node in graph
* @description.zh-CN 判断节点是否在图中
* @param node
* @returns
*/
hasNode = (node: NodeIDType) => this.nodesLabelMap.has(node);
/**
* @description if graph is not compound then throw error
* @description.zh-CN 如果图不是复合图就报错
*/
private checkCompound = () => {
if (!this.isCompound()) {
throw new Error('Cannot construct parent-children relations in a non-compound graph');
}
};
/**
* @description Find node's parent (compond graph only)
* @description.zh-CN 寻找节点的父节点 (只有复合图可以使用)
* @param node
* @returns
*/
parent = (node: NodeIDType) => {
if (this.isCompound()) {
const parent = this.parentMap?.get(node);
if (parent !== this.GRAPH_NODE) {
return parent;
}
}
};
/**
* @description Remove node from its parent (compond graph only)
* @description.zh-CN 将节点与其父节点之间的父子关系删除(只有复合图可以使用)
* @param node
*/
private removeFromParentsChildList = (node: NodeIDType) => {
const targetParent = this.parentMap!.get(node)!;
this.childrenMap!.get(targetParent)!.delete(node);
};
/**
* @description Set node's parent(default is the graph) (compond graph only)
* @description.zh-CN 设置节点的父节点,如果没有给定,父节点为这个图 (只有复合图可以使用)
* @param node
* @param parent
* @returns
*/
setParent = (node: NodeIDType, parent?: NodeIDType) => {
this.checkCompound();
let realParent = parent === undefined ? this.GRAPH_NODE : parent;
let checkNode = this.parent(realParent);
while (checkNode) {
if (node === checkNode) {
throw new Error('Setting ' + parent + ' as parent of ' + node + ' would create a cycle');
}
checkNode = this.parent(checkNode);
}
if (parent) {
this.setNode(parent);
}
this.setNode(node);
this.removeFromParentsChildList(node);
this.parentMap?.set(node, realParent);
const realParentChilren = this.childrenMap!.get(realParent)!;
realParentChilren.set(node, true);
this.childrenMap?.set(realParent, realParentChilren);
return this;
};
/**
* @description get graph's or node's children
* @description.zh-CN 获取图或者节点的字节点
* @param node
* @returns
*/
children = (node?: NodeIDType) => {
const targetNode = node === undefined ? this.GRAPH_NODE : node;
if (this.isCompound()) {
const target = this.childrenMap?.get(targetNode);
if (target) {
return Array.from(target.keys());
}
return undefined;
}
if (targetNode === this.GRAPH_NODE) {
return this.nodes();
}
if (node && this.hasNode(node)) {
return [];
}
};
/**
* @description get node's predecessors
* @description.zh-CN 获取节点的所有上游节点
* @param node
* @returns
*/
predecessors = (node: NodeIDType) => {
const preds = this.predecessorsMap.get(node);
return preds ? Array.from(preds.keys()) : undefined;
};
/**
* @description get node's successors
* @description.zh-CN 获取节点的所有下游节点
* @param node
* @returns
*/
successors = (node: NodeIDType) => {
const succs = this.successorsMap.get(node);
return succs ? Array.from(succs.keys()) : undefined;
};
/**
* @description get node's neighbors
* @description.zh-CN 获取节点的所有邻居节点
* @param node
* @returns
*/
neighbors = (node: NodeIDType) => {
if (!this.hasNode(node)) {
return undefined;
}
return Array.from(new Set(this.predecessors(node)?.concat(this.successors(node)!)));
};
/**
* @description Is the node a leaf node
* @description.zh-CN 判断节点是否为叶子节点
* @param node
* @returns
*/
isLeaf = (node: NodeIDType) => {
if (this.isDirected()) {
return !this.successors(node)?.length;
}
return !this.neighbors(node)?.length;
};
/**
* @description Using node filter to create a new graph;
* @description.zh-CN 过滤节点并创建一个新图
* @param filter
* @returns
*/
filterNodes = (filter: (node: NodeIDType) => boolean) => {
const { directed, multigraph, compound } = this;
const copyGraph = new Graph<NodeIDType, NodeType, EdgeType, GraphType>({
directed,
multigraph,
compound,
});
copyGraph.setGraph(this.graph());
this.nodes().forEach((n) => {
if (filter(n)) {
copyGraph.setNode(n, this.node(n));
}
});
this.edges().forEach((edgeObj) => {
if (copyGraph.hasNode(edgeObj.v) && copyGraph.hasNode(edgeObj.w)) {
copyGraph.setEdgeObj(edgeObj, this.edge(edgeObj));
}
});
if (compound) {
const findParent = (node: NodeIDType) => {
let parent = this.parent(node);
while (parent !== undefined && !copyGraph.hasNode(parent)) {
parent = this.parent(parent);
}
return parent;
};
copyGraph.nodes().forEach((node) => {
copyGraph.setParent(node, findParent(node));
});
}
return copyGraph;
};
/**
* @description Remove node from graph
* @description.zh-CN 将节点从图中移除
* @param node
* @returns
*/
removeNode(node: NodeIDType) {
if (this.hasNode(node)) {
const cleanEdge = (edgeObj: DefaultEdgeType<NodeIDType, EdgeType>) => {
this.removeEdge(edgeObj.v, edgeObj.w, edgeObj.name);
};
const { inEdgesMap, outEdgesMap, predecessorsMap, successorsMap, nodesLabelMap } = this;
if (this.isCompound()) {
this.removeFromParentsChildList(node);
this.parentMap?.delete(node);
this.children(node)?.forEach((n) => this.setParent(n));
this.childrenMap?.delete(node);
}
const inE = inEdgesMap.get(node)!;
const outE = outEdgesMap.get(node)!;
Array.from(inE.values()).forEach((edge) => cleanEdge(edge));
Array.from(outE.values()).forEach((edge) => cleanEdge(edge));
nodesLabelMap.delete(node);
inEdgesMap.delete(node);
outEdgesMap.delete(node);
predecessorsMap.delete(node);
successorsMap.delete(node);
this.nodeCountNum -= 1;
}
return this;
}
/**
* @description Set function that generate default label for edge, if param is non-function value then default label will always be this value;
* @description.zh-CN 设置默认获取边Label的方法,如果传入不是函数的,那么默认label 的值只会是传入值
* @param newDefault
* @returns
*/
setDefaultEdgeLabel = (newDefault: any) => {
if (isFunction(newDefault)) {
this.defaultEdgeLabelFn = newDefault;
} else {
this.defaultEdgeLabelFn = () => newDefault;
}
return this;
};
/**
* @description Count the edge in graph
* @description.zh-CN 返回图中边的数量
* @returns number
*/
edgeCount = () => this.edgeCountNum;
/**
* @description set edge value, if nodes or edges not exsit then add to graph
* @description.zh-CN 设置边的属性,如果边或节点不存在,那么将他们加入这个图
* @param v
* @param w
* @param value
* @param name
* @returns
*/
setEdge(v_: NodeIDType, w_: NodeIDType, value?: any, name?: string) {
const edgeObj = edgeArgsToObj<NodeIDType>(this.isDirected(), v_, w_, name);
const edgeId = edgeObjToId(this.isDirected(), edgeObj);
const { v, w } = edgeObj;
if (this.edgesLabelsMap.has(edgeId)) {
this.edgesLabelsMap.set(edgeId, value);
return this;
}
if (name !== undefined && !this.isMultigraph()) {
throw new Error('Cannot set a named edge when isMultigraph = false');
}
this.setNode(v);
this.setNode(w);
this.edgesLabelsMap.set(edgeId, value || this.defaultEdgeLabelFn(v, w, name));
Object.freeze(edgeObj);
this.edgesMap.set(edgeId, edgeObj);
const preds = this.predecessorsMap.get(w)!;
const succs = this.successorsMap.get(v)!;
incrementOrInitEntry(preds, v);
incrementOrInitEntry(succs, w);
this.inEdgesMap.get(w)?.set(edgeId, edgeObj);
this.outEdgesMap.get(v)?.set(edgeId, edgeObj);
this.edgeCountNum += 1;
return this;
}
setEdgeObj = (edgeObj: DefaultEdgeType<NodeIDType, EdgeType>, value?: EdgeType) => {
return this.setEdge(edgeObj.v, edgeObj.w, value, edgeObj.name);
};
/**
* @description Add edge using a sorted node array ([a,b,c] => a->b b->c c->a)
* @description.zh-CN 用一系列节点来定义一群边([a,b,c] => a->b b->c c->a)
* @param edges
* @param value
* @returns
*/
setPath = (edges: NodeIDType[], value?: any) => {
edges.reduce((v, w) => {
this.setEdge(v, w, value);
return w;
});
return this;
};
/**
* @description Get edge between two nodes
* @description.zh-CN 获得两个节点中的一条边
* @param v
* @param w
* @param name
* @returns
*/
edgeFromArgs = (v: NodeIDType, w: NodeIDType, name?: any) => {
return this.edge({ v, w, name });
};
/**
* @description Get edge between two nodes by edge object
* @description.zh-CN 从edgeObj获得两个节点中的一条边
* @param edgeObj
* @returns
*/
edge = (edgeObj: { v: NodeIDType; w: NodeIDType; name?: any }) => {
return this.edgesLabelsMap.get(edgeObjToId(this.isDirected(), edgeObj));
};
/**
* @description Does two nodes has a specific edge
* @description.zh-CN 两个节点之间是否存在确定的一条边
* @param v
* @param w
* @param name
* @returns
*/
hasEdge = (v: NodeIDType, w: NodeIDType, name?: any) => {
return this.edgesLabelsMap.has(edgeObjToId(this.isDirected(), { v, w, name }));
};
/**
* @description remove a specific edge
* @description.zh-CN 删除一条边
* @param v
* @param w
* @param name
* @returns
*/
removeEdge(v_: NodeIDType, w_: NodeIDType, name?: any) {
const edgeId = edgeArgsToId(this.isDirected(), v_, w_, name);
const edgeObj = this.edgesMap.get(edgeId);
if (edgeObj) {
const { v, w } = edgeArgsToObj(this.isDirected(), v_, w_, name);
this.edgesLabelsMap.delete(edgeId);
this.edgesMap.delete(edgeId);
const preds = this.predecessorsMap.get(w)!;
const succs = this.successorsMap.get(v)!;
decrementOrRemoveEntry(preds, v);
decrementOrRemoveEntry(succs, w);
this.inEdgesMap.get(w)!.delete(edgeId);
this.outEdgesMap.get(v)!.delete(edgeId);
this.edgeCountNum -= 1;
}
return this;
}
/**
* @description remove a specific edge by edge object
* @description.zh-CN 删除一条边
*/
removeEdgeObj = ({ v, w, name }: { v: NodeIDType; w: NodeIDType; name?: any }) => {
return this.removeEdge(v, w, name);
};
/**
* @description get all edges object in graph
* @description.zh-CN 获得图中所有的边对象
*/
edges = () => Array.from(this.edgesMap.values());
/**
* @description get edges that target at the node (could be from certain node)
* @description.zh-CN 获取所有指向节点的边,可以指定来源节点
* @param v
* @param u
* @returns
*/
inEdges = (v: NodeIDType, u?: NodeIDType) => {
const inV = this.inEdgesMap.get(v);
if (inV) {
return Array.from(inV.values()).filter((e) => !u || e.v === u);
}
return undefined;
};
/**
* @description get edges that from the node (could target at certain node)
* @description.zh-CN 获取所有来源于节点的边,可以指定目标节点
* @param w
* @param u
* @returns
*/
outEdges = (w: NodeIDType, u?: NodeIDType) => {
const outW = this.outEdgesMap.get(w);
if (outW) {
return Array.from(outW.values()).filter((e) => !u || e.w === u);
}
return undefined;
};
/**
* @description get edges between two nodes
* @description.zh-CN 获取两个节点间所有的节点
* @param w
* @param u
* @returns
*/
nodeEdges = (v: NodeIDType, w?: NodeIDType) => {
if (!this.hasNode(v)) {
return undefined;
}
return this.inEdges(v, w)?.concat(this.outEdges(v, w)!);
};
static fromJSON = read;
toJSON = () => write<NodeIDType, NodeType, EdgeType, GraphType>(this);
// ver 2 function
/**
* @description Count the in edges of node
* @description.zh-CN 计算节点的入边的数量
*/
nodeInDegree = (node: NodeIDType) => {
const inEdges = this.inEdgesMap.get(node);
if (inEdges) {
return inEdges.size;
}
return 0;
};
/**
* @description Count the out edges of node
* @description.zh-CN 计算节点的出边的数量
*/
nodeOutDegree = (node: NodeIDType) => {
const outEdges = this.outEdgesMap.get(node);
if (outEdges) {
return outEdges.size;
}
return 0;
};
/**
* @description Count the total edges of node
* @description.zh-CN 计算节点的所有边的数量
*/
nodeDegree = (node: NodeIDType) => {
return this.nodeInDegree(node) + this.nodeOutDegree(node);
};
/**
* @description Get the source of edge
* @description.zh-CN 获取边的源节点
*/
source = (edge: DefaultEdgeType<NodeIDType, EdgeType>) => edge.v;
/**
* @description Get the target of edge
* @description.zh-CN 获取边的目标节点
*/
target = (edge: DefaultEdgeType<NodeIDType, EdgeType>) => edge.w;
}
|