@Test public void testGetNeighbours () { Node node1 = mGraph.addNode ("node1"); Node node2 = mGraph.addNode ("node2"); Node node3 = mGraph.addNode ("node3"); Node node4 = mGraph.addNode ("node4"); Node node5 = mGraph.addNode ("node5"); assertTrue (mGraph.getEdgesFromOrTo (node1).isEmpty ()); Edge edge1 = mGraph.addEdge ("node1", "node2"); Edge edge2 = mGraph.addEdge ("node1", "node3"); Edge edge3 = mGraph.addEdge ("node2", "node3"); Edge edge4 = mGraph.addEdge ("node2", "node4"); Edge edge5 = mGraph.addEdge ("node3", "node5"); Set<Node> neighbours = mGraph.getNeighbours (node1, 1); assertEquals (2, neighbours.size ()); assertFalse (neighbours.contains (node1)); assertTrue (neighbours.contains (node2)); assertTrue (neighbours.contains (node3)); assertFalse (neighbours.contains (node4)); assertFalse (neighbours.contains (node4)); neighbours = mGraph.getNeighbours (node1, 2); assertEquals (2, neighbours.size ()); assertFalse (neighbours.contains (node1)); assertFalse (neighbours.contains (node2)); assertFalse (neighbours.contains (node3)); assertTrue (neighbours.contains (node4)); assertTrue (neighbours.contains (node4)); neighbours = mGraph.getNeighbours (node5, 2); assertEquals (2, neighbours.size ()); assertTrue (neighbours.contains (node1)); assertTrue (neighbours.contains (node2)); assertFalse (neighbours.contains (node3)); assertFalse (neighbours.contains (node4)); assertFalse (neighbours.contains (node4)); }
public Set<Node> getNeighbours (Node node, int distance) { if (distance <= 0) { throw new RuntimeException ("distance must be 1 or greater"); } else { Set<Node> knownNodes = new HashSet<Node> (); Set<Node> newNeighbours = new HashSet<Node> (); newNeighbours.add (node); for (int currentDistance = 1; currentDistance <= distance; ++currentDistance) { knownNodes.addAll (newNeighbours); Set<Node> nodesToProcess = newNeighbours; newNeighbours = new HashSet<Node> (); for (Node currentNode : nodesToProcess) { for (Edge edge : getEdgesFrom (currentNode)) { Node target = edge.getTarget (); if (!knownNodes.contains (target)) { newNeighbours.add (target); } } for (Edge edge : getEdgesTo (currentNode)) { Node source = edge.getSource (); if (!knownNodes.contains (source)) { newNeighbours.add (source); } } } knownNodes.addAll (newNeighbours); } return newNeighbours; } }
DirectedGraph extends AttributeSetWithDefaultNodeAttributes { public Set<Node> getNeighbours (Node node, int distance) { if (distance <= 0) { throw new RuntimeException ("distance must be 1 or greater"); } else { Set<Node> knownNodes = new HashSet<Node> (); Set<Node> newNeighbours = new HashSet<Node> (); newNeighbours.add (node); for (int currentDistance = 1; currentDistance <= distance; ++currentDistance) { knownNodes.addAll (newNeighbours); Set<Node> nodesToProcess = newNeighbours; newNeighbours = new HashSet<Node> (); for (Node currentNode : nodesToProcess) { for (Edge edge : getEdgesFrom (currentNode)) { Node target = edge.getTarget (); if (!knownNodes.contains (target)) { newNeighbours.add (target); } } for (Edge edge : getEdgesTo (currentNode)) { Node source = edge.getSource (); if (!knownNodes.contains (source)) { newNeighbours.add (source); } } } knownNodes.addAll (newNeighbours); } return newNeighbours; } } }
DirectedGraph extends AttributeSetWithDefaultNodeAttributes { public Set<Node> getNeighbours (Node node, int distance) { if (distance <= 0) { throw new RuntimeException ("distance must be 1 or greater"); } else { Set<Node> knownNodes = new HashSet<Node> (); Set<Node> newNeighbours = new HashSet<Node> (); newNeighbours.add (node); for (int currentDistance = 1; currentDistance <= distance; ++currentDistance) { knownNodes.addAll (newNeighbours); Set<Node> nodesToProcess = newNeighbours; newNeighbours = new HashSet<Node> (); for (Node currentNode : nodesToProcess) { for (Edge edge : getEdgesFrom (currentNode)) { Node target = edge.getTarget (); if (!knownNodes.contains (target)) { newNeighbours.add (target); } } for (Edge edge : getEdgesTo (currentNode)) { Node source = edge.getSource (); if (!knownNodes.contains (source)) { newNeighbours.add (source); } } } knownNodes.addAll (newNeighbours); } return newNeighbours; } } DirectedGraph();  DirectedGraph(boolean concentrateEdges); }
DirectedGraph extends AttributeSetWithDefaultNodeAttributes { public Set<Node> getNeighbours (Node node, int distance) { if (distance <= 0) { throw new RuntimeException ("distance must be 1 or greater"); } else { Set<Node> knownNodes = new HashSet<Node> (); Set<Node> newNeighbours = new HashSet<Node> (); newNeighbours.add (node); for (int currentDistance = 1; currentDistance <= distance; ++currentDistance) { knownNodes.addAll (newNeighbours); Set<Node> nodesToProcess = newNeighbours; newNeighbours = new HashSet<Node> (); for (Node currentNode : nodesToProcess) { for (Edge edge : getEdgesFrom (currentNode)) { Node target = edge.getTarget (); if (!knownNodes.contains (target)) { newNeighbours.add (target); } } for (Edge edge : getEdgesTo (currentNode)) { Node source = edge.getSource (); if (!knownNodes.contains (source)) { newNeighbours.add (source); } } } knownNodes.addAll (newNeighbours); } return newNeighbours; } } DirectedGraph();  DirectedGraph(boolean concentrateEdges); void setConcentrateEdges(boolean value); boolean getConcentrateEdges(); void setRankDirection(RankDirection direction); RankDirection getRankDirection(); void setDistanceBetweenRanks(float distance); float getDistanceBetweenRanks(); void setDistanceBetweenNodes(float distance); float getDistanceBetweenNodes(); Node addNode(String name); Node addNode(String name, Cluster cluster); boolean hasNode(String name); Node getNode(String name); Node getRoot(); Iterator<Node> nodeIterator(); Edge addEdge(String sourceName, String targetName); Edge addEdge(Node source, String targetName); Edge addEdge(String sourceName, Node target); Edge addEdge(Node source, Node target); Edge addEdge(String sourceName, String targetName, String label); Edge addEdge(Node source, String targetName, String label); Edge addEdge(String sourceName, Node target, String label); Edge addEdge(Node source, Node target, String label); boolean hasEdge(Node source, Node target); Edge getEdge(Node source, Node target); Set<Edge> getEdgesFrom(Node source); Set<Edge> getEdgesTo(Node target); Set<Edge> getEdgesFromOrTo(Node node); Set<Node> getNeighbours(Node node, int distance); Cluster addCluster(String label); Cluster addCluster(String label, Cluster cluster); Cluster getCluster(String label); Rank addRank(String name, RankType type); Rank getRank(String name); }
DirectedGraph extends AttributeSetWithDefaultNodeAttributes { public Set<Node> getNeighbours (Node node, int distance) { if (distance <= 0) { throw new RuntimeException ("distance must be 1 or greater"); } else { Set<Node> knownNodes = new HashSet<Node> (); Set<Node> newNeighbours = new HashSet<Node> (); newNeighbours.add (node); for (int currentDistance = 1; currentDistance <= distance; ++currentDistance) { knownNodes.addAll (newNeighbours); Set<Node> nodesToProcess = newNeighbours; newNeighbours = new HashSet<Node> (); for (Node currentNode : nodesToProcess) { for (Edge edge : getEdgesFrom (currentNode)) { Node target = edge.getTarget (); if (!knownNodes.contains (target)) { newNeighbours.add (target); } } for (Edge edge : getEdgesTo (currentNode)) { Node source = edge.getSource (); if (!knownNodes.contains (source)) { newNeighbours.add (source); } } } knownNodes.addAll (newNeighbours); } return newNeighbours; } } DirectedGraph();  DirectedGraph(boolean concentrateEdges); void setConcentrateEdges(boolean value); boolean getConcentrateEdges(); void setRankDirection(RankDirection direction); RankDirection getRankDirection(); void setDistanceBetweenRanks(float distance); float getDistanceBetweenRanks(); void setDistanceBetweenNodes(float distance); float getDistanceBetweenNodes(); Node addNode(String name); Node addNode(String name, Cluster cluster); boolean hasNode(String name); Node getNode(String name); Node getRoot(); Iterator<Node> nodeIterator(); Edge addEdge(String sourceName, String targetName); Edge addEdge(Node source, String targetName); Edge addEdge(String sourceName, Node target); Edge addEdge(Node source, Node target); Edge addEdge(String sourceName, String targetName, String label); Edge addEdge(Node source, String targetName, String label); Edge addEdge(String sourceName, Node target, String label); Edge addEdge(Node source, Node target, String label); boolean hasEdge(Node source, Node target); Edge getEdge(Node source, Node target); Set<Edge> getEdgesFrom(Node source); Set<Edge> getEdgesTo(Node target); Set<Edge> getEdgesFromOrTo(Node node); Set<Node> getNeighbours(Node node, int distance); Cluster addCluster(String label); Cluster addCluster(String label, Cluster cluster); Cluster getCluster(String label); Rank addRank(String name, RankType type); Rank getRank(String name);  }