cs 61b: final review data structures steve sinha and winston liaw

91
CS 61b: Final Review Data Structures Data Structures Steve Sinha and Winston Liaw

Post on 19-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

CS 61b: Final Review

Data StructuresData Structures

Steve Sinha and Winston Liaw

Page 2: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 2

DISCLAIMER

We have NOTNOT seen the exam.

We do NOTNOT know the format of the exam

What we are presenting is what we

“think is important” for the exam

Page 3: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 3

Review Topics

Inheritance, Method Calls Asymptotic Analysis Data Structures

Binary Search Trees B-Trees Heaps Hash Tables

Graphs DFS, BFS Topological Sort Dijkstra Kruskal

Sorting Skip Lists Threading, Synchronization

Page 4: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 4

Inheritance/Method Calls

Given the class definitions on the next slide, which lines in class foobarbaz are illegal?

Page 5: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 5

Inheritancepackage foo;public class foo { static void f1() {…} protected boolean f2(int x) {…} private String f3(String s) {…}}

package bar;import foo.foo;public class bar extends foo { protected boolean f3(int x) {…}}

package foo;public class baz extends foo { private String f3(String s) {…}}

package foo;import bar.bar;public class foobarbaz { static void main(String[] args) { foo f = new foo(); bar r = new bar(); baz z; r.f3(3); f.f2(3); z = (baz) f; f = new baz(); f.f2(3); z = (baz) f; z.f1(); r.f1(); ((foo) r).f1(); }}

Page 6: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 6

Inheritance/Method Calls

Access table:

Static methods called according to static type Child type can be assigned to parent variable

without a cast, but the reverse requires one, and the dynamic types must match

world package child definer

public X X X X

private X

protected X X X

<default> X X

Page 7: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 7

Inheritancepackage foo;public class foo { static void f1() {…} protected boolean f2(int x) {…} private String f3(String s) {…}}

package bar;import foo.foo;public class bar extends foo { protected boolean f3(int x) {…}}

package foo;public class baz extends foo { private String f3(String s) {…}}

package foo;import bar.bar;public class foobarbaz { static void main(String[] args) { foo f = new foo(); bar r = new bar(); baz z; r.f3(3); // ILLEGAL f.f2(3); z = (baz) f; // ILLEGAL f = new baz(); f.f2(3); z = (baz) f; z.f1(); r.f1(); // ILLEGAL ((foo) r).f1(); }}

Page 8: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 8

Asymptotic Analysis

O – Upper bound/Worst case Ω – Lower bound θ – both o – strictly Upper bound

More detail…

Page 9: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 9

Asymptotic Analysis

T(n) is O( f(n) ) if and only if there exists positive constants C and N such that

T(n) <= C f(n) for all n >= N

n

T(n)

5 f(n)

f(n)

T(n) = 4nf(n) = n

4n is O( n )

Page 10: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 10

Asymptotic Analysis

T(n) is O( f(n) ) if and only if there exists positive constants C and N such that

T(n) <= C f(n) for all n >= NN

n

T(n)

C f(n)

Page 11: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 11

Asymptotic Analysis

T(n) is O( f(n) ) if and only if there exists positive constants C and N such that

T(n) <= C f(n) for all n >= N

T(n) is Ω( f(n) ) if and only if there exists positive constants C and N such that

T(n) >= C f(n) for all n >= N

Page 12: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 12

Asymptotic Analysis

T(n) is θ( f(n) ) if and only if T(n) is O( f(n) )

and T(n) is Ω( f(n) )

Examples

5n2+1 is θ(n2)

3n is O(n2), but 3n is NOT θ(n2)because 3n is not Ω(n2)

Page 13: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 13

Asymptotic Analysis Problem

Find the running time of the following code:

int foo(int x) { int ans = 1; for (int i = 0; i < x; i++) { for (int j = 0; j < i; j++) { ans *= (i + j); } } return ans;}

Page 14: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 14

Asymptotic Analysis Solution

The nested loops give away the answer: the outer loop executes x times, the inner loop an average of x/2 times, for a running time of O(x2).

int foo(int x) { int ans = 1; for (int i = 0; i < x; i++) { for (int j = 0; j < i; j++) { ans *= (i + j); } } return ans;}

Page 15: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 15

Trees: Binary Tree

Tree: AB

CDE

FG

Preorder : ABCEGFD

Inorder : CEBAGDF

Postorder: ECBDFGA

What are the Pre-, In-, and Post- order traversals of this tree?

Page 16: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 16

Trees: BST Problem Remove 8 from:

8

75

3

11

13

1

6

9

Page 17: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 17

Trees: BST Problem Remove 8 from:

8

75

3

11

13

1

Replace with successor (left-most node in Replace with successor (left-most node in right subtree)right subtree)

6

9

Page 18: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 18

Trees: BST Solution Final tree:

9

75

3

11

13

1

6

Page 19: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 19

Trees: B-Tree of Order 4 / 2-3-4 Tree Suggest a sequence of operations that

would create the 2-3-4 tree.You can use removal as well as insertion.

5, 7

2, 4 6 8

Page 20: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 20

Trees: 2-3-4 Tree Solution

Insert: 4, 5, 6, 7, 8, 9, 2 Remove: 9

4, 5, 6

Page 21: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 21

Trees: 2-3-4 Tree Solution

Insert: 4, 5, 6, 7, 8, 9, 2 Remove: 9

4, 5, 6 Touch node size = 3Touch node size = 3

Page 22: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 22

Trees: 2-3-4 Tree Solution

Insert: 4, 5, 6, 7, 8, 9, 2 Remove: 9

5

6, 74

Page 23: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 23

Trees: 2-3-4 Tree Solution

Insert: 4, 5, 6, 7, 8, 9, 2 Remove: 9

5

6, 7, 84

Page 24: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 24

Trees: 2-3-4 Tree Solution

Insert: 4, 5, 6, 7, 8, 9, 2 Remove: 9

5

6, 7, 84 Touch node size = 3Touch node size = 3

Page 25: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 25

Trees: 2-3-4 Tree Solution

Insert: 4, 5, 6, 7, 8, 9, 2 Remove: 9

5, 7

4 6 8, 9

Page 26: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 26

Trees: 2-3-4 Tree Solution

Insert: 4, 5, 6, 7, 8, 9, 2 Remove: 9

5, 7

2, 4 6 8

Page 27: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 27

Priority Queues – Problem

Add 9, 76, 54, 3, 33, 21 to a max heap, using only the array based representation

Page 28: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 28

Priority Queues – Insertion

Insert at the last position in the heap Reheapify up: if the element is greater than

its parent, swap them and repeat For an element at position n, its children are

at 2n+1 and 2n+2 For an element at position n, its parent is at

floor[(n-1)/2]

Page 29: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 29

Priority Queues – Solution

Add 9, 76, 54, 3, 33, 21 to a max heap, using only the array based representation

9 …

0 1 2 3 4 5 …

Page 30: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 30

Priority Queues – Solution

Add 9, 76, 54, 3, 33, 21 to a max heap, using only the array based representation

9 76 …

0 1 2 3 4 5 …

76 9 …

0 1 2 3 4 5 …

Page 31: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 31

Priority Queues – Solution

Add 9, 76, 54, 3, 33, 21 to a max heap, using only the array based representation

76 9 54 …

0 1 2 3 4 5 …

Page 32: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 32

Priority Queues – Solution

Add 9, 76, 54, 3, 33, 21 to a max heap, using only the array based representation

76 9 54 3 …

0 1 2 3 4 5 …

Page 33: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 33

Priority Queues – Solution

Add 9, 76, 54, 3, 33, 21 to a max heap, using only the array based representation

76 9 54 3 33 …

0 1 2 3 4 5 …

76 33 54 3 9 …

0 1 2 3 4 5 …

Page 34: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 34

Priority Queues – Solution

76 33 54 3 9 21 …

0 1 2 3 4 5 …

7633 54

3 9 21

Tree Representation

Add 9, 76, 54, 3, 33, 21 to a max heap, using only the array based representation

Page 35: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 35

Priority Queues – Problem

76 33 54 3 9 21 …

0 1 2 3 4 5 …

Remove the max from the heap

Page 36: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 36

Priority Queues – Removal

Replace the max element with the last element in the heap

Reheapify down: if one or both of its children is larger than it, swap with the larger of the children and repeat

For an element at position n, its children are at 2n+1 and 2n+2

For an element at position n, its parent is at floor[(n-1)/2]

Page 37: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 37

Priority Queues – Solution

76 33 54 3 9 21 …

0 1 2 3 4 5 …

21 33 54 3 9 …

0 1 2 3 4 5 …

Remove the max from the heap

Page 38: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 38

Priority Queues – Solution

21 33 54 3 9 …

0 1 2 3 4 5 …

54 33 21 3 9 …

0 1 2 3 4 5 …

Remove the max from the heap

Page 39: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 39

Priority Queues – Solution

54 33 21 3 9 …

0 1 2 3 4 5 …

5433 21

3 9

Tree Representation

Remove the max from the heap

Page 40: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 40

Hash Table Problem

Draw the structure of a size 7 hash table after insertion of keys with the following hash codes: 0, 95, 21, 6, 64, 74, 3, 54, 34, 75, 10.

Page 41: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 41

Hash Tables

High-level idea – 2 components1. Big array called hash table of size M

2. Function h which maps keys to integer values For (key, item), use h(key) % M to find

location of item in table Linked list in each entry that stores all items

that map to that location (chaining)

Page 42: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 42

Hash Table Solution

Draw the structure of a size 7 hash table after insertion of keys with the following hash codes: 0, 95, 21, 6, 64, 74, 3, 54, 34, 75, 10.

0 1 2 3 4 5 6

0

21

64 3

10

95

74

54

75

6

34

Page 43: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 43

Searches (BFS and DFS)

BFS uses a queue, DFS uses a stackpublic void BFS/DFS(Node start) {

Queue/Stack s = new Queue/Stack();s.enqueue/push(start);while (!s.empty()) {

Node n = s.dequeue/pop();mark(n);for (all children that are not yet marked) {

s.enqueue/push(child);}

}}

Page 44: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 44

Searches (BFS and DFS) Problem Perform BFS and DFS on the graph, starting

at node 1

5

326

74

1

Page 45: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 45

Searches (BFS and DFS) Solution Perform BFS and DFS on the graph, starting

at node 1

5

326

74

1BFS

1253467

DFS1234567

Page 46: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 46

Example graph

Introduction

Kiss

Date

I can’t say whatgoes in this box

Flirt

Hug Personal Ad

Give me an ordering that I can do these in so that I don’t violate the dependencies (or my date).

Page 47: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 47

Topological Sort

Topological sorting gives us an ordering which won’t violate these dependencies. Perform a DFS from each source (root), marking

start and finish times. Now, our ordering is simply the nodes we visited

in decreasing finishing time.

Page 48: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 48

Example graph

Introduction

Kiss

Date

I can’t say whatgoes in this box

Flirt

Hug Personal Ad

1. Personal Ad2. Introduction3. Flirt4. Date5. Hug6. Kiss7. Umm…

5/8

1/12

6/7

4/9

2/11 3/10

13/14

Page 49: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 49

Dijkstra’s Algorithm Problem

5

326

74

1

10

63

13

5

1 12

8

16

4

23

7

9

Find the shortest distances to each node from node 1

Page 50: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 50

Dijkstra’s Algorithm

Set all distances initially to ∞, except the start node, which should be set to 0

Construct a min priority queue of the nodes, with their distances as keys

Repeatedly remove the minimum element, updating each of its adjacent node’s distances if they are still in the queue and if the updated distance is less than the current distance

Page 51: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 51

Dijkstra’s Algorithm Solution

5

326

74

1

10

63

13

5

1 12

8

16

4

23

7

9

P.Q. 1 (0) 2 (∞) 3 (∞) 4 (∞) 5 (∞) 6 (∞) 7 (∞)

∞∞

0

Find the shortest distances to each node from node 1

Page 52: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 52

Dijkstra’s Algorithm Solution

5

326

74

1

10

63

13

5

1 12

8

16

4

23

7

9

P.Q.x1 (0) 2 (3) 3 (9) 5 (13) 4 (∞) 6 (∞) 7 (∞)

13

∞3

9

0

Find the shortest distances to each node from node 1

Page 53: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 53

Dijkstra’s Algorithm Solution

5

326

74

1

10

63

13

5

1 12

8

16

4

23

7

9

P.Q.x1 (0)x2 (3) 3 (9) 5 (13) 4 (13) 6 (∞) 7 (∞)

13

∞3

13

9

0

Find the shortest distances to each node from node 1

Page 54: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 54

Dijkstra’s Algorithm Solution

5

326

74

1

10

63

13

5

1 12

8

16

4

23

7

9

P.Q.x1 (0)x2 (3)x3 (9) 5 (10) 4 (11) 7 (16) 6 (∞)

10

∞3

11

9

16

0

Find the shortest distances to each node from node 1

Page 55: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 55

Dijkstra’s Algorithm Solution

5

326

74

1

10

63

13

5

1 12

8

16

4

23

7

9

P.Q.x1 (0)x2 (3)x3 (9)x5 (10) 4 (11) 7 (15) 6 (18)

10

18

3

11

9

15

0

Find the shortest distances to each node from node 1

Page 56: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 56

Dijkstra’s Algorithm Solution

5

326

74

1

10

63

13

5

1 12

8

16

4

23

7

9

P.Q.x1 (0)x2 (3)x3 (9)x5 (10)x4 (11) 7 (14) 6 (15)

10

15

3

11

9

14

0

Find the shortest distances to each node from node 1

Page 57: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 57

Dijkstra’s Algorithm Solution

5

326

74

1

10

63

13

5

1 12

8

16

4

23

7

9

P.Q.x1 (0)x2 (3)x3 (9)x5 (10)x4 (11)x7 (14) 6 (15)

10

15

3

11

9

14

0

Find the shortest distances to each node from node 1

Page 58: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 58

Dijkstra’s Algorithm Solution

5

326

74

1

10

63

13

5

1 12

8

16

4

23

7

9

P.Q.x1 (0)x2 (3)x3 (9)x5 (10)x4 (11)x7 (14)x6 (15)

10

15

3

11

9

14

0

Find the shortest distances to each node from node 1

Page 59: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 59

Kruskal’s Algorithm Problem

5

326

74

1

10

63

13

5

1 12

8

16

4

2

Find the MST of the graph, using Kruskal’s Algorithm

Page 60: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 60

Kruskal’s Algorithm

Put each node into a set by itself Sort all the edges in ascending order by their

weights Pick the least-weight edge, if the edge

connects two nodes in different sets, add the edge to the MST and merge the two sets

Page 61: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 61

Kruskal’s Algorithm Solution

5

326

74

1

10

63

13

5

1 12

8

16

4

Edges 3-5 (1) 3-4 (2) 1-2 (3) 4-6 (4) 5-7 (5) 2-3 (6) 5-6 (8) 2-4 (10) 5-6 (12) 1-5 (13) 6-7 (16)

2

Find the MST of the graph, using Kruskal’s Algorithm

Page 62: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 62

Kruskal’s Algorithm Solution

5

326

74

1

10

63

13

5

1 12

8

16

4

Edgesx3-5 (1) 3-4 (2) 1-2 (3) 4-6 (4) 5-7 (5) 2-3 (6) 5-6 (8) 2-4 (10) 5-6 (12) 1-5 (13) 6-7 (16)

2

Find the MST of the graph, using Kruskal’s Algorithm

Page 63: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 63

Kruskal’s Algorithm Solution

5

326

74

1

10

63

13

5

1 12

8

16

4

Edgesx3-5 (1)x3-4 (2) 1-2 (3) 4-6 (4) 5-7 (5) 2-3 (6) 5-6 (8) 2-4 (10) 5-6 (12) 1-5 (13) 6-7 (16)

2

Find the MST of the graph, using Kruskal’s Algorithm

Page 64: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 64

Kruskal’s Algorithm Solution

5

326

74

1

10

63

13

5

1 12

8

16

4

Edgesx3-5 (1)x3-4 (2)x1-2 (3) 4-6 (4) 5-7 (5) 2-3 (6) 5-6 (8) 2-4 (10) 5-6 (12) 1-5 (13) 6-7 (16)

2

Find the MST of the graph, using Kruskal’s Algorithm

Page 65: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 65

Kruskal’s Algorithm Solution

5

326

74

1

10

63

13

5

1 12

8

16

4

Edgesx3-5 (1)x3-4 (2)x1-2 (3)x4-6 (4) 5-7 (5) 2-3 (6) 5-6 (8) 2-4 (10) 5-6 (12) 1-5 (13) 6-7 (16)

2

Find the MST of the graph, using Kruskal’s Algorithm

Page 66: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 66

Kruskal’s Algorithm Solution

5

326

74

1

10

63

13

5

1 12

8

16

4

Edgesx3-5 (1)x3-4 (2)x1-2 (3)x4-6 (4)x5-7 (5) 2-3 (6) 5-6 (8) 2-4 (10) 5-6 (12) 1-5 (13) 6-7 (16)

2

Find the MST of the graph, using Kruskal’s Algorithm

Page 67: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 67

Kruskal’s Algorithm Solution

5

326

74

1

10

63

13

5

1 12

8

16

4

Edgesx3-5 (1)x3-4 (2)x1-2 (3)x4-6 (4)x5-7 (5)x2-3 (6) 5-6 (8) 2-4 (10) 5-6 (12) 1-5 (13) 6-7 (16)

2

Find the MST of the graph, using Kruskal’s Algorithm

Page 68: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 68

Kruskal’s Algorithm Solution

5

326

74

1

63

5

1

4

2

Find the MST of the graph, using Kruskal’s Algorithm

Page 69: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 69

Disjoint Sets – Problem

Given the following array representation of a disjoint sets data structure:

[2 5 -3 5 3 -3 2 2 3 0]

a) Draw the forest that this array represents.

b) Give a sequence of union and find operations whose execution will convert the array to:

[2 5 -3 2 5 2 2 2 2 0]

Page 70: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 70

Disjoint Sets – Solution

0 1 2 3 4 5 6 7 8 9[2 5 -3 5 3 -3 2 2 3 0]

2 5 / | \ / \ 0 6 7 1 3 / / \ 9 4 8

Page 71: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 71

Disjoint Sets – Solution, cont.

2 5 / | \ / \ 0 6 7 1 3 / / \ 9 4 8

-----2------- / / | \ \ \ 0 6 7 5 3 8 | / \ 9 1 4

[2 5 -3 2 5 2 2 2 2 0] Find( 4 )

Union( 2 , 5 )

Find( 8 )

Page 72: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 72

Sorting

Given the following steps, which sorting algorithms were used in each case?

13 27 89 26 9 37 5 1 381 27 89 26 9 37 5 13 381 5 89 26 9 37 27 13 381 5 9 26 89 37 27 13 381 5 9 13 89 37 27 26 381 5 9 13 26 37 27 89 381 5 9 13 26 27 37 89 381 5 9 13 26 27 37 89 381 5 9 13 26 27 37 38 89

13 27 89 26 9 37 5 1 3813 27 26 9 37 5 1 38 891 13 27 26 9 37 5 38 891 5 13 27 26 9 37 38 891 5 13 27 26 9 37 38 891 5 9 13 27 26 37 38 891 5 9 13 26 27 37 38 89

Page 73: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 73

Sorting

Selection Sort Quick Sort

13 27 89 26 9 37 5 1 381 27 89 26 9 37 5 13 381 5 89 26 9 37 27 13 381 5 9 26 89 37 27 13 381 5 9 13 89 37 27 26 381 5 9 13 26 37 27 89 381 5 9 13 26 27 37 89 381 5 9 13 26 27 37 89 381 5 9 13 26 27 37 38 89

13 27 89 26 9 37 5 1 3813 27 26 9 37 5 1 38 891 13 27 26 9 37 5 38 891 5 13 27 26 9 37 38 891 5 13 27 26 9 37 38 891 5 9 13 27 26 37 38 891 5 9 13 26 27 37 38 89

Page 74: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 74

Sorting

Do a radix sort on the following sequence, showing each step

(1087 643 2532 954 8174 65 340 1752)

Page 75: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 75

Sorting

Step 1: sort by ones place

(1087 643 2532 954 8174 65 340 1752)

(340 2532 1752 643 954 8174 65 1087)

Page 76: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 76

Sorting

Step 2: sort by tens place

(340 2532 1752 643 954 8174 65 1087)

(2532 340 643 1752 954 65 8174 1087)

Page 77: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 77

Sorting

Step 3: sort by hundreds place

(2532 340 643 1752 954 65 8174 1087)

