Summary of Algo Analysis / Slide 1
Algorithm complexity
Problems algorithms programs
Bounds are for the algorithms, rather than programs
s
Algorithms are often written in pseudo-codes
s
programs are j
Quicksort
2
Introduction
Fastest known sorting algorithm in practice
Average case: O(N log N) (we dont prove it)
Worst case: O(N2)
s
But, the worst case seldom happens.
Another divide-and-conquer re
COMP171
Spring 2009
Pattern Matching
Pattern Matching / Slide 2
Pattern Matching
Given a text string T[0.n-1] and a pattern
P[0.m-1], find all occurrences of the pattern
within the text.
Example: T =
Hashing
The Magic Container
Interface
Main methods:
Void Put(Object)
Object Get(Object) returns null if not i
Remove(Object)
Goal: methods are O(1)! (ususally)
Implementation details
HashTable:
Merge sort, Insertion sort
Sorting I / Slide 2
Sorting
Selection sort or bubble sort
Find the minimum value in the list
2. Swap it with the value in the first position
3. Repeat the steps above for re
Heaps, Heap Sort, and Priority Queues
Sorting III / Slide 2
Trees
A tree T is a collection of nodes
s
s
T can be empty
(recursive definition) If not empty, a tree T consists
of
a (distinguished) node
COMP171
Hashing
Hashing 2
Hashing
Again, a (dynamic) set of elements in which we do search, insert, and
delete
Linear ones: lists, stacks, queues,
s Nonlinear ones: trees, graphs (relations between
Graph & BFS
Graph & BFS / Slide 2
Graphs
Extremely
useful tool in modeling problems
Consist of:
s Vertices
s Edges
D
E
Vertices can be
considered sites
or locations.
C
A
Vertex
B
F
Edge
Edges repres
Dynamic Memory Allocation
Topics
Topics
Simple explicit allocators
Data structures
Mechanisms
Policies
Harsh Reality
Memory Matters
Memory is not unbounded
It must be allocated and managed
Many app
List, (dynamic) linked list
Lets first forget about classes, but only a dynamic list.
We make lists with classes afterwards.
A simple list Example:
using a dynamic array
concept of a list, e.g. a list
Classes with dynamic objects
List as a (dynamic) class
Class matters!
global
local
public
class A cfw_
int x;
int x;
void f() cfw_
void f() cfw_
x=10;
int x;
int x;
x=10;
void f();
public
x=0;
main
Doubly Linked List
Doubly Linked Lists / Slide 2
Motivation
Doubly linked lists are useful for playing video
and sound files with rewind and instant
replay
They are also useful for other linked data
COMP171
Depth-First Search
Graph / Slide 2
Depth-First Search (DFS)
DFS is another popular graph search strategy
s
Idea is similar to pre-order traversal (visit node,
then visit children recursively)
COMP171
Connected Components,
Directed Graphs,
Topological Sort
Graph / Slide 2
Graph Application: Connectivity
G=
P
N
L
Q
R
O
M
s
D
E
How do we tell if two vertices
are connected?
C
A
F
B
K
G
H
A con
COMP171
B+-Trees (Part 2)
Slide 2
Review: B+ Tree of order M and of
leaf size L
s The
root is either a leaf or 2 to M children
s Each (internal) node (except the root) has
between M/2 and M children (
COMP171
B+-Trees (Part 1)
Slide 2
Main and secondary memories
Secondary storage device is much, much
slower than the main RAM
Pages and blocks
Internal, external sorting
CPU operations
Disk access: Di
COMP171
Trees, Binary Trees,
and Binary Search Trees
2
Trees
Linear access time of linked lists is prohibitive
s
Does there exist any simple data structure for
which the running time of most operation
COMP171
Breadth First Search (BFS)
Part 2
Graph / Slide 2
Shortest Path Recording
BFS we saw only tells us whether a path
exists from source s, to other vertices v.
s
s
It doesnt tell us the path!
We
Algorithm Analysis
Analysis of Algorithms / Slide 2
Introduction
Data structures
s
Methods of organizing data
What is Algorithm?
s
a clearly specified set of simple instructions on the data to be
foll
Sorting Techniques
1
Sorting
OUTPUT
INPUT
sequence of numbers
a1, a2, a3,.,an
2
5
4
10
Sort
7
Correctness
Correctness
For any given input the algorithm
For any given input the algorithm
halts with the
Searching and Sorting
Eric Roberts
CS 106A
March 5, 2010
Onceuponatime.
The Millennium Challenge Problems
Searching and Sorting
Searching
Chapter 12 looks at two operations on arrayssearching and
sor
Stepwise Refinement
Eric Roberts
CS 106A
January 8, 2010
Outline
1. Review Karel statement forms
2. Exercise: The putBeeperLine method
3. Stepwise refinement
4. Exercise: The BanishWinter program
5. P
Trees
Outline
Binary trees
Binary search trees
AVL trees
B trees
Applications
04/13/12
Data Structure: Trees
2
Trees
Tree ADT
A tree is a collection (may be empty) of nodes,
containing:
a distinguish
CS 332: Algorithms
Linear-Time Sorting Continued
Medians and Order Statistics
David Luebke
1
Review: Comparison Sorts
Comparison sorts: O(n lg n) at best
Model sort with decision tree
Path down tre
CS 332: Algorithms
Linear-Time Sorting Algorithms
David Luebke
1
Sorting So Far
q
Insertion sort:
s
s
s
s
s
s
Easy to code
Fast on small inputs (less than ~50 elements)
Fast on nearly-sorted inputs
O(
Stacks
Outline
Linked-list Implementation
Array Implementation
Applications
04/13/12
Data Structure: Stacks
2
Stack ADT
If N=0, it is an empty stack.
Methods
Push: add the element on
the top of st