|
Alan Kaminsky
|
|
•
|
|
Department of Computer Science
|
|
•
|
|
Rochester Institute of Technology
|
|
•
|
|
4486 +
2220 =
6706
|
|
Home Page
|
Simulation Simplified
9. Mean, Median, and the Like
Class P2Pedia02
import edu.rit.numeric.ListSeries;
import edu.rit.numeric.Series;
import edu.rit.util.Random;
import edu.rit.util.RandomSubset;
import java.util.Arrays;
import java.util.LinkedList;
public class P2Pedia02
{
public static void main
(String[] args)
throws Exception
{
if (args.length != 4) usage();
int N = Integer.parseInt (args[0]);
int K = Integer.parseInt (args[1]);
int T = Integer.parseInt (args[2]);
long seed = Long.parseLong (args[3]);
Random prng = Random.getInstance (seed);
int[][] conn = new int[N][K+1];
for (int i = 0; i < N; ++ i) conn[i][0] = (i + 1)%N;
int[] dist = new int[N];
ListSeries len = new ListSeries();
for (int trial = 0; trial < T; ++ trial)
{
for (int i = 0; i < N; ++ i)
{
RandomSubset rs = new RandomSubset (prng, N)
.remove (i) .remove ((i + 1)%N);
for (int j = 1; j <= K; ++ j) conn[i][j] = rs.next();
}
int orig = prng.nextInt (N);
int match = prng.nextInt (N);
len.add (shortestPathLength (conn, dist, orig, match));
}
System.out.printf ("Query path length:%n");
Series.Stats stats = len.stats();
System.out.printf ("Mean = %.2f%n", stats.mean);
System.out.printf ("Stddev = %.2f%n", stats.stddev);
Series.RobustStats rstats = len.robustStats();
System.out.printf ("Median = %.0f%n", rstats.median);
System.out.printf ("90%% ci = %.0f .. %.0f%n",
rstats.quantile (0.05), rstats.quantile (0.95));
}
private static int shortestPathLength
(int[][] conn,
int[] dist,
int orig,
int match)
{
if (orig == match) return 0;
Arrays.fill (dist, -1);
dist[orig] = 0;
LinkedList<Integer> queue = new LinkedList<Integer>();
queue.addLast (orig);
for (;;)
{
int i = queue.removeFirst();
for (int j : conn[i])
{
if (j == match)
{
return dist[i] + 1;
}
else if (dist[j] == -1)
{
dist[j] = dist[i] + 1;
queue.addLast (j);
}
}
}
}
private static void usage()
{
System.err.println
("Usage: java P2Pedia02 <N> <K> <T> <seed>");
System.exit (1);
}
}
|
Alan Kaminsky
|
|
•
|
|
Department of Computer Science
|
|
•
|
|
Rochester Institute of Technology
|
|
•
|
|
4486 +
2220 =
6706
|
|
Home Page
|
Copyright © 2011 Alan Kaminsky.
All rights reserved.
Last updated 31-Aug-2011.
Please send comments to ark@cs.rit.edu.