![Page 1: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/1.jpg)
15.082J/6.855J/ESD.78J September 14, 2010
Data Structures
![Page 2: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/2.jpg)
2
Overview of this Lecture
A very fast overview of some data structures that we will be using this semester lists, sets, stacks, queues, networks, treesa variation on the well known heap data
structurebinary 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).
![Page 3: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/3.jpg)
3
Two standard data structures
1 0 1 1 0 1 0 1 0 0
1 2 3 4 5 6 7 8 9 10
Array: a vector: stored consecutively in memory, and typically allocated in advance
cells: hold fields of numbers and pointers to implement lists.
1 6 3 8 4
firstThis is a singly linked list
![Page 4: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/4.jpg)
Representations of subsets of a set
4
1 0 1 1 0 1 0 1 0 0
1 2 3 4 5 6 7 8 9 10
Array A
subset S = {1, 3, 4, 6, 8}
1 6 3 8 4
first List L
The choice of data structure depends on what operations need to be carried out.
![Page 5: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/5.jpg)
Example 1: Creating an empty set
5
0
first
Initialize: subset S = ∅
0 0 0 0 0
1 2 3 4 n… O(n) steps
O(1) steps
![Page 6: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/6.jpg)
Example 2: Is x S?∈
6
1 0 1 1 0 1 0 1 0 0
1 2 3 4 5 6 7 8 9 10
1 6 3 8 4
first To determine if 9 S?, one needs to scan the ∈entire list.
Is 9 S?∈
O(1) steps
O(n) steps
![Page 7: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/7.jpg)
7
Representing a linked list as an array
6 8 ∅ 3 4
1 2 3 4 5 6 7 8 9 10
1 6 3 8 4
first
Array: Next
If Next(j) is empty, then j is not on the list
If Next(j) = , then j is the last element on the list∅
![Page 8: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/8.jpg)
8
Two key concepts
Abstract data types: a descriptor of the operations that are permitted, e.g., abstract data type: set S initialize(S): creates an empty set Sadd(S, s): replaces S by S {s}.∪delete(S, s): replaces S by S \{s} IsElement(S, s): returns true if s ∈ Setc.
Data structure: usually describes the high level implementation of the abstract data types, and can be analyzed for running time.doubly linked list, etc
![Page 9: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/9.jpg)
9
A note on data structures
PreferencesSimplicityEfficiency In case there are multiple good
representations, we will choose one
![Page 10: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/10.jpg)
10
Abstract data type: SetOperations
Operation (assume S {1, 2, 3, … n}.⊆
initialize(S): S := ∅
add(S, j): S := S {j} ∪
delete(S, j): S := S\{j}
IsElement(S, j): returns TRUE if s ∈ S
FindElement(S): if S ≠ , returns j for some j S∅ ∈
Next(S, j) : if j S, it finds the next element ∈
after j on S (viewed as a list)
![Page 11: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/11.jpg)
Implementation using doubly linked list
11
42first 2 4
8 2
1 2 3 4 5 6 7 8 9 10
∅
8
4 ∅
1 2 3 4 5 6 7 8 9 10
2Next( )
Prev
First = 8
![Page 12: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/12.jpg)
Add element 5 to the set (in first position)
12
42first 2 4
8 2 ∅
8
4 ∅
1 2 3 4 5 6 7 8 9 10
2Next( )
Prev( )
Temp:= First
Temp
First 8
Elt 5
Prev(Temp): = Elt
First:= Elt
Prev(Elt): = ∅
Next(Elt): = Temp
5
5
![Page 13: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/13.jpg)
Add element 5 to the set (in first position)
13
42first 2 4
8 2 ∅
8
4 ∅
1 2 3 4 5 6 7 8 9 10
2Next( )
Prev( )
Temp:= First
Temp
First 8
8
Elt 5
Prev(Temp): = Elt
First:= Elt
Prev(Elt): = ∅
Next(Elt): = Temp
5
8
∅
5
5
Adding an element to a set takes O(1) steps using this implementation.
![Page 14: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/14.jpg)
Delete element 2 from the set (assume it is neither in first or last position)
14
42first 2 4
8 2 ∅
8
4 ∅
1 2 3 4 5 6 7 8 9 10
2Next( )
Prev( )Temp
First 8
Elt 2
5
8
∅
5
5
![Page 15: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/15.jpg)
Delete element 2 from the set (assume it is neither in first or last position)
15
2first 4
8 2 ∅
8 2
4 ∅
1 2 3 4 5 6 7 8 9 10
2Next( )
Prev( ) First 8
Elt 2
Prev(Elt) := 0
Next(Prev(Elt)):=Next(Elt)
Next(Elt): = 0
5
8
∅
5
5
Deleting an element from the set takes O(1) steps using this implementation.
4
Prev(Next(Elt)):=Prev(Elt)
8
![Page 16: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/16.jpg)
16
Operations using doubly linked lists
Operation Number of steps
initialize(S): O(n)
add(S, j): O(1)
delete(S, j): O(1)
IsElement(S, j): O(1)
FindElement(S): O(1)
Next(S, j): O(1)
Previous(S, j) : O(1)
![Page 17: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/17.jpg)
Maintaining disjoint subsets of elements
17
42First(1) 2 48
S1 = {8, 2, 4} S1 = {5, 7, 1}
42First(2) 7 15
7 8 2 5 ∅Prev( ) ∅∅28
4 ∅ 1
1 2 3 4 5 6 7 8 9 10
2Next( ) 7 24∅
8 5First( )
![Page 18: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/18.jpg)
Maintaining ordered lists
The doubly linked list is not efficient for maintaining ordered lists of nodes.
18
42first 4 82
4 ∅
1 2 3 4 5 6 7 8 9 10
2Next( )
8 2 ∅Prev( ) 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.
![Page 19: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/19.jpg)
Mental Break
19
Who created the cartoon characters "The Simpson's?
Matt Groening
Which country would you be in if you were to ski in the
Dolomites?
Italy
Who was the first U.S. president to adopt the informal
version of his first name?
Jimmy Carter
![Page 20: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/20.jpg)
Mental Break
20
Which country owns the island of Bermuda?
Great Britain
What colors do most color blind persons have trouble
distinguishing?
red and green
What bird is used as the sign of peace?
the dove
![Page 21: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/21.jpg)
Complete binary tree with n elementsComplete binary trees for storing ordered lists of
nodes or arcs. We assume that the number of nodes is n (e.g., 8) and the number of arcs is m.
21
S = {2, 4, 8} n = 8.
2 4
1 2 3 4 5 6 7
8
8
![Page 22: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/22.jpg)
Complete binary tree with n elementsBuild up the binary tree. In each parent store the
least value of its children.
22
2 4
1 2 3 4 5 6 7
8
S = {2, 4, 8} n = 8.
8
2 4 8
![Page 23: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/23.jpg)
Complete binary tree with n elementsBuild up the binary tree. In each parent store the
least value of its children.
23
2 4
1 2 3 4 5 6 7
8
S = {2, 4, 8} n = 8.
8
2 84
2 8
![Page 24: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/24.jpg)
Complete binary tree with n elementsBuild up the binary tree. In each parent store the
least value of its children.
24
2 4
1 2 3 4 5 6 7
8
S = {2, 4, 8} n = 8.
8
2 84
2 8
2
![Page 25: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/25.jpg)
Find greatest element less than je.g., find the greatest
element less than 7
25
2 4
1 2 3 4 5 6 7
8
8
2 84
2 8
2
5
5
5
2O(log n) steps for finding greatest element less than j.
start at 7go up the tree until a node has label < 7. Take left branch.Choose the largest label child going down.
![Page 26: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/26.jpg)
Delete an elemente.g., delete element 2
26
2 4
1 2 3 4 5 6 7
8
S = {4, 5, 8} n = 8.
8
2 84
2 8
2
5
5
5
2
O(log n) steps for an deletion
4
4
Start at node 2 and update it and its ancestors.
![Page 27: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/27.jpg)
Operations using complete binary trees
27
Operation Number of steps
initialize(S): O(n)
add(S, j): O(log n)
delete(S, j): O(log n)
IsElement(S, j): O(1)
FindElement(S): O(1)
Next(S, j): O(log n)
Previous(S, j) : O(log n)
MinElement(S) O(1)
MaxElement(S) O(log n)
![Page 28: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/28.jpg)
28
A network
1
2
3
45
We can view the arcs of a networks as a collection of sets.
Let A(i) be the arcs emanating from node i.
e.g., A(5) = { (5,3), (5,4) }
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.
![Page 29: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/29.jpg)
29
Storing Arc Lists: A(i)
Operations permittedFind first arc in A(i)Store a pointer to the current arc in A(i)Find the arc after CurrentArc(i)
i j cij uij
2 4 15 40
3 2 45 10 5 45 60
5 3 25 20 4 35 50
4
1 2 25 30 5 15 403 23 35
![Page 30: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/30.jpg)
30
Scanning the arc list
CurrentArc(i) is a pointer to the arc of A(i) that is being scanned or is the next to be scanned.
1 2 25 30 5 15 403 23 35
CurrentArc(1) CurrentArc(1) CurrentArc(1)
Initially, CurrentArc(i) is the first arc of A(i)
After CurrentArc(i) is fully scanned, CurrentArc(i) := Next(CurrentArc(i))
![Page 31: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/31.jpg)
31
Scanning the arc list
CurrentArc(i) is a pointer to the arc of A(i) that is being scanned or is the next to be scanned.
1 2 25 30 5 15 403 23 35
1
2
3
5
CurrentArc(1) CurrentArc(1) CurrentArc(1)
Finding CurrentArc and the arc after CurrentArc takes O(1) steps.
These are also implemented often using arrays called forward star representations.
![Page 32: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/32.jpg)
The Adjacency Matrix (for directed graphs)
2
34
1
a
b
c
d
e
A Directed Graph
0 1 0 1
0 0 1 0
0 0 0 0
0 1 1 0
•Have a row for each node
1 2 3 4
1
2
3
4
•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
![Page 33: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/33.jpg)
The Adjacency Matrix (for undirected graphs)
0 1 0 1
1 0 1 1
0 1 0 1
1 1 1 0
•Have a row for each node
1 2 3 4
1
2
3
4
•Have a column for each node
•Put a 1 in row i- column j if (i,j) is an arc
2
34
1
a
b
c
d
e
An Undirected Graph
The degree of a node is the number of incident arcs
degree
2
3
2
3
33
![Page 34: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/34.jpg)
34
Adjacency Matrix vs Arc Lists
Adjacency Matrix?Efficient storage if matrix is very “dense.”
Can determine if (i,j) A(i) in O(1) steps.∈Scans arcs in A(i) in O(n) steps.
Adjacency lists?
Efficient storage if matrix is “sparse.”
Determining if (i,j) A(i) can take |A(i)| steps∈Can scan all arcs in A(i) in |A(i)| steps
![Page 35: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/35.jpg)
35
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.
1
2
3
6
54
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.)
3
![Page 36: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/36.jpg)
1
2
3
6
5
9
10
84
7
36
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.
To store trees efficiently, each subtree has a root node.
31
10
![Page 37: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/37.jpg)
37
One way of storing subtrees
1
2
3
6
54
3
2
3 1 6
5
4
1 4 2
6 5
Lists of children
parent (predecessor) array
3 1 1 6 3
1 2 3 4 5 6node
parent
![Page 38: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/38.jpg)
On storing trees
Trees are important parts of flow algorithms
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
1
2
3
6
54
3
![Page 39: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/39.jpg)
39
Stacks -- Last In, First Out (LIFO)
Operations: create(S) creates an empty stack S push(S, j) adds j to the top of the stack pop(S) deletes the top element in S top(S) returns the top element in S
3
7
2
5
6
pop(S) pop(S)
3
7
2
5
6
3
7
2
5
3
7
2
9push(S,9)
![Page 40: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/40.jpg)
40
Queues – First in, First Out (FIFO)
Operations: create(Q) creates an empty queue Q Insert(Q, j) adds j to the end of the queue Delete(Q) deletes the first element in Q first(Q) returns the top element in S
37256 Delete(Q)
37256 Delete(Q)
3725 Insert(Q,9)
372 9
![Page 41: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/41.jpg)
41
Binary Search
2 4 6 9 13 17 22 24 27 31 33 36 42 45
1 2 3 4 5 6 7 8 9 10 11 12 13 14
In the ordered list of numbers below, stored in an array, determine whether the number 25 is on the list.
Left Right
![Page 42: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/42.jpg)
42
Binary Search
2 4 6 9 13 17 22 24 27 31 33 36 42 45
1 2 3 4 5 6 7 8 9 10 11 12 13 14
In the ordered list of numbers below, stored in an array, determine whether the number 25 is on the list.
Left RightLeft
![Page 43: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/43.jpg)
43
Binary Search
22 24 27 31 33 36 42 45
7 8 9 10 11 12 13 14
In the ordered list of numbers below, stored in an array, determine whether the number 25 is on the list.
RightLeft 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.
![Page 44: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/44.jpg)
44
Summary
Review of data structuresLists, sets, complete binary trees, treesqueues, stacksbinary search
Next Lecture: Search algorithms
![Page 45: 15.082J/6.855J/ESD.78J September 14, 2010 Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062715/56649d965503460f94a7fb4d/html5/thumbnails/45.jpg)
MITOpenCourseWarehttp://ocw.mit.edu
15.082J / 6.855J / ESD.78J Network Optimization Fall 2010
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.