java - 试图获取图表的边缘
问题描述
我正在尝试编写一种方法,该方法采用两个节点并在每个节点的边缘对象列表中搜索一个连接两个节点并返回源节点边缘的对象。如果没有找到任何东西,它应该返回 null。
我的 getEdges() 方法返回节点拥有的边列表。
/**
* Searches for an edge from the source node to the destination.
* @param source The source, or first, node
* @param destination The destination, or second, node
* @return The edge between the nodes, or null if not found
*/
public Edge getEdge(Node source, Node destination) {
// TODO
Edge e1 = null;
for (int i = 0; i < source.getEdges().size(); i++) {
if (source.getEdges().contains(destination)) {
e1.setNode1(source);
e1.setNode2(destination);
}
}
return e1;
}
解决方案
通常在众所周知的图表示中没有“边对象”这样的东西:邻接表、邻接矩阵、关联矩阵。对于图中的两个节点,您通常可以找到它们之间的路线(BFS、DFS、Dijkstra 和其他算法,具体取决于您正在寻找的路线类型),或者判断两个节点是否相邻(这在技术上意味着它们之间有一条边,但再一次 - 边或多或少完全由它连接的节点来表征 - 一组边是一组图节点的二进制子集,排序)。请澄清你的问题。
推荐阅读
- python - 如何加载保存的 TensorFlow 模型并对其进行评估
- animation - 为什么 NavigationView 中的动画从左上角开始?
- python - 每次for循环的交互如何创建新线程?
- php - Firestore PHP SDK - 如何保存日期?
- arrays - 无法将 appsettings IConfiguration 部分反序列化为类型化元素的数组
- html - 如何使用 HTML CSS 和 Bootstrap 4 使行和列容器垂直堆叠并为移动视图缩放
- git - git diff 获取所有内容
- tkinter - 何时以及为什么需要将窗口坐标更改为画布坐标?
- vb.net - 试图在函数头中使用类型类型的变量 - 包含类型
- python - 从 Python 与 Node.js 进程通信