15.082J/6.855J/ESD.78J September 14, 2010: Data Structures
15.082J/6.855J/ESD.78J September 14, 2010: Data Structures
Data Structures
A very fast overview of some data structures that we will be using this semester lists, sets, stacks, queues, networks, trees a variation on the well known heap data structure binary search Illustrated using animation We are concerned with O( ) computation counts, and so do not need to get down to C++- level (or Java level).
1 2 3 4 5 6 7 8 9 10
Array A 1 0 1 1 0 1 0 1 0 0
first
List L
The choice of data structure depends on what operations need to be carried out.
Example 1:
Initialize: subset S =
1 2 3 4
0 0 0 0
n
0 O(n) steps
first
O(1) steps 0
5
Example 2:
Is 9 S?
Is x S?
1 2 3 4 5 6 7 8 9 10
1 0 1 1 0 1 0 1 0 0 O(1) steps
first
4
O(n) steps
6
1 2 3 4 5 6 7 8 9 10 Array: Next 6 8 3 4
If Next(j) is empty, then j is not on the list If Next(j) = , then j is the last element on the list
7
Data structure: usually describes the high level implementation of the abstract data types, and can be analyzed for running time. doubly linked list, etc
8
Preferences Simplicity Efficiency In case there are multiple good representations, we will choose one
Operation
Next(S, j) :
First = 8
1 2 3 4 5 6 7 8 9 10
Next( ) 4 2
1 2 3 4 5 6 7 8 9 10 Prev 8 2
11
5 8
First
Temp
8 5
8
Temp:= First
Delete element 2 from the set (assume it is neither in first or last position)
5 first 2 8 4 2 4
First
Temp
8 5
14
Delete element 2 from the set (assume it is neither in first or last position)
5 first 2 8 2 4
First
8 5
Next(Prev(Elt)):=Next(Elt)
Deleting an element from the set takes O(1) steps using this implementation.
15
Operation
delete(S, j):
IsElement(S, j): FindElement(S): Next(S, j): Previous(S, j) :
O(1)
O(1) O(1) O(1) O(1)
16
7 2
1 2 5
1 2 3 4 5 6 7 8 9 10
Next( )
Prev( )
4
8
2
5
Inserting an element into the set (such as 7) requires finding Prev and Next for 7 (4 and 8), and this requires O(n) time with this implementation.
18
Mental Break
Who created the cartoon characters "The Simpson's?
Matt Groening
Jimmy Carter
19
Mental Break
Which country owns the island of Bermuda?
Great Britain
20
21
S = {2, 4, 8}
n = 8.
2 2
4 4
8 8
22
S = {2, 4, 8}
n = 8.
2 2
4 4
8 8
23
2 2
4 4
8 8
24
8 5
2 2
4 4 5
8 8
25
Delete an element
e.g., delete element 2 O(log n) steps for an deletion Start at node 2 and update it and its ancestors. 2 4 S = {4, 5, 8} n = 8.
2 4
8 5
2 2
4 4 5
8 8
26
FindElement(S):
Next(S, j): Previous(S, j) : MinElement(S) MaxElement(S)
O(1)
O(log n) O(log n) O(1) O(log n)
27
A network
2 We can view the arcs of a networks as a collection of sets. 3 Let A(i) be the arcs emanating from node i.
Note: these sets are usually static. They stay the same.
Common operations: scanning the list A(i) one arc at a time starting at the first arc.
28
3
4 5
3 25 20
4 35 50
29
2 25 30
3 23 35
5 15 40
30
2 25 30
3 23 35
5 15 40
2 1 3 5
Finding CurrentArc and the arc after CurrentArc takes O(1) steps. These are also implemented often using arrays called forward star representations.
31
1 2
a 4
A Directed Graph
1 2 3 4
0 0 0 0
1 0 1 0 1 0 0 0 0 1 1 0
Have a column for each node Put a 1 in row i- column j if (i,j) is an arc
What would happen if (4,2) became (2,4)?
32
1 2
4 degree
1 2 3 4
0 1 0 1
1 0 1 0 1 1 1 0 1 1 1 0
2 3 2 3
An Undirected Graph
Have a column for each node Put a 1 in row i- column j if (i,j) is an arc The degree of a node is the number of incident arcs
33
34
Trees
A tree is a connected acyclic graph. (Acyclic here, means it has no undirected cycles.) If a tree has n nodes, it has n-1 arcs. 3 This is an undirected tree. To store trees efficiently, we hang the tree from a root node. (In principle, any node can be selected for the root.) 4 2 5
35
Forest
A forest is an acyclic graph that includes all of the nodes. A subtree of a forest is a connected component of the forest. 10 3 1 6 9 2 5 7 To store trees efficiently, each subtree has a root node. 4 8
36
1 2
4
5 4 2 node parent 5 1 3 2 1 6 3 4 1
5
5 6
6 3
37
On storing trees
Trees are important parts of flow algorithms
3
Some data structures are expressed as trees
The best implementation of trees depends on what operations need to be performed in the abstract data type.
38
pop(S)
pop(S)
5 2 7 3
push(S,9)
2 7 3
39
Binary Search
In the ordered list of numbers below, stored in an array, determine whether the number 25 is on the list.
10 11 12 13 14
13 17 22 24 27 31 33 36 42 45
Left
Right
41
Binary Search
In the ordered list of numbers below, stored in an array, determine whether the number 25 is on the list.
10 11 12 13 14
13 17 22 24 27 31 33 36 42 45
Left
Left
Right
42
Binary Search
In the ordered list of numbers below, stored in an array, determine whether the number 25 is on the list.
10 11 12 13 14
22 24 27 31 33 36 42 45
Left
Right
Right
After two more iterations, we will determine that 25 is not on the list, but that 24 and 27 are on the list. Running time is O( log n) for running binary search.
43
Summary
Review of data structures Lists, sets, complete binary trees, trees queues, stacks binary search Next Lecture: Search algorithms
44
MITOpenCourseWare http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.