3 The Stack ADT
65
1. ADT
A stack is a Last-In-First-Out (LIFO) list,
that is, an ordered list in which insertions and
deletions are made at the top only.
Objects: A finite ordered list with zero or
more elements.
Operations:
Int IsEmpty( Stack S );
Sta
7 Quicksort
- the fastest known sorting algorithm in practice
1. The Algorithm
void Quicksort ( ElementType A[ ], int N )
cfw_
if ( N < 2 ) return;
pivot = pick any element in A[ ];
Partition S = cfw_ A[ ] \ pivot into two disjoint sets:
A1=cfw_ aS | a
CHAPTER
7
Search by Formula
HASHING
Interpolation Search :
Theres
aalready
theoremhave
saying
thatsearch
But
we
binary
Find key
from
a sorted list
f [sorts
l ].key, f [ l+1 ].key, . , f [ u ].key.
Any
algorithm
that
in O( ln n ) time after sorting.
by
com
3
Lists, Stacks, and Queues
CHAPTER
1 Abstract Data Type (ADT)
Definition Data Type = cfw_ Objects cfw_ Operations
Example int = cfw_ 0, 1, 2, , INT_MAX, INT_MIN
cfw_ , , , , ,
Definition An Abstract Data Type (ADT) is a data
type that is organize
3 The Stack ADT
65
1. ADT
A stack is a Last-In-First-Out (LIFO) list,
that is, an ordered list in which insertions and
deletions are made at the top only.
Objects: A finite ordered list with zero or
more elements.
Operations:
Int IsEmpty( Stack S );
Sta
3 Compare the Algorithms
Example Given (possibly negative) integers A1, A2,
j
, AN, find the maximum value ofk i Ak .
Algorithm 1
Max sum is 0 if all
int MaxSubsequenceSum ( const int A[ ], int N )
the integers are
cfw_
negative.
int ThisSum, MaxSum, i,
CHAPTER
6
SORTING
1 Preliminaries
void X_Sort ( ElementType A[ ], int N )
Comparisonbased sorting
/* N must be a legal integer */
/* Assume integer array for the sake of simplicity */
/* > and < operators exist and are the only operations
allowed on the i
2 Binary Trees
Note:
Note:In
Inaatree,
tree,the
theorder
orderof
ofchildren
childrendoes
doesnot
notmatter.
matter.
But
Butin
inaabinary
binarytree,
tree,left
leftchild
childand
andright
rightchild
childare
are
different.
different.
A
B
and
A
are two diff
CHAPTER
4 TREES
A Book Store
A Dictionary
1/16
CHAPTER
4
TREES
1 Preliminaries
1. Terminology
Pedigree Tree
( binary tree )
Lineal Tree
1/16
1 Preliminaries
Definition A tree is a collection of nodes. The collection
can be empty; otherwise, a tree consis
CHAPTER
2
ALGORITHM ANALYSIS
Definition An algorithm is a finite set of instructions
that, if followed, accomplishes a particular task. In
addition, all algorithms must satisfy the following criteria:
(1) Input There are zero or more quantities that are
3
Lists, Stacks, and Queues
CHAPTER
1 Abstract Data Type (ADT)
Definition Data Type = cfw_ Objects cfw_ Operations
Example int = cfw_ 0, 1, 2, , INT_MAX,
INT_MIN
cfw_ , , , , ,
Definition An Abstract Data Type (ADT) is a data
type that is organize
CHAPTER
5
PRIORITY QUEUES (HEAPS)
delete the element with the highest \ lowest priority
1 ADT Model
Objects: A finite ordered list with zero or more elements.
Operations:
PriorityQueue Initialize( int MaxElements );
void Insert( ElementType X, Priority
CHAPTER
8
THE DISJOINT SET ADT
1 Equivalence Relations
Definition A relation R is defined on a set S if for every
pair of elements (a, b), a, b S, a R b is either true or false.
If a R b is true, then we say that a is related to b.
Definition A relation
3 Shortest Path Algorithms
Given a digraph G = ( V, E ), and a cost function c( e )
for e E( G ). The length of a path P from source to
destination is
c(ei ) (also called weighted path length).
e i P
1. Single-Source Shortest-Path Problem
Given as input a
3 The Search Tree ADT - Binary Search Trees
1. Definition
Definition A binary search tree is a binary tree. It may
(1)
(2)
(3)
(4)
be empty. If it is not empty, it satisfies the following
properties:
Every node has a key which is an integer, and the keys
CHAPTER
9
GRAPH ALGORITHMS
1 Definitions
G( V, E ) where G := graph, V = V( G ) := finite nonempty set of ve
rtices, and E = E( G ) := finite set of edges.
Undirected graph: ( vi , vj ) = ( vj , vi ) := the same edge.
Directed graph (digraph): < vi , v
CHAPTER
4
TREES
1 Preliminaries
1. Terminology
Pedigree Tree
( binary tree )
Lineal Tree
1/18
1 Preliminaries
Definition A tree is a collection of nodes. The collection
can be empty; otherwise, a tree consists of
(1) a distinguished node r, called the ro
3 Compare the Algorithms
Example Given (possibly negative) integers A1, A2,
j
, AN, find the maximum value ofk i Ak .
Algorithm 1
Max sum is 0 if all
int MaxSubsequenceSum ( const int A[ ], int N )
the integers are
cfw_
negative.
int ThisSum, MaxSum, i,
CHAPTER
2
ALGORITHM ANALYSIS
Definition An algorithm is a finite set of instructions
that, if followed, accomplishes a particular task. In
addition, all algorithms must satisfy the following criteria:
(1) Input There are zero or more quantities that are
Fundamentals of Data Structures
Instructor: CHEN, YUE
E-mail: [email protected]
Courseware and homework sets can be downloaded fr
om http:/www.cs.zju.edu.cn/ds/
(Text Book)
Data Structures and
Algorithm Analysis in C
(2nd Edition)
Mark Allen Weiss
E
CHAPTER
5
PRIORITY QUEUES (HEAPS)
delete the element with the highest \ lowest priority
1 ADT Model
Objects: A finite ordered list with zero or more elements.
Operations:
PriorityQueue Initialize( int MaxElements );
void Insert( ElementType X, Priority
Project 1: Performance Measurement
Given a list of ordered N integers, numbered from 0 to N checking to see that 1, N is not in this list provides a worst case for many search algorithms. Consider two algorithms: one is called sequential search which scan
Project 3: Universal Travel Sites
Time Limit: 2 seconds
After finishing her tour around the Earth, CYJJ is now planning a universal travel sites development project. After a careful investigation, she has a list of capacities of all the satellite transpor
/ feng1-1.cpp : Defines the entry point for the console application. / / #include "stdafx.h" #include <stdio.h> # #include <malloc.h>
int _tmain(int argc, _TCHAR* argv[]) cfw_ int *A = NULL; long N; int i; e errno_t err; F FILE *stream; int ThisSum,MaxSum
9.4 Network Flow Problems
. Example .
source
3
Consider the following network of pipes: s
1 2
Note: Total coming in (v) Total going out (v) b
2
a
3
where v cfw_ s, t Determine the maximum amount of flow that can pass from s to t.
c
2
4
d t
3
sink
1. A Si
CHAPTER
9
GRAPH ALGORITHMS
9.1 Definitions
G( V, E ) where G := graph, V = V( G ) := finite nonempty set of vertices, and E = E( G ) := finite set of edges. Undirected graph: ( vi , vj ) = ( vj , vi ) := the same edge. Directed graph (digraph): < vi , vj
CHAPTER
7
HASHING
7.1 General Idea
Symbol Table ( = Dictionary) := cfw_ < name, attribute > . Example . In Oxford English dictionary name = since M[0] = after a date, event, etc. attribute = a list of meanings M[1] = seeing that (expressing
reason)
Thi
6.9 A General Lower Bound for Sorting
. Theorem h Any algorithm that sorts by comparisons only must have a worst case computing time of ( N log N ).
When sorting N etistincta binary of LEMMA 6.1 L d T be T F LEMMA 6.2 T h N d tree 2 elements, . thenA bina
CHAPTER
6
SORTING
6.1 Preliminaries
void X_Sort ( ElementType A[ ], int N ) /* N must be a legal integer */
Comparisonbased sorting
/* Assume integer array for the sake of simplicity */ /* > and < operators exist and are the only operations allowed on the