Answer:
Step-by-step explanation:
package net.qiguang.algorithms.C1_Fundamentals.S5_CaseStudyUnionFind;
import java.util.Random;
/**
* 1.5.20 Dynamic growth.
* Using linked lists or a resizing array, develop a weighted quick-union implementation that
* removes the restriction on needing the number of objects ahead of time. Add a method newSite()
* to the API, which returns an int identifier
*/
public class Exercise_1_5_20 {
public static class WeightedQuickUnionUF {
private int[] parent; // parent[i] = parent of i
private int[] size; // size[i] = number of sites in subtree rooted at i
private int count; // number of components
int N; // number of items
public WeightedQuickUnionUF() {
N = 0;
count = 0;
parent = new int[4];
size = new int[4];
}
private void resize(int n) {
int[] parentCopy = new int[n];
int[] sizeCopy = new int[n];
for (int i = 0; i < count; i++) {
parentCopy[i] = parent[i];
sizeCopy[i] = size[i];
}
parent = parentCopy;
size = sizeCopy;
}
public int newSite() {
N++;
if (N == parent.length) resize(N * 2);
parent[N - 1] = N - 1;
size[N - 1] = 1;
return N - 1;
}
public int count() {
return count;
}
public int find(int p) {
// Now with path compression
validate(p);
int root = p;
while (root != parent[root]) {
root = parent[root];
}
while (p != root) {
int next = parent[p];
parent[p] = root;
p = next;
}
return p;
}
// validate that p is a valid index
private void validate(int p) {
if (p < 0 || p >= N) {
throw new IndexOutOfBoundsException("index " + p + " is not between 0 and " + (N - 1));
}
}
public boolean connected(int p, int q) {
return find(p) == find(q);
}
public void union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) {
return;
}
// make smaller root point to larger one
if (size[rootP] < size[rootQ]) {
parent[rootP] = rootQ;
size[rootQ] += size[rootP];
} else {
parent[rootQ] = rootP;
size[rootP] += size[rootQ];
}
count--;
}
}
public static void main(String[] args) {
WeightedQuickUnionUF uf = new WeightedQuickUnionUF();
Random r = new Random();
for (int i = 0; i < 20; i++) {
System.out.printf("\n%2d", uf.newSite());
int p = r.nextInt(i+1);
int q = r.nextInt(i+1);
if (uf.connected(p, q)) continue;
uf.union(p, q);
System.out.printf("%5d-%d", p, q);
uf.union(r.nextInt(i+1), r.nextInt(i+1));
}
}
}
Look at the data set. What is the median?<br><br>
3, 3, 3, 4, 4, 4, 5, 7, 8, 8, 8, 9, 10, 12, 14
Ber [7]
THESE ARE JUST SOME EXAMPLES! I HOPE SOME OF THEM CAN HELP YOU SOLVE UR QUESTION!
Problem: The Doran family has 5 children, aged 9, 12, 7, 16 and 13. What is the age of the middle child? [IMAGE]
Solution: Ordering the childrens' ages from least to greatest, we get:
7, 9, 12, 13, 16
Answer: The age of the middle child is the middlemost number in the data set, which is 12.
In the problem above, we found the median of a set of 5 numbers.
Definition: The median of a set of data is the middlemost number in the set. The median is also the number that is halfway into the set. To find the median, the data should first be arranged in order from least to greatest.
To remember the definition of a median, just think of the median of a road, which is the middlemost part of the road. In the problem above, 12 is the median: it is the number that is halfway into the set. There are two children older than 12 and two children younger than 12. Let's look at some more examples.
Example 1: The Jameson family drove through 7 states on their summer vacation. Gasoline prices varied from state to state. What is the median gasoline price? [IMAGE]
$1.79, $1.61, $1.96, $2.09, $1.84, $1,75, $2.11
Solution: Ordering the data from least to greatest, we get:
$1.61, $1.75, $1.79, $1.84, $1.96, $2.09, $2.11
Answer: The median gasoline price is $1.84. (There were 3 states with higher gasoline prices and 3 with lower prices.)
Example 2: During the first marking period, Nicole's math quiz scores were 90, 92, 93, 88, 95, 88, 97, 87, and 98. What was the median quiz score? [IMAGE]
Solution: Ordering the data from least to greatest, we get:
87, 88, 88, 90, 92, 93, 95, 96, 98
Answer: The median quiz score was 92. (Four quiz scores were higher than 92 and four were lower.)
In each of the examples above, there is an odd number of items in each data set. In Example 1, there are 7 numbers in the data set; in Example 2 there are 9 numbers. Let's look at some examples in which there is an even number of items in the data set.
Example 3: A marathon race was completed by 4 participants. What was the median race time? [IMAGE]
2.7 hr, 8.3 hr, 3.5 hr, 5.1 hr
Solution: Ordering the data from least to greatest, we get:
2.7, 3.5, 5.1, 8.3
Since there is an even number of items in the data set, we compute the median by taking the mean of the two middlemost numbers.
3.5 + 5.1 = 8.6
Answer: The median race time was 4.3 hr.
Example 4: The salaries of 8 employees who work for a small company are listed below. What is the median salary?
$40,000, $29,000, $35,500, $31,000, $43,000, $30,000, $27,000, $32,000
Solution: Ordering the data from least to greatest, we get:
$27,000, $29,000, $30,000, $31,000, $32,000, $35,500, $40,000, $43,000
Since there is an even number of items in the data set, we compute the median by taking the mean of the two middlemost numbers.
$31,000 + $32,000 = $63,000