Aflv 02 Data Structures
Aflv 02 Data Structures
2
Basic data types
3
Basic data types
Type Bytes Min value Max value
bool 1
char 1 -128 127
short 2 -32768 32767
int 4 -2148364748 2147483647
long long 8 -9223372036854775808 9223372036854775807
n −28n−1 28n−1 − 1
4
Big integers
▶ What if we need to represent and do computations
with very large integers, i.e. something that doesn’t fit
in a long long
▶ Simple idea: Store the integer as a string
▶ But how do we perform arithmetic on a pair of strings?
▶ We can use the same algorithms as we learned in
elementary school
– Addition: Add digit-by-digit, and maintain the carry
– Subtraction: Similar to addition
– Multiplication: Long multiplication
– Division: Long division
– Modulo: Long division
5
Example problem: Integer Inquiry
▶ http://uva.onlinejudge.org/external/4/424.html
6
Why do we need data structures?
7
Data structures you’ve seen before
▶ Static arrays
▶ Dynamic arrays
▶ Linked lists
▶ Stacks
▶ Queues
▶ Priority Queues
▶ Sets
▶ Maps
8
Data structures you’ve seen before
▶ Static arrays - int arr[10]
▶ Dynamic arrays - vector<int>
▶ Linked lists - list<int>
▶ Stacks - stack<int>
▶ Queues - queue<int>
▶ Priority Queues - priority_queue<int>
▶ Sets - set<int>
▶ Maps - map<int, int>
8
Data structures you’ve seen before
▶ Static arrays - int arr[10]
▶ Dynamic arrays - vector<int>
▶ Linked lists - list<int>
▶ Stacks - stack<int>
▶ Queues - queue<int>
▶ Priority Queues - priority_queue<int>
▶ Sets - set<int>
▶ Maps - map<int, int>
9
Sorting and searching
9
Representing sets
10
Representing sets
▶ Empty set:
0
▶ Single element set:
1<<i
▶ The universe set (i.e. all elements):
(1<<n)-1
▶ Union of sets:
x|y
▶ Intersection of sets:
x&y
▶ Complement of a set:
~x & ((1<<n)-1)
11
Representing sets
12
Representing sets
13
Applications of Arrays and Linked Lists
14
Example problem: Broken Keyboard
▶ http://uva.onlinejudge.org/external/119/11988.html
15
Applications of Stacks
▶ Processing events in a first-in first-out order
▶ Simulating recursion
▶ Depth-first search in a graph
▶ Reverse a sequence
▶ Matching brackets
▶ And a lot more
16
Applications of Queues
▶ Processing events in a first-in first-out order
▶ Breadth-first search in a graph
▶ And a lot more
17
Applications of Priority Queues
▶ Processing events in order of priority
▶ Finding a shortest path in a graph
▶ Some greedy algorithms
▶ And a lot more
18
Applications of Sets
▶ Keep track of distinct items
▶ Have we seen an item before?
▶ If implemented as a binary search tree:
– Find the successor of an element (the smallest element
that is greater than the given element)
– Count how many elements are less than a given element
– Count how many elements are between two given
elements
– Find the kth largest element
▶ And a lot more
19
Applications of Maps
▶ Associating a value with a key
▶ As a frequency table
▶ As a memory when we’re doing Dynamic
Programming (later)
▶ And a lot more
20
Augmenting Data Structures
▶ Sometimes we can store extra information in our data
structures to gain more functionality
▶ Usually we can’t do this to data structures in the
standard library
▶ Need our own implementation that we can customize
▶ Example: Augmenting binary search trees
21
Augmenting Binary Search Trees
33
▶ We have a binary
search tree and want
to efficiently: 15 47
– Count number of
elements < x 10 20 38 51
– Find the kth smallest
element
5 18 36 39 49
▶ Naive method is to go
through all vertices,
34 37
but that is slow: O(n)
22
Augmenting Binary Search Trees
33, 14
▶ Idea: In each vertex
store the size of the
15, 5 47, 8
subtree
▶ This information can
10, 2 20, 2 38, 5 51, 2
be maintained when
we insert/delete
5, 1 18, 1 36, 3 39, 1 49, 1
elements without
adding time
34, 1 37, 1
complexity
23
Augmenting Binary Search Trees
▶ Count number of
elements < 38 33, 14
– When we are at a
vertex where we
5, 1 18, 1 36, 3 39, 1 49, 1
should go right, get
the size of the left
subtree and add it to 34, 1 37, 1
our count
24
Augmenting Binary Search Trees
▶ Count number of
elements < 38 33, 14
– When we are at a
vertex where we
5, 1 18, 1 36, 3 39, 1 49, 1
should go right, get
the size of the left
subtree and add it to 34, 1 37, 1
our count
▶ Time complexity
O(log n)
25
Augmenting Binary Search Trees
▶ Find kth smallest
element 33, 14
– We’re on a vertex
whose left subtree is 15, 5 47, 8
of size m
– If k = m + 1, we
found it 10, 2 20, 2 38, 5 51, 2
26
Augmenting Binary Search Trees
▶ Find kth smallest
element 33, 14
– We’re on a vertex
whose left subtree is 15, 5 47, 8
of size m
– If k = m + 1, we
found it 10, 2 20, 2 38, 5 51, 2
27
Representing graphs
28
Adjacency list
0: 1, 2
0
1: 0, 2
2: 0, 1, 3
3: 2
1 2
vector<int> adj[4];
adj[0].push_back(1);
adj[0].push_back(2);
3
adj[1].push_back(0);
adj[1].push_back(2);
adj[2].push_back(0);
adj[2].push_back(1);
adj[2].push_back(2);
adj[3].push_back(2);
29
Adjacency matrix
0 1 1 0
0
1 0 1 0
1 1 0 1
0 0 1 0
1 2
bool adj[4][4];
adj[0][1] = true;
adj[0][2] = true;
3
adj[1][0] = true;
adj[1][2] = true;
adj[2][0] = true;
adj[2][1] = true;
adj[2][3] = true;
adj[3][2] = true;
30
Edge list
0, 1
0
0, 2
1, 2
2, 3
1 2
31
Efficiency
32
Example problem: Easy Problem from Rujia
Liu?
▶ http://uva.onlinejudge.org/external/119/11991.html
33