Introduction to Algorithms--Part 1 and 2

Part 1:Foundations

Loop invariants(循环不变式): help judging the rightness of algorithm

Chapter 2: Getting Started

2.1 insert sorting

2.2 analyze algorithm

  1. the worst running time: Θ ( n 2 ) \Theta(n^2) Θ(n2)

2.3 design algorithm

  1. insert sorting using incremental method(增量方法)
    complexity: Θ ( n 2 ) \Theta(n^2) Θ(n2)

  2. divide and conquer(分治法)
    Here is a skill to sort cards using this way. place a sentinel card(哨兵卡) to avoid comparing every times.
    complexity: Θ ( n lg ⁡ n ) \Theta(n\lg^n) Θ(nlgn)

Chapter 3: Growth of Functions

3.1 asymptotic sign(渐近记号)

Θ \Theta Θ: asymptotically tight bound (渐近紧确界), pronounced: Theta
O \Omicron O: asymptotically upper bound (渐近上界), pronounced: O
Ω \Omega Ω: asymptotically lower bound (渐近下界), pronounced: Omega
在这里插入图片描述

Chapter 4: Divide-and-Conquer

4.1 maximum subarray problem

  1. using divide and conquer method: Θ ( n l g n ) \Theta(nlgn) Θ(nlgn)
  2. in some case, only use Θ ( n ) \Theta(n) Θ(n) to solve the problem

4.2 Strassen algorithm of matrix multiplication

  1. In practical, sparse matrix (稀疏矩阵) have special algorithms.
  2. In practical, the fast multiple program of dense matrix use Strassen algorithm when the matrix size exceeds a “intersection” value. Once the size is reduced below the intersection value, program will choose a simpler method.

4.3 Solving recurrence(求解递归式)

  1. Substitution method(代入法)
    guess a bound. Then prove it by mathematical induction(数学归纳法)
  2. Recursive tree method(递归树法)
    Convert recurrence to recursive tree. Then solve it.
  3. Master Theorem(主定理)
    provides a recipe solution for the following form of recurrence:
    T ( n ) = a T ( n / b ) + f ( n ) T(n)=aT(n/b)+f(n) T(n)=aT(n/b)+f(n)
    a ≥ 1 a≥1 a1 and b > 1 b>1 b>1 are constants, f ( n ) f(n) f(n) is asymptotic positive function, T ( n ) T(n) T(n) is a recurrence defined on nonnegative integers.
    == For a constant ϵ > 0 , f ( n ) = O ( n log ⁡ b a − ϵ ) \epsilon>0, f(n)=\Omicron(n^{\log_b^{a-\epsilon}}) ϵ>0,f(n)=O(nlogbaϵ) , then T ( n ) = Θ ( n log ⁡ b a ) T(n)=\Theta(n^{\log_b^a}) T(n)=Θ(nlogba).
    == f ( n ) = Θ ( n log ⁡ b a ) f(n)=\Theta(n^{\log_b^a}) f(n)=Θ(nlogba), then T ( n ) = Θ ( n log ⁡ b a lg ⁡ n ) T(n)=\Theta(n^{\log_b^a}\lg^n) T(n)=Θ(nlogbalgn).
    == For a constant ϵ > 0 , f ( n ) = Ω ( n log ⁡ b a + ϵ ) \epsilon>0,f(n)=\Omega(n^{\log_b^{a+\epsilon}}) ϵ>0,f(n)=Ω(nlogba+ϵ), and, for a constant c < 1 c<1 c<1 and all enough big n, have a f ( n / b ) ≤ c f ( n ) af(n/b)≤cf(n) af(n/b)cf(n), then T ( n ) = Θ ( f ( n ) ) T(n)=\Theta(f(n)) T(n)=Θ(f(n)).

Chapter 5: Probabilistic Analysis and Randomized Algorithms

5.2 Indicator Random Variable(指示器随机变量)

Part 2: Sorting and Order Statistics

在这里插入图片描述

