BIT 2319 Lecture 1_Review of Data Structures and Algorithms
BIT 2319 Lecture 1_Review of Data Structures and Algorithms
2
Observation
All programs manipulate data
Programs process, store, display, gather
Data can be information, numbers,
images, sound
Each program must decide how to store
data
Choice influences program at every level
Execution speed
Memory requirements
Maintenance (debugging, extending, etc.)
3
Data Structures
Data structure is
representation of the logical
relationship existing between
individual elements of data.
The logical or mathematical
model of a particular
organization of data is called
a data structure.
A data structure is a way of
organizing all data items that
considers not only the
elements stored but also their
relationship to each other.
4
Data Structures Contd.
A data structure is defined by
(1) The logical arrangement of data
elements, combined with
(2) The set of operations we need to
access the elements.
Atomic Variables
Atomic variables can only store one value
at a time.
int num; float s;
A value stored in an atomic variable
cannot be subdivided.
5
What is Algorithm?
Algorithm:
A computable set of steps to achieve a
desired result
Relationship to Data Structure
Example: Find an element
2 4 6
1 3 5 7
1 2 3 4 5 6 7
6
What is it all about?
Solving problems
Get me from home to work
Balance my checkbook
Simulate a jet engine
Graduate from JKUAT
Using a computer to help solve problems
Designing programs (architecture,
algorithms)
Writing programs
Verifying programs
Documenting programs
7
Basic Data Types – the
Simplest Data Structure
Basic data types a language supports:
Integer, float, double, char, boolean
string: usually an array of char supported with
library
A single datum in one of the basic types
A structure is a combination of the basic types
A Publication: code—string, description—string,
price– double
A structure is a combination of basic types and
structures
An Order item: publication—Publication, quantity–
integer, deleteFlag – boolean
8
Storage Container
For storing multiple occurrences of a
structure
Contiguous structures:
Array – supported by a language but
needs care of array size, overflow
Vector – a structure to allow handling of its
size and overflow “automatically”
Linked List: allow data connected by “links”
to save space which may be wasted in an
array/vector.
Combination of vector and linked list
9
Examples of Storage
Containers
10
Notes on the Basic Storage
Containers
Vector and list allow data to be stored in
different ways but not restricted to any
order, or any operations, e.g.,
Data can be ordered in any sequence,
even though searching may prefer a
sorted one.
Operations support inserting an
element in between elements of a
vector, even though it may involve a
lot of “shift” operations.
11
Data Structures and
Algorithms
Algorithm
Outline, the essence of a computational
procedure, step-by-step instructions
A high level, language independent description
of a step-by-step process for solving a problem
Program
An implementation of an algorithm in some
programming language
Data structure
Organization of data needed to solve the
problem
A set of algorithms which implement an ADT
12
Why so many Data
Structures?
Ideal data structure: Dictionary ADT
fast, elegant, memory list
efficient binary search
Generates tensions:
time vs. space
tree
performance vs. AVL tree
elegance Splay tree
generality vs. Red-Black tree
simplicity hash table
one operation’s
performance vs.
another’s
13
Code Implementation
Theoretically
Abstract base class describes ADT
Inherited implementations implement data
structures
Can change data structures transparently (to
client code)
Practice
Different implementations sometimes suggest
different interfaces (generality vs. simplicity)
Performance of a data structure may influence
form of client code (time vs. space, one
operation vs. another)
14
ADT Presentation Algorithm
Present an ADT
Motivate with some applications
Repeat until browned entirely through
Develop a data structure for the ADT
Analyze its properties
Efficiency
Correctness
Limitations
Ease of programming
Contrast data structure’s strengths and
weaknesses
Understand when to use each one
15
Algorithm Strategies
There are countless algorithms
Strategies
Greedy
Divide and Conquer
Dynamic Programming
Exhaustive Search
16
Overall Picture
Data Structure and Implementation
Algorithm Design Goals Goals
Correctness Robustness
Adaptability
Efficiency
Reusability
17
Terminology
Abstract Data Type (ADT)
Mathematical description of an object with set
of operations on the object. Useful building
block.
Algorithm
A high level, language independent,
description of a step-by-step process
Data structure
A specific family of algorithms for
implementing an abstract data type.
Implementation of data structure
A specific implementation in a specific
language
18
Algorithmic Problem
Specificati
Specificati ? on of
on of input output as a
function of
input
Infinite number of input instances satisfying the
specification.
For example:
A sorted, non-decreasing sequence of natural
numbers. The sequence is of non-zero, finite length:
1, 20, 908, 909, 100000, 1000000000.
3.
19
Algorithmic Solution
21
Example: Sorting
INPUT OUTPUT
sequence of numbers a permutation of the
sequence of numbers
Correctness
Correctness Running
Runningtime
time
For
Forany
anygiven
giveninput
inputthethealgorithm
algorithm Depends
Dependson on
halts
haltswith
withthetheoutput:
output: ••number
numberof ofelements
elements(n)(n)
••bb1 <<bb2 <<bb3 <<…. ….<< bbnn ••how
how(partially)
(partially)sorted
sorted
1 2 3
••bb1,,bb2,,bb3,,…., bn is a they
theyare
are
1 2 3 …., bn is a
••algorithm
algorithm
22 permutation
permutationof ofaa11,,aa22,,aa33,….,a
,….,ann
Analysis of Algorithms
Efficiency:
Running time
Space used
Efficiency as a function of input size:
Number of data elements (numbers,
points)
A number of bits in an input number
23
Linear Data Structures
A data structure is said to be linear if its elements
form a sequence or a linear list.
Arrays
Linked Lists
Stacks, Queues
A one:one relationship between elements in the
collection.
Assuming the structure is not empty, there is a first
and a last element.
Every element except the first has a unique
predecessor.
Every element except the last has a unique
successor.
24
General model of a linear
data structure
25
Hierarchical Data Structures
Hierarchical Data Structures
A one:many relationship between
elements in the collection.
Assuming the structure is not empty,
there is a unique element called the
root.
There may be zero to many
terminating nodes called leaves.
Nodes that are neither roots nor
leaves are called internal.
26
General model of a
hierarchical data structure
27
Hierarchical Data Structures
contd.
Every element except the root has a unique
predecessor.
Every element except a leaf has a one or
more successors.
An internal node has exactly one
predecessor and one or more
successors.
There is more than one way to traverse a
hierarchical data structure.
Generally called trees, they are very
important and widely used.
28
Hierarchical Data Structures
contd.
A few types are; Generalized Trees,
Binary Trees, Binary Search Trees, AVL
Trees (balanced binary search trees),
Splay Trees, B Trees, & P Trees.
Similar to linear data types, the basic
structure is the same.
Each version has different rules and
operations.
29
Graph Data Structures
A many:many
relationship between
elements in the
collection.
An element (E) in
graph can be
connected arbitrarily
to any other element
in the graph, (including
itself).
Conversely, any
number of elements
can be connected to E.
30
General model of a graph
Linear Data Structures
Traversal through a liner data structure
is called iteration.
The basic structures are the same.
The operations and restrictions are
different.
31
Operations on Linear Data
Structure
Traversal: Travel through the data structure.
Search: Traversal through the data structure
for a given element.
Insertion: Adding new elements to the data
structure.
Deletion: Removing an element from the
data structure.
Sorting: Arranging the elements in some
type of order.
Merging: Combining two similar data
structures into one.
32
Queue ADT
Queue operations
create
destroy
enqueue dequeue
enqueue
dequeue G FEDCB A
is_empty
Queue property: if x is enQed before y is
enQed, then x will be deQed before y is
deQed
FIFO: First In First Out
33
Applications of the Queue
Hold jobs for a printer
Store packets on network routers
Hold memory “freelists”
Make waitlists fair
Breadth first search
34
LIFO Stack ADT
Stack operations
create A EDCBA
destroy
push B
pop C
top D
is_empty E
Stack property: if x is F F before y is
on the stack
pushed, then x will be popped after y is
popped
LIFO: Last In First Out
35
Stacks in Practice
Function call stack
Removing recursion
Balancing symbols (parentheses)
Evaluating Reverse Polish Notation
Depth first search
36
Array Stack Data Structure
0 S size - 1
f e d c b
back
void push(Object x) {
assert(!is_full()) Object pop() {
S[back] = x back--
back++ return S[back]
}
Object top() {
}
assert(!is_empty()) bool is_full() {
return S[back - 1] return back == size
}
}
37
Linked List Stack Data
Structure
b c d e f
back
void push(Object x) {
temp = back
Object pop() {
back = new Node(x) assert(!is_empty())
back->next = temp return_data = back->data
}
temp = back
Object top() {
assert(!is_empty()) back = back->next
return back->data return return_data
}
}
38
Data Structures you should
already know
Arrays
Linked lists
Trees
Queues
Stacks
39
Proof by Induction
Basis Step:
The algorithm is correct for the base
case (e.g. n=0) by inspection.
Inductive Hypothesis (n=k):
Assume that the algorithm works
correctly for the first k cases, for any k.
Inductive Step (n=k+1):
Given the hypothesis above, show that
the k+1 case will be calculated
correctly.
40
Program Correctness by
Induction
Basis Step: sum(v,0) = 0.
Inductive Hypothesis (n=k):
Assume sum(v,k) correctly returns
sum of first k elements of v, i.e.
v[0]+v[1]+…+v[k-1]
Inductive Step (n=k+1):
sum(v,n) returns v[k]+sum(v,k)
which is the sum of first k+1 elements
of v.
41