Skip to content
Snippets Groups Projects
Commit 8dbe3226 authored by Ant01n3's avatar Ant01n3
Browse files

Correction of the #16 bug in tracker.

The Toolkit.random* methods now correctly return null whan the graph
is empty or when the set of elements on which the random choise is
made is empty.
parent 7ba85175
Branches
Tags
No related merge requests found
...@@ -271,13 +271,16 @@ public class Toolkit { ...@@ -271,13 +271,16 @@ public class Toolkit {
*/ */
public static Node randomNode(Graph graph, Random random) { public static Node randomNode(Graph graph, Random random) {
int n = graph.getNodeCount(); int n = graph.getNodeCount();
int r = random.nextInt(n);
int i = 0; if(n > 0) {
int r = random.nextInt(n);
int i = 0;
for (Node node : graph) { for (Node node : graph) {
if (r == i) if (r == i)
return node; return node;
i++; i++;
}
} }
return null; return null;
...@@ -303,13 +306,16 @@ public class Toolkit { ...@@ -303,13 +306,16 @@ public class Toolkit {
*/ */
public static Edge randomEdge(Graph graph, Random random) { public static Edge randomEdge(Graph graph, Random random) {
int n = graph.getEdgeCount(); int n = graph.getEdgeCount();
int r = random.nextInt(n);
int i = 0; if(n > 0) {
int r = random.nextInt(n);
int i = 0;
for (Edge edge : graph.getEachEdge()) { for (Edge edge : graph.getEachEdge()) {
if (r == i) if (r == i)
return edge; return edge;
i++; i++;
}
} }
return null; return null;
...@@ -357,13 +363,16 @@ public class Toolkit { ...@@ -357,13 +363,16 @@ public class Toolkit {
*/ */
public static Edge randomEdge(Node node, Random random) { public static Edge randomEdge(Node node, Random random) {
int n = node.getDegree(); int n = node.getDegree();
int r = random.nextInt(n);
int i = 0; if (n > 0) {
int r = random.nextInt(n);
int i = 0;
for (Edge edge : node.getEdgeSet()) { for (Edge edge : node.getEdgeSet()) {
if (r == i) if (r == i)
return edge; return edge;
i++; i++;
}
} }
return null; return null;
...@@ -380,13 +389,16 @@ public class Toolkit { ...@@ -380,13 +389,16 @@ public class Toolkit {
*/ */
public static Edge randomInEdge(Node node, Random random) { public static Edge randomInEdge(Node node, Random random) {
int n = node.getInDegree(); int n = node.getInDegree();
int r = random.nextInt(n);
int i = 0; if(n > 0) {
int r = random.nextInt(n);
int i = 0;
for (Edge edge : node.getEnteringEdgeSet()) { for (Edge edge : node.getEnteringEdgeSet()) {
if (r == i) if (r == i)
return edge; return edge;
i++; i++;
}
} }
return null; return null;
...@@ -403,13 +415,16 @@ public class Toolkit { ...@@ -403,13 +415,16 @@ public class Toolkit {
*/ */
public static Edge randomOutEdge(Node node, Random random) { public static Edge randomOutEdge(Node node, Random random) {
int n = node.getOutDegree(); int n = node.getOutDegree();
int r = random.nextInt(n);
int i = 0; if (n > 0) {
int r = random.nextInt(n);
int i = 0;
for (Edge edge : node.getLeavingEdgeSet()) { for (Edge edge : node.getLeavingEdgeSet()) {
if (r == i) if (r == i)
return edge; return edge;
i += 1; i += 1;
}
} }
return null; return null;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment