Created
February 18, 2014 21:38
-
-
Save rayjcwu/9080780 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class DetectCycle { | |
| int time; | |
| public boolean hasCycle(List <Node> nodes) { | |
| time = 0; | |
| for (Node node: nodes) { | |
| if (node.color.equals(Color.White)) { | |
| if (dfsVisit(node)) { | |
| return true; | |
| } | |
| } | |
| } | |
| return false; | |
| } | |
| // has cycle, return true | |
| private boolean dfsVisit(Node node) { | |
| node.inTime = ++time; | |
| node.color = Color.Gray; | |
| for (Node u: node.adjs) { | |
| if (u.color.equals(Color.Gray)) { | |
| return true; | |
| } | |
| if (u.color.equals(Color.White)) { | |
| u.parent = node; | |
| if (dfsVisit(u)) { | |
| return true; | |
| } | |
| } | |
| } | |
| node.color = Color.Black; | |
| node.outTime = ++time; | |
| return false; | |
| } | |
| public static void main(String []argv) { | |
| Node n0 = new Node(0); | |
| Node n1 = new Node(1); | |
| Node n2 = new Node(2); | |
| n0.adjs.add(n1); | |
| n1.adjs.add(n2); | |
| n2.adjs.add(n0); | |
| List <Node> nodes = new ArrayList<Node>(); | |
| nodes.add(n0); | |
| nodes.add(n1); | |
| nodes.add(n2); | |
| DetectCycle dc = new DetectCycle(); | |
| System.out.println(dc.hasCycle(nodes)); | |
| Node b0 = new Node(0); | |
| Node b1 = new Node(1); | |
| Node b2 = new Node(2); | |
| b0.adjs.add(b1); | |
| b0.adjs.add(b2); | |
| b1.adjs.add(b2); | |
| List <Node> nodes2 = new ArrayList<Node>(); | |
| nodes.add(b0); | |
| nodes.add(b1); | |
| nodes.add(b2); | |
| System.out.println(dc.hasCycle(nodes2)); | |
| } | |
| } | |
| enum Color { | |
| White, Gray, Black | |
| } | |
| class Node { | |
| int val; | |
| int inTime; | |
| int outTime; | |
| Color color; | |
| Set <Node> adjs; | |
| Node parent; | |
| public Node(int x) { | |
| val = x; | |
| inTime = 0; | |
| outTime = 0; | |
| color = Color.White; | |
| adjs = new HashSet<Node>(); | |
| parent = null; | |
| } | |
| @Override | |
| public int hashCode() { | |
| final int prime = 31; | |
| int result = 1; | |
| result = prime * result + ((adjs == null) ? 0 : adjs.hashCode()); | |
| result = prime * result + ((color == null) ? 0 : color.hashCode()); | |
| result = prime * result + inTime; | |
| result = prime * result + outTime; | |
| result = prime * result + ((parent == null) ? 0 : parent.hashCode()); | |
| result = prime * result + val; | |
| return result; | |
| } | |
| @Override | |
| public boolean equals(Object obj) { | |
| if (this == obj) | |
| return true; | |
| if (obj == null) | |
| return false; | |
| if (getClass() != obj.getClass()) | |
| return false; | |
| Node other = (Node) obj; | |
| if (adjs == null) { | |
| if (other.adjs != null) | |
| return false; | |
| } else if (!adjs.equals(other.adjs)) | |
| return false; | |
| if (color != other.color) | |
| return false; | |
| if (inTime != other.inTime) | |
| return false; | |
| if (outTime != other.outTime) | |
| return false; | |
| if (parent == null) { | |
| if (other.parent != null) | |
| return false; | |
| } else if (!parent.equals(other.parent)) | |
| return false; | |
| if (val != other.val) | |
| return false; | |
| return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment