Commiting for a review

This commit is contained in:
pahimar 2013-05-29 13:42:44 -04:00
parent 28d7216096
commit 4bd9ae52f2

View file

@ -1,17 +1,47 @@
package com.pahimar.ee3.emc.graph;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.SortedSet;
import java.util.TreeSet;
public class WeightedDirectedGraph<T, E> implements Iterable<T> {
private final Map<T, SortedSet<E>> graph = new HashMap<T, SortedSet<E>>();
private List<T> orderedNodes = new ArrayList<T>();
public boolean addNode(T node)
{
// Ignore nodes already added
if (graph.containsKey(node)) {
return false;
}
orderedNodes.add(node);
graph.put(node, new TreeSet<E>(new Comparator<E>()
{
public int compare(E o1, E o2) {
return orderedNodes.indexOf(o1)-orderedNodes.indexOf(o2);
}
}));
return true;
}
public void addEdge(T from, T to)
{
if (!(graph.containsKey(from) && graph.containsKey(to)))
{
throw new NoSuchElementException("Missing nodes from graph");
}
//graph.get(from).add((E) to);
}
@Override
public Iterator<T> iterator() {