Created
September 10, 2014 23:39
-
-
Save stochastic-thread/508a952dfbf4c6431b1e to your computer and use it in GitHub Desktop.
This file contains 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
package cmsc433.p1; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.Iterator; | |
import java.util.Set; | |
public class SimpleGraph<T> { | |
HashMap<Node, HashSet<Node>> graph = new HashMap<Node, HashSet<Node>>(); | |
HashMap<T, Node> nodeCache = new HashMap<T, Node>(); | |
HashMap<Node, HashMap<Node, Edge>> edgeCache = new HashMap<Node, HashMap<Node, Edge>>(); | |
public class Node { | |
public T data; | |
public Node(T data) { | |
this.data = data; | |
} | |
public Node build(T data) { | |
Node node = nodeCache.get(data); | |
if (node == null) { | |
node = new Node(data); | |
nodeCache.put(data, node); | |
} | |
return node; | |
} | |
public String toString() { | |
return data.toString(); | |
} | |
} | |
public class Edge { | |
Node from; | |
Node to; | |
/* We can assume that these Nodes have been "built" | |
* and we know they are certainly non-duplicates | |
* */ | |
public Edge(Node from, Node to) { | |
this.from = from; | |
this.to = to; | |
} | |
public Edge(T from, T to) { | |
this.from = new Node(from); | |
this.to = new Node(to); | |
} | |
public Edge build(T f, T t) { | |
Node from = new Node(f); // create a node with f data | |
from = from.build(f); // check if a node with this same data already exists, if so, set this as node | |
Node to = new Node(t); // same as above | |
to = to.build(t); // ditto | |
Edge e = new Edge(from, to); // instantiate an Edge object | |
Edge finalEdge = null; | |
HashMap<Node, Edge> response = edgeCache.get(from); // fetch the from Node from cache | |
if (response == null) { | |
/* First possibility is that there is no | |
* response, which implies that there is | |
* no edge yet with that as the source | |
* Node. If so, create that | |
* Edge, and cache it. */ | |
HashMap<Node, Edge> hm = new HashMap<Node,Edge>(); // instantiate the nested HashMap for edge cache | |
hm.put(to, e); // initialize the nested HashMap for edge cache | |
edgeCache.put(from, hm); // actually caches the edge | |
finalEdge = e; | |
} | |
else { | |
/* If there is a response, then there are some Edges with | |
* from as a Node. Keep in mind these nodes might not | |
* actually be the "to" node, so we check for that. */ | |
Edge edge = response.get(to); // try to get the "to" Node. | |
if (edge == null) { // If edge is null now there is no to | |
response.put(to, e); // inserts new edge | |
edgeCache.put(from, response); // caches the new edge | |
finalEdge = e; | |
} | |
finalEdge = edge; | |
} | |
return finalEdge; | |
} | |
public Edge build(Node from, Node to) { | |
from = from.build(from.data); | |
to = to.build(to.data); | |
Edge e = new Edge(from, to); | |
Edge finalEdge = null; | |
HashMap<Node, Edge> possible = edgeCache.get(from); | |
if (possible == null) { | |
HashMap<Node, Edge> cacheAddition = new HashMap<Node, Edge>(); | |
cacheAddition.put(to, e); | |
edgeCache.put(from, cacheAddition); | |
finalEdge = e; | |
} | |
else { | |
Edge possibleEdge = possible.get(to); | |
if (possibleEdge == null) { | |
possible.put(to, e); | |
finalEdge = e; | |
} | |
finalEdge = possibleEdge; | |
} | |
return finalEdge; | |
} | |
public String toString() { | |
Node from = this.from; | |
Node to = this.to; | |
return from.toString() + " -> " + to.toString(); | |
} | |
} | |
public Node addNode(T data) { | |
Node n = new Node(data); | |
n = n.build(data); | |
HashSet<Node> toSet = graph.get(n); | |
if (toSet == null) { | |
graph.put(n, null); | |
} | |
return n; | |
} | |
public Edge addEdge(T from, T to) { | |
Node f = new Node(from); | |
Node t = new Node(to); | |
f = f.build(from); | |
t = t.build(to); | |
Edge e = new Edge(f, t); | |
e = e.build(f, t); | |
HashSet<Node> set = graph.get(f); | |
if (set == null) { | |
set = new HashSet<Node>(); | |
} | |
set.add(t); | |
graph.put(f, set); | |
return e; | |
} | |
public Edge addEdge(Node from, Node to) { | |
from = from.build(from.data); | |
to = to.build(to.data); | |
HashSet<Node> set = graph.get(from); | |
if (set == null) | |
set = new HashSet<Node>(); | |
set.add(to); | |
graph.put(from, set); | |
Edge edge = new Edge(from, to); | |
return edge.build(from.data, to.data); | |
} | |
public String toString() { | |
Set<Node> fromNodes = graph.keySet(); | |
Iterator<Node> x = fromNodes.iterator(); | |
StringBuffer nodes = new StringBuffer(); | |
while(x.hasNext()) { | |
Node next = x.next(); | |
HashSet<Node> destNodes = graph.get(next); | |
nodes.append(next.data.toString()); | |
if (destNodes != null) { | |
nodes.append(" -> " + destNodes.toString()); | |
} | |
nodes.append("\n"); | |
} | |
return nodes.toString(); | |
} | |
public static void main(String[] args) { | |
SimpleGraph<String> sg = new SimpleGraph<String>(); | |
sg.addNode("D"); | |
int h1 = sg.addNode("C").hashCode(); | |
sg.addEdge("A", "B"); | |
sg.addEdge("B", "A"); | |
sg.addEdge("B", "C"); | |
sg.addEdge("A", "C"); | |
int h2 = sg.addNode("C").hashCode(); | |
sg.addEdge("C", "A"); | |
sg.addEdge("C", "B"); | |
System.out.println(sg); | |
System.out.println(h1 + " " +h2); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment