I\'m concerned that this might be working on an NP-Complete problem. I\'m hoping someone can give me an answer as to whether it is or not. And I\'m looking for more of an an
Any graph shortest path algorithm will be overkill if all you need is to find if a node is connected to another. A good Java library that accomplishes that is JGraphT. It's usage is quite simple, here's an example of an Integer graph:
public void loadGraph() {
// first we create a new undirected graph of Integers
UndirectedGraph graph = new SimpleGraph<>(DefaultEdge.class);
// then we add some nodes
graph.addVertex(1);
graph.addVertex(2);
graph.addVertex(3);
graph.addVertex(4);
graph.addVertex(5);
graph.addVertex(6);
graph.addVertex(7);
graph.addVertex(8);
graph.addVertex(9);
graph.addVertex(10);
graph.addVertex(11);
graph.addVertex(12);
graph.addVertex(13);
graph.addVertex(14);
graph.addVertex(15);
graph.addVertex(16);
// then we connect the nodes
graph.addEdge(1, 2);
graph.addEdge(2, 3);
graph.addEdge(3, 4);
graph.addEdge(3, 5);
graph.addEdge(5, 6);
graph.addEdge(6, 7);
graph.addEdge(7, 8);
graph.addEdge(8, 9);
graph.addEdge(9, 10);
graph.addEdge(10, 11);
graph.addEdge(11, 12);
graph.addEdge(13, 14);
graph.addEdge(14, 15);
graph.addEdge(15, 16);
// finally we use ConnectivityInspector to check nodes connectivity
ConnectivityInspector inspector = new ConnectivityInspector<>(graph);
debug(inspector, 1, 2);
debug(inspector, 1, 4);
debug(inspector, 1, 3);
debug(inspector, 1, 12);
debug(inspector, 16, 5);
}
private void debug(ConnectivityInspector inspector, Integer n1, Integer n2) {
System.out.println(String.format("are [%s] and [%s] connected? [%s]", n1, n2, inspector.pathExists(n1, n2)));
}
This lib also offers all the shortest paths algorithms as well.