Review Final - ecs.umass.edupolizzi/Teaching/ECE242/lec-review2.pdf · 1 DepthFirst Search (DFS)...
Transcript of Review Final - ecs.umass.edupolizzi/Teaching/ECE242/lec-review2.pdf · 1 DepthFirst Search (DFS)...
ECE 242Data Structures and Algorithms
http://www.ecs.umass.edu/~polizzi/Teaching/ECE242/
Review Final
Prof. Eric Polizzi
2
Lectures 18192021 Trees BST runs in O(logN) for search/insertion/deletion, and O(N) for traversal
class Node { public int key; // key-id value public Node left; // left child public Node right;// right child
// constructor public Node(int key) {
this.key = key; left = null; right = null;
}}
class Tree { private Node root;// Ref. root
public Tree() { // constructor root = null; }// methods...Here}
Binary Search Tree (BST)
parent
right child
left child
level 0
level 1
level 2
10
5 14
1 7 16
root
public Node find(int i) { Node current = root; // start at root while (current!=null && current.key!=i) { if (i<current.key) { // go left
current = current.left;}
else { // go rightcurrent = current.right;}
} return current; // return Node or null}
3
Lectures 18192021 Trees
Transform a list into a binary search tree:
10, 4, 7, 15, 3, 18, 1610
(1)
10
4
(2)
10
4
7
(3)
10
4 15
7
(4)
(5)
10
4 15
73
(6)
10
4 15
7 183
10
4 15
7 18
16
3
(7)
4
Lectures 18192021 Trees Main methods (recursive or iterative) : find, insert, delete, maximum/minimum,
traversal (inorder,postorder,preorder)
public Node recfind(Node current,int i) { if (current==null) return null; elseif (i<current.key) // search left
recfind(current.left,i); elseif (i>current.key)// search right
recfind(current.right,i); else // find the target return current;}
public void recinsert(Node current,int i) { if (i<current.key) { // search left if (current.left==null)// insert node?
current.left= new Node(i); else
recinsert(current.left,i); // keep searching } else if (i>=current.key) {// search right if (current.right==null)// insert node?
current.right= new Node(i); else
recinsert(current.right,i);// keep searching }}public Node minimum()
{ Node current,last; current=root; while(current!=null){ //until the bottom last=current; //remember node current=current.left; } return last;}
Lecture 20: Deleting a node
5
Lectures 18192021 Trees
1 Visit the left subtree 2 Visit the right subtree3 Visit the node (ex: display it)
10
5 14
1 7 16
Result is (InOrder) : 1 5 7 10 14 16Result is (PreOrder) : 10 5 1 7 14 16Result is (PostOrder): 1 7 5 16 14 10
1 Visit the left subtree2 Visit the node (ex: display it)3 Visit the right subtree
1 Visit the node (ex: display it)3 Visit the left subtree 3 Visit the right subtree
inorder
preorder
postorder
public void inOrder(Node current) { if (current!=null){
inOrder(current.left); //1 System.out.print(current.key+” ”);//2 inOrder(current.right); //3 }}
6
Lectures 18192021 Trees
RedBlack Tree rules (to ensure that the tree is balanced)1 Every nodes is either red or black
2 The root is always black3 If a node is red, its children must be black4 Every path from the root to a leaf, or to a null child, must contain the same number of black nodes (black height is the same)
Insertion Color of inserted node is always red by default The insertion is first similar to the one for BST, find the
position where the node should be inserted Three main stages to fix violations of the rules
a Color flips on the way downEvery time the insertion routine encounters a black node that has two red children, it must change the children to black and the parent to red (unless the later is the root)
b Rotations after the node is inserted c Rotations on the way down
2
3
1 4
5
List: 1, 2, 3, 4, 5
7
Lectures 2223: Heaps
Heap offers a fast implementation for priorityQ → O(logN) for enqueue/dequeue Priority item can be the largest, smallest, etc. Priority queue ← Heap == (CBT+ heap cond.) ← array implementation CBT: Complete Binary Tree heap condition: every node's key is larger than (or equal to) the keys of its
children. A heap is then weakly ordered compared to a BST Difference with BST: the priority item always at the root level (no need to
search/traverse to locate it) Example of HeapArray and its CBT representation
8 20 33 10 7 2 6 15 19 27
271915
7
8
3320
10 2 6
8
Lectures 2223: Heaps
Insertion (uses trickleup)
23
27
17
37
1014 619
15 35
27 17
37
1014 6
1915
35
23
Removal (uses trickledown)
27 17
37
1014 6
1915
35
23
27 17
37
1014 6
15
19
23 19
17
37
1014 6
15
27
23
O(logN) Algorithms
9
Lectures 2223: Heaps
Heapify an array (using CBT representation): Example:
1st approach: using successive insertion
2nd approach: using trickledown in place
8 20 33 10 7 2 6 15 19 27
7108
19
33
2027
15 2 6
33 27 20 15 19 2 6 8 10 7
O(NlogN) Algorithms Work 'inplace'
271915
7
8
3320
10 2 6
71915
27
8
3320
10 2 6
71015
20
8
3327
19 2 671015
20
33
827
19 2 6
33 27 8 19 20 2 6 15 10 7
10
Lectures 2223: Heaps
Heapsort Motivations It is not difficult to Heapify a random array cost is → O(NlogN) Removing one priority item is O(logN)
Remark: once root item is replaced by last node, only one trickledown is enough to heapify the new data
Removing N times (successive removals) → O(NlogN) sorting algorithm (in practice a bit slower than quicksort, but no worst case and it easy enough to implement)
Work in place (no additional memory) Heapify + Heapsort:
for (int j=N-1;j>=0;j--) { Node priorityNode=remove(); heapArray[j] = priorityNode; }
for (int i=N/2-1;i>=0;i--) trickleDown(i);
11
Lectures 2223: Heaps
71015
20
33
827
19 2 6
1015
7
27
820
19 2 6
10
7
20
819
15 2 6
7
19
815
10 2 6 7
15
810
6 2 2
10
87
6
8
27
6
7
26
6
2
2
20 19 8 15 7 2 6 10 27 33
19 15 8 10 7 2 6 20 27 33
15 10 8 6 7 2 19 20 27 33
10 7 8 6 2 15 19 20 27 33
8 7 2 6 10 15 19 20 27 33
7 6 2 8 10 15 19 20 27 33
6 2 7 8 10 15 19 20 27 33
2 6 7 8 10 15 19 20 27 33
33 27 8 19 20 2 6 15 10 7
27 20 8 19 7 2 6 15 10 33
12
Lectures 2425 Hash Tables Motivation: optimal insertion and search O(1) (cannot traverse) Arraybased implementation Hashing: It is a mapping that converts a 'number key' (integer, String,
etc.) that belongs to a large range into a much smaller index array number.
Hash function: int hash(key) Simple approach: smallNumber=largeNumber%smallRange It is not possible to avoid collisions
Two main approaches to deal with collisions Open addressing
Use x2 size array If a cell is already occupied, one must find another location 3 options: Linear probing, Quadratic Probing, Double hashing
Separate Chaining (each index of the hash table associated with linked list)
Application: Hashing Strings (Horner method and modulo operator)
13
Lecture 2629: Graphs
Graphs (nondirected/directed, weighted/unweighted) and adjacency matrix
Graph operations: 1 DepthFirst Search (DFS) using a stack 2 BreadthFirst Search (BFS) using a queue 3Minimum spanning Tree (MST) (weighted) 4 Shortest Path (Dijkstra) (weighted)
0
1
3
2
0 1 1 0
1 0 1 1
1 1 0 1
0 1 1 0
0
1
3
2
0 1 1 1
0 0 0 1
0 0 0 1
0 0 0 0
0
2
3
1
20 10
1
54
0 20 10 1
20 0 0 5
10 0 0 4
1 5 4 0
14
Lecture 2629: Graphs DFS
A
B C E
F D
A
A
B C E
F D
A
B C E
F D
A
B C E
F D
A
B C E
F D
A
B C E
F D
A
B C E
F D
A
B C E
F D
BA
CBA
DCBA
DCB
E
A
DCBA
DCB
F
A
DCBA
CBA
BA A
Visit: ABCDEF
15
Lecture 2629: Graphs BFS
A
B C E
F D
B
A
B C E
F D
A
B C E
F D
A
B C E
F D
A
B C E
F D
A
B C E
F D
A
B C E
F D
A
B C E
F D
BC
BCE
CE
CEF
EF
EFD
FD D
Visit: ABCEFD
A
B C E
F D
16
Lecture 2629: Graphs MST Prim
A
B
D E
C
8
6 7
12
10
5 F
4
6
77 A
B
D E
C
8
6 7
12
10
5 F
4
6
77
A
B
D E
C
8
6 7
12
10
5 F
4
6
77 A
B
D E
C
8
6 7
12
10
5 F
4
6
77
A
B
D E
C
8
6 7
12
10
5 F
4
6
77 A
B
D E
C
8
6 7
12
10
5 F
4
6
77
A in TreeAB6,AD4Dequeue AD4→D in TreeDE12,DC8,DB7,AB6Dequeue AB6→B in TreeDE12,BC10,DC8,DB7,BE7Dequeue BE7→E in TreeDE12,BC10,DC8,DB7,EF7, EC5Dequeue EC5→C in TreeDE12,BC10,DC8,DB7,EF7, CF6Dequeue CF6→F in TreeDone Tree{A,D,B,E,C,F}
17
Lecture 2629: Graphs Dijkstra
From A to B C D E A 50A oo 80A oo B 50A 110B 80A oo D 50A 100D 80A 150D C 50A 100D 80A 140C E Done
A
B
D E
C
$20
$50 $50
$70
$60
$40
$80
$90
Remark: distance to D through B is 140. However, 80<140, 80A is then retained
PATHS: B (AB), D (AD), C (ADC), E (ADCE)
18
DATA STRUCTURES
Lecture 30: Epilogue
Linked List
(simple,doubly)
Array(1d,2d,etc.)
Unsorted list
Ordered ArraySorted list
Trees(BST,CBT,RBT)
HashTables
Stacks
Queues
Heap(Priority Q)
Graphs
Generalpurpose
Specialized (ADT)
ALGORITHMS
Insert Remove Search TraverseSortPop, Push, dequeue,
enqueue, etc.
19
Lecture 30: Epilogue
ShellSort
QuickSort
Insertion Sort O(N2)O(N2) comp.+ O(N2) copies
● half #comp. than Bubble● O(N) total if data almost sorted
Enhanced Insertion Sort
O(N2)O(NlogN) comp.+ O(N2) copies
● Use binary search rather than linear search
List Insertion Sort
O(N2)O(N2) comp.+ O(N) copies
● Only 2N copies● Does not work 'inplace'
MergeSort O(NlogN)O(N) copies by O(logN) levels
● Divide & Conquer + Recursive● Does not work 'in place'
Bubble SortO(N2)
O(N2) comp.+ O(N2) swaps● Slow, slow, slow
Selection Sort O(N2)O(N2) comp.+ O(N) swaps
● Intuitive but still slow
O(N(logN)2)In average
●'Insertion sort' using increment●Worst case not far from average
O(NlogN)Comp.>swaps
● Divide and Conquer● Uses partitioning recursively
O(NlogN)● Require a heap datastructure● No worst caseHeapSort
20
Final Words
Exam: Tuesday December 19 [7:50am10am] Marcus 131 auditorium
Practice all algorithms, come wellprepared, do your best, no pressure Data Structure and Algorithms – Complement
Hope you enjoyed the class and the projects This was the last ECE242 class (ever)…...curriculum change C'est la fin, merci.