《Algorithm Part I:Union-Find》

1.动态联通性问题描述:

有N个元素,开始时我们让每一个元素肚子构成一个集合。然后按一定的顺序将属于同一组中的元素合并,构成新的集合。其间要反复查询某个元素在哪个集合中。如下所示:

解决办法:

(1)Quick-Find

声明一个长度为N的数组id,数组中元素的值代表它所属组的编号。将数组中的元素初始化为每个元素的索引值,这样就表示开始时每个元素各自构成一个独立的集合。每次union(i,j)的时候就将所有组编号等于id[i]的元素的组编号变为id[j]。每次查询元素i的组编号时,返回id[i]即可。

实现:

时间复杂度分析:union操作的时间复杂度为O(n),find操作的时间复杂度为O(1)

(2)Quick-Union

同样是声明一个长度为N的int型数组id。但与Quick-Find方法不同的是,Quick-Union让每一个集合中的元素构成一棵树,每一个元素对应的id存的是自己在树上的父节点。在执行union(i,j)操作时,将i元素所在树的根指向j所在元素的根。在查询元素i的id时,返回元素i所在集合的树的根节点index即可。

实现:

时间复杂度分析:union操作的时间复杂度为O(n),find的时间复杂度为O(n).

(3)改进的Quick-Union

为了防止构建树的过程中出现tall trees,我们记录每个集合的大小。每次union的时候将小集合对应树的根链接到大集合所对应树的根上。

实现:

时间复杂度分析:union操作的时间复杂度为lg(n),find操作的时间复杂度为lg(n).

2.Programming Assignments

问题描述:

Programming Assignment 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 stdlib.jar and algs4.jar: the former contains libraries for reading data from standard
input
, writing data tostandard 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 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*.

Percolation data type. To model a percolation system, create a data type Percolation with the following API:

public class Percolation {
   public Percolation(int N)                // create N-by-N grid, with all sites blocked
   public void open(int i, int j)           // open site (row i, column j) if it is not already
   public boolean isOpen(int i, int j)      // is site (row i, column j) open?
   public boolean isFull(int i, int j)      // is site (row i, column j) full?
   public boolean percolates()              // does the system percolate?
   public static void main(String[] args)   // 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 N2; 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:
    • Choose a site (row i, column j) uniformly at random among all blocked sites.
    • 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 xt 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
   public double mean()                     // sample mean of percolation threshold
   public double stddev()                   // sample standard deviation of percolation threshold
   public double confidenceLo()             // returns lower bound of the 95% confidence interval
   public double confidenceHi()             // returns upper bound of the 95% confidence interval
   public static void main(String[] args)   // test client, described below
}

The constructor should throw a java.lang.IllegalArgumentException 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.

% java PercolationStats 200 100
mean                    = 0.5929934999999997
stddev                  = 0.00876990421552567
95% confidence interval = 0.5912745987737567, 0.5947124012262428

% java PercolationStats 200 100
mean                    = 0.592877
stddev                  = 0.009990523717073799
95% confidence interval = 0.5909188573514536, 0.5948351426485464

% java PercolationStats 2 10000
mean                    = 0.666925
stddev                  = 0.11776536521033558
95% confidence interval = 0.6646167988418774, 0.6692332011581226

% java PercolationStats 2 100000
mean                    = 0.6669475
stddev                  = 0.11775205263262094
95% confidence interval = 0.666217665216461, 0.6676773347835391

Analysis of running time and memory usage (optional and not graded). Implement the Percolation data type using the quick-find algorithm QuickFindUF.java from algs4.jar.

  • 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 WeightedQuickUnionUF.java from algs4.jar.
Answer the questions in the previous paragraph.

Deliverables. Submit only Percolation.java (using the weighted quick-union algorithm as implemented in the WeightedQuickUnionUF class) and PercolationStats.java. We will
supply stdlib.jar and WeightedQuickUnionUF. Your submission may not call any library functions other than those in java.langstdlib.jar, and WeightedQuickUnionUF.

For fun. Create your own percolation input file and share it in the discussion forums. For some inspiration, see these nonogram
puzzles
.

This assignment was developed by Bob Sedgewick and Kevin Wayne.

Copyright ? 2008.

代码:

Percolation类

public class Percolation {

	private UF uf;
	private int N;
	private int [][] map;

	public Percolation(int N)
	{
		if (N <= 0)
			throw new IllegalArgumentException();
		this.N = N;
		uf = new UF((N+2)*(N+2));
		map = new int[N+2][N+2];
		map[0][0] = 1;
		map[N+1][0] = 1;
		int i;
		for(i = 1;i <= N;i++)
		{
			int index1 = N+2+i;
			int index2 = 0;
			uf.union(index1, index2);
		}
		for(i = 1;i <= N;i++)
		{
			int index1 = N*(N+2)+i;
			int index2 = (N+1)*(N+2);
			uf.union(index1, index2);
		}
	}

