use of chapter1.section5.UnionFind in project algorithms-sedgewick-wayne by reneargento.
the class Exercise52_NegativeWeightsI method main.
// Parameters example: -1 1 6 0.5 9 5 100
// -1 1 6 0.5 16 40 100
public static void main(String[] args) {
Exercise52_NegativeWeightsI negativeWeightsI = new Exercise52_NegativeWeightsI();
List<Double> weightsToRescale = new ArrayList<>();
weightsToRescale.add(0.9);
weightsToRescale.add(0.99);
weightsToRescale.add(0.35);
weightsToRescale.add(-0.2);
weightsToRescale.add(-0.3);
double minWeightInScaleTest = -1;
double maxWeightInScaleTest = 1;
StdOut.print("Rescaled weights: ");
List<Double> rescaledWeights = negativeWeightsI.rescaleWeights(weightsToRescale, minWeightInScaleTest, maxWeightInScaleTest);
for (double rescaledWeight : rescaledWeights) {
StdOut.printf("%.2f ", rescaledWeight);
}
StdOut.println("\nExpected: 0.86 1.00 0.01 -0.84 -1.00 ");
double minWeightInScale = Double.parseDouble(args[0]);
double maxWeightInScale = Double.parseDouble(args[1]);
int verticesInEuclideanDigraph = Integer.parseInt(args[2]);
double radius = Double.parseDouble(args[3]);
int verticesInGridDigraph = Integer.parseInt(args[4]);
int extraEdgesInGridDigraph = Integer.parseInt(args[5]);
int numberOfDigraphs = Integer.parseInt(args[6]);
if (minWeightInScale < -1 || minWeightInScale > 1) {
throw new IllegalArgumentException("Min weight in scale must be between -1 and 1");
}
if (maxWeightInScale < -1 || maxWeightInScale > 1) {
throw new IllegalArgumentException("Max weight in scale must be between -1 and 1");
}
if (minWeightInScale > maxWeightInScale) {
throw new IllegalArgumentException("Min weight in scale cannot be higher than max weight in scale");
}
// 1- Random sparse edge-weighted digraphs
RandomSparseEdgeWeightedDigraphsGenerator randomSparseEdgeWeightedDigraphsGenerator = negativeWeightsI.new RandomSparseEdgeWeightedDigraphsGenerator();
List<EdgeWeightedDigraphInterface> randomSparseEdgeWeightedDigraphsUniform = randomSparseEdgeWeightedDigraphsGenerator.randomSparseEdgeWeightedDigraphsGenerator(numberOfDigraphs, true, minWeightInScale, maxWeightInScale);
EdgeWeightedDigraphInterface firstRandomSparseEdgeWeightedDigraphUniform = randomSparseEdgeWeightedDigraphsUniform.get(0);
StdOut.println("\nRandom sparse-edge-weighted-digraph with uniform weights distribution");
StdOut.println(firstRandomSparseEdgeWeightedDigraphUniform);
List<EdgeWeightedDigraphInterface> randomSparseEdgeWeightedDigraphsGaussian = randomSparseEdgeWeightedDigraphsGenerator.randomSparseEdgeWeightedDigraphsGenerator(numberOfDigraphs, false, minWeightInScale, maxWeightInScale);
EdgeWeightedDigraphInterface firstRandomSparseEdgeWeightedDigraphGaussian = randomSparseEdgeWeightedDigraphsGaussian.get(0);
StdOut.println("Random sparse-edge-weighted-digraph with gaussian weights distribution:");
StdOut.println(firstRandomSparseEdgeWeightedDigraphGaussian);
// 2- Random Euclidean edge-weighted digraphs
StdOut.println("Random Euclidean edge-weighted digraph:");
List<EdgeWeightedDigraphInterface> randomEuclideanEdgeWeightedDigraphs = negativeWeightsI.new RandomEuclideanEdgeWeightedDigraphsGenerator().generateRandomEuclideanEdgeWeightedDigraphs(numberOfDigraphs, verticesInEuclideanDigraph, radius, minWeightInScale, maxWeightInScale);
EdgeWeightedDigraphInterface firstEuclideanEdgeWeightedDigraph = randomEuclideanEdgeWeightedDigraphs.get(0);
StdOut.println(firstEuclideanEdgeWeightedDigraph);
UnionFind unionFind = new UnionFind(verticesInEuclideanDigraph);
for (int vertex = 0; vertex < verticesInEuclideanDigraph; vertex++) {
for (DirectedEdge edge : firstEuclideanEdgeWeightedDigraph.adjacent(vertex)) {
int neighbor = edge.to();
unionFind.union(vertex, neighbor);
}
}
// Check theory that if "radius" is larger than threshold value SQRT(ln(V) / (Math.PI * V)) then the graph is
// almost certainly connected. Otherwise, it is almost certainly disconnected.
double thresholdValue = Math.sqrt(Math.log(verticesInEuclideanDigraph) / (Math.PI * verticesInEuclideanDigraph));
StdOut.println("Euclidean digraph expected to be connected (if it were an undirected graph): " + (radius > thresholdValue));
StdOut.println("Euclidean digraph would be connected (if it were an undirected graph): " + (unionFind.count() == 1));
// 3- Random grid edge-weighted digraphs
StdOut.println("\nRandom grid edge-weighted digraph:");
List<EdgeWeightedDigraphInterface> randomGridEdgeWeightedDigraphs = negativeWeightsI.new RandomGridEdgeWeightedDigraphsGenerator().generateRandomGridEdgeWeightedDigraphs(numberOfDigraphs, verticesInGridDigraph, extraEdgesInGridDigraph, minWeightInScale, maxWeightInScale);
EdgeWeightedDigraphInterface firstRandomGridEdgeWeightedDigraph = randomGridEdgeWeightedDigraphs.get(0);
StdOut.println(firstRandomGridEdgeWeightedDigraph);
}
use of chapter1.section5.UnionFind in project algorithms-sedgewick-wayne by reneargento.
the class Exercise53_NegativeWeightsII method main.
// Parameters example: 50 6 0.5 9 5 100
// 50 6 0.5 16 40 100
public static void main(String[] args) {
Exercise53_NegativeWeightsII negativeWeightsII = new Exercise53_NegativeWeightsII();
List<Double> weights = new ArrayList<>();
weights.add(0.9);
weights.add(0.99);
weights.add(0.35);
weights.add(0.2);
int percentageToNegateTest = 50;
StdOut.print("Updated weights: ");
List<Double> updatedWeights = negativeWeightsII.negateRandomWeights(weights, percentageToNegateTest);
for (double updatedWeight : updatedWeights) {
StdOut.printf("%.2f ", updatedWeight);
}
StdOut.println("\nExpected: 2 out of 4 edges with negative weights");
int percentageToNegate = Integer.parseInt(args[0]);
int verticesInEuclideanDigraph = Integer.parseInt(args[1]);
double radius = Double.parseDouble(args[2]);
int verticesInGridDigraph = Integer.parseInt(args[3]);
int extraEdgesInGridDigraph = Integer.parseInt(args[4]);
int numberOfDigraphs = Integer.parseInt(args[5]);
if (percentageToNegate < 0 || percentageToNegate > 100) {
throw new IllegalArgumentException("Percentage to negate must be between 0 and 100");
}
// 1- Random sparse edge-weighted digraphs
RandomSparseEdgeWeightedDigraphsGenerator randomSparseEdgeWeightedDigraphsGenerator = negativeWeightsII.new RandomSparseEdgeWeightedDigraphsGenerator();
List<EdgeWeightedDigraphInterface> randomSparseEdgeWeightedDigraphsUniform = randomSparseEdgeWeightedDigraphsGenerator.randomSparseEdgeWeightedDigraphsGenerator(numberOfDigraphs, true, percentageToNegate);
EdgeWeightedDigraphInterface firstRandomSparseEdgeWeightedDigraphUniform = randomSparseEdgeWeightedDigraphsUniform.get(0);
StdOut.println("\nRandom sparse-edge-weighted-digraph with uniform weights distribution");
StdOut.println(firstRandomSparseEdgeWeightedDigraphUniform);
List<EdgeWeightedDigraphInterface> randomSparseEdgeWeightedDigraphsGaussian = randomSparseEdgeWeightedDigraphsGenerator.randomSparseEdgeWeightedDigraphsGenerator(numberOfDigraphs, false, percentageToNegate);
EdgeWeightedDigraphInterface firstRandomSparseEdgeWeightedDigraphGaussian = randomSparseEdgeWeightedDigraphsGaussian.get(0);
StdOut.println("Random sparse-edge-weighted-digraph with gaussian weights distribution:");
StdOut.println(firstRandomSparseEdgeWeightedDigraphGaussian);
// 2- Random Euclidean edge-weighted digraphs
StdOut.println("Random Euclidean edge-weighted digraph:");
List<EdgeWeightedDigraphInterface> randomEuclideanEdgeWeightedDigraphs = negativeWeightsII.new RandomEuclideanEdgeWeightedDigraphsGenerator().generateRandomEuclideanEdgeWeightedDigraphs(numberOfDigraphs, verticesInEuclideanDigraph, radius, percentageToNegate);
EdgeWeightedDigraphInterface firstEuclideanEdgeWeightedDigraph = randomEuclideanEdgeWeightedDigraphs.get(0);
StdOut.println(firstEuclideanEdgeWeightedDigraph);
UnionFind unionFind = new UnionFind(verticesInEuclideanDigraph);
for (int vertex = 0; vertex < verticesInEuclideanDigraph; vertex++) {
for (DirectedEdge edge : firstEuclideanEdgeWeightedDigraph.adjacent(vertex)) {
int neighbor = edge.to();
unionFind.union(vertex, neighbor);
}
}
// Check theory that if "radius" is larger than threshold value SQRT(ln(V) / (Math.PI * V)) then the graph is
// almost certainly connected. Otherwise, it is almost certainly disconnected.
double thresholdValue = Math.sqrt(Math.log(verticesInEuclideanDigraph) / (Math.PI * verticesInEuclideanDigraph));
StdOut.println("Euclidean digraph expected to be connected (if it were an undirected graph): " + (radius > thresholdValue));
StdOut.println("Euclidean digraph would be connected (if it were an undirected graph): " + (unionFind.count() == 1));
// 3- Random grid edge-weighted digraphs
StdOut.println("\nRandom grid edge-weighted digraph:");
List<EdgeWeightedDigraphInterface> randomGridEdgeWeightedDigraphs = negativeWeightsII.new RandomGridEdgeWeightedDigraphsGenerator().generateRandomGridEdgeWeightedDigraphs(numberOfDigraphs, verticesInGridDigraph, extraEdgesInGridDigraph, percentageToNegate);
EdgeWeightedDigraphInterface firstRandomGridEdgeWeightedDigraph = randomGridEdgeWeightedDigraphs.get(0);
StdOut.println(firstRandomGridEdgeWeightedDigraph);
}
use of chapter1.section5.UnionFind in project algorithms-sedgewick-wayne by reneargento.
the class Exercise48_RandomEuclideanDigraphs method main.
// Parameters example: 6 0.5 100
public static void main(String[] args) {
int vertices = Integer.parseInt(args[0]);
double radius = Double.parseDouble(args[1]);
int numberOfDigraphs = Integer.parseInt(args[2]);
List<Exercise38_EuclideanDigraphs.EuclideanDigraph> randomEuclideanDigraphs = new Exercise48_RandomEuclideanDigraphs().generateRandomEuclideanDigraphs(numberOfDigraphs, vertices, radius);
Exercise38_EuclideanDigraphs.EuclideanDigraph firstEuclideanDigraph = randomEuclideanDigraphs.get(0);
firstEuclideanDigraph.show(-0.1, 1.1, -0.1, 1.1, 0.03, 0.01, 0.03);
UnionFind unionFind = new UnionFind(vertices);
for (int vertex = 0; vertex < vertices; vertex++) {
for (int neighbor : firstEuclideanDigraph.adjacent(vertex)) {
unionFind.union(vertex, neighbor);
}
}
// Check theory that if "radius" is larger than threshold value SQRT(ln(V) / (Math.PI * V)) then the graph is
// almost certainly connected. Otherwise, it is almost certainly disconnected.
double thresholdValue = Math.sqrt(Math.log(vertices) / (Math.PI * vertices));
StdOut.println("Expected to be connected (if it were an undirected graph): " + (radius > thresholdValue));
StdOut.println("Would be connected (if it were an undirected graph): " + (unionFind.count() == 1));
}
Aggregations