众所周知,作为一种数学结构,图论广泛出现于人的社会关系、计算机网络、软件工程模块规划等等诸多学科、事务中。实现一个交互良好的图论GUI框架,可以方便人们理解、分析各种复杂的事物。
表示边和节点
节点和边用两个类表示,为方便以后的开发,最好把边和节点之间的关系和边和节点储存的数据分离开来。
于是我们可以得到四个类:
- MathNode
- MathEdge
- FactNode
- FactEdge
Fact开头的一组类就专注于领域应用,声明为interface方便根据具体应用具体来实现。
Math开头的一组就专注于图本身的操作,比如:
- 新增加一条边时,判断会不会使得图中出现环路。因为在某些应用场景下,不能出现环路。
- 进行图论算法,比如最短路径算法,此类算法在任务规划、网络通信中会用到。
代码:
interface FactNode{
}
interface FactEdge{
}
class MathNode{
constructor(readonly innerData: FactNode){
}
}
class MathEdge{
constructor(readonly innerData: FactEdge){
}
}
每一个MathNode都有一组出去的边、一组进来的边;每一个MathEdge都有sourceNode和targetNode。
为方便之后的算法,我们用MyArray来做数据容器:
interface FactNode{
}
interface FactEdge{
}
class MyArray{
add(val:FactEdge){
// todo
return this;
}
}
class MathNode{
constructor(readonly innerData: FactNode){
}
//
// 储存、添加出边
//
goingEdges = new MyArray();
addGoingEdge(edge: MathEdge){
this.goingEdges.add( edge );
this.innerData
return this;
}
//
// 储存、添加入边
//
commingEdges = new MyArray();
addCommingEdge(edge: MathEdge){
this.commingEdges.add( edge );
return this;
}
}
class MathEdge{
constructor(readonly innerData: FactEdge){
}
//
// 储存、设置起点
//
sourceNode!: MathNode;
setSourceNode(node: MathNode){
this.sourceNode = node;
return this;
}
//
// 储存、设置终点
//
targetNode!: MathNode;
setTargetNode(node: MathNode){
this.targetNode = node;
return this;
}
}