├── README.md ├── part1 ├── week5.md ├── week2.md ├── week6.md ├── week3.md ├── week4.md └── week1.md └── part2 ├── week4.md ├── week2.md ├── week1.md ├── week5.md ├── week6.md └── week3.md /README.md: -------------------------------------------------------------------------------- 1 | # Interview-Questions-of-Algorithms-by-Princeton 2 | personal views on interview questions of course "Algorithms" in coursera by Princeton, not guaranteed to be correct 3 | the course link is https://www.coursera.org/learn/algorithms-part1 and https://www.coursera.org/learn/algorithms-part2 4 | 5 | 6 | Looking back years after I created this repo, I can see some **dazzling** **blatant** errors in my answers. However, I don't have energy to update it nowadays. So use it with your judgement. 7 | -------------------------------------------------------------------------------- /part1/week5.md: -------------------------------------------------------------------------------- 1 | ## Balanced Search Trees 2 | 1.Red–black BST with no extra memory. Describe how to save the memory for storing the color information when implementing a red–black BST. 3 | 4 | if it's an internal node, reverse the left and right child to mark it red, else, it's a leaf node, make its left and right child point to itself to mark it red,otherwise be null 5 | 6 | 2.Document search. Design an algorithm that takes a sequence of n document words and a sequence of m query words and find the shortest interval in which the m query words appear in the document in the order given. The length of an interval is the number of words in that interval. 7 | 8 | for each occurrence of the first query word, determine the shortest interval beginning from that word using greedy algorithm, then find the min among all the occurrences. 9 | 10 | 3.Generalized queue. Design a generalized queue data type that supports all of the following operations in logarithmic time (or better) in the worst case. 11 | * Create an empty data structure. 12 | * Append an item to the end of the queue. 13 | * Remove an item from the front of the queue. 14 | * Return the ith item in the queue. 15 | * Remove the ith item from the queue. 16 | 17 | create a red–black BST where the keys are integers and the values are the items such that the ith largest integer key in the red–black BST corresponds to the ith item in the queue. 18 | -------------------------------------------------------------------------------- /part1/week2.md: -------------------------------------------------------------------------------- 1 | ## Stacks and Queues 2 | 1.Queue with two stacks. Implement a queue with two stacks so that each queue operations takes a constant amortized number of stack operations. 3 | 4 | enqueue: push to `stack#1` 5 | 6 | dequeue: if `stack#2` is empty { pop all from `stack#1` and push `stack#2` } else { pop from `stack#2` } 7 | 8 | 2.Stack with max. Create a data structure that efficiently supports the stack operations (push and pop) and also a return-the-maximum operation. Assume the elements are reals numbers so that you can compare them. 9 | 10 | use two stacks, one stores the data, another stores the max from the bottom to each storey 11 | 12 | 3.Java generics. Explain why Java prohibits generic array creation. 13 | 14 | https://www.quora.com/Why-does-Java-prohibit-generic-array-creation 15 | 16 | ## Elementary Sorts 17 | 1.Intersection of two sets. Given two arrays a[] and b[], each containing n distinct 2D points in the plane, design a subquadratic algorithm to count the number of points that are contained both in array a[] and array b[]. 18 | 19 | first sort and then count 20 | 21 | 2.Permutation. Given two integer arrays of size nn, design a subquadratic algorithm to determine whether one is a permutation of the other. That is, do they contain exactly the same entries but, possibly, in a different order. 22 | 23 | sort both arrays 24 | 25 | 3.Dutch national flag. Given an array of nn buckets, each containing a red, white, or blue pebble, sort them by color. The allowed operations are: 26 |
27 | swap(i,j): swap the pebble in bucket i with the pebble in bucket j. 28 | color(i): determine the color of the pebble in bucket i. 29 |30 | The performance requirements are as follows: 31 |
32 | At most n calls to color(). 33 | At most n calls to swap(). 34 | Constant extra space. 35 |36 | 37 | 3-way partitioning,but need not to swap the first item with pivot because we already know that there are only 3 colors. 38 | -------------------------------------------------------------------------------- /part1/week6.md: -------------------------------------------------------------------------------- 1 | ## Hash Tables 2 | 1.4-SUM. Given an array a[] of nn integers, the 4-SUM problem is to determine if there exist distinct indices i, j, k, and l such that a[i]+a[j]=a[k]+a[l]. Design an algorithm for the 4-SUM problem that takes time proportional to n^2(under suitable technical assumptions). 3 | 4 | store all possible sum of two integers to hash table, if the position to insert that already contains a sum, check if they are equal, if so, there exist. 5 | 6 | 2.Hashing with wrong hashCode() or equals(). Suppose that you implement a data type OlympicAthlete for use in a java.util.HashMap. 7 |
8 | Describe what happens if you override hashCode() but not equals(). 9 | Describe what happens if you override equals() but not hashCode(). 10 | Describe what happens if you override hashCode() but implement public boolean equals(OlympicAthlete that) instead of public boolean equals(Object that). 11 |12 | 13 | https://www.geeksforgeeks.org/override-equalsobject-hashcode-method/ 14 | in terms of the third question, the problems could be subtle.please refer to http://users.csc.calpoly.edu/~gfisher/classes/102/info/howToOverrideEquals.html 15 |
16 | When you override any method you must match the method name, return type, and parameter types exactly. For the equals method, it must be this:
17 |
18 | public boolean equals(Object other)
19 | {
20 | // Logic here to be discuss below...
21 | }
22 |
23 | Notice that the parameter type is Object - it must be Object or you will have overloaded equals instead of overriding it. The errors that can occur when you do this are subtle. Your code will work correctly much of the time but fail some of the time. This is due to polymorphism and runtime binding of methods. The affect is that, depending on the type of the parameter being passed to equals, sometimes your equals method will execute and sometimes the one in Object (performing strict reference equality) will be execute which, you recall, performs reference equality not logical equality.
24 |
25 |
--------------------------------------------------------------------------------
/part2/week4.md:
--------------------------------------------------------------------------------
1 | ## Tries
2 | 1. Prefix free codes. In data compression, a set of binary strings is if no string is a prefix of another. For example, {01,10,0010,1111} is prefix free, but {01,10,0010,10100} is not because 10 is a prefix of 10100. Design an efficient algorithm to determine if a set of binary strings is prefix-free. The running time of your algorithm should be proportional the number of bits in all of the binary stings.
3 |
4 | insert the binary strings into a 2-way trie
5 |
6 | 2. Boggle. Boggle is a word game played on an 44-by-44 grid of tiles, where each tile contains one letter in the alphabet. The goal is to find all words in the dictionary that can be made by following a path of adjacent tiles (with no tile repeated), where two tiles are adjacent if they are horizontal, vertical, or diagonal neighbors.
7 |
8 | create a trie containing all of the words in the dictionary,starting from each grid, using dfs to trace down all the possible paths if it is not null in the trie
9 |
10 | 3. Suffix trees. Learn about and implement , the ultimate string searching data structure.
11 |
12 | lectures ahead talked about this.
13 |
14 | ## Substring Search
15 | 1. Cyclic rotation of a string. A string s is a cyclic rotation of a string t if s and t have the same length and s consists of a suffix of t followed by a prefix of t. For example, "winterbreak" is a cyclic rotation of "breakwinter" (and vice versa). Design a linear-time algorithm to determine whether one string is a cyclic rotation of another.
16 |
17 | hash a string s as sum of hashcode for all possible cyclic rotations of s, compare the hash code(can use horner's method to speed up,see lectures), or use kmp
18 |
19 | 2. Tandem repeat. A tandem repeat of a base string b within a string s is a substring of s consisting of at least one consecutive copy of the base string b. Given b and s, design an algorithm to find a tandem repeat of b within s of maximum length. Your algorithm should run in time proportional to M+N, where M is length of b and N is the length s.
20 | For example, if s is "abcabcababcaba" and b is "abcab", then "abcababcab" is the tandem substring of maximum length (2 copies).
21 |
22 | boyer-moore or kmp
23 |
24 | 3. Longest palindromic substring. Given a string s, find the longest substring that is a palindrome in expected linearithmic time.
25 | Signing bonus: Do it in linear time in the worst case.
26 |
27 | manacher's algorithm
28 |
29 |
30 |
--------------------------------------------------------------------------------
/part2/week2.md:
--------------------------------------------------------------------------------
1 | ## Minimum Spanning Trees
2 | 1. Bottleneck minimum spanning tree. Given a connected edge-weighted graph, design an efficient algorithm to find a minimum bottleneck spanning tree. The bottleneck capacity of a spanning tree is the weights of its largest edge. A minimum bottleneck spanning tree is a spanning tree of minimum bottleneck capacity.
3 |
4 | mst is mbst(O(E) algorithm:Camerini's algorithm)
5 |
6 | 2. Is an edge in a MST. Given an edge-weighted graph G and an edge e, design a linear-time algorithm to determine whether e appears in some MST of G.
7 | Note: Since your algorithm must take linear time in the worst case, you cannot afford to compute the MST itself.
8 |
9 | consider the subgraph G' of G containing only those edges whose weight is strictly less than that of e,if it's connected, then yes, else no.
10 |
11 | 3. Minimum-weight feedback edge set. A feedback edge set of a graph is a subset of edges that contains at least one edge from every cycle in the graph. If the edges of a feedback edge set are removed, the resulting graph is acyclic. Given an edge-weighted graph, design an efficient algorithm to find a feedback edge set of minimum weight. Assume the edge weights are positive.
12 |
13 | https://cstheory.stackexchange.com/questions/36222/minimum-weight-feedback-edge-set-in-undirected-graph-how-to-find-it-is-it-np
14 |
15 | ## Shortest Paths
16 | 1. Monotonic shortest path. Given an edge-weighted digraph G, design an ElogE algorithm to find a monotonic shortest path from s to every other vertex. A path is monotonic if the sequence of edge weights along the path are either strictly increasing or strictly decreasing.
17 |
18 | https://stackoverflow.com/questions/22876105/find-a-monotonic-shortest-path-in-a-graph-in-oe-logv
19 |
20 | 2. Second shortest path. Given an edge-weighted digraph and let P be a shortest path from vertex s to vertex t. Design an ElogV algorithm to find a path other than P from s to t that is as short as possible. Assume all of the edge weights are strictly positive.
21 |
22 | compute the shortest path distances from s to every vertex and the shortest path distances from every vertex to t.
23 |
24 | 3. Shortest path with one skippable edge. Given an edge-weighted digraph, design an ElogV algorithm to find a shortest path from s to t where you can change the weight of any one edge to zero. Assume the edge weights are nonnegative.
25 |
26 | use a modified version of Dijkstra's algorithm, maintain an array of max weight of path from s to every vertex, each time we select a new edge and its end node, we choose the one with the shortest path given that we could delete the max weight from path.
27 |
28 |
29 |
--------------------------------------------------------------------------------
/part1/week3.md:
--------------------------------------------------------------------------------
1 | ## Mergesort
2 | 1.Merging with smaller auxiliary array. Suppose that the subarray a[0] to a[n−1] is sorted and the subarray a[n] to a[2∗n−1] is sorted. How can you merge the two subarrays so that a[0] to a[2∗n−1] is sorted using an auxiliary array of length nn (instead of 2n)?
3 |
4 | copy only the left half into the auxiliary array and then merge directly to the original array.
5 |
6 | 2.Counting inversions. An inversion in an array a[] is a pair of entries a[i] and a[j] such that i21 | Version 1 : n1 = n2 and k = n/2 22 | Version 2: k = n/2 23 | Version 3: no restrictions 24 |25 | 26 | general approach: 27 |
28 | while not found: 29 | let i=length of a/2,j=length of b/2 30 | if k>i+j, 31 | if b[j]<=a[i], 32 | k-=j 33 | b=b[j+1:] 34 | else, 35 | k-=i 36 | a=a[i+1:] 37 | else, 38 | if b[j]<=a[i], 39 | a=a[:i] 40 | else, 41 | b=b[:j] 42 | 43 | Recur in a subproblem of roughly half the size. 44 |45 | details might be tricky,omitted 46 | https://www.geeksforgeeks.org/k-th-element-two-sorted-arrays/ 47 | 48 | 3.Decimal dominants. Given an array with nn keys, design an algorithm to find all values that occur more than n/10n/10 times. The expected running time of your algorithm should be linear. 49 | 50 | https://www.cnblogs.com/evasean/p/7273857.html https://stackoverflow.com/questions/50558730/how-to-find-all-values-that-occur-more-than-n-k-times-in-an-array 51 | -------------------------------------------------------------------------------- /part2/week1.md: -------------------------------------------------------------------------------- 1 | ## Undirected Graphs 2 | 1. Nonrecursive depth-first search. Implement depth-first search in an undirected graph without using recursion. 3 | 4 | use an explicit stack 5 | 6 | 2. Diameter and center of a tree. Given a connected graph with no cycles 7 | Diameter: design a linear-time algorithm to find the longest simple path in the graph. 8 | Center: design a linear-time algorithm to find a vertex such that its maximum distance from any other vertex is minimized. 9 | 10 | (diameter): to compute the diameter, pick a vertex ss; run BFS from ss; then run BFS again from the vertex that is furthest from ss. 11 | (center): middle vertices on the longest path. 12 | 13 | 3. Euler cycle. An Euler cycle in a graph is a cycle (not necessarily simple) that uses every edge in the graph exactly one. 14 | Show that a connected graph has an Euler cycle if and only if every vertex has even degree. 15 | Design a linear-time algorithm to determine whether a graph has an Euler cycle, and if so, find one. 16 | 17 | Because in euler cycle every vertex must be visited from one edge and be departed from another. 18 | Traverse all the edges by, first randomly select one unvistied edge and then visit its arbitrary unvisited adjacent edge and so forth until there are no unvisited adjacent edges, if there are still unvisited edges, randomly select one and repeat the above process. If edge traversal of all times form a loop, then it has euler cycle. 19 | 20 | ## Directed Graphs 21 | 1. Shortest directed cycle. Given a digraph G, design an efficient algorithm to find a directed cycle with the minimum number of edges (or report that the graph is acyclic). The running time of your algorithm should be at most proportional to V(E+V) and use space proportional to E+V, where V is the number of vertices and E is the number of edges. 22 | 23 | run BFS from each vertex 24 | 25 | 2. Hamiltonian path in a DAG. Given a directed acyclic graph, design a linear-time algorithm to determine whether it has a Hamiltonian path (a simple path that visits every vertex), and if so, find one. 26 | 27 | if more than one vertex has 0 in degree or more than one vertex has 0 out degree, then none, else topological sort from the vertex that has 0 in degree, when encountering branches, record the vertex we randomly shift to, if we reach the termination points and stil have unvisited vertices, we should return back to same vertex with multiple children again, when we recurse on another vertex and check if it will end on the vertex posterior to the one we record, if not, then no. 28 | or https://stackoverflow.com/questions/16124844/algorithm-for-finding-a-hamilton-path-in-a-dag 29 | 30 | 3. Reachable vertex. 31 | DAG: Design a linear-time algorithm to determine whether a DAG has a vertex that is reachable from every other vertex, and if so, find one. 32 | Digraph: Design a linear-time algorithm to determine whether a digraph has a vertex that is reachable from every other vertex, and if so, find one. 33 | 34 | DAG:if it's reachable from every other vertex, then it must have 0 out degree, run dfs from this vertex on the reverse graph 35 | Digraph:compute the strongly connected components(Kosaraju's algorithm), run dfs on random vertex of the last scc, and check if it can reach every other scc. 36 | -------------------------------------------------------------------------------- /part2/week5.md: -------------------------------------------------------------------------------- 1 | ## Regular Expressions 2 | 1. Challenging REs. Construct a regular expression for each of the following languages over the binary alphabet or prove that no such regular expression is possible: 3 |
4 | All strings except 11 or 111. 5 | Strings with 1 in every odd-number bit position. 6 | Strings with an equal number of 0s and 1s. 7 | Strings with at least two 0s and at most one 1. 8 | Strings that when interpreted as a binary integer are a multiple of 3. 9 | Strings with no two consecutive 1s. 10 | Strings that are palindromes (same forwards and backwards). 11 | Strings with an equal number of substrings of the form 01 and 10. 12 | 13 | 1|10(0|1)*|110(0|1)*|111(0|1)+|0(0|1)* 14 | (1(0|1)+)*1* 15 | impossible, proof: https://stackoverflow.com/questions/12497514/regular-expression-for-equal-number-of-0-and-1 16 | 00+|100+|010+|0010*|00+10* 17 | https://stackoverflow.com/questions/7974655/regex-for-binary-multiple-of-3 18 | 0*(10|0)* 19 | impossible, proof: https://stackoverflow.com/questions/233243/how-to-check-that-a-string-is-a-palindrome-using-regular-expressions 20 | https://cs.stackexchange.com/questions/57342/write-a-regular-expression-contains-an-equal-number-of-01-and-10-subtrings 21 |22 | 23 | general idea:https://cs.stackexchange.com/questions/45570/how-do-i-find-a-regular-expression-for-a-particular-language 24 | 25 | 2. Exponential-size DFA. Design a regular expressions of length n such that any DFA that recognizes the same language has an exponential number of states 26 | 27 | https://cs.stackexchange.com/questions/6063/how-to-prove-that-dfas-from-nfas-can-have-exponential-number-of-states 28 | 29 | 3. Extensions to NFA. Add to NFA.java the ability to handle multiway or, wildcard, and the + closure operator. 30 | 31 | omitted. 32 | 33 | ### Data Compression 34 | 1. Ternary Huffman codes. Generalize the Huffman algorithm to codewords over the ternary alphabet (0, 1, and 2) instead of the binary alphabet. That is, given a bytestream, find a prefix-free ternary code that uses as few trits (0s, 1s, and 2s) as possible. Prove that it yields optimal prefix-free ternary code. 35 | 36 | if it's not optimal, there exist some code that could be shorter, yet shorter code representations has been occupied by words with higher frequency, so it has to replace, then it cannot use fewer trits 37 | 38 | 2. * Identify an optimal uniquely-decodable code that is neither prefix free nor suffix tree. 39 | * Identify two optimal prefix-free codes for the same input that have a different distribution of codeword lengths. 40 | 41 | { 0011, 011, 11, 1110 } or { 01, 10, 011, 110 }. 42 | when building trees, if two subtrees are equally frequent, one tree chooses the lower one, the other choose the higher one 43 | 44 | 3. Move-to-front coding. Design an algorithm to implement move-to-front encoding so that each operation takes logarithmic time in the worst case. That is, maintain alphabet of symbols in a list. A symbol is encoded as the number of symbols that precede it in the list. After encoding a symbol, move it to the front of the list. 45 | 46 | use segment tree to maintain prefix sum. idea similar to: https://leetcode.com/problems/minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits/discuss/720548/O(n-logn)-or-Detailed-Explanation 47 | -------------------------------------------------------------------------------- /part2/week6.md: -------------------------------------------------------------------------------- 1 | ### Reductions 2 | 1. Longest path and longest cycle. Consider the following two problems 3 | * LongestPath: Given an undirected graph G and two distinct vertices s and t, find a simple path (no repeated vertices) between s and t with the most edges. 4 | * LongestCycle: Given an undirected graph G′, find a simple cycle (no repeated vertices or edges except the first and last vertex) with the most edges. 5 | Show that LongestPath linear-time reduces to LongestCycle. 6 | 7 | add a new path (with new vertices) between s and t. 8 | 9 | 2. 3Sum and 4Sum. Consider the following two problems: 10 | * 3Sum: Given an integer array a, are there three distinct indices i, j, and k such that a_i + a_j + a_k = 0=0? 11 | * 4Sum: Given an integer array b, are there four distinct integers i, j, k, and l such that b_i + b_j + b_k + b_l = 0? 12 | Show that 3Sum linear-time reduces to 4Sum. 13 | 14 | add M= max_i|ai| + 1 to every element and append -3*M,compute 4 sum 15 | 16 | 3. 3Sum and 3Linear. Consider the following two problems: 17 | * 3Linear: Given an integer array aa, are there three indices (not necessarily distinct) i, j, and k such that a_i + a_j = 8*a_k? 18 | * 3Sum: Given an integer array bb, are there three indices (not necessarily distinct) ii, jj, and kk such that b_i + b_j + b_k = 0? 19 | Show that 3Linear linear-time reduces to 3Sum. 20 | 21 | let M= max_i|ai| + 1, for each element x in array,append -8*x-8*M to array , for each original element in array, add 4*M inplace 22 | 23 | ### Linear Programming 24 | too hard, skipped 25 | 26 | ### Intractability 27 | 28 | 1. Graph 3-colorability. An undirected graph is 3-colorable if the vertices can be colored red, green, or blue in such a way that no edge connects two vertices with the same color. Prove that 3COLOR is NP-complete. 29 | 30 | reduce to sat, for each edge, s represented by x1+x2, t represented by y1+y2, or all the possible "and assignment" together equals true (possible sum:0,1,2) or (!0 and !0 and !0 and 1) or (1 and !0 and !0 and !0) or ...=true 31 | 32 | 2. Decision problems. Traditionally, the complexity classes P and NP are defined in terms of decision problems (where the answer is either yes or no) instead of search problems (where the answer is the solution itself). Prove that the search problem version of SAT (find a binary solution to a given system of boolean equations?) polynomial-time reduces to the decision version of SAT (does there exists a binary solution to a given system of boolean equations?). 33 | 34 | to determine whether to set x_1 to true, set it to true; simplify the resulting system of boolean equations; and check whether there exists a solution to the simplified system. 35 | 36 | 3. Optimization problems. Given an undirected graph with positive edge weights, the traveling salesperson problem is to find a simple cycle that visits every vertex and has minimum total weight. The search problem version of the problem is, given a parameter LL, find a tour of length at most LL. Prove that the optimization version of the problem polynomial-time reduces to the search version of the problem. 37 | Remark: for many problems such as this one, the optimization version of the problem (which is not known to be in NP) is solvable in polynomial time if and only if the search version of the problem (which is easily seen to be in NP) is. 38 | 39 | binary search 40 | -------------------------------------------------------------------------------- /part1/week4.md: -------------------------------------------------------------------------------- 1 | ## Priority Queues 2 | 1.Dynamic median. Design a data type that supports insert in logarithmic time, find-the-median in constant time, and remove-the-median in logarithmic time. 3 | 4 | maintain two binary heaps, one that is max-oriented and one that is min-oriented. 5 | https://stackoverflow.com/questions/15319561/how-to-implement-a-median-heap 6 | 7 | 2.Randomized priority queue. Describe how to add the methods sample() and delRandom() to our binary heap implementation. The two methods return a key that is chosen uniformly at random among the remaining keys, with the latter method also removing that key. The sample() method should take constant time; the delRandom() method should take logarithmic time. Do not worry about resizing the underlying array. 8 | 9 | sample():sample from the underlying array 10 | delRandom():swap with the last element, and then swim up or sink down, delete the last item(after swap) 11 | 12 | 3.Taxicab numbers. A taxicab number is an integer that can be expressed as the sum of two cubes of positive integers in two different ways: a^3 + b^3 = c^3 + d^3. For example, 1729 is the smallest taxicab number: 9^3 + 10^3 = 1^3 + 12^39 . Design an algorithm to find all taxicab numbers less than n. 13 |
14 | Version 1: Use time proportional to n^2logn and space proportional to n^2. 15 | Version 2:Use time proportional to n^2logn and space proportional to n. 16 |17 | 18 | I think the question does not state the task correctly. Actually, we should design an algorithm to find all taxicab numbers with a, b, c, and d less than n. 19 | generate the sum of two cubes for all possibilities from 1,8,27,...,(n-1)^3 20 | 1.quicksort all the sums and traverse from the beginning 21 | 2. https://stackoverflow.com/a/23658420/7801448 very brilliant! 22 | 23 | ## Elementary Symbol Tables 24 | 1.Java autoboxing and equals(). Consider two double values a and b and their corresponding Double values x and y. 25 |
26 | Find values such that (a==b) is true but x.equals(y) is false. 27 | Find values such that (a==b) is false but x.equals(y) is true. 28 |29 | 30 | https://stackoverflow.com/a/12559675/7801448 31 |
32 | double x1 = 0.0, y1 = -0.0; 33 | Double a1 = x1, b1 = y1; 34 | StdOut.println(x1 == y1); 35 | StdOut.println(a1.equals(b1)); 36 | 37 | double x2 = 0.0/0.0, y2 = 0.0/0.0; 38 | Double a2 = x2, b2 = y2; 39 | StdOut.println(x2 != y2); 40 | StdOut.println(!a2.equals(b2)); 41 |42 | 43 | 2.Check if a binary tree is a BST. Given a binary tree where each Node contains a key, determine whether it is a binary search tree. Use extra space proportional to the height of the tree. 44 | 45 | Hint: design a recursive function isBST(Nodex,Keymin,Keymax) that determines whether x is the root of a binary search tree with all keys between min and max. 46 | 47 | 3.Inorder traversal with constant extra space. Design an algorithm to perform an inorder traversal of a binary search tree using only a constant amount of extra space. 48 | 49 | https://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/ 50 | https://www.scss.tcd.ie/disciplines/software_systems/fmg/fmg_web/IFMSIG/winter2000/HughGibbonsSlides.pdf 51 | 52 | 4.Web tracking. Suppose that you are tracking nn web sites and mm users and you want to support the following API: 53 |
54 | User visits a website. 55 | How many times has a given user visited a given site? 56 |57 | What data structure or data structures would you use? 58 | 59 | Hint: maintain a symbol table of symbol tables. 60 | -------------------------------------------------------------------------------- /part1/week1.md: -------------------------------------------------------------------------------- 1 | ## union-find 2 | 1.Social network connectivity. Given a social network containing n members and a log file containing m timestamps at which times pairs of members formed friendships, design an algorithm to determine the earliest time at which all members are connected (i.e., every member is a friend of a friend of a friend ... of a friend). Assume that the log file is sorted by timestamp and that friendship is an equivalence relation. The running time of your algorithm should be mlogn or better and use extra space proportional to n. 3 | 4 | use weighted quick union until size of root reaches n 5 | 6 | 2.Union-find with specific canonical element. Add a method find() to the union-find data type so that find(i) returns the largest element in the connected component containing i. The operations, union(), connected(), and find() should all take logarithmic time or better. 7 | For example, if one of the connected components is {1,2,6,9}, then the find() method should return 99 for each of the four elements in the connected components. 8 | 9 | as shown in the hint,maintain an extra array to the weighted quick-union data structure that stores for each root i the large element in the connected component containing i. 10 | 11 | 3.Successor with delete. Given a set of n integers S={0,1,...,n−1} and a sequence of requests of the following form: 12 |
13 | Remove x from S 14 | Find the successor of x: the smallest y in S such that y≥x. 15 |16 | design a data type so that all operations (except construction) take logarithmic time or better in the worst case. 17 | 18 | construct an array of n integers in order, link each element with the preceding one and the subsequent one, when remove x from, make the backward link of the element that x links forward to, denoted as f, point to what x links backward to, denoted as b, and make the forward link of b point to f. 19 | 20 | ## Analysis of Algorithms 21 | 1.3-SUM in quadratic time. Design an algorithm for the 3-SUM problem that takes time proportional to n^2 in the worst case. You may assume that you can sort the n integers in time proportional to n^2 or better. 22 | 23 | Here's a method which makes no use of external storage(like hash table).first sort the array, let the sum be S, for each element i in the array, let W=S-i, start from the begin index and end index of array, if sum of two elements > W, decrease end index by 1, else increase begin index by 1, stop when the sum =W 24 | 25 | 2.Search in a bitonic array. An array is bitonic if it is comprised of an increasing sequence of integers followed immediately by a decreasing sequence of integers. Write a program that, given a bitonic array of n distinct integer values, determines whether a given integer is in the array. 26 |
27 | Standard version: Use ∼3lgn compares in the worst case. 28 | Signing bonus: Use ∼2lgn compares in the worst case (and prove that no algorithm can guarantee to perform fewer than ∼2lgn compares in the worst case). 29 |30 | 31 | Standard version:one binary search to find the max, and two binary searches to search in two parts. 32 | bonus: use a modified binary search, let i denote the element to find 33 |
34 | if i < pivot, 35 | if left bound of interval < i, 36 | search in left subarray 37 | if right bound of interval < i, 38 | search in right subarray 39 | else if i > pivot, 40 | if pivot is in increasing trend, 41 | search in right subarray 42 | else, 43 | search in left subarray 44 |45 | proof:determining whether a given integer is in the array is equivalent to finding the index of a given integer i or return -1, for an input array of n integers, each integer can have n different indices (meaning situated in different positions), so the algorithm must have n * n different possible executions paths for all inputs, by resorting to the comparison-based decision tree, we know the algorithm should perform log(n*n) compares in the worst case. 46 | 47 | 3.Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg breaks if it is dropped from floor T or higher and does not break otherwise. Your goal is to devise a strategy to determine the value of T given the following limitations on the number of eggs and tosses: 48 |
49 | Version 0: 1 egg, ≤T tosses. 50 | Version 1: ∼1lgn eggs and ∼1lgn tosses. 51 | Version 2: ∼lgT eggs and ∼2lgT tosses. 52 | Version 3: 2 eggs and55 | 56 | https://math.stackexchange.com/questions/835582/egg-drop-problem 57 | -------------------------------------------------------------------------------- /part2/week3.md: -------------------------------------------------------------------------------- 1 | ## Maximum Flow 2 | 1. Fattest path. Given an edge-weighted digraph and two vertices ss and tt, design an ElogE algorithm to find a fattest path from s to t. The bottleneck capacity of a path is the minimum weight of an edge on the path. A fattest path is a path such that no other path has a higher bottleneck capacity. 3 | 4 | sort all edges in descending order, begin with a graph with only s and t, add one edge at a time to the graph successively, check whether s and t are connected, repeat until connected. As for how to check connectivity:using quick union 5 | 6 | 2. Perfect matchings in k-regular bipartite graphs. Suppose that there are n men and n women at a dance and that each man knows exactly k women and each woman knows exactly k men (and relationships are mutual). Show that it is always possible to arrange a dance so that each man and woman are matched with someone they know. 7 | 8 | build a graph with source s connected to n men, which is connected the n women they know respectively, all the women are connected to t, and compute the maxflow as shown in the job seeker example in lecture. For rigorous proof, refer to:https://math.stackexchange.com/questions/1805181/prove-that-a-k-regular-bipartite-graph-has-a-perfect-matching/1805195 9 | 10 | 3. Maximum weight closure problem. A subset of vertices S in a digraph is closed if there are no edges pointing from S to a vertex outside S. Given a digraph with weights (positive or negative) on the vertices, find a closed subset of vertices of maximum total weight. 11 | 12 | https://en.wikipedia.org/wiki/Closure_problem 13 | elaboration: 14 | As Picard (1976) showed, a maximum-weight closure may be obtained from G by solving a maximum flow problem on a graph H constructed from G by adding to it two additional vertices s and t. For each vertex v with positive weight in G, the augmented graph H contains an edge from s to v with capacity equal to the weight of v, and for each vertex v with negative weight in G, the augmented graph H contains an edge from v to t whose capacity is the negation of the weight of v. All of the edges in G are given infinite capacity in H. 15 | A minimum cut separating s from t in this graph cannot have any edges of G passing in the forward direction across the cut: a cut with such an edge would have infinite capacity and would not be minimum. Therefore, the set of vertices on the same side of the cut as s automatically forms a closure C. [**because it does not have an edge point to other vertices outside the set, whose capacity is infinity**] The capacity of the cut equals the weight of all positive-weight vertices minus the weight of the vertices in C [**why? as we know, the capacity of a cut(A,B) equals the sum of the flows on its edges from A to B minus the sum of the flows on its edges from from B to A, in this situtation, the edges that cross the cut can only be the subset of edges pointed from s or pointed to t, since other edges have weight of infinity, for every positive-weight vertex v, it is connected to s with some edge e by definition, if v is not at the same side of s,e crosses the cut, by maxflow property "A = set of vertices connected to s by an undirected path with no full forward or empty backward edges", e is full forward edge, it should contribute to the capacity of the cut, else v is at the same side of s, the edge (s,v) will not cross the cut and therefore does not contribute to the capacity, for every negative-weight vertex v, it is connected to t, similarly as above, if it is at the same side of s, the edge e from v to t is full forward and will contribute to the capacity of cut, else is unconcerned, therefore the mincut would be sum of the weights of positive-weight vertices at the side of t and the negative weights of negative-weight vertices at the side of s**] , which is minimized when the weight of C is maximized. By the max-flow min-cut theorem, a minimum cut, and the optimal closure derived from it, can be found by solving a maximum flow problem. 16 | 17 | ## Radix Sorts 18 | 1. 2-sum. Given an array a of n 64-bit integers and a target value T, determine whether there are two distinct integers i and j such that a_i + a_j =T. Your algorithm should run in linear time in the worst case. 19 | 20 | radix sort the array, and begin with the first index i and last index j, if ai+ajtosses. 53 | Version 4: 2 eggs and
tosses for some fixed constant c 54 |