public class DS
cfw_
int[] s;
public DS(int n)
cfw_
s=new int[n];
for (int i=1; i<n; i+)
s[i]=-1;
public void union(int r1, int r2)
cfw_
if (s[r1]>s[r2]) s[r1]=r2;
else
cfw_
if (s[r1]=s[r2]) s[r1]-;
s[r2]=r1;
public int find(int i)
cfw_
int result, j=i
Find and Union
Connectivity problems, equivalences
Described by distinct pairs of integers between 1 and n, each pair i j specifies that i and j
are connected (or equivalent) and should be in the same set.
Given: Disjoint sets containing the integers 1 to
public class HeapSortDemo
cfw_
public HeapSortDemo()
cfw_
int[] a = cfw_30, 3, 7, 15, 20, 100, 25, 80, 10, 90, 17, 40, 5;
int i;
for (i=0; i<a.length; i+) System.out.println(a[i]);
heapsort(a);
System.out.println("Sorted:");
for (i=0; i<a.length;
import java.util.*;
import java.io.*;
public class AVLTest
cfw_
public static int getdepth(AVLNode node)
cfw_
int L=0; int R=0; int result=0;
if (node!=null)
cfw_
if (node.left!=null) L=node.left.depth;
if (node.right!=null) R=node.right.depth;
result=max
Graphs
G = (V, E) and we use v and e to denote, respectively, the # vertices in V and edges in E.
Breadth-first-search
This is analogous to a level traversal of a binary tree.
Each vertex x has a color, color[x], a distance d[x], and a parent, [x]
Q is a
Discuss Graphs, directed and undirected, adjacency lists representation and adjacency
matrix representation both can also be used for weighted graphs.
Spanning Trees
Given undirected connected graph G with no more than one edge between any two
vertices an
Amortized Analysis
Consider a sequence of operations on a data structure and let ti be the cost of the ith
operation. An amortized analysis means determining the worst case cost of the sequence.
The amortized cost is the worst case cost of the sequence di
Binary Search Trees
Define a BST.
Show the connection with inorder numbering of nodes and sorted order.
Simple search, insertion, deletion, and growing algorithms.
Behavior under random inputs - each of the n! input sequences of integers 1 to n are
equall
Topological Sorting
Background
Topological sorting requires ranking a set of objects subject to constraints on the
resultant topology-that is, on the placement of the objects. It occurs in many practical
situations. For example, textbooks are often writte
Optimal Binary Search Tree
We want to build a binary search tree which will contain keys K1 to Kn. They will be
searched for over time with probabilities 1 to n respectively. Also, keys not in the tree
whose values lie between Ki to Ki+1 are searched for
Strahler Numbers and Pruning Numbers
Binary Trees - Strahler Numbers
Each node of a binary tree is assigned a Strahler as follows:
All leaf nodes are assigned 1. If a node has only one successor, it is assigned the number
of its successor otherwise it is
Trees
Vertices (or nodes) , edges, adjacent vertices, paths, simple paths, cycles, simple cycles of
undirected graphs - no self references, no multiple edges.
Connected graphs, components
Let G be a graph with n>1 vertices. Then G is a free tree iff
1. G
Path Length and Huffman Codes
The path length of a binary tree t with nonnegative weights attached to its nodes is
W(t) = w(i) d(i) where node i has weight w(i) and is at depth d(i) in t.
All nodes
i in t
Example:
t:
2
/ \
1
0
/ \
\
2 3
5
W(t) = 2x1 + 1x2
Heaps
Definition
Two methods of construction O(nlog n) and O(n)
O(n) proof:
A node at depth i can move down at most d-i levels before reaching the bottom in a
tree of depth d. There are at most 2i-1 nodes at depth i. So the total number of comparisons
(an
Binary Tree Traversals
Review recursive traversals
Discuss in relation to Preorder Traversal just after P has been visited.
The idea is to keep on a stack, pointers to all the nodes on the path from the predecessor
of node p (which is about to be visited)
Maximum Subsequence Sum Problem
Given integers a1, a2, . . . , an find the maximum value of the sum of all integers in any
subsequence of consecutive integers. This maximum value will be at least 0 since the
subsequence consisting of no integers is always
Garwicks Algorithm
Motivate using successor lists of the topological sort problem!
Garwicks technique:for 3 or more stacks (or any contiguous information):
(fixed fraction) + space (~recent growth) + space (~size)
10%
x 90%
(1-) x 90%
Table
Base
X
Base
1
Parenthesis and Binary Trees
Consider the 4 node binary tree:
1
/
\
2
4
/ \ / \
3
/ \
If traverse the tree in preorder then the numbers assigned to the nodes reflect the order in
which they are visited. Now, suppose create a sequence of 1s and 0s by trave
Some facts:
Sum of 1+2+3+ . . . +n=n(n+1)/2
If n is even, sum of odd integers from 1 to n=n2/4
and sum of even integers = n(n+2)/4
If n is odd, sum of odd integers from 1 to n=(n+1)2/4
and sum of even integers = (n-1)(n+1)/4
sum of the 12+22+32+ . . . +n2