Data Structures 4 Flashcards
Algorithm Analysis
how long it takes a computer to do something
Algorithm
has input, produces output, definite, finite, operates on the data it is given
Big-Oh
T(n) = O(f(n)) if there are positive constants c & n° such that T(n) <= c * f(n) for all n >= n°
Big Omega
T(n) = Ω(f(n)) if ∃ positive constants c & n° such that T(n) >= c * f(n) for all n >= n°
Little-Oh
T(n) = 0(f(n)) if T(n) = O(f(n)) and T(n) != Ω(f(n))
Queues
First in, first out. O(1)
L N R
in-order traversal
N L R
Preorder traversal (Polish)
L R N
Postorder traversal (Reverse Polish)
Insertion Sort
Stable, O(n^2), Ω(n) : Swapping elements one at a time starting at the beginning.
Selection Sort
Unstable, O(n^2), Ω(n^2) : Iterates through every elements to ensure the list is sorted.
Bubble Sort
Stable, O(n^2), Ω(n) : Compares neighboring elements to see if sorted. Stops when there’s nothing left to sort.
Heap Sort
Unstable, O(n log n), Ω(n log n): Make a heap, take everything out.
Tree Sort
Stable, O(n log n), Ω(n log n) : Put everything in the tree, traverse in-order.
Merge Sort
Stable, O(n log n), Ω(n log n): Use recursion to split arrays in half repeatedly. An array with size 1 is already sorted.
Quick Sort
Unstable, O(n log n) for a good pivot,O(n^2) for a bad pivot Ω(n log n) : Uses partitioning O(n), Pick a median of 1st, middle, and last element for pivot. Random selection is also good, but expensive. Algorithm can be slow because of many function calls.
Insertion & Quick Sort
Using both algorithms together is more efficient since O(n log n) is only for large arrays.
Indirect Sorting
Involves the use of smart pointers; objects which contains pointers.
Bucket Sort
O(n+m) where m is the # of buckets.
Graphs
”"”Uber”” data structure. Shows connections between objects. Can be displayed as either a matrix or linked list representation.”
DAG
directed acyclic graph
Strongly connected
A directed graph in which there exists a path between every pair of vertices.
Weakly connected
A path between every pair of vertices which are undirected.
Depth First Search
Runs in time equal to the size of the graph, can determine if a graph has a cycle.
Topological Sorting
Receives a DAG as input, outputs the ordering of vertices. Selects a node with no incoming edges, reads it’s outgoing edges.
Single Source Shortest Path
Input: Graph and starting vertex.Output: shortest path to all points.Unweighted: BFSWeighted: Dijkstra’s Method