	public void open(int i,int j)
	{
		if(i < 1 || i > N || j < 1 || j > N)
			throw new IndexOutOfBoundsException();
		int index1 = i*(N+2)+j;
		int index2;
		if(map[i][j] == 1)
			return;
		map[i][j] = 1;
		if(i-1 >= 1)
		{
			if(map[i-1][j] == 1)
			{
				index2 = (i-1)*(N+2)+j;
				uf.union(index1, index2);
			}
		}
		if(i+1 <= N)
		{
			if(map[i+1][j] == 1)
			{
				index2 = (i+1)*(N+2)+j;
				uf.union(index1, index2);
			}
		}
		if(j-1 >= 1)
		{
			if(map[i][j-1] == 1)
			{
				index2 = i*(N+2)+j-1;
				uf.union(index1, index2);
			}
		}
		if(j+1 <= N)
		{
			if(map[i][j+1] == 1)
			{
				index2 = i*(N+2)+j+1;
				uf.union(index1, index2);
			}
		}
	}

	public boolean isOpen(int i,int j)
	{
		if(i < 1 || i > N || j < 1 || j > N)
			throw new IndexOutOfBoundsException();
		if(map[i][j] == 1)
			return true;
		else
			return false;
	}

	public boolean isFull(int i,int j)
	{
		if(i < 1 || i > N || j < 1 || j > N)
			throw new IndexOutOfBoundsException();
		if(N == 1)
			return isOpen(1,1);
		int k;
		int index1 = i*(N+2)+j;
		int index2 = 0;
		if(isOpen(i,j))
			return uf.connected(index1, index2);
		else
			return false;
	}

	public boolean percolates()
	{
		if(N == 1)
			return isOpen(1,1);
		int index1 = 0;
		int index2 = (N+1)*(N+2);
		return uf.connected(index1, index2);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//System.out.print("ni hao");
		Percolation p = new Percolation(5);
		//p.open(1, 1);
		//p.open(1, 2);
		p.open(1, 3);
		p.open(2, 3);
		p.open(3, 2);
		p.open(2, 2);
		p.open(5, 1);
		p.open(4, 1);
		p.open(3, 1);
		for(int i = 1;i <= 5;i++)
		{
			for(int j = 1;j <=5;j++)
				System.out.print(p.map[i][j]+" ");
			System.out.print("\n");
		}
		for(int i = 1;i <= 5;i++)
		{
			for(int j = 1;j <=5;j++)
				System.out.print(p.isFull(i,j)+" ");
			System.out.print("\n");
		}
		System.out.print(p.percolates());
	}

}

PercolationStats类

import java.util.Random;

public class PercolationStats {

	private int N;
	private int T;
	private long []count;

	public PercolationStats(int N, int T)
	{
		if(N <= 0 || T <= 0)
			throw new IllegalArgumentException();
		count = new long[T];
		this.N = N;
		this.T = T;
		for(int k = 0;k < T;k++)
		{
			Percolation p = new Percolation(N);
			while(!p.percolates())
			{
				//Random random = new Random();
				int i = (int) ((Math.random()*N)+1);
				int j = (int) ((Math.random()*N)+1);
				if(!p.isOpen(i, j))
				{
					p.open(i, j);
					count[k]++;
				}
			}
		}
	}

	public double mean()
	{
		double u;
		long sum = 0;
		for(int i = 0;i < T;i++)
			sum += count[i];
		u = (sum*1.0/T)/(N*N);
		return u;
	}

	public double stddev()
	{
		double u = mean();
		double sum = 0;
		for(int i = 0;i < T;i++)
		{
			double x = count[i]*1.0/(N*N);
			sum = sum + (x-u)*(x-u);
		}
		double stddev = Math.sqrt(sum/(T-1));
		return stddev;
	}

	public double confidenceLo()
	{
		double u = mean();
		double stddev = stddev();
		double lo = u - (1.96*stddev/Math.sqrt(T));
		return lo;
	}

	public double confidenceHi()
	{
		double u = mean();
		double stddev = stddev();
		double hi = u + (1.96*stddev/Math.sqrt(T));
		return hi;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//System.out.print("hello");
		int N = Integer.parseInt(args[0]);
		int T = Integer.parseInt(args[1]);
		PercolationStats p = new PercolationStats(N,T);
		System.out.print("mean:\t\t\t\t\t");
		System.out.print("= "+p.mean()+"\n");
		System.out.print("stddev:\t\t\t\t\t");
		System.out.print("= "+p.stddev()+"\n");
		System.out.print("95% confidence interval\t\t\t");
		System.out.print("= "+p.confidenceLo()+",");
		System.out.print(p.confidenceHi()+"\n");
	}

}
时间: 2024-08-05 12:04:35

《Algorithm Part I:Union-Find》的相关文章

《Algorithm in C》by Sedgewick 读书笔记

