diff --git a/src/main/java/edu/princeton/cs/algs4/AVLTreeST.java b/src/main/java/edu/princeton/cs/algs4/AVLTreeST.java index d8520da38..b3fb6a136 100644 --- a/src/main/java/edu/princeton/cs/algs4/AVLTreeST.java +++ b/src/main/java/edu/princeton/cs/algs4/AVLTreeST.java @@ -74,7 +74,7 @@ public class AVLTreeST, Value> { /** * This class represents an inner node of the AVL tree. */ - private class Node { + protected class Node { private final Key key; // the key private Value val; // the associated value private int height; // height of the subtree diff --git a/src/main/java/edu/princeton/cs/algs4/BoruvkaMST.java b/src/main/java/edu/princeton/cs/algs4/BoruvkaMST.java index 701103388..7f10db7c5 100644 --- a/src/main/java/edu/princeton/cs/algs4/BoruvkaMST.java +++ b/src/main/java/edu/princeton/cs/algs4/BoruvkaMST.java @@ -160,6 +160,13 @@ private boolean check(EdgeWeightedGraph G) { } // check that it is a minimal spanning forest (cut optimality conditions) + if(checkIfMinimalSpanningForest(G)) return false; + + return true; + } + + private boolean checkIfMinimalSpanningForest(EdgeWeightedGraph G){ + UF uf; for (Edge e : edges()) { // all edges in MST except e @@ -175,14 +182,13 @@ private boolean check(EdgeWeightedGraph G) { if (uf.find(x) != uf.find(y)) { if (f.weight() < e.weight()) { System.err.println("Edge " + f + " violates cut optimality conditions"); - return false; + return true; } } } } - - return true; + return false; } /** diff --git a/src/main/java/edu/princeton/cs/algs4/BoyerMoore.java b/src/main/java/edu/princeton/cs/algs4/BoyerMoore.java index db21c7cac..b744059e9 100644 --- a/src/main/java/edu/princeton/cs/algs4/BoyerMoore.java +++ b/src/main/java/edu/princeton/cs/algs4/BoyerMoore.java @@ -46,10 +46,9 @@ public class BoyerMoore { private final int R; // the radix private int[] right; // the bad-character skip array - private char[] pattern; // store the pattern as a character array private String pat; // or as a string - + private int skip; /** * Preprocesses the pattern string. * @@ -98,7 +97,6 @@ public BoyerMoore(char[] pattern, int R) { public int search(String txt) { int m = pat.length(); int n = txt.length(); - int skip; for (int i = 0; i <= n - m; i += skip) { skip = 0; for (int j = m-1; j >= 0; j--) { @@ -124,7 +122,6 @@ public int search(String txt) { public int search(char[] text) { int m = pattern.length; int n = text.length; - int skip; for (int i = 0; i <= n - m; i += skip) { skip = 0; for (int j = m-1; j >= 0; j--) { diff --git a/src/main/java/edu/princeton/cs/algs4/Complex.java b/src/main/java/edu/princeton/cs/algs4/Complex.java index 6f5dadedd..3c011b9fd 100644 --- a/src/main/java/edu/princeton/cs/algs4/Complex.java +++ b/src/main/java/edu/princeton/cs/algs4/Complex.java @@ -210,33 +210,6 @@ public Complex divides(Complex that) { public Complex exp() { return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im)); } - - /** - * Returns the complex sine of this complex number. - * - * @return the complex sine of this complex number - */ - public Complex sin() { - return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im)); - } - - /** - * Returns the complex cosine of this complex number. - * - * @return the complex cosine of this complex number - */ - public Complex cos() { - return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im)); - } - - /** - * Returns the complex tangent of this complex number. - * - * @return the complex tangent of this complex number - */ - public Complex tan() { - return sin().divides(cos()); - } /** @@ -247,6 +220,7 @@ public Complex tan() { public static void main(String[] args) { Complex a = new Complex(5.0, 6.0); Complex b = new Complex(-3.0, 4.0); + ComplexTrignometric math_obj = new ComplexTrignometric(); StdOut.println("a = " + a); StdOut.println("b = " + b); @@ -260,7 +234,7 @@ public static void main(String[] args) { StdOut.println("(a / b) * b = " + a.divides(b).times(b)); StdOut.println("conj(a) = " + a.conjugate()); StdOut.println("|a| = " + a.abs()); - StdOut.println("tan(a) = " + a.tan()); + StdOut.println("tan(a) = " + math_obj.tan()); } } diff --git a/src/main/java/edu/princeton/cs/algs4/ComplexTrignometric.java b/src/main/java/edu/princeton/cs/algs4/ComplexTrignometric.java new file mode 100644 index 000000000..4767dde07 --- /dev/null +++ b/src/main/java/edu/princeton/cs/algs4/ComplexTrignometric.java @@ -0,0 +1,33 @@ +package edu.princeton.cs.algs4; + +public class ComplexTrignometric { + private double re; + private double im; + + /** + * Returns the complex sine of this complex number. + * + * @return the complex sine of this complex number + */ + public Complex sin() { + return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im)); + } + + /** + * Returns the complex cosine of this complex number. + * + * @return the complex cosine of this complex number + */ + public Complex cos() { + return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im)); + } + + /** + * Returns the complex tangent of this complex number. + * + * @return the complex tangent of this complex number + */ + public Complex tan() { + return sin().divides(cos()); + } +} diff --git a/src/main/java/edu/princeton/cs/algs4/DepthFirstOrder.java b/src/main/java/edu/princeton/cs/algs4/DepthFirstOrder.java index 8433921e9..47f323a91 100644 --- a/src/main/java/edu/princeton/cs/algs4/DepthFirstOrder.java +++ b/src/main/java/edu/princeton/cs/algs4/DepthFirstOrder.java @@ -58,8 +58,6 @@ public class DepthFirstOrder { private int[] post; // post[v] = postorder number of v private Queue preorder; // vertices in preorder private Queue postorder; // vertices in postorder - private int preCounter; // counter or preorder numbering - private int postCounter; // counter for postorder numbering /** * Determines a depth-first order for the digraph {@code G}. @@ -93,6 +91,8 @@ public DepthFirstOrder(EdgeWeightedDigraph G) { // run DFS in digraph G from vertex v and compute preorder/postorder private void dfs(Digraph G, int v) { + // counter or preorder numbering + int preCounter=0; marked[v] = true; pre[v] = preCounter++; preorder.enqueue(v); @@ -102,11 +102,15 @@ private void dfs(Digraph G, int v) { } } postorder.enqueue(v); +// counter for postorder numbering + int postCounter=0; post[v] = postCounter++; } // run DFS in edge-weighted digraph G from vertex v and compute preorder/postorder private void dfs(EdgeWeightedDigraph G, int v) { + // counter or preorder numbering + int preCounter=0; marked[v] = true; pre[v] = preCounter++; preorder.enqueue(v); @@ -117,6 +121,8 @@ private void dfs(EdgeWeightedDigraph G, int v) { } } postorder.enqueue(v); +// counter for postorder numbering + int postCounter=0; post[v] = postCounter++; } diff --git a/src/main/java/edu/princeton/cs/algs4/FlowNetwork.java b/src/main/java/edu/princeton/cs/algs4/FlowNetwork.java index ddb997e9f..31381a8e7 100644 --- a/src/main/java/edu/princeton/cs/algs4/FlowNetwork.java +++ b/src/main/java/edu/princeton/cs/algs4/FlowNetwork.java @@ -35,7 +35,6 @@ */ public class FlowNetwork { private static final String NEWLINE = System.getProperty("line.separator"); - private final int V; private int E; private Bag[] adj; diff --git a/src/main/java/edu/princeton/cs/algs4/LazyPrimMST.java b/src/main/java/edu/princeton/cs/algs4/LazyPrimMST.java index 9ad382231..6e9b339f7 100644 --- a/src/main/java/edu/princeton/cs/algs4/LazyPrimMST.java +++ b/src/main/java/edu/princeton/cs/algs4/LazyPrimMST.java @@ -69,11 +69,10 @@ * @author Robert Sedgewick * @author Kevin Wayne */ -public class LazyPrimMST { +public class LazyPrimMST extends LazyPrimMSTWeighted { private static final double FLOATING_POINT_EPSILON = 1E-12; + protected double weight; // total weight of MST - private double weight; // total weight of MST - private Queue mst; // edges in the MST private boolean[] marked; // marked[v] = true iff v on tree private MinPQ pq; // edges with one endpoint in tree @@ -82,7 +81,7 @@ public class LazyPrimMST { * @param G the edge-weighted graph */ public LazyPrimMST(EdgeWeightedGraph G) { - mst = new Queue(); + super(); pq = new MinPQ(); marked = new boolean[G.V()]; for (int v = 0; v < G.V(); v++) // run Prim from all vertices to @@ -111,18 +110,13 @@ private void prim(EdgeWeightedGraph G, int s) { private void scan(EdgeWeightedGraph G, int v) { assert !marked[v]; marked[v] = true; + getWeightedGraph(G,v); + } + + private void getWeightedGraph(EdgeWeightedGraph G,int v){ for (Edge e : G.adj(v)) if (!marked[e.other(v)]) pq.insert(e); } - - /** - * Returns the edges in a minimum spanning tree (or forest). - * @return the edges in a minimum spanning tree (or forest) as - * an iterable of edges - */ - public Iterable edges() { - return mst; - } /** * Returns the sum of the edge weights in a minimum spanning tree (or forest). diff --git a/src/main/java/edu/princeton/cs/algs4/LazyPrimMSTWeighted.java b/src/main/java/edu/princeton/cs/algs4/LazyPrimMSTWeighted.java new file mode 100644 index 000000000..67af53d74 --- /dev/null +++ b/src/main/java/edu/princeton/cs/algs4/LazyPrimMSTWeighted.java @@ -0,0 +1,20 @@ +package edu.princeton.cs.algs4; + +public class LazyPrimMSTWeighted { + // edges in the MST + protected Queue mst; + + public LazyPrimMSTWeighted(){ + mst=new Queue(); + } + + public Iterable edges(){ + /** + * Returns the edges in a minimum spanning tree (or forest). + * + * @return the edges in a minimum spanning tree (or forest) as + * an iterable of edges + */ + return mst; + } +} diff --git a/src/main/java/edu/princeton/cs/algs4/LinkedStack.java b/src/main/java/edu/princeton/cs/algs4/LinkedStack.java index 7f7a61aff..5f340c97a 100644 --- a/src/main/java/edu/princeton/cs/algs4/LinkedStack.java +++ b/src/main/java/edu/princeton/cs/algs4/LinkedStack.java @@ -40,22 +40,13 @@ * @author Robert Sedgewick * @author Kevin Wayne */ -public class LinkedStack implements Iterable { - private int n; // size of the stack - private Node first; // top of stack - - // helper linked list class - private class Node { - private Item item; - private Node next; - } +public class LinkedStack extends LinkedStackIMPL implements Iterable { /** * Initializes an empty stack. */ public LinkedStack() { - first = null; - n = 0; + super(); assert check(); } @@ -147,35 +138,6 @@ 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; - } - else if (n == 1) { - if (first == null) return false; - 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 && numberOfNodes <= n; x = x.next) { - numberOfNodes++; - } - if (numberOfNodes != n) return false; - - return true; - } - /** * Unit tests the {@code LinkedStack} data type. * diff --git a/src/main/java/edu/princeton/cs/algs4/LinkedStackIMPL.java b/src/main/java/edu/princeton/cs/algs4/LinkedStackIMPL.java new file mode 100644 index 000000000..6aedbf1fb --- /dev/null +++ b/src/main/java/edu/princeton/cs/algs4/LinkedStackIMPL.java @@ -0,0 +1,46 @@ +package edu.princeton.cs.algs4; + +public class LinkedStackIMPL { + protected int n; // size of the stack + protected Node first; // top of stack + + public LinkedStackIMPL(){ + n = 0; + first = null; + } + + // check internal invariants + protected boolean check() { + + // check a few properties of instance variable 'first' + if (n < 0) { + return false; + } + if (n == 0) { + if (first != null) return false; + } + else if (n == 1) { + if (first == null) return false; + 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 && numberOfNodes <= n; x = x.next) { + numberOfNodes++; + } + if (numberOfNodes != n) return false; + + return true; + } + + // helper linked list class + protected class Node { + protected Item item; + protected LinkedStack.Node next; + } +} diff --git a/src/main/java/edu/princeton/cs/algs4/NFA.java b/src/main/java/edu/princeton/cs/algs4/NFA.java index a866099c9..71599bd87 100644 --- a/src/main/java/edu/princeton/cs/algs4/NFA.java +++ b/src/main/java/edu/princeton/cs/algs4/NFA.java @@ -114,7 +114,18 @@ public boolean recognizes(String txt) { if (dfs.marked(v)) pc.add(v); // Compute possible NFA states for txt[i+1] - for (int i = 0; i < txt.length(); i++) { + pc=computeNFA(txt,pc); + if(pc==null) return false; + + // check for accept state + for (int v : pc) + if (v == m) return true; + return false; + } + + private Bag computeNFA(String txt,Bag pc){ + DirectedDFS dfs; + for (int i=0; i < txt.length(); i++) { if (txt.charAt(i) == '*' || txt.charAt(i) == '|' || txt.charAt(i) == '(' || txt.charAt(i) == ')') throw new IllegalArgumentException("text contains the metacharacter '" + txt.charAt(i) + "'"); @@ -122,23 +133,19 @@ public boolean recognizes(String txt) { for (int v : pc) { if (v == m) continue; if ((regexp.charAt(v) == txt.charAt(i)) || regexp.charAt(v) == '.') - match.add(v+1); + match.add(v+1); } if (match.isEmpty()) continue; - dfs = new DirectedDFS(graph, match); - pc = new Bag(); + dfs = new DirectedDFS(graph, match); + pc= new Bag(); for (int v = 0; v < graph.V(); v++) if (dfs.marked(v)) pc.add(v); // optimization if no states reachable - if (pc.size() == 0) return false; + if (pc.size() == 0) return null; } - - // check for accept state - for (int v : pc) - if (v == m) return true; - return false; + return pc; } /** diff --git a/src/main/java/edu/princeton/cs/algs4/QuickX.java b/src/main/java/edu/princeton/cs/algs4/QuickX.java index 76ba38b6b..cdd2c97a9 100644 --- a/src/main/java/edu/princeton/cs/algs4/QuickX.java +++ b/src/main/java/edu/princeton/cs/algs4/QuickX.java @@ -53,49 +53,16 @@ private static void sort(Comparable[] a, int lo, int hi) { Insertion.sort(a, lo, hi + 1); return; } + QuickXIMPL sort_object = new QuickXIMPL(); - int j = partition(a, lo, hi); + int j = QuickXIMPL.partition(a, lo, hi); sort(a, lo, j-1); sort(a, j+1, hi); } - // partition the subarray a[lo..hi] so that a[lo..j-1] <= a[j] <= a[j+1..hi] - // and return the index j. - private static int partition(Comparable[] a, int lo, int hi) { - int n = hi - lo + 1; - int m = median3(a, lo, lo + n/2, hi); - exch(a, m, lo); - - int i = lo; - int j = hi + 1; - Comparable v = a[lo]; - - // a[lo] is unique largest element - while (less(a[++i], v)) { - if (i == hi) { exch(a, lo, hi); return hi; } - } - - // a[lo] is unique smallest element - while (less(v, a[--j])) { - if (j == lo + 1) return lo; - } - - // the main loop - while (i < j) { - exch(a, i, j); - while (less(a[++i], v)) ; - while (less(v, a[--j])) ; - } - - // put partitioning item v at a[j] - exch(a, lo, j); - - // now, a[lo .. j-1] <= a[j] <= a[j+1 .. hi] - return j; - } // return the index of the median element among a[i], a[j], and a[k] - private static int median3(Comparable[] a, int i, int j, int k) { + protected static int median3(Comparable[] a,int i,int j,int k) { return (less(a[i], a[j]) ? (less(a[j], a[k]) ? j : less(a[i], a[k]) ? k : i) : (less(a[k], a[j]) ? j : less(a[k], a[i]) ? k : i)); @@ -106,12 +73,12 @@ private static int median3(Comparable[] a, int i, int j, int k) { ***************************************************************************/ // is v < w ? - private static boolean less(Comparable v, Comparable w) { + protected static boolean less(Comparable v,Comparable w) { return v.compareTo(w) < 0; } // exchange a[i] and a[j] - private static void exch(Object[] a, int i, int j) { + protected static void exch(Object[] a,int i,int j) { Object swap = a[i]; a[i] = a[j]; a[j] = swap; diff --git a/src/main/java/edu/princeton/cs/algs4/QuickXIMPL.java b/src/main/java/edu/princeton/cs/algs4/QuickXIMPL.java new file mode 100644 index 000000000..1500b5324 --- /dev/null +++ b/src/main/java/edu/princeton/cs/algs4/QuickXIMPL.java @@ -0,0 +1,40 @@ +package edu.princeton.cs.algs4; + +import static edu.princeton.cs.algs4.QuickX.*; + +// partition the subarray a[lo..hi] so that a[lo..j-1] <= a[j] <= a[j+1..hi] +// and return the index j. +public class QuickXIMPL { + public static int partition(Comparable[] a,int lo,int hi) { + int n = hi - lo + 1; + int m = median3(a, lo, lo + n/2, hi); + exch(a, m, lo); + + int i = lo; + int j = hi + 1; + Comparable v = a[lo]; + + // a[lo] is unique largest element + while (less(a[++i], v)) { + if (i == hi) { exch(a, lo, hi); return hi; } + } + + // a[lo] is unique smallest element + while (less(v, a[--j])) { + if (j == lo + 1) return lo; + } + + // the main loop + while (i < j) { + exch(a, i, j); + while (less(a[++i], v)) ; + while (less(v, a[--j])) ; + } + + // put partitioning item v at a[j] + exch(a, lo, j); + + // now, a[lo .. j-1] <= a[j] <= a[j+1 .. hi] + return j; + } +} diff --git a/src/main/java/edu/princeton/cs/algs4/RabinKarp.java b/src/main/java/edu/princeton/cs/algs4/RabinKarp.java index 65d669341..93676e7ba 100644 --- a/src/main/java/edu/princeton/cs/algs4/RabinKarp.java +++ b/src/main/java/edu/princeton/cs/algs4/RabinKarp.java @@ -93,7 +93,7 @@ private long hash(String key, int m) { } // Las Vegas version: does pat[] match txt[i..i-m+1] ? - private boolean check(String txt, int i) { + private boolean checkForPatternString(String txt,int i) { for (int j = 0; j < m; j++) if (pat.charAt(j) != txt.charAt(i + j)) return false; @@ -101,7 +101,7 @@ private boolean check(String txt, int i) { } // Monte Carlo version: always return true - // private boolean check(int i) { + // private boolean checkForPatternString(int i) { // return true; //} @@ -118,19 +118,19 @@ public int search(String txt) { if (n < m) return n; long txtHash = hash(txt, m); - // check for match at offset 0 - if ((patHash == txtHash) && check(txt, 0)) + // checkForPatternString for match at offset 0 + if ((patHash == txtHash) && checkForPatternString(txt, 0)) return 0; - // check for hash match; if hash match, check for exact match + // checkForPatternString for hash match; if hash match, checkForPatternString for exact match for (int i = m; i < n; i++) { - // Remove leading digit, add trailing digit, check for match. + // Remove leading digit, add trailing digit, checkForPatternString for match. txtHash = (txtHash + q - RM*txt.charAt(i-m) % q) % q; txtHash = (txtHash*R + txt.charAt(i)) % q; // match int offset = i - m + 1; - if ((patHash == txtHash) && check(txt, offset)) + if ((patHash == txtHash) && checkForPatternString(txt, offset)) return offset; } diff --git a/src/main/java/edu/princeton/cs/algs4/StaticSETofInts.java b/src/main/java/edu/princeton/cs/algs4/StaticSETofInts.java index aeefe9037..b884e4699 100644 --- a/src/main/java/edu/princeton/cs/algs4/StaticSETofInts.java +++ b/src/main/java/edu/princeton/cs/algs4/StaticSETofInts.java @@ -26,7 +26,8 @@ * @author Robert Sedgewick * @author Kevin Wayne */ -public class StaticSETofInts { +public class +StaticSETofInts { private int[] a; /**