COMP 251 Final Crib Sheet Lecture 2 – Balanced Search Trees BST best case: nodes=2height1 depth O(log n) ;; worst case: nodes=height ;; AVL balance condition: height of left and right subtree must differ by at most 1 node rotateRight(root) newRoot=root.left ;; root.left=newRoot.right ;; newRoot.right=root ;; return newRoot Outside balances fixed by simple rotations ;; Inside inside tree rotation main tree rotation
Lecture 3 – Hashing
A map is a set of (key, value) pairs: Each key max one value ;; Hash map = hash funct + hash table bad hash funct same values for different keys ;; good less value collisions for different keys. h:U (hash coding) {integers} (compression: hash values) {0, 1, … m1} Composition of maps: h(key) = compression(hashCode(key)) LinearProbingEntry(h(key)) //closed hashing If h(key) is full ;; LinearProbingEntry((h(key)+1)%m) //hi(key)=h0(key)+i //Quadratic probing = hi(key)=h0(key)+i2 //We avoid clustering but it still happens Then there is double hashing: hi(key)=h0(key)+i g0(key) Lecture 4 – Heaps Queue – FILO ;; Priority Queue – remove object with highest priority (defined by a key) Array based implementation: Root is at A[1] ;; A[i].leftchild = A[2*i] ;; A[i].rightchild = A[2*i + 1] ;; buildHeap():: for i=1 to n ;; upHeap(i) ;; //O( n*log(n) ) FasterBuildHeap():: for i=n/2 down to 1 ;; downHeap(i) Height=log(n+1)–1 ;; #nodes = 2^(h+1)1 ;; Level L = 2^L nodes ;; Till level L = 2(L+1)1 nodes ;; Worst case = if each element bubbles up to the root ;; Total # of swaps = sum of depths Max number of swaps for a node at level l = l Worst case total number of swaps :: sum of all (hL)2^L = n=log(n+1) Lecture 5 – Disjoint Sets The set of vertices V in a graph G is portioned “connected components” that are “path connected” For any undirected graph, pathconnected(u, v) defines an equivalence relation on vertices • there is a path of length 0 from u to v
• •
there is a path from u to v if and only if there is a path from v to u
if there is a path from (u to v) & (v to w) then there is a path from (u to w) Disjoint Sets ADT: Assume each set in the partition has a unique representative element find(i) returns the representative ;; sameset(i, j) returns Boolean ;; union(i, j) merges ;;
COMP 251 Final Crib Sheet quickfind(i) {return rep[i];} union(i, j) (with quickfind(i)) //O(n) per union too slow if rep[i] ≠ rep[j] //oldrep=rep[i] for k in 0, 1 … n1 ;; if rep[k] == rep[i] oldrep ;; rep[k]=rep[j] //Redundant calls Quick union: represent the disjoint sets as rooted trees //The root being the representative i.e. find(i)==findrep(i)==findroot(i) //Each node points to their parents use parent[] ;; non root elements hold root value ;; root nodes have value 1 Find(i):: if p[i]=1 return i ;; else return find(p[i]) ;; Union(i,j):: if find(i)!=find(j) then p[find(i)]=find(j) Uni size: links small tree to root of big tree. //mx tree size=2n; depth of any node is logn Uni height: links short tree to root of tall tree //mx height=logn; nodes added; proof by induction n≥2^h PathCompressionFind(i):: if p[i]==1 return i ;; else p[i]=PathCompressionFind(p[i]) return p[i] ;; Quick find is O(1) for find and O(n) for union ;; Quick union by h or s is O(logn) for both Lecture 6 – Directed Graphs “strongly connected components of directed graphs” set of “mutually reachable ”vertices. To find the SCC containing v you have to find the intersection of Rfrom(v) and Rto(v) //sets of verts Rfrom(G, v) DFS/BFS :: v.visited=true ;; for each w ϵ v.adjlist ;; if (!w.visited) ;; BFS/DFS(G, w) //We follow the edges backwards for Rto or we run Rfrom(G^T, v) on the transverse graph Run for all vertices in a graph to find all SCCs DIRECTED ACYCLIC GRAPHS are directed graphs that have no cycles If DAG then it must have at least one vertex with no incoming edges and one with no outgoing. Topological Ordering is when a graph label the vertices v1, v2, v3 vi such that if (vi, vj)ϵE then i<j G has TO if and only if G is a DAG Lecture 7 – Dijkstra’s Algorithm To find the shortest path to reach a vertex (non weighted), one must use BFS. BFS(v):: v.visited=true; while queue!empty ; Add neighbours u to que if !visited; u.visited=true; Dijktra: 2 sets: S Verts we know the short path ;; V Verts we don’t ;; Dijkstra(G=(V,E), s):: //use a priority queue Initialize empty pq; For each u in V: parent[u]=null; pq.add(u, infinity) pq.add(s,0) While pq is !empty:: u=pq.getMinVertex() ;; dist[u]=pq.removeMinDistance; S.add(u); For each v in u.adj:: if v in V/S:: tempDistToV=dist[u]+cost(u,v); If tmpDist