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 just implementations of an algorithm, and almost
always
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 recursive
algorithm, like mergesort
3
Quicksort
Divide st
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 = 000010001010001 and P =
0001:
s
s
s
first occurrence st
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: the storage bin
hashfunction(object): tells where obj
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 remainder of the list (starting at the
second position)
1
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 r (the root),
and zero or more nonempty subtrees T1, T2
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 elements are explicit)
s
s
Now for the case relation is
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 represent
connections.
Graph & BFS / Slide 3
Application 1
Ai
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 applications are memory dominated
Especially those based
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 of integers
s
s
s
s
s
s
Print out info
Empty test
Sear
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() cfw_
main() cfw_
f();
x=0;
A a;
f();
a.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
where require a fast forward of the data as
needed
Doub
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)
DFS can provide certain information about
the graph tha
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 connected to F?
A connected to L?
Graph / Slide 3
Connecti
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 (at most M
chidren, so at most M-1 keys)
leaf has betwee
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: Disk-read(), disk-write(), much
more expensive than the o
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 operations (search,
insert, delete) is O(log N)?
Trees
s
s
s
s
B
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 need to modify the algorithm to record the path
How can
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
followed to solve a problem
Takes a set of values, as input
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 output:
halts with the output
b1 < b2 < b3 < .< bn
b1
*8053*
8083
(Pages : 2)
Reg. No. : .
Name : .
Fourth Semester B.Tech. Degree Examination, December 2009
(2003 Scheme)
03.405 : DATA STRUCTURES & PROGRAMMING METHODOLOGY (R.F.)
Time : 3 Hours
Max. Marks : 100
PART A
Answer all questions. All question carry
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
sortingboth of which turn out to be important in a wide
ra
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. Preconditions and postconditions
6. Story time: Past Kar
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 distinguished node called the root r,
zero or more non-empty subt
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 tree = execution trace of algorithm
Leaves of tree = poss
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(n2) worst case
O(n2) average (equally-likely inputs) ca
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 stack
Pop: take the element from
the top of stack
Top:
CS 332: Algorithms
S-S Shortest Path: Dijkstras Algorithm
Disjoint-Set Union
Amortized Analysis
David Luebke
1
Review:
Single-Source Shortest Path
Problem: given a weighted directed graph G,
find the minimum-weight path from a given
source vertex s to an