CS 3345 Final Review

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

2

How many stacks are needed to implement a queue? Consider the situation where no other data structure like arrays, linked list is available to you.

Dense, sparse

•Graph is _______ if |E| is Q(|V|2) (means "lots of edges") •If |E| is O(|V|) we say the graph is _______ (means "most possible edges missing")

Direct applications: -Page-visited history in a Web browser -Undo sequence in a text editor -Chain of method calls in the Java Virtual Machine Indirect applications: -Auxiliary data structure for algorithms -Component of other data structures

Applications of stacks

1. Pick 1 node 2. Choose the path with the least weight 3. With your old node(s) and newly available node, repeat step 2 4. Repeat steps 2 and 3 all nodes have been reached, without making any cycles in the graph

Prim's algorithm

less than ex. f(n) is o(g(n)) if f(n) is less than g(n) (STRICTLY)

o (Little o)

equal to ex. f(n) is (g(n)) if f(n) is asymptotically equal to g(n)

Θ (Big Theta)

greater than ex. f(n) is ω(g(n)) if f(n) is greater than g(n) (STRICTLY)

ω (Little omega)

biconnected

•A connected undirected graph is _____________ if there are no vertices whose removal disconnects the rest of the graph. •If a graph is not _____________, the vertices whose removal would disconnect the graph are known as articulation points •Depth-first search provides a linear - time algorithm to find all articulation points in a connected graph.

True

2^n = O(n!)

connected

A graph is ___________ if there is a path from every vertex to every other vertex.

False

Best hash function will never create any collision among the hash table values.

Prints all nodes of linked list in reverse order

What does the following function do for a given Linked List with first node as head? void fun1 (Node head) { if(head == NULL) return; fun1(head.next); print(head.data); }

N2 = O(N2) 2N = O(N2) N = O(N2) N2 = O(N) ***

Which of the following is false.

True

True or False: You can implement both stacks and queues using singly linked lists

Deletion

________ is difficult for hashing; standard ________ cannot be performed in a probing hash table, because cells might have caused collision to go past it.. Probing hash tables require lazy ________.

Definition: A forest is a graph that has no simple circuit, but is not connected. Each of the connected components in a forest is a tree.

what is a forest?

less than or equal to ex. f(n) is O(g(n)) if f(n) is asymptotically less than or equal to g(n)

O (Big O)

Depth

The _______ of a node is the length of the path from the root to the node.

printList and find(X) are O(N) findKth(i) takes O(i)

Time complexity for linked list operations

True

True or False: You can implement 2 or even 3 queues in one array

Dijkstra's

Use ___________ algorithm to find the shortest weighted path from a vertex, s, to every other vertex in the graph.

O(1) and O(n)

What are the time complexities of finding 8th element from beginning and 8th element from end in a singly linked list? Let n be the number of nodes in linked list, you may assume that n > 8.

Load factor

_______ measures how full the hash table is.

greater than or equal to ex. f(n) is Ω(g(n)) if f(n) is asymptotically greater than or equal to g(n)

Ω (Big Omega)

O(logN)

AVL Trees have depth O(____)

Path compression for union-find heuristic 2

After performing a find, compress all the pointers on the path just traversed so that they all point to the root

•A relation R is defined on a set S if for every pair of element (a,b) a, b ϵ S, a R b is either true or false. •An equivalence relation is a relation R that satisfies three properties: 1.Reflexive: a R a for all a ϵ S 2.Symmetric: a R b if and only if b R a 3.Transitive a R b and b R c implies a R c

An equivalence relation satisfies these 3 properties

•Web pages with links •Social Media Network •Facebook •Road maps •Network Connectivity •Airline routes •Blue print of building •Electricity circuits •Database

Applications of graphs

Rehashing

Building a second table ( twice as big) and computing the new hash value for each element and inserting into the new table is called _________. This is very expensive [O(N)] but it happens very infrequently.

Depth first - follow down 1 path from root to leaf and repeat for all paths Breadth first - level 0, level 1, level 2, ... , level d; follows from root down to all nodes of next level, and so on

Depth first search vs breadth first search

O(|V| + |E| * log|V|)

Dijkstra's algorithm run time complexity is ___________

Union-find data structure, merge-find set

Disjoint sets are also called:

