From e7fa8784a29df625efd098af1d06eaa04b453a23 Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Mon, 3 Jul 2017 16:32:36 -0400 Subject: [PATCH] Javadoc clean-up --- .../algorithms/data_structures/AVLTree.java | 6 +- .../algorithms/data_structures/BTree.java | 6 +- .../data_structures/BinaryHeap.java | 6 +- .../data_structures/BinarySearchTree.java | 6 +- .../data_structures/CompactSuffixTrie.java | 6 +- .../data_structures/DisjointSet.java | 14 +-- .../data_structures/FenwickTree.java | 8 +- .../algorithms/data_structures/Graph.java | 6 +- .../data_structures/HashArrayMappedTrie.java | 8 +- .../algorithms/data_structures/HashMap.java | 6 +- .../data_structures/ImplicitKeyTreap.java | 12 +- .../data_structures/IntervalTree.java | 6 +- .../algorithms/data_structures/KdTree.java | 3 +- .../algorithms/data_structures/LCPArray.java | 45 +++---- .../algorithms/data_structures/List.java | 32 +++-- .../algorithms/data_structures/Matrix.java | 6 +- .../data_structures/PatriciaTrie.java | 6 +- .../algorithms/data_structures/QuadTree.java | 6 +- .../algorithms/data_structures/Queue.java | 8 ++ .../algorithms/data_structures/RadixTrie.java | 10 +- .../data_structures/RedBlackTree.java | 6 +- .../data_structures/SegmentTree.java | 8 +- .../algorithms/data_structures/SkipList.java | 6 +- .../data_structures/SkipListMap.java | 10 +- .../algorithms/data_structures/SplayTree.java | 6 +- .../algorithms/data_structures/Stack.java | 12 +- .../data_structures/SuffixArray.java | 26 ++-- .../data_structures/SuffixTree.java | 26 ++-- .../data_structures/SuffixTrie.java | 13 +- .../algorithms/data_structures/Treap.java | 6 +- .../algorithms/data_structures/TreeMap.java | 10 +- .../algorithms/data_structures/Trie.java | 16 +-- .../algorithms/data_structures/TrieMap.java | 10 +- .../data_structures/interfaces/IHeap.java | 6 +- .../data_structures/interfaces/IList.java | 6 +- .../data_structures/interfaces/IMap.java | 6 +- .../data_structures/interfaces/IQueue.java | 6 +- .../data_structures/interfaces/ISet.java | 6 +- .../data_structures/interfaces/IStack.java | 6 +- .../interfaces/ISuffixTree.java | 6 +- .../data_structures/interfaces/ITree.java | 6 +- .../jwetherell/algorithms/graph/AStar.java | 6 +- .../algorithms/graph/BellmanFord.java | 8 +- .../graph/BreadthFirstTraversal.java | 49 +++++++- .../algorithms/graph/ConnectedComponents.java | 6 +- .../algorithms/graph/CycleDetection.java | 6 +- .../algorithms/graph/DepthFirstTraversal.java | 116 +++++++++++++++--- .../jwetherell/algorithms/graph/Dijkstra.java | 8 +- .../algorithms/graph/EdmondsKarp.java | 2 +- .../algorithms/graph/FloydWarshall.java | 8 +- .../jwetherell/algorithms/graph/Johnson.java | 8 +- .../jwetherell/algorithms/graph/Kruskal.java | 4 +- src/com/jwetherell/algorithms/graph/Prim.java | 2 +- .../algorithms/graph/PushRelabel.java | 2 +- .../algorithms/graph/TopologicalSort.java | 6 +- .../algorithms/graph/TurboMatching.java | 30 +++-- .../algorithms/mathematics/Coprimes.java | 4 +- .../mathematics/Exponentiation.java | 7 +- .../mathematics/FastFourierTransform.java | 4 +- .../mathematics/GreatestCommonDivisor.java | 4 +- .../algorithms/mathematics/Knapsack.java | 12 +- .../algorithms/mathematics/Modular.java | 2 +- .../algorithms/mathematics/Permutations.java | 2 +- .../algorithms/mathematics/Primes.java | 6 +- .../mathematics/RamerDouglasPeucker.java | 6 +- .../algorithms/numbers/Complex.java | 2 +- .../algorithms/search/BinarySearch.java | 4 +- .../search/InterpolationSearch.java | 4 +- .../algorithms/search/LinearSearch.java | 4 +- .../algorithms/search/LowerBound.java | 15 +-- .../algorithms/search/QuickSelect.java | 4 +- .../algorithms/search/UpperBound.java | 12 +- .../sequence/ArithmeticProgression.java | 4 +- .../sequence/FibonacciSequence.java | 3 +- .../LargestSumContiguousSubarray.java | 2 +- .../sequence/LongestCommonSubsequence.java | 2 +- .../LongestIncreasingSubsequence.java | 2 +- .../LongestPalindromicSubsequence.java | 25 ++-- .../sequence/SubsequenceCounter.java | 2 +- .../algorithms/sorts/AmericanFlagSort.java | 21 ++-- .../algorithms/sorts/BubbleSort.java | 20 +-- .../algorithms/sorts/CountingSort.java | 21 ++-- .../jwetherell/algorithms/sorts/HeapSort.java | 20 +-- .../algorithms/sorts/InsertionSort.java | 20 +-- .../algorithms/sorts/MergeSort.java | 20 +-- .../algorithms/sorts/QuickSort.java | 20 +-- .../algorithms/sorts/RadixSort.java | 21 ++-- .../algorithms/sorts/ShellSort.java | 22 ++-- .../algorithms/strings/KnuthMorrisPratt.java | 9 +- .../algorithms/strings/Manacher.java | 6 +- .../algorithms/strings/Rotation.java | 19 +-- .../algorithms/strings/StringFunctions.java | 2 +- .../data_structures/test/LCPArrayTest.java | 8 +- .../data_structures/test/common/HeapTest.java | 22 ---- .../test/common/IteratorTest.java | 10 -- .../test/common/JavaCollectionTest.java | 16 --- .../test/common/JavaMapTest.java | 17 --- .../test/common/ListIteratorTest.java | 13 -- .../data_structures/test/common/ListTest.java | 14 --- .../data_structures/test/common/MapTest.java | 16 --- .../test/common/QueueTest.java | 18 --- .../data_structures/test/common/SetTest.java | 14 --- .../test/common/StackTest.java | 15 --- .../data_structures/test/common/TreeTest.java | 16 --- .../graph/test/BreadthFirstTraversalTest.java | 36 +++++- .../graph/test/DepthFirstTraversalTest.java | 89 ++++++++++++++ 106 files changed, 731 insertions(+), 596 deletions(-) create mode 100644 test/com/jwetherell/algorithms/graph/test/DepthFirstTraversalTest.java diff --git a/src/com/jwetherell/algorithms/data_structures/AVLTree.java b/src/com/jwetherell/algorithms/data_structures/AVLTree.java index b5df686a..f3b74e3e 100644 --- a/src/com/jwetherell/algorithms/data_structures/AVLTree.java +++ b/src/com/jwetherell/algorithms/data_structures/AVLTree.java @@ -12,9 +12,9 @@ * trees are more rigidly balanced, they are faster than red-black trees for * lookup intensive applications. However, red-black trees are faster for * insertion and removal. - * - * http://en.wikipedia.org/wiki/AVL_tree - * + *

