Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-wayne committed Aug 31, 2015
1 parent a41168e commit 8468f0d
Show file tree
Hide file tree
Showing 182 changed files with 41,862 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.class
*.html
*~
README-GIT
103 changes: 103 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.princeton.cs</groupId>
<artifactId>algs4</artifactId>
<packaging>jar</packaging>
<version>1.0.0.0</version>
<name>algs4</name>
<licenses>
<license>
<name>GNU GENERAL PUBLIC LICENSE</name>
<url>http://www.gnu.org/licenses/gpl-3.0.en.html</url>
</license>
</licenses>
<developers>
<developer>
<id>kevin</id>
<name>Kevin Wayne</name>
<email>[email protected]</email>
<url>https://www.cs.princeton.edu/~wayne/contact/</url>
<organization>Princeton University </organization>
<organizationUrl>https://www.cs.princeton.edu</organizationUrl>
<roles>
<role>author</role>
<role>developer</role>
</roles>
<timezone>UTC/GMT -4:00 hours</timezone>
<properties>
<picUrl>https://www.cs.princeton.edu/~wayne/contact/KevinWayne.jpg</picUrl>
</properties>
</developer>
<developer>
<id>robert</id>
<name>Robert Sedgewick</name>
<roles>
<role>author</role>
</roles>
</developer>
</developers>
<issueManagement>
<system>github.com</system>
<url>https://github.com/kevin-wayne/algs4/issues</url>
</issueManagement>
<profiles>
<profile>
<id>main-build</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>com.springsource.repository.bundles.external</id>
<name>SpringSource Enterprise Bundle Repository - External Bundle Releases</name>
<url>http://repository.springsource.com/maven/bundles/external</url>
</repository>
</repositories>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>javax.media.jai</groupId>
<artifactId>com.springsource.javax.media.jai.core</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>java3d</groupId>
<artifactId>vecmath</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>gov.nist.math</groupId>
<artifactId>jama</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>java3d</groupId>
<artifactId>j3d-core</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>java3d</groupId>
<artifactId>j3d-core-utils</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
</project>
166 changes: 166 additions & 0 deletions src/main/java/edu/princeton/cs/algs4/AcyclicLP.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/******************************************************************************
* 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 <tt>AcyclicLP</tt> 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 <tt>distTo()</tt> and <tt>hasPathTo()</tt> methods take
* constant time and the <tt>pathTo()</tt> 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 <tt>s</tt> to every other vertex in
* the directed acyclic graph <tt>G</tt>.
* @param G the acyclic digraph
* @param s the source vertex
* @throws IllegalArgumentException if the digraph is not acyclic
* @throws IllegalArgumentException unless 0 &le; <tt>s</tt> &le; <tt>V</tt> - 1
*/
public AcyclicLP(EdgeWeightedDigraph G, int s) {
distTo = new double[G.V()];
edgeTo = new DirectedEdge[G.V()];
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 <tt>s</tt> to vertex <tt>v</tt>.
* @param v the destination vertex
* @return the length of a longest path from the source vertex <tt>s</tt> to vertex <tt>v</tt>;
* <tt>Double.NEGATIVE_INFINITY</tt> if no such path
*/
public double distTo(int v) {
return distTo[v];
}

/**
* Is there a path from the source vertex <tt>s</tt> to vertex <tt>v</tt>?
* @param v the destination vertex
* @return <tt>true</tt> if there is a path from the source vertex
* <tt>s</tt> to vertex <tt>v</tt>, and <tt>false</tt> otherwise
*/
public boolean hasPathTo(int v) {
return distTo[v] > Double.NEGATIVE_INFINITY;
}

/**
* Returns a longest path from the source vertex <tt>s</tt> to vertex <tt>v</tt>.
* @param v the destination vertex
* @return a longest path from the source vertex <tt>s</tt> to vertex <tt>v</tt>
* as an iterable of edges, and <tt>null</tt> if no such path
*/
public Iterable<DirectedEdge> pathTo(int 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;
}



/**
* Unit tests the <tt>AcyclicLP</tt> data type.
*/
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-2015, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/
Loading

0 comments on commit 8468f0d

Please sign in to comment.