-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- CPM - DepthFirstSearch - FarthestPair - Heap - InsertionX - Knuth - MergeBU - QuickUnionUF - ResizingArrayBag
- Loading branch information
1 parent
a698625
commit 5b922aa
Showing
10 changed files
with
941 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using algs4.stdlib; | ||
|
||
namespace algs4.algs4 | ||
{ | ||
public static class CPM | ||
{ | ||
/// <summary> | ||
/// Reads the precedence constraints from standard input | ||
/// and prints a feasible schedule to standard output. | ||
/// </summary> | ||
/// <param name="args"></param> | ||
public static void RunMain(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("{0:0000} {1:0000000.0} {2:0000000.0}\n", i, lp.DistTo(i), lp.DistTo(i + n)); | ||
} | ||
StdOut.PrintF("Finish time: {0:0000000.0}\n", lp.DistTo(sink)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using algs4.stdlib; | ||
|
||
namespace algs4.algs4 | ||
{ | ||
public class DepthFirstSearch | ||
{ | ||
/// <summary> | ||
/// marked[v] = is there an s-v path? | ||
/// </summary> | ||
private readonly bool[] _marked; | ||
|
||
/// <summary> | ||
/// Number of vertices connected to s | ||
/// </summary> | ||
private int _count; | ||
|
||
/// <summary> | ||
/// Computes the vertices in graph G that are | ||
/// connected to the source vertex s. | ||
/// </summary> | ||
/// <param name="g">the graph</param> | ||
/// <param name="s">the source vertex</param> | ||
public DepthFirstSearch(Graph g, int s) | ||
{ | ||
_marked = new bool[g.V()]; | ||
DFS(g, s); | ||
} | ||
|
||
/// <summary> | ||
/// Depth first search from v | ||
/// </summary> | ||
/// <param name="g"></param> | ||
/// <param name="v"></param> | ||
private void DFS(Graph g, int v) | ||
{ | ||
_count++; | ||
_marked[v] = true; | ||
foreach (int w in g.Adj(v)) | ||
{ | ||
if (!_marked[w]) | ||
{ | ||
DFS(g, w); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Is there a path between the source vertex s and vertex v? | ||
/// </summary> | ||
/// <param name="v">the vertex</param> | ||
/// <returns>true if there is a path, false otherwise</returns> | ||
public bool Marked(int v) | ||
{ | ||
return _marked[v]; | ||
} | ||
|
||
/// <summary> | ||
/// Returns the number of vertices connected to the source vertex s. | ||
/// </summary> | ||
/// <returns>the number of vertices connected to the source vertex s</returns> | ||
public int Count() | ||
{ | ||
return _count; | ||
} | ||
|
||
/// <summary> | ||
/// Unit tests the DepthFirstSearch data type. | ||
/// </summary> | ||
/// <param name="args"></param> | ||
public static void RunMain(string[] args) | ||
{ | ||
In input = new In(args[0]); | ||
Graph g = new Graph(input); | ||
int s = int.Parse(args[1]); | ||
DepthFirstSearch search = new DepthFirstSearch(g, s); | ||
for (int v = 0; v < g.V(); v++) | ||
{ | ||
if (search.Marked(v)) | ||
{ | ||
StdOut.Print(v + " "); | ||
} | ||
} | ||
|
||
StdOut.PrintLn(); | ||
if (search.Count() != g.V()) | ||
{ | ||
StdOut.PrintLn("NOT connected"); | ||
} | ||
else | ||
{ | ||
StdOut.PrintLn("connected"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System.Linq; | ||
using algs4.stdlib; | ||
|
||
namespace algs4.algs4 | ||
{ | ||
public class FarthestPair | ||
{ | ||
/// <summary> | ||
/// Farthest pair of points and distance | ||
/// </summary> | ||
private readonly Point2D _best1, _best2; | ||
|
||
private readonly double _bestDistance = double.NegativeInfinity; | ||
|
||
public FarthestPair(Point2D[] points) | ||
{ | ||
GrahamScan graham = new GrahamScan(points); | ||
|
||
// single point | ||
if (points.Length <= 1) | ||
{ | ||
return; | ||
} | ||
|
||
// number of points on the hull | ||
int pointCount = graham.Hull().Count(); | ||
|
||
// the hull, in counterclockwise order | ||
Point2D[] hull = new Point2D[pointCount + 1]; | ||
int m = 1; | ||
foreach (Point2D p in graham.Hull()) | ||
{ | ||
hull[m++] = p; | ||
} | ||
|
||
// all points are equal | ||
if (pointCount == 1) | ||
{ | ||
return; | ||
} | ||
|
||
// points are collinear | ||
if (pointCount == 2) | ||
{ | ||
_best1 = hull[1]; | ||
_best2 = hull[2]; | ||
_bestDistance = _best1.DistanceTo(_best2); | ||
return; | ||
} | ||
|
||
// k = farthest vertex from edge from hull[1] to hull[M] | ||
int k = 2; | ||
while (Point2D.Area2(hull[pointCount], hull[k + 1], hull[1]) > Point2D.Area2(hull[pointCount], hull[k], hull[1])) | ||
{ | ||
k++; | ||
} | ||
|
||
int j = k; | ||
for (int i = 1; i <= k; i++) | ||
{ | ||
// StdOut.println("hull[i] + " and " + hull[j] + " are antipodal"); | ||
if (hull[i].DistanceTo(hull[j]) > _bestDistance) | ||
{ | ||
_best1 = hull[i]; | ||
_best2 = hull[j]; | ||
_bestDistance = hull[i].DistanceTo(hull[j]); | ||
} | ||
while ((j < pointCount) && Point2D.Area2(hull[i], hull[j + 1], hull[i + 1]) > Point2D.Area2(hull[i], hull[j], hull[i + 1])) | ||
{ | ||
j++; | ||
// StdOut.println(hull[i] + " and " + hull[j] + " are antipodal"); | ||
double distance = hull[i].DistanceTo(hull[j]); | ||
if (distance > _bestDistance) | ||
{ | ||
_best1 = hull[i]; | ||
_best2 = hull[j]; | ||
_bestDistance = hull[i].DistanceTo(hull[j]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public Point2D Either() | ||
{ | ||
return _best1; | ||
} | ||
|
||
public Point2D Other() | ||
{ | ||
return _best2; | ||
} | ||
|
||
public double Distance() | ||
{ | ||
return _bestDistance; | ||
} | ||
|
||
public static void RunMain(string[] args) | ||
{ | ||
int n = StdIn.ReadInt(); | ||
Point2D[] points = new Point2D[n]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
int x = StdIn.ReadInt(); | ||
int y = StdIn.ReadInt(); | ||
points[i] = new Point2D(x, y); | ||
} | ||
FarthestPair farthest = new FarthestPair(points); | ||
StdOut.PrintLn(farthest.Distance() + " from " + farthest.Either() + " to " + farthest.Other()); | ||
} | ||
} | ||
} |
Oops, something went wrong.