In the course of learning algorithms, you will definitely encounter graphs. Because I am familiar with Java, so I am accustomed to use Java to implement it again, but I have not found the tree and figure data structure of the jar package, it is very regrettable.
Today search topology sorting implementation, inadvertently found a diagram of the Java implementation of "1", very good, founder than my own much better, map of the use is a bright spot:
Enum color {white, gray, black}static class vertex {private string name; private color color; // Used to mark the processing state private vertex parent; //search ends can get multiple trees private int discover; //Start processing Time private int finish; / /End Processing Time Public vertex (string name) {this.name = name;this.color = Color.White;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} Public color getcolor () {return color;} Public void setcolor (Color color) {this.color = color;} Public vertex getparent () {return parent;} Public void setparent (vertex parent) {this.parent = parent;} Public int getDiscover () {return discover;} Public void setdiscover (Int discover) {this.discover = discover;} Public int getfinish () {return finish;} Public void setfinish (Int finish) {this.finish = finish;} @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = prime * result + ((Name == null) ? 0 : name.hashcode ()); return result;} @Overridepublic boolean equals (object obj) {if (this == obj) return true;if (obj == null) return false;if (GetClass () != obj.getclass ()) return false; vertex other = (Vertex) obj;if (name == null) {if (other.name != null) Return false;} else if (!name.equals (other.name)) return false;return true;}}static class graph {private set<vertex> vertexset = new hashset <Vertex> ();// adjacent nodes Private map<vertex, vertex[]> adjacencys = new HashMap<Vertex, Vertex[]> ();p ublic set<vertex> getvertexset () {return vertexset;} Public void setvertexset (Set<vertex> vertexset) {this.vertexSet = Vertexset;} Public map<vertex, vertex[]> getadjacencys () {return adjacencys;} Public void setadjacencys (Map<vertex, vertex[]> adjacencys) {this.adjacencys = adjacencys;}}
Attached to my own realization, silly according to the definition of the book, it is almost explosive:
public static class Alnode{public Alnode (String name) {this.name = name;} String name; Arcnode first;} public static class Arcnode{public Arcnode (Alnode node) {this.node = node;} Alnode node; Arcnode Next;} public static class Algraph{alnode nlist[];}
Resources
"1" Topology sort Java implementation http://blog.csdn.net/kimylrong/article/details/17220455
Java implementation of Graph adjacency table notation