Update: July 4, 2014 Chap 5: At the beginning, he mentioned that: recursion is a divide-and-conquer method. Although many algorithms can be solved in simple recursion but it is often an equally algorithm lies in the details of a nonrecursive implemen

《algorithm puzzles》——概述

这个专题我们开始对<algorithm puzzles>一书的学习,这本书是一本谜题集,包括一些数学与计算机起源性的古典命题和一些比较新颖的谜题,序章的几句话非常好,在这里做简单的摘录. 手里拿着一把锤子,看什么都像钉子.我们这个年代最厉害的锤子就是算法.——William Poundstone.  解题是一种实用技能,怎么说呢,有点像游泳吧.我们学习任何使用技能的办法就是模仿和实践.——George Polya.  如果想使得上课不那么无聊,那么没有比加入带有创造力的主题更好的办法了,这些主

《深入理解Android 卷III》第五章 深入理解Android输入系统

<深入理解Android 卷III>即将公布.作者是张大伟.此书填补了深入理解Android Framework卷中的一个主要空白.即Android Framework中和UI相关的部分.在一个特别讲究颜值的时代,本书分析了Android 4.2中WindowManagerService.ViewRoot.Input系统.StatusBar.Wallpaper等重要"颜值绘制/处理"模块 第5章 深入理解Android输入系统(节选) 本章主要内容: ·  研究输入事件从设

《MySQL 必知必会》读书总结

这是 <MySQL 必知必会> 的读书总结.也是自己整理的常用操作的参考手册. ? ? 使用 MySQL 连接到 MySQL shell>mysql -u root -p Enter password:****** 显示数据库 mysql>SHOW DATABASES; 选择数据库 mysql>USE mytest; 显示数据库中的表 mysql>SHOW TABLES; 显示表列 mysql>SHOW COLUMNS FROM tmall_user; mysql

《MySQL必知必会》读书笔记_3

PS:这次的信息量有点大. 聚集不同值 SELECT AVG(DISTINCT prod_price) AS avg_price FROM products WHERE vend_id = 1003 #相同的值不会被计算 组合聚集函数 SELECT COUNT(*) AS num_items, MIN(prod_price) AS price_min, MAX(prod_price) AS price_max, AVG(prod_price) AS price_avg FROM products

《Mastering Opencv ...读书笔记系列》车牌识别(I)

http://blog.csdn.net/jinshengtao/article/details/17883075/  <Mastering Opencv ...读书笔记系列>车牌识别(I) http://blog.csdn.net/jinshengtao/article/details/17954427   <Mastering Opencv ...读书笔记系列>车牌识别(II) Mastering Opencv ...读书笔记系列>车牌识别(I) 标签: 车牌分割svm西

《计算机算法设计与分析》v4 第1章 算法概述 算法实现题答案

博主今年刚上大三,正好开算法这门课.由于博主本人比较喜欢算法但又比较懒,啃不动算法导论,所以决定拿这本书下手. 这本书是王晓东的第四版<计算机算法设计与分析>.初步打算将每章后面的算法题都用代码实现. 有些题跟某个ACM题目很像,我会把该ACM题的链接贴上.有的题没OJ交所以可能是错的.如有发现,还望指出. 1-1 统计数字问题 http://poj.org/problem?id=2282 这个题要按位分解,一位一位的来处理. #include<iostream> #include

《Linux内核设计与实现》读书笔记(十二)- 内存管理

内核的内存使用不像用户空间那样随意,内核的内存出现错误时也只有靠自己来解决(用户空间的内存错误可以抛给内核来解决). 所有内核的内存管理必须要简洁而且高效. 主要内容: 内存的管理单元 获取内存的方法 获取高端内存 内核内存的分配方式 总结 1. 内存的管理单元 内存最基本的管理单元是页,同时按照内存地址的大小,大致分为3个区. 1.1 页 页的大小与体系结构有关,在 x86 结构中一般是 4KB或者8KB. 可以通过 getconf 命令来查看系统的page的大小: [[email prote

《Neural networks and deep learning》概览

最近阅读了<Neural networks and deep learning>这本书(online book,还没出版),算是读得比较仔细,前面几章涉及的内容比较简单,我着重看了第三章<Improving the way neural networks learn>,涉及深度神经网络优化和训练的各种技术,对第三章做了详细的笔记(同时参考了其他资料,以后读到其他相关的论文资料也会补充或更改),欢迎有阅读这本书的同学一起交流.以下属个人理解,如有错误请指正. What this bo

《深入理解LINUX网络内幕》读书笔记1

一个在线看Linux源码的网站http://lxr.free-electrons.com,需要翻墙 第二章 关键数据结构 本章讲述以下两个重要数据结构: struct sk_buff:存储所有网络分层的包头.有效载荷,其他内部信息 struct net_device:网络设备的通用数据结构 sk_buff 布局: 书上sk_buff 写到的对分层头的设计是以union形式来表示,这是Linux 2.6.24之前的方式          /* Transport layer header */