To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/2/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());
  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);
  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 void print() {
 170        print(this);
 171    }
 172    
 173    public static void print(Graph g) {
 174
 175        System.out.println(" --- Graph --- ");
 176        
 177        for(Node n : g.getNodes()) {
 178            
 179            System.out.println(n + ": " + g.getAdjacencyList(n));
 180
 181        }
 182        System.out.println(" ------------ ");
 183    }
 184
 185    
 186    public boolean isDirty() {
 187        return isDirty;
 188    }
 189    
 190    public void setDirty(boolean value) {
 191        isDirty = value;
 192    }
 193    
 194   
 195    private char currentChar = 'a' - 1;
 196    
 197    public char getCurrentChar() {
 198        return currentChar;
 199    }
 200    
 201    public void setCurrentChar(char c) {
 202        currentChar = c;
 203    }
 204    
 205    public char getNextChar() {
 206        currentChar++;
 207        return getCurrentChar();
 208    }    
 209}
 210

· Graph.java ends ·

Generated by CHIP: Code Highlighting in PHP, version 2.7.0.