data structures and search algorithms
TRANSCRIPT
![Page 2: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/2.jpg)
Data structures
Storage Insertion, deletion Searching Sorting Big O
![Page 3: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/3.jpg)
Stacks
LIFO Push, pop O(1) operations
![Page 4: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/4.jpg)
Linked lists v. Arrays
Linked lists:– Resizable– Insertion/deletion
Arrays:– Faster index– O(1) lookup– Preset size
![Page 5: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/5.jpg)
Hash tables
Keys and values O(1) lookup Hash function
– Good v fast Clustering Databases
![Page 6: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/6.jpg)
Selection sort :-(
O(n2) Algorithm:
– Find the minimum value– Swap with 1st position value– Repeat with 2nd position down
![Page 7: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/7.jpg)
Insertion sort :-)
O(n2) O(1) space Great with small number of elements
(becomes relevant later) Algorithm:
– Move element from unsorted to sorted list
![Page 8: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/8.jpg)
Bubble sort :-(
O(n2) Algorithm:
– Iterate through each n, and sort with n+1 element
Maybe go n-1 steps every iteration? Great for big numbers, bad for small Totally useless?
![Page 9: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/9.jpg)
Merge sort :-)
O(nlogn) Requires O(n) extra space Parallelizable Algorithm:
– Break list into 2 sublists– Sort sublist– Merge
![Page 10: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/10.jpg)
Quick sort :-)
Average O(nlogn), worst O(n2) O(n) extra space (can optimized for O(logn)) Algorithm:
– pick a pivot– put all x < pivot in less, all x > pivot in more– Concat and recurse through less, pivot, and more
Advantages also based on caching, registry (single pivot comparison)
Variations: use fat pivot
![Page 11: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/11.jpg)
Linear search :-(
O(n) Examines every item
![Page 12: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/12.jpg)
Binary search :-)
Requires a sorted list O(log n) Divide and conquer
![Page 13: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/13.jpg)
Trees
Almost like linked lists! Traverse: Pre-order v. Post-order v. In-
order Node, edge, sibling/parent/child, leaf
![Page 14: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/14.jpg)
Binary trees
0, 1, or 2 children per node Binary Search Tree: a binary tree where
node.left_child < node.value and node.right_child >= node.value
![Page 15: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/15.jpg)
Balanced binary trees
Minimizes the level of nodes Compared with “bad” binary tree? Advantages:
– Lookup, insertion, removal: O(log n) Disadvantages:
– Overhead to maintain balance
![Page 16: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/16.jpg)
Heaps (binary)
Complete: all leafs are at n or n-1, toward the left
Node.value >= child.value In binary min/max heap
– Insert = O(logn) .. add to bottom, bubble-up– deleteMax = O(logn) .. Move last to root
and bubble-down
![Page 17: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/17.jpg)
Heapsort
O(nlogn) Algorithm:
– Build a heap– deleteMax (or Min) repeatedly
O(1) overhead
![Page 18: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/18.jpg)
Why bother?
Tries (say trees)– Position determines the key– Great for lots of short words– Prefix matching
But..– Long strings..– Complex algorithms
![Page 19: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/19.jpg)
Chess!
Minimax:
Alpha-beta pruning - pick a bag!– ordering
B: B1 B: B2 B: B3
A: A1 +3 -2 +2
A: A2 -1 0 +4
A: A3 -4 -3 +1
![Page 20: Data Structures and Search Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022030319/585c3c5e1a28abed218c008e/html5/thumbnails/20.jpg)
Useful
http://www.cs.pitt.edu/~kirk/cs1501/animations/Sort3.html