1 import java
.util
.Collection
;
2 import java
.util
.Enumeration
;
3 import java
.util
.Hashtable
;
6 * Graph is a representing the complex datastructure graph, which
7 * stores nodes and connections between them.
9 * @author "Anton Johansson" <anton.johansson@gmail.com>
10 * @author "Victor Zamanian" <victor.zamanian@gmail.com>
14 private Hashtable
<Object
, GraphNode
> nodes
;
15 private int initialCapacityNeighbours
;
16 private int numberOfEdges
= 0;
19 * Creates a new instance of a Graph.
21 * @param initialSize The number of nodes that need
22 * to have memory allocated for them.
23 * @param initialCapacityNeighbours The maximum number of neighbours.
24 * (The number of nodes in the network.)
26 public Graph(int initialSize
, int initialCapacityNeighbours
) {
27 this.initialCapacityNeighbours
= initialCapacityNeighbours
;
28 nodes
= new Hashtable
<Object
, GraphNode
>(initialSize
);
32 * Inserts a node with name address name into the graph.
34 * @param name The name address of the node to insert.
35 * @param x the x-coordante of the node.
36 * @param y the y-coordante of the node.
37 * @return the newly created GraphNode
39 public GraphNode
insertNode(String name
, int x
, int y
) {
40 GraphNode node
= new GraphNode(name
, x
, y
, initialCapacityNeighbours
);
41 nodes
.put(name
, node
);
46 * Insers an edge between two nodes in the graph.
47 * It is assumed that the parameter nodes exist in the graph.
49 * @param srcName The source node.
50 * @param destName The destination node.
51 * @param edge The edge between the nodes.
52 * @return true if an edge is inserted.
54 public Boolean
insertEdge(String srcName
, String destName
, Object edge
) {
55 //Om det inte redan finns en kant
56 GraphNode srcNode
= nodes
.get(srcName
);
57 GraphNode destNode
= nodes
.get(destName
);
59 if (srcNode
!= null && destNode
!= null) {
60 srcNode
.addNeighbour(destNode
, edge
);
61 //destNode.addNeighbour(srcNode, weight);
71 * Inspects the graph to see if it is empty of nodes.
72 * @return true if the graph contains no nodes, else false.
74 public boolean isEmpty() {
75 return nodes
.isEmpty();
79 * Inspects if the graph has no edges.
80 * @return true if the graph contains no edges, else false.
82 public boolean hasNoEdges() {
83 return (numberOfEdges
== 0);
87 * The set of nodes which are neighbours
88 * of the node with name address given by parameter.
90 * @param name The name address of the node.
91 * @return An Enumeration with the set of neighbours of the node.
93 public Enumeration
<GraphNode
> neighbours(String name
) {
94 return nodes
.get(name
).getNeighbours();
98 * Inspects the number of nodes in this Graph.
100 * @return The number of nodes in this Graph.
107 * Get all nodes in the graph.
109 * @return An collection of all nodes in the graph.
111 public Collection
<GraphNode
> getNodes() {
112 return nodes
.values();
116 * Inspects the weight of an edge between two nodes in the grapn.
118 * @param srcName The name address of the source node.
119 * @param destName The name address of the destination node.
120 * @return The weight of the edge.
122 public Object
getEdge(String srcName
, String destName
) {
123 GraphNode srcNode
= nodes
.get(srcName
);
124 GraphNode destNode
= nodes
.get(destName
);
125 return srcNode
.getEdge(destNode
);
131 * Removes a node from the graph.
133 * @param node The node to be removed.
135 public void deleteNode(GraphNode node
) {
136 for (Enumeration
<GraphNode
> e
= node
.getNeighbours(); e
.hasMoreElements();) {
137 e
.nextElement().deleteNeighbour(node
);
139 nodes
.remove(node
.getName());
143 * Removes an edge between two nodes.
145 * @param src The source node.
146 * @param dest The destination node.
148 public void deleteEdge(GraphNode src
, GraphNode dest
) {
149 src
.deleteNeighbour(dest
);
150 dest
.deleteNeighbour(src
);
156 * Alters the weight of an edge between two nodes in the graph.
158 * @param srcNode The source node.
159 * @param destNode The destination node.
160 * @param weight The new weight between the two nodes.
162 public void setWeight(GraphNode srcNode
, GraphNode destNode
, Double weight
) {
163 deleteEdge(srcNode
, destNode
);
164 insertEdge(srcNode
.getName(), destNode
.getName(), weight
);
168 * Inspects whether a node with a certain name address
169 * given by parameter exists in the graph or not.
171 * @param name The name address of the node.
172 * @return true if the node exists, else false.
174 public boolean isInGraph(String name
) {
175 return (nodes
.containsKey(name
));
179 * Returns the node with the specified name, if it exists in this
182 * @param name The name of the node to fetch.
183 * @return the node with the specified name.
185 public GraphNode
getNode(String name
) {
186 return nodes
.get(name
);
190 * Returns a String representation of this Graph.
192 * @return the String representation of this Graph.
194 public String
toString() {
195 String returnString
= "";
196 for (Enumeration
<GraphNode
> e
= nodes
.elements(); e
.hasMoreElements();) {
197 returnString
+= e
.nextElement();