Universal Hashing
Def: : Let u be a universe of keys, and let H be a finite collection of hash functions mapping u to {0, 1, 2, 3, ... m - 1}
H is universal if for all pairs of keys, distinct keys the following is true: if for any pair of keys I pick, the number of hash functions that hash these two keys to the same place is a one over m fraction of the total set of keys( |H| / m ).
If H is chosen randomly, from the set of keys H, the probability of collision between x and y is what?
Graphs
有向图中可以有自身环, 而无向图中不允许有自身环。
如果(u,v) 是有向图G = (V, E) 的一条边,则称(u, v) 离开顶点u, 进入顶点v。
Digraph: (Direct Graph) G = (V , E)
. Set of vertices( Singular: vertex)
Adjacency matrix of G = (V, E) : 邻接矩阵
where V = {1, 2, .... n} is the n x n matrix A given by
1 if (i,j) not in E
A[ i , j ] = 0 if (i,j) in E
![]()
![](https://img-my.csdn.net/uploads/201302/02/1359791188_1963.jpg)
list Adj[v] of vertices adjacent to V
Adj[1] = {2,3}
Adj[2] = {3}
Adj[3] = {}
Adj[4] ={3}
|Adj[v]| = degree(v);
lemma 引理
Handshaking Lemma (undirected graph)
SUM(degree(v) = 2|E| v belongs to V
Minimum spanning tree:
Input: Connected undir graph G = (V ,E) with edge weight function : W : E -> R
Output: a spanning tree T (connects all the vertices) of minimum weight
Hall mark for greedy algorithms: 贪心算法
Creedy choice property: A locally optimal choice is a globally optimal
Theorem: Let T be the MST of our graph, and let's let A be any subset of V/ And now, suppose that edge(u,v) is the least weight edge connecting our set A to A complement(V - A). Then the (u, v) is in the minimum spanning tree
Prim's Algorithm
Idea: Maintain V-A as a priority queue Q. Key each vertex in Q with the weight of least weight edge connecting it to a vertex in A.
PSEUDO CODE:
Q <--- V
key[v] < --- infinite v in V
key[s] <--- 0 for s in V
while ! != empty
do u <--- Extract-Min(Q)
for each v in Adj[u]
do if v in Q and w(V) < key [v]
then key[v] <-- w(u, v)
[v] <--- u
Lecture: Balanced Search Trees
Examples:
-- AVL tress
-- Red-black Trees
Todayt focuse on Read-black trees: a binary data structure with color field for each node satisfying:
1: every node is either red or black
2: the root and leaves are black
3: the parent of every red node is black
4: All simple paths from a node x to a descended leaf of x have the same black nodes
(1)红黑树的来源
a)双向链表是二叉树的最初来源,虽然二叉树也可以做到基本有序,但是查找起来十分麻烦
b)在双向链表的基础上,人们发明了二叉树,二叉树保证了数据可以像数组一样快速完成二分查找,极大地提高了查找效率
c)二叉树存在各个数据查找效率不一致的情况,为了做到数据查找效率一致,人们设计了二叉平衡树,左子树和右子树的高度绝对值之差要小于1
d)为了减少子树旋转的次数,人们设计了红黑树,进一步提高了数据插入和删除的效率
(2)红黑树的概念
a)红黑树的每个节点必须是红色或者是黑色
b)根节点到叶节点之间的路径不存在连续的红色节点
c)根节点到叶节点之间的黑色节点数相同