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
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
int a = cfw_30, 3, 7, 15, 20, 100, 25, 80, 10, 90, 17, 40, 5;
for (i=0; i<a.length; i+) System.out.println(a[i]);
for (i=0; i<a.length;
public class AVLTest
public static int getdepth(AVLNode node)
int L=0; int R=0; int result=0;
if (node.left!=null) L=node.left.depth;
if (node.right!=null) R=node.right.depth;
G = (V, E) and we use v and e to denote, respectively, the # vertices in V and edges in E.
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.
Given undirected connected graph G with no more than one edge between any two
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
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
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
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.
i in t
W(t) = 2x1 + 1x2
Two methods of construction O(nlog n) and O(n)
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
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
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)
(1-) x 90%
Parenthesis and Binary Trees
Consider the 4 node binary tree:
/ \ / \
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
public class DS
public DS(int n)
for (int i=1; i<n; i+)
public void union(int r1, int r2)
if (s[r1]>s[r2]) s[r1]=r2;
if (s[r1]=s[r2]) s[r1]-;
public int find(int i)
int result, j=i