COMP 210 Final

¡Supera tus tareas y exámenes ahora con Quizwiz!

Uses for graphs

Course prerequisite structure, maps, modeling digital networks, airline and auto routes, flow of traffic, work flow in an organization, finite state machines, document structure, hypermedia, linguistic representations, all tree uses, etc.

Chicken Hole Principle

For E items being put into B slots, if E > B, then some slot will have 2 or more items - a "collision". # of collisions is affected by the quality of a hash function (how well it distributes keys over the integers) and the table structure (total number of array slots and mathematical properties of the maximum index).

Index of parent node in BHEAP

From left child: index / 2 From right child: (index - 1) / 2

(Weakly) Connected Graph

Has a connection between every vertex.

Single rotation

Left and right rotations symmetric. Root becomes child, child (pivot) becomes the root, and the subtree moves from R to root L or L to R. If the left leaves not under the imbalanced node are < their parent and < the imbalanced node, rotate to the left.

Bogo Sort

Orders a list of values by repetitively shuffling them and checking if they are sorted

Exponential Probing

h(i) = hash(key) + f(i) f(0)=0 , f(i) = 2^i for i>0

Splaying (Node has a parent, but no grandparent)

single rotation

Euler Path

a path that travels along each edge of a graph once and only once

Splay Tree

a self-balancing binary tree that places recently accessed elements near the top of the tree for fast access. No balance property involved. Instead a "splaying" operation.

Full Binary Tree

A full binary tree (sometimes referred to as a proper or plane binary tree) is a tree in which every node in the tree has either 0 or 2 children.

Euler Theorems

1 (EC): If the graph has any odd vertices, there is no Euler Circuit. If it has all even vertices then there is at least one Euler Circuit. 2 (EP): If the graph has more than 2 odd vertices, then there is no Euler Path. If it has exactly 2 odd vertices, then it has at least one Euler Path, starting at one odd vertex and ending at the other. 3: The sum of the degrees of all vertices in G is even. The number of odd vertices is even. Graph has an EC iff #odd vertices = 0 Graph has an EP iff #odd vertices = 2

Prim's Algorithm for MST

Greedy algorithm. 1. Start with empty tree T 2. Pick any node n, add to T 3. Examine edges (n,l) and add the one with the lowest weight 4. Now add min weight edge (u,v) where u is in the tree but v is not (reject cycles) 5. Repeat until all vertices are included

Strongly Connected Graph

Has a path from every vertex to every other vertex

Hashing

Hashing is the basic concept of computing an integer (the "hash" or "hash value" ) from some data value (the "key" ) We intend to use that hash integer as an index into an array or table of associated data. Hash Table is the array where data is stored. HashMap is a MAP ADT implemented via hashing or hash table. Hash Function is the computation that generates a hash value from a key (str).

Big O of recursive functions

2^number of times the recursive function is called

AVL Tree (Adelson-Velskii and Landis)

A BST with a balance condition that maintains low height, bushy structure as we add and delete nodes

Euler Circuit

A Euler path that begins and ends on the same vertex

Hamiltonian Circuit

A Hamiltonian path with endpoints that are adjacent. If the sum of the degrees of any two non-adjacent vertices is greater than or equal to n, a graph has a Hamiltonian circuit. A graph G with N vertices is Hamiltonian if for every pair of non-adjacent vertices the sum of their degrees is >=N.

Complete Binary Tree

A binary tree in which every level, except possibly the leaves, is filled in order from left to right.

Bipartite Graph

A graph consisting of two sets of vertices, X and Y. The edges only join vertices in X to vertices in Y, not vertices within a set. Ex. A tree

Complete Graph

A graph in which each of the n vertices is connected to every other vertex.

Undirected Graph

A graph in which the edges have no direction.

Topological Sort

A linear ordering of its vertices such that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering. Does not apply to DAGs.

Adjacency Matrix

A matrix which records the number of direct links between vertices. Useful when a graph is dense ( |E| approximately = |V|^2 ) Worst case space used: O( |V|^2)

Perfect Binary Tree

A perfect binary tree is a binary tree in which all interior nodes have two children and all leaves have the same depth or same level.

Adjacency List

A representation of a graph in which each node has a list of nodes that are adjacent to it. Useful when a graph is sparse ( |E| << |V|^2 ) Although an undirected edge between nodes A and B is not the same theoretically as two directed edges A --> B and B --> A, we often represent them that way in adjacency lists for an undirected graph.

Map

Abstract type that is a collection of key and value pairs. Like a mathematical function. Each key must be unique and maps to only one value. Can use tree for MAP. Balanced BST has O(log N) worst case times.

Set

Abstract type that stores unique values without any particular order

Spanning trees in a weighted undirected graph

If all edge weights are equal, then every spanning tree is also a minimum spanning tree. If each edge has a different weight, then there is exactly one minimum spanning tree.

Binary Heap

Implements Priority Queues. Structure property - a binary tree, completely filled except for the last row of leaves, which we fill from L to R. Heap-Order property - minimum element in the heap is at the root. Every child is >= its parent. Each path is an ordered list, root to leaf and small to large. Every subtree of a BHEAP is also a BHEAP

Planar Graph

All edges can be drawn on a plane with none crossing.

Dijkstra's algorithm

An algorithm for finding the shortest paths between nodes in a weighted graph. Starts at a given node, accesses adjacent nodes and marks known. Enqueues their adjacent nodes and dequeues if not known. Dequeue until the queue is empty. O(ElogV)

Balance Condition for AVL Tree

At every node, the height of the left and right subtree differ by 1 at most. If there is a difference in height of 2 or more, we re-balance using rotation. We have single and double rotations. Four Cases: LL - solved with R rotation RR - solved with a L rotation LR - solved with a L rotation on left child, then R rotation on the root RL - solved with a R rotation on the right child, then L rotation on the root

Breadth First Traversal

Begins at a root node and inspects all the neighboring nodes. Then for each of those neighbor nodes in turn, it inspects their neighbor nodes which were unvisited, and so on. Implemented with a FIFO queue

DAG

Directed Acyclic Graph A graph with directed edges, but no cycles Ex. Trees

Heap Sort: In place?: Stable?: How does it work?

In place?: not necessarily Stable?: no BuildHeap with array of items, delMin N times back into array.

Merge Sort: In place?: Stable?: How does it work?

In place?: usually not Stable?: can be made stable easily/yes Divides list by two until all elements are by themselves. Then compares neighboring lists and combines until recombined.

In-Place Heap Sort In place?: Stable?: How does it work?

In place?: yes Stable?: no BuildHeap loads all elements into an array. delMin removes an element from heap array, leaving one slot open at the end. Removed element will be placed in the empty slot at the end. Puts the array in reverse order.

Selection sort: In place?: Stable?: How does it work?

In place?: yes Stable?: no Goes through the array N times. Every time find the smallest item in the array and swap it with slot i.

Quick Sort: In place?: Stable?: How does it work?

In place?: yes Stable?: no Makes a binary search tree based on the last element of each list. Sometimes uses insertion sort as the finisher.

Insertion Sort: In place?: Stable?: How does it work?

In place?: yes Stable?: yes Goes through the main array once. Compares current item to its neighbor to the left. If it is out of order, it pulls out the left neighbor and shifts to theft until the correct slot is found.

Bucket Sort In-Place?: Stable?: How does it work?

In-Place?: no Stable?: yes 1) Create n empty buckets (Or lists). 2) For every array element arr[I], insert arr[i] into bucket[n*array[i]] 3) Sort individual buckets using insertion sort. 4) Concatenate all sorted buckets. The tradeoff is that is uses lots of memory (not all array slots are filled) and you have to know in advance the range of inputs.

"Stable" sort

Keeps the same order for items with the same value. Ex. Bubble Sort Ex. Original list: 3, 5, 1, 8 ,2, 7, 8, 2(diff), 4 Stable sort: 1, 2, 2*, 3, 4, 5, 7, 8, 9 Unstable sort: 1, 2*, 2, 3, 4, 5, 7, 8, 9 Could be important if there are secondary keys to sort on as well.

Worst case time complexity for linear graph

O( |V| + |E| )

We can prove that no sort that uses only pairwise comparisons can do better (faster) than ________________________best case.

O(N log N)

Algorithm for Euler Path/Circuit

O(|V|+|E|)

Hamiltonian Path

Path in a graph that contains each vertex only once

Kruskal's Algorithm for MST

Picking the local best at each step gives the best global solution. Greedy algorithm. 1. Start with all nodes, no edges 2. Select edges in order of smallest weight up Stop when all vertices have been included (and the graph is connected) 3. Reject an edge If it creates a cycle

Splaying (Node is a grandchild of another root splay, but left and then right)

RL case = R then L rotation LR case = L then R rotation

If load λ goes over the limit...

Resize: ◦Allocate a new larger array ◦double it is a good plan, to a prime beyond Rehash: ◦For each item in the old table, hash it into the new table ◦O(N) operation for N keys in the table ◦Must rehash since hash function is based on table size ◦Rehashing will shorten the chains, space out keys

Combo Sort

