To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/3/src/antgraph/Graph.java. This is a spam-protection measure; sorry for the inconvenience.
· Graph.java ·
1/* 2 * Graph.java 3 * 4 * Created on 02 March 2007, 22:52 5 * 6 * To change this template, choose Tools | Template Manager 7 * and open the template in the editor. 8 */ 9 10package antgraph; 11 12 13import java.util.ArrayList; 14import java.util.HashMap; 15import java.util.List; 16 17/** 18 * 19 * @author James 20 */ 21public class Graph { 22 23 24 private List<Node> nodes = new ArrayList<Node>(); 25 private HashMap<Node, List<Node>> adjacencyList = new HashMap<Node, List<Node>>(); 26 private HashMap<String, Edge> edges = new HashMap<String, Edge>(); 27 28 private boolean isDirty = false; 29 30 public Graph() { 31 32 } 33 34 public void addNode(Node n) { 35 nodes.add(n); 36 } 37 38 public void addNode(String o) { 39 this.addNode(new Node(o)); 40 } 41 42 public void addNode() { 43 this.addNode(new Node(this.getNextChar())); 44 } 45 46 public void addNode(int x, int y) { 47 Node n = new Node(this.getNextChar(), x, y); 48 // n.setLocation(x, y); 49 this.addNode(n); 50 } 51 52 public void addNode(String s, int x, int y) { 53 Node n = new Node(s, x, y); 54 //n.setLocation(x, y); 55 this.addNode(n); 56 } 57 58 public void removeNode(Node n) { 59 System.out.println("Removing " + n + "..."); 60 61 62 for(Node node : getNodes()) { 63 //edges.remove(n.toString() + node.toString()); 64 // edges.remove(node.toString() + n.toString()); 65 removeEdge(node, n); 66 } 67 68 nodes.remove(n); 69 70 //adjacencyList.remove(n); 71 72 // for(List l : adjacencyList.values()) { 73 // l.remove(n); 74 // } 75 76 77 } 78 79 public int getNumberOfVertices() { 80 return nodes.size(); 81 } 82 83 public void addEdge(Node a, Node b) throws NoSuchNodeException { 84 System.out.println("Creating an edge between " + a + " and " + b); 85 86 if(hasEdge(a, b) || a.equals(b)) return; 87 88 Edge e = new Edge(a, b); 89 // Edge e1 = new Edge(b, a); 90 91 edges.put(a.toString() + b.toString(), e); 92 //edges.put(b.toString() + a.toString(), e1); 93 94 if(!nodes.contains(a)) 95 throw new NoSuchNodeException("No such node: " + a); 96 if(!nodes.contains(b)) 97 throw new NoSuchNodeException("No such node: " + b); 98 99 if(!adjacencyList.containsKey(a)) { 100 adjacencyList.put(a, new ArrayList()); 101 } 102 if(!adjacencyList.containsKey(b)) { 103 adjacencyList.put(b, new ArrayList()); 104 } 105 106 adjacencyList.get(a).add(b); 107 adjacencyList.get(b).add(a); 108 109 } 110 111 public Edge getEdge(Node a, Node b) { 112 if(hasEdge(a, b)) { 113 if(edges.containsKey(a.toString() + b.toString())) 114 return edges.get(a.toString() + b.toString()); 115 else 116 return edges.get(b.toString() + a.toString()); 117 } 118 return null; 119 } 120 121 public List<Edge> getEdges(Node node) { 122 List<Edge> edges = new ArrayList(); 123 for(Edge e : getEdges()) { 124 if(e.from(node)) edges.add(e); 125 } 126 return edges; 127 } 128 129 public List<Edge> getEdges() { 130 return new ArrayList(edges.values()); 131 } 132 133 public HashMap<String, Edge> getEdgesMap() { 134 return edges; 135 } 136 137 public boolean hasEdge(Node a, Node b) { 138 try { 139 return adjacencyList.get(a).contains(b) || adjacencyList.get(b).contains(a); 140 }catch (NullPointerException e) { 141 return false; 142 } 143 } 144 145 public void removeEdge(Node a, Node b) { 146 edges.remove(a.toString() + b.toString()); 147 edges.remove(b.toString() + a.toString()); 148 149 getAdjacencyList(a).remove(b); 150 getAdjacencyList(b).remove(a); 151 } 152 153 public void removeEdge(Edge e) { 154 removeEdge(e.a(), e.b()); 155 } 156 157 public List<Node> getAdjacencyList(Node n) { 158 return adjacencyList.containsKey(n) ? adjacencyList.get(n) : new ArrayList(); 159 } 160 161 public HashMap<Node, List<Node>> getAdjacencyList() { 162 return adjacencyList; 163 } 164 165 public List<Node> getNodes() { 166 return nodes; 167 } 168 169 public List<GraphComponent> getComponents() { 170 List l = new ArrayList(nodes); 171 l.addAll(edges.values()); 172 return l; 173 } 174 175 public void highlight(GraphComponent gc) { 176 if(gc instanceof Edge) 177 for(Edge e : getEdges()) 178 e.highlight(false); 179 else 180 for(Node e : getNodes()) 181 e.highlight(false); 182 gc.highlight(true); 183 } 184 185 public void highlight(List<Node> nodes) { 186 try { 187 nodes.add(nodes.get(0)); 188 for(int i = 0; i < nodes.size() - 1; i++) { 189 getEdge(nodes.get(i), nodes.get(i + 1)).highlight(true); 190 nodes.get(i).highlight(true); 191 } 192 nodes.get(nodes.size()-1).highlight(true); 193 194 }catch (NullPointerException e) {} 195 } 196 197 public void print() { 198 print(this); 199 } 200 201 public static void print(Graph g) { 202 203 System.out.println(" --- Graph --- "); 204 205 for(Node n : g.getNodes()) { 206 207 System.out.println(n + ": " + g.getAdjacencyList(n)); 208 209 } 210 System.out.println(" ------------ "); 211 } 212 213 214 public boolean isDirty() { 215 return isDirty; 216 } 217 218 public void setDirty(boolean value) { 219 isDirty = value; 220 } 221 222 223 private char currentChar = 'a' - 1; 224 225 public char getCurrentChar() { 226 return currentChar; 227 } 228 229 public void setCurrentChar(char c) { 230 currentChar = c; 231 } 232 233 public char getNextChar() { 234 currentChar++; 235 return getCurrentChar(); 236 } 237} 238
· Graph.java ends ·
To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/3/src/antgraph/Graph.java. This is a spam-protection measure; sorry for the inconvenience.