T - the types of the nodes in the graphpublic class DirectedGraph<T> extends Object implements Collection<T>
Example of use:
Graph<Integer> g = new Graph<Integer>();
g.add(7, 5, 3, 11, 8, 2, 9, 10);
g.buildEdges(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if (o1 == 5 && o2 == 3) {
return 1;
}
return 0;
}
});
System.out.println(g.topologicalSort());
Prints out: [7, 3, 11, 8, 2, 9, 10, 5] (5 is pushed to the end
because it is after 3). Note that the elements that are already in the right
order remain unchanged.
The sorting method is deterministic, i.e. it always gives the same result and tries to minimize necessary changes.
| Modifier and Type | Class and Description |
|---|---|
static class |
DirectedGraph.Edge<T>
A generic object representing an edge in the graph.
|
static class |
DirectedGraph.Node<T>
An object representing the nodes of the graph.
|
| Constructor and Description |
|---|
DirectedGraph()
Constructs an empty graph collection.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(T... elements)
Add a set of elements to the nodes of this graph.
|
boolean |
add(T element)
Adds an element to the nodes.
|
boolean |
addAll(Collection<? extends T> elements)
Adds a set of elements to the nodes of this graph.
|
void |
addEdge(T sourceElement,
T destinationElement)
Add an edge between the given elements (nodes).
|
void |
addEdges(T sourceElement,
T... destinationElements)
Adds some edges form the source elements to the given destination
elements.
|
<U extends T> |
buildEdges(Comparator<U> nodeComparator)
Automatically builds the edges between all the nodes of the graph by
using the given comparator.
|
void |
clear()
Clears all the nodes of this graph.
|
boolean |
contains(Object o)
Tells if this graph contains the given object as a node.
|
boolean |
containsAll(Collection<?> c)
Tells if this graph contains the given collection as a node.
|
static <T> void |
dumpCycles(List<DirectedGraph.Node<T>> nodes,
Function<T,String> toString)
Dumps the found cycles to System.out.
|
boolean |
equals(Object obj)
Equals two graphs.
|
List<T> |
getDestinationElements(T sourceElement) |
List<T> |
getSourceElements(T destinationElement) |
boolean |
hasEdge(T sourceElement,
T destinationElement)
Tells if this graph contains an edge between the given source and the
destination elements/nodes.
|
int |
hashCode() |
boolean |
isEmpty()
Returns true if this graph has no nodes.
|
Iterator<T> |
iterator()
Return an iterator to iterate on graph nodes.
|
static void |
main(String[] args)
Just for testing.
|
boolean |
remove(Object o)
Removes an object from the graph nodes (if exists).
|
boolean |
removeAll(Collection<?> c)
Removes all the objects from the graph nodes (if exists).
|
boolean |
retainAll(Collection<?> c)
Keeps only the elements of the given collection in the graph.
|
int |
size()
Returns the nodes count in this graph.
|
Object[] |
toArray()
Returns the graph's nodes as an array (not sorted).
|
<U> U[] |
toArray(U[] a)
Returns the graph's nodes as a well-typed array (not sorted).
|
List<T> |
topologicalSort(Consumer<DirectedGraph.Node<T>> cycleHandler)
Sorts this graph using a topological sort algorithm given in this
StackOverflow thread.
|
String |
toString() |
clone, finalize, getClass, notify, notifyAll, wait, wait, waitparallelStream, removeIf, spliterator, streampublic boolean addAll(Collection<? extends T> elements)
addAll in interface Collection<T>public boolean add(T element)
add in interface Collection<T>public void clear()
clear in interface Collection<T>public boolean contains(Object o)
contains in interface Collection<T>public boolean containsAll(Collection<?> c)
containsAll in interface Collection<T>public boolean equals(Object obj)
equals in interface Collection<T>equals in class Objectpublic int hashCode()
hashCode in interface Collection<T>hashCode in class Objectpublic boolean isEmpty()
isEmpty in interface Collection<T>public boolean remove(Object o)
remove in interface Collection<T>public boolean removeAll(Collection<?> c)
removeAll in interface Collection<T>public boolean retainAll(Collection<?> c)
retainAll in interface Collection<T>public int size()
size in interface Collection<T>public Object[] toArray()
toArray in interface Collection<T>public <U> U[] toArray(U[] a)
toArray in interface Collection<T>public void add(T... elements)
public void addEdge(T sourceElement, T destinationElement)
sourceElement - the source element/nodedestinationElement - the destination element/nodepublic <U extends T> void buildEdges(Comparator<U> nodeComparator)
nodeComparator - a comparator which is used to build the edgespublic void addEdges(T sourceElement, T... destinationElements)
sourceElement - the source of the edgesdestinationElements - the destination elements of the edgespublic boolean hasEdge(T sourceElement, T destinationElement)
sourceElement - the source nodedestinationElement - the destination nodepublic List<T> topologicalSort(Consumer<DirectedGraph.Node<T>> cycleHandler)
CycleFoundException - thrown if a cycle is found in the graph (in that case no
topological sort is possible)public static <T> void dumpCycles(List<DirectedGraph.Node<T>> nodes, Function<T,String> toString)
nodes - the nodes in which to look for cyclestoString - the element's toString functionpublic static void main(String[] args)