CS 4306 Algorithm Analysis - Final Study Guide
What's the difference between a tree and a graph?
Graphs can have loops and multiple edges
How can you make the traveling salesman run in polynomial time with a minimum factor of 2 with the best run times?
- Utilize a minimum spanning tree with a pre-order tracker added - Use Prim's algorithm
What are the different ways to implement dynamic programming?
o Top down with memoization o Bottom up with optimal subset
What does Σ represent in a finite automata 5-tuple?
Σ - finite input alphabet
What are the 2 best ways to find if there is a path from starting node S to ending node T in a graph?
- Depth-first search utilizing a stack - Breadth-first search utilizing a queue
What does a string-matching algorithm attempt to do?
- Looks for the shift as to where the pattern occurs - Can find the pattern in more than one place
Adjacency matrix example:
Adjacency Matrix for graph in adjacency list example:
What is the 5-tuple of a finite automaton M?
(Q, q0, A, Σ, δ)
Adjacency list example:
Adjacency List for attached graph: A: B E F B: A C E C: B D E D: C E G E: A B C D G F: A G G: D E F
Adjacency List information
- Memory usage depends on the number of edges (not number of nodes), which might save a lot of memory if the adjacency matrix is sparse - Finding the presence or absence of specific edge between any two nodes is slightly slower than with the matrix: O(k); where k is the number of neighbors nodes - It is fast to iterate over all edges because you can access any node neighbors directly - It is fast to add/delete a node; easier than the matrix representation - It fast to add a new edge O(1) - Does NOT allow storing of weighted edges
Adjacency Matrix info
- Uses O(n^2) memory - Allows storage of the weights of the edges in the graph - fast to add a new edge O(1) - fast to lookup and check for presence or absence of a specific edge between any two nodes = O(1) - slow to iterate over all edges - slow to add/delete a node; a complex operation O(n^2)
What is an NP-complete problem?
- if there is a polynomial time solution for 1 then there is a polynomial time solution for all - Intuitively - "hardest" NP problems - Non-deterministic; same input can give different outputs - In order to be NP-complete, the problem must be both NP AND hard - need to show that no efficient algorithm is likely to exist - e.g. Travelling Salesman problem
What does q0 represent in a finite automata 5-tuple?
- q0 ∈ Q - start state
What does Q represent in a finite automata 5-tuple?
Q - a finite set of states; Q = {0,1,2,...,m} sub-definitions of Q: - q0 is the start state (q0) - M is the accepting state (qm) - |Q| = |pattern| + 1 = # of patterns
Selection Sort
A sort algorithm that repeatedly scans for the smallest item in the list and swaps it with the element at the current index. The index is then incremented, and the process repeats until the last two elements are sorted.
In-order traversal of binary search tree will produce
A sorted list.
What does A represent in a finite automata 5-tuple?
A ⊆ Q - set of accepting states
In the C/C++ programming language, the index of arrays start at:
ANSWER: 0
What is the minimum number of spanning trees in a connected graph?
ANSWER: 1
Which of the following algorithms does not divide the array being searched: A - linear search B - binary search C - merge sort D - quick sort
ANSWER: A: linear search EXPLANATION: searches for the desired element in the target list in a sequential manner, without breaking it in any way.
Which of the following sorting methods is a divide-and-conquer approach? A - Insertion Sort B - Merge Sort C - Shell Sort D - Heap Sort
ANSWER: B - Merge Sort EXPLANATION: Among the options, only Merge sort divides the list in sub-list, sorts and then merges them together
Which of the following is not an example of in-place algorithm? A - Bubble Sort B - Merge Sort C - Insertion Sort D - All of the above
ANSWER: B - Merge Sort EXPLANATION: Merge Sort requires extra memory to execute the division and then merging; therefore it is not in-place.
In conversion from prefix to postfix using a stack data structure, if operators and operands are pushed and popped exactly once, then the run-time complexity is: A - Ο(1) B - Ο(n) C - Ο(log n) D - Ο(n2)
ANSWER: B - O(n)
What does this formula represent? left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)
ANSWER: Binary Search Tree EXPLANATION: A binary search tree (BST) is a tree in which all nodes follows the below mentioned properties: • The left sub-tree of a node has key less than or equal to its parent node's key. • The right sub-tree of a node has key greater than or equal to its parent node's key.
Which of the following approaches use memoization? A - Greedy approach B - Divide and conquer approach C - Dynamic programming approach D - None of the above
ANSWER: C - Dynamic programming approach EXPLANATION: Storing the results of previously calculated solutions is called memoization
Which of the following is not an NP-Complete problem? a) Traveling Salesman Problem b) Vertex Cover Problem c) Fibonacci Problem d) Hamiltonian Path Problem
ANSWER: C - Fibonacci
Which of the following data structures has a search efficiency of O(1)? A - Tree B - Heap C - Hash Table D - Linked-List
ANSWER: C - Hash Table
When comparing recursion and iteration, which of the following statements is true? A - very expensive in terms of memory. B - low performance. C - every recursive program can be written with iteration too. D - all of the above
ANSWER: D - All of the above EXPLANATION: Recursion is just another way to write the same program code. But calling a function again and again makes it expensive in terms of memory, CPU cycles and delivers less performance.
What would be the worst run-time complexity of queue and dequeue operations on a queue that has been implemented using an array?
ANSWER: O(1) EXPLANATION: As queues are maintained by two separate pointers for queue and dequeue operations, the runtime for both is constant.
What is proof by mathematical induction?
ANSWER: Proof by mathematical induction has two steps: 1 - Show the statement (n) is true for a base case (n = k) 2 - Prove that the statement is also true for the next case (n = k+1)
What is the maximum degree of any vertex in a simple graph of vertices (n), where n is the number if vertices?
ANSWER: n - 1 EXPLANATION: In a simple graph, a vertex can have 1 less edge than it has vertices.
What data structure is used for depth first traversal of a graph?
ANSWER: stack
In doubly linked lists: A - a pointer is maintained to store both next and previous nodes. B - two pointers are maintained to store next and previous nodes. C - a pointer to self is maintained for each node. D - none of the above.
ANSWER: B - two pointers are maintained to store next and previous nodes. EXPLANATION: One pointer variable cannot store more than one address value.
What is important to have in a brute-force approach to algorithms?
An algorithm that checks every possibility of a solution until a problem is found.
Quick Sort
An in-place algorithm based on divide and conquer strategy: - Runtime: Θ(n log(n)) - It uses a key element (pivot) for partitioning the elements. - One left partition contains all those elements that are smaller than the pivot and one right partition contains all those elements which are greater than the key element. - Best if the target array is randomized before sorting - Best for large arrays (array > 10,000 elements)
What is matrix chain multiplication?
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The problem is not actually to perform the multiplications, but merely to decide in which order to perform the multiplications. EXAMPLE: A is a 10 × 30 matrix, B is a 30 × 5 matrix, and C is a 5 × 60 matrix. Then: o (AB)C = (10×30×5) + (10×5×60) = 1500 + 3000 = 4500 operations o A(BC) = (30×5×60) + (10×30×60) = 9000 + 18000 = 27000 operations.
How is traversing a graph different than traversing a tree?
Graphs may have loops.
What is the most efficient way to find a cycle in a graph?
Depth first search, because it uses a stack
What are the prefix and suffix comparisons in KMP?
Prefix - All the characters in a string pattern being searched for, with one or more cut off the end. Suffix - All the characters in a string pattern being searched for, with one or more cut off the beginning.
What does Dijkstra's algorithm do?
o Iterative algorithm that finds the shortest path from the source vertex to all other vertices in the graph o Rule 1: Make sure there are no negative edges; set distance to source vertex as zero o Rule 2: Relax all vertices adjacent to the current vertex o Rule 3: Choose the closest vertex as the next current vertex o Rule 4: Repeat rules 2 and 3 until the queue or destination is reached
What is a simple graph?
a graph with: - no loops - no multiple edges
Merge Sort
an external algorithm based on divide and conquer strategy: - Runtime: Θ(n log(n)) - The elements are split into two sub-arrays (n/2) again and again until only one element is left. - Merge sort uses additional storage for sorting the auxiliary array. - Merge sort uses three arrays where two are used for storing each half, and the third external one is used to store the final sorted list by merging other two and each array is then sorted recursively. - At last, the all sub arrays are merged to make it 'n' element size of the array. - Best for smaller arrays
Insertion Sort
first find the smallest element in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted
What does δ represent in a finite automata 5-tuple?
δ - a function from Q x Σ called the transition function of M