-
西安电子科技大学
算法大作业
(2016
年度
)
实
验
报
告
实验名称:
算法的设计与分析
班级:
1403019
姓名:
张致远
学号:
西安电子科技大学《算法分析与设计》实验报告
2016
年度
一、实验内容
1.
使用合并
-
查找数据结构,实现估
计渗漏(
Percolation
)问题阈值的程序。
问题描述:
Write a program
to estimate the value of the
percolation threshold
via
Monte Carlo simulation.
Install a Java programming environment.
Install a Java
programming
environment on your computer by following these
step-by-step instructions for your
operating system [
Mac OS
X
·
Windows
·
Linux
]. After following
these instructions, the
commands javac-
algs4 and java-algs4 will classpath in both
and
:
the former contains libraries for reading
data from
standard
input
, writing data to
standard output
, drawing
results to
standard
draw
, generating random numbers,
computing
statistics, and timing
programs; the latter contains all of the
algorithms in the textbook.
Percolation.
Given a
composite systems comprised of randomly
distributed insulating and metallic
materials: what fraction of the
materials need to be metallic so that
the composite system is an
electrical
conductor? Given a porous landscape with water on
the
surface (or oil below), under what
conditions will the water be able
to
drain through to the bottom (or the oil to gush
through to the
surface)? Scientists
have defined an abstract process known as
percolation
to model such
situations.
The model.
We model a percolation system using an
N
-by-
N
grid
of
sites
. Each site is either
open
or
blocked
. A
full
site is an open site
that
can be connected to an open site in the top row
via a chain of
neighboring (left,
right, up, down) open sites. We say the system
percolates
if there is a
full site in the bottom row. In other words, a
system percolates if we fill all open
sites connected to the top row
and that
process fills some open site on the bottom row.
(For the
insulating/metallic materials
example, the open sites correspond to
metallic materials, so that a system
that percolates has a metallic
path
from top to bottom, with full sites conducting.
For the
porous substance example, the
open sites correspond to empty
西安电子科技大学《算法分析与设计》实验报告
2016
年度
space through which water might flow,
so that a system that
percolates lets
water fill open sites, flowing from top to
bottom.)
The problem.
In a
famous scientific problem, researchers are
interested in the following question:
if sites are independently set
to be
open with probability
p
(and
therefore blocked with
probability 1
?
p
),
what is the probability that the system
percolates? When
p
equals 0, the system does not
percolate; when
p
equals 1,
the system percolates. The plots below show the
site
vacancy probability
p
versus the percolation probability for
20-by-
20 random grid (left) and
100-by-100 random grid (right).
When
N
is sufficiently large, there is a
threshold
value
p
* such that
when
p
<
p
*
a random
N
-by-
N
grid almost never percolates, and
when
p
>
p
*, a random
N
-by-
N
grid almost always percolates. No
mathematical solution for determining
the percolation threshold
p
*
has yet been derived. Your task is to
write a computer program to
estimate
p
*.
西安电子科技大学《算法分析与设计》实验报告
2016
年度
Percolation data type.
To
model a percolation system, create a
data type Percolation with the
following API:
西安电子科技大学《算法分析与设计》实验报告
2016
年度
public class Percolation {
public Percolation(int N)
public void open(int i, int
j)
public boolean isOpen(int
i, int j) public boolean isFull(int i, int j)
public boolean percolates()
public static void main(String[] args)
}
//
create N-by-N grid, with all sites blocked
// open site (row i, column
j) if it is not already // is site (row i,
column j) open?
// is site (row i, column j) full? //
does the system percolate?
// test client, optional
By convention, the row and column
indices
i
and
j
are integers
between 1 and
N
, where (1, 1) is the
upper-left site: Throw an
IndexOutOfBoundsException if any
argument to open(),
isOpen(), or
isFull() is outside its prescribed range. The
constructor should throw an
IllegalArgumentException if
N
≤
0.
The constructor should take time
proportional to
N
2
; all methods
should take constant time plus a
constant number of calls to the
union-
find methods union(), find(), connected(), and
count().
Monte Carlo
simulation.
To estimate the percolation
threshold,
consider the following
computational experiment:
?
Initialize all sites to be blocked.
?
Repeat the
following until the system percolates:
o
Choose a site
(row
i
, column
j
) uniformly at random among
all
blocked sites.
西安电子科技大学《算法分析与设计》实验报告
2016
年度
o
Open the site (row
i
, column
j
).
?
The fraction of sites that
are opened when the system percolates
provides an estimate of the
percolation threshold.
For example, if sites are opened in a
20-by-20 lattice according to
the
snapshots below, then our estimate of the
percolation
threshold is 204/400 = 0.51
because the system percolates when
the
204th site is opened.
50
open sites 100 open sites 150 open sites 204 open
sites
By
repeating this computation experiment
T
times and averaging
the
results, we obtain a more accurate estimate of the
percolation
threshold. Let
x
t
be the
fraction of open sites in computational
experiment
t
. The
sample mean
μ
provides an
estimate of the
percolation threshold;
the sample standard deviation
σ
measures
the sharpness of
the threshold.
Assuming
T
is sufficiently large
(say, at least 30), the following
provides a 95% confidence interval for
the percolation threshold:
To perform a series of computational
experiments, create a data
type
PercolationStats with the following API.
public class
PercolationStats {
public
PercolationStats(int N, int T) // perform T
independent
computational experiments
on
an N-by-N grid
西安电子科技大学《算法分析与设计》实验报告
2016
年度
}
public double mean()
public double stddev()
public double confidenceLo() public
double confidenceHi() public
static
void main(String[] args)
//
sample mean of percolation threshold
// sample standard deviation of
percolation threshold // returns
lower
bound of the 95% confidence interval // returns
upper
bound of the 95% confidence
interval
// test client,
described below
The
constructor should throw a
lArgumentException if either
N
≤
0 or
T
≤
0.
Also, include a main()
method that takes two
command-line
arguments
N
and
T
, performs
T
independent computational experiments
(discussed above) on an
N
-by-
N
grid, and prints out the mean,
standard deviation, and the
95% confidence interval
for
the
percolation threshold. Use
standard random
from our
standard
libraries to generate random
numbers; use
standard statistics
to
compute the sample mean
and standard deviation.
分析设计:本题我的思路是构建
一个
N*N
的网格,开始将网格设计的全空,这
样必然无法渗漏。写一个
while
循环调用
unionfind
来判断改系统是否渗漏,如
果没有渗漏,则调用
isopen
函数来随即打开一个格子,再
次判断,直到能够渗
漏为止,记下打开格子的数目,利用大量的实验样本来求出渗漏的置
信区间
核心代码如下:
package
Al1;
import
ption;
西安电子科技大学《算法分析与设计》实验报告
2016
年度
/**
*
*
@author
zhangzhiyuan
*
*/
//
异常抛出
publicclass
IlleagalArgumentException
extends
IOException
{
privatestaticf
inallong
serialVersionUID
=
1L;
IlleagalArgumentException()
{
}
}
System.
out
.println(
输入的行列值不符合要求
p>
);
(0);
此为异常保护。
package
Al1;
publicclass
UnionFind
//
加权带压缩路径
quick-
union
{
privateint
[]
id
;
privateint
count
;
privateint
openco
unt
=0;
privateint
[]
sz
;
privateboolean
[]
openflag
;
privateint
scale
;
public
UnionFind(
int
N
)
{
scale
=
N
;
N
=
N
*
N
;
count
=
N
;
id
=
newint
[
N
+2];
sz
=
newint
[
N
+2];
openflag
=
newboo
lean
[
N
+2];
for
(
int
i
=0;
i
<
N
+2;
i
++)
{
<
/p>
id
[
i
]=<
/p>
i
;
sz
[
i<
/p>
]=1;
openflag
[
i
] =
false
;
}
<
/p>
openflag
[
N
< br>] =
true
;
openflag
[
N
+1] =
true
;
}
publicvoid
open(
int
i
,
int
j
)
{
int
index
= (
i
-1)*
this
.<
/p>
scale
+
j
-1;
//
在数组中的下标
openflag
[
index
] =
true
;
西安电子科技大学《算法分析与设计》实验报告
2016
年度
opencount
++;
count
++;
if
(
i
==1)
//
第一行置
full
状态
{
union(
p>
index
,
scale
< br>*
scale
);
//
将此节点与最后一个
if
(
j
==1)
{
p>
if
(
openflag
< br>[
index
+1])
+1
节点相连
union(
index
,
index
+1);
/
/
右
if
(<
/p>
openflag
[
index
+
scale
])
u
nion(
index
,
index<
/p>
+
scale
);
//
下
}
elseif
(
j
==
scale
)
{
if
(<
/p>
openflag
[
index
-1]) union(
index
,
index
-
1);
/
/
左
if
(<
/p>
openflag
[
index
+
scale
])
u
nion(
index
,
index<
/p>
+
scale
);
//
下
}
else
{
if
(
openflag
[
index
+1])
u
nion(
index
,
index<
/p>
+1);
//
右
if<
/p>
(
openflag
[
< br>index
-1]) union(
index
,
index
-
1);
//
左
if<
/p>
(
openflag
[
< br>index
+
scale
])
union(
index
,
index
+
scale
);
//
下
}
}
elseif
(
i
==
this
.
scale
)
//
如果是打开的最后一行,判断上左右是
{
union(
index
,
scale
*
scale
+1);
if
(
j<
/p>
==1)
{
if
(<
/p>
openflag
[
index
+1])
否
full
union(
index
,
index
+1);
//
右
if<
/p>
(
openflag
[
< br>index
-
scale
])
union(
index
,
index
-
scale
);
//
上
}
els
eif
(
j
==
scale
)
{
if<
/p>
(
openflag
[
< br>index
-1]) union(
index
,
index
-
1);
//
左
if<
/p>
(
openflag
[
< br>index
-
scale
])
union(
index
,
index
-
scale
);
//
上
西安电子科技大学《算法分析与设计》实验报告
2016
年度
}
else
{
if
< br>(
openflag
[
inde
x
+1])
union(
index
,
index
+1);
//
右
if
(<
/p>
openflag
[
index
-1]) union(
index
,
index
-
1);
/
/
左
if
(<
/p>
openflag
[
index
-
scale
])
u
nion(
index
,
index<
/p>
-
scale
);
//
上
}
}
else
{
if<
/p>
(
j
==1)
/
/
判断右上下是否
full
{
if
(
openflag
[
index
+1])
union(
index
,
index
+1);
//
右
if<
/p>
(
openflag
[
< br>index
+
scale
])
union(
index
,
index
+
scale
);
//
下
if<
/p>
(
openflag
[
< br>index
-
scale
])
union(
index
,
index
-
scale
);
//
上
}
elseif
(
j
==
this
.
scale
)
//
判断左上下是否
full
{
if
(
openflag
[
index
-1])
union(
index
,
index
-1);
//
左
if<
/p>
(
openflag
[
< br>index
+
scale
])
union(
index
,
index
+
scale
);
//
下
if<
/p>
(
openflag
[
< br>index
-
scale
])
union(
index
,
index
-
scale
);
//
上
}
p>
else
//
判断上下左右是否
full
{
if
(
op
enflag
[
index
-1])
union(
index
,
index
-
if
(<
/p>
openflag
[
index
+1])
1);
//
左
union(
index
,
index
+1);
//
右
if
(<
/p>
openflag
[
index
+
scale
])
u
nion(
index
,
index<
/p>
+
scale
);
//
下
if
(
op
enflag
[
index
-
scale
]) union(
index
p>
,
index
-
s
cale
);
//
上
< br>
}
}
}
publicboolean
isopen(
int
i
,
int
j
)
//throws
IndexOutOfBoundsException
西安电子科技大学《算法分析与设计》实验报告
2016
年度
}
{
}
int
index
= (
i
-1)*
this
.<
/p>
scale
+
j
-1;
//
在数组中的下标
return
openflag
[
index
];
publicint
sitenum()
{
return
(
scale
*
scale
);
}
publicint
opencount()
{
return
opencount
;
}
publicboolean
connected(
int
p
,
int
p>
q
)
{
return
find(
p
) ==
find(
q
);
}
publicint
find(
int
p
)
{
while
(
p
!=
id
[
p
])
{
id
[
p
]
=
id
[
id
[
p
]];
p
=
id<
/p>
[
p
];
}
return
p
;
}
publicvoid
union(
int
p
,
int
q
)
{
int
i
=
find(
p
);
int
j
=
find(
q
);
if
(
i
==
j
)
return
;
if<
/p>
(
sz
[
i
p>
] <
sz
[
j<
/p>
])
{
id
[
i
]
=
j
;
sz
[
j
]
+=
sz
[
i
];
}
else
{
id
[
j
]
=
i
;
sz
[
i
]
+=
sz
[
j
];
}
count
--;
}
上述代码提供了
unionfind
对系统渗漏的判
断
package
Al1;
import
r;
西安电子科技大学《算法分析与设计》实验报告
2016
年度
publicclass
PercolationStats
{
privatedouble
[]
mean
;
public
PercolationStats(
int
N
,
int
p>
T
)
throws
IlleagalArgumentException
{
if
(
N
<=0||
T
<=0)
{
thrownew
IllegalArgumentException();
}
int
i
,
j
;
mean
=
newdouble<
/p>
[
T
];
for
(
i
nt
loop
=0;
loop
<
T
;
loop
++)
{
{
UnionFind
p
=
new
UnionFind(
N
);
while
(!
p
.connected(
N<
/p>
*
N
,
N
*
N
+1))
{
do
{
i
= (
int
)(()*
N
)+1;
j
= (
int
)(()*
N
)+1;
没有被打开的
p>
p
.open(
i
,
j
);
}
while
(
p
.isopen(
i
,
j
));
//
打开了
,<
/p>
就继续找一个
//
n(
了
i:
j:
}
p>
mean
[
loop
] =
(
double
)
p
.opencount()/(
double
)
p
.sitenum();
}
}
}
publicdouble
mean()
// percolation
threshold
的样本均值
{
double
temp
= 0.0;
for
(
d
ouble
e
:
this
.
mean
)
{
temp
+=
e
;
}
return
temp
/
this
.
mean
p>
.
length
;
}
publicdouble
stddev()
// percolation
threshold
的样本标准偏差
{
double
temp
=0;
西安电子科技大学《算法分析与设计》实验报告
2016
年度
}
double
temp_mean
=
this
.mean();
for<
/p>
(
double
e
:
this
.
mean
)
{
temp
+= (
e
-
temp_mean
)*(
e
-
temp_mean
);
}
return
(
temp
/(
mean
.
p>
length
-1));
publicdouble
confidenceLo()<
/p>
//
返回
95
%
置信区间的下限
{
return
(
this
.mean() -
1.96*(stddev())/(
mean
.
length
));
}
publicdouble
confidenceHi()<
/p>
//
返回
95
%
置信区间的上界
{
return
(
this
.mean() +
1.96*(stddev())/(
mean
.
length
));
}
publicstaticvoid
main(String []
args
)
< br>throws
IlleagalArgumentException
//
测试客户端,如下所述
{
System.
out
.print(
请输入
N*
N
网格的边长
N:n
);
Scanner
scan
=
new
Scanner(System.
in
);
< br>int
N
,
T
< br>;
N
=
scan
.nextInt();
System.
out
.print(
请输入实验的次数
< br>T:n
);
T
=
scan
.nextInt();
p>
if
(
T
<=0|
|
N
<=0)
{
thrownew
IlleagalArgumentException();
}
PercolationStats
pe
=
new
PercolationStats(
N
,
T
);
scan
.close();
System.
< br>out
.println(
+<
/p>
pe
.mean()+
< br>+
=
+
pe
.stddev());
System.
< br>out
.println(
=<
/p>
+
pe
.confidenceHi()
+
=
+
pe<
/p>
.confidenceLo()+
);
}
}
模拟渗漏。
实验结果如下:
西安电子科技大学《算法分析与设计》实验报告
2016
年度
2.
对排序算法的实现并做性能分析
比较
问题描述:
Analysis
of running time and memory usage (optional and not
graded).
Implement the
Percolation data type using the
quick-
find algorithm
from .
?
Use the
stopwatch data type
from our standard library to
measure the total running time of
PercolationStats. How
does doubling
N
affect the total running
time? How does
doubling
T
affect the total running time? Give a
formula
(using tilde notation) of the
total running time on your
computer (in
seconds) as a single function of both
N
and
T
.
?
Using the
64-bit memory-cost model from lecture, give
the total memory usage in bytes (using
tilde notation) that a
Percolation
object uses to model an
N
-by-
N
percolation
system. Count
all memory that is used, including memory for
the union-find data structure.
Now, implement the
Percolation data type using the
weighted quick-union algorithm
from . Answer the
questions in the previous paragraph.
Deliverables.
Submit only
(using the weighted
quick-union
algorithm as implemented in the
WeightedQuickUnionUF class) and . We
will
西安电子科技大学《算法分析与设计》实验报告
2016
年度
supply and WeightedQuickUnionUF. Your
submission
may not call any library
functions other than those in ,
, and
WeightedQuickUnionUF.
For
fun.
Create your own percolation input
file and share it in the
discussion
forums. This assignment was developed by Bob
Sedgewick and Kevin Wayne.
2. You are asked to write and perform
an empirical
complexity analysis of the
following sorting
algorithms://
< br>实现并做性能分析比较
(1) Insertion sort;
(2) Merge sort (please write the
recursive merge sort and bottom-
up
mergesort); (3) Quick sort (please write a
randomized quicksort
method as covered
in class); (4) Quicksort with Dijkstra 2-way
partitioning;
(5)* Quicksort with Bentley-McIlroy
3-way partitioning.
Note
that the question with a star is optional.
问题分析:由于本人没有找到所谓的
Quicksort
with Dijkstra 2-way partitioning;
所以没有写这个代码,看了一下我们的算法书,感觉我们算法书上的排序算法
写的十分巧妙,并且可读性强,就讲书上的代码抄下,统统放到
sortClass<
/p>
类中
,并且在
test
< br>程序中对每一个算法进行调用,以比较他们之间的不同。
核心代码:
package
Al2;
import
dom;
publicclass
SortClass
{
privatestatic
Comparable[]
aux
;
publicstaticvoid
insertSort(Comparable[]
a
)
{
int
N
=
a
.
length
;
for
(
int
i
= 1;
< br>i
<
N
;
i
++)
{
<
/p>
for
(
int
j
=
i
;
j<
/p>
>0 && less(
a
[
j
],
a
[
j
-1]);
j
--)
{
p>
exch(
a
,
j
,
j
-1);
}
西安电子科技大学《算法分析与设计》实验报告
2016
年度
}
}
//
插入
排序
publicstaticvoid
mergeSort_ud(Comparable[]
a
)
{
aux
=
new
Comparable[
a<
/p>
.
length
];
mergeSort_ud(
a
p>
,0,
a
.
len
gth
-1);
}
//
合并
排序
,
自顶向下
publicstaticvoid
mergeSort_ud(Comparable[]
a
,
int
lo
,
int
hi
)
{
<
/p>
if
(
hi
<=
lo
)
return
int
mid
=
lo
+(
hi
-
lo
)/2;
mergeSort_ud(
a
p>
,
lo
,
mid<
/p>
);
me
rgeSort_ud(
a
,
mid<
/p>
+1,
hi
);
merge(
a
,
lo
,
mid
,
hi
);
}
publicstaticvoid
merge(Comparable[]
a
,
int
lo
,
int
mid
,
int
hi
)
{
int
i
=
lo
,
j
=
mid
+1;
for
(
i
nt
k
=
lo
;
k
<=
hi
;
k
++)
aux
[
k
]
=
a
[
k
];
for
(
i
nt
k
=
lo
;
k
<=
hi
;
k
++)
{
if
(
i
>
mid
)
a
[
k
] =
aux
[
j
+
+];
elseif
(
j
>
hi
)
a
[
k
] =
aux
[
i
+
+];
elseif
(less(
aux
[
j
],
aux
[
i
]))
a
[
k
] =
aux
[
j
++];
else
a
[
k
]
=
aux
[
i
++];
}
}
publicvoid
mergeSort_du(Comparable[]
a
)
{
int
N
=
a
.
length
;
aux
=
new
Comparable[
N
];
for
(
int
sz
= 1;
sz
<
N
;
< br>sz
+=
sz
)
//sz
p>
for
(
int
l
o
=0;
lo
<
N
-
sz
;
lo
+=
sz
+
sz
)
merge(
a
,
lo
,
lo
+
sz
-
1,(
< br>lo
+
sz
+
< br>sz
-1,
N
-1));
}
p>
//
合并排序,自底向上
privatestaticboolean
less(Co
mparable
v
,Comparable
< br>w
)
{
-
-
-
-
-
-
-
-
-
上一篇:NLP前提假设十二条
下一篇:研究生英语外研社上翻译