首页 > 解决方案 > 试图获取图表的边缘

问题描述

我正在尝试编写一种方法,该方法采用两个节点并在每个节点的边缘对象列表中搜索一个连接两个节点并返回源节点边缘的对象。如果没有找到任何东西,它应该返回 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;
}

标签: javadata-structuresgraph

解决方案


通常在众所周知的图表示中没有“边对象”这样的东西:邻接表、邻接矩阵、关联矩阵。对于图中的两个节点,您通常可以找到它们之间的路线(BFS、DFS、Dijkstra 和其他算法,具体取决于您正在寻找的路线类型),或者判断两个节点是否相邻(这在技术上意味着它们之间有一条边,但再一次 - 边或多或少完全由它连接的节点来表征 - 一组边是一组图节点的二进制子集,排序)。请澄清你的问题。


推荐阅读