Skip to content

Commit 1e51b7f

Browse files
authored
Merge branch 'master' into master
2 parents e29241c + 218accd commit 1e51b7f

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,3 @@ This is a collection of algorithms and data structures which I've implement over
202202
* [String rotations](src/com/jwetherell/algorithms/strings/Rotation.java)
203203
+ Findin lexicographically minimal string rotation
204204
+ Findin lexicographically maximal string rotation
205-

src/com/jwetherell/algorithms/graph/CycleDetection.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,40 @@ private CycleDetection() { }
2121
* @param graph Graph
2222
* @return true if a cycle exists
2323
*/
24-
public static boolean detect(Graph<Integer> graph) {
24+
public static <T extends Comparable<T>> boolean detect(Graph<T> graph) {
2525
if (graph == null)
2626
throw new IllegalArgumentException("Graph is NULL.");
2727

2828
if (graph.getType() != Graph.TYPE.UNDIRECTED)
2929
throw new IllegalArgumentException("Graph is needs to be Undirected.");
3030

31-
final Set<Graph.Vertex<Integer>> visitedVerticies = new HashSet<Graph.Vertex<Integer>>();
32-
final Set<Graph.Edge<Integer>> visitedEdges = new HashSet<Graph.Edge<Integer>>();
31+
final Set<Graph.Vertex<T>> visitedVerticies = new HashSet<Graph.Vertex<T>>();
32+
final Set<Graph.Edge<T>> visitedEdges = new HashSet<Graph.Edge<T>>();
3333

34-
final List<Graph.Vertex<Integer>> verticies = graph.getVertices();
34+
final List<Graph.Vertex<T>> verticies = graph.getVertices();
3535
if (verticies == null || verticies.size() == 0)
3636
return false;
3737

3838
// Select the zero-ith element as the root
39-
final Graph.Vertex<Integer> root = verticies.get(0);
39+
final Graph.Vertex<T> root = verticies.get(0);
4040
return depthFirstSearch(root, visitedVerticies, visitedEdges);
4141
}
4242

43-
private static final boolean depthFirstSearch(Graph.Vertex<Integer> vertex, Set<Graph.Vertex<Integer>> visitedVerticies, Set<Graph.Edge<Integer>> visitedEdges) {
43+
private static final <T extends Comparable<T>> boolean depthFirstSearch(Graph.Vertex<T> vertex, Set<Graph.Vertex<T>> visitedVerticies, Set<Graph.Edge<T>> visitedEdges) {
4444
if (!visitedVerticies.contains(vertex)) {
4545
// Found an unvisited, add to the set
4646
visitedVerticies.add(vertex);
4747

48-
final List<Graph.Edge<Integer>> edges = vertex.getEdges();
48+
final List<Graph.Edge<T>> edges = vertex.getEdges();
4949
if (edges != null) {
5050
// Follow each unvisited edge, visit the vertex the edge connects to.
51-
for (Graph.Edge<Integer> edge : edges) {
52-
final Graph.Vertex<Integer> to = edge.getToVertex();
51+
for (Graph.Edge<T> edge : edges) {
52+
final Graph.Vertex<T> to = edge.getToVertex();
5353
boolean result = false;
5454
if (to != null && !visitedEdges.contains(edge)) {
5555
visitedEdges.add(edge);
5656

57-
final Graph.Edge<Integer> recip = new Graph.Edge<Integer>(edge.getCost(), edge.getToVertex(), edge.getFromVertex());
57+
final Graph.Edge<T> recip = new Graph.Edge<T>(edge.getCost(), edge.getToVertex(), edge.getFromVertex());
5858
visitedEdges.add(recip);
5959

6060
result = depthFirstSearch(to, visitedVerticies, visitedEdges);

0 commit comments

Comments
 (0)