2351b382 by Branden

Initial Commit

1 parent 11b71c47
Showing 222 changed files with 4894 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Kd-Trees</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
0.206107 0.095492
0.975528 0.654508
0.024472 0.345492
0.793893 0.095492
0.793893 0.904508
0.975528 0.345492
0.206107 0.904508
0.500000 0.000000
0.024472 0.654508
0.500000 1.000000
0.740877 0.938153
0.181288 0.114743
0.157726 0.864484
0.684062 0.035112
0.975528 0.345492
0.684062 0.964888
0.938153 0.259123
0.315938 0.964888
0.793893 0.904508
0.938153 0.740877
0.047586 0.287110
0.952414 0.712890
0.114743 0.818712
0.922164 0.767913
0.008856 0.406309
0.999013 0.531395
0.008856 0.593691
0.624345 0.984292
0.345492 0.975528
0.206107 0.904508
0.000000 0.500000
0.500000 0.000000
0.181288 0.885257
0.562667 0.003943
0.654508 0.975528
0.003943 0.437333
0.468605 0.999013
0.061847 0.740877
0.468605 0.000987
0.922164 0.232087
0.818712 0.885257
0.712890 0.952414
0.593691 0.008856
0.015708 0.624345
0.406309 0.991144
0.035112 0.315938
0.740877 0.061847
0.000987 0.468605
0.095492 0.206107
0.885257 0.181288
0.767913 0.077836
0.003943 0.562667
0.984292 0.375655
0.035112 0.684062
0.259123 0.061847
0.232087 0.922164
0.996057 0.562667
0.077836 0.232087
0.593691 0.991144
0.375655 0.015708
0.000987 0.531395
0.375655 0.984292
0.077836 0.767913
0.114743 0.181288
0.904508 0.206107
0.024472 0.654508
0.206107 0.095492
0.562667 0.996057
0.287110 0.952414
0.406309 0.008856
0.315938 0.035112
0.975528 0.654508
0.991144 0.593691
0.437333 0.003943
0.842274 0.864484
0.345492 0.024472
0.287110 0.047586
0.232087 0.077836
0.624345 0.015708
0.904508 0.793893
0.015708 0.375655
0.964888 0.684062
0.999013 0.468605
0.500000 1.000000
0.135516 0.842274
0.095492 0.793893
0.991144 0.406309
0.654508 0.024472
0.767913 0.922164
0.984292 0.624345
0.531395 0.999013
0.996057 0.437333
0.818712 0.114743
0.864484 0.157726
0.964888 0.315938
1.000000 0.500000
0.531395 0.000987
0.842274 0.135516
0.259123 0.938153
0.061847 0.259123
0.885257 0.818712
0.135516 0.157726
0.024472 0.345492
0.437333 0.996057
0.793893 0.095492
0.864484 0.842274
0.952414 0.287110
0.047586 0.712890
0.157726 0.135516
0.712890 0.047586
This diff could not be displayed because it is too large.
0.206107 0.095492
0.975528 0.654508
0.024472 0.345492
0.793893 0.095492
0.793893 0.904508
0.975528 0.345492
0.206107 0.904508
0.500000 0.000000
0.024472 0.654508
0.500000 1.000000
0.761250 0.317125
\ No newline at end of file
0.000000 0.500000
0.500000 1.000000
0.500000 0.000000
1.000000 0.500000
0.9 0.5
0.2 0.5
0.3 0.5
0.4 0.5
0.1 0.5
0.6 0.5
0.5 0.5
0.7 0.5
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
import java.io.BufferedReader;
import java.io.FileReader;
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.Queue;
import edu.princeton.cs.algs4.RectHV;
import edu.princeton.cs.algs4.StdDraw;
public class KdTree {
private static final boolean vertical = true;
private static final boolean horizontal = false;
private Node root;
private int size;
private static class Node {
private Point2D p; // the point
private RectHV rect; // the axis-aligned rectangle corresponding to this
// node
private Node lb; // the left/bottom subtree
private Node rt; // the right/top subtree
public Node(Point2D p, RectHV rect) {
this.p = p;
this.rect = rect;
}
}
// construct an empty set of points
public KdTree() {
root = null;
}
// is the set empty?
public boolean isEmpty() {
return size() == 0;
}
// number of points in the set
public int size() {
return size;
}
// add the point to the set (if it is not already in the set)
public void insert(Point2D p) {
if (p == null)
throw new NullPointerException();
root = insert(root, p, new RectHV(0, 0, 1, 1), vertical);
}
// helper function to recursively insert to the tree
private Node insert(Node x, Point2D p, RectHV rect, boolean orientation) {
// if x is null we've reached the end and can add a new node
if (x == null) {
this.size++;
return new Node(p, rect);
}
// if the node's point equals the point passed in
// then return that node to avoid duplicates
if (x.p.equals(p)) {
return x;
}
// determine if a node belongs to the left or right branch of the tree
// based off it's orientation. The root node is vertical and the
// orientation
// alternates between that and horizontal
if (orientation == vertical) {
// if the current node is vertical then the node it branches from
// will be horizontal
// so the x values are compared to determine which side to add the
// new node to
double cmp = p.x() - x.p.x();
if (cmp < 0) {
x.lb = insert(x.lb, p, new RectHV(x.rect.xmin(), x.rect.ymin(), x.p.x(), x.rect.ymax()), horizontal);
} else {
x.rt = insert(x.rt, p, new RectHV(x.p.x(), x.rect.ymin(), x.rect.xmax(), x.rect.ymax()), horizontal);
}
} else {
// same as above except the current node is horizontal so the
// branches will be vertical
// the y values are compared to determine which side to add the new
// node to
double cmp = p.y() - x.p.y();
if (cmp < 0) {
x.lb = insert(x.lb, p, new RectHV(x.rect.xmin(), x.rect.ymin(), x.rect.xmax(), x.p.y()), vertical);
} else {
x.rt = insert(x.rt, p, new RectHV(x.rect.xmin(), x.p.y(), x.rect.xmax(), x.rect.ymax()), vertical);
}
}
return x;
}
// does the set contain point p?
public boolean contains(Point2D p) {
if (p == null)
throw new NullPointerException();
return get(p);
}
// helper function to get a specific point p
private boolean get(Point2D p) {
return get(root, p, vertical);
}
// helper function to recursively find the node in the tree
private boolean get(Node x, Point2D p, boolean orientation) {
// the point doesn't exist in the tree
if (x == null)
return false;
// the point does exist in the tree
if (x.p.equals(p)) {
return true;
}
// compare points based on the orientation and either their x or y
// coordinate
// and returns the next node in the tree
double cmp;
if (orientation == vertical) {
cmp = p.x() - x.p.x();
} else {
cmp = p.y() - x.p.y();
}
if (cmp < 0) {
return get(x.lb, p, !orientation);
} else {
return get(x.rt, p, !orientation);
}
}
// draw all points to standard draw
public void draw() {
draw(root, vertical);
}
// draws red lines for vertical line segments
// draws blue lines for horizontal line segments
private void draw(Node x, boolean orientation) {
if (orientation == vertical) {
StdDraw.setPenColor(StdDraw.RED);
StdDraw.line(x.p.x(), x.rect.ymin(), x.p.x(), x.rect.ymax());
} else {
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.line(x.rect.xmin(), x.p.y(), x.rect.xmax(), x.p.y());
}
if (x.lb != null) {
draw(x.lb, !orientation);
}
if (x.rt != null) {
draw(x.rt, !orientation);
}
// draw point last to be on top of line
StdDraw.setPenColor(StdDraw.BLACK);
x.p.draw();
}
// all points that are inside the rectangle
public Iterable<Point2D> range(RectHV rect) {
Queue<Point2D> queue = new Queue<>();
range(root, rect, queue);
return queue;
}
// recurse through the tree to find intersecting rectangles of the
// nodes in the tree while the node is not null.
private void range(Node x, RectHV rect, Queue<Point2D> queue) {
if (x != null) {
if (!x.rect.intersects(rect)) {
return;
}
if (rect.contains(x.p)) {
queue.enqueue(x.p);
}
range(x.lb, rect, queue);
range(x.rt, rect, queue);
}
}
// a nearest neighbor in the set to point p; null if the set is empty
public Point2D nearest(Point2D p) {
if (p == null)
throw new NullPointerException();
return nearest(root, p, root.p, vertical);
}
// garbage please redo
private Point2D nearest(Node x, Point2D p, Point2D min, boolean orientation) {
if (x == null)
return min;
if (orientation == vertical) {
if (p.x() < x.p.x()) {
min = nearest(x.rt, p, min, horizontal);
if (x.lb != null && min.distanceSquaredTo(p) > x.lb.rect.distanceSquaredTo(p)) {
min = nearest(x.lb, p, min, horizontal);
}
} else {
min = nearest(x.lb, p, min, horizontal);
if (x.rt != null && min.distanceSquaredTo(p) > x.rt.rect.distanceSquaredTo(p)) {
min = nearest(x.rt, p, min, horizontal);
}
}
} else {
if (p.y() < x.p.y()) {
min = nearest(x.lb, p, min, vertical);
if (x.lb != null && min.distanceSquaredTo(p) > x.lb.rect.distanceSquaredTo(p)) {
min = nearest(x.lb, p, min, vertical);
}
} else {
min = nearest(x.lb, p, min, vertical);
if (x.rt != null && min.distanceSquaredTo(p) > x.rt.rect.distanceSquaredTo(p)) {
min = nearest(x.rt, p, min, vertical);
}
}
}
return min;
}
// unit testing of the methods (optional)
public static void main(String[] args) throws Exception {
KdTree kdtree = new KdTree();
/*
* System.out.println(kdtree.size());
* System.out.println(kdtree.isEmpty()); kdtree.insert(new Point2D(0.2,
* 0.4)); kdtree.insert(new Point2D(0.9, 0.6)); kdtree.insert(new
* Point2D(0.024, 0.34)); kdtree.insert(new Point2D(0.1, 0.6));
* kdtree.insert(new Point2D(0.6, 0.2)); kdtree.insert(new Point2D(0.7,
* 0.1)); kdtree.insert(new Point2D(0.6, 0.2)); kdtree.insert(new
* Point2D(0.7, 0.1)); kdtree.insert(new Point2D(0.5, 0.5));
*
* System.out.println(kdtree.isEmpty());
* System.out.println(kdtree.contains(new Point2D(0.97, 0.34)));
* System.out.println(kdtree.contains(new Point2D(0.5, 0.5)));
*
* Iterable<Point2D> iterable = kdtree.range(new RectHV(0,0,1,1));
*
* for(Point2D point : iterable){ System.out.println(point.toString());
* }
*
* kdtree.draw();
*/
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(args[0]));
} catch (Exception e) {
System.out.println("File not found");
}
String line;
while ((line = reader.readLine()) != null) {
String[] splitLine = line.trim().split("\\s+");
double a = Double.parseDouble(splitLine[0]);
double b = Double.parseDouble(splitLine[1]);
Point2D p = new Point2D(a, b);
kdtree.insert(p);
}
}
}
\ No newline at end of file
/******************************************************************************
* Compilation: javac KdTreeGenerator.java
* Execution: java KdTreeGenerator n
* Dependencies:
*
* Creates n random points in the unit square and print to standard output.
*
* % java KdTreeGenerator 5
* 0.195080 0.938777
* 0.351415 0.017802
* 0.556719 0.841373
* 0.183384 0.636701
* 0.649952 0.237188
*
******************************************************************************/
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdOut;
public class KdTreeGenerator {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
for (int i = 0; i < n; i++) {
double x = StdRandom.uniform(0.0, 1.0);
double y = StdRandom.uniform(0.0, 1.0);
StdOut.printf("%8.6f %8.6f\n", x, y);
}
}
}
/******************************************************************************
* Compilation: javac KdTreeVisualizer.java
* Execution: java KdTreeVisualizer
* Dependencies: KdTree.java
*
* Add the points that the user clicks in the standard draw window
* to a kd-tree and draw the resulting kd-tree.
*
******************************************************************************/
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
public class KdTreeVisualizer {
public static void main(String[] args) {
RectHV rect = new RectHV(0.0, 0.0, 1.0, 1.0);
StdDraw.enableDoubleBuffering();
KdTree kdtree = new KdTree();
while (true) {
if (StdDraw.mousePressed()) {
double x = StdDraw.mouseX();
double y = StdDraw.mouseY();
StdOut.printf("%8.6f %8.6f\n", x, y);
Point2D p = new Point2D(x, y);
if (rect.contains(p)) {
StdOut.printf("%8.6f %8.6f\n", x, y);
kdtree.insert(p);
StdDraw.clear();
kdtree.draw();
StdDraw.show();
}
}
StdDraw.pause(50);
}
}
}
/******************************************************************************
* Compilation: javac NearestNeighborVisualizer.java
* Execution: java NearestNeighborVisualizer input.txt
* Dependencies: PointSET.java KdTree.java
*
* Read points from a file (specified as a command-line argument) and
* draw to standard draw. Highlight the closest point to the mouse.
*
* The nearest neighbor according to the brute-force algorithm is drawn
* in red; the nearest neighbor using the kd-tree algorithm is drawn in blue.
*
******************************************************************************/
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.StdDraw;
public class NearestNeighborVisualizer {
public static void main(String[] args) {
String filename = args[0];
In in = new In(filename);
StdDraw.enableDoubleBuffering();
// initialize the two data structures with point from standard input
PointSET brute = new PointSET();
KdTree kdtree = new KdTree();
while (!in.isEmpty()) {
double x = in.readDouble();
double y = in.readDouble();
Point2D p = new Point2D(x, y);
kdtree.insert(p);
brute.insert(p);
}
while (true) {
// the location (x, y) of the mouse
double x = StdDraw.mouseX();
double y = StdDraw.mouseY();
Point2D query = new Point2D(x, y);
// draw all of the points
StdDraw.clear();
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.01);
brute.draw();
// draw in red the nearest neighbor (using brute-force algorithm)
StdDraw.setPenRadius(0.03);
StdDraw.setPenColor(StdDraw.RED);
brute.nearest(query).draw();
StdDraw.setPenRadius(0.02);
// draw in blue the nearest neighbor (using kd-tree algorithm)
StdDraw.setPenColor(StdDraw.BLUE);
kdtree.nearest(query).draw();
StdDraw.show();
StdDraw.pause(40);
}
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import edu.princeton.cs.algs4.SET;
public class PointSET {
private SET<Point2D> points;
// construct an empty set of points
public PointSET() {
this.points = new SET<Point2D>();
}
// is the set empty?
public boolean isEmpty() {
return points.isEmpty();
}
// number of points in the set
public int size() {
return points.size();
}
// add the point to the set (if it is not already in the set)
public void insert(Point2D p) {
if(p == null)
throw new NullPointerException();
if (!points.contains(p)) {
points.add(p);
}
}
// does the set contain point p?
public boolean contains(Point2D p) {
if(p == null)
throw new NullPointerException();
return points.contains(p);
}
// draw all points to standard draw
public void draw() {
for (Point2D p : points) {
p.draw();
}
}
// all points that are inside the rectangle
public Iterable<Point2D> range(RectHV rect) {
SET<Point2D> contains = new SET<Point2D>();
for (Point2D p : points) {
if (rect.contains(p)) {
contains.add(p);
}
}
return contains;
}
// a nearest neighbor in the set to point p; null if the set is empty
public Point2D nearest(Point2D p) {
Point2D closest = null;
for (Point2D point : points) {
if (closest == null || p.distanceTo(point) < p.distanceTo(closest)) {
closest = p;
}
}
return closest;
}
// unit testing of the methods (optional)
public static void main(String[] args) throws Exception{
PointSET set = new PointSET();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(args[0]));
} catch (Exception e) {
System.out.println("File not found");
}
String line;
while((line = reader.readLine()) != null){
String [] splitLine = line.trim().split("\\s+");
double a = Double.parseDouble(splitLine[0]);
double b = Double.parseDouble(splitLine[1]);
Point2D p = new Point2D(a,b);
set.insert(p);
}
set.draw();
}
}
\ No newline at end of file
/******************************************************************************
* Compilation: javac RangeSearchVisualizer.java
* Execution: java RangeSearchVisualizer input.txt
* Dependencies: PointSET.java KdTree.java
*
* Read points from a file (specified as a command-line arugment) and
* draw to standard draw. Also draw all of the points in the rectangle
* the user selects by dragging the mouse.
*
* The range search results using the brute-force algorithm are drawn
* in red; the results using the kd-tree algorithms are drawn in blue.
*
******************************************************************************/
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import edu.princeton.cs.algs4.StdDraw;
public class RangeSearchVisualizer {
public static void main(String[] args) {
String filename = args[0];
In in = new In(filename);
StdDraw.enableDoubleBuffering();
// initialize the data structures with N points from standard input
PointSET brute = new PointSET();
KdTree kdtree = new KdTree();
while (!in.isEmpty()) {
double x = in.readDouble();
double y = in.readDouble();
Point2D p = new Point2D(x, y);
kdtree.insert(p);
brute.insert(p);
}
double x0 = 0.0, y0 = 0.0; // initial endpoint of rectangle
double x1 = 0.0, y1 = 0.0; // current location of mouse
boolean isDragging = false; // is the user dragging a rectangle
// draw the points
StdDraw.clear();
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.01);
brute.draw();
StdDraw.show();
while (true) {
// user starts to drag a rectangle
if (StdDraw.mousePressed() && !isDragging) {
x0 = StdDraw.mouseX();
y0 = StdDraw.mouseY();
isDragging = true;
continue;
}
// user is dragging a rectangle
else if (StdDraw.mousePressed() && isDragging) {
x1 = StdDraw.mouseX();
y1 = StdDraw.mouseY();
continue;
}
// mouse no longer pressed
else if (!StdDraw.mousePressed() && isDragging) {
isDragging = false;
}
RectHV rect = new RectHV(Math.min(x0, x1), Math.min(y0, y1),
Math.max(x0, x1), Math.max(y0, y1));
// draw the points
StdDraw.clear();
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.01);
brute.draw();
// draw the rectangle
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius();
rect.draw();
// draw the range search results for brute-force data structure in red
StdDraw.setPenRadius(0.03);
StdDraw.setPenColor(StdDraw.RED);
for (Point2D p : brute.range(rect))
p.draw();
// draw the range search results for kd-tree in blue
StdDraw.setPenRadius(.02);
StdDraw.setPenColor(StdDraw.BLUE);
for (Point2D p : kdtree.range(rect))
p.draw();
StdDraw.show();
StdDraw.pause(40);
}
}
}
/******************************************************************************
* 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 {@code Accumulator} 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 {@code Accumulator} 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.
*
* @param args the command-line arguments
*/
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-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac AcyclicLP.java
* Execution: java AcyclicP V E
* Dependencies: EdgeWeightedDigraph.java DirectedEdge.java Topological.java
* Data files: http://algs4.cs.princeton.edu/44sp/tinyEWDAG.txt
*
* Computes longeset paths in an edge-weighted acyclic digraph.
*
* Remark: should probably check that graph is a DAG before running
*
* % java AcyclicLP tinyEWDAG.txt 5
* 5 to 0 (2.44) 5->1 0.32 1->3 0.29 3->6 0.52 6->4 0.93 4->0 0.38
* 5 to 1 (0.32) 5->1 0.32
* 5 to 2 (2.77) 5->1 0.32 1->3 0.29 3->6 0.52 6->4 0.93 4->7 0.37 7->2 0.34
* 5 to 3 (0.61) 5->1 0.32 1->3 0.29
* 5 to 4 (2.06) 5->1 0.32 1->3 0.29 3->6 0.52 6->4 0.93
* 5 to 5 (0.00)
* 5 to 6 (1.13) 5->1 0.32 1->3 0.29 3->6 0.52
* 5 to 7 (2.43) 5->1 0.32 1->3 0.29 3->6 0.52 6->4 0.93 4->7 0.37
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code AcyclicLP} class represents a data type for solving the
* single-source longest paths problem in edge-weighted directed
* acyclic graphs (DAGs). The edge weights can be positive, negative, or zero.
* <p>
* This implementation uses a topological-sort based algorithm.
* The constructor takes time proportional to <em>V</em> + <em>E</em>,
* where <em>V</em> is the number of vertices and <em>E</em> is the number of edges.
* Afterwards, the {@code distTo()} and {@code hasPathTo()} methods take
* constant time and the {@code pathTo()} method takes time proportional to the
* number of edges in the longest path returned.
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/44sp">Section 4.4</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class AcyclicLP {
private double[] distTo; // distTo[v] = distance of longest s->v path
private DirectedEdge[] edgeTo; // edgeTo[v] = last edge on longest s->v path
/**
* Computes a longest paths tree from {@code s} to every other vertex in
* the directed acyclic graph {@code G}.
* @param G the acyclic digraph
* @param s the source vertex
* @throws IllegalArgumentException if the digraph is not acyclic
* @throws IllegalArgumentException unless {@code 0 <= s < V}
*/
public AcyclicLP(EdgeWeightedDigraph G, int s) {
distTo = new double[G.V()];
edgeTo = new DirectedEdge[G.V()];
validateVertex(s);
for (int v = 0; v < G.V(); v++)
distTo[v] = Double.NEGATIVE_INFINITY;
distTo[s] = 0.0;
// relax vertices in toplogical order
Topological topological = new Topological(G);
if (!topological.hasOrder())
throw new IllegalArgumentException("Digraph is not acyclic.");
for (int v : topological.order()) {
for (DirectedEdge e : G.adj(v))
relax(e);
}
}
// relax edge e, but update if you find a *longer* path
private void relax(DirectedEdge e) {
int v = e.from(), w = e.to();
if (distTo[w] < distTo[v] + e.weight()) {
distTo[w] = distTo[v] + e.weight();
edgeTo[w] = e;
}
}
/**
* Returns the length of a longest path from the source vertex {@code s} to vertex {@code v}.
* @param v the destination vertex
* @return the length of a longest path from the source vertex {@code s} to vertex {@code v};
* {@code Double.NEGATIVE_INFINITY} if no such path
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public double distTo(int v) {
validateVertex(v);
return distTo[v];
}
/**
* Is there a path from the source vertex {@code s} to vertex {@code v}?
* @param v the destination vertex
* @return {@code true} if there is a path from the source vertex
* {@code s} to vertex {@code v}, and {@code false} otherwise
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public boolean hasPathTo(int v) {
validateVertex(v);
return distTo[v] > Double.NEGATIVE_INFINITY;
}
/**
* Returns a longest path from the source vertex {@code s} to vertex {@code v}.
* @param v the destination vertex
* @return a longest path from the source vertex {@code s} to vertex {@code v}
* as an iterable of edges, and {@code null} if no such path
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public Iterable<DirectedEdge> pathTo(int v) {
validateVertex(v);
if (!hasPathTo(v)) return null;
Stack<DirectedEdge> path = new Stack<DirectedEdge>();
for (DirectedEdge e = edgeTo[v]; e != null; e = edgeTo[e.from()]) {
path.push(e);
}
return path;
}
// throw an IllegalArgumentException unless {@code 0 <= v < V}
private void validateVertex(int v) {
int V = distTo.length;
if (v < 0 || v >= V)
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V-1));
}
/**
* Unit tests the {@code AcyclicLP} data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
In in = new In(args[0]);
int s = Integer.parseInt(args[1]);
EdgeWeightedDigraph G = new EdgeWeightedDigraph(in);
AcyclicLP lp = new AcyclicLP(G, s);
for (int v = 0; v < G.V(); v++) {
if (lp.hasPathTo(v)) {
StdOut.printf("%d to %d (%.2f) ", s, v, lp.distTo(v));
for (DirectedEdge e : lp.pathTo(v)) {
StdOut.print(e + " ");
}
StdOut.println();
}
else {
StdOut.printf("%d to %d no path\n", s, v);
}
}
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac AcyclicSP.java
* Execution: java AcyclicSP V E
* Dependencies: EdgeWeightedDigraph.java DirectedEdge.java Topological.java
* Data files: http://algs4.cs.princeton.edu/44sp/tinyEWDAG.txt
*
* Computes shortest paths in an edge-weighted acyclic digraph.
*
* % java AcyclicSP tinyEWDAG.txt 5
* 5 to 0 (0.73) 5->4 0.35 4->0 0.38
* 5 to 1 (0.32) 5->1 0.32
* 5 to 2 (0.62) 5->7 0.28 7->2 0.34
* 5 to 3 (0.61) 5->1 0.32 1->3 0.29
* 5 to 4 (0.35) 5->4 0.35
* 5 to 5 (0.00)
* 5 to 6 (1.13) 5->1 0.32 1->3 0.29 3->6 0.52
* 5 to 7 (0.28) 5->7 0.28
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code AcyclicSP} class represents a data type for solving the
* single-source shortest paths problem in edge-weighted directed acyclic
* graphs (DAGs). The edge weights can be positive, negative, or zero.
* <p>
* This implementation uses a topological-sort based algorithm.
* The constructor takes time proportional to <em>V</em> + <em>E</em>,
* where <em>V</em> is the number of vertices and <em>E</em> is the number of edges.
* Afterwards, the {@code distTo()} and {@code hasPathTo()} methods take
* constant time and the {@code pathTo()} method takes time proportional to the
* number of edges in the shortest path returned.
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/44sp">Section 4.4</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class AcyclicSP {
private double[] distTo; // distTo[v] = distance of shortest s->v path
private DirectedEdge[] edgeTo; // edgeTo[v] = last edge on shortest s->v path
/**
* Computes a shortest paths tree from {@code s} to every other vertex in
* the directed acyclic graph {@code G}.
* @param G the acyclic digraph
* @param s the source vertex
* @throws IllegalArgumentException if the digraph is not acyclic
* @throws IllegalArgumentException unless {@code 0 <= s < V}
*/
public AcyclicSP(EdgeWeightedDigraph G, int s) {
distTo = new double[G.V()];
edgeTo = new DirectedEdge[G.V()];
validateVertex(s);
for (int v = 0; v < G.V(); v++)
distTo[v] = Double.POSITIVE_INFINITY;
distTo[s] = 0.0;
// visit vertices in toplogical order
Topological topological = new Topological(G);
if (!topological.hasOrder())
throw new IllegalArgumentException("Digraph is not acyclic.");
for (int v : topological.order()) {
for (DirectedEdge e : G.adj(v))
relax(e);
}
}
// relax edge e
private void relax(DirectedEdge e) {
int v = e.from(), w = e.to();
if (distTo[w] > distTo[v] + e.weight()) {
distTo[w] = distTo[v] + e.weight();
edgeTo[w] = e;
}
}
/**
* Returns the length of a shortest path from the source vertex {@code s} to vertex {@code v}.
* @param v the destination vertex
* @return the length of a shortest path from the source vertex {@code s} to vertex {@code v};
* {@code Double.POSITIVE_INFINITY} if no such path
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public double distTo(int v) {
validateVertex(v);
return distTo[v];
}
/**
* Is there a path from the source vertex {@code s} to vertex {@code v}?
* @param v the destination vertex
* @return {@code true} if there is a path from the source vertex
* {@code s} to vertex {@code v}, and {@code false} otherwise
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public boolean hasPathTo(int v) {
validateVertex(v);
return distTo[v] < Double.POSITIVE_INFINITY;
}
/**
* Returns a shortest path from the source vertex {@code s} to vertex {@code v}.
* @param v the destination vertex
* @return a shortest path from the source vertex {@code s} to vertex {@code v}
* as an iterable of edges, and {@code null} if no such path
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public Iterable<DirectedEdge> pathTo(int v) {
validateVertex(v);
if (!hasPathTo(v)) return null;
Stack<DirectedEdge> path = new Stack<DirectedEdge>();
for (DirectedEdge e = edgeTo[v]; e != null; e = edgeTo[e.from()]) {
path.push(e);
}
return path;
}
// throw an IllegalArgumentException unless {@code 0 <= v < V}
private void validateVertex(int v) {
int V = distTo.length;
if (v < 0 || v >= V)
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V-1));
}
/**
* Unit tests the {@code AcyclicSP} data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
In in = new In(args[0]);
int s = Integer.parseInt(args[1]);
EdgeWeightedDigraph G = new EdgeWeightedDigraph(in);
// find shortest path from s to each other vertex in DAG
AcyclicSP sp = new AcyclicSP(G, s);
for (int v = 0; v < G.V(); v++) {
if (sp.hasPathTo(v)) {
StdOut.printf("%d to %d (%.2f) ", s, v, sp.distTo(v));
for (DirectedEdge e : sp.pathTo(v)) {
StdOut.print(e + " ");
}
StdOut.println();
}
else {
StdOut.printf("%d to %d no path\n", s, v);
}
}
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac AdjMatrixEdgeWeightedDigraph.java
* Execution: java AdjMatrixEdgeWeightedDigraph V E
* Dependencies: StdOut.java
*
* An edge-weighted digraph, implemented using an adjacency matrix.
* Parallel edges are disallowed; self-loops are allowed.
*
******************************************************************************/
package edu.princeton.cs.algs4;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* The {@code AdjMatrixEdgeWeightedDigraph} class represents a edge-weighted
* digraph of vertices named 0 through <em>V</em> - 1, where each
* directed edge is of type {@link DirectedEdge} and has a real-valued weight.
* It supports the following two primary operations: add a directed edge
* to the digraph and iterate over all of edges incident from a given vertex.
* It also provides
* methods for returning the number of vertices <em>V</em> and the number
* of edges <em>E</em>. Parallel edges are disallowed; self-loops are permitted.
* <p>
* This implementation uses an adjacency-matrix representation.
* All operations take constant time (in the worst case) except
* iterating over the edges incident from a given vertex, which takes
* time proportional to <em>V</em>.
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/44sp">Section 4.4</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class AdjMatrixEdgeWeightedDigraph {
private static final String NEWLINE = System.getProperty("line.separator");
private final int V;
private int E;
private DirectedEdge[][] adj;
/**
* Initializes an empty edge-weighted digraph with {@code V} vertices and 0 edges.
* @param V the number of vertices
* @throws IllegalArgumentException if {@code V < 0}
*/
public AdjMatrixEdgeWeightedDigraph(int V) {
if (V < 0) throw new IllegalArgumentException("number of vertices must be nonnegative");
this.V = V;
this.E = 0;
this.adj = new DirectedEdge[V][V];
}
/**
* Initializes a random edge-weighted digraph with {@code V} vertices and <em>E</em> edges.
* @param V the number of vertices
* @param E the number of edges
* @throws IllegalArgumentException if {@code V < 0}
* @throws IllegalArgumentException if {@code E < 0}
*/
public AdjMatrixEdgeWeightedDigraph(int V, int E) {
this(V);
if (E < 0) throw new IllegalArgumentException("number of edges must be nonnegative");
if (E > V*V) throw new IllegalArgumentException("too many edges");
// can be inefficient
while (this.E != E) {
int v = StdRandom.uniform(V);
int w = StdRandom.uniform(V);
double weight = Math.round(100 * StdRandom.uniform()) / 100.0;
addEdge(new DirectedEdge(v, w, weight));
}
}
/**
* Returns the number of vertices in the edge-weighted digraph.
* @return the number of vertices in the edge-weighted digraph
*/
public int V() {
return V;
}
/**
* Returns the number of edges in the edge-weighted digraph.
* @return the number of edges in the edge-weighted digraph
*/
public int E() {
return E;
}
/**
* Adds the directed edge {@code e} to the edge-weighted digraph (if there
* is not already an edge with the same endpoints).
* @param e the edge
*/
public void addEdge(DirectedEdge e) {
int v = e.from();
int w = e.to();
validateVertex(v);
validateVertex(w);
if (adj[v][w] == null) {
E++;
adj[v][w] = e;
}
}
/**
* Returns the directed edges incident from vertex {@code v}.
* @param v the vertex
* @return the directed edges incident from vertex {@code v} as an Iterable
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public Iterable<DirectedEdge> adj(int v) {
validateVertex(v);
return new AdjIterator(v);
}
// support iteration over graph vertices
private class AdjIterator implements Iterator<DirectedEdge>, Iterable<DirectedEdge> {
private int v;
private int w = 0;
public AdjIterator(int v) {
this.v = v;
}
public Iterator<DirectedEdge> iterator() {
return this;
}
public boolean hasNext() {
while (w < V) {
if (adj[v][w] != null) return true;
w++;
}
return false;
}
public DirectedEdge next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return adj[v][w++];
}
public void remove() {
throw new UnsupportedOperationException();
}
}
/**
* Returns a string representation of the edge-weighted digraph. This method takes
* time proportional to <em>V</em><sup>2</sup>.
* @return the number of vertices <em>V</em>, followed by the number of edges <em>E</em>,
* followed by the <em>V</em> adjacency lists of edges
*/
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V + " " + E + NEWLINE);
for (int v = 0; v < V; v++) {
s.append(v + ": ");
for (DirectedEdge e : adj(v)) {
s.append(e + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
// throw an IllegalArgumentException unless {@code 0 <= v < V}
private void validateVertex(int v) {
if (v < 0 || v >= V)
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V-1));
}
/**
* Unit tests the {@code AdjMatrixEdgeWeightedDigraph} data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
int V = Integer.parseInt(args[0]);
int E = Integer.parseInt(args[1]);
AdjMatrixEdgeWeightedDigraph G = new AdjMatrixEdgeWeightedDigraph(V, E);
StdOut.println(G);
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac Alphabet.java
* Execution: java Alphabet
* Dependencies: StdOut.java
*
* A data type for alphabets, for use with string-processing code
* that must convert between an alphabet of size R and the integers
* 0 through R-1.
*
* Warning: supports only the basic multilingual plane (BMP), i.e,
* Unicode characters between U+0000 and U+FFFF.
*
******************************************************************************/
package edu.princeton.cs.algs4;
public class Alphabet {
/**
* The binary alphabet { 0, 1 }.
*/
public static final Alphabet BINARY = new Alphabet("01");
/**
* The octal alphabet { 0, 1, 2, 3, 4, 5, 6, 7 }.
*/
public static final Alphabet OCTAL = new Alphabet("01234567");
/**
* The decimal alphabet { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }.
*/
public static final Alphabet DECIMAL = new Alphabet("0123456789");
/**
* The hexadecimal alphabet { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F }.
*/
public static final Alphabet HEXADECIMAL = new Alphabet("0123456789ABCDEF");
/**
* The DNA alphabet { A, C, T, G }.
*/
public static final Alphabet DNA = new Alphabet("ACGT");
/**
* The lowercase alphabet { a, b, c, ..., z }.
*/
public static final Alphabet LOWERCASE = new Alphabet("abcdefghijklmnopqrstuvwxyz");
/**
* The uppercase alphabet { A, B, C, ..., Z }.
*/
public static final Alphabet UPPERCASE = new Alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
/**
* The protein alphabet { A, C, D, E, F, G, H, I, K, L, M, N, P, Q, R, S, T, V, W, Y }.
*/
public static final Alphabet PROTEIN = new Alphabet("ACDEFGHIKLMNPQRSTVWY");
/**
* The base-64 alphabet (64 characters).
*/
public static final Alphabet BASE64 = new Alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
/**
* The ASCII alphabet (0-127).
*/
public static final Alphabet ASCII = new Alphabet(128);
/**
* The extended ASCII alphabet (0-255).
*/
public static final Alphabet EXTENDED_ASCII = new Alphabet(256);
/**
* The Unicode 16 alphabet (0-65,535).
*/
public static final Alphabet UNICODE16 = new Alphabet(65536);
private char[] alphabet; // the characters in the alphabet
private int[] inverse; // indices
private final int R; // the radix of the alphabet
/**
* Initializes a new alphabet from the given set of characters.
*
* @param alpha the set of characters
*/
public Alphabet(String alpha) {
// check that alphabet contains no duplicate chars
boolean[] unicode = new boolean[Character.MAX_VALUE];
for (int i = 0; i < alpha.length(); i++) {
char c = alpha.charAt(i);
if (unicode[c])
throw new IllegalArgumentException("Illegal alphabet: repeated character = '" + c + "'");
unicode[c] = true;
}
alphabet = alpha.toCharArray();
R = alpha.length();
inverse = new int[Character.MAX_VALUE];
for (int i = 0; i < inverse.length; i++)
inverse[i] = -1;
// can't use char since R can be as big as 65,536
for (int c = 0; c < R; c++)
inverse[alphabet[c]] = c;
}
/**
* Initializes a new alphabet using characters 0 through R-1.
*
* @param radix the number of characters in the alphabet (the radix R)
*/
private Alphabet(int radix) {
this.R = radix;
alphabet = new char[R];
inverse = new int[R];
// can't use char since R can be as big as 65,536
for (int i = 0; i < R; i++)
alphabet[i] = (char) i;
for (int i = 0; i < R; i++)
inverse[i] = i;
}
/**
* Initializes a new alphabet using characters 0 through 255.
*/
public Alphabet() {
this(256);
}
/**
* Returns true if the argument is a character in this alphabet.
*
* @param c the character
* @return {@code true} if {@code c} is a character in this alphabet;
* {@code false} otherwise
*/
public boolean contains(char c) {
return inverse[c] != -1;
}
/**
* Returns the number of characters in this alphabet (the radix).
*
* @return the number of characters in this alphabet
* @deprecated Replaced by {@link #radix()}.
*/
@Deprecated
public int R() {
return R;
}
/**
* Returns the number of characters in this alphabet (the radix).
*
* @return the number of characters in this alphabet
*/
public int radix() {
return R;
}
/**
* Returns the binary logarithm of the number of characters in this alphabet.
*
* @return the binary logarithm (rounded up) of the number of characters in this alphabet
*/
public int lgR() {
int lgR = 0;
for (int t = R-1; t >= 1; t /= 2)
lgR++;
return lgR;
}
/**
* Returns the index corresponding to the argument character.
*
* @param c the character
* @return the index corresponding to the character {@code c}
* @throws IllegalArgumentException unless {@code c} is a character in this alphabet
*/
public int toIndex(char c) {
if (c >= inverse.length || inverse[c] == -1) {
throw new IllegalArgumentException("Character " + c + " not in alphabet");
}
return inverse[c];
}
/**
* Returns the indices corresponding to the argument characters.
*
* @param s the characters
* @return the indices corresponding to the characters {@code s}
* @throws IllegalArgumentException unless every character in {@code s}
* is a character in this alphabet
*/
public int[] toIndices(String s) {
char[] source = s.toCharArray();
int[] target = new int[s.length()];
for (int i = 0; i < source.length; i++)
target[i] = toIndex(source[i]);
return target;
}
/**
* Returns the character corresponding to the argument index.
*
* @param index the index
* @return the character corresponding to the index {@code index}
* @throws IllegalArgumentException unless {@code 0 <= index < R}
*/
public char toChar(int index) {
if (index < 0 || index >= R) {
throw new IndexOutOfBoundsException("Alphabet index out of bounds");
}
return alphabet[index];
}
/**
* Returns the characters corresponding to the argument indices.
*
* @param indices the indices
* @return the characters corresponding to the indices {@code indices}
* @throws IllegalArgumentException unless {@code 0 < indices[i] < R}
* for every {@code i}
*/
public String toChars(int[] indices) {
StringBuilder s = new StringBuilder(indices.length);
for (int i = 0; i < indices.length; i++)
s.append(toChar(indices[i]));
return s.toString();
}
/**
* Unit tests the {@code Alphabet} data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
int[] encoded1 = Alphabet.BASE64.toIndices("NowIsTheTimeForAllGoodMen");
String decoded1 = Alphabet.BASE64.toChars(encoded1);
StdOut.println(decoded1);
int[] encoded2 = Alphabet.DNA.toIndices("AACGAACGGTTTACCCCG");
String decoded2 = Alphabet.DNA.toChars(encoded2);
StdOut.println(decoded2);
int[] encoded3 = Alphabet.DECIMAL.toIndices("01234567890123456789");
String decoded3 = Alphabet.DECIMAL.toChars(encoded3);
StdOut.println(decoded3);
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac Arbitrage.java
* Execution: java Arbitrage < input.txt
* Dependencies: EdgeWeightedDigraph.java DirectedEdge.java
* BellmanFordSP.java
* Data file: http://algs4.cs.princeton.edu/44sp/rates.txt
*
* Arbitrage detection.
*
* % more rates.txt
* 5
* USD 1 0.741 0.657 1.061 1.005
* EUR 1.349 1 0.888 1.433 1.366
* GBP 1.521 1.126 1 1.614 1.538
* CHF 0.942 0.698 0.619 1 0.953
* CAD 0.995 0.732 0.650 1.049 1
*
* % java Arbitrage < rates.txt
* 1000.00000 USD = 741.00000 EUR
* 741.00000 EUR = 1012.20600 CAD
* 1012.20600 CAD = 1007.14497 USD
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code Arbitrage} class provides a client that finds an arbitrage
* opportunity in a currency exchange table by constructing a
* complete-digraph representation of the exchange table and then finding
* a negative cycle in the digraph.
* <p>
* This implementation uses the Bellman-Ford algorithm to find a
* negative cycle in the complete digraph.
* The running time is proportional to <em>V</em><sup>3</sup> in the
* worst case, where <em>V</em> is the number of currencies.
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/44sp">Section 4.4</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class Arbitrage {
// this class cannot be instantiated
private Arbitrage() { }
/**
* Reads the currency exchange table from standard input and
* prints an arbitrage opportunity to standard output (if one exists).
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
// V currencies
int V = StdIn.readInt();
String[] name = new String[V];
// create complete network
EdgeWeightedDigraph G = new EdgeWeightedDigraph(V);
for (int v = 0; v < V; v++) {
name[v] = StdIn.readString();
for (int w = 0; w < V; w++) {
double rate = StdIn.readDouble();
DirectedEdge e = new DirectedEdge(v, w, -Math.log(rate));
G.addEdge(e);
}
}
// find negative cycle
BellmanFordSP spt = new BellmanFordSP(G, 0);
if (spt.hasNegativeCycle()) {
double stake = 1000.0;
for (DirectedEdge e : spt.negativeCycle()) {
StdOut.printf("%10.5f %s ", stake, name[e.from()]);
stake *= Math.exp(-e.weight());
StdOut.printf("= %10.5f %s\n", stake, name[e.to()]);
}
}
else {
StdOut.println("No arbitrage opportunity");
}
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac Average.java
* Execution: java Average < data.txt
* Dependencies: StdIn.java StdOut.java
*
* Reads in a sequence of real numbers, and computes their average.
*
* % java Average
* 10.0 5.0 6.0
* 3.0 7.0 32.0
* [Ctrl-d]
* Average is 10.5
*
* Note [Ctrl-d] signifies the end of file on Unix.
* On windows use [Ctrl-z].
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code Average} class provides a client for reading in a sequence
* of real numbers and printing out their average.
* <p>
* For additional documentation, see <a href="http://algs4.cs.princeton.edu/11model">Section 1.1</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class Average {
// this class should not be instantiated
private Average() { }
/**
* Reads in a sequence of real numbers from standard input and prints
* out their average to standard output.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
int count = 0; // number input values
double sum = 0.0; // sum of input values
// read data and compute statistics
while (!StdIn.isEmpty()) {
double value = StdIn.readDouble();
sum += value;
count++;
}
// compute the average
double average = sum / count;
// print results
StdOut.println("Average is " + average);
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac Bag.java
* Execution: java Bag < input.txt
* Dependencies: StdIn.java StdOut.java
*
* A generic bag or multiset, implemented using a singly-linked list.
*
* % more tobe.txt
* to be or not to - be - - that - - - is
*
* % java Bag < tobe.txt
* size of bag = 14
* is
* -
* -
* -
* that
* -
* -
* be
* -
* to
* not
* or
* be
* to
*
******************************************************************************/
package edu.princeton.cs.algs4;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* The {@code Bag} class represents a bag (or multiset) of
* generic items. It supports insertion and iterating over the
* items in arbitrary order.
* <p>
* This implementation uses a singly-linked list with a static nested class Node.
* See {@link LinkedBag} for the version from the
* textbook that uses a non-static nested class.
* The <em>add</em>, <em>isEmpty</em>, and <em>size</em> operations
* take constant time. Iteration takes time proportional to the number of items.
* <p>
* For additional documentation, see <a href="http://algs4.cs.princeton.edu/13stacks">Section 1.3</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*
* @param <Item> the generic type of an item in this bag
*/
public class Bag<Item> implements Iterable<Item> {
private Node<Item> first; // beginning of bag
private int n; // number of elements in bag
// helper linked list class
private static class Node<Item> {
private Item item;
private Node<Item> next;
}
/**
* Initializes an empty bag.
*/
public Bag() {
first = null;
n = 0;
}
/**
* Returns true if this bag is empty.
*
* @return {@code true} if this bag is empty;
* {@code false} otherwise
*/
public boolean isEmpty() {
return first == null;
}
/**
* Returns the number of items in this bag.
*
* @return the number of items in this bag
*/
public int size() {
return n;
}
/**
* Adds the item to this bag.
*
* @param item the item to add to this bag
*/
public void add(Item item) {
Node<Item> oldfirst = first;
first = new Node<Item>();
first.item = item;
first.next = oldfirst;
n++;
}
/**
* Returns an iterator that iterates over the items in this bag in arbitrary order.
*
* @return an iterator that iterates over the items in this bag in arbitrary order
*/
public Iterator<Item> iterator() {
return new ListIterator<Item>(first);
}
// an iterator, doesn't implement remove() since it's optional
private class ListIterator<Item> implements Iterator<Item> {
private Node<Item> current;
public ListIterator(Node<Item> first) {
current = first;
}
public boolean hasNext() { return current != null; }
public void remove() { throw new UnsupportedOperationException(); }
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
}
/**
* Unit tests the {@code Bag} data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
Bag<String> bag = new Bag<String>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
bag.add(item);
}
StdOut.println("size of bag = " + bag.size());
for (String s : bag) {
StdOut.println(s);
}
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac BinaryDump.java
* Execution: java BinaryDump N < file
* Dependencies: BinaryStdIn.java
* Data file: http://introcs.cs.princeton.edu/stdlib/abra.txt
*
* Reads in a binary file and writes out the bits, N per line.
*
* % more abra.txt
* ABRACADABRA!
*
* % java BinaryDump 16 < abra.txt
* 0100000101000010
* 0101001001000001
* 0100001101000001
* 0100010001000001
* 0100001001010010
* 0100000100100001
* 96 bits
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code BinaryDump} class provides a client for displaying the contents
* of a binary file in binary.
* <p>
* For more full-featured versions, see the Unix utilities
* {@code od} (octal dump) and {@code hexdump} (hexadecimal dump).
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/55compress">Section 5.5</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
* <p>
* See also {@link HexDump} and {@link PictureDump}.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class BinaryDump {
// Do not instantiate.
private BinaryDump() { }
/**
* Reads in a sequence of bytes from standard input and writes
* them to standard output in binary, k bits per line,
* where k is given as a command-line integer (defaults
* to 16 if no integer is specified); also writes the number
* of bits.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
int bitsPerLine = 16;
if (args.length == 1) {
bitsPerLine = Integer.parseInt(args[0]);
}
int count;
for (count = 0; !BinaryStdIn.isEmpty(); count++) {
if (bitsPerLine == 0) {
BinaryStdIn.readBoolean();
continue;
}
else if (count != 0 && count % bitsPerLine == 0) StdOut.println();
if (BinaryStdIn.readBoolean()) StdOut.print(1);
else StdOut.print(0);
}
if (bitsPerLine != 0) StdOut.println();
StdOut.println(count + " bits");
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac BinaryInsertion.java
* Execution: java BinaryInsertion < input.txt
* Dependencies: StdOut.java StdIn.java
* Data files: http://algs4.cs.princeton.edu/21sort/tiny.txt
* http://algs4.cs.princeton.edu/21sort/words3.txt
*
* Sorts a sequence of strings from standard input using
* binary insertion sort with half exchanges.
*
* % more tiny.txt
* S O R T E X A M P L E
*
* % java BinaryInsertion < tiny.txt
* A E E L M O P R S T X [ one string per line ]
*
* % more words3.txt
* bed bug dad yes zoo ... all bad yet
*
* % java BinaryInsertion < words3.txt
* all bad bed bug dad ... yes yet zoo [ one string per line ]
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code BinaryInsertion} class provides a static method for sorting an
* array using an optimized binary insertion sort with half exchanges.
* <p>
* This implementation makes ~ n lg n compares for any array of length n.
* However, in the worst case, the running time is quadratic because the
* number of array accesses can be proportional to n^2 (e.g, if the array
* is reverse sorted). As such, it is not suitable for sorting large
* arrays (unless the number of inversions is small).
* <p>
* The sorting algorithm is stable and uses O(1) extra memory.
* <p>
* For additional documentation, see <a href="http://algs4.cs.princeton.edu/21elementary">Section 2.1</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Ivan Pesin
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class BinaryInsertion {
// This class should not be instantiated.
private BinaryInsertion() { }
/**
* Rearranges the array in ascending order, using the natural order.
* @param a the array to be sorted
*/
public static void sort(Comparable[] a) {
int n = a.length;
for (int i = 1; i < n; i++) {
// binary search to determine index j at which to insert a[i]
Comparable v = a[i];
int lo = 0, hi = i;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (less(v, a[mid])) hi = mid;
else lo = mid + 1;
}
// insetion sort with "half exchanges"
// (insert a[i] at index j and shift a[j], ..., a[i-1] to right)
for (int j = i; j > lo; --j)
a[j] = a[j-1];
a[lo] = v;
}
assert isSorted(a);
}
/***************************************************************************
* Helper sorting function.
***************************************************************************/
// is v < w ?
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
/***************************************************************************
* Check if array is sorted - useful for debugging.
***************************************************************************/
private static boolean isSorted(Comparable[] a) {
return isSorted(a, 0, a.length - 1);
}
// is the array sorted from a[lo] to a[hi]
private static boolean isSorted(Comparable[] a, int lo, int hi) {
for (int i = lo+1; i <= hi; i++)
if (less(a[i], a[i-1])) return false;
return true;
}
// print array to standard output
private static void show(Comparable[] a) {
for (int i = 0; i < a.length; i++) {
StdOut.println(a[i]);
}
}
/**
* Reads in a sequence of strings from standard input; insertion sorts them;
* and prints them to standard output in ascending order.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
String[] a = StdIn.readAllStrings();
BinaryInsertion.sort(a);
show(a);
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac BinarySearch.java
* Execution: java BinarySearch whitelist.txt < input.txt
* Dependencies: In.java StdIn.java StdOut.java
* Data files: http://algs4.cs.princeton.edu/11model/tinyW.txt
* http://algs4.cs.princeton.edu/11model/tinyT.txt
* http://algs4.cs.princeton.edu/11model/largeW.txt
* http://algs4.cs.princeton.edu/11model/largeT.txt
*
* % java BinarySearch tinyW.txt < tinyT.txt
* 50
* 99
* 13
*
* % java BinarySearch largeW.txt < largeT.txt | more
* 499569
* 984875
* 295754
* 207807
* 140925
* 161828
* [367,966 total values]
*
******************************************************************************/
package edu.princeton.cs.algs4;
import java.util.Arrays;
/**
* The {@code BinarySearch} class provides a static method for binary
* searching for an integer in a sorted array of integers.
* <p>
* The <em>indexOf</em> operations takes logarithmic time in the worst case.
* <p>
* For additional documentation, see <a href="http://algs4.cs.princeton.edu/11model">Section 1.1</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class BinarySearch {
/**
* This class should not be instantiated.
*/
private BinarySearch() { }
/**
* Returns the index of the specified key in the specified array.
*
* @param a the array of integers, must be sorted in ascending order
* @param key the search key
* @return index of key in array {@code a} if present; {@code -1} otherwise
*/
public static int indexOf(int[] a, int key) {
int lo = 0;
int hi = a.length - 1;
while (lo <= hi) {
// Key is in a[lo..hi] or not present.
int mid = lo + (hi - lo) / 2;
if (key < a[mid]) hi = mid - 1;
else if (key > a[mid]) lo = mid + 1;
else return mid;
}
return -1;
}
/**
* Returns the index of the specified key in the specified array.
* This function is poorly named because it does not give the <em>rank</em>
* if the array has duplicate keys or if the key is not in the array.
*
* @param key the search key
* @param a the array of integers, must be sorted in ascending order
* @return index of key in array {@code a} if present; {@code -1} otherwise
* @deprecated Replaced by {@link #indexOf(int[], int)}.
*/
@Deprecated
public static int rank(int key, int[] a) {
return indexOf(a, key);
}
/**
* Reads in a sequence of integers from the whitelist file, specified as
* a command-line argument; reads in integers from standard input;
* prints to standard output those integers that do <em>not</em> appear in the file.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
// read the integers from a file
In in = new In(args[0]);
int[] whitelist = in.readAllInts();
// sort the array
Arrays.sort(whitelist);
// read integer key from standard input; print if not in whitelist
while (!StdIn.isEmpty()) {
int key = StdIn.readInt();
if (BinarySearch.indexOf(whitelist, key) == -1)
StdOut.println(key);
}
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac BinaryStdOut.java
* Execution: java BinaryStdOut
* Dependencies: none
*
* Write binary data to standard output, either one 1-bit boolean,
* one 8-bit char, one 32-bit int, one 64-bit double, one 32-bit float,
* or one 64-bit long at a time.
*
* The bytes written are not aligned.
*
******************************************************************************/
package edu.princeton.cs.algs4;
import java.io.BufferedOutputStream;
import java.io.IOException;
/**
* <i>Binary standard output</i>. This class provides methods for converting
* primtive type variables ({@code boolean}, {@code byte}, {@code char},
* {@code int}, {@code long}, {@code float}, and {@code double})
* to sequences of bits and writing them to standard output.
* Uses big-endian (most-significant byte first).
* <p>
* The client must {@code flush()} the output stream when finished writing bits.
* <p>
* The client should not intermixing calls to {@code BinaryStdOut} with calls
* to {@code StdOut} or {@code System.out}; otherwise unexpected behavior
* will result.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public final class BinaryStdOut {
private static BufferedOutputStream out = new BufferedOutputStream(System.out);
private static int buffer; // 8-bit buffer of bits to write out
private static int n; // number of bits remaining in buffer
// don't instantiate
private BinaryStdOut() { }
/**
* Write the specified bit to standard output.
*/
private static void writeBit(boolean bit) {
// add bit to buffer
buffer <<= 1;
if (bit) buffer |= 1;
// if buffer is full (8 bits), write out as a single byte
n++;
if (n == 8) clearBuffer();
}
/**
* Write the 8-bit byte to standard output.
*/
private static void writeByte(int x) {
assert x >= 0 && x < 256;
// optimized if byte-aligned
if (n == 0) {
try {
out.write(x);
}
catch (IOException e) {
e.printStackTrace();
}
return;
}
// otherwise write one bit at a time
for (int i = 0; i < 8; i++) {
boolean bit = ((x >>> (8 - i - 1)) & 1) == 1;
writeBit(bit);
}
}
// write out any remaining bits in buffer to standard output, padding with 0s
private static void clearBuffer() {
if (n == 0) return;
if (n > 0) buffer <<= (8 - n);
try {
out.write(buffer);
}
catch (IOException e) {
e.printStackTrace();
}
n = 0;
buffer = 0;
}
/**
* Flush standard output, padding 0s if number of bits written so far
* is not a multiple of 8.
*/
public static void flush() {
clearBuffer();
try {
out.flush();
}
catch (IOException e) {
e.printStackTrace();
}
}
/**
* Flush and close standard output. Once standard output is closed, you can no
* longer write bits to it.
*/
public static void close() {
flush();
try {
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
/**
* Write the specified bit to standard output.
* @param x the {@code boolean} to write.
*/
public static void write(boolean x) {
writeBit(x);
}
/**
* Write the 8-bit byte to standard output.
* @param x the {@code byte} to write.
*/
public static void write(byte x) {
writeByte(x & 0xff);
}
/**
* Write the 32-bit int to standard output.
* @param x the {@code int} to write.
*/
public static void write(int x) {
writeByte((x >>> 24) & 0xff);
writeByte((x >>> 16) & 0xff);
writeByte((x >>> 8) & 0xff);
writeByte((x >>> 0) & 0xff);
}
/**
* Write the r-bit int to standard output.
* @param x the {@code int} to write.
* @param r the number of relevant bits in the char.
* @throws IllegalArgumentException if {@code r} is not between 1 and 32.
* @throws IllegalArgumentException if {@code x} is not between 0 and 2<sup>r</sup> - 1.
*/
public static void write(int x, int r) {
if (r == 32) {
write(x);
return;
}
if (r < 1 || r > 32) throw new IllegalArgumentException("Illegal value for r = " + r);
if (x < 0 || x >= (1 << r)) throw new IllegalArgumentException("Illegal " + r + "-bit char = " + x);
for (int i = 0; i < r; i++) {
boolean bit = ((x >>> (r - i - 1)) & 1) == 1;
writeBit(bit);
}
}
/**
* Write the 64-bit double to standard output.
* @param x the {@code double} to write.
*/
public static void write(double x) {
write(Double.doubleToRawLongBits(x));
}
/**
* Write the 64-bit long to standard output.
* @param x the {@code long} to write.
*/
public static void write(long x) {
writeByte((int) ((x >>> 56) & 0xff));
writeByte((int) ((x >>> 48) & 0xff));
writeByte((int) ((x >>> 40) & 0xff));
writeByte((int) ((x >>> 32) & 0xff));
writeByte((int) ((x >>> 24) & 0xff));
writeByte((int) ((x >>> 16) & 0xff));
writeByte((int) ((x >>> 8) & 0xff));
writeByte((int) ((x >>> 0) & 0xff));
}
/**
* Write the 32-bit float to standard output.
* @param x the {@code float} to write.
*/
public static void write(float x) {
write(Float.floatToRawIntBits(x));
}
/**
* Write the 16-bit int to standard output.
* @param x the {@code short} to write.
*/
public static void write(short x) {
writeByte((x >>> 8) & 0xff);
writeByte((x >>> 0) & 0xff);
}
/**
* Write the 8-bit char to standard output.
* @param x the {@code char} to write.
* @throws IllegalArgumentException if {@code x} is not betwen 0 and 255.
*/
public static void write(char x) {
if (x < 0 || x >= 256) throw new IllegalArgumentException("Illegal 8-bit char = " + x);
writeByte(x);
}
/**
* Write the r-bit char to standard output.
* @param x the {@code char} to write.
* @param r the number of relevant bits in the char.
* @throws IllegalArgumentException if {@code r} is not between 1 and 16.
* @throws IllegalArgumentException if {@code x} is not between 0 and 2<sup>r</sup> - 1.
*/
public static void write(char x, int r) {
if (r == 8) {
write(x);
return;
}
if (r < 1 || r > 16) throw new IllegalArgumentException("Illegal value for r = " + r);
if (x >= (1 << r)) throw new IllegalArgumentException("Illegal " + r + "-bit char = " + x);
for (int i = 0; i < r; i++) {
boolean bit = ((x >>> (r - i - 1)) & 1) == 1;
writeBit(bit);
}
}
/**
* Write the string of 8-bit characters to standard output.
* @param s the {@code String} to write.
* @throws IllegalArgumentException if any character in the string is not
* between 0 and 255.
*/
public static void write(String s) {
for (int i = 0; i < s.length(); i++)
write(s.charAt(i));
}
/**
* Write the String of r-bit characters to standard output.
* @param s the {@code String} to write.
* @param r the number of relevants bits in each character.
* @throws IllegalArgumentException if r is not between 1 and 16.
* @throws IllegalArgumentException if any character in the string is not
* between 0 and 2<sup>r</sup> - 1.
*/
public static void write(String s, int r) {
for (int i = 0; i < s.length(); i++)
write(s.charAt(i), r);
}
/**
* Test client.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
int m = Integer.parseInt(args[0]);
// write n integers to binary standard output
for (int i = 0; i < m; i++) {
BinaryStdOut.write(i);
}
BinaryStdOut.flush();
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac BinomialMinPQ.java
* Execution:
*
* A binomial heap.
*
******************************************************************************/
package edu.princeton.cs.algs4;
import java.util.Iterator;
import java.util.Comparator;
import java.util.NoSuchElementException;
/**
* The BinomialMinPQ class represents a priority queue of generic keys.
* It supports the usual insert and delete-the-minimum operations,
* along with the merging of two heaps together.
* It also supports methods for peeking at the minimum key,
* testing if the priority queue is empty, and iterating through
* the keys.
* It is possible to build the priority queue using a Comparator.
* If not, the natural order relation between the keys will be used.
*
* This implementation uses a binomial heap.
* The insert, delete-the-minimum, union, min-key
* and size operations take logarithmic time.
* The is-empty and constructor operations take constant time.
*
* @author Tristan Claverie
*/
public class BinomialMinPQ<Key> implements Iterable<Key> {
private Node head; //head of the list of roots
private final Comparator<Key> comp; //Comparator over the keys
//Represents a Node of a Binomial Tree
private class Node {
Key key; //Key contained by the Node
int order; //The order of the Binomial Tree rooted by this Node
Node child, sibling; //child and sibling of this Node
}
/**
* Initializes an empty priority queue
* Worst case is O(1)
*/
public BinomialMinPQ() {
comp = new MyComparator();
}
/**
* Initializes an empty priority queue using the given Comparator
* Worst case is O(1)
* @param C a comparator over the keys
*/
public BinomialMinPQ(Comparator<Key> C) {
comp = C;
}
/**
* Initializes a priority queue with given keys
* Worst case is O(n*log(n))
* @param a an array of keys
*/
public BinomialMinPQ(Key[] a) {
comp = new MyComparator();
for (Key k : a) insert(k);
}
/**
* Initializes a priority queue with given keys using the given Comparator
* Worst case is O(n*log(n))
* @param C a comparator over the keys
* @param a an array of keys
*/
public BinomialMinPQ(Comparator<Key> C, Key[] a) {
comp = C;
for (Key k : a) insert(k);
}
/**
* Whether the priority queue is empty
* Worst case is O(1)
* @return true if the priority queue is empty, false if not
*/
public boolean isEmpty() {
return head == null;
}
/**
* Number of elements currently on the priority queue
* Worst case is O(log(n))
* @throws java.lang.ArithmeticException if there are more than 2^63-1 elements in the queue
* @return the number of elements on the priority queue
*/
public int size() {
int result = 0, tmp;
for (Node node = head; node != null; node = node.sibling) {
if (node.order > 30) { throw new ArithmeticException("The number of elements cannot be evaluated, but the priority queue is still valid."); }
tmp = 1 << node.order;
result |= tmp;
}
return result;
}
/**
* Puts a Key in the heap
* Worst case is O(log(n))
* @param key a Key
*/
public void insert(Key key) {
Node x = new Node();
x.key = key;
x.order = 0;
BinomialMinPQ<Key> H = new BinomialMinPQ<Key>(); //The Comparator oh the H heap is not used
H.head = x;
this.head = this.union(H).head;
}
/**
* Get the minimum key currently in the queue
* Worst case is O(log(n))
* @throws java.util.NoSuchElementException if the priority queue is empty
* @return the minimum key currently in the priority queue
*/
public Key minKey() {
if (isEmpty()) throw new NoSuchElementException("Priority queue is empty");
Node min = head;
Node current = head;
while (current.sibling != null) {
min = (greater(min.key, current.sibling.key)) ? current : min;
current = current.sibling;
}
return min.key;
}
/**
* Deletes the minimum key
* Worst case is O(log(n))
* @throws java.util.NoSuchElementException if the priority queue is empty
* @return the minimum key
*/
public Key delMin() {
if(isEmpty()) throw new NoSuchElementException("Priority queue is empty");
Node min = eraseMin();
Node x = (min.child == null) ? min : min.child;
if (min.child != null) {
min.child = null;
Node prevx = null, nextx = x.sibling;
while (nextx != null) {
x.sibling = prevx;
prevx = x;
x = nextx;nextx = nextx.sibling;
}
x.sibling = prevx;
BinomialMinPQ<Key> H = new BinomialMinPQ<Key>();
H.head = x;
head = union(H).head;
}
return min.key;
}
/**
* Merges two Binomial heaps together
* This operation is destructive
* Worst case is O(log(n))
* @param heap a Binomial Heap to be merged with the current heap
* @throws java.lang.IllegalArgumentException if the heap in parameter is null
* @return the union of two heaps
*/
public BinomialMinPQ<Key> union(BinomialMinPQ<Key> heap) {
if (heap == null) throw new IllegalArgumentException("Cannot merge a Binomial Heap with null");
this.head = merge(new Node(), this.head, heap.head).sibling;
Node x = this.head;
Node prevx = null, nextx = x.sibling;
while (nextx != null) {
if (x.order < nextx.order ||
(nextx.sibling != null && nextx.sibling.order == x.order)) {
prevx = x; x = nextx;
} else if (greater(nextx.key, x.key)) {
x.sibling = nextx.sibling;
link(nextx, x);
} else {
if (prevx == null) { this.head = nextx; }
else { prevx.sibling = nextx; }
link(x, nextx);
x = nextx;
}
nextx = x.sibling;
}
return this;
}
/*************************************************
* General helper functions
************************************************/
//Compares two keys
private boolean greater(Key n, Key m) {
if (n == null) return false;
if (m == null) return true;
return comp.compare(n, m) > 0;
}
//Assuming root1 holds a greater key than root2, root2 becomes the new root
private void link(Node root1, Node root2) {
root1.sibling = root2.child;
root2.child = root1;
root2.order++;
}
//Deletes and return the node containing the minimum key
private Node eraseMin() {
Node min = head;
Node previous = null;
Node current = head;
while (current.sibling != null) {
if (greater(min.key, current.sibling.key)) {
previous = current;
min = current.sibling;
}
current = current.sibling;
}
previous.sibling = min.sibling;
if (min == head) head = min.sibling;
return min;
}
/**************************************************
* Functions for inserting a key in the heap
*************************************************/
//Merges two root lists into one, there can be up to 2 Binomial Trees of same order
private Node merge(Node h, Node x, Node y) {
if (x == null && y == null) return h;
else if (x == null) h.sibling = merge(y, null, y.sibling);
else if (y == null) h.sibling = merge(x, x.sibling, null);
else if (x.order < y.order) h.sibling = merge(x, x.sibling, y);
else h.sibling = merge(y, x, y.sibling);
return h;
}
/******************************************************************
* Iterator
*****************************************************************/
/**
* Gets an Iterator over the keys in the priority queue in ascending order
* The Iterator does not implement the remove() method
* iterator() : Worst case is O(n)
* next() : Worst case is O(log(n))
* hasNext() : Worst case is O(1)
* @return an Iterator over the keys in the priority queue in ascending order
*/
public Iterator<Key> iterator() {
return new MyIterator();
}
private class MyIterator implements Iterator<Key> {
BinomialMinPQ<Key> data;
//Constructor clones recursively the elements in the queue
//It takes linear time
public MyIterator() {
data = new BinomialMinPQ<Key>(comp);
data.head = clone(head, false, false, null);
}
private Node clone(Node x, boolean isParent, boolean isChild, Node parent) {
if (x == null) return null;
Node node = new Node();
node.key = x.key;
node.sibling = clone(x.sibling, false, false, parent);
node.child = clone(x.child, false, true, node);
return node;
}
public boolean hasNext() {
return !data.isEmpty();
}
public Key next() {
if (!hasNext()) throw new NoSuchElementException();
return data.delMin();
}
public void remove() {
throw new UnsupportedOperationException();
}
}
/***************************
* Comparator
**************************/
//default Comparator
private class MyComparator implements Comparator<Key> {
@Override
public int compare(Key key1, Key key2) {
return ((Comparable<Key>) key1).compareTo(key2);
}
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac Bipartite.java
* Execution: java Bipartite V E F
* Dependencies: Graph.java
* Data files: http://algs4.cs.princeton.edu/41graph/tinyG.txt
* http://algs4.cs.princeton.edu/41graph/mediumG.txt
* http://algs4.cs.princeton.edu/41graph/largeG.txt
*
* Given a graph, find either (i) a bipartition or (ii) an odd-length cycle.
* Runs in O(E + V) time.
*
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code Bipartite} class represents a data type for
* determining whether an undirected graph is bipartite or whether
* it has an odd-length cycle.
* The <em>isBipartite</em> operation determines whether the graph is
* bipartite. If so, the <em>color</em> operation determines a
* bipartition; if not, the <em>oddCycle</em> operation determines a
* cycle with an odd number of edges.
* <p>
* This implementation uses depth-first search.
* The constructor takes time proportional to <em>V</em> + <em>E</em>
* (in the worst case),
* where <em>V</em> is the number of vertices and <em>E</em> is the number of edges.
* Afterwards, the <em>isBipartite</em> and <em>color</em> operations
* take constant time; the <em>oddCycle</em> operation takes time proportional
* to the length of the cycle.
* See {@link BipartiteX} for a nonrecursive version that uses breadth-first
* search.
* <p>
* For additional documentation, see <a href="http://algs4.cs.princeton.edu/41graph">Section 4.1</a>
* of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class Bipartite {
private boolean isBipartite; // is the graph bipartite?
private boolean[] color; // color[v] gives vertices on one side of bipartition
private boolean[] marked; // marked[v] = true if v has been visited in DFS
private int[] edgeTo; // edgeTo[v] = last edge on path to v
private Stack<Integer> cycle; // odd-length cycle
/**
* Determines whether an undirected graph is bipartite and finds either a
* bipartition or an odd-length cycle.
*
* @param G the graph
*/
public Bipartite(Graph G) {
isBipartite = true;
color = new boolean[G.V()];
marked = new boolean[G.V()];
edgeTo = new int[G.V()];
for (int v = 0; v < G.V(); v++) {
if (!marked[v]) {
dfs(G, v);
}
}
assert check(G);
}
private void dfs(Graph G, int v) {
marked[v] = true;
for (int w : G.adj(v)) {
// short circuit if odd-length cycle found
if (cycle != null) return;
// found uncolored vertex, so recur
if (!marked[w]) {
edgeTo[w] = v;
color[w] = !color[v];
dfs(G, w);
}
// if v-w create an odd-length cycle, find it
else if (color[w] == color[v]) {
isBipartite = false;
cycle = new Stack<Integer>();
cycle.push(w); // don't need this unless you want to include start vertex twice
for (int x = v; x != w; x = edgeTo[x]) {
cycle.push(x);
}
cycle.push(w);
}
}
}
/**
* Returns true if the graph is bipartite.
*
* @return {@code true} if the graph is bipartite; {@code false} otherwise
*/
public boolean isBipartite() {
return isBipartite;
}
/**
* Returns the side of the bipartite that vertex {@code v} is on.
*
* @param v the vertex
* @return the side of the bipartition that vertex {@code v} is on; two vertices
* are in the same side of the bipartition if and only if they have the
* same color
* @throws IllegalArgumentException unless {@code 0 <= v < V}
* @throws UnsupportedOperationException if this method is called when the graph
* is not bipartite
*/
public boolean color(int v) {
validateVertex(v);
if (!isBipartite)
throw new UnsupportedOperationException("graph is not bipartite");
return color[v];
}
/**
* Returns an odd-length cycle if the graph is not bipartite, and
* {@code null} otherwise.
*
* @return an odd-length cycle if the graph is not bipartite
* (and hence has an odd-length cycle), and {@code null}
* otherwise
*/
public Iterable<Integer> oddCycle() {
return cycle;
}
private boolean check(Graph G) {
// graph is bipartite
if (isBipartite) {
for (int v = 0; v < G.V(); v++) {
for (int w : G.adj(v)) {
if (color[v] == color[w]) {
System.err.printf("edge %d-%d with %d and %d in same side of bipartition\n", v, w, v, w);
return false;
}
}
}
}
// graph has an odd-length cycle
else {
// verify cycle
int first = -1, last = -1;
for (int v : oddCycle()) {
if (first == -1) first = v;
last = v;
}
if (first != last) {
System.err.printf("cycle begins with %d and ends with %d\n", first, last);
return false;
}
}
return true;
}
// throw an IllegalArgumentException unless {@code 0 <= v < V}
private void validateVertex(int v) {
int V = marked.length;
if (v < 0 || v >= V)
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V-1));
}
/**
* Unit tests the {@code Bipartite} data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
int V1 = Integer.parseInt(args[0]);
int V2 = Integer.parseInt(args[1]);
int E = Integer.parseInt(args[2]);
int F = Integer.parseInt(args[3]);
// create random bipartite graph with V1 vertices on left side,
// V2 vertices on right side, and E edges; then add F random edges
Graph G = GraphGenerator.bipartite(V1, V2, E);
for (int i = 0; i < F; i++) {
int v = StdRandom.uniform(V1 + V2);
int w = StdRandom.uniform(V1 + V2);
G.addEdge(v, w);
}
StdOut.println(G);
Bipartite b = new Bipartite(G);
if (b.isBipartite()) {
StdOut.println("Graph is bipartite");
for (int v = 0; v < G.V(); v++) {
StdOut.println(v + ": " + b.color(v));
}
}
else {
StdOut.print("Graph has an odd-length cycle: ");
for (int x : b.oddCycle()) {
StdOut.print(x + " ");
}
StdOut.println();
}
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac BipartiteX.java
* Execution: java Bipartite V E F
* Dependencies: Graph.java
*
* Given a graph, find either (i) a bipartition or (ii) an odd-length cycle.
* Runs in O(E + V) time.
*
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code BipartiteX} class represents a data type for
* determining whether an undirected graph is bipartite or whether
* it has an odd-length cycle.
* The <em>isBipartite</em> operation determines whether the graph is
* bipartite. If so, the <em>color</em> operation determines a
* bipartition; if not, the <em>oddCycle</em> operation determines a
* cycle with an odd number of edges.
* <p>
* This implementation uses breadth-first search and is nonrecursive.
* The constructor takes time proportional to <em>V</em> + <em>E</em>
* (in the worst case),
* where <em>V</em> is the number of vertices and <em>E</em> is the number of edges.
* Afterwards, the <em>isBipartite</em> and <em>color</em> operations
* take constant time; the <em>oddCycle</em> operation takes time proportional
* to the length of the cycle.
* See {@link Bipartite} for a recursive version that uses depth-first search.
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/41graph">Section 4.1</a>
* of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class BipartiteX {
private static final boolean WHITE = false;
private static final boolean BLACK = true;
private boolean isBipartite; // is the graph bipartite?
private boolean[] color; // color[v] gives vertices on one side of bipartition
private boolean[] marked; // marked[v] = true if v has been visited in DFS
private int[] edgeTo; // edgeTo[v] = last edge on path to v
private Queue<Integer> cycle; // odd-length cycle
/**
* Determines whether an undirected graph is bipartite and finds either a
* bipartition or an odd-length cycle.
*
* @param G the graph
*/
public BipartiteX(Graph G) {
isBipartite = true;
color = new boolean[G.V()];
marked = new boolean[G.V()];
edgeTo = new int[G.V()];
for (int v = 0; v < G.V() && isBipartite; v++) {
if (!marked[v]) {
bfs(G, v);
}
}
assert check(G);
}
private void bfs(Graph G, int s) {
Queue<Integer> q = new Queue<Integer>();
color[s] = WHITE;
marked[s] = true;
q.enqueue(s);
while (!q.isEmpty()) {
int v = q.dequeue();
for (int w : G.adj(v)) {
if (!marked[w]) {
marked[w] = true;
edgeTo[w] = v;
color[w] = !color[v];
q.enqueue(w);
}
else if (color[w] == color[v]) {
isBipartite = false;
// to form odd cycle, consider s-v path and s-w path
// and let x be closest node to v and w common to two paths
// then (w-x path) + (x-v path) + (edge v-w) is an odd-length cycle
// Note: distTo[v] == distTo[w];
cycle = new Queue<Integer>();
Stack<Integer> stack = new Stack<Integer>();
int x = v, y = w;
while (x != y) {
stack.push(x);
cycle.enqueue(y);
x = edgeTo[x];
y = edgeTo[y];
}
stack.push(x);
while (!stack.isEmpty())
cycle.enqueue(stack.pop());
cycle.enqueue(w);
return;
}
}
}
}
/**
* Returns true if the graph is bipartite.
*
* @return {@code true} if the graph is bipartite; {@code false} otherwise
*/
public boolean isBipartite() {
return isBipartite;
}
/**
* Returns the side of the bipartite that vertex {@code v} is on.
*
* @param v the vertex
* @return the side of the bipartition that vertex {@code v} is on; two vertices
* are in the same side of the bipartition if and only if they have the
* same color
* @throws IllegalArgumentException unless {@code 0 <= v < V}
* @throws UnsupportedOperationException if this method is called when the graph
* is not bipartite
*/
public boolean color(int v) {
validateVertex(v);
if (!isBipartite)
throw new UnsupportedOperationException("Graph is not bipartite");
return color[v];
}
/**
* Returns an odd-length cycle if the graph is not bipartite, and
* {@code null} otherwise.
*
* @return an odd-length cycle if the graph is not bipartite
* (and hence has an odd-length cycle), and {@code null}
* otherwise
*/
public Iterable<Integer> oddCycle() {
return cycle;
}
private boolean check(Graph G) {
// graph is bipartite
if (isBipartite) {
for (int v = 0; v < G.V(); v++) {
for (int w : G.adj(v)) {
if (color[v] == color[w]) {
System.err.printf("edge %d-%d with %d and %d in same side of bipartition\n", v, w, v, w);
return false;
}
}
}
}
// graph has an odd-length cycle
else {
// verify cycle
int first = -1, last = -1;
for (int v : oddCycle()) {
if (first == -1) first = v;
last = v;
}
if (first != last) {
System.err.printf("cycle begins with %d and ends with %d\n", first, last);
return false;
}
}
return true;
}
// throw an IllegalArgumentException unless {@code 0 <= v < V}
private void validateVertex(int v) {
int V = marked.length;
if (v < 0 || v >= V)
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V-1));
}
/**
* Unit tests the {@code BipartiteX} data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
int V1 = Integer.parseInt(args[0]);
int V2 = Integer.parseInt(args[1]);
int E = Integer.parseInt(args[2]);
int F = Integer.parseInt(args[3]);
// create random bipartite graph with V1 vertices on left side,
// V2 vertices on right side, and E edges; then add F random edges
Graph G = GraphGenerator.bipartite(V1, V2, E);
for (int i = 0; i < F; i++) {
int v = StdRandom.uniform(V1 + V2);
int w = StdRandom.uniform(V1 + V2);
G.addEdge(v, w);
}
StdOut.println(G);
BipartiteX b = new BipartiteX(G);
if (b.isBipartite()) {
StdOut.println("Graph is bipartite");
for (int v = 0; v < G.V(); v++) {
StdOut.println(v + ": " + b.color(v));
}
}
else {
StdOut.print("Graph has an odd-length cycle: ");
for (int x : b.oddCycle()) {
StdOut.print(x + " ");
}
StdOut.println();
}
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac BlackFilter.java
* Execution: java BlackFilter blacklist.txt < input.txt
* Dependencies: SET In.java StdIn.java StdOut.java
* Data files: http://algs4.cs.princeton.edu/35applications/tinyTale.txt
* http://algs4.cs.princeton.edu/35applications/list.txt
*
* Read in a blacklist of words from a file. Then read in a list of
* words from standard input and print out all those words that
* are not in the first file.
*
* % more tinyTale.txt
* it was the best of times it was the worst of times
* it was the age of wisdom it was the age of foolishness
* it was the epoch of belief it was the epoch of incredulity
* it was the season of light it was the season of darkness
* it was the spring of hope it was the winter of despair
*
* % more list.txt
* was it the of
*
* % java BlackFilter list.txt < tinyTale.txt
* best times worst times
* age wisdom age foolishness
* epoch belief epoch incredulity
* season light season darkness
* spring hope winter despair
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code BlackFilter} class provides a client for reading in a <em>blacklist</em>
* of words from a file; then, reading in a sequence of words from standard input,
* printing out each word that <em>does not</em> appear in the file.
* It is useful as a test client for various symbol table implementations.
* <p>
* For additional documentation, see <a href="http://algs4.cs.princeton.edu/35applications">Section 3.5</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class BlackFilter {
// Do not instantiate.
private BlackFilter() { }
public static void main(String[] args) {
SET<String> set = new SET<String>();
// read in strings and add to set
In in = new In(args[0]);
while (!in.isEmpty()) {
String word = in.readString();
set.add(word);
}
// read in string from standard input, printing out all exceptions
while (!StdIn.isEmpty()) {
String word = StdIn.readString();
if (!set.contains(word))
StdOut.println(word);
}
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac BoruvkaMST.java
* Execution: java BoruvkaMST filename.txt
* Dependencies: EdgeWeightedGraph.java Edge.java Bag.java
* UF.java In.java StdOut.java
* Data files: http://algs4.cs.princeton.edu/43mst/tinyEWG.txt
* http://algs4.cs.princeton.edu/43mst/mediumEWG.txt
* http://algs4.cs.princeton.edu/43mst/largeEWG.txt
*
* Compute a minimum spanning forest using Boruvka's algorithm.
*
* % java BoruvkaMST tinyEWG.txt
* 0-2 0.26000
* 6-2 0.40000
* 5-7 0.28000
* 4-5 0.35000
* 2-3 0.17000
* 1-7 0.19000
* 0-7 0.16000
* 1.81000
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code BoruvkaMST} class represents a data type for computing a
* <em>minimum spanning tree</em> in an edge-weighted graph.
* The edge weights can be positive, zero, or negative and need not
* be distinct. If the graph is not connected, it computes a <em>minimum
* spanning forest</em>, which is the union of minimum spanning trees
* in each connected component. The {@code weight()} method returns the
* weight of a minimum spanning tree and the {@code edges()} method
* returns its edges.
* <p>
* This implementation uses <em>Boruvka's algorithm</em> and the union-find
* data type.
* The constructor takes time proportional to <em>E</em> log <em>V</em>
* and extra space (not including the graph) proportional to <em>V</em>,
* where <em>V</em> is the number of vertices and <em>E</em> is the number of edges.
* Afterwards, the {@code weight()} method takes constant time
* and the {@code edges()} method takes time proportional to <em>V</em>.
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/43mst">Section 4.3</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
* For alternate implementations, see {@link LazyPrimMST}, {@link PrimMST},
* and {@link KruskalMST}.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class BoruvkaMST {
private static final double FLOATING_POINT_EPSILON = 1E-12;
private Bag<Edge> mst = new Bag<Edge>(); // edges in MST
private double weight; // weight of MST
/**
* Compute a minimum spanning tree (or forest) of an edge-weighted graph.
* @param G the edge-weighted graph
*/
public BoruvkaMST(EdgeWeightedGraph G) {
UF uf = new UF(G.V());
// repeat at most log V times or until we have V-1 edges
for (int t = 1; t < G.V() && mst.size() < G.V() - 1; t = t + t) {
// foreach tree in forest, find closest edge
// if edge weights are equal, ties are broken in favor of first edge in G.edges()
Edge[] closest = new Edge[G.V()];
for (Edge e : G.edges()) {
int v = e.either(), w = e.other(v);
int i = uf.find(v), j = uf.find(w);
if (i == j) continue; // same tree
if (closest[i] == null || less(e, closest[i])) closest[i] = e;
if (closest[j] == null || less(e, closest[j])) closest[j] = e;
}
// add newly discovered edges to MST
for (int i = 0; i < G.V(); i++) {
Edge e = closest[i];
if (e != null) {
int v = e.either(), w = e.other(v);
// don't add the same edge twice
if (!uf.connected(v, w)) {
mst.add(e);
weight += e.weight();
uf.union(v, w);
}
}
}
}
// check optimality conditions
assert check(G);
}
/**
* 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).
* @return the sum of the edge weights in a minimum spanning tree (or forest)
*/
public double weight() {
return weight;
}
// is the weight of edge e strictly less than that of edge f?
private static boolean less(Edge e, Edge f) {
return e.weight() < f.weight();
}
// check optimality conditions (takes time proportional to E V lg* V)
private boolean check(EdgeWeightedGraph G) {
// check weight
double totalWeight = 0.0;
for (Edge e : edges()) {
totalWeight += e.weight();
}
if (Math.abs(totalWeight - weight()) > FLOATING_POINT_EPSILON) {
System.err.printf("Weight of edges does not equal weight(): %f vs. %f\n", totalWeight, weight());
return false;
}
// check that it is acyclic
UF uf = new UF(G.V());
for (Edge e : edges()) {
int v = e.either(), w = e.other(v);
if (uf.connected(v, w)) {
System.err.println("Not a forest");
return false;
}
uf.union(v, w);
}
// check that it is a spanning forest
for (Edge e : G.edges()) {
int v = e.either(), w = e.other(v);
if (!uf.connected(v, w)) {
System.err.println("Not a spanning forest");
return false;
}
}
// check that it is a minimal spanning forest (cut optimality conditions)
for (Edge e : edges()) {
// all edges in MST except e
uf = new UF(G.V());
for (Edge f : mst) {
int x = f.either(), y = f.other(x);
if (f != e) uf.union(x, y);
}
// check that e is min weight edge in crossing cut
for (Edge f : G.edges()) {
int x = f.either(), y = f.other(x);
if (!uf.connected(x, y)) {
if (f.weight() < e.weight()) {
System.err.println("Edge " + f + " violates cut optimality conditions");
return false;
}
}
}
}
return true;
}
/**
* Unit tests the {@code BoruvkaMST} data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
In in = new In(args[0]);
EdgeWeightedGraph G = new EdgeWeightedGraph(in);
BoruvkaMST mst = new BoruvkaMST(G);
for (Edge e : mst.edges()) {
StdOut.println(e);
}
StdOut.printf("%.5f\n", mst.weight());
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac BoyerMoore.java
* Execution: java BoyerMoore pattern text
* Dependencies: StdOut.java
*
* Reads in two strings, the pattern and the input text, and
* searches for the pattern in the input text using the
* bad-character rule part of the Boyer-Moore algorithm.
* (does not implement the strong good suffix rule)
*
* % java BoyerMoore abracadabra abacadabrabracabracadabrabrabracad
* text: abacadabrabracabracadabrabrabracad
* pattern: abracadabra
*
* % java BoyerMoore rab abacadabrabracabracadabrabrabracad
* text: abacadabrabracabracadabrabrabracad
* pattern: rab
*
* % java BoyerMoore bcara abacadabrabracabracadabrabrabracad
* text: abacadabrabracabracadabrabrabracad
* pattern: bcara
*
* % java BoyerMoore rabrabracad abacadabrabracabracadabrabrabracad
* text: abacadabrabracabracadabrabrabracad
* pattern: rabrabracad
*
* % java BoyerMoore abacad abacadabrabracabracadabrabrabracad
* text: abacadabrabracabracadabrabrabracad
* pattern: abacad
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code BoyerMoore} class finds the first occurrence of a pattern string
* in a text string.
* <p>
* This implementation uses the Boyer-Moore algorithm (with the bad-character
* rule, but not the strong good suffix rule).
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/53substring">Section 5.3</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*/
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
/**
* Preprocesses the pattern string.
*
* @param pat the pattern string
*/
public BoyerMoore(String pat) {
this.R = 256;
this.pat = pat;
// position of rightmost occurrence of c in the pattern
right = new int[R];
for (int c = 0; c < R; c++)
right[c] = -1;
for (int j = 0; j < pat.length(); j++)
right[pat.charAt(j)] = j;
}
/**
* Preprocesses the pattern string.
*
* @param pattern the pattern string
* @param R the alphabet size
*/
public BoyerMoore(char[] pattern, int R) {
this.R = R;
this.pattern = new char[pattern.length];
for (int j = 0; j < pattern.length; j++)
this.pattern[j] = pattern[j];
// position of rightmost occurrence of c in the pattern
right = new int[R];
for (int c = 0; c < R; c++)
right[c] = -1;
for (int j = 0; j < pattern.length; j++)
right[pattern[j]] = j;
}
/**
* Returns the index of the first occurrrence of the pattern string
* in the text string.
*
* @param txt the text string
* @return the index of the first occurrence of the pattern string
* in the text string; n if no such match
*/
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--) {
if (pat.charAt(j) != txt.charAt(i+j)) {
skip = Math.max(1, j - right[txt.charAt(i+j)]);
break;
}
}
if (skip == 0) return i; // found
}
return n; // not found
}
/**
* Returns the index of the first occurrrence of the pattern string
* in the text string.
*
* @param text the text string
* @return the index of the first occurrence of the pattern string
* in the text string; n if no such match
*/
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--) {
if (pattern[j] != text[i+j]) {
skip = Math.max(1, j - right[text[i+j]]);
break;
}
}
if (skip == 0) return i; // found
}
return n; // not found
}
/**
* Takes a pattern string and an input string as command-line arguments;
* searches for the pattern string in the text string; and prints
* the first occurrence of the pattern string in the text string.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
String pat = args[0];
String txt = args[1];
char[] pattern = pat.toCharArray();
char[] text = txt.toCharArray();
BoyerMoore boyermoore1 = new BoyerMoore(pat);
BoyerMoore boyermoore2 = new BoyerMoore(pattern, 256);
int offset1 = boyermoore1.search(txt);
int offset2 = boyermoore2.search(text);
// print results
StdOut.println("text: " + txt);
StdOut.print("pattern: ");
for (int i = 0; i < offset1; i++)
StdOut.print(" ");
StdOut.println(pat);
StdOut.print("pattern: ");
for (int i = 0; i < offset2; i++)
StdOut.print(" ");
StdOut.println(pat);
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac BreadthFirstDirectedPaths.java
* Execution: java BreadthFirstDirectedPaths digraph.txt s
* Dependencies: Digraph.java Queue.java Stack.java
* Data files: http://algs4.cs.princeton.edu/42digraph/tinyDG.txt
* http://algs4.cs.princeton.edu/42digraph/mediumDG.txt
* http://algs4.cs.princeton.edu/42digraph/largeDG.txt
*
* Run breadth-first search on a digraph.
* Runs in O(E + V) time.
*
* % java BreadthFirstDirectedPaths tinyDG.txt 3
* 3 to 0 (2): 3->2->0
* 3 to 1 (3): 3->2->0->1
* 3 to 2 (1): 3->2
* 3 to 3 (0): 3
* 3 to 4 (2): 3->5->4
* 3 to 5 (1): 3->5
* 3 to 6 (-): not connected
* 3 to 7 (-): not connected
* 3 to 8 (-): not connected
* 3 to 9 (-): not connected
* 3 to 10 (-): not connected
* 3 to 11 (-): not connected
* 3 to 12 (-): not connected
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code BreadthDirectedFirstPaths} class represents a data type for finding
* shortest paths (number of edges) from a source vertex <em>s</em>
* (or set of source vertices) to every other vertex in the digraph.
* <p>
* This implementation uses breadth-first search.
* The constructor takes time proportional to <em>V</em> + <em>E</em>,
* where <em>V</em> is the number of vertices and <em>E</em> is the number of edges.
* It uses extra space (not including the digraph) proportional to <em>V</em>.
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/42digraph">Section 4.2</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class BreadthFirstDirectedPaths {
private static final int INFINITY = Integer.MAX_VALUE;
private boolean[] marked; // marked[v] = is there an s->v path?
private int[] edgeTo; // edgeTo[v] = last edge on shortest s->v path
private int[] distTo; // distTo[v] = length of shortest s->v path
/**
* Computes the shortest path from {@code s} and every other vertex in graph {@code G}.
* @param G the digraph
* @param s the source vertex
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public BreadthFirstDirectedPaths(Digraph G, int s) {
marked = new boolean[G.V()];
distTo = new int[G.V()];
edgeTo = new int[G.V()];
for (int v = 0; v < G.V(); v++)
distTo[v] = INFINITY;
validateVertex(s);
bfs(G, s);
}
/**
* Computes the shortest path from any one of the source vertices in {@code sources}
* to every other vertex in graph {@code G}.
* @param G the digraph
* @param sources the source vertices
* @throws IllegalArgumentException unless each vertex {@code v} in
* {@code sources} satisfies {@code 0 <= v < V}
*/
public BreadthFirstDirectedPaths(Digraph G, Iterable<Integer> sources) {
marked = new boolean[G.V()];
distTo = new int[G.V()];
edgeTo = new int[G.V()];
for (int v = 0; v < G.V(); v++)
distTo[v] = INFINITY;
validateVertices(sources);
bfs(G, sources);
}
// BFS from single source
private void bfs(Digraph G, int s) {
Queue<Integer> q = new Queue<Integer>();
marked[s] = true;
distTo[s] = 0;
q.enqueue(s);
while (!q.isEmpty()) {
int v = q.dequeue();
for (int w : G.adj(v)) {
if (!marked[w]) {
edgeTo[w] = v;
distTo[w] = distTo[v] + 1;
marked[w] = true;
q.enqueue(w);
}
}
}
}
// BFS from multiple sources
private void bfs(Digraph G, Iterable<Integer> sources) {
Queue<Integer> q = new Queue<Integer>();
for (int s : sources) {
marked[s] = true;
distTo[s] = 0;
q.enqueue(s);
}
while (!q.isEmpty()) {
int v = q.dequeue();
for (int w : G.adj(v)) {
if (!marked[w]) {
edgeTo[w] = v;
distTo[w] = distTo[v] + 1;
marked[w] = true;
q.enqueue(w);
}
}
}
}
/**
* Is there a directed path from the source {@code s} (or sources) to vertex {@code v}?
* @param v the vertex
* @return {@code true} if there is a directed path, {@code false} otherwise
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public boolean hasPathTo(int v) {
validateVertex(v);
return marked[v];
}
/**
* Returns the number of edges in a shortest path from the source {@code s}
* (or sources) to vertex {@code v}?
* @param v the vertex
* @return the number of edges in a shortest path
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public int distTo(int v) {
validateVertex(v);
return distTo[v];
}
/**
* Returns a shortest path from {@code s} (or sources) to {@code v}, or
* {@code null} if no such path.
* @param v the vertex
* @return the sequence of vertices on a shortest path, as an Iterable
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public Iterable<Integer> pathTo(int v) {
validateVertex(v);
if (!hasPathTo(v)) return null;
Stack<Integer> path = new Stack<Integer>();
int x;
for (x = v; distTo[x] != 0; x = edgeTo[x])
path.push(x);
path.push(x);
return path;
}
// throw an IllegalArgumentException unless {@code 0 <= v < V}
private void validateVertex(int v) {
int V = marked.length;
if (v < 0 || v >= V)
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V-1));
}
// throw an IllegalArgumentException unless {@code 0 <= v < V}
private void validateVertices(Iterable<Integer> vertices) {
if (vertices == null) {
throw new IllegalArgumentException("argument is null");
}
int V = marked.length;
for (int v : vertices) {
if (v < 0 || v >= V) {
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V-1));
}
}
}
/**
* Unit tests the {@code BreadthFirstDirectedPaths} data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
In in = new In(args[0]);
Digraph G = new Digraph(in);
// StdOut.println(G);
int s = Integer.parseInt(args[1]);
BreadthFirstDirectedPaths bfs = new BreadthFirstDirectedPaths(G, s);
for (int v = 0; v < G.V(); v++) {
if (bfs.hasPathTo(v)) {
StdOut.printf("%d to %d (%d): ", s, v, bfs.distTo(v));
for (int x : bfs.pathTo(v)) {
if (x == s) StdOut.print(x);
else StdOut.print("->" + x);
}
StdOut.println();
}
else {
StdOut.printf("%d to %d (-): not connected\n", s, v);
}
}
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac CC.java
* Execution: java CC filename.txt
* Dependencies: Graph.java StdOut.java Queue.java
* Data files: http://algs4.cs.princeton.edu/41graph/tinyG.txt
* http://algs4.cs.princeton.edu/41graph/mediumG.txt
* http://algs4.cs.princeton.edu/41graph/largeG.txt
*
* Compute connected components using depth first search.
* Runs in O(E + V) time.
*
* % java CC tinyG.txt
* 3 components
* 0 1 2 3 4 5 6
* 7 8
* 9 10 11 12
*
* % java CC mediumG.txt
* 1 components
* 0 1 2 3 4 5 6 7 8 9 10 ...
*
* % java -Xss50m CC largeG.txt
* 1 components
* 0 1 2 3 4 5 6 7 8 9 10 ...
*
* Note: This implementation uses a recursive DFS. To avoid needing
* a potentially very large stack size, replace with a non-recurisve
* DFS ala NonrecursiveDFS.java.
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code CC} class represents a data type for
* determining the connected components in an undirected graph.
* The <em>id</em> operation determines in which connected component
* a given vertex lies; the <em>connected</em> operation
* determines whether two vertices are in the same connected component;
* the <em>count</em> operation determines the number of connected
* components; and the <em>size</em> operation determines the number
* of vertices in the connect component containing a given vertex.
* The <em>component identifier</em> of a connected component is one of the
* vertices in the connected component: two vertices have the same component
* identifier if and only if they are in the same connected component.
* <p>
* This implementation uses depth-first search.
* The constructor takes time proportional to <em>V</em> + <em>E</em>
* (in the worst case),
* where <em>V</em> is the number of vertices and <em>E</em> is the number of edges.
* Afterwards, the <em>id</em>, <em>count</em>, <em>connected</em>,
* and <em>size</em> operations take constant time.
* <p>
* For additional documentation, see <a href="http://algs4.cs.princeton.edu/41graph">Section 4.1</a>
* of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class CC {
private boolean[] marked; // marked[v] = has vertex v been marked?
private int[] id; // id[v] = id of connected component containing v
private int[] size; // size[id] = number of vertices in given component
private int count; // number of connected components
/**
* Computes the connected components of the undirected graph {@code G}.
*
* @param G the undirected graph
*/
public CC(Graph G) {
marked = new boolean[G.V()];
id = new int[G.V()];
size = new int[G.V()];
for (int v = 0; v < G.V(); v++) {
if (!marked[v]) {
dfs(G, v);
count++;
}
}
}
/**
* Computes the connected components of the edge-weighted graph {@code G}.
*
* @param G the edge-weighted graph
*/
public CC(EdgeWeightedGraph G) {
marked = new boolean[G.V()];
id = new int[G.V()];
size = new int[G.V()];
for (int v = 0; v < G.V(); v++) {
if (!marked[v]) {
dfs(G, v);
count++;
}
}
}
// depth-first search for a Graph
private void dfs(Graph G, int v) {
marked[v] = true;
id[v] = count;
size[count]++;
for (int w : G.adj(v)) {
if (!marked[w]) {
dfs(G, w);
}
}
}
// depth-first search for an EdgeWeightedGraph
private void dfs(EdgeWeightedGraph G, int v) {
marked[v] = true;
id[v] = count;
size[count]++;
for (Edge e : G.adj(v)) {
int w = e.other(v);
if (!marked[w]) {
dfs(G, w);
}
}
}
/**
* Returns the component id of the connected component containing vertex {@code v}.
*
* @param v the vertex
* @return the component id of the connected component containing vertex {@code v}
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public int id(int v) {
validateVertex(v);
return id[v];
}
/**
* Returns the number of vertices in the connected component containing vertex {@code v}.
*
* @param v the vertex
* @return the number of vertices in the connected component containing vertex {@code v}
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public int size(int v) {
validateVertex(v);
return size[id[v]];
}
/**
* Returns the number of connected components in the graph {@code G}.
*
* @return the number of connected components in the graph {@code G}
*/
public int count() {
return count;
}
/**
* Returns true if vertices {@code v} and {@code w} are in the same
* connected component.
*
* @param v one vertex
* @param w the other vertex
* @return {@code true} if vertices {@code v} and {@code w} are in the same
* connected component; {@code false} otherwise
* @throws IllegalArgumentException unless {@code 0 <= v < V}
* @throws IllegalArgumentException unless {@code 0 <= w < V}
*/
public boolean connected(int v, int w) {
validateVertex(v);
validateVertex(w);
return id(v) == id(w);
}
/**
* Returns true if vertices {@code v} and {@code w} are in the same
* connected component.
*
* @param v one vertex
* @param w the other vertex
* @return {@code true} if vertices {@code v} and {@code w} are in the same
* connected component; {@code false} otherwise
* @throws IllegalArgumentException unless {@code 0 <= v < V}
* @throws IllegalArgumentException unless {@code 0 <= w < V}
* @deprecated Replaced by {@link #connected(int, int)}.
*/
@Deprecated
public boolean areConnected(int v, int w) {
validateVertex(v);
validateVertex(w);
return id(v) == id(w);
}
// throw an IllegalArgumentException unless {@code 0 <= v < V}
private void validateVertex(int v) {
int V = marked.length;
if (v < 0 || v >= V)
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V-1));
}
/**
* Unit tests the {@code CC} data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
In in = new In(args[0]);
Graph G = new Graph(in);
CC cc = new CC(G);
// number of connected components
int m = cc.count();
StdOut.println(m + " components");
// compute list of vertices in each connected component
Queue<Integer>[] components = (Queue<Integer>[]) new Queue[m];
for (int i = 0; i < m; i++) {
components[i] = new Queue<Integer>();
}
for (int v = 0; v < G.V(); v++) {
components[cc.id(v)].enqueue(v);
}
// print results
for (int i = 0; i < m; i++) {
for (int v : components[i]) {
StdOut.print(v + " ");
}
StdOut.println();
}
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac CPM.java
* Execution: java CPM < input.txt
* Dependencies: EdgeWeightedDigraph.java AcyclicDigraphLP.java StdOut.java
* Data files: http://algs4.cs.princeton.edu/44sp/jobsPC.txt
*
* Critical path method.
*
* % java CPM < jobsPC.txt
* job start finish
* --------------------
* 0 0.0 41.0
* 1 41.0 92.0
* 2 123.0 173.0
* 3 91.0 127.0
* 4 70.0 108.0
* 5 0.0 45.0
* 6 70.0 91.0
* 7 41.0 73.0
* 8 91.0 123.0
* 9 41.0 70.0
* Finish time: 173.0
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code CPM} class provides a client that solves the
* parallel precedence-constrained job scheduling problem
* via the <em>critical path method</em>. It reduces the problem
* to the longest-paths problem in edge-weighted DAGs.
* It builds an edge-weighted digraph (which must be a DAG)
* from the job-scheduling problem specification,
* finds the longest-paths tree, and computes the longest-paths
* lengths (which are precisely the start times for each job).
* <p>
* This implementation uses {@link AcyclicLP} to find a longest
* path in a DAG.
* The running time is proportional to <em>V</em> + <em>E</em>,
* where <em>V</em> is the number of jobs and <em>E</em> is the
* number of precedence constraints.
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/44sp">Section 4.4</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class CPM {
// this class cannot be instantiated
private CPM() { }
/**
* Reads the precedence constraints from standard input
* and prints a feasible schedule to standard output.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
// number of jobs
int n = StdIn.readInt();
// source and sink
int source = 2*n;
int sink = 2*n + 1;
// build network
EdgeWeightedDigraph G = new EdgeWeightedDigraph(2*n + 2);
for (int i = 0; i < n; i++) {
double duration = StdIn.readDouble();
G.addEdge(new DirectedEdge(source, i, 0.0));
G.addEdge(new DirectedEdge(i+n, sink, 0.0));
G.addEdge(new DirectedEdge(i, i+n, duration));
// precedence constraints
int m = StdIn.readInt();
for (int j = 0; j < m; j++) {
int precedent = StdIn.readInt();
G.addEdge(new DirectedEdge(n+i, precedent, 0.0));
}
}
// compute longest path
AcyclicLP lp = new AcyclicLP(G, source);
// print results
StdOut.println(" job start finish");
StdOut.println("--------------------");
for (int i = 0; i < n; i++) {
StdOut.printf("%4d %7.1f %7.1f\n", i, lp.distTo(i), lp.distTo(i+n));
}
StdOut.printf("Finish time: %7.1f\n", lp.distTo(sink));
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
/******************************************************************************
* Compilation: javac Cat.java
* Execution: java Cat input0.txt input1.txt ... output.txt
* Dependencies: In.java Out.java
* Data files: http://algs4.cs.princeton.edu/11model/in1.txt
* http://algs4.cs.princeton.edu/11model/in2.txt
*
* Reads in text files specified as the first command-line
* arguments, concatenates them, and writes the result to
* filename specified as the last command-line arguments.
*
* % more in1.txt
* This is
*
* % more in2.txt
* a tiny
* test.
*
* % java Cat in1.txt in2.txt out.txt
*
* % more out.txt
* This is
* a tiny
* test.
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code Cat} class provides a client for concatenating the results
* of several text files.
* <p>
* For additional documentation, see <a href="http://algs4.cs.princeton.edu/11model">Section 1.1</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class Cat {
// this class should not be instantiated
private Cat() { }
/**
* Reads in a sequence of text files specified as the first command-line
* arguments, concatenates them, and writes the results to the file
* specified as the last command-line argument.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
Out out = new Out(args[args.length - 1]);
for (int i = 0; i < args.length - 1; i++) {
In in = new In(args[i]);
String s = in.readAll();
out.println(s);
in.close();
}
out.close();
}
}
/******************************************************************************
* Copyright 2002-2016, 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.
******************************************************************************/
No preview for this file type
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.