Algorithm insertAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p) {link v to its predecessor} v.setNext(p.getNext()) {link v to its successor} (p.getNext()).setPrev(v) {link p's old successor to v} p.setNext(v) {link p to its new successor, v} return v {the position for the element e} Algorithm remove(p): t = p.element {a temporary variable to hold the return value} (p.getPrev()).setNext(p.getNext()) {linking out p} (p.getNext()).setPrev(p.getPrev()) p.setPrev(null) {invalidating the position p} p.setNext(null) return t

Doubly linked list insertion and deletion algorithms

In the same set

For disjoint sets, find(a) == find(b) is true if and only if a and b are:

Heap sort

For large input sizes, which of the following sorting algorithm will be fastest?

Heap sort

For large input sizes, which of the following sorting algorithm will be fastest? Insertion Sort Heap Sort Selection Sort Bubble Sort

•Assuming add and remove are O(1), entire traversal is O(|E|)

Graph time complexity

•Hash tables are often used where quick access is needed. •Compliers use hash table to keep track of declared variables in source code. •It is used in gaming programs. •Online spelling checker. •They are used to implement caches, both in software and in hardware. •Implementation of routers.

Hashing applications

14

How many distinct binary search trees can be created out of 4 distinct keys?

True

If f(n) = 3n^3 + 5n^2 + 6n + 10 and g(n) = n^3 / 3 then f(n) = Θ(g(n))

f(n) = Θ(g(n))

If f(n) = O(g(n)) and f(n) = Ω(g(n)), then which of the following statements is always true

False

If f(n) = n and g(n) = log n then f(n) is always slower than g(n)

1. Choose the edge(s) with least weight 2. Repeat step 1 until all nodes have been reached, without making any cycles in the graph

Kruskal's algorithm

Insert/delete an element at the middle of the list. Insert/delete an element at the beginning of the list. *** Insert/delete an element at the end of the list. Retrieve an element given the index.

LinkedList is more efficient than Array for the following operations:

FIRST in, FIRST out (FIFO)

Queues are a ____ in, ____ out operation

O(|V|2)

Run time complexity of unweighted shortest path is ________. •This can be improved by using a queue for the next list of vertices to check. The running time is O(|E| + |V|) . (Similar to topological sort)

Splay

Searching and inserting in a _____ tree, a type of BST, moves the searched or inserted node to the root node

•splaying costs O(h), where h is height of the tree - which is still O(n) worst-case [O(h) rotations, each of which is O(1)] •The analysis of splay tree is difficult because it must take into account the ever changing structure of the tree. •Amortized cost of any splay operation is O(log n) •In fact, the analysis goes through for any reasonable definition of rank(x) •Splay trees can actually adapt to perform searches on frequently-requested items much faster than O(log n) in some cases

Splaying and splay tree time complexity

•Stack is initially empty •When an operand is read it is immediately placed onto the output. •If we see a right parenthesis, then we pop stack writing symbols until we see left parenthesis which is popped but not output. •if we see any other symbol (, +, *, then we pop the entire stack until we see lower priority one exception is we never remove ( unless we encounter )

Stack infix to postfix conversion

LAST in, FIRST out (LIFO)

Stacks are a ____ in, ____ out operation

Inorder - left child, parent, right child Preorder - parent, left child, right child Postorder - left child, right child, parent

Traversal methods

Stack

Suppose the rule of the party is that the participants who arrive later will leave earlier. Which data structure is appropriate to store the participants?

Length, cost

The path ______ is the number of edges in a path. The path _______ is the sum of weights of edges in a path

•Path : from node n1 to nk is defined as sequence of nodes n1, n2,... nk, such that ni, is the parent of ni+1, for 1 <= i <= k. The length of the path is k-1 •Length of path: number of edges on the path •Depth of Node ni: is the length of path from root to ni. •Tree depth: length from root to deepest leaf (= tree height) •Height of Node ni: is the length of longest path from ni to a leaf •Depth of root is 0 and height of all leaves are 0 •Grandchild and Grandparent

Tree terminology

AVL tree

The worst case time complexity for insertion, deletion, and search is O(logn) for a ___________.

binary tree binary search tree AVL tree *** binary heap

The worst case time complexity for insertion, deletion, and search is O(logn) for a ___________.

True

There exists a data structure to maintain a dynamic set with operations Insert(x), Delete(X) and Contains(X) that has an expected running time of O(1) per operation.

•A find(x) is performed by returning the root of the tree containing x. •The time to perform this operation is proportional to depth of the node representing x. •So the worst case running time of find is θ(N)

Time complexity of find(x) method for disjoint sets

•Figuring out how to graduate •Computing an order in which to recompute cells in a spreadsheet •Determining an order to compile files using a Makefile •In general, taking a dependency graph and finding an order of execution

Topological sort applications

step by step instructions to solve a problem

What is an algorithm?

300n + 400n*n 23logn + 50 45n + 45nlogn + 503 *** n*n*n + nlogn

Which of the following function's complexity is best represented by O(nlogn)

Insert at head Remove at head Insert at tail Remove at tail ***

Which of the following operations is inefficient for linked lists?

Time complexity is equal to number of operations performed by the algorithm on a given input size

Which of the following sentences define how time-complexity is calculated

B logA

Which one of the following statements is equivalent to log(AB) ?

Equivalence class

•The __________ _____ of an element a ϵ S is the subset that contains all the elements that are related to a. Notice that __________ ______ form a partition of S. •The __________ _____ forms a partition of S, every element is in exactly one __________ _____. •We use ~ to mean an equivalence relation. •To decide a ~ b we need only to check if a and b are in the same subset.

Separate chaining

........................is a technique where linked lists are used to take care of the collision from hash table.

Merge sort or Quick sort

.............Sorting technique uses Divide and Conquer Strategy.

In the implementation of the List ADT by means of a doubly linked list: -The space used by a list with n elements is O(n) -The space used by each position of the list is O(1) -All the operations of the List ADT run in O(1) time -Operation element() of the Position ADT runs in O(1) time

Doubly linked list performance analysis

Path compression

During _____ _____________, every node after find(x) on the path from x to the root has its parent changed to the root. When union is done arbitrarily, this is a good idea, because there is an abundance of deep nodes and these are brought near to the root.

False; only AVL trees do

Every binary search tree of n nodes has height O(log n).

Linked List

Suppose you want to store students and perform the operations to insert and delete students. Which data structure is best for this application?

Union by size (union-find heuristic 1)

Term for: When performing a union, make the root of smaller tree point to the root of the larger (implies O(n * log n) time for performing n union-find operations)

Degree

The ______ of a vertex is the number of edges containing that vertex (the number of adjacent vertices)

The height of a rooted tree is the maximum of the levels of the vertices.

What is the height of a tree?

The level of a vertex v in a rooted tree is the length of the unique path from the root to this vertex.

What is the level of a tree node?

logp(n)

What is time complexity of following code? for (int i =0; i < n; i++) i = i * p;

16

What will be output of following program int fun(int n) { if (n == 4) return n; else return 2*fun(n+1); } int main() { printf("%d ", fun(2)); return 0; }

largest from left subtree, smallest from right subtree

When removing the root node in an AVL tree, the _______ node of the left subtree replaces it, or else the ________ node of the right subtree replaces it

Splay tree

Which of the following tree gives best performance while searching same element again and again? Binary search tree AVL tree Splay tree Expression tree

When a resource is shared among multiple consumers. When data is transferred asynchronously (data not necessarily received at same rate as sent) between two processes. Load Balancing. All of the above. ***

Which one of the following is an application of Queue Data Structure?

Separate chaining, Linear probing, Double hashing

_______ _______ lets each cell in a hash table point to a linked list of entries that map there. _______ probing handles collisions by placing the colliding item in the next (circularly) available table cell _______ probing handles collisions using a function hi = (hash(x) + i^2) mod TableSize, with f(0) = 0, x is the key. If _______ probing is used and the table size is prime, then a new element can always be inserted if the table is at least half empty (λ < ½ ). Unfortunately, there is no guarantee of finding an empty cell once the table gets half full. _______ _______ uses a secondary hash function d(k) and handles collisions by placing an item in the first available cell of the series. The secondary hash function d(k) cannot have zero values AND the table size N must be a prime to allow probing of all the cells.

Selection, O(n^2) Insertion, O(n^2) Bubble, O(n^2) Merge, O(n * log2(n)) Quick, O(n * log2(n)), O(n^2)

_________ sort selects the minimum element in an array and moves it to its correct position in the front (sorted) portion of the array, making n-1 passes and keeping sorted and unsorted portions within the array (sorted in front, unsorted in back). It runs in O(__) time. _________ sort also uses a sorted and unsorted portions within one array. In each pass of _________ sort, the first element of the unsorted part is placed into its correct position in the sorted portion. It also runs in O(__) time. _________ sort involves bubbling of 2 elements and sorting them, then moving one element up and sorting those 2, and so on, making n-1 passes. It also runs in O(__) time. _________ sort divides the list into individual units and sorts them accordingly once all divided. It runs in O(__) time. _________ sort partitions an array into 2 parts by selecting a pivot and deciding which elements are less than or greater than the pivot. It does this recursively, performing in average O(__) time, and O(__) worst case.

Strongly, weakly, complete (fully connected)

•A directed graph is ________ connected if there is a path from every vertex to every other vertex •A directed graph is ________ connected if its undirected representation is connected. •A ________ graph has an edge between every vertex to every other vertex (every pair of vertices)

Topological sort

•Given a DAG (directed acyclic graph), ordering of all the vertices so that every vertex comes before all of its neighbors •If there is a path from vi to vj, then vj appears after vi in that ordering Ex. course prerequisites or course completion order


Set pelajaran terkait

Final exam -Dimensions of Nursing Ch 12, 13, 14, 15, 16,17, 18, 21, 22, 23, 24, 27

View Set

Econ 2020 final practice questions

View Set

Homeostasis & Cell Transport Study Island

View Set

Principles of Management (Ch 6, 7, 8, 9) (only thorugh 6.2 for now)

View Set