From 3b521de823970a7e3f27c75cddb32383cb39a433 Mon Sep 17 00:00:00 2001 From: pahimar Date: Wed, 29 May 2013 16:19:07 -0400 Subject: [PATCH] Need removeEdge and equals, and documentation, and testing, but looking good --- .../ee3/emc/graph/WeightedDirectedGraph.java | 87 +++++++++++++++---- .../pahimar/ee3/emc/graph/WeightedEdge.java | 46 +++++++--- 2 files changed, 102 insertions(+), 31 deletions(-) diff --git a/ee3_common/com/pahimar/ee3/emc/graph/WeightedDirectedGraph.java b/ee3_common/com/pahimar/ee3/emc/graph/WeightedDirectedGraph.java index b5401e96..ddba370b 100644 --- a/ee3_common/com/pahimar/ee3/emc/graph/WeightedDirectedGraph.java +++ b/ee3_common/com/pahimar/ee3/emc/graph/WeightedDirectedGraph.java @@ -1,12 +1,14 @@ package com.pahimar.ee3.emc.graph; import java.util.ArrayList; +import java.util.Collections; 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.Set; import java.util.SortedSet; import java.util.TreeSet; @@ -15,45 +17,94 @@ public class WeightedDirectedGraph implements Iterable { private final Map>> graph = new HashMap>>(); private List orderedNodes = new ArrayList(); - public boolean addNode(T node) - { + public boolean addNode(T node) { + // Ignore nodes already added if (graph.containsKey(node)) { return false; } orderedNodes.add(node); - graph.put(node, new TreeSet>(new Comparator>() - { + graph.put(node, new TreeSet>(new Comparator>() { + public int compare(WeightedEdge o1, WeightedEdge o2) { - return orderedNodes.indexOf(o1)-orderedNodes.indexOf(o2); + + return orderedNodes.indexOf(o1) - orderedNodes.indexOf(o2); } })); - + return true; } - - public void addEdge(T from, T to) - { + + public void addEdge(T from, T to) { + addEdge(from, to, 1); } - - public void addEdge(T from, T to, int weight) - { - if (!(graph.containsKey(from) && graph.containsKey(to))) - { + + public void addEdge(T from, T to, int weight) { + + if (!(graph.containsKey(from) && graph.containsKey(to))) { throw new NoSuchElementException("Missing nodes from graph"); } - - WeightedEdge edge = new WeightedEdge(weight, to); - graph.get(from).add(edge); + graph.get(from).add(new WeightedEdge(weight, to)); } - + + public boolean edgeExists(T from, T to) { + + if (!(graph.containsKey(from) && graph.containsKey(to))) { + throw new NoSuchElementException("Missing nodes from graph"); + } + + Iterator> edgeIterator = graph.get(from).iterator(); + + while (edgeIterator.hasNext()) { + if (edgeIterator.next().getTarget().equals(to)) { + return true; + } + } + + return false; + } + + public boolean edgeExists(T from, T to, int weight) { + + if (!(graph.containsKey(from) && graph.containsKey(to))) { + throw new NoSuchElementException("Missing nodes from graph"); + } + + return graph.get(from).contains(new WeightedEdge(weight, to)); + } + + public Set> edgesFrom(T from) { + + if (!graph.containsKey(from)) { + throw new NoSuchElementException("Missing node from graph"); + } + + return Collections.unmodifiableSortedSet(graph.get(from)); + } + @Override public Iterator iterator() { return orderedNodes.iterator(); } + public int size() { + + return graph.size(); + } + + public boolean isEmpty() { + + return graph.isEmpty(); + } + + @Override + public String toString() { + + return graph.toString(); + } + } diff --git a/ee3_common/com/pahimar/ee3/emc/graph/WeightedEdge.java b/ee3_common/com/pahimar/ee3/emc/graph/WeightedEdge.java index a2d41d9e..441a8663 100644 --- a/ee3_common/com/pahimar/ee3/emc/graph/WeightedEdge.java +++ b/ee3_common/com/pahimar/ee3/emc/graph/WeightedEdge.java @@ -1,35 +1,55 @@ package com.pahimar.ee3.emc.graph; - - public class WeightedEdge { - + private int weight; private T target; - + public WeightedEdge(int weight, T target) { - + this.weight = weight; this.target = target; } - + public int getWeight() { - + return weight; } - + public T getTarget() { - + return target; } - + public void setWeight(int weight) { - + this.weight = weight; } - + public void setTarget(T target) { - + this.target = target; } + + @Override + public boolean equals(Object object) { + + if (!(object instanceof WeightedEdge)) { + return false; + } + + WeightedEdge edge = (WeightedEdge) object; + + return ((this.weight == edge.weight) && (target.equals(edge.target))); + } + + @Override + public String toString() { + + StringBuilder stringBuilder = new StringBuilder(); + + stringBuilder.append(String.format("Weight: %s, Target: %s ", weight, target)); + + return stringBuilder.toString(); + } }