Chapter 6: Heapsort(堆排序)

6.1 Heap

As figure 6-1, (binary) heap is array, it can be regarded as binary tree. The root of tree is A[1], so give a element’s index i, we can calculate its parent / left child / right child node:

PARENT(i)
1	return floor(i / 2)

LEFT(i)
1	return 2i

RIGHT(i)
1	return 2i + 1

在这里插入图片描述

Figure 6-1

binary heap has two forms: max-heap (最大堆) and min-heap (最小堆).

max-heap: all node except root need to satisfy

A[PARENT(i)] ≥ A[i]

min-heap: all node except root node need to satisfy

A[PARENT(i)] ≤ A[i]

The common operations involving heaps are:

  • MAX-HEAPIFY(A, i): complexity O ( l g n ) \Omicron(lgn) O(lgn), key of maintain max-heap property.
  • BUILD-MAX-HEAP(A): complexity O ( n ) \Omicron(n) O(n), function is constructing a max-heap from a disorder array A.
  • HEAPSORT(A): complexity O ( n l g n ) \Omicron(nlgn) O(nlgn), function is sorting a array A in place.
  • MAX-HEAP-INSERT / HEAP-EXTRACT-MAX / HEAP-INCREASE-KEY / HEAP-MAXIMUM: complexity O ( l g n ) \Omicron(lgn) O(lgn), function is use heap to construct a priority-queue

The C++ Standard Library provides the make_heap, push_heap and pop_heap algorithms for heaps.

Chapter 7: Quicksort

Quicksort is usually best choice in practice. The reason is below:

  1. good average performance: expected time complexity Θ ( n l g n ) \Theta(nlgn) Θ(nlgn), and the implicit constant factor is very small.
  2. sorting in place
  3. work well in virtual memory environment

Chapter 8: Sorting in Linear Time

8.3 Counting Sort(计数排序)

Array A[1…n] is input. B[1…n] store the result of sorting, C[0…k] is count array. complexity: Θ ( n ) \Theta(n) Θ(n).

COUNTING-SORT(A, B, k)
1	let C[0..k] = 0 be a new array
2	for j = 1 to A.length
3		C[A[j]] = C[A[j]] + 1
4	// C[i] now contains the number of elements equal to i.
5	for i = 1 to k
6		C[i] = C[i] + C[i-1]
7	// C[i] now contains the number of elements less than or equal to i.
8	for j = A.length downto 1	// note: this is for the stability of counting sort
9		B[C[A[j]]] = A[j]
10	C[A[j]] = C[A[j]] - 1

the figure is the process of above pseudo code.
在这里插入图片描述

8.3 Radix Sort(基数排序)

在这里插入图片描述

8.4 Bucket Sort(桶排序)

在这里插入图片描述
在这里插入图片描述

Chapter 9: Medians and Order Statistics

The i-th order statistic(顺序统计量) is the i-th smallest element of set.

9.1 Minimum and Maximum

When we want to find minimum and maximum, there is a skill that only need 3*floor(n/2) comparisons at most.
Handling input element in pair

  1. compare a pair of input elements, get smaller value and higher value
  2. compare smaller value with current minimum
  3. compare higher value with current maximum

9.2 Select Algorithm of Linear Time Expectation

The function can return i-th smallest element of array A[p…r]. Expection running time: Θ ( n ) \Theta(n) Θ(n).

RANDOMIZED-SELECT(A, p, r, i)
1	if p == r 
2		return A[p]
3	q = RANDOMIZED-PARTITION(A, p, r) // the function is the same as randonmized quick sort algorithm
4	k = q - p + 1
5	if i == k	// the privot value is the answer
6		return A[q]
7	else if i < k
8		return RANDOMIZED-SELECT(A, p, q-1, i)
9	else
10		return RANDOMIZED-SELECT(A, q+1, r, i-k)

9.3 Select Algorithm of Linear Time in Worst Case

