Skip to content

Commit

Permalink
Adding some new classes to algs4:
Browse files Browse the repository at this point in the history
- CPM
- DepthFirstSearch
- FarthestPair
- Heap
- InsertionX
- Knuth
- MergeBU
- QuickUnionUF
- ResizingArrayBag
  • Loading branch information
mina-asham committed May 10, 2015
1 parent a698625 commit 5b922aa
Show file tree
Hide file tree
Showing 10 changed files with 941 additions and 0 deletions.
9 changes: 9 additions & 0 deletions algs4/algs4.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@
<Compile Include="algs4\Complex.cs" />
<Compile Include="algs4\Count.cs" />
<Compile Include="algs4\Counter.cs" />
<Compile Include="algs4\CPM.cs" />
<Compile Include="algs4\DeDup.cs" />
<Compile Include="algs4\DepthFirstOrder.cs" />
<Compile Include="algs4\DepthFirstSearch.cs" />
<Compile Include="algs4\Digraph.cs" />
<Compile Include="algs4\DigraphGenerator.cs" />
<Compile Include="algs4\DijkstraAllPairsSP.cs" />
Expand All @@ -69,6 +71,7 @@
<Compile Include="algs4\EdgeWeightedDigraph.cs" />
<Compile Include="algs4\EdgeWeightedDirectedCycle.cs" />
<Compile Include="algs4\EdgeWeightedGraph.cs" />
<Compile Include="algs4\FarthestPair.cs" />
<Compile Include="algs4\FileIndex.cs" />
<Compile Include="algs4\FlowEdge.cs" />
<Compile Include="algs4\FlowNetwork.cs" />
Expand All @@ -79,23 +82,29 @@
<Compile Include="algs4\Graph.cs" />
<Compile Include="algs4\GraphGenerator.cs" />
<Compile Include="algs4\GREP.cs" />
<Compile Include="algs4\Heap.cs" />
<Compile Include="algs4\HexDump.cs" />
<Compile Include="algs4\IndexMinPQ.cs" />
<Compile Include="algs4\InsertionX.cs" />
<Compile Include="algs4\Interval1D.cs" />
<Compile Include="algs4\Interval2D.cs" />
<Compile Include="algs4\Knuth.cs" />
<Compile Include="algs4\KWIK.cs" />
<Compile Include="algs4\LongestCommonSubstring.cs" />
<Compile Include="algs4\LookupIndex.cs" />
<Compile Include="algs4\LZW.cs" />
<Compile Include="algs4\MergeBU.cs" />
<Compile Include="algs4\MinPQ.cs" />
<Compile Include="algs4\Multiway.cs" />
<Compile Include="algs4\NFA.cs" />
<Compile Include="algs4\Particle.cs" />
<Compile Include="algs4\PictureDump.cs" />
<Compile Include="algs4\Point2D.cs" />
<Compile Include="algs4\Queue.cs" />
<Compile Include="algs4\QuickUnionUF.cs" />
<Compile Include="algs4\RandomSeq.cs" />
<Compile Include="algs4\RedBlackBST.cs" />
<Compile Include="algs4\ResizingArrayBag.cs" />
<Compile Include="algs4\RunLength.cs" />
<Compile Include="algs4\SequentialSearchST.cs" />
<Compile Include="algs4\Set.cs" />
Expand Down
52 changes: 52 additions & 0 deletions algs4/algs4/CPM.cs
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));
}
}
}
95 changes: 95 additions & 0 deletions algs4/algs4/DepthFirstSearch.cs
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");
}
}
}
}
112 changes: 112 additions & 0 deletions algs4/algs4/FarthestPair.cs
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());
}
}
}
Loading

0 comments on commit 5b922aa

Please sign in to comment.