Skip to content

Commit

Permalink
renames LRS to LongestRepeatedSubstring
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-wayne committed Sep 26, 2015
1 parent 36a3b7a commit 929f10c
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 14 deletions.
133 changes: 133 additions & 0 deletions src/main/java/edu/princeton/cs/algs4/Accumulator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/******************************************************************************
* Compilation: javac Accumulator.java
* Execution: java Accumulator < input.txt
* Dependencies: StdOut.java StdIn.java
*
* Mutable data type that calculates the mean, sample standard
* deviation, and sample variance of a stream of real numbers
* use a stable, one-pass algorithm.
*
******************************************************************************/

package edu.princeton.cs.algs4;


/**
* The <tt>Accumulator</tt> class is a data type for computing the running
* mean, sample standard deviation, and sample variance of a stream of real
* numbers. It provides an example of a mutable data type and a streaming
* algorithm.
* <p>
* This implementation uses a one-pass algorithm that is less susceptible
* to floating-point roundoff error than the more straightforward
* implementation based on saving the sum of the squares of the numbers.
* This technique is due to
* <a href = "https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm">B. P. Welford</a>.
* Each operation takes constant time in the worst case.
* The amount of memory is constant - the data values are not stored.
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/12oop">Section 1.2</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class Accumulator {
private int n = 0; // number of data values
private double sum = 0.0; // sample variance * (n-1)
private double mu = 0.0; // sample mean

/**
* Initializes an accumulator.
*/
public Accumulator() {
}

/**
* Adds the specified data value to the accumulator.
* @param x the data value
*/
public void addDataValue(double x) {
n++;
double delta = x - mu;
mu += delta / n;
sum += (double) (n - 1) / n * delta * delta;
}

/**
* Returns the mean of the data values.
* @return the mean of the data values
*/
public double mean() {
return mu;
}

/**
* Returns the sample variance of the data values.
* @return the sample variance of the data values
*/
public double var() {
return sum / (n - 1);
}

/**
* Returns the sample standard deviation of the data values.
* @return the sample standard deviation of the data values
*/
public double stddev() {
return Math.sqrt(this.var());
}

/**
* Returns the number of data values.
* @return the number of data values
*/
public int count() {
return n;
}

/**
* Unit tests the <tt>Accumulator</tt> data type.
* Reads in a stream of real number from standard input;
* adds them to the accumulator; and prints the mean,
* sample standard deviation, and sample variance to standard
* output.
*/
public static void main(String[] args) {
Accumulator stats = new Accumulator();
while (!StdIn.isEmpty()) {
double x = StdIn.readDouble();
stats.addDataValue(x);
}

StdOut.printf("N = %d\n", stats.count());
StdOut.printf("mean = %.5f\n", stats.mean());
StdOut.printf("stddev = %.5f\n", stats.stddev());
StdOut.printf("var = %.5f\n", stats.var());
}
}

/******************************************************************************
* Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/
8 changes: 6 additions & 2 deletions src/main/java/edu/princeton/cs/algs4/LinkedQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ public String toString() {

// check internal invariants
private boolean check() {
if (N == 0) {
if (N < 0) {
return false;
}
else if (N == 0) {
if (first != null) return false;
if (last != null) return false;
}
Expand All @@ -135,13 +138,14 @@ else if (N == 1) {
if (first.next != null) return false;
}
else {
if (first == null || last == null) return false;
if (first == last) return false;
if (first.next == null) return false;
if (last.next != null) return false;

// check internal consistency of instance variable N
int numberOfNodes = 0;
for (Node x = first; x != null; x = x.next) {
for (Node x = first; x != null && numberOfNodes <= N; x = x.next) {
numberOfNodes++;
}
if (numberOfNodes != N) return false;
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/edu/princeton/cs/algs4/LinkedStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public Item next() {

// check internal invariants
private boolean check() {

// check a few properties of instance variable 'first'
if (N < 0) {
return false;
}
if (N == 0) {
if (first != null) return false;
}
Expand All @@ -154,12 +159,13 @@ else if (N == 1) {
if (first.next != null) return false;
}
else {
if (first == null) return false;
if (first.next == null) return false;
}

// check internal consistency of instance variable N
int numberOfNodes = 0;
for (Node x = first; x != null; x = x.next) {
for (Node x = first; x != null && numberOfNodes <= N; x = x.next) {
numberOfNodes++;
}
if (numberOfNodes != N) return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
* Compilation: javac LRS.java
* Execution: java LRS < file.txt
* Compilation: javac LongestRepeatedSubstring.java
* Execution: java LongestRepeatedSubstring < file.txt
* Dependencies: StdIn.java SuffixArray.java
* Data files: http://algs4.cs.princeton.edu/63suffix/tinyTale.txt
* http://algs4.cs.princeton.edu/63suffix/mobydick.txt
Expand All @@ -9,17 +9,17 @@
* whitespace with a single space, and then computes the longest
* repeated substring in that text using a suffix array.
*
* % java LRS < tinyTale.txt
* % java LongestRepeatedSubstring < tinyTale.txt
* 'st of times it was the '
*
* % java LRS < mobydick.txt
* % java LongestRepeatedSubstring < mobydick.txt
* ',- Such a funny, sporty, gamy, jesty, joky, hoky-poky lad, is the Ocean, oh! Th'
*
* % java LRS
* % java LongestRepeatedSubstring
* aaaaaaaaa
* 'aaaaaaaa'
*
* % java LRS
* % java LongestRepeatedSubstring
* abcdefg
* ''
*
Expand All @@ -28,8 +28,8 @@
package edu.princeton.cs.algs4;

/**
* The <tt>LRS</tt> class provides a {@link SuffixArray} client for computing
* the longest repeated substring of a string.
* The <tt>LongestRepeatedSubstring</tt> class provides a {@link SuffixArray}
* client for computing the longest repeated substring of a string.
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/63suffix">Section 6.3</a> of
Expand All @@ -40,10 +40,10 @@
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class LRS {
public class LongestRepeatedSubstring {

// Do not instantiate.
private LRS() { }
private LongestRepeatedSubstring() { }

/**
* Returns the longest common string of the two specified strings.
Expand All @@ -61,8 +61,8 @@ private LRS() { }
* the empty string if no such string
*/
public static String lrs(String text) {
int N = text.length();
SuffixArray sa = new SuffixArray(text);
int N = sa.length();
String lrs = "";
for (int i = 1; i < N; i++) {
int length = sa.lcp(i);
Expand Down

0 comments on commit 929f10c

Please sign in to comment.