+ All Categories
Home > Documents > CS 261 – Recitation 9 & 10 Graphs & Final review

CS 261 – Recitation 9 & 10 Graphs & Final review

Date post: 22-Feb-2016
Category:
Upload: shanta
View: 140 times
Download: 0 times
Share this document with a friend
Description:
Oregon State University School of Electrical Engineering and Computer Science. CS 261 – Recitation 9 & 10 Graphs & Final review. Fall 2013. Graph: Reachability Problem. Given a single starting vertex, produce the set of vertices that can be reached starting from the initial location. - PowerPoint PPT Presentation
17
CS 261 – Recitation 9 & 10 Graphs & Final review Fall 2013 Oregon State University School of Electrical Engineering and Computer Science
Transcript
Page 1: CS 261 –  Recitation  9 & 10 Graphs & Final review

CS 261 – Recitation 9 & 10Graphs & Final reviewFall 2013

Oregon State UniversitySchool of Electrical Engineering and Computer Science

Page 2: CS 261 –  Recitation  9 & 10 Graphs & Final review

Graph: Reachability Problem• Given a single starting vertex, produce the set of

vertices that can be reached starting from the initial location.

• A depth-first search follows each path as far (deep) as possible before backtracking.

• A breadth-first search looks at all possible paths at the same time.

Order in which nodes are reached: (left) DFS; and (right) BFS. Source: Wikipedia

Page 3: CS 261 –  Recitation  9 & 10 Graphs & Final review

Graph: Reachability ProblemfindReachable (graph g, vertex start) {

create a set of reachable vertices, initially empty. call this r. create a container for vertices known to be reachable. call this c

add start vertex to container cwhile the container c is not empty {

remove first entry from the container c, assign to vif v is not already in the set of reachable vertices r {

add v to the reachable set radd the neighbors of v to the container c

}}return r

}

DFS: Container is a StackBFS: Container is a Queue

Page 4: CS 261 –  Recitation  9 & 10 Graphs & Final review

ExerciseSimulate the DFS and BFS on the following graph starting at node A. Notes: (1) Nodes must be added to the container (Stack or Queue) in COUNTER-CLOCKWISE order; (2) We do not add neighbors to the container if they have already been visited.

Page 5: CS 261 –  Recitation  9 & 10 Graphs & Final review

CS 261 – Data Structures

Outline – Final exam review• BST/AVL/Tree sort/Tree traversal/Tree iterator• Heaps/heap sort• Hash tables

Materials in these slides were collected from different Internet sources.

5

Page 6: CS 261 –  Recitation  9 & 10 Graphs & Final review

CS 261 – Data Structures

Question

6

H

JD

C G

B KF

A

I

E

Page 7: CS 261 –  Recitation  9 & 10 Graphs & Final review

Pre, In, Post-order traversalPre-order: 10 – 5 – 1 – 8 – 7 – 6 – 34 –

56 – 40 - 60

In-order: 1 – 5 – 6 – 7 – 8 – 10 – 34 –

40 – 56 - 60

Post–order: 1 – 6 – 7 – 8 – 5 – 40 – 60 –

56 – 34 – 10

CS 261 – Data Structures 7

Page 8: CS 261 –  Recitation  9 & 10 Graphs & Final review

Adding 13???

CS 261 – Data Structures 8

13

Page 9: CS 261 –  Recitation  9 & 10 Graphs & Final review

Removing 10???

CS 261 – Data Structures 9

Page 10: CS 261 –  Recitation  9 & 10 Graphs & Final review

TreeSort

struct AVLTree* newAVLTree();void addAVLTree(struct AVLTree *tree, TYPE val);

void treeSort (TYPE data[], int n) {…}void _treeSortHelper(AVLNode *cur, TYPE *data, int

*count){…}

CS 261 – Data Structures 10

Page 11: CS 261 –  Recitation  9 & 10 Graphs & Final review

treeSortvoid treeSort(TYPE data[], int n){ int i; int count = 0;

/* declare an AVL tree */ struct AVLTree *tree = newAVLtree(); assert(data != NULL && n > 0);

/* add elements to the tree */ for (i = 0; i < n; i++) addAVLTree(tree, data[i]);

/* call the helper function */ _treeSortHelper(tree->root, data, &count);}

CS 261 – Data Structures 11

Page 12: CS 261 –  Recitation  9 & 10 Graphs & Final review

_treeSortHelper/* *count goes from 0 to n-1 */

void _treeSortHelper(AVLNode *cur, TYPE *data, int *count){

if (cur != NULL) { _treeSortHelper(cur->left, data, count); data[*count] = cur->val; (*count)++; _treeSortHelper(cur->right, data, count); }}

CS 261 – Data Structures 12

Page 13: CS 261 –  Recitation  9 & 10 Graphs & Final review

True or False

CS 261 – Data Structures 13

Page 14: CS 261 –  Recitation  9 & 10 Graphs & Final review

Question• Add 12, remove 3, remove 5

CS 261 – Data Structures 14

Page 15: CS 261 –  Recitation  9 & 10 Graphs & Final review

When to do a double rotation?Balance Factor = height(right subtree) - height(left

subtree)

• At an unbalanced node N, a double rotation is needed when:– N’s BF is positive and N’s right subtree’s BF is

negative– N’s BF is negative and N’s left subtree’s BF is

positive.

CS 261 – Data Structures 15

Page 16: CS 261 –  Recitation  9 & 10 Graphs & Final review

Heaps and priority queues• How to represent a binary heap?

– Using an array (dyArray)• Suppose the root has index 0, what are the indices of the 2

children of a node at index i? • What is the index of the parent of a node at index i?

CS 261 – Data Structures 17

2

5

8

3

79 10

14 12 11 1602

13

25

39

410

57

68

714

812

911

1016

2 * i + 1, 2 * i + 2 (i-1)/2

Page 17: CS 261 –  Recitation  9 & 10 Graphs & Final review

Simulate heap sort for the following heap

CS 261 – Data Structures 18

3

9 10

14 12 11 16


Recommended