(65 1087 8174 340 2532 643 1752 954)

Page 78: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 78

Sorting

Step 4: sort by thousands place

(65 1087 8174 340 2532 643 1752 954)

(65 340 643 954 1087 1752 2532 8174)

Page 79: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 79

Skip List Problem

Write code for searching a skip list for a key. Assume a skip list node is defined as

and that the skip list pointer references the top left node.

class Node { Comparable key; Node left, right, up, down;}

Page 80: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 80

Skip Lists

2D linked lists Bottom level contains all keys, and each

subsequent level contains probabilistically half the keys of the previous level

Each level starts at -∞ and ends at +∞ The keys in each level are in ascending order

Page 81: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 81

Skip List Example

-∞

-∞

-∞

-∞

∞-10 3 9 18 35 59 84

-10 9 18 59

-10 9

9

Page 82: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 82

Skip List Searching

Start at top left node If the current key is equal to the search key,

return the node If the next key is greater than the search key,

go down and repeat search Otherwise go right and repeat search

Page 83: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 83

Skip List Solution

Write code for searching a skip list for a key

Node search(Node n, Comparable key) { if (n.key.equals(key)) { return n; } else if (n.next.key.compareTo(key) > 0) { return search(n.down, key); } else { return search(n.next, key); }}

Page 84: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 84

Skip List Searching

-∞

-∞

-∞

-∞

∞-10 3 9 18 35 59 84

-10 9 18 59

-10 9

9

Search for 18

9 is not greater than 18, so move right

Page 85: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 85

Skip List Searching

-∞

-∞

-∞

-∞

∞-10 3 9 18 35 59 84

-10 9 18 59

-10 9

9

Search for 18

∞ is greater than 18, so move down

Page 86: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 86

Skip List Searching

-∞

-∞

-∞

-∞

∞-10 3 9 18 35 59 84

-10 9 18 59

-10 9

9

Search for 18

∞ is greater than 18, so move down

Page 87: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 87

Skip List Searching

-∞

-∞

-∞

-∞

∞-10 3 9 18 35 59 84

-10 9 18 59

-10 9

9

Search for 18

18 is not greater than 18, so move right

Page 88: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 88

Skip List Searching

-∞

-∞

-∞

-∞

∞-10 3 9 18 35 59 84

-10 9 18 59

-10 9

9

Search for 18

18 is equal to 18, so return node

Page 89: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 89

Threading Motivations:

Modeling of simultaneous actions Counteract I/O Latency

Mechanism: Multiple threads of control Shared memory space, multiple program counters

Dangers: Shared access to memory can result in conflicts Multiple threads per processor can result in unequal time sharing (see

scheduling) Conflict types:

WAR (write after read) WAW (write after write) RAW (read after write)

How to avoid shared data conflicts? Locking Dangers of locking? Deadlock

Page 90: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 90

Credits

Thanks to CS 61b staff of Fall 2001 Spring 2002 Summer 2002

Thanks to Amir and Jack Thanks to

CMU – MIT Cornell – Johns Hopkins U

for slide and example ideas

Page 91: CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

Steve Sinha and Winston Liaw

Final Review 91

GOOD LUCK!GOOD LUCK!(and may you not need it)(and may you not need it)