Data Structures and Algorithms
In the following sections, we will explore more about Data Structures and Algorithms.
In the meantime, these topics will also serve as an introductory part to Java Programming.
1 Data Structures and Algorithms Overview
1.1 Data Storage & Logical Structures
1.1.1 Data Storage Structures
Sequential Storage Structures:
Linear list
Array
Vector
Linked Storage Structure
Linked list
Index Storage Structure
B-Tree/B+-Tree
Hashing Storage Structure
Hash table
1.1.2 Data Logical Structures
Set
Linear
Tree
Graph
1.2 Mathematical Models
Simplifications:
Cost Model: Use some basic operations as a proxy for running time.
Operation
Frequency
Variable declaration
Assignment statement
Less than compare
Equal to compare
Array access
Increment
to Estimate running time (or memory) as a function of input size
Ignore lower order terms.
When
is large, terms are negligible. When
is small, we don't care.
1.3 Order-of-Growth Classifications
1.3.1 Common Classifications
Order of growth | Name | Typical code Framework | Decription | Example | T(2N)/T(N) |
---|---|---|---|---|---|
Constant | a = b + c; | statement | add two numbers | ||
Logarithmic |
while (N > 1)
{N = N / 2; ...}
| divide in half | binary search | ||
Linear |
for (int i = 0; i < N; i++)
{...}
| loop | find the maximum | 2 | |
Linearithmatic | divide and conquer | mergesort | |||
Quadratic |
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{...}
| double loop | check all pairs | ||
Cubic |
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
for (int k = 0; k < N; k++)
{...}
| triple loop | check all triples | 8 | |
Exponential | see combinatorial search lecture | exhaustive search | check all subsets | T(N) |
1.3.2 Binary Search
Property: Binary search uses at most
Proof:
Binary Seach
Compare key against middle entry.
Too small, go left.
Too big, go right.
Equal, found.
1.3.3 3-Sum
Description:
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]]
such that i != j
, i != k
, and j != k
, and nums[i] + nums[j] + nums[k] == 0
.
3-Sum
Sort the
(distinct) numbers. For each pair of numbers
a[i]
anda[j]
, binary search for-(a[i] + a[j])
.
Analysis: Order of growth is
Step 1:
with insertion sort. Step 2:
with binary search.
Better Algorithm: Sorting & Two pointers =>
1.4 Theory of Algorithms
Notion | Formal Definition | Provides | Shorthand for | Example | Used to |
---|---|---|---|---|---|
Big Theta | means there are positive constants | asymptotic order of growth | Classify Algorithms | ||
Big Oh | means there is a positive constant | Develop Upper Bounds | |||
Big Omega | means there is a positive constant | Develop Lower Bounds |