+ * @see AVL Tree (Wikipedia) + *
* @author Justin Wetherell */ public class AVLTree> extends BinarySearchTree { diff --git a/src/com/jwetherell/algorithms/data_structures/BTree.java b/src/com/jwetherell/algorithms/data_structures/BTree.java index 088f9f37..d15d398c 100644 --- a/src/com/jwetherell/algorithms/data_structures/BTree.java +++ b/src/com/jwetherell/algorithms/data_structures/BTree.java @@ -14,9 +14,9 @@ * two children. Unlike self-balancing binary search trees, the B-tree is * optimized for systems that read and write large blocks of data. It is * commonly used in databases and file-systems. - * - * http://en.wikipedia.org/wiki/B-tree - * + *

+ * @see B-Tree (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java b/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java index dd1cf53a..61f38a16 100644 --- a/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java +++ b/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java @@ -16,9 +16,9 @@ * the tree is not complete, the nodes of that level are filled from left to * right. 2) The heap property: each node is right than or equal to each of its * children according to a comparison predicate defined for the data structure. - * - * http://en.wikipedia.org/wiki/Binary_heap - * + *

+ * @see Binary Heap (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/BinarySearchTree.java b/src/com/jwetherell/algorithms/data_structures/BinarySearchTree.java index 96089b8f..d2b832a5 100644 --- a/src/com/jwetherell/algorithms/data_structures/BinarySearchTree.java +++ b/src/com/jwetherell/algorithms/data_structures/BinarySearchTree.java @@ -19,9 +19,9 @@ * keys less than the node's key. 2) The right subtree of a node contains only * nodes with keys greater than the node's key. 3) Both the left and right * subtrees must also be binary search trees. - * - * http://en.wikipedia.org/wiki/Binary_search_tree - * + *

+ * @see Binary Search Tree (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/CompactSuffixTrie.java b/src/com/jwetherell/algorithms/data_structures/CompactSuffixTrie.java index c88b6bcb..bc64b598 100644 --- a/src/com/jwetherell/algorithms/data_structures/CompactSuffixTrie.java +++ b/src/com/jwetherell/algorithms/data_structures/CompactSuffixTrie.java @@ -8,9 +8,9 @@ * string in a way that allows for a particularly fast implementation of many * important string operations. This implementation is based upon a patricia * trie which IS a compact trie. - * - * http://en.wikipedia.org/wiki/Suffix_trie - * + *

+ * @see Suffix Trie (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/DisjointSet.java b/src/com/jwetherell/algorithms/data_structures/DisjointSet.java index 6b4c1dbb..c7c02a08 100644 --- a/src/com/jwetherell/algorithms/data_structures/DisjointSet.java +++ b/src/com/jwetherell/algorithms/data_structures/DisjointSet.java @@ -3,14 +3,14 @@ /** * In computer science, a disjoint-set data structure, also called a union–find data structure or merge–find set, is a data structure that keeps track of a set of * elements partitioned into a number of disjoint (non-overlapping) subsets. - * - * It supports two useful operations: + *

+ * It supports two useful operations:
* Find: Determine which subset a particular element is in. Find typically returns an item from this set that serves as its "representative"; by comparing the - * result of two Find operations, one can determine whether two elements are in the same subset. - * Union: Join two subsets into a single subset. - * - * http://en.wikipedia.org/wiki/Disjoint-set_data_structure - * + * result of two Find operations, one can determine whether two elements are in the same subset.
+ * Union: Join two subsets into a single subset.
+ *

+ * @see Disjoint Set (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/FenwickTree.java b/src/com/jwetherell/algorithms/data_structures/FenwickTree.java index cf936211..60634d40 100644 --- a/src/com/jwetherell/algorithms/data_structures/FenwickTree.java +++ b/src/com/jwetherell/algorithms/data_structures/FenwickTree.java @@ -10,13 +10,13 @@ * for calculation and manipulation of the prefix sums of a table of values. Fenwick trees * primarily solve the problem of balancing prefix sum calculation efficiency with element * modification efficiency. - * - * http://en.wikipedia.org/wiki/Fenwick_tree - * + *

* This class is meant to be somewhat generic, all you'd have to do is extend * the Data abstract class to store your custom data. I've included a range sum * implementations. - * + *

+ * @see Fenwick Tree (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/Graph.java b/src/com/jwetherell/algorithms/data_structures/Graph.java index d52f7baf..c28bbc6e 100644 --- a/src/com/jwetherell/algorithms/data_structures/Graph.java +++ b/src/com/jwetherell/algorithms/data_structures/Graph.java @@ -10,9 +10,9 @@ * Graph. Could be directed or undirected depending on the TYPE enum. A graph is * an abstract representation of a set of objects where some pairs of the * objects are connected by links. - * - * http://en.wikipedia.org/wiki/Graph_(mathematics) - * + *

+ * @see Graph (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/HashArrayMappedTrie.java b/src/com/jwetherell/algorithms/data_structures/HashArrayMappedTrie.java index 3f177faa..b33d361b 100644 --- a/src/com/jwetherell/algorithms/data_structures/HashArrayMappedTrie.java +++ b/src/com/jwetherell/algorithms/data_structures/HashArrayMappedTrie.java @@ -10,12 +10,12 @@ * A hash array mapped trie (HAMT) is an implementation of an associative * array that combines the characteristics of a hash table and an array mapped * trie. It is a refined version of the more general notion of a hash tree. - * + *

* This implementation is 32-bit and steps in 5-bit intervals, maximum tree * height of 7. - * - * http://en.wikipedia.org/wiki/Hash_array_mapped_trie - * + *

+ * @see Hash Array Mapped Trie (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/HashMap.java b/src/com/jwetherell/algorithms/data_structures/HashMap.java index 2245eb34..76c669de 100644 --- a/src/com/jwetherell/algorithms/data_structures/HashMap.java +++ b/src/com/jwetherell/algorithms/data_structures/HashMap.java @@ -9,9 +9,9 @@ * Hash Map using either chaining or probing. hash map is a data structure that * uses a hash function to map identifying values, known as keys, to their * associated values. - * - * http://en.wikipedia.org/wiki/Hash_table - * + *

+ * @see Hash Map/Table (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/ImplicitKeyTreap.java b/src/com/jwetherell/algorithms/data_structures/ImplicitKeyTreap.java index d709b44b..bc98df10 100644 --- a/src/com/jwetherell/algorithms/data_structures/ImplicitKeyTreap.java +++ b/src/com/jwetherell/algorithms/data_structures/ImplicitKeyTreap.java @@ -9,18 +9,18 @@ /** * A Treap is a self-balancing binary search tree that uses randomization to maintain * a low height. In this version, it is used emulate the operations of an array and linked list. - * + *

* Time Complexity: Assuming the join/merge functions have constant complexity. * add(value), add(index,value), remove(index), set(index,value), get(index) all have O(log N). * remove(value), get(value), contains(value) all have O(N). - * + *

* Space Complexity: O(N) - * + *

* Note: This implementation is 0-based, meaning that all * indices from 0 to size() - 1, inclusive, are accessible. - * - * http://en.wikipedia.org/wiki/Treap - * + *

+ * @see Treap (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/IntervalTree.java b/src/com/jwetherell/algorithms/data_structures/IntervalTree.java index 9a341efa..a04113ba 100644 --- a/src/com/jwetherell/algorithms/data_structures/IntervalTree.java +++ b/src/com/jwetherell/algorithms/data_structures/IntervalTree.java @@ -12,9 +12,9 @@ * An interval tree is an ordered tree data structure to hold intervals. * Specifically, it allows one to efficiently find all intervals that overlap * with any given interval or point. - * - * http://en.wikipedia.org/wiki/Interval_tree - * + *

+ * @see Interval Tree (Wikipedia) + *
* @author Justin Wetherell */ public class IntervalTree { diff --git a/src/com/jwetherell/algorithms/data_structures/KdTree.java b/src/com/jwetherell/algorithms/data_structures/KdTree.java index 9fecb819..b7558302 100644 --- a/src/com/jwetherell/algorithms/data_structures/KdTree.java +++ b/src/com/jwetherell/algorithms/data_structures/KdTree.java @@ -21,9 +21,10 @@ * useful data structure for several applications, such as searches involving a * multidimensional search key (e.g. range searches and nearest neighbor * searches). k-d trees are a special case of binary space partitioning trees. + *

+ * @see K-D Tree (Wikipedia) *
* @author Justin Wetherell - * @see K-d_tree (Wikipedia) */ public class KdTree implements Iterable { diff --git a/src/com/jwetherell/algorithms/data_structures/LCPArray.java b/src/com/jwetherell/algorithms/data_structures/LCPArray.java index 88d299c8..9dd1632d 100644 --- a/src/com/jwetherell/algorithms/data_structures/LCPArray.java +++ b/src/com/jwetherell/algorithms/data_structures/LCPArray.java @@ -7,32 +7,33 @@ * data structure to the suffix array. It stores the lengths of the longest common * prefixes (LCPs) between all pairs of consecutive suffixes in a sorted suffix array. *

- * https://en.wikipedia.org/wiki/LCP_array + * @see LCP Array (Wikipedia) *
* @author Jakub Szarawarski * @author Justin Wetherell */ -public class LCPArray { +public class LCPArray { private static final char DEFAULT_END_SEQ_CHAR = '$'; - private char END_SEQ_CHAR; - private SuffixArray suffixArrayBuilder; - private ArrayList LCP; + private final char endSeqChar; - public LCPArray(CharSequence sequence){ + private SuffixArray suffixArray; + private ArrayList lcp; + + public LCPArray(C sequence){ this(sequence, DEFAULT_END_SEQ_CHAR); } - public LCPArray(CharSequence sequence, char endChar) { - END_SEQ_CHAR = endChar; - suffixArrayBuilder = new SuffixArray(sequence, END_SEQ_CHAR); + public LCPArray(C sequence, char endChar) { + endSeqChar = endChar; + suffixArray = new SuffixArray(sequence, endSeqChar); } public ArrayList getLCPArray() { - if (LCP == null) + if (lcp == null) LCPAlgorithm(); - return LCP; + return lcp; } private void LCPAlgorithm() { @@ -41,20 +42,20 @@ private void LCPAlgorithm() { } private ArrayList getLCPR() { - final ArrayList KMRArray = suffixArrayBuilder.getKMRarray(); - final ArrayList suffixArray = suffixArrayBuilder.getSuffixArray(); - final String string = suffixArrayBuilder.getString(); - final int length = KMRArray.size(); + final ArrayList KMRArrayList = suffixArray.getKMRarray(); + final ArrayList suffixArrayList = suffixArray.getSuffixArray(); + final String string = suffixArray.getString(); + final int length = KMRArrayList.size(); final ArrayList LCPR = new ArrayList(); // helper array, LCP[i] = LCPR[suffixArray[i]] int startingValue = 0; for (int i=0; i getLCPR() { } private void getLCPfromLCPR(ArrayList LCPR) { - final ArrayList suffixArray = suffixArrayBuilder.getSuffixArray(); - final int length = suffixArray.size(); + final ArrayList suffixArrayList = suffixArray.getSuffixArray(); + final int length = suffixArrayList.size(); - LCP = new ArrayList(); - LCP.add(null); //no value for LCP[0] + lcp = new ArrayList(); + lcp.add(null); //no value for LCP[0] for (int i=1; i + * @see Sequence (Wikipedia) + *
+ * @author Justin Wetherell + */ @SuppressWarnings("unchecked") public abstract class List implements IList { @@ -11,9 +19,9 @@ public abstract class List implements IList { * A dynamic array, growable array, resizable array, dynamic table, or array * list is a random access, variable-size list data structure that allows * elements to be added or removed. - * - * http://en.wikipedia.org/wiki/Dynamic_array - * + *

+ * @see Dynamic Array (Wikipedia) + *
* @author Justin Wetherell */ public static class ArrayList extends List { @@ -280,9 +288,9 @@ public T set(int index, T value) { /** * Linked List (Singly link). A linked list is a data structure consisting * of a group of nodes which together represent a sequence. - * - * http://en.wikipedia.org/wiki/Linked_list - * + *

+ * @see Linked List (Wikipedia) + *
* @author Justin Wetherell */ public static class SinglyLinkedList extends List { @@ -477,9 +485,9 @@ public String toString() { /** * Linked List (singly link). A linked list is a data structure consisting * of a group of nodes which together represent a sequence. - * - * http://en.wikipedia.org/wiki/Linked_list - * + *

+ * @see Linked List (Wikipedia) + *
* @author Justin Wetherell */ public static class JavaCompatibleSinglyLinkedList extends java.util.AbstractSequentialList { @@ -688,9 +696,9 @@ public T previous() { /** * Linked List (doubly link). A linked list is a data structure consisting * of a group of nodes which together represent a sequence. - * - * http://en.wikipedia.org/wiki/Linked_list - * + *

+ * @see Linked List (Wikipedia) + *
* @author Justin Wetherell */ public static class DoublyLinkedList extends List { diff --git a/src/com/jwetherell/algorithms/data_structures/Matrix.java b/src/com/jwetherell/algorithms/data_structures/Matrix.java index 717b757c..a835210b 100644 --- a/src/com/jwetherell/algorithms/data_structures/Matrix.java +++ b/src/com/jwetherell/algorithms/data_structures/Matrix.java @@ -7,9 +7,9 @@ /** * Matrx. This Matrix implementation is designed to be more efficient * in cache. A matrix is a rectangular array of numbers, symbols, or expressions. - * - * http://en.wikipedia.org/wiki/Matrix_(mathematics) - * + *

+ * @see Matrix (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/PatriciaTrie.java b/src/com/jwetherell/algorithms/data_structures/PatriciaTrie.java index 4354d037..b3bce4bc 100644 --- a/src/com/jwetherell/algorithms/data_structures/PatriciaTrie.java +++ b/src/com/jwetherell/algorithms/data_structures/PatriciaTrie.java @@ -9,9 +9,9 @@ * non-terminating (black) node with only one child is merged with its child. * The result is that every internal non-terminating (black) node has at least * two children. Each terminating node (white) represents the end of a string. - * - * http://en.wikipedia.org/wiki/Radix_tree - * + *

+ * @see Radix Tree / Patricia Trie (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/QuadTree.java b/src/com/jwetherell/algorithms/data_structures/QuadTree.java index 69f664d2..0535a245 100644 --- a/src/com/jwetherell/algorithms/data_structures/QuadTree.java +++ b/src/com/jwetherell/algorithms/data_structures/QuadTree.java @@ -11,9 +11,9 @@ * A quadtree is a tree data structure in which each internal node has exactly four children. Quadtrees * are most often used to partition a two dimensional space by recursively subdividing it into four * quadrants or regions. The regions may be square or rectangular, or may have arbitrary shapes. - * - * http://en.wikipedia.org/wiki/Quadtree - * + *

+ * @see QuadTree (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/Queue.java b/src/com/jwetherell/algorithms/data_structures/Queue.java index 7a05f86f..1a078f71 100644 --- a/src/com/jwetherell/algorithms/data_structures/Queue.java +++ b/src/com/jwetherell/algorithms/data_structures/Queue.java @@ -2,6 +2,14 @@ import com.jwetherell.algorithms.data_structures.interfaces.IQueue; +/** + * In computer science, a queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations + * on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. + *

+ * @see Queue (Wikipedia) + *
+ * @author Justin Wetherell + */ @SuppressWarnings("unchecked") public interface Queue extends IQueue { diff --git a/src/com/jwetherell/algorithms/data_structures/RadixTrie.java b/src/com/jwetherell/algorithms/data_structures/RadixTrie.java index 1188ca74..012f0d01 100644 --- a/src/com/jwetherell/algorithms/data_structures/RadixTrie.java +++ b/src/com/jwetherell/algorithms/data_structures/RadixTrie.java @@ -10,13 +10,13 @@ * This makes them much more efficient for small sets (especially if the strings * are long) and for sets of strings that share long prefixes. This particular * radix tree is used to represent an associative array. - * + *

* This implementation is a composition of a PatriciaTrie as the backing * structure. - * - * http://en.wikipedia.org/wiki/Radix_tree - * http://en.wikipedia.org/wiki/Associative_array - * + *

+ * @see Radix Tree / Patricia Trie (Wikipedia) + * @see Associative Array (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/RedBlackTree.java b/src/com/jwetherell/algorithms/data_structures/RedBlackTree.java index 451db6db..c3c7400c 100644 --- a/src/com/jwetherell/algorithms/data_structures/RedBlackTree.java +++ b/src/com/jwetherell/algorithms/data_structures/RedBlackTree.java @@ -14,9 +14,9 @@ * often compared with AVL trees. AVL trees are more rigidly balanced, they are * faster than red-black trees for lookup intensive applications. However, * red-black trees are faster for insertion and removal. - * - * http://en.wikipedia.org/wiki/Red%E2%80%93black_tree - * + *

+ * @see Red-Black Tree (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/SegmentTree.java b/src/com/jwetherell/algorithms/data_structures/SegmentTree.java index 3ae10898..9a04b8e6 100644 --- a/src/com/jwetherell/algorithms/data_structures/SegmentTree.java +++ b/src/com/jwetherell/algorithms/data_structures/SegmentTree.java @@ -18,13 +18,13 @@ * stored segments contain a given point. It is, in principle, a static * structure; that is, its content cannot be modified once the structure is * built. - * - * http://en.wikipedia.org/wiki/Segment_tree - * + *

* This class is meant to be somewhat generic, all you'd have to do is extend * the Data abstract class to store your custom data. I've also included a range * minimum, range maximum, range sum, and interval stabbing implementations. - * + *

+ * @see Segment Tree (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/SkipList.java b/src/com/jwetherell/algorithms/data_structures/SkipList.java index 5743fa32..29b61f70 100644 --- a/src/com/jwetherell/algorithms/data_structures/SkipList.java +++ b/src/com/jwetherell/algorithms/data_structures/SkipList.java @@ -9,9 +9,9 @@ * using a hierarchy of linked lists that connect increasingly sparse * subsequences of the items. These auxiliary lists allow item lookup with * efficiency comparable to balanced binary search trees. - * - * http://en.wikipedia.org/wiki/Skip_list - * + *

+ * @see Skip List (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/SkipListMap.java b/src/com/jwetherell/algorithms/data_structures/SkipListMap.java index df811c23..d7c77eb9 100644 --- a/src/com/jwetherell/algorithms/data_structures/SkipListMap.java +++ b/src/com/jwetherell/algorithms/data_structures/SkipListMap.java @@ -6,12 +6,12 @@ /** * A set used to store key->values pairs, this is an implementation of an * associative array. - * + *

* This implementation is a composition of a Skip List as the backing structure. - * - * http://en.wikipedia.org/wiki/Skip_list - * http://en.wikipedia.org/wiki/Associative_array - * + *

+ * @see Skip List (Wikipedia) + * @see Associative Array (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/SplayTree.java b/src/com/jwetherell/algorithms/data_structures/SplayTree.java index 90ed9c27..d6e279db 100644 --- a/src/com/jwetherell/algorithms/data_structures/SplayTree.java +++ b/src/com/jwetherell/algorithms/data_structures/SplayTree.java @@ -3,9 +3,9 @@ /** * A splay tree is a self-adjusting binary search tree (BST) with the additional * property that recently accessed elements are quick to access again. - * - * http://en.wikipedia.org/wiki/Splay_tree - * + *

+ * @see Splay Tree (Wikipedia) + *
* @author Justin Wetherell */ public class SplayTree> extends BinarySearchTree { diff --git a/src/com/jwetherell/algorithms/data_structures/Stack.java b/src/com/jwetherell/algorithms/data_structures/Stack.java index 62e0cc92..2ad749c2 100644 --- a/src/com/jwetherell/algorithms/data_structures/Stack.java +++ b/src/com/jwetherell/algorithms/data_structures/Stack.java @@ -4,12 +4,20 @@ import com.jwetherell.algorithms.data_structures.interfaces.IStack; +/** + * In computer science, a stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes + * the most recently added element that was not yet removed. + *

+ * @see Stack (Wikipedia) + *
+ * @author Justin Wetherell + */ @SuppressWarnings("unchecked") public interface Stack extends IStack { /** * This stack implementation is backed by an array. - * + *

* @author Justin Wetherell */ public static class ArrayStack implements Stack { @@ -187,7 +195,7 @@ public String toString() { /** * This stack implementation is backed by a linked list. - * + *

* @author Justin Wetherell */ public static class LinkedStack implements Stack { diff --git a/src/com/jwetherell/algorithms/data_structures/SuffixArray.java b/src/com/jwetherell/algorithms/data_structures/SuffixArray.java index a7212f18..2d01ed9f 100644 --- a/src/com/jwetherell/algorithms/data_structures/SuffixArray.java +++ b/src/com/jwetherell/algorithms/data_structures/SuffixArray.java @@ -9,7 +9,7 @@ * It is a data structure used, among others, in full text indices, data compression * algorithms and within the field of bibliometrics. *

- * https://en.wikipedia.org/wiki/Suffix_array + * @see Suffix Array (Wikipedia) *

* NOTE: This implementation returns starting indexes instead of full suffixes *
@@ -21,17 +21,18 @@ public class SuffixArray { private static final StringBuilder STRING_BUILDER = new StringBuilder(); private static final char DEFAULT_END_SEQ_CHAR = '$'; - private char END_SEQ_CHAR; + private final char endSeqChar; + private String string; - private ArrayList suffixArray = null; - private ArrayList KMRarray = null; + private ArrayList suffixArray; + private ArrayList KMRarray; public SuffixArray(CharSequence sequence) { this(sequence, DEFAULT_END_SEQ_CHAR); } public SuffixArray(CharSequence sequence, char endChar) { - END_SEQ_CHAR = endChar; + endSeqChar = endChar; string = buildStringWithEndChar(sequence); } @@ -78,8 +79,9 @@ private void KMRalgorithm() { KMRarray = new ArrayList(KMR.subList(0, length)); suffixArray = new ArrayList(); - for (KMRsWithIndex kmr : KMRinvertedList) - suffixArray.add(kmr.index); + for (KMRsWithIndex kmr : KMRinvertedList) { + suffixArray.add(new Integer(kmr.index)); + } } /** @@ -93,13 +95,13 @@ private void KMRalgorithm() { private ArrayList getKMR(ArrayList KMRinvertedList, int length) { final ArrayList KMR = new ArrayList(length*2); for (int i=0; i<2*length; i++) - KMR.add(-1); + KMR.add(new Integer(-1)); int counter = 0; for (int i=0; i0 && substringsAreEqual(KMRinvertedList, i)) counter++; - KMR.set(KMRinvertedList.get(i).index, counter); + KMR.set(KMRinvertedList.get(i).index, new Integer(counter)); } return KMR; @@ -150,15 +152,15 @@ private ArrayList getBasicKMR(int length) { for (int i=0; i + * @see Suffix Tree (Wikipedia) + *
* @author Justin Wetherell */ public class SuffixTree implements ISuffixTree { private static final char DEFAULT_END_SEQ_CHAR = '$'; - private String string = null; - private char[] characters = null; + private final char endSeqChar; private Map linksMap = new HashMap(); private Map> edgeMap = new TreeMap>(); @@ -34,7 +33,8 @@ public class SuffixTree implements ISuffixTree { private int firstCharIndex = 0; private int lastCharIndex = -1; - private char END_SEQ_CHAR = DEFAULT_END_SEQ_CHAR; + private String string; + private char[] characters; /** * Create suffix tree with sequence and default end sequence. @@ -55,10 +55,10 @@ public SuffixTree(C seq) { * which defines the end of a sequence. */ public SuffixTree(C seq, char endSeq) { - END_SEQ_CHAR = endSeq; + endSeqChar = endSeq; StringBuilder builder = new StringBuilder(seq); - if (builder.indexOf(String.valueOf(END_SEQ_CHAR)) < 0) - builder.append(END_SEQ_CHAR); + if (builder.indexOf(String.valueOf(endSeqChar)) < 0) + builder.append(endSeqChar); string = builder.toString(); int length = string.length(); characters = new char[length]; @@ -124,14 +124,14 @@ private Set getSuffixes(int start) { String s = (string.substring(e.firstCharIndex, e.lastCharIndex + 1)); Link n = linksMap.get(e.endNode); if (n == null) { - int index = s.indexOf(END_SEQ_CHAR); + int index = s.indexOf(endSeqChar); if (index >= 0) s = s.substring(0, index); set.add(s); } else { Set set2 = getSuffixes(e.endNode); for (String s2 : set2) { - int index = s2.indexOf(END_SEQ_CHAR); + int index = s2.indexOf(endSeqChar); if (index >= 0) s2 = s2.substring(0, index); set.add(s + s2); @@ -297,7 +297,7 @@ private int[] searchEdges(char[] query) { public String toString() { StringBuilder builder = new StringBuilder(); builder.append("String = ").append(this.string).append("\n"); - builder.append("End of word character = ").append(END_SEQ_CHAR).append("\n"); + builder.append("End of word character = ").append(endSeqChar).append("\n"); builder.append(TreePrinter.getString(this)); return builder.toString(); } @@ -514,7 +514,7 @@ private static String getString(SuffixTree tree, Edg if (e != null) { value = e.endNode; String string = tree.string.substring(e.firstCharIndex, e.lastCharIndex + 1); - int index = string.indexOf(tree.END_SEQ_CHAR); + int index = string.indexOf(tree.endSeqChar); if (index >= 0) string = string.substring(0, index + 1); builder.append(prefix + (isTail ? "└── " : "├── ") + "(" + value + ") " + string + "\n"); diff --git a/src/com/jwetherell/algorithms/data_structures/SuffixTrie.java b/src/com/jwetherell/algorithms/data_structures/SuffixTrie.java index 774f53c8..e82af621 100644 --- a/src/com/jwetherell/algorithms/data_structures/SuffixTrie.java +++ b/src/com/jwetherell/algorithms/data_structures/SuffixTrie.java @@ -9,17 +9,18 @@ /** * A suffix trie is a data structure that presents the suffixes of a given * string in a way that allows for a particularly fast implementation of many - * important string operations. This implementation is based upon a Trie which - * is NOT a compact trie. - * - * http://en.wikipedia.org/wiki/Suffix_trie - * + * important string operations. + *

+ * This implementation is based upon a Trie which is NOT a compact trie. + *

+ * @see Suffix Trie (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") public class SuffixTrie implements ISuffixTree { - private Trie tree = null; + private Trie tree; /** * Create a suffix trie from sequence diff --git a/src/com/jwetherell/algorithms/data_structures/Treap.java b/src/com/jwetherell/algorithms/data_structures/Treap.java index 99f87e11..79792f37 100644 --- a/src/com/jwetherell/algorithms/data_structures/Treap.java +++ b/src/com/jwetherell/algorithms/data_structures/Treap.java @@ -12,9 +12,9 @@ * particular, with high probability its height is proportional to the logarithm * of the number of values, so that each search, insertion, or deletion * operation takes logarithmic time to perform. - * - * http://en.wikipedia.org/wiki/Treap - * + *

+ * @see Treap (Wikipedia) + *
* @author Justin Wetherell */ public class Treap> extends BinarySearchTree { diff --git a/src/com/jwetherell/algorithms/data_structures/TreeMap.java b/src/com/jwetherell/algorithms/data_structures/TreeMap.java index fe58d88a..728d7750 100644 --- a/src/com/jwetherell/algorithms/data_structures/TreeMap.java +++ b/src/com/jwetherell/algorithms/data_structures/TreeMap.java @@ -10,12 +10,12 @@ /** * An tree used to store key->values pairs, this is an implementation of an * associative array. - * + *

* This implementation is a composition of a AVLTree as the backing structure. - * - * http://en.wikipedia.org/wiki/AVL_tree - * http://en.wikipedia.org/wiki/Associative_array - * + *

+ * @see AVL Tree (Wikipedia) + * @see Associative Array (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/Trie.java b/src/com/jwetherell/algorithms/data_structures/Trie.java index 2c900c95..7ff1bfd2 100644 --- a/src/com/jwetherell/algorithms/data_structures/Trie.java +++ b/src/com/jwetherell/algorithms/data_structures/Trie.java @@ -7,20 +7,20 @@ /** * A trie, or prefix tree, is an ordered tree data structure that is used to * store an associative array where the keys are usually strings. - * - * == This is NOT a compact Trie. == - * - * http://en.wikipedia.org/wiki/Trie - * + *

+ * NOTE: This is NOT a compact Trie + *

+ * @see Trie (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") public class Trie implements ITree { - private int size = 0; + protected INodeCreator creator; + protected Node root; - protected INodeCreator creator = null; - protected Node root = null; + private int size = 0; public Trie() { this.creator = new INodeCreator() { diff --git a/src/com/jwetherell/algorithms/data_structures/TrieMap.java b/src/com/jwetherell/algorithms/data_structures/TrieMap.java index 86db4d73..06037c7d 100644 --- a/src/com/jwetherell/algorithms/data_structures/TrieMap.java +++ b/src/com/jwetherell/algorithms/data_structures/TrieMap.java @@ -6,12 +6,12 @@ /** * A trie used to store key->values pairs, this is an implementation of an * associative array. - * + *

* This implementation is a composition using a Trie as the backing structure. - * - * http://en.wikipedia.org/wiki/Trie - * http://en.wikipedia.org/wiki/Associative_array - * + *

+ * @see Trie (Wikipedia) + * @see Associative Array (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/data_structures/interfaces/IHeap.java b/src/com/jwetherell/algorithms/data_structures/interfaces/IHeap.java index ca70da88..14e9ec8f 100644 --- a/src/com/jwetherell/algorithms/data_structures/interfaces/IHeap.java +++ b/src/com/jwetherell/algorithms/data_structures/interfaces/IHeap.java @@ -7,9 +7,9 @@ * the keys of parent nodes are always greater than or equal to those of the children * and the highest key is in the root node (this kind of heap is called max heap) or * the keys of parent nodes are less than or equal to those of the children (min heap). - * - * http://en.wikipedia.org/wiki/Heap - * + *

+ * @see Heap (Wikipedia) + *
* @author Justin Wetherell */ public interface IHeap { diff --git a/src/com/jwetherell/algorithms/data_structures/interfaces/IList.java b/src/com/jwetherell/algorithms/data_structures/interfaces/IList.java index 345b421f..1cc75929 100644 --- a/src/com/jwetherell/algorithms/data_structures/interfaces/IList.java +++ b/src/com/jwetherell/algorithms/data_structures/interfaces/IList.java @@ -3,9 +3,9 @@ /** * A list or sequence is an abstract data type that implements an ordered * collection of values, where the same value may occur more than once. - * - * http://en.wikipedia.org/wiki/List_(computing) - * + *

+ * @see List (Wikipedia) + *
* @author Justin Wetherell */ public interface IList { diff --git a/src/com/jwetherell/algorithms/data_structures/interfaces/IMap.java b/src/com/jwetherell/algorithms/data_structures/interfaces/IMap.java index 131d78aa..c47df1db 100644 --- a/src/com/jwetherell/algorithms/data_structures/interfaces/IMap.java +++ b/src/com/jwetherell/algorithms/data_structures/interfaces/IMap.java @@ -4,9 +4,9 @@ * In computer science, an associative array, map, or dictionary is an abstract * data type composed of a collection of (key, value) pairs, such that each possible * key appears at most once in the collection. - * - * http://en.wikipedia.org/wiki/Associative_array - * + *

+ * @see Associative Array (Wikipedia) + *
* @author Justin Wetherell */ public interface IMap { diff --git a/src/com/jwetherell/algorithms/data_structures/interfaces/IQueue.java b/src/com/jwetherell/algorithms/data_structures/interfaces/IQueue.java index 3cf40dd8..a5c9af8c 100644 --- a/src/com/jwetherell/algorithms/data_structures/interfaces/IQueue.java +++ b/src/com/jwetherell/algorithms/data_structures/interfaces/IQueue.java @@ -8,9 +8,9 @@ * This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO * data structure, the first element added to the queue will be the first one to * be removed. - * - * http://en.wikipedia.org/wiki/Queue_(abstract_data_type) - * + *

+ * @see Queue (Wikipedia) + *
* @author Justin Wetherell */ public interface IQueue { diff --git a/src/com/jwetherell/algorithms/data_structures/interfaces/ISet.java b/src/com/jwetherell/algorithms/data_structures/interfaces/ISet.java index 6929c83a..3e565190 100644 --- a/src/com/jwetherell/algorithms/data_structures/interfaces/ISet.java +++ b/src/com/jwetherell/algorithms/data_structures/interfaces/ISet.java @@ -5,9 +5,9 @@ * any particular order, and no repeated values. It is a computer implementation of the mathematical * concept of a finite set. Unlike most other collection types, rather than retrieving a specific * element from a set, one typically tests a value for membership in a set. - * - * http://en.wikipedia.org/wiki/Set_(abstract_data_type) - * + *

+ * @see Set (Wikipedia) + *
* @author Justin Wetherell */ public interface ISet { diff --git a/src/com/jwetherell/algorithms/data_structures/interfaces/IStack.java b/src/com/jwetherell/algorithms/data_structures/interfaces/IStack.java index 0a00f781..cf2d41b5 100644 --- a/src/com/jwetherell/algorithms/data_structures/interfaces/IStack.java +++ b/src/com/jwetherell/algorithms/data_structures/interfaces/IStack.java @@ -8,9 +8,9 @@ * if it is empty. If the stack is full and does not contain enough space to * accept the given item, the stack is then considered to be in an overflow * state. The pop operation removes an item from the top of the stack. - * - * http://en.wikipedia.org/wiki/Stack_(abstract_data_type) - * + *

+ * @see Stack (Wikipedia) + *
* @author Justin Wetherell */ public interface IStack { diff --git a/src/com/jwetherell/algorithms/data_structures/interfaces/ISuffixTree.java b/src/com/jwetherell/algorithms/data_structures/interfaces/ISuffixTree.java index 6aec9aa3..64fd913c 100644 --- a/src/com/jwetherell/algorithms/data_structures/interfaces/ISuffixTree.java +++ b/src/com/jwetherell/algorithms/data_structures/interfaces/ISuffixTree.java @@ -6,9 +6,9 @@ * In computer science, a suffix tree (also called PAT tree or, in an earlier form, position tree) is a compressed trie * containing all the suffixes of the given text as their keys and positions in the text as their values. Suffix trees * allow particularly fast implementations of many important string operations. - * - * http://en.wikipedia.org/wiki/Suffix_tree - * + *

+ * @see Suffix Tree (Wikipedia) + *
* @author Justin Wetherell */ public interface ISuffixTree { diff --git a/src/com/jwetherell/algorithms/data_structures/interfaces/ITree.java b/src/com/jwetherell/algorithms/data_structures/interfaces/ITree.java index a1b63cd7..1e48299b 100644 --- a/src/com/jwetherell/algorithms/data_structures/interfaces/ITree.java +++ b/src/com/jwetherell/algorithms/data_structures/interfaces/ITree.java @@ -5,9 +5,9 @@ * where each node is a data structure consisting of a value, together with a list of nodes (the "children"), * with the constraints that no node is duplicated. A tree can be defined abstractly as a whole (globally) * as an ordered tree, with a value assigned to each node. - * - * http://en.wikipedia.org/wiki/Tree_(data_structure) - * + *

+ * @see Tree (Wikipedia) + *
* @author Justin Wetherell */ public interface ITree { diff --git a/src/com/jwetherell/algorithms/graph/AStar.java b/src/com/jwetherell/algorithms/graph/AStar.java index 3a957dbc..c8e3c751 100644 --- a/src/com/jwetherell/algorithms/graph/AStar.java +++ b/src/com/jwetherell/algorithms/graph/AStar.java @@ -16,9 +16,9 @@ /** * In computer science, A* is a computer algorithm that is widely used in path finding and graph traversal, the process * of plotting an efficiently traversable path between multiple points, called nodes. - * - * http://en.wikipedia.org/wiki/A*_search_algorithm - * + *

+ * @see A* Algorithm (Wikipedia) + *
* @author Justin Wetherell */ public class AStar> { diff --git a/src/com/jwetherell/algorithms/graph/BellmanFord.java b/src/com/jwetherell/algorithms/graph/BellmanFord.java index 320313bf..a7e0b56c 100644 --- a/src/com/jwetherell/algorithms/graph/BellmanFord.java +++ b/src/com/jwetherell/algorithms/graph/BellmanFord.java @@ -11,11 +11,11 @@ * Bellman-Ford's shortest path. Works on both negative and positive weighted * edges. Also detects negative weight cycles. Returns a tuple of total cost of * shortest path and the path. - * + *

* Worst case: O(|V| |E|) - * - * https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm - * + *

+ * @see Bellman Ford (Wikipedia) + *
* @author Justin Wetherell */ public class BellmanFord { diff --git a/src/com/jwetherell/algorithms/graph/BreadthFirstTraversal.java b/src/com/jwetherell/algorithms/graph/BreadthFirstTraversal.java index c634d4d7..d6e1b8e4 100644 --- a/src/com/jwetherell/algorithms/graph/BreadthFirstTraversal.java +++ b/src/com/jwetherell/algorithms/graph/BreadthFirstTraversal.java @@ -11,7 +11,14 @@ import com.jwetherell.algorithms.data_structures.Graph.Edge; import com.jwetherell.algorithms.data_structures.Graph.Vertex; -/* Breath First Travesal in given "Directed graph" (Adjacany matrix) */ +/** + * Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root (or some arbitrary node of a graph, sometimes referred to as a + * 'search key') and explores the neighbor nodes first, before moving to the next level neighbors. + *

+ * @see Breadth-First Search (Wikipedia) + *
+ * @author Justin Wetherell + */ public class BreadthFirstTraversal { @SuppressWarnings("unchecked") @@ -51,10 +58,11 @@ public static final > Graph.Vertex[] breadthFirstTrav // start at the source Vertex element = source; int c = 0; - int i = vertexToIndex.get(element); - visited[i] = 1; + int i = vertexToIndex.get(element); int k = 0; + arr[k] = element; + visited[i] = 1; k++; final Queue> queue = new ArrayDeque>(); @@ -68,6 +76,7 @@ public static final > Graph.Vertex[] breadthFirstTrav final Vertex v = vertices.get(i); queue.add(v); visited[i] = 1; + arr[k] = v; k++; } @@ -77,5 +86,39 @@ public static final > Graph.Vertex[] breadthFirstTrav } return arr; } + + public static int[] breadthFirstTraversal(int n, byte[][] adjacencyMatrix, int source) { + final int[] visited = new int[n]; + for (int i = 0; i < visited.length; i++) + visited[i] = -1; + + int element = source; + int i = source; + int arr[] = new int[n]; + int k = 0; + + arr[k] = element; + visited[i] = 1; + k++; + + final Queue queue = new ArrayDeque(); + queue.add(source); + while (!queue.isEmpty()) { + element = queue.peek(); + i = 0; + while (i < n) { + if (adjacencyMatrix[element][i] == 1 && visited[i] == -1) { + queue.add(i); + visited[i] = 1; + + arr[k] = i; + k++; + } + i++; + } + queue.poll(); + } + return arr; + } } diff --git a/src/com/jwetherell/algorithms/graph/ConnectedComponents.java b/src/com/jwetherell/algorithms/graph/ConnectedComponents.java index 88c05d8d..dae44174 100644 --- a/src/com/jwetherell/algorithms/graph/ConnectedComponents.java +++ b/src/com/jwetherell/algorithms/graph/ConnectedComponents.java @@ -13,9 +13,9 @@ * In graph theory, a connected component (or just component) of an undirected graph is a subgraph in which any two vertices are connected to each * other by paths, and which is connected to no additional vertices in the supergraph. A vertex with no incident edges is itself a connected * component. A graph that is itself connected has exactly one connected component, consisting of the whole graph. - * - * http://en.wikipedia.org/wiki/Connected_component_(graph_theory) - * + *

+ * @see Connected Components (Wikipedia) + *
* @author Justin Wetherell */ public class ConnectedComponents { diff --git a/src/com/jwetherell/algorithms/graph/CycleDetection.java b/src/com/jwetherell/algorithms/graph/CycleDetection.java index 276808e1..d084f16c 100644 --- a/src/com/jwetherell/algorithms/graph/CycleDetection.java +++ b/src/com/jwetherell/algorithms/graph/CycleDetection.java @@ -7,8 +7,10 @@ import com.jwetherell.algorithms.data_structures.Graph; /** - * Cycle detection in an undirected graph using depth first search. - * + * In computer science, cycle detection or cycle finding is the algorithmic problem of finding a cycle in a sequence of iterated function values. + *

+ * @see Cycle Detection (Wikipedia) + *
* @author Justin Wetherell */ public class CycleDetection { diff --git a/src/com/jwetherell/algorithms/graph/DepthFirstTraversal.java b/src/com/jwetherell/algorithms/graph/DepthFirstTraversal.java index 8d216a6b..76c5714b 100644 --- a/src/com/jwetherell/algorithms/graph/DepthFirstTraversal.java +++ b/src/com/jwetherell/algorithms/graph/DepthFirstTraversal.java @@ -1,40 +1,123 @@ package com.jwetherell.algorithms.graph; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.Stack; +import com.jwetherell.algorithms.data_structures.Graph; +import com.jwetherell.algorithms.data_structures.Graph.Edge; +import com.jwetherell.algorithms.data_structures.Graph.Vertex; + /** - * Implemented Depth First Traversal in given the "Directed graph" (Adjacency matrix) - * + * Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and + * explores as far as possible along each branch before backtracking. + *

+ * @see Depth-First Search (Wikipedia) + *
* @author Justin Wetherell - * */ -class DepthFirstTraversal { +public class DepthFirstTraversal { + + @SuppressWarnings("unchecked") + public static > Graph.Vertex[] depthFirstTraversal(Graph graph, Graph.Vertex source) { + // use for look-up via index + final ArrayList> vertices = new ArrayList>(); + vertices.addAll(graph.getVertices()); + + // used for look-up via vertex + final int n = vertices.size(); + final Map,Integer> vertexToIndex = new HashMap,Integer>(); + for (int i=0; i v = vertices.get(i); + vertexToIndex.put(v,i); + } - public static int[] arr; - public static int k = 0; + // adjacency matrix + final byte[][] adj = new byte[n][n]; + for (int i=0; i v = vertices.get(i); + final int idx = vertexToIndex.get(v); + final byte[] array = new byte[n]; + adj[idx] = array; + final List> edges = v.getEdges(); + for (Edge e : edges) + array[vertexToIndex.get(e.getToVertex())] = 1; + } - public static void depthFirstTraversal(int[][] a, int[] visited,int source){ + // visited array + final byte[] visited = new byte[n]; for (int i = 0; i < visited.length; i++) visited[i] = -1; - final Stack stack = new Stack(); - int element = source; + // for holding results + final Graph.Vertex[] arr = new Graph.Vertex[n]; + + // start at the source + Vertex element = source; + int c = 0; + int i = vertexToIndex.get(element); + int k = 0; + + visited[i] = 1; + arr[k] = element; + k++; + + final Stack> stack = new Stack>(); + stack.push(source); + while (!stack.isEmpty()) { + element = stack.peek(); + c = vertexToIndex.get(element); + i = 0; + while (i < n) { + if (adj[c][i] == 1 && visited[i] == -1) { + final Vertex v = vertices.get(i); + stack.push(v); + visited[i] = 1; + + element = v; + c = vertexToIndex.get(element); + i = 0; + + arr[k] = v; + k++; + continue; + } + i++; + } + stack.pop(); + } + return arr; + } + + public static int[] depthFirstTraversal(int n, byte[][] adjacencyMatrix, int source) { + final int[] visited = new int[n]; + for (int i = 0; i < visited.length; i++) + visited[i] = -1; + + int element = source; int i = source; - int n = visited.length - 1; + int arr[] = new int[n]; + int k = 0; + + visited[source] = 1; arr[k] = element; k++; - visited[source] = 1; + + final Stack stack = new Stack(); stack.push(source); - while (!stack.isEmpty()) { element = stack.peek(); - i = element; - while (i <= n) { - if (a[element][i] == 1 && visited[i] == -1) { + i = 0; + while (i < n) { + if (adjacencyMatrix[element][i] == 1 && visited[i] == -1) { stack.push(i); visited[i] = 1; + element = i; - i = 1; + i = 0; + arr[k] = element; k++; continue; @@ -43,5 +126,6 @@ public static void depthFirstTraversal(int[][] a, int[] visited,int source){ } stack.pop(); } + return arr; } } diff --git a/src/com/jwetherell/algorithms/graph/Dijkstra.java b/src/com/jwetherell/algorithms/graph/Dijkstra.java index 46ff85ce..929cded7 100644 --- a/src/com/jwetherell/algorithms/graph/Dijkstra.java +++ b/src/com/jwetherell/algorithms/graph/Dijkstra.java @@ -13,11 +13,11 @@ /** * Dijkstra's shortest path. Only works on non-negative path weights. Returns a * tuple of total cost of shortest path and the path. - * + *

* Worst case: O(|E| + |V| log |V|) - * - * https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm - * + *

+ * @see Dijkstra's Algorithm (Wikipedia) + *
* @author Justin Wetherell */ public class Dijkstra { diff --git a/src/com/jwetherell/algorithms/graph/EdmondsKarp.java b/src/com/jwetherell/algorithms/graph/EdmondsKarp.java index 3f6a333c..1431bbfa 100644 --- a/src/com/jwetherell/algorithms/graph/EdmondsKarp.java +++ b/src/com/jwetherell/algorithms/graph/EdmondsKarp.java @@ -8,7 +8,7 @@ * In computer science, the Edmonds–Karp algorithm is an implementation of the Ford–Fulkerson method for * computing the maximum flow in a flow network in O(V*E^2) time. *

- * http://en.wikipedia.org/wiki/Edmonds%E2%80%93Karp_algorithm + * @see Edmonds-Karp Algorithm (Wikipedia) *
* @author Mateusz Cianciara * @author Justin Wetherell diff --git a/src/com/jwetherell/algorithms/graph/FloydWarshall.java b/src/com/jwetherell/algorithms/graph/FloydWarshall.java index d36a48be..b9251df6 100644 --- a/src/com/jwetherell/algorithms/graph/FloydWarshall.java +++ b/src/com/jwetherell/algorithms/graph/FloydWarshall.java @@ -9,11 +9,11 @@ /** * Floyd–Warshall algorithm is a graph analysis algorithm for finding shortest * paths in a weighted graph (with positive or negative edge weights). - * + *

* Worst case: O(V^3) - * - * https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm - * + *

+ * @see Floyd-Warshall Algorithm (Wikipedia) + *
* @author Justin Wetherell */ public class FloydWarshall { diff --git a/src/com/jwetherell/algorithms/graph/Johnson.java b/src/com/jwetherell/algorithms/graph/Johnson.java index 8c7b1a12..5981b057 100644 --- a/src/com/jwetherell/algorithms/graph/Johnson.java +++ b/src/com/jwetherell/algorithms/graph/Johnson.java @@ -10,11 +10,11 @@ * Johnson's algorithm is a way to find the shortest paths between all pairs of * vertices in a sparse directed graph. It allows some of the edge weights to be * negative numbers, but no negative-weight cycles may exist. - * + *

* Worst case: O(V^2 log V + VE) - * - * https://en.wikipedia.org/wiki/Johnson%27s_algorithm - * + *

+ * @see Johnson's Algorithm (Wikipedia) + *
* @author Justin Wetherell */ public class Johnson { diff --git a/src/com/jwetherell/algorithms/graph/Kruskal.java b/src/com/jwetherell/algorithms/graph/Kruskal.java index 74629ffe..db21ee80 100644 --- a/src/com/jwetherell/algorithms/graph/Kruskal.java +++ b/src/com/jwetherell/algorithms/graph/Kruskal.java @@ -9,8 +9,8 @@ * subset of the edges that forms a tree that includes every vertex, where the * total weight of all the edges in the tree is minimized. *

- * https://en.wikipedia.org/wiki/Kruskal%27s_algorithm - * + * @see Kruskal's Algorithm (Wikipedia) + *
* @author Bartlomiej Drozd * @author Justin Wetherell */ diff --git a/src/com/jwetherell/algorithms/graph/Prim.java b/src/com/jwetherell/algorithms/graph/Prim.java index b8d4e366..e73724ca 100644 --- a/src/com/jwetherell/algorithms/graph/Prim.java +++ b/src/com/jwetherell/algorithms/graph/Prim.java @@ -14,7 +14,7 @@ * subset of the edges that forms a tree that includes every vertex, where the * total weight of all the edges in the tree is minimized. *

- * https://en.wikipedia.org/wiki/Prim%27s_algorithm + * @see Prim's Minimum Spanning Tree (Wikipedia) *
* @author Justin Wetherell */ diff --git a/src/com/jwetherell/algorithms/graph/PushRelabel.java b/src/com/jwetherell/algorithms/graph/PushRelabel.java index 9e4a01c9..fa5c03fa 100644 --- a/src/com/jwetherell/algorithms/graph/PushRelabel.java +++ b/src/com/jwetherell/algorithms/graph/PushRelabel.java @@ -18,7 +18,7 @@ * flow locally between neighboring nodes using push operations under the guidance of an * admissible network maintained by relabel operations. *

- * https://en.wikipedia.org/wiki/Push%E2%80%93relabel_maximum_flow_algorithm + * @see Push-Relabel Algorithm (Wikipedia) *
* @author Miron Ficak * @author Justin Wetherell diff --git a/src/com/jwetherell/algorithms/graph/TopologicalSort.java b/src/com/jwetherell/algorithms/graph/TopologicalSort.java index 732fc17f..20a2f551 100644 --- a/src/com/jwetherell/algorithms/graph/TopologicalSort.java +++ b/src/com/jwetherell/algorithms/graph/TopologicalSort.java @@ -9,9 +9,9 @@ * In computer science, a topological sort (sometimes abbreviated topsort or * toposort) or topological ordering of a directed graph is a linear ordering of * its vertices such that, for every edge uv, u comes before v in the ordering. - * - * https://en.wikipedia.org/wiki/Topological_sorting - * + *

+ * @see Topological Sort (Wikipedia) + *
* @author Justin Wetherell */ public class TopologicalSort { diff --git a/src/com/jwetherell/algorithms/graph/TurboMatching.java b/src/com/jwetherell/algorithms/graph/TurboMatching.java index 88120c82..aabaf1ba 100644 --- a/src/com/jwetherell/algorithms/graph/TurboMatching.java +++ b/src/com/jwetherell/algorithms/graph/TurboMatching.java @@ -13,9 +13,10 @@ * may incident with some edge of the matching, but this is not required and can only occur * if the number of vertices is even. *

- * https://en.wikipedia.org/wiki/Matching_(graph_theory) + * @see Matching (Wikipedia) *
* @author Jakub Szarawarski + * @author Justin Wetherell */ public class TurboMatching { @@ -27,9 +28,9 @@ public class TurboMatching { * @return a MatchingResult class instance containg a map of mates for each paired vertex and number of pairs */ public static > MatchingResult getMaximumMatching(Graph graph){ - Map, Graph.Vertex> mate = new HashMap, Graph.Vertex>(); + final Map, Graph.Vertex> mate = new HashMap, Graph.Vertex>(); - while(pathset(graph, mate)); + while (pathset(graph, mate)); return new MatchingResult(mate); } @@ -42,17 +43,15 @@ public static > MatchingResult getMaximumMatching(Gra * @return information if any augmenting path was found */ private static > boolean pathset(Graph graph, Map, Graph.Vertex> mate){ - Set> visited = new HashSet>(); + final Set> visited = new HashSet>(); boolean result = false; - - for(Graph.Vertex vertex : graph.getVertices()){ - if (mate.containsKey(vertex) == false){ + for (Graph.Vertex vertex : graph.getVertices()) { + if (mate.containsKey(vertex) == false) { if (path(graph, mate, visited, vertex)) result = true; } } - return result; } @@ -68,25 +67,24 @@ private static > boolean pathset(Graph graph, Map> boolean path(Graph graph, Map, Graph.Vertex> mate, Set> visited, Graph.Vertex vertex){ - if (visited.contains(vertex)) return false; - visited.add(vertex); - - for(Graph.Edge edge : vertex.getEdges()){ + if (visited.contains(vertex)) + return false; - Graph.Vertex neighbour = edge.getFromVertex().equals(vertex) ? edge.getToVertex() : edge.getFromVertex(); - - if (mate.containsKey(neighbour) == false || path(graph, mate, visited, mate.get(neighbour))){ + visited.add(vertex); + for (Graph.Edge edge : vertex.getEdges()) { + final Graph.Vertex neighbour = edge.getFromVertex().equals(vertex) ? edge.getToVertex() : edge.getFromVertex(); + if (mate.containsKey(neighbour) == false || path(graph, mate, visited, mate.get(neighbour))) { mate.put(vertex, neighbour); mate.put(neighbour, vertex); return true; } } - return false; } public static class MatchingResult>{ + private final Map, Graph.Vertex> mate; private final int size; diff --git a/src/com/jwetherell/algorithms/mathematics/Coprimes.java b/src/com/jwetherell/algorithms/mathematics/Coprimes.java index 0877ce40..73bf8288 100644 --- a/src/com/jwetherell/algorithms/mathematics/Coprimes.java +++ b/src/com/jwetherell/algorithms/mathematics/Coprimes.java @@ -2,10 +2,10 @@ /** * In number theory, two integers a and b are said to be relatively prime, mutually prime, or coprime (also spelled - * co-prime)[1] if the only positive integer that divides both of them is 1. That is, the only common positive factor + * co-prime) if the only positive integer that divides both of them is 1. That is, the only common positive factor * of the two numbers is 1. This is equivalent to their greatest common divisor being 1. *

- * http://en.wikipedia.org/wiki/Coprime_integers + * @see Mutually Prime / Co-prime (Wikipedia) *
* @author Szymon Stankiewicz * @author Justin Wetherell diff --git a/src/com/jwetherell/algorithms/mathematics/Exponentiation.java b/src/com/jwetherell/algorithms/mathematics/Exponentiation.java index 0b29b967..b5746a77 100644 --- a/src/com/jwetherell/algorithms/mathematics/Exponentiation.java +++ b/src/com/jwetherell/algorithms/mathematics/Exponentiation.java @@ -3,21 +3,20 @@ /** * Recursive function of exponentiation is just an implementation of definition. *

- * https://en.wikipedia.org/wiki/Exponentiation + * @see Exponentiation (Wikipedia) *

* Complexity - O(N) where N is exponent. *

* Fast exponentiation's complexity is O(lg N) *

- * https://en.wikipedia.org/wiki/Exponentiation_by_squaring + * @see Exponentiation by Squaring (Wikipedia) *
* Modular exponentiation is similar. *

- * https://en.wikipedia.org/wiki/Modular_exponentiation + * @see Modular Exponentiation (Wikipedia) *

* This implementation is the fast version of this algorithm with a complexity of O(lg N) also *
- * * @author Bartlomiej Drozd * @author Justin Wetherell */ diff --git a/src/com/jwetherell/algorithms/mathematics/FastFourierTransform.java b/src/com/jwetherell/algorithms/mathematics/FastFourierTransform.java index 93dc4126..30e2ecd8 100644 --- a/src/com/jwetherell/algorithms/mathematics/FastFourierTransform.java +++ b/src/com/jwetherell/algorithms/mathematics/FastFourierTransform.java @@ -8,7 +8,7 @@ * domain and vice versa. An FFT rapidly computes such transformations by factorizing the DFT matrix into a product of * sparse (mostly zero) factors. *

- * http://en.wikipedia.org/wiki/Fast_Fourier_transform + * @see Fast Fourier Transform (Wikipedia) *
* @author Mateusz Cianciara * @author Justin Wetherell @@ -23,7 +23,7 @@ private FastFourierTransform() { } * in terms of N1 smaller DFTs of sizes N2, recursively, to reduce the computation time to O(N log N) for highly * composite N (smooth numbers). *

- * http://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm + * @see Cooley–Tukey Algorithm (Wikipedia) *
* @param coefficients size must be power of 2 */ diff --git a/src/com/jwetherell/algorithms/mathematics/GreatestCommonDivisor.java b/src/com/jwetherell/algorithms/mathematics/GreatestCommonDivisor.java index f12889ef..4ef25cd2 100644 --- a/src/com/jwetherell/algorithms/mathematics/GreatestCommonDivisor.java +++ b/src/com/jwetherell/algorithms/mathematics/GreatestCommonDivisor.java @@ -4,7 +4,7 @@ * In mathematics, the greatest common divisor (gcd) of two or more integers, when at least one of them is not * zero, is the largest positive integer that is a divisor of both numbers. *

- * http://en.wikipedia.org/wiki/Greatest_common_divisor + * @see Greatest Common Divisor (Wikipedia) *
* @author Szymon Stankiewicz * @author Justin Wetherell @@ -30,7 +30,7 @@ public static long gcdUsingRecursion(long a, long b) { * A much more efficient method is the Euclidean algorithm, which uses a division algorithm such as long division * in combination with the observation that the gcd of two numbers also divides their difference. *

- * http://en.wikipedia.org/wiki/Greatest_common_divisor#Using_Euclid.27s_algorithm + * @see Euclidean Algorithm (Wikipedia) */ public static final long gcdUsingEuclides(long x, long y) { long greater = x; diff --git a/src/com/jwetherell/algorithms/mathematics/Knapsack.java b/src/com/jwetherell/algorithms/mathematics/Knapsack.java index c981132e..69f848c2 100644 --- a/src/com/jwetherell/algorithms/mathematics/Knapsack.java +++ b/src/com/jwetherell/algorithms/mathematics/Knapsack.java @@ -3,6 +3,15 @@ import java.util.ArrayList; import java.util.List; +/** + * The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a + * collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained + * by a fixed-size knapsack and must fill it with the most valuable items. + *

+ * @see Knapsack Problem (Wikipedia) + *
+ * @author Justin Wetherell + */ public class Knapsack { public static final int[] zeroOneKnapsack(int[] values, int[] weights, int capacity) { @@ -28,7 +37,7 @@ public static final int[] zeroOneKnapsack(int[] values, int[] weights, int capac } } - List list = new ArrayList(); + final List list = new ArrayList(); int i = height - 1; int j = width - 1; while (i != 0 || j != 0) { @@ -52,5 +61,4 @@ public static final int[] zeroOneKnapsack(int[] values, int[] weights, int capac return result; } - } diff --git a/src/com/jwetherell/algorithms/mathematics/Modular.java b/src/com/jwetherell/algorithms/mathematics/Modular.java index 91ece6c1..1c78de2c 100644 --- a/src/com/jwetherell/algorithms/mathematics/Modular.java +++ b/src/com/jwetherell/algorithms/mathematics/Modular.java @@ -5,7 +5,7 @@ * upon reaching a certain value—the modulus (plural moduli). The modern approach to modular arithmetic was * developed by Carl Friedrich Gauss in his book Disquisitiones Arithmeticae, published in 1801. *

- * http://en.wikipedia.org/wiki/Modular_arithmetic + * @see Modular Arithmetic (Wikipedia) *
* @author Szymon Stankiewicz * @author Justin Wetherell diff --git a/src/com/jwetherell/algorithms/mathematics/Permutations.java b/src/com/jwetherell/algorithms/mathematics/Permutations.java index a9ec3fb8..144a17c4 100644 --- a/src/com/jwetherell/algorithms/mathematics/Permutations.java +++ b/src/com/jwetherell/algorithms/mathematics/Permutations.java @@ -7,7 +7,7 @@ * In mathematics, the notion of permutation relates to the act of arranging all the members of a set into some sequence * or order, or if the set is already ordered, rearranging (reordering) its elements, a process called permuting. *

- * http://en.wikipedia.org/wiki/Permutation + * @see Permutation (Wikipedia) *
* @author Justin Wetherell * @author Lucjan Roslanowski diff --git a/src/com/jwetherell/algorithms/mathematics/Primes.java b/src/com/jwetherell/algorithms/mathematics/Primes.java index c6ec298c..ee4ca62b 100644 --- a/src/com/jwetherell/algorithms/mathematics/Primes.java +++ b/src/com/jwetherell/algorithms/mathematics/Primes.java @@ -17,7 +17,7 @@ public class Primes { * of determining these factors is called integer factorization. The fundamental theorem of arithmetic says that every positive * integer has a single unique prime factorization. *

- * https://en.wikipedia.org/wiki/Prime_factor + * @see Prime Factor (Wikipedia) *
*/ public static final Map getPrimeFactorization(long number) { @@ -94,7 +94,7 @@ public static final boolean isPrime(long number) { * number, 2. The multiples of a given prime are generated as a sequence of numbers starting from that prime, with constant * difference between them that is equal to that prime. *

- * https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes + * @see Sieve of Eratosthenes (Wikipedia) *
*/ public static final boolean sieveOfEratosthenes(int number) { @@ -128,7 +128,7 @@ public static final boolean sieveOfEratosthenes(int number) { * algorithm, witnesses are not randomized. Used set of witnesses guarantees that result * will be correct for sure (not probably) for any number lower than 10^18. *

- * https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test + * @see Miller-Rabin Primality Test (Wikipedia) *
*/ public static final boolean millerRabinTest(int number) { diff --git a/src/com/jwetherell/algorithms/mathematics/RamerDouglasPeucker.java b/src/com/jwetherell/algorithms/mathematics/RamerDouglasPeucker.java index eca8a3b2..e43d24c9 100644 --- a/src/com/jwetherell/algorithms/mathematics/RamerDouglasPeucker.java +++ b/src/com/jwetherell/algorithms/mathematics/RamerDouglasPeucker.java @@ -6,9 +6,9 @@ /** * The Ramer–Douglas–Peucker algorithm (RDP) is an algorithm for reducing the number of points in a * curve that is approximated by a series of points. - * - * http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm - * + *

+ * @see Ramer–Douglas–Peucker Algorithm (Wikipedia) + *
* @author Justin Wetherell */ public class RamerDouglasPeucker { diff --git a/src/com/jwetherell/algorithms/numbers/Complex.java b/src/com/jwetherell/algorithms/numbers/Complex.java index 48b1a834..bef753d4 100644 --- a/src/com/jwetherell/algorithms/numbers/Complex.java +++ b/src/com/jwetherell/algorithms/numbers/Complex.java @@ -5,7 +5,7 @@ * imaginary unit, satisfying the equation i2 = −1.[1] In this expression, a is the real part and b is the imaginary * part of the complex number. If z=a+bi z=a+bi, then Rz=a, Iz=b. *

- * http://en.wikipedia.org/wiki/Complex_number + * @see Complex Number (Wikipedia) *
* @author Mateusz Cianciara * @author Justin Wetherell diff --git a/src/com/jwetherell/algorithms/search/BinarySearch.java b/src/com/jwetherell/algorithms/search/BinarySearch.java index fabff71c..8d3f8521 100644 --- a/src/com/jwetherell/algorithms/search/BinarySearch.java +++ b/src/com/jwetherell/algorithms/search/BinarySearch.java @@ -9,9 +9,9 @@ * Best-case performance O(1)
* Average performance O(log n)
* Worst-case space complexity O(1)
+ *

+ * @see Binary Search (Wikipedia) *
- * https://en.wikipedia.org/wiki/Binary_search_algorithm - * * @author Justin Wetherell */ public class BinarySearch { diff --git a/src/com/jwetherell/algorithms/search/InterpolationSearch.java b/src/com/jwetherell/algorithms/search/InterpolationSearch.java index c3798292..d9050256 100644 --- a/src/com/jwetherell/algorithms/search/InterpolationSearch.java +++ b/src/com/jwetherell/algorithms/search/InterpolationSearch.java @@ -6,9 +6,9 @@ *

* Worst-case performance O(n)
* Average performance O(log(log(n)))
+ *

+ * @see Interpolation Search (Wikipedia) *
- * https://en.wikipedia.org/wiki/Interpolation_search - * * @author Justin Wetherell */ public class InterpolationSearch { diff --git a/src/com/jwetherell/algorithms/search/LinearSearch.java b/src/com/jwetherell/algorithms/search/LinearSearch.java index a811240b..196591e7 100644 --- a/src/com/jwetherell/algorithms/search/LinearSearch.java +++ b/src/com/jwetherell/algorithms/search/LinearSearch.java @@ -8,9 +8,9 @@ * Best-case performance O(1)
* Average performance O(n)
* Worst-case space complexity O(1)
+ *

+ * @see Linear Search (Wikipedia) *
- * https://en.wikipedia.org/wiki/Linear_search - * * @author Justin Wetherell */ public class LinearSearch { diff --git a/src/com/jwetherell/algorithms/search/LowerBound.java b/src/com/jwetherell/algorithms/search/LowerBound.java index ae028fb0..569e4532 100644 --- a/src/com/jwetherell/algorithms/search/LowerBound.java +++ b/src/com/jwetherell/algorithms/search/LowerBound.java @@ -1,15 +1,16 @@ package com.jwetherell.algorithms.search; /** - * Lower bound search algorithm. - * Lower bound is kind of binary search algorithm but: - * -If searched element doesn't exist function returns index of first element which is bigger than searched value. - * -If searched element is bigger than any array element function returns first index after last element. - * -If searched element is lower than any array element function returns index of first element. - * -If there are many values equals searched value function returns first occurrence. + * Lower bound search algorithm.
+ * Lower bound is kind of binary search algorithm but:
+ * -If searched element doesn't exist function returns index of first element which is bigger than searched value.
+ * -If searched element is bigger than any array element function returns first index after last element.
+ * -If searched element is lower than any array element function returns index of first element.
+ * -If there are many values equals searched value function returns first occurrence.
* Behaviour for unsorted arrays is unspecified. + *

* Complexity O(log n). - * + *

* @author Bartlomiej Drozd * @author Justin Wetherell */ diff --git a/src/com/jwetherell/algorithms/search/QuickSelect.java b/src/com/jwetherell/algorithms/search/QuickSelect.java index f7bc20ef..3fe048a9 100644 --- a/src/com/jwetherell/algorithms/search/QuickSelect.java +++ b/src/com/jwetherell/algorithms/search/QuickSelect.java @@ -8,9 +8,9 @@ * Worst-case performance О(n2)
* Best-case performance О(n)
* Average performance O(n)
+ *

+ * @see Quickselect (Wikipedia) *
- * https://en.wikipedia.org/wiki/Quickselect - * * @author Justin Wetherell */ public class QuickSelect { diff --git a/src/com/jwetherell/algorithms/search/UpperBound.java b/src/com/jwetherell/algorithms/search/UpperBound.java index 803dfb57..5c6d8a21 100644 --- a/src/com/jwetherell/algorithms/search/UpperBound.java +++ b/src/com/jwetherell/algorithms/search/UpperBound.java @@ -1,13 +1,15 @@ package com.jwetherell.algorithms.search; /** - * Upper bound search algorithm. - * Upper bound is kind of binary search algorithm but: - * -It returns index of first element which is grater than searched value. - * -If searched element is bigger than any array element function returns first index after last element. + * Upper bound search algorithm.
+ * Upper bound is kind of binary search algorithm but:
+ * -It returns index of first element which is grater than searched value.
+ * -If searched element is bigger than any array element function returns first index after last element.
+ *
* Behaviour for unsorted arrays is unspecified. + *

* Complexity O(log n). - * + *
* @author Bartlomiej Drozd * @author Justin Wetherell */ diff --git a/src/com/jwetherell/algorithms/sequence/ArithmeticProgression.java b/src/com/jwetherell/algorithms/sequence/ArithmeticProgression.java index f8c1b3a5..50fad654 100644 --- a/src/com/jwetherell/algorithms/sequence/ArithmeticProgression.java +++ b/src/com/jwetherell/algorithms/sequence/ArithmeticProgression.java @@ -3,7 +3,7 @@ /** * Compute the result of adding a sequence of numbers from N (startNumber) to N+X (startNumber+numberOfNumbersToCompute) *

- * https://en.wikipedia.org/wiki/Arithmetic_progression + * @see Arithmetic Progression (Wikipedia) *
* @author Justin Wetherell */ @@ -30,7 +30,7 @@ public static final long sequenceTotalUsingLoop(int startNumber, int numberOfNum *

* e.g. result = N + (N+1) + (N+2) + (N+3) + ..... + (N+X)
*
- * https://en.wikipedia.org/wiki/Triangular_number + * @see Triangular Number (Wikipedia) */ public static final long sequenceTotalUsingTriangularNumbers(int startNumber, int numberOfNumbersToCompute) { // n*(n+1)/2 diff --git a/src/com/jwetherell/algorithms/sequence/FibonacciSequence.java b/src/com/jwetherell/algorithms/sequence/FibonacciSequence.java index 283bcc86..7626a42d 100644 --- a/src/com/jwetherell/algorithms/sequence/FibonacciSequence.java +++ b/src/com/jwetherell/algorithms/sequence/FibonacciSequence.java @@ -4,8 +4,7 @@ * In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the * sum of the two preceding ones: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... *

- * - * https://en.wikipedia.org/wiki/Fibonacci_number + * @see Fibonacci Sequence (Wikipedia) *
* @author Justin Wetherell */ diff --git a/src/com/jwetherell/algorithms/sequence/LargestSumContiguousSubarray.java b/src/com/jwetherell/algorithms/sequence/LargestSumContiguousSubarray.java index cc82f8ec..87f8da9c 100644 --- a/src/com/jwetherell/algorithms/sequence/LargestSumContiguousSubarray.java +++ b/src/com/jwetherell/algorithms/sequence/LargestSumContiguousSubarray.java @@ -4,7 +4,7 @@ * Given an array of integers, we want to find the largest sum of contiguous * subarray. *

- * https://en.wikipedia.org/wiki/Maximum_subarray_problem + * @see Maximum Subarray Problem (Wikipedia) *
* @author Miguel Stephane KAKANAKOU * @author Justin Wetherell diff --git a/src/com/jwetherell/algorithms/sequence/LongestCommonSubsequence.java b/src/com/jwetherell/algorithms/sequence/LongestCommonSubsequence.java index fa807cd4..66fa392f 100644 --- a/src/com/jwetherell/algorithms/sequence/LongestCommonSubsequence.java +++ b/src/com/jwetherell/algorithms/sequence/LongestCommonSubsequence.java @@ -7,7 +7,7 @@ * The longest common subsequence (LCS) problem is the problem of finding the longest subsequence common to all sequences in a set of sequences (often just two sequences). It differs from problems * of finding common substrings: unlike substrings, subsequences are not required to occupy consecutive positions within the original sequences. *

- * https://en.wikipedia.org/wiki/Longest_common_subsequence_problem + * @see Longest Common Subsequence Problem (Wikipedia) *
* @author Justin Wetherell */ diff --git a/src/com/jwetherell/algorithms/sequence/LongestIncreasingSubsequence.java b/src/com/jwetherell/algorithms/sequence/LongestIncreasingSubsequence.java index cdb39de8..4421896b 100644 --- a/src/com/jwetherell/algorithms/sequence/LongestIncreasingSubsequence.java +++ b/src/com/jwetherell/algorithms/sequence/LongestIncreasingSubsequence.java @@ -8,7 +8,7 @@ * In computer science, the longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in * which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique. *

- * https://en.wikipedia.org/wiki/Longest_increasing_subsequence + * @see Longest Increasing Subsequence Problem (Wikipedia) *
* @author Bartlomiej Drozd * @author Justin Wetherell diff --git a/src/com/jwetherell/algorithms/sequence/LongestPalindromicSubsequence.java b/src/com/jwetherell/algorithms/sequence/LongestPalindromicSubsequence.java index 040d270c..9d870978 100644 --- a/src/com/jwetherell/algorithms/sequence/LongestPalindromicSubsequence.java +++ b/src/com/jwetherell/algorithms/sequence/LongestPalindromicSubsequence.java @@ -4,17 +4,17 @@ * A longest palin­dromic sub­se­quence is a sequence that appears in the same * rel­a­tive order, but not nec­es­sar­ily contiguous(not sub­string) and * palin­drome in nature. - * - * Given a string, find the length of the longest palin­dromic sub­se­quence in - * it. - * + *

+ * Given a string, find the length of the longest palin­dromic sub­se­quence in it. + *

+ * @see Longest Palin­dromic Sub­se­quence (Wikipedia) + *
* @author Miguel Stephane KAKANAKOU - * + * @author Justin Wetherell */ public class LongestPalindromicSubsequence { - private LongestPalindromicSubsequence() { - } + private LongestPalindromicSubsequence() { } /** * Find the length of the longest palin­dromic sub­se­quence in the given @@ -23,9 +23,11 @@ private LongestPalindromicSubsequence() { public static int getLongestPalindromeSubsequence(String s) { if (s == null) throw new NullPointerException("The given String is null"); - int len = s.length(); - int[][] M = new int[len][len]; - char[] ch = s.toCharArray(); + + final int len = s.length(); + final int[][] M = new int[len][len]; + final char[] ch = s.toCharArray(); + initializeMatrix(M); fillMatrix(M, ch); return M[0][len-1]; @@ -44,7 +46,7 @@ private static void initializeMatrix(int[][] M) { } private static void fillMatrix(int[][] M, char[] ch) { - int len = M.length; + final int len = M.length; int i, j; for (int k=1; k - * http://www.geeksforgeeks.org/find-number-times-string-occurs-given-string/ + * @see Substring occurs in String (GeeksForGeeks) *
* @author Justin Wetherell */ diff --git a/src/com/jwetherell/algorithms/sorts/AmericanFlagSort.java b/src/com/jwetherell/algorithms/sorts/AmericanFlagSort.java index 0ff11cda..7015a147 100644 --- a/src/com/jwetherell/algorithms/sorts/AmericanFlagSort.java +++ b/src/com/jwetherell/algorithms/sorts/AmericanFlagSort.java @@ -6,16 +6,19 @@ * algorithms such as radix sort and American flag sort are typically used to * sort large objects such as strings, for which comparison is not a unit-time * operation. - * - * Family: Bucket. - * Space: In-place. - * Stable: False. - * - * Average case = O(n*k/d) Worst case = O(n*k/d) Best case = O(n*k/d) + *

+ * Family: Bucket.
+ * Space: In-place.
+ * Stable: False.
+ *

+ * Average case = O(n*k/d)
+ * Worst case = O(n*k/d)
+ * Best case = O(n*k/d)
+ *

* NOTE: n is the number of digits and k is the average bucket size - * - * http://en.wikipedia.org/wiki/American_flag_sort - * + *

+ * @see American Flag Sort (Wikipedia) + *
* @author Justin Wetherell */ public class AmericanFlagSort { diff --git a/src/com/jwetherell/algorithms/sorts/BubbleSort.java b/src/com/jwetherell/algorithms/sorts/BubbleSort.java index b91a066d..3ac320ff 100644 --- a/src/com/jwetherell/algorithms/sorts/BubbleSort.java +++ b/src/com/jwetherell/algorithms/sorts/BubbleSort.java @@ -5,15 +5,17 @@ * through the list to be sorted, comparing each pair of adjacent items and * swapping them if they are in the wrong order. The pass through the list is * repeated until no swaps are needed, which indicates that the list is sorted. - * - * Family: Exchanging. - * Space: In-place. - * Stable: True. - * - * Average case = O(n^2) Worst case = O(n^2) Best case = O(n) - * - * http://en.wikipedia.org/wiki/Bubble_sort - * + *

+ * Family: Exchanging.
+ * Space: In-place.
+ * Stable: True.
+ *

+ * Average case = O(n^2)
+ * Worst case = O(n^2)
+ * Best case = O(n)
+ *

+ * @see Bubble Sort (Wikipedia) + *
* @author Justin Wetherell */ public class BubbleSort> { diff --git a/src/com/jwetherell/algorithms/sorts/CountingSort.java b/src/com/jwetherell/algorithms/sorts/CountingSort.java index d2d177b0..f1593c50 100644 --- a/src/com/jwetherell/algorithms/sorts/CountingSort.java +++ b/src/com/jwetherell/algorithms/sorts/CountingSort.java @@ -6,16 +6,19 @@ * It operates by counting the number of objects that have each distinct key * value, and using arithmetic on those counts to determine the positions of * each key value in the output sequence. - * - * Family: Counting. - * Space: An Array of length r. - * Stable: True. - * - * Average case = O(n+r) Worst case = O(n+r) Best case = O(n+r) + *

+ * Family: Counting.
+ * Space: An Array of length r.
+ * Stable: True.
+ *

+ * Average case = O(n+r)
+ * Worst case = O(n+r)
+ * Best case = O(n+r)
+ *

* NOTE: r is the range of numbers (0 to r) to be sorted. - * - * http://en.wikipedia.org/wiki/Counting_sort - * + *

+ * @see Counting Sort (Wikipedia) + *
* @author Justin Wetherell */ public class CountingSort { diff --git a/src/com/jwetherell/algorithms/sorts/HeapSort.java b/src/com/jwetherell/algorithms/sorts/HeapSort.java index 1e5b7d6f..6991347d 100644 --- a/src/com/jwetherell/algorithms/sorts/HeapSort.java +++ b/src/com/jwetherell/algorithms/sorts/HeapSort.java @@ -5,15 +5,17 @@ * list), and is part of the selection sort family. Although somewhat slower in * practice on most machines than a well-implemented quicksort, it has the * advantage of a more favorable worst-case O(n log n) runtime. - * - * Family: Selection. - * Space: In-place. - * Stable: False. - * - * Average case = O(n*log n) Worst case = O(n*log n) Best case = O(n*log n) - * - * http://en.wikipedia.org/wiki/Heap_sort - * + *

+ * Family: Selection.
+ * Space: In-place.
+ * Stable: False.
+ *

+ * Average case = O(n*log n)
+ * Worst case = O(n*log n)
+ * Best case = O(n*log n)
+ *

+ * @see Heap Sort (Wikipedia) + *
* @author Justin Wetherell */ public class HeapSort> { diff --git a/src/com/jwetherell/algorithms/sorts/InsertionSort.java b/src/com/jwetherell/algorithms/sorts/InsertionSort.java index c9772940..6aedf610 100644 --- a/src/com/jwetherell/algorithms/sorts/InsertionSort.java +++ b/src/com/jwetherell/algorithms/sorts/InsertionSort.java @@ -5,15 +5,17 @@ * sorted array (or list) is built one entry at a time. It is much less * efficient on large lists than more advanced algorithms such as quicksort, * heapsort, or merge sort. - * - * Family: Insertion. - * Space: In-place. - * Stable: True. - * - * Average case = O(n^2) Worst case = O(n^2) Best case = O(n) - * - * http://en.wikipedia.org/wiki/Insertion_sort - * + *

+ * Family: Insertion.
+ * Space: In-place.
+ * Stable: True.
+ *

+ * Average case = O(n^2)
+ * Worst case = O(n^2)
+ * Best case = O(n)
+ *

+ * @see Insertion Sort (Wikipedia) + *
* @author Justin Wetherell */ public class InsertionSort> { diff --git a/src/com/jwetherell/algorithms/sorts/MergeSort.java b/src/com/jwetherell/algorithms/sorts/MergeSort.java index bc7d4dd4..69823f81 100644 --- a/src/com/jwetherell/algorithms/sorts/MergeSort.java +++ b/src/com/jwetherell/algorithms/sorts/MergeSort.java @@ -4,15 +4,17 @@ * Merge sort is an O(n log n) comparison-based sorting algorithm. Most * implementations produce a stable sort, which means that the implementation * preserves the input order of equal elements in the sorted output. - * - * Family: Merging. - * Space: In-place. - * Stable: True. - * - * Average case = O(n*log n) Worst case = O(n*log n) Best case = O(n*log n) - * - * http://en.wikipedia.org/wiki/Merge_sort - * + *

+ * Family: Merging.
+ * Space: In-place.
+ * Stable: True.
+ *

+ * Average case = O(n*log n)
+ * Worst case = O(n*log n)
+ * Best case = O(n*log n)
+ *

+ * @see Merge Sort (Wikipedia) + *
* @author Justin Wetherell */ @SuppressWarnings("unchecked") diff --git a/src/com/jwetherell/algorithms/sorts/QuickSort.java b/src/com/jwetherell/algorithms/sorts/QuickSort.java index 2328ffa2..7484942b 100644 --- a/src/com/jwetherell/algorithms/sorts/QuickSort.java +++ b/src/com/jwetherell/algorithms/sorts/QuickSort.java @@ -6,15 +6,17 @@ * Quicksort is a sorting algorithm which, on average, makes O(n*log n) comparisons to sort * n items. In the worst case, it makes O(n^2) comparisons, though this behavior is * rare. Quicksort is often faster in practice than other algorithms. - * - * Family: Divide and conquer. - * Space: In-place. - * Stable: False. - * - * Average case = O(n*log n), Worst case = O(n^2), Best case = O(n) [three-way partition and equal keys] - * - * http://en.wikipedia.org/wiki/Quick_sort - * + *

+ * Family: Divide and conquer.
+ * Space: In-place.
+ * Stable: False.
+ *

+ * Average case = O(n*log n)
+ * Worst case = O(n^2)
+ * Best case = O(n) [three-way partition and equal keys]
+ *

+ * @see Quicksort (Wikipedia) + *
* @author Justin Wetherell */ public class QuickSort> { diff --git a/src/com/jwetherell/algorithms/sorts/RadixSort.java b/src/com/jwetherell/algorithms/sorts/RadixSort.java index 5d0f945d..299f6fcb 100644 --- a/src/com/jwetherell/algorithms/sorts/RadixSort.java +++ b/src/com/jwetherell/algorithms/sorts/RadixSort.java @@ -9,16 +9,19 @@ * because integers can represent strings of characters (e.g., names or dates) * and specially formatted floating point numbers, radix sort is not limited to * integers. - * - * Family: Bucket. - * Space: 10 Buckets with at most n integers per bucket. - * Stable: True. - * - * Average case = O(n*k) Worst case = O(n*k) Best case = O(n*k) + *

+ * Family: Bucket.
+ * Space: 10 Buckets with at most n integers per bucket.
+ * Stable: True.
+ *

+ * Average case = O(n*k)
+ * Worst case = O(n*k)
+ * Best case = O(n*k)
+ *

* NOTE: n is the number of digits and k is the average bucket size - * - * http://en.wikipedia.org/wiki/Radix_sort - * + *

+ * @see Radix Sort (Wikipedia) + *
* @author Justin Wetherell */ public class RadixSort { diff --git a/src/com/jwetherell/algorithms/sorts/ShellSort.java b/src/com/jwetherell/algorithms/sorts/ShellSort.java index 293dad31..c94e7fed 100644 --- a/src/com/jwetherell/algorithms/sorts/ShellSort.java +++ b/src/com/jwetherell/algorithms/sorts/ShellSort.java @@ -9,17 +9,21 @@ * bubble sort, by starting the comparison and exchange of elements with * elements that are far apart before finishing with neighboring elements. * Starting with far apart elements can move some out-of-place elements into - * position faster than a simple nearest neighbor exchange. Family: Exchanging. - * Space: In-place. Stable: False. - * - * Average case = depends on the gap Worst case = O(n * log^2 n) Best case = - * O(n) - * - * http://en.wikipedia.org/wiki/Shell_sort - * + * position faster than a simple nearest neighbor exchange. + *

+ * Family: Exchanging.
+ * Space: In-place.
+ * Stable: False.
+ *

+ * Average case = depends on the gap
+ * Worst case = O(n * log^2 n)
+ * Best case = O(n)
+ *

+ * @see Shell Sort (Wikipedia) + *
* @author Justin Wetherell */ -public abstract class ShellSort> { +public class ShellSort> { private ShellSort() { } diff --git a/src/com/jwetherell/algorithms/strings/KnuthMorrisPratt.java b/src/com/jwetherell/algorithms/strings/KnuthMorrisPratt.java index 361ccd31..97806084 100644 --- a/src/com/jwetherell/algorithms/strings/KnuthMorrisPratt.java +++ b/src/com/jwetherell/algorithms/strings/KnuthMorrisPratt.java @@ -8,8 +8,8 @@ * Prefix-suffix of string S is a substring which occurs at the beginning and at the end of S. *

* Time complexity: O(n)
- *
- * https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm + *

+ * @see Knuth Morris Pratt (Wikipedia) *
* @author Szymon Stankiewicz * @author Justin Wetherell @@ -22,14 +22,11 @@ private KnuthMorrisPratt() {} * This function implements KMP algorithm for finding length of maximal prefix-suffix for each prefix of the string. * Prefix-suffix of string S is a substring which occurs at the beginning and at the end of S. *

- * Time complexity: O(n)

- *

- * https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm - *
* @param text Text * @return maximal length of prefix-suffix for each prefix of the string text */ public static List getPrefSufTable(String text) { + final List prefSufTable = new ArrayList(); final char[] chars = text.toCharArray(); diff --git a/src/com/jwetherell/algorithms/strings/Manacher.java b/src/com/jwetherell/algorithms/strings/Manacher.java index af715875..eab8d5c4 100644 --- a/src/com/jwetherell/algorithms/strings/Manacher.java +++ b/src/com/jwetherell/algorithms/strings/Manacher.java @@ -6,10 +6,10 @@ *

* The longest palindromic substring problem should not be confused with * the different problem of finding the longest palindromic subsequence. - *
+ *

* Manacher's algorithm finds the longest palindromic substring in linear time O(n); where n = length(input) - *
- * https://en.wikipedia.org/wiki/Longest_palindromic_substring#Manacher.27s_algorithm + *

+ * @see Manacher's Algorithm (Wikipedia) *
* @author Piotr Kruk * @author Justin Wetherell diff --git a/src/com/jwetherell/algorithms/strings/Rotation.java b/src/com/jwetherell/algorithms/strings/Rotation.java index c92ebb0f..09d8520d 100644 --- a/src/com/jwetherell/algorithms/strings/Rotation.java +++ b/src/com/jwetherell/algorithms/strings/Rotation.java @@ -4,7 +4,7 @@ * Rotation of the string is some cyclic transformation of that string. * More formally a string s = uv is said to be a rotation of t if t = vu. *

- * http://en.wikipedia.org/wiki/String_(computer_science)#Rotations + * @see String Rotation (Wikipedia) *
* @Author Szymon Stankiewicz * @author Justin Wetherell @@ -57,11 +57,12 @@ private static String bestRotation(String text, boolean greatest) { * lowest lexicographical order of all such rotations. * Finding the lexicographically minimal rotation is useful as a way of normalizing strings. *

- * http://en.wikipedia.org/wiki/Lexicographically_minimal_string_rotation - *
+ * @see Lexicographically Minimal String Rotation (Wikipedia) + *

* This function implements Duval's algorithm. - *
- * http://en.wikipedia.org/wiki/Lexicographically_minimal_string_rotation#Duval.27s_Lyndon_Factorization_Algorithm + *

+ * @see Duval's Algorithm (Wikipedia) + *

* Complexity: O(n) *
* @param text @@ -77,11 +78,11 @@ public static String getLexicographicallyMinimalRotation(String text) { * highest lexicographical order of all such rotations. * Finding the lexicographically maximal rotation is useful as a way of normalizing strings. *

- * http://en.wikipedia.org/wiki/Lexicographically_minimal_string_rotation - *
+ * @see Lexicographically Minimal String Rotation (Wikipedia) + *

* This function implements Duval's algorithm. - * http://en.wikipedia.org/wiki/Lexicographically_minimal_string_rotation#Duval.27s_Lyndon_Factorization_Algorithm - *
+ * @see Duval's Algorithm (Wikipedia) + *

* Complexity: O(n) *
* @param text diff --git a/src/com/jwetherell/algorithms/strings/StringFunctions.java b/src/com/jwetherell/algorithms/strings/StringFunctions.java index a4e05234..8b9cdb41 100644 --- a/src/com/jwetherell/algorithms/strings/StringFunctions.java +++ b/src/com/jwetherell/algorithms/strings/StringFunctions.java @@ -5,7 +5,7 @@ /** * This class contains methods for modifying text. - * + *

* @author Justin Wetherell */ public class StringFunctions { diff --git a/test/com/jwetherell/algorithms/data_structures/test/LCPArrayTest.java b/test/com/jwetherell/algorithms/data_structures/test/LCPArrayTest.java index 3bfbe3fa..4fea9863 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/LCPArrayTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/LCPArrayTest.java @@ -13,7 +13,7 @@ public class LCPArrayTest { @Test public void smallTest(){ String string = "asdasdd"; - LCPArray LCPArrayBuilder = new LCPArray(string); + LCPArray LCPArrayBuilder = new LCPArray(string); ArrayList LCPArray = LCPArrayBuilder.getLCPArray(); ArrayList result = new ArrayList(); @@ -24,9 +24,8 @@ public void smallTest(){ @Test public void longTest(){ - String string = "aasfaasdsadasdfasdasdasdasfdasfassdfas"; - LCPArray LCPArrayBuilder = new LCPArray(string); + LCPArray LCPArrayBuilder = new LCPArray(string); ArrayList LCPArray = LCPArrayBuilder.getLCPArray(); ArrayList result = new ArrayList(); @@ -38,9 +37,8 @@ public void longTest(){ @Test public void singleLetterTest(){ - String string = "aaaaaaaaaaaa"; - LCPArray LCPArrayBuilder = new LCPArray(string); + LCPArray LCPArrayBuilder = new LCPArray(string); ArrayList LCPArray = LCPArrayBuilder.getLCPArray(); ArrayList result = new ArrayList(); diff --git a/test/com/jwetherell/algorithms/data_structures/test/common/HeapTest.java b/test/com/jwetherell/algorithms/data_structures/test/common/HeapTest.java index 3c92e2e8..b580f666 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/common/HeapTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/common/HeapTest.java @@ -7,28 +7,6 @@ public class HeapTest { - /** - * Test the heap invariants. - * - * In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: If A is a parent - * node of B then the key of node A is ordered with respect to the key of node B with the same ordering applying across the - * heap. Either the keys of parent nodes are always greater than or equal to those of the children and the highest key is in - * the root node (this kind of heap is called max heap) or the keys of parent nodes are less than or equal to those of the - * children and the lowest key is in the root node (min heap). - * - * http://en.wikipedia.org/wiki/Heap_(data_structure) - * - * @author Justin Wetherell - * - * @param heapType Type of heap (Min or Max). - * @param heap Heap to test. - * @param type Type of data in the heap (Either String or Integer). - * @param name Name used in debug. - * @param unsorted Unsorted test data. - * @param sorted Sorted test data. - * @param invalid Invalid data which isn't in the data-structure. - * @return True if the heap passes it's invariants tests. - */ public static > boolean testHeap(BinaryHeap.Type heapType, IHeap heap, Class type, String name, Integer[] unsorted, Integer[] sorted, Integer _invalid) { for (int i = 0; i < unsorted.length; i++) { diff --git a/test/com/jwetherell/algorithms/data_structures/test/common/IteratorTest.java b/test/com/jwetherell/algorithms/data_structures/test/common/IteratorTest.java index db4f92c6..fd30f061 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/common/IteratorTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/common/IteratorTest.java @@ -4,16 +4,6 @@ public class IteratorTest { - /** - * In object-oriented computer programming, an iterator is an object that enables a programmer to traverse a container. - * - * http://en.wikipedia.org/wiki/Iterator - * - * @author Justin Wetherell - * - * @param iter Iterator to test. - * @return True if iterator passes invariant tests. - */ public static > boolean testIterator(Iterator iter) { while (iter.hasNext()) { T item = iter.next(); diff --git a/test/com/jwetherell/algorithms/data_structures/test/common/JavaCollectionTest.java b/test/com/jwetherell/algorithms/data_structures/test/common/JavaCollectionTest.java index fdf9db50..12e790a7 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/common/JavaCollectionTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/common/JavaCollectionTest.java @@ -4,22 +4,6 @@ public class JavaCollectionTest { - /** - * In computer science, a collection or container is a grouping of some variable number of data items - * (possibly zero) that have some shared significance to the problem being solved and need to be operated - * upon together in some controlled fashion. - * - * http://en.wikipedia.org/wiki/Collection_(abstract_data_type) - * - * @author Justin Wetherell - * - * @param collection Collection to test. - * @param type Type of data in the collection (Either String or Integer). - * @param name name Name used in debug. - * @param unsorted Unsorted test data. - * @param sorted Sorted test data. - * @return True if the collection passes it's invariants tests. - */ public static > boolean testCollection(Collection collection, Class type, String name, Integer[] unsorted, Integer[] sorted, Integer _invalid) { // Make sure the collection is empty diff --git a/test/com/jwetherell/algorithms/data_structures/test/common/JavaMapTest.java b/test/com/jwetherell/algorithms/data_structures/test/common/JavaMapTest.java index 9f2f0a57..df74fbdd 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/common/JavaMapTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/common/JavaMapTest.java @@ -3,23 +3,6 @@ @SuppressWarnings("unchecked") public class JavaMapTest { - /** - * In computer science, an associative array, map, symbol table, or dictionary is an abstract data - * type composed of a collection of (key, value) pairs, such that each possible key appears at most - * once in the collection. - * - * http://en.wikipedia.org/wiki/Associative_array - * - * @author Justin Wetherell - * - * @param map Map to test. - * @param type Type of data in the map (Either String or Integer). - * @param name Name used in debug. - * @param Unsorted test data. - * @param sorted Sorted test data. - * @param invalid Invalid data which isn't in the data-structure. - * @return True if the map passes it's invariants tests. - */ public static > boolean testJavaMap(java.util.Map map, Class type, String name, Integer[] unsorted, Integer[] sorted, Integer _invalid) { // Make sure the map is empty diff --git a/test/com/jwetherell/algorithms/data_structures/test/common/ListIteratorTest.java b/test/com/jwetherell/algorithms/data_structures/test/common/ListIteratorTest.java index 50ea8879..0d2eb1a1 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/common/ListIteratorTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/common/ListIteratorTest.java @@ -5,19 +5,6 @@ public class ListIteratorTest { - /** - * An iterator for lists that allows the programmer to traverse the list in either direction, - * modify the list during iteration, and obtain the iterator's current position in the list. - * - * http://en.wikipedia.org/wiki/Iterator - * - * @author Justin Wetherell - * - * @param iter List iterator to test. - * @param data Test data to test against. - * @param size Size of the test data. - * @return True if list iterator passes it's invariant tests. - */ public static > boolean testListIterator(ListIterator iter, Class type, Integer[] data, int size) { // Make sure you catch going prev at the start diff --git a/test/com/jwetherell/algorithms/data_structures/test/common/ListTest.java b/test/com/jwetherell/algorithms/data_structures/test/common/ListTest.java index e3875f54..67b5914d 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/common/ListTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/common/ListTest.java @@ -4,20 +4,6 @@ public class ListTest { - /** - * In computer science, a list or sequence is an abstract data type that implements a - * finite ordered collection of values, where the same value may occur more than once. - * - * http://en.wikipedia.org/wiki/List_(abstract_data_type) - * - * @author Justin Wetherell - * - * @param list List to test. - * @param name Name used in debug. - * @param data Test data. - * @param invalid Invalid data which should not exist in the data. - * @return - */ public static > boolean testList(IList list, String name, T[] data, T _invalid) { for (int i = 0; i < data.length; i++) { diff --git a/test/com/jwetherell/algorithms/data_structures/test/common/MapTest.java b/test/com/jwetherell/algorithms/data_structures/test/common/MapTest.java index b1d12b62..41edd578 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/common/MapTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/common/MapTest.java @@ -4,22 +4,6 @@ public class MapTest { - /** - * In computer science, an associative array, map, symbol table, or dictionary is an abstract data - * type composed of a collection of (key, value) pairs, such that each possible key appears at most - * once in the collection. - * - * http://en.wikipedia.org/wiki/Associative_array - * - * @author Justin Wetherell - * - * @param map Map to test. - * @param type Type of data in the map (Either String or Integer). - * @param name Name used in debug. - * @param data test data. - * @param invalid Invalid data which isn't in the data-structure. - * @return True if the map passes it's invariants tests. - */ @SuppressWarnings("unchecked") public static > boolean testMap(IMap map, Class type, String name, Integer[] data, Integer _invalid) { diff --git a/test/com/jwetherell/algorithms/data_structures/test/common/QueueTest.java b/test/com/jwetherell/algorithms/data_structures/test/common/QueueTest.java index 71990102..5bcef7f1 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/common/QueueTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/common/QueueTest.java @@ -4,24 +4,6 @@ public class QueueTest { - /** - * In computer science, a queue is a particular kind of abstract data type or collection in which - * the entities in the collection are kept in order and the principal (or only) operations on the - * collection are the addition of entities to the rear terminal position, known as enqueue, and removal - * of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out - * (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first - * one to be removed. - * - * http://en.wikipedia.org/wiki/Queue_(abstract_data_type) - * - * @author Justin Wetherell - * - * @param queue Queue to test. - * @param name Name used in debug. - * @param data Test data. - * @param invalid Invalid data which isn't in the data-structure. - * @return Trus if the Queue passes all it's invariants. - */ public static > boolean testQueue(IQueue queue, String name, T[] data, T _invalid) { for (int i = 0; i < data.length; i++) { diff --git a/test/com/jwetherell/algorithms/data_structures/test/common/SetTest.java b/test/com/jwetherell/algorithms/data_structures/test/common/SetTest.java index d64d371d..194e1a0e 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/common/SetTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/common/SetTest.java @@ -4,20 +4,6 @@ public class SetTest { - /** - * In computer science, a set is an abstract data structure that can store certain values, - * without any particular order, and no repeated values. - * - * http://en.wikipedia.org/wiki/Set_(abstract_data_type) - * - * @author Justin Wetherell - * - * @param set Set to test. - * @param name Name to use in debug. - * @param data Test data. - * @param invalid Invalid data which isn't in the data-structure. - * @return True if the set passes it's invariants tests. - */ public static > boolean testSet(ISet set, String name, T[] data, T _invalid) { for (int i = 0; i < data.length; i++) { diff --git a/test/com/jwetherell/algorithms/data_structures/test/common/StackTest.java b/test/com/jwetherell/algorithms/data_structures/test/common/StackTest.java index 36f30c87..57554dba 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/common/StackTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/common/StackTest.java @@ -4,21 +4,6 @@ public class StackTest { - /** - * In computer science, a stack is a particular kind of abstract data type or collection - * in which the principal (or only) operations on the collection are the addition of an - * entity to the collection, known as push and removal of an entity, known as pop. - * - * http://en.wikipedia.org/wiki/Stack_(abstract_data_type) - * - * @author Justin Wetherell - * - * @param stack Stack to test. - * @param name Name to use in debug. - * @param data Test data. - * @param invalid Invalid data which isn't in the data-structure. - * @return True if the stack passes it's invariants tests. - */ public static > boolean testStack(IStack stack, String name, T[] data, T _invalid) { for (int i = 0; i < data.length; i++) { diff --git a/test/com/jwetherell/algorithms/data_structures/test/common/TreeTest.java b/test/com/jwetherell/algorithms/data_structures/test/common/TreeTest.java index 5c4cef50..6df37169 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/common/TreeTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/common/TreeTest.java @@ -4,22 +4,6 @@ public class TreeTest { - /** - * In computer science, a tree is a widely used abstract data type (ADT) or data structure implementing this ADT - * that simulates a hierarchical tree structure, with a root value and subtrees of children, represented as a set - * of linked nodes. - * - * http://en.wikipedia.org/wiki/Tree_(data_structure) - * - * @author Justin Wetherell - * - * @param tree Tree to test. - * @param type Type of data in the heap (Either String or Integer). - * @param name Name used in debug. - * @param data Test data. - * @param invalid Invalid data which isn't in the data-structure. - * @return True if the tree passes it's invariants tests. - */ public static > boolean testTree(ITree tree, Class type, String name, Integer[] data, Integer _invalid) { for (int i = 0; i < data.length; i++) { diff --git a/test/com/jwetherell/algorithms/graph/test/BreadthFirstTraversalTest.java b/test/com/jwetherell/algorithms/graph/test/BreadthFirstTraversalTest.java index 2fe88bcb..057ec566 100644 --- a/test/com/jwetherell/algorithms/graph/test/BreadthFirstTraversalTest.java +++ b/test/com/jwetherell/algorithms/graph/test/BreadthFirstTraversalTest.java @@ -12,6 +12,38 @@ public class BreadthFirstTraversalTest { + private static final byte[][] adjacencyMatrix = new byte[4][4]; + static { + // v0 + adjacencyMatrix[0][1] = 1; + adjacencyMatrix[0][2] = 1; + // v1 + adjacencyMatrix[1][2] = 1; + // v2 + adjacencyMatrix[2][0] = 1; + adjacencyMatrix[2][3] = 1; + // v3 + adjacencyMatrix[3][3] = 1; + } + + @Test + public void test0() { + final int[] result = BreadthFirstTraversal.breadthFirstTraversal(4, adjacencyMatrix, 2); + Assert.assertTrue(result[0]==2); + Assert.assertTrue(result[1]==0); + Assert.assertTrue(result[2]==3); + Assert.assertTrue(result[3]==1); + } + + @Test + public void test1() { + final int[] result = BreadthFirstTraversal.breadthFirstTraversal(4, adjacencyMatrix, 0); + Assert.assertTrue(result[0]==0); + Assert.assertTrue(result[1]==1); + Assert.assertTrue(result[2]==2); + Assert.assertTrue(result[3]==3); + } + private static final List> vertices = new ArrayList>(); private static final List> edges = new ArrayList>(); @@ -37,7 +69,7 @@ public class BreadthFirstTraversalTest { private static final Graph graph = new Graph(Graph.TYPE.DIRECTED, vertices, edges); @Test - public void test1() { + public void test2() { final Graph.Vertex[] result = BreadthFirstTraversal.breadthFirstTraversal(graph, v2); Assert.assertTrue(result[0].getValue()==2); Assert.assertTrue(result[1].getValue()==0); @@ -46,7 +78,7 @@ public void test1() { } @Test - public void test2() { + public void test3() { final Graph.Vertex[] result = BreadthFirstTraversal.breadthFirstTraversal(graph, v0); Assert.assertTrue(result[0].getValue()==0); Assert.assertTrue(result[1].getValue()==1); diff --git a/test/com/jwetherell/algorithms/graph/test/DepthFirstTraversalTest.java b/test/com/jwetherell/algorithms/graph/test/DepthFirstTraversalTest.java new file mode 100644 index 00000000..48e26c89 --- /dev/null +++ b/test/com/jwetherell/algorithms/graph/test/DepthFirstTraversalTest.java @@ -0,0 +1,89 @@ +package com.jwetherell.algorithms.graph.test; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; + +import org.junit.Test; + +import com.jwetherell.algorithms.data_structures.Graph; +import com.jwetherell.algorithms.graph.DepthFirstTraversal; + +public class DepthFirstTraversalTest { + + private static final byte[][] adjacencyMatrix = new byte[4][4]; + static { + // v0 + adjacencyMatrix[0][1] = 1; + adjacencyMatrix[0][2] = 1; + // v1 + adjacencyMatrix[1][2] = 1; + // v2 + adjacencyMatrix[2][0] = 1; + adjacencyMatrix[2][3] = 1; + // v3 + adjacencyMatrix[3][3] = 1; + } + + @Test + public void test0() { + final int[] result = DepthFirstTraversal.depthFirstTraversal(4, adjacencyMatrix, 2); + Assert.assertTrue(result[0]==2); + Assert.assertTrue(result[1]==0); + Assert.assertTrue(result[2]==1); + Assert.assertTrue(result[3]==3); + } + + @Test + public void test1() { + final int[] result = DepthFirstTraversal.depthFirstTraversal(4, adjacencyMatrix, 0); + Assert.assertTrue(result[0]==0); + Assert.assertTrue(result[1]==1); + Assert.assertTrue(result[2]==2); + Assert.assertTrue(result[3]==3); + } + + private static final List> vertices = new ArrayList>(); + private static final List> edges = new ArrayList>(); + + private static final Graph.Vertex v0 = new Graph.Vertex(0); + private static final Graph.Vertex v1 = new Graph.Vertex(1); + private static final Graph.Vertex v2 = new Graph.Vertex(2); + private static final Graph.Vertex v3 = new Graph.Vertex(3); + + static { + vertices.add(v0); + vertices.add(v1); + vertices.add(v2); + vertices.add(v3); + + edges.add(new Graph.Edge(0, v0, v1)); + edges.add(new Graph.Edge(0, v0, v2)); + edges.add(new Graph.Edge(0, v1, v2)); + edges.add(new Graph.Edge(0, v2, v0)); + edges.add(new Graph.Edge(0, v2, v3)); + edges.add(new Graph.Edge(0, v3, v3)); + } + + private static final Graph graph = new Graph(Graph.TYPE.DIRECTED, vertices, edges); + + @Test + public void test2() { + final Graph.Vertex[] result = DepthFirstTraversal.depthFirstTraversal(graph, v2); + Assert.assertTrue(result[0].getValue()==2); + Assert.assertTrue(result[1].getValue()==0); + Assert.assertTrue(result[2].getValue()==1); + Assert.assertTrue(result[3].getValue()==3); + } + + @Test + public void test3() { + final Graph.Vertex[] result = DepthFirstTraversal.depthFirstTraversal(graph, v0); + Assert.assertTrue(result[0].getValue()==0); + Assert.assertTrue(result[1].getValue()==1); + Assert.assertTrue(result[2].getValue()==2); + Assert.assertTrue(result[3].getValue()==3); + } +} +