Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored algorithms to increase maintainability and support redability. #118

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/main/java/edu/princeton/cs/algs4/AVLTreeST.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class AVLTreeST<Key extends Comparable<Key>, 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
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/edu/princeton/cs/algs4/BoruvkaMST.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/edu/princeton/cs/algs4/BoyerMoore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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--) {
Expand All @@ -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--) {
Expand Down
30 changes: 2 additions & 28 deletions src/main/java/edu/princeton/cs/algs4/Complex.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}


/**
Expand All @@ -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);
Expand All @@ -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());
}

}
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/edu/princeton/cs/algs4/ComplexTrignometric.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
10 changes: 8 additions & 2 deletions src/main/java/edu/princeton/cs/algs4/DepthFirstOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ public class DepthFirstOrder {
private int[] post; // post[v] = postorder number of v
private Queue<Integer> preorder; // vertices in preorder
private Queue<Integer> 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}.
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -117,6 +121,8 @@ private void dfs(EdgeWeightedDigraph G, int v) {
}
}
postorder.enqueue(v);
// counter for postorder numbering
int postCounter=0;
post[v] = postCounter++;
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/edu/princeton/cs/algs4/FlowNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<FlowEdge>[] adj;
Expand Down
20 changes: 7 additions & 13 deletions src/main/java/edu/princeton/cs/algs4/LazyPrimMST.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Edge> mst; // edges in the MST
private boolean[] marked; // marked[v] = true iff v on tree
private MinPQ<Edge> pq; // edges with one endpoint in tree

Expand All @@ -82,7 +81,7 @@ public class LazyPrimMST {
* @param G the edge-weighted graph
*/
public LazyPrimMST(EdgeWeightedGraph G) {
mst = new Queue<Edge>();
super();
pq = new MinPQ<Edge>();
marked = new boolean[G.V()];
for (int v = 0; v < G.V(); v++) // run Prim from all vertices to
Expand Down Expand Up @@ -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<Edge> edges() {
return mst;
}

/**
* Returns the sum of the edge weights in a minimum spanning tree (or forest).
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/edu/princeton/cs/algs4/LazyPrimMSTWeighted.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package edu.princeton.cs.algs4;

public class LazyPrimMSTWeighted {
// edges in the MST
protected Queue<Edge> mst;

public LazyPrimMSTWeighted(){
mst=new Queue<Edge>();
}

public Iterable<Edge> 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;
}
}
42 changes: 2 additions & 40 deletions src/main/java/edu/princeton/cs/algs4/LinkedStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,13 @@
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class LinkedStack<Item> implements Iterable<Item> {
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<Item> extends LinkedStackIMPL<Item> implements Iterable<Item> {

/**
* Initializes an empty stack.
*/
public LinkedStack() {
first = null;
n = 0;
super();
assert check();
}

Expand Down Expand Up @@ -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.
*
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/edu/princeton/cs/algs4/LinkedStackIMPL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package edu.princeton.cs.algs4;

public class LinkedStackIMPL<Item> {
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;
}
}
Loading