Need removeEdge and equals, and documentation, and testing, but looking good
This commit is contained in:
parent
376b37d4bf
commit
3b521de823
2 changed files with 102 additions and 31 deletions
|
@ -1,12 +1,14 @@
|
||||||
package com.pahimar.ee3.emc.graph;
|
package com.pahimar.ee3.emc.graph;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
|
||||||
|
@ -15,17 +17,18 @@ public class WeightedDirectedGraph<T> implements Iterable<T> {
|
||||||
private final Map<T, SortedSet<WeightedEdge<T>>> graph = new HashMap<T, SortedSet<WeightedEdge<T>>>();
|
private final Map<T, SortedSet<WeightedEdge<T>>> graph = new HashMap<T, SortedSet<WeightedEdge<T>>>();
|
||||||
private List<T> orderedNodes = new ArrayList<T>();
|
private List<T> orderedNodes = new ArrayList<T>();
|
||||||
|
|
||||||
public boolean addNode(T node)
|
public boolean addNode(T node) {
|
||||||
{
|
|
||||||
// Ignore nodes already added
|
// Ignore nodes already added
|
||||||
if (graph.containsKey(node)) {
|
if (graph.containsKey(node)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
orderedNodes.add(node);
|
orderedNodes.add(node);
|
||||||
graph.put(node, new TreeSet<WeightedEdge<T>>(new Comparator<WeightedEdge<T>>()
|
graph.put(node, new TreeSet<WeightedEdge<T>>(new Comparator<WeightedEdge<T>>() {
|
||||||
{
|
|
||||||
public int compare(WeightedEdge<T> o1, WeightedEdge<T> o2) {
|
public int compare(WeightedEdge<T> o1, WeightedEdge<T> o2) {
|
||||||
|
|
||||||
return orderedNodes.indexOf(o1) - orderedNodes.indexOf(o2);
|
return orderedNodes.indexOf(o1) - orderedNodes.indexOf(o2);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
@ -33,21 +36,53 @@ public class WeightedDirectedGraph<T> implements Iterable<T> {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addEdge(T from, T to)
|
public void addEdge(T from, T to) {
|
||||||
{
|
|
||||||
addEdge(from, to, 1);
|
addEdge(from, to, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addEdge(T from, T to, int weight)
|
public void addEdge(T from, T to, int weight) {
|
||||||
{
|
|
||||||
if (!(graph.containsKey(from) && graph.containsKey(to)))
|
if (!(graph.containsKey(from) && graph.containsKey(to))) {
|
||||||
{
|
|
||||||
throw new NoSuchElementException("Missing nodes from graph");
|
throw new NoSuchElementException("Missing nodes from graph");
|
||||||
}
|
}
|
||||||
|
|
||||||
WeightedEdge<T> edge = new WeightedEdge<T>(weight, to);
|
graph.get(from).add(new WeightedEdge<T>(weight, to));
|
||||||
|
}
|
||||||
|
|
||||||
graph.get(from).add(edge);
|
public boolean edgeExists(T from, T to) {
|
||||||
|
|
||||||
|
if (!(graph.containsKey(from) && graph.containsKey(to))) {
|
||||||
|
throw new NoSuchElementException("Missing nodes from graph");
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterator<WeightedEdge<T>> 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<T>(weight, to));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<WeightedEdge<T>> edgesFrom(T from) {
|
||||||
|
|
||||||
|
if (!graph.containsKey(from)) {
|
||||||
|
throw new NoSuchElementException("Missing node from graph");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Collections.unmodifiableSortedSet(graph.get(from));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -56,4 +91,20 @@ public class WeightedDirectedGraph<T> implements Iterable<T> {
|
||||||
return orderedNodes.iterator();
|
return orderedNodes.iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
|
||||||
|
return graph.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
|
||||||
|
return graph.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
|
||||||
|
return graph.toString();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package com.pahimar.ee3.emc.graph;
|
package com.pahimar.ee3.emc.graph;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class WeightedEdge<T> {
|
public class WeightedEdge<T> {
|
||||||
|
|
||||||
private int weight;
|
private int weight;
|
||||||
|
@ -32,4 +30,26 @@ public class WeightedEdge<T> {
|
||||||
|
|
||||||
this.target = 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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue