Skip to content

Commit ca26b74

Browse files
committed
making performance guarantees in API more precise
1 parent e6f2065 commit ca26b74

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+424
-297
lines changed

src/main/java/edu/princeton/cs/algs4/AcyclicLP.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
* acyclic graphs (DAGs). The edge weights can be positive, negative, or zero.
2929
* <p>
3030
* This implementation uses a topological-sort based algorithm.
31-
* The constructor takes time proportional to <em>V</em> + <em>E</em>,
32-
* where <em>V</em> is the number of vertices and <em>E</em> is the number of edges.
33-
* Each call to {@code distTo(int)} and {@code hasPathTo(int)} takes constant time;
34-
* each call to {@code pathTo(int)} takes time proportional to the number of
35-
* edges in the shortest path returned.
31+
* The constructor takes &Theta;(<em>V</em> + <em>E</em>) time in the
32+
* worst case, where <em>V</em> is the number of vertices and
33+
* <em>E</em> is the number of edges.
34+
* Each instance method takes &Theta;(1) time.
35+
* It uses &Theta;(<em>V</em>) extra space (not including the
36+
* edge-weighted digraph).
3637
* <p>
3738
* For additional documentation,
3839
* see <a href="https://algs4.cs.princeton.edu/44sp">Section 4.4</a> of

src/main/java/edu/princeton/cs/algs4/AcyclicSP.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
* graphs (DAGs). The edge weights can be positive, negative, or zero.
2727
* <p>
2828
* This implementation uses a topological-sort based algorithm.
29-
* The constructor takes time proportional to <em>V</em> + <em>E</em>,
30-
* where <em>V</em> is the number of vertices and <em>E</em> is the number of edges.
31-
* Each call to {@code distTo(int)} and {@code hasPathTo(int)} takes constant time;
32-
* each call to {@code pathTo(int)} takes time proportional to the number of
33-
* edges in the shortest path returned.
29+
* The constructor takes &Theta;(<em>V</em> + <em>E</em>) time in the
30+
* worst case, where <em>V</em> is the number of vertices and
31+
* <em>E</em> is the number of edges.
32+
* Each instance method takes &Theta;(1) time.
33+
* It uses &Theta;(<em>V</em>) extra space (not including the
34+
* edge-weighted digraph).
3435
* <p>
3536
* For additional documentation,
3637
* see <a href="https://algs4.cs.princeton.edu/44sp">Section 4.4</a> of

src/main/java/edu/princeton/cs/algs4/BellmanFordSP.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737
* This class finds either a shortest path from the source vertex <em>s</em>
3838
* to every other vertex or a negative cycle reachable from the source vertex.
3939
* <p>
40-
* This implementation uses the Bellman-Ford-Moore algorithm.
41-
* The constructor takes time proportional to <em>V</em> (<em>V</em> + <em>E</em>)
42-
* in the worst case, where <em>V</em> is the number of vertices and <em>E</em>
43-
* is the number of edges.
44-
* Each call to {@code distTo(int)} and {@code hasPathTo(int)},
45-
* {@code hasNegativeCycle} takes constant time;
46-
* each call to {@code pathTo(int)} and {@code negativeCycle()}
47-
* takes time proportional to length of the path returned.
40+
* This implementation uses a queue-based implementation of
41+
* the Bellman-Ford-Moore algorithm.
42+
* The constructor takes &Theta;(<em>V</em> (<em>V</em> + <em>E</em>)) time
43+
* in the worst case, where <em>V</em> is the number of vertices and
44+
* <em>E</em> is the number of edges. In practice, it performs much better.
45+
* Each instance method takes &Theta;(1) time.
46+
* It uses &Theta;(<em>V</em>) extra space (not including the
47+
* edge-weighted digraph).
4848
* <p>
4949
* For additional documentation,
5050
* see <a href="https://algs4.cs.princeton.edu/44sp">Section 4.4</a> of

src/main/java/edu/princeton/cs/algs4/BinaryInsertion.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,20 @@
2828
* The {@code BinaryInsertion} class provides a static method for sorting an
2929
* array using an optimized binary insertion sort with half exchanges.
3030
* <p>
31-
* This implementation makes ~ n lg n compares for any array of length n.
32-
* However, in the worst case, the running time is quadratic because the
33-
* number of array accesses can be proportional to n^2 (e.g, if the array
34-
* is reverse sorted). As such, it is not suitable for sorting large
35-
* arrays (unless the number of inversions is small).
31+
* In the worst case, this implementation makes
32+
* ~ <em>n</em> log<sub>2</sub><em>n</em> compares to sort an array of length
33+
* <em>n</em>. However, in the worst case, the running time is
34+
* &Theta;(<em>n</em><sup>2</sup>) because the number of array accesses
35+
* can be quadratic.
36+
* As such, it is not suitable for sorting large arrays
37+
* (unless the number of inversions is small).
3638
* <p>
37-
* The sorting algorithm is stable and uses O(1) extra memory.
39+
* This sorting algorithm is stable.
40+
* It uses &Theta;(1) extra memory (not including the input array).
3841
* <p>
39-
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/21elementary">Section 2.1</a> of
40-
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
42+
* For additional documentation,
43+
* see <a href="https://algs4.cs.princeton.edu/21elementary">Section 2.1</a>
44+
* of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
4145
*
4246
* @author Ivan Pesin
4347
* @author Robert Sedgewick

src/main/java/edu/princeton/cs/algs4/Bipartite.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616

1717
/**
1818
* The {@code Bipartite} class represents a data type for
19-
* determining whether an undirected graph is bipartite or whether
20-
* it has an odd-length cycle.
19+
* determining whether an undirected graph is <em>bipartite</em> or whether
20+
* it has an <em>odd-length cycle</em>.
21+
* A graph is bipartite if and only if it has no odd-length cycle.
2122
* The <em>isBipartite</em> operation determines whether the graph is
2223
* bipartite. If so, the <em>color</em> operation determines a
2324
* bipartition; if not, the <em>oddCycle</em> operation determines a
2425
* cycle with an odd number of edges.
2526
* <p>
26-
* This implementation uses depth-first search.
27-
* The constructor takes time proportional to <em>V</em> + <em>E</em>
28-
* (in the worst case),
29-
* where <em>V</em> is the number of vertices and <em>E</em> is the number of edges.
30-
* Afterwards, the <em>isBipartite</em> and <em>color</em> operations
31-
* take constant time; the <em>oddCycle</em> operation takes time proportional
32-
* to the length of the cycle.
27+
* This implementation uses <em>depth-first search</em>.
28+
* The constructor takes &Theta;(<em>V</em> + <em>E</em>) time in
29+
* the worst case, where <em>V</em> is the number of vertices and <em>E</em>
30+
* is the number of edges.
31+
* Each instance method takes &Theta;(1) time.
32+
* It uses &Theta;(<em>V</em>) extra space (not including the graph).
3333
* See {@link BipartiteX} for a nonrecursive version that uses breadth-first
3434
* search.
3535
* <p>

src/main/java/edu/princeton/cs/algs4/BipartiteX.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414

1515
/**
1616
* The {@code BipartiteX} class represents a data type for
17-
* determining whether an undirected graph is bipartite or whether
18-
* it has an odd-length cycle.
17+
* determining whether an undirected graph is <em>bipartite</em> or whether
18+
* it has an <em>odd-length cycle</em>.
19+
* A graph is bipartite if and only if it has no odd-length cycle.
1920
* The <em>isBipartite</em> operation determines whether the graph is
2021
* bipartite. If so, the <em>color</em> operation determines a
2122
* bipartition; if not, the <em>oddCycle</em> operation determines a
2223
* cycle with an odd number of edges.
2324
* <p>
24-
* This implementation uses breadth-first search and is nonrecursive.
25-
* The constructor takes time proportional to <em>V</em> + <em>E</em>
26-
* (in the worst case),
27-
* where <em>V</em> is the number of vertices and <em>E</em> is the number of edges.
28-
* Afterwards, the <em>isBipartite</em> and <em>color</em> operations
29-
* take constant time; the <em>oddCycle</em> operation takes time proportional
30-
* to the length of the cycle.
25+
* This implementation uses <em>breadth-first search</em> and is nonrecursive.
26+
* The constructor takes &Theta;(<em>V</em> + <em>E</em>) time in
27+
* in the worst case, where <em>V</em> is the number of vertices
28+
* and <em>E</em> is the number of edges.
29+
* Each instance method takes &Theta;(1) time.
30+
* It uses &Theta;(<em>V</em>) extra space (not including the graph).
3131
* See {@link Bipartite} for a recursive version that uses depth-first search.
3232
* <p>
3333
* For additional documentation,

src/main/java/edu/princeton/cs/algs4/BreadthFirstDirectedPaths.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,16 @@
2929
package edu.princeton.cs.algs4;
3030

3131
/**
32-
* The {@code BreadthDirectedFirstPaths} class represents a data type for finding
33-
* shortest paths (number of edges) from a source vertex <em>s</em>
32+
* The {@code BreadthDirectedFirstPaths} class represents a data type for
33+
* finding shortest paths (number of edges) from a source vertex <em>s</em>
3434
* (or set of source vertices) to every other vertex in the digraph.
3535
* <p>
3636
* This implementation uses breadth-first search.
37-
* The constructor takes time proportional to <em>V</em> + <em>E</em>,
38-
* where <em>V</em> is the number of vertices and <em>E</em> is the number of edges.
39-
* Each call to {@link #distTo(int)} and {@link #hasPathTo(int)} takes constant time;
40-
* each call to {@link #pathTo(int)} takes time proportional to the length
41-
* of the path.
42-
* It uses extra space (not including the digraph) proportional to <em>V</em>.
37+
* The constructor takes &Theta;(<em>V</em> + <em>E</em>) time in the
38+
* worst case, where <em>V</em> is the number of vertices and <em>E</em> is
39+
* the number of edges.
40+
* Each instance method takes &Theta;(1) time.
41+
* It uses &Theta;(<em>V</em>) extra space (not including the digraph).
4342
* <p>
4443
* For additional documentation,
4544
* see <a href="https://algs4.cs.princeton.edu/42digraph">Section 4.2</a> of

src/main/java/edu/princeton/cs/algs4/BreadthFirstPaths.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@
4848
* to every other vertex in an undirected graph.
4949
* <p>
5050
* This implementation uses breadth-first search.
51-
* The constructor takes time proportional to <em>V</em> + <em>E</em>,
52-
* where <em>V</em> is the number of vertices and <em>E</em> is the number of edges.
53-
* Each call to {@link #distTo(int)} and {@link #hasPathTo(int)} takes constant time;
54-
* each call to {@link #pathTo(int)} takes time proportional to the length
55-
* of the path.
56-
* It uses extra space (not including the graph) proportional to <em>V</em>.
51+
* The constructor takes &Theta;(<em>V</em> + <em>E</em>) time in the
52+
* worst case, where <em>V</em> is the number of vertices and <em>E</em>
53+
* is the number of edges.
54+
* Each instance method takes &Theta;(1) time.
55+
* It uses &Theta;(<em>V</em>) extra space (not including the graph).
5756
* <p>
5857
* For additional documentation,
5958
* see <a href="https://algs4.cs.princeton.edu/41graph">Section 4.1</a>

src/main/java/edu/princeton/cs/algs4/CC.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@
4747
4848
* <p>
4949
* This implementation uses depth-first search.
50-
* The constructor takes time proportional to <em>V</em> + <em>E</em>
51-
* (in the worst case),
52-
* where <em>V</em> is the number of vertices and <em>E</em> is the number of edges.
53-
* Afterwards, the <em>id</em>, <em>count</em>, <em>connected</em>,
54-
* and <em>size</em> operations take constant time.
50+
* The constructor takes &Theta;(<em>V</em> + <em>E</em>) time,
51+
* where <em>V</em> is the number of vertices and <em>E</em> is the
52+
* number of edges.
53+
* Each instance method takes &Theta;(1) time.
54+
* It uses &Theta;(<em>V</em>) extra space (not including the graph).
5555
* <p>
56-
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/41graph">Section 4.1</a>
57-
* of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
56+
* For additional documentation, see
57+
* <a href="https://algs4.cs.princeton.edu/41graph">Section 4.1</a>
58+
* of <em>Algorithms, 4th Edition</em> by Robert Sedgewick and Kevin Wayne.
5859
*
5960
* @author Robert Sedgewick
6061
* @author Kevin Wayne

src/main/java/edu/princeton/cs/algs4/CPM.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
* <p>
3838
* This implementation uses {@link AcyclicLP} to find a longest
3939
* path in a DAG.
40-
* The running time is proportional to <em>V</em> + <em>E</em>,
41-
* where <em>V</em> is the number of jobs and <em>E</em> is the
42-
* number of precedence constraints.
40+
* The program takes &Theta;(<em>V</em> + <em>E</em>) time in
41+
* the worst case, where <em>V</em> is the number of jobs and
42+
* <em>E</em> is the number of precedence constraints.
4343
* <p>
4444
* For additional documentation,
4545
* see <a href="https://algs4.cs.princeton.edu/44sp">Section 4.4</a> of

0 commit comments

Comments
 (0)