CSC 349 Final Reveiw
What is a cycle in a directed graph?
A directed path with at least 2 edges in which the first and last vertices are the same, but otherwise all vertices are distinct
What is a decision problem?
A problem for which any proposed solution can be checked in polynomial time for correctness
What is a path between two vertices in a graph?
A sequence of vertices with the property that each consecutive pair in the sequence is connected by an edge
What is an adjacency list?
A set of n linked lists, one for each vertex. The linked list for vertex v holds the names of all vertices, u, such that there is an edge from v to u
What is a connected component of a graph?
A subset of vertices such that every vertex in the subset has a path to every other vertex in the subset, AND is maximal
What do you need to do to problem inputs and outputs for reductions?
Change their descriptions to a decision version. Generally that means adding a threshold.
Once a tree is made as a result of DFS or BFS, how can you recover the original edge list?
Combine the tree edges and the back edges
What is the Master Theorem?
If T(n) = a( CEILING{n/b} ) + O(n^d), and a > 0, b >= 0, d>=0 then: 1. if d > logb(a), T(n) = O(n^d) 2. if d = logb(a), T(n) = O(n^dlogn) 3. if d < logb(a), T(n) = O(n^(logb(a)))
What qualifies if vertices A and B are neighbors in a graph?
If they are connected by an edge
What is the distance between two disconnected points?
Infinity
What is merge sort's run time?
O(nlogn)
What is a c-approximation algorithm?
OPT <= f(x) <= cOPT
What are the four different types of edges in a directed DFS tree?
Regular tree edges, back edges, forward edges, and cross edges
What are strongly connected components in a directed graph?
Strongly connected subgraphs
What is a reduction?
The existence of functions f and h that turn a decision problem A into another decision problem B in polynomial time
What is the distance between two vertices in a graph?
The length of the shortest path connected them
What is the degree of a vertex?
The number of edges incident to the vertex
What does connected mean in the context of a directed graph?
There is a path from x to y AND y to x
C&A HW#8: Prove that set cover is NP-complete using vertex cover.
f: (S, k) (suppose there are n vertices in G. Define Si (1 <= i <= n) to contain the edges incident to vertex i h: h(S) = V (for each Si in S, include the vertex that it corresponds to in the solution set)
What is a strongly connected directed graph?
for all pairs of vertices, u,v in V, u and v are connected
What qualifies an NP-Complete problem, X?
- X is in NP -Every problem in NP is reducible to X in polynomial time (if you can solve X, you can solve all)
What is NP-Hard?
-Every problem in NP is reducible to X in polynomial time (if you can solve X, you can solve all) - X is NOT in NP
How does divide and conquer solve problems? (3 steps)
1. Breaking the problem into subproblems that are themselves smaller instances of the same type of problem 2. Recursively solving the subproblems 3. Appropriately combining their answers
Order the following fastest to slowest: 17n, n!, 48nlogb(n), n^3, 36, 367n^2logb(n), 2^n, logb(n), n^n
36, logb(n), 17n, 48nlogb(n), 367n^2logb(n), 2^n, n^3, n!, n^n
What are the two basic ways to represent a graph?
Adjacency matrix and adjacency list
What is an adjacency matrix?
An nxn array where the (i, j) entry is: -1 if there's an edge from vi to vj -0 if otherwise
What algorithm should you use to determine distance?
BFS
What is a connected graph?
For each pair of vertices, there's a path between them
How does selection sort work?
Iterates through the list and swaps the minimum valued element with the current element (unless the current element is the minimum)
What is the indegree of a vertex in a directed graph?
Number of edges directed into v
What is the outdegree of a vertex in a directed graph?
Number of edges directed out of v
What is selection sort's run time?
O(n^2)
What is the run time of matrix multiplication?
O(n^3)
What is Dijkstra's Algorithm?
Overestimate the distance of each vertex from the starting vertex (set to infinity). Then we visit each node and its neighbors to find the shortest subpath to those neighbors.
Tree method practice relations: 1. T(n) = T(n/2) + O(1) 2. T(n) = 2T(n/2) + O(n) 3. T(n) = 2T(n/2) + O(1) 4. T(n) = T(n-1) + O(1) 5. T(n) = T(n/2) + O(n^2) 6. T(n) = T(n-1) + O(n) 7. T(n) = T(n/2) + O(n) 8. T(n) = 3T(n/3) + O(1) 9. T(n) = T(n/3) + O(n)
Solutions: 1. O(log2(n)) 2. O(nlog2(n)) 3. O(n) 4. O(n) 5. O(n^2) 6. O(n^2) 7. O(n) 8. O(n) 9. O(n)
What property of a tree guarantees a cycle in a graph?
The existence of back edges
Does P = NP?
We don't know
What is a graph?
A collection of vertices and edges between them
What is a Eularian Cycle?
A cycle that includes every edge once
What is a Hamiltonian Cycle?
A cycle that includes every vertex once
With DFS, how can you determine back edges?
By using pre and post visit numbers
What algorithm should you use to determine reachability?
DFS
What algorithm should you use to determine shortest path in weighted graphs?
Dijkstra
What is a DAG graph?
Directed, acyclic graph: a directed graph with no cycles
How does merge sort work?
Divides list into two equal halves until they are at the smallest sorted value. Merge the sorted list based off of minimum value.
D&C HW#6: Suppose that you are given an integer, x, and a sorted list of integers. Design and analyze a divide and conquer algorithm that counts the number of occurrences of x in the list. Example: Suppose x = 4 and the list is {1, 2, 2, 2, 4, 4, 12, 20, 20, 20}. Your algorithm should return 2. Give the run time.
Summary: -function takes in list, x, min, max -if max < min return 0 -if there's only one item in the list and it equals x, return 1, otherwise return 0 -return the sum of one recursive call on the first half (min -> mid) and another recursive call on the second half (mid+1->max)
G HW#16 Algorithm: Suppose a CS curriculum consists of n courses, all of them mandatory. The prerequisite graph G has a vertex for each course, and an edge from course v to course w if and only if v is a prerequisite for w. Design and analyze an algorithm that works directly with this graph representation, and computes the minimum number of quarters necessary to complete the curriculum (assume that a student can take any number of courses in one quarter)
fill in
What is a key theorem about directed graphs and strongly connected components?
Every directed graph is a DAG of its strongly connected components
What is P?
The class of all decision problems that can be solved in polynomial time (all problems that have a polynomial time checking and solving algorithm) *P is in NP
D&C HW#9: Suppose we are given a list of n numbers representing stock prices on a single day. We want to find a pair (buy, sell) with buy ≤sell such that if we bought the stock on buy day and sold the stock on sell day, we would maximize our profit. Design and analyze a divide and conquer algorithm that finds the optimal (buy, sell) pair. Give the run time.
Summary: Function takes in an array of a given stock's price, each index representing a different day: - both min and max are set to 0 to handle the base case of n = 1 - min and max can be initialized to 0 because a stock's price can never be negative if size of array is 1: - if element in array is < min or min = 0, set min to element in array - if element in array is > max or max = 0, set max to element in array - return (min, max) else: - let leftPair be a recursive call on the first half of the list - let rightPair be a recursive call on the second half of the list - return the min of the two mins and the max of the two maxes returned by leftPair and rightPair
D&C HW#7: Suppose that you are given a list of n elements. Design and analyze a divide and conquer algorithm to remove all duplicates from the list in time O(n log n). Give the run time.
Summary: first function: -takes in the list -if there's 1 or less items in the list, return the list -otherwise return the function 2 with the two arguments as the first half of the list and the second half of the list second function: -takes in two lists -if one of the lists is empty, return the other -if the first element of the first list is less than the first element of the second list, concatenate the first element of the first list with a recursive call with the first list w/o the first arg, and the second list as is -if the first element of the first list is greater than the first element of the second list, concatenate the first element of the second list with a recursive call with the second list w/o the first arg, and the first list as is -otherwise (if the two are equal) return a recursive call with the first element of the first list removed
D&C HW#12: You are given a list of numbers. Your goal is to return the sum of the contiguous sublist of numbers that has the largest sum. For example, if you are given the list {1, −4, 3, 7, −5, 6, −9, 5}, your algorithm should return 11.Design and analyze a divide and conquer algorithm to solve this problem. Give the run time.
Summary: function 1: -takes in the list -if A is empty return zero, if it has one element return the max of 0 and that element -otherwise return the max between a recursive call over 0 to mid, a recursive call over mid to n-1, and the max crossover sum function 2: -return the leftcount from 0 to mid + the right count mid+1 to n-1 leftcount: max sum starting at mid and ending at zero rightcount: max sum starting at mid and ending at n-1
D&C HW#10: In this problem we find the closest pair of points in the Euclidean plan. Suppose you are given a set of n points in the plane. The goal is to find the two points that are the closest. Recall that the distance between two points, a = (ax, ay) and b = (bx, by) is √(ax −bx)2 + (ay −by)2.Design and analyze a divide and conquer algorithm to solve this problem. Give the run time.
Summary: preprocess: -sort points in increasing order by x coordinate function 1: -takes in list of coordinate pairs -if there's only two points in the list, they're the closest so return them -set d1 to a recursive call on the first half of the coordinates -set d2 to a recursive call on the second half of the coordinates -let d be the minimum of d1 and d2 -return the argmin between d1, d2, and a call to function 2 with the following arguments: (x at the midpoint - d, y at the midpoint - d), (x at the midpoint + 1 + d, y at the midpoint + 1 + d) function 2: -takes in d and a list -let the current min distance be the distance between the first two points in the list -let the current min points be the first two points -for all i in the first half of the coordinates (0 -> mid) -for all j in the second half of the coordinates (mid+1->n-1) -if the distance between the coordinate at index i and the coordinate at index j is less then the current min distance, replace the current min distance with that new distance and replace the current min points with those points
D&C HW#4: Suppose that you are given a sorted list of distinct integers {a1, a2, . . . an}. Design and analyze a divide-and-conquer algorithm that determines whether there exists an index i such that ai = i. For example, in {−10, −4, 3, 41}, a3 = 3, and in {4, 7, 19, 20} there is no such i. Give the run time.
Summary: -Takes in list, min, and max -if max < min, return -1 -If the middle value is less than the middle index, recursively call function on the second half of the list (mid+1 -> max) -if the middle value is greater than the middle index, recursively call function on the first half of the list (min -> mid - 1) -if the middle value equals the middle index, return the middle index
D&C HW#5: Suppose you are given 3^n marbles that look identical, with one special marble that weighs more than the other marbles. You are also given a balancing scale that takes two items (or sets of items) and compares their weights. Design and analyze a divide and conquer algorithm to find the heavy marble using the balancing scale at most n times. Give the run time.
Summary: -takes in list, min, and max -if max < min return -1 -if the first third is greater than the second third, recursively call the function on the first third (min -> n/3) -if the second third is greater than the first third, recursively call the function on the second third (n/3 -> 2n/3) -otherwise, recursively call the function on the last third (2n/3 -> max)
D&C HW#8: A list A is said to have a majority element if more than half of its entries are the same. There is not necessarily an order on the list, so there can't be comparisons of the form "Is A[i] ≤A[j]?". However, in constant time, the question "Is A[i] = A[j]?" can be answered. Given an array, design a divide and conquer algorithm to determine if there is a majority element, and, if so, to find that element. Your algorithm should run in time O(n log n). Give the run time.
The algorithm recursively breaks down the array into increasingly small subarrays that then produce the correct majority element for that subarray. The reason this works is because for an array to have a majority element, then at least one half of the array ought to have that same majority element. Thus, after all recursive calls complete, either elementL or elementR will have the correct majority element, or it doesnt have one.
What is NP?
The class of all decision problems (all problems that have a polynomial time checking algorithm)
What is a degree distribution of a graph?
list containing the number of vertices of each degree