1 | import LinkList from "../linklist/LinkList";
|
2 | import { toString } from "../util";
|
3 | import { GraphEdge } from "./GraphEdge";
|
4 | export class GraphVertex {
|
5 | constructor(node, property) {
|
6 | this.property = property;
|
7 | this.indegree = 0;
|
8 | if (property) {
|
9 | const key = node[property];
|
10 | this.key = toString(key);
|
11 | }
|
12 | else {
|
13 | this.key = toString(node);
|
14 | }
|
15 | this.node = node;
|
16 | this.edges = new LinkList();
|
17 | }
|
18 | set InDegree(value) {
|
19 | this.indegree = value;
|
20 | }
|
21 | get Key() {
|
22 | return this.key;
|
23 | }
|
24 | get Node() {
|
25 | return this.node;
|
26 | }
|
27 | get Property() {
|
28 | return this.property;
|
29 | }
|
30 | addUndirectedEdge(endVertex, weight) {
|
31 | if (!endVertex) {
|
32 | throw new Error("end vertex is not empty");
|
33 | }
|
34 | const exist = this.edges.findNode(item => item.EndVertex.Key === endVertex.Key);
|
35 | if (exist) {
|
36 | return false;
|
37 | }
|
38 | const edge = new GraphEdge(this, endVertex, weight);
|
39 | this.edges.append(edge);
|
40 | endVertex.addUndirectedEdge(this, weight);
|
41 | }
|
42 | addEdge(endVertex, weight) {
|
43 | if (!endVertex) {
|
44 | throw new Error("end vertex is not empty");
|
45 | }
|
46 | const exist = this.edges.findNode(item => item.EndVertex.Key === endVertex.Key);
|
47 | if (exist) {
|
48 | return false;
|
49 | }
|
50 | const edge = new GraphEdge(this, endVertex, weight);
|
51 | this.edges.append(edge);
|
52 | endVertex.InDegree = endVertex.getInDegree() + 1;
|
53 | return true;
|
54 | }
|
55 | getEdges() {
|
56 | return this.edges;
|
57 | }
|
58 | getEdge(endKey) {
|
59 | const edge = this.edges.findNode(item => item.EndVertex.Key === endKey);
|
60 | if (edge) {
|
61 | return edge.Value;
|
62 | }
|
63 | return null;
|
64 | }
|
65 | deleteEdgeByKey(endKey, directed = true) {
|
66 | const edge = this.edges.findNode(item => item.EndVertex.key === endKey);
|
67 | const success = this.edges.deleteNode(item => item.EndVertex.Key === endKey);
|
68 | if (success) {
|
69 | if (directed) {
|
70 | edge.Value.EndVertex.InDegree = edge.Value.EndVertex.getInDegree() - 1;
|
71 | }
|
72 | else {
|
73 | edge.Value.EndVertex.deleteEdgeByKey(edge.Value.StartVertex.key, true);
|
74 | }
|
75 | }
|
76 | return success;
|
77 | }
|
78 | deleteEdge(edge) {
|
79 | const success = this.edges.deleteNode(edge);
|
80 | if (success) {
|
81 | edge.EndVertex.InDegree = edge.EndVertex.getInDegree() - 1;
|
82 | }
|
83 | return success;
|
84 | }
|
85 | hasEdge() {
|
86 | return !!this.edges.Size;
|
87 | }
|
88 | getInDegree() {
|
89 | return this.indegree;
|
90 | }
|
91 | getOutDegree() {
|
92 | return this.edges.Size;
|
93 | }
|
94 | getDegree() {
|
95 | return this.getInDegree() + this.getOutDegree();
|
96 | }
|
97 | getNeighbors() {
|
98 | const arr = [];
|
99 | let node = this.edges.getHeadNode();
|
100 | while (node) {
|
101 | arr.push(node.Value.EndVertex);
|
102 | node = node.Next;
|
103 | }
|
104 | return arr;
|
105 | }
|
106 | }
|