Selection and Insertion efficient for small lists. Merge and Quicksort efficient enlarge sets, but slow on small lists. Common to combine merge/quick until sub lists are smallish, then finish with something like insertion sort.

Minimum Spanning Tree

Spanning tree - a tree formed from the nodes of a graph and a subset of its edges, such that all nodes are connected and the total cost of the edges is minimal. Every minimum spanning tree is a spanning tree and has a sum of edge weights as small as possible. Exists if G is connected. If a graph has a spanning tree, it must also have at least one minimum spanning tree.

Good Hash Function Qualities

Spin through string using prime multi to prevent cycles Load λ = #elts in table / table size. Should be about 1/2 for a well distributed hash function. Should be 1/2 for probing, 1 for chaining. Hash into lists (buckets, chaining). Each array slot would contain not a single element, but rather a list. Linear probing would allow each array slot to contain one element and there would be some sort of "plan" to go on to some other next slot. To avoid clustering, you could probe "more randomly". Make an empty table - each cell contains a key and a data object. If a new key hashes into an empty slot, start a new list with that key data. If a new key hashes to an occupied slot, then add that key data to the list.

Depth First Traversal

Start at some node and keep following children until you hit a node with no adjacencies. When this node is reached, back up to the previous node and pick another adjacency. Implemented with a LIFO stack

Index of child node in BHEAP

To left child: 2 * index To right child: (2 * index) + 1

MAP Implementation

TreeMap ◦getKeys( ) can give back a sort on keys ◦Also can be used to hierarchical structure visualizations HashMap ◦Set has no order... the items in the set are in arbitrary (undefined) order

Double rotation

Two single rotation at different locations, either right-left or left-right. This can occur if the single rotation would break the rules of the BST, like if both of the potential children were less than the parent. If there is a height of 2 due to an inner child, perform one rotation to make the child an outer child, then rotations, then once more to balance the tree.

Splaying (Node is a grandchild of another root splay...two levels down deep in the same direction)

Two single rotations to the left/right. "Zig-Zig". They just flip the opposite way.

Unweighted Shortest Path

Use breadth-first order to visit all of the adjacent nodes. You can use a queue. Enqueue adjacent nodes, dequeue to get the next node.

"In-place" sort

We don't need another array of size N to act as a temporary space for the sorting process. Ex. Bubble Sort

Why does implicit array representation work ok for binary heap, but not for the general case of a binary tree?

When one node is added, double the space is needed in the array immediately

Set ADT

add: elt --> void (or boolean, etc.) remove: elt --> void (or boolean, etc.) contains : elt --> boolean size, empty, etc.

locality of reference

behavior observed in many executing programs in which memory locations recently referenced, and those near them, are likely to be referenced in the near future. A computer program tends to access same set of memory locations for a particular time period.

Linear Probing Function

h(i) = hash(key) + f(i) f(0)=0 , f(i) = i for i>0

Quadratic Probing Funciton

h(i) = hash(key) + f(i) f(0)=0 , f(i) = i^2 for i>0

BHEAP operations

insert ◦put new val in next open slot in array/tree ◦swap/bubble up towards root until heap-order is achieved ◦O(log N) as it follows path to root (height) getMin ◦just read the value at root (array slot 1) ◦O(1) complexity delMin ◦Remove root node val (it is min val) ◦Leaves a "hole" at root node ◦Pull out val in last leaf (eliminates a node) ◦See if it fits in root hole If so, put leaf val into root hole If not, move hole down to smaller child repeat

MAP ADT

put: key x value --> value (or boolean, void, etc) get: key --> value remove: key --> value (or Boolean, void, etc) hasKey : key --> boolean keys: --> SET of key values: --> "blob" of value

Often a sort can be implemented to be stable or unstable, and in many cases making it stable sacrifices ______________.

some efficiency


Conjuntos de estudio relacionados

Chapter 1: What is deep learning

View Set

Real Estate Practice Chapters 1 - 15 (Exam Prep Text)

View Set

chapter 4 entrepreneurshipWhich is a reason why managers develop company objectives?

View Set

Chapter 39: Equal Employment Opportunity Law

View Set

Chapter 9 Lesson 1 Colonists Protest British Rule

View Set

😲😲APWH STUDY GUIDE!!!!! 😲😲

View Set

MasteringA&P CH08 IHW Biol 1020 (2018)

View Set

**(NH1) Unité 5--En arrière-plan et Explore le monde francophone: Visiton la France! / Visitons la Belgique! / Visitons le Maroc!

View Set

The Uniform Securities Act Definitions

View Set

4.1 Vocabulary y gramática en contexto 2

View Set

Ch. 11 - Work-Life Conflicts and Diversity

View Set