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]-;
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
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.dept
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 colo
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
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 a
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
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 practi
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 tre
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 i
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 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
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 n
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 t
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 si
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) +
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 visite
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