Reputation: 1
I'm trying to write a method that takes two nodes and searches each node's list of edge objects for one that connects the two nodes and returns the source node's edge. If there is nothing found it should return null.
My getEdges() method returns a list of edges a node has.
/**
* 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;
}
Upvotes: 0
Views: 161
Reputation: 99
Usually there is no such thing as an "edge object" in commonly known graph representations: adjacency list, adjacency matrix, incidence matrix. With two nodes in a graph you can generally either find a route between them (BFS, DFS, Dijkstra and other algorithms, depending on the kind of route you're looking for), or tell if the two nodes are adjacent (this technically means that there is an edge beween them, but once again - edge is more or less completely characterized by the nodes it connects - a set of edges is a set of binary subsets of nodes of a graph, sorta). Please, clarify your question.
Upvotes: 0