234 Trees
1. Practice the various operations by inserting the folloing nodes into an initially empty 234 tree:
5, 16, 22, 45, 2, 10, 18, 30, 50, 12, 1
Now, delete the nodes in the same order.
2. Show that there are sets of elements, such that differen
Introduction
Introduction: MultiPop Stack Example
Today we begin studying how to calculate the total time of a
sequence of operations as a whole.
(As opposed to each operation individually.)
Multipop stack operations:
Why and when we care:
I
you have an
Hash Tables
Hash Function: Division Method
Assume you can map keys to natural numbers from 0 to m 1.
Call that function h.
Assume each key is an integer.
h(k) = k mod m
Use an array A of length m.
Simple but susceptible to regular patterns in keys (more c
Text Strings
Evolution
Strings are trivial. (Oh are they now?)
Pre1960: Vary by manufacturers in the US.
Just represent each character by an 8bit number. Done?
1960s: Converge to two in the US: ASCII (led by Bell) and EBCDIC
(led by IBM). (ASCII will gr
Introduction
Graph (Undirected): Definition
What is common among:
C
I
cities and highways between them
I
computers and network cables between them
I
people and relationships
I
in a board game: a state and legal moves to other states
A
E
B
D
An undirected
Motivation
BTrees, e.g., (2,4)Trees
Suppose a block can hold 2t pointers and 2t 1 keys.
Old storage organization:
CPU
RAM
A node can have c children and c 1 keys, t c 2t.
(Root can have c < t.)
disk
slow
Keys are in ascending order: k1 < k2 < < kc1 .
ki
Ordered Dictionary & Binary Search Tree
Binary Search Tree: Definition
Finite map from keys to values, assuming keys are comparable (<,
=, >).
I
insert(k, v)
I
lookup(k) aka find: the associated value if any
I
delete(k)
I
rank(k): k is the ?th smallest ke
Ranking
Ranks from Sizes: Introduction
In a set or a dictionary:
Rank of key k is i iff k is the ith smallest key.
Recall: each node stores size of subtree.
class Node <K> cfw_
public K key;
public Node <K> left , right , parent ;
public int num;
static
This Course Is About. . .
Ordered Dictionary
Finite map from keys to values, assuming keys are comparable (,
=, ).
Data structures: how to store data.
Examples: arrays, linked lists, binary search trees.
This course goes way beyond them.
But why? Why othe
Introduction to WeightBalanced BSTs
Albert Y. C. Lai
I assume that you already know the basic ideas of binary search trees, how to look up a key, how to
insert a key, and how to delete a key.
Recall that if you stick to the basic insertion algorithm, the
CSCB63/263 Summer 2015  Assignment 1 Solution
1.
(a) Proof: 2lg n + n = n + n = 2n (n).
(b) Disproof: 22 lg n = (2lg n )2 = n2 < (n).
2.
(a) Even when f (n) O(g(n), the limit limn
larly for the other limit.
f (n)
g(n)
may not exist or may not be finite.
Introduction
(Edge)Weighted Graph
Many useful graphs have numbers assigned to edges.
Think of: each edge has a price tag.
(Usually 0. Some cases have < 0.)
c
8
b
7
d
2
A weighted (edgeweighted) graph consists of:
9
4
4
a
i
11
e
14
7
8
10
h
1
g
a set of
Disjoint Sets (UnionFind)
Linked List Implementation
Collection of disjoint sets.
I
makeset(x) creates a singleton set containing x
I
find(x): which set x is in now
(What represents a set? Whatever supports asking
find(x) = find(y).)
I
union(S, S0 ) mer
=
CSC B63H Tutorial Outline for Week 3 Fall 2007
=

Augmenting data structures

Example 1:
 How do we compute MIN (return the smallest key stored in the tree)
for a 234 tree?
 What's the running time for MIN?
 What if we need to carry out this
=
CSC 263H Tutorial Outline for Week 1 Fall 2007
=
This week's tutorial is a review of asymptotic notation and basic
complexity analysis. It's all material that you should know from CSC148H,
CSC165H, and CSC236H.
Asymptotic notation:
1. Show that (n+1)^5
Tutorial 4
1. adjMatrixOps.tex
Let A be the adjacency matrix of an undirected graph G = (V, E) and let B = (bie ) be the V  E
incidence matrix of G, where
1 if i e
bie =
0 if i 6 e
(a) Describe what the entries of the matrix product AA represent. Just
CSCB63 Assignment 1
Due: October 1, 2007
1. (25%) A 23 tree is similar to a 234 tree, but each node has at least two and at most three children.
Each internal node of a 23 tree has either 1 or 2 keys. Consider inserting 7 unique elements into a
23 tr
CSCB63 Assignment 4
Due: 5pm, Friday 4 April, 2014.
1. Consider the binary search tree below. Using this tree, we will search for numbers chosen randomly
from the set cfw_1, 2, 3, ., 20. The numbers are not distributed uniformly. Instead, a number has
pro
Tutorial Average Case Complexity
Probability Review Indicator random variables
Indicator random variables are a powerful and easy to use tool that can simplify the computation
of expected values tremendously.
For example, suppose that we want to compute t
Tutorial 11 Average Case Complexity
The Toronto Maple Leafs are hoping to have an above .500 season, i.e., they are hoping to win at least 50%
of their games. Assume that the Leafs play n games in a season. This question will ask you to exam the
worst, be
a little test.
Course Information
Instructor: Anna Bretscher
Email: bretscher@utsc.toronto.edu
Phone: (416) 2084745 (better to email)
Required Textbook:
Michael Goodrich and Roberto Tamassia: Algorithm Design: Foundations, Analysis,
and Internet Examples
Two Puzzles
State Transition Perspective
Toss a fair coin until two consecutive heads (HH). What is the
expected number of tosses? (Answer: 6)
Toss coin until HH:
Toss a fair coin until head and then tail (HT). What is the expected
number of tosses? (Answ
Random, Average, Probability
Example of Probability
Some algorithms have terrible worstcase times, but:
Setting: toss a coin twice.
I
on random input, they are fast on average
The set of all outcomes: cfw_HH, HT, TH, TT
I
or, they contain randomizing ste
This Course Is About. . . (1/2)
Data structures: how to store data.
Examples: arrays, linked lists.
This course goes way beyond them.
But why? Why other data structures?
Because you need this course to graduate. :)
1 / 30
This Course Is About. . . (1/2)
D
CSCB63 Design and Analysis of Algorithms
Course Topics
Balanced Search Trees
Graphs and Graph Traversals
Priority Queues and Heaps
Disjoint Sets
Amortized Complexity
Average Case Analysis
Hashing
1
This week
Data Structures
Worst Case Complexity
Mini Stats Review
(see http:/www.stats.gla.ac.uk/steps/glossary for an excellent review)
What is
a sample(probability) space? The sample space of a random experim
For example, if the experiment is to throw a standard die
and record the outcome, the sampl
This Course Is About. . . (1/2)
This Course Is About. . . (2/2)
Data structures: how to store data.
We will learn:
Examples: arrays, linked lists.
I
Several data structures.
This course goes way beyond them.
I
Algorithms that look up and update them.
I
Wh
Introduction
Introduction: MultiPop Stack Example
Today we begin studying how to calculate the total time of a
sequence of operations as a whole.
(As opposed to each operation individually.)
Multipop stack operations:
Why and when we care:
I
you have an
CSCB63 Summer 2015 Assignment 1
Due: June 1, 12:15 PM, in class
1.
(a) [5 marks] Prove or disprove: 2lg n + n (n).
(b) [5 marks] Prove of disprove: 22 lg n (n).
2. This question is about proving a useful theorem from the textbook:
If f (n) O(g(n) and g(n)
CSCB63/263 Summer 2015  Assignment 2 Solution
1. Tree form:
40
36
21
15
5
30
9
7
11
13
Array form: 40, 36, 21, 15, 30, 7, 11, 5, 9, 13
2. Base case: G = (V, E) has only one vertex, and therefore no edge. Let v1 be that vertex. Then
hv1 i is a simple path
Data structures for graphs
There are two reasonable data structures to store graphs:
adjacency matrix
adjacency list
An adjacency matrix: let V = cfw_v1 , v2 , ., vn .
We store information about the edges of the graph in an
n n array A where
!
1 if (vi
Minimum Cost Spanning Trees (MCSTs)
Let G = (V, E) be a connected, undirected graph with
edge weights w(e) for each edge e E.
A tree is a subset of edges A E such that A is connected
and does not contain a cycle.
Thick edges are in A, the thin ones are
Randomized Quicksort
Note: This section discusses a complexity measure which may
seem similar to Tavg , the averagecase complexity. They are not
the same!
Weve seen that:
Quicksort does pretty well on the average input, but
there are some particular in
Amortized Analysis
[See Section 1.5 in G& T or Chapter 17 in CLRS]
Goal: We want to analyze the complexity of performing a sequence of operations on a particular data structure.
Two cases:
1. We need to know the complexity of each operation in the
sequenc
Dictionaries
A dictionary is an important abstract data type (ADT). It represents the following object and operations:
Object:
Sets
Each element x has a value key(x)
key (x ) comes from a totallyordered universe. This means
that for any two keys a and