Through follow steps, SELECT algorithm can determine i-th smallest element.

  1. dividing n elements of input array into floor(n / 5) groups, every groups have 5 elements, the last group has (n mod 5) elements.
  2. find medians of the ceil(n/5) groups: firstly using insert sort for each group, then determine medians of the groups
  3. For the ceil(n/5) medians finding in step 2, recursively call SELECT to find the median x of the medians(If the number is even, convention x is the smaller median for convenient).
  4. use modified PARTITION function to part input array by median x. Let k be one more than elements’ number in lower part of the partition, so the x is the k-th smallest element, and have (n - k) elements in high part.
  5. If i = k, return x. If i < k, recursively call SELECT to find i-th smallest element in lower part. If i > k, recursively find (i-k)-th smallest element in high part.

note: modified PARTITION function is from determine PARTITION of quick-sort. The modification is taking pivot element as input parameter.

This is my realization. Just simple test, may be have problem.

#include <vector>
#include <list>
using namespace std;

int PARTITION(vector<int>& a, int p, int r, int x)
{
	int j = p-1, k = p-1;
	for (int i = p; i <= r; ++i)
	{
		if (a[i] < x)
		{
			++j;
			++k;
			int tmp = a[j];
			a[j] = a[i];
			a[i] = a[k];
			a[k] = tmp;
		}
		else if (a[i] == x)
		{
			++k;
			int tmp = a[i];
			a[i] = a[k];
			a[k] = tmp;
		}
	}

	return j + 2 - p;
}

// O(n^2)
void insertSort(vector<int> &a)
{
	for (int i = 1; i < a.size(); ++i)
	{
		for (int j = i - 1; j >= 0; --j)
		{
			if (a[j+1] < a[j])
			{
				int tmp = a[j+1];
				a[j+1] = a[j];
				a[j] = tmp;
			}
			else
				break;
		}
	}
}

int SELECT(vector<int> &a, int p, int r, int i)
{
	vector<int> medians;

	if (p == r) return a[p];
	if (p > r) return 0;

	vector<int> tmp;

	for (int i = p; i <= r; ++i)
	{
		if (tmp.size() == 5)
		{
			insertSort(tmp);
			medians.push_back(tmp[2]);
			tmp.clear();
		}
		
		tmp.push_back(a[i]);
	}

	// calculate the last group median
	if (!tmp.empty())
	{
		insertSort(tmp);
		int med = (tmp.size() - 1) / 2;
		medians.push_back(tmp[med]);
	}

	// find median of medians
	int rightBound = medians.size() - 1;
	int x = SELECT(medians, 0, rightBound, rightBound / 2);

	int k = PARTITION(a, p, r, x);

	if (i == k)
		return x;
	else if (i < k)
		return SELECT(a, p, p+k-2, i);
	else
		return SELECT(a, p+k, r, i-k);
}

int main()
{
	vector<int> a = {1, 7, 3, 1, 0, 9};
	int outValue = SELECT(a, 0, a.size()-1, 4);

	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本书自第一版出版以来,已经成为世界范围内广泛使用的大学教材和专业人员的标准参考手册。本书全面论述了算法的内容,从一定深度上涵盖了算法的诸多方面,同时其讲授和分析方法又兼顾了各个层次读者的接受能力。各章内容自成体系,可作为独立单元学习。所有算法都用英文和伪码描述,使具备初步编程经验的人也可读懂。全书讲解通俗易懂,且不失深度和数学上的严谨性。第二版增加了新的章节,如算法作用、概率分析与随机算法、线性编程等,几乎对第一版的各个部分都作了大量修订。学过计算机的都知道,这本书是全世界最权威的算法课程的大学课本了,基本上全世界的名牌大学用的教材都是它。这本书一共四位作者,Thomas H. Cormen,Charles E. Leiserson和Ronald L. Rivest是来自MIT的教授,Clifford Stein是MIT出来的博士,现在哥伦比亚大学做教授,四人姓氏的首字母联在一起即是此书的英文简称(CLRS 2e),其中的第三作者Ronald L. Rivest是RSA算法的老大(算法名字里面的R即是指他),四个超级大牛出的一本书,此书不看人生不能算完整。再介绍一下课堂录像里面授课的两位MIT的老师,第一位,外表“绝顶聪明”的,是本书的第二作者Charles E. Leiserson,以逻辑严密,风趣幽默享誉MIT。第二位,留着金黄色的络腮胡子和马尾发的酷哥是Erik Demaine,21岁即取得MIT教授资格的天才,1981出生,今年才25岁,业余爱好是俄罗斯方块、演戏、琉璃、折纸、杂耍、魔术和结绳游戏。另外,附上该书的中文电子版,pdg转pdf格式,中文版翻译自该书的第一版,中文书名没有使用《算法导论》,而使用的是《现代计算机常用数据结构和算法》,1994年出版时没有得到国外的授权,属于“私自翻译出版”,译者是南京大学计算机系的潘金贵。
This fourth edition of Robert Sedgewick and Kevin Wayne’s Algorithms is the leading textbook on algorithms today and is widely used in colleges and universities worldwide. This book surveys the most important computer algorithms currently in use and provides a full treatment of data structures and algorithms for sorting, searching, graph processing, and string processing--including fifty algorithms every programmer should know. In this edition, new Java implementations are written in an accessible modular programming style, where all of the code is exposed to the reader and ready to use. The algorithms in this book represent a body of knowledge developed over the last 50 years that has become indispensable, not just for professional programmers and computer science students but for any student with interests in science, mathematics, and engineering, not to mention students who use computation in the liberal arts. The companion web site, algs4.cs.princeton.edu, contains An online synopsis Full Java implementations Test data Exercises and answers Dynamic visualizations Lecture slides Programming assignments with checklists Links to related material The MOOC related to this book is accessible via the "Online Course" link at algs4.cs.princeton.edu. The course offers more than 100 video lecture segments that are integrated with the text, extensive online assessments, and the large-scale discussion forums that have proven so valuable. Offered each fall and spring, this course regularly attracts tens of thousands of registrants. Robert Sedgewick and Kevin Wayne are developing a modern approach to disseminating knowledge that fully embraces technology, enabling people all around the world to discover new ways of learning and teaching. By integrating their textbook, online content, and MOOC, all at the state of the art, they have built a unique resource that greatly expands the breadth and depth of the educational experience.
算法导论》Hardcover版的引言(Introduction to Algorithms - Hardcover Edition)是一本经典的计算机科学教材。该版本不仅在内容上与平装版相同,还具有精美的硬皮封面,能够更好地保护书籍,并增添一份高质感和专业感。 首先,这本书是由Thomas H. Cormen等四位作者共同编写。他们是计算机科学领域的权威人物,在算法研究和教育方面具有丰富的经验。这本书的目的是为计算机科学专业的学生和从业人员提供系统而全面的算法知识,帮助他们深入理解和应用算法。 《算法导论》Hardcover版首先介绍了算法设计和分析的基础知识,包括分治法、贪婪算法、动态规划和回溯法等。接着,书中详细阐述了各种经典算法,如排序、图算法、字符串匹配、高级数据结构等。此外,书中还介绍了算法的优化技巧和应用领域,例如算法的并行化和近似算法。 与平装版相比,Hardcover版的封面更加美观,书页由高品质纸张制成,更加耐用。这使得读者在长时间研究和使用这本书时,能够更好地保存它的完整性和精美外观。此外,Hardcover版也更加适合作为礼品或收藏品,体现了读者对该书的重视和对算法学习的热爱。 总之,《算法导论》Hardcover版是一本内容丰富、思想深刻的算法教材,通过系统化的介绍和实例,帮助读者深入理解和应用各种算法。同时,Hardcover版的精美外观和耐用性也增强了读者在日常使用和收藏方面的满意度。无论是学习算法的新手还是资深专家,都能从这本书中获得极大的收益。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值