Minimum Spanning Trees
CIS 606
Spring 2010
Problem
A town has a set of houses and a set of
roads.
A road connects 2 and only 2 houses.
A road connecting houses u and v has a
repair cost w(u, v).
Goal: Repair enough (and no more) roads
such that
1.
everyon
Elementary Graph Algorithms
CIS 606
Spring 2010
Graph representation
Given graph G = (V, E). In pseudocode,
represent vertex set by G.V and edge set
by G.E.
G may be either directed or undirected.
Two common ways to represent graphs for
algorithms:
1. Adj
Greedy Algorithms
CIS 606
Spring 2010
Greedy Algorithms
Similar to dynamic programming.
Used for optimization problems.
Idea
When we have a choice to make, make the
one that looks best right now. Make a locally
optimal choice in hope of getting a globally
Dynamic Programming
CIS 606
Spring 2010
Dynamic Programming
Not a specific algorithm, but a technique
(like divide-and-conquer).
Developed back in the day when
programming meant tabular method
(like linear programming).
Doesnt really refer to computer
pro
Red-Black Trees
CIS 606
Spring 2010
Red-black trees
A variation of binary search trees.
Balanced: height is O(lg n), where n is the
number of nodes.
Operations will take O(lg n) time in the
worst case.
Red-black trees
A red-black tree is a binary search t
Binary Search Trees
CIS 606
Spring 2010
Search trees
Data structures that support many
dynamic-set operations.
Can be used as both a dictionary and as a
priority queue.
Basic operations take time proportional to the
height of the tree.
For complete binary
Hash Tables
CIS 606
Spring 2010
Hash tables
Many applications require a dynamic set
that supports only the dictionary
operations INSERT, SEARCH, and
DELETE. Example: a symbol table in a
compiler.
A hash table is effective for implementing
a dictionary.
Th
Quicksort
CIS 606
Spring 2010
Quicksort
Worst-case running time: (n2).
Expected running time: (n lg n).
Constants hidden in (n lg n) are small.
Sorts in place.
Quicksort
Quicksort is based on the three-step
process of divide-and-conquer.
To sort the subar
Heapsort
CIS 606
Spring 2010
Overview
Heapsort
Sorts in placelike insertion sort.
O(n lg n) worst caselike merge sort.
Combines the best of both algorithms.
To understand heapsort, well cover heaps
and heap operations, and then well take a
look at priorit
Divide-and-Conquer
CIS 606
Spring 2010
Analyzing Divide-and-Conquer
Algorithms
Use a recurrence to characterize the
running time of a divide-and-conquer
algorithm.
Solving the recurrence gives us the
asymptotic running time.
A recurrence is a function is
Growth of Functions
CIS 606 Spring 2010
Asymptotic Notation
O-notation
O(g(n) = cfw_ f(n): there exist positive constants
c and n0 such that 0 f(n) c g(n) for all n
n0 .
Asymptotic Notation
g(n) is an asymptotic upper bound for
f(n)
If f(n) O(g(n), we wr
Introduction
CIS 606 Spring 2010
The sorting problem
Input: A sequence of n numbers ? a1,
a2, , an ? .
Output: A permutation (reordering) ? a1,
a2, , an ? of the input sequence such
that a1 a2 an.
The sequences are typically stored in
arrays.
We also refe