Download presentation

Presentation is loading. Please wait.

1
**CS 261 – Recitation 9 & 10 Graphs & Final review**

Oregon State University School of Electrical Engineering and Computer Science CS 261 – Recitation 9 & 10 Graphs & Final review Fall 2013

2
**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. “DFS could be simulated as an ant walking along the edges of the graph, and returning to a previous node when it discovers it has reached a vertex it has already seen.” “A mental image for BFS would be to pour ink at the starting vertex, and see where it travels along all possible paths” Order in which nodes are reached: (left) DFS; and (right) BFS. Source: Wikipedia 2

3
**Graph: Reachability Problem**

findReachable (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 c while the container c is not empty { remove first entry from the container c, assign to v if v is not already in the set of reachable vertices r { add v to the reachable set r add the neighbors of v to the container c } return r DFS: Container is a Stack BFS: Container is a Queue 3

4
Exercise Simulate 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. DFS: r = {}; c = {A}; r={A}; c = {B}; r={A, B}; c={C, E, D}; // Note: D is the top of the stack r={A, B, D}; c={C, E, F}; r={A, B, D, F}; c={C, E}; r={A, B, D, F, E}; c={C}; r={A, B, D, F, E, C}; c={}; BFS r={A, B}; c={C, E, D}; // Note: C is the front of the queue r={A, B, C}; c={E, D}; r={A, B, C, E}; c={D, D}; r={A, B, C, E, D}; c={D, F}; r={A, B, C, E, D}; c={F}; r={A, B, C, E, D, F}; c={}; 4

5
**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. CS 261 – Data Structures

6
**Question H D J B F I K A C E G CS 261 – Data Structures**

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible A C E G CS 261 – Data Structures

7
**Pre, In, Post-order traversal**

Pre-order: 10 – 5 – 1 – 8 – 7 – 6 – 34 – 56 – In-order: 1 – 5 – 6 – 7 – 8 – 10 – 34 – 40 – Post–order: 1 – 6 – 7 – 8 – 5 – 40 – 60 – 56 – 34 – 10 We can do other things with traversal other than just printing out values. CS 261 – Data Structures

8
Adding 13??? 13 CS 261 – Data Structures

9
Removing 10??? CS 261 – Data Structures

10
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

11
treeSort void 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

12
_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

13
True or False CS 261 – Data Structures

14
Question Add 12, remove 3, remove 5 CS 261 – Data Structures

15
**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 15

16
**Draw the stack of in-order traversal using iterator**

if stack is empty perform slide left on root otherwise let n be top of stack pop n slide left on right child of n void _slideLeft(struct Stack *stk, struct Node *n) while (n != 0) { pushStack(stk, n); n = n->left; } CS 261 – Data Structures

17
**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? 2 * i + 1, 2 * i + 2 (i-1)/2 2 3 5 9 10 7 8 2 1 3 2 5 3 9 4 10 5 7 6 8 7 14 8 12 9 11 10 16 14 12 11 16 CS 261 – Data Structures

18
**Simulate heap sort for the following heap**

3 10 9 16 14 12 11 CS 261 – Data Structures

Similar presentations

OK

Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.

Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on data transmission by laser Ppt on marketing download Ppt on barack obama leadership failure Ppt on traumatic brain injury Ppt on animals our friends essay Ppt on the environmental pollution its causes effects and solutions Ppt on four stroke diesel engine Ppt on cash flow statement Download ppt on respiration in animals Ppt on standing order template