algorithm

C++ Treiber Stack and atomic next pointers

为君一笑 提交于 2021-02-07 09:19:07
问题 The "Treiber Stack" is generally one of the simplest lock-free data structures, and so it is often used when teaching introductions to lock-free algorithms. I've seen many implementations of Treiber Stacks using C++ atomics. The algorithm itself is trivial, so the real challenge is handling all the other incidental details of lock-free data-structures, such as providing some way of performing safe memory reclamation, avoiding the ABA problem, and allocating nodes in a lock-free manner. This

Reverse Cartesian Product

空扰寡人 提交于 2021-02-07 09:16:28
问题 Given the data set below: a | b | c | d 1 | 3 | 7 | 11 1 | 5 | 7 | 11 1 | 3 | 8 | 11 1 | 5 | 8 | 11 1 | 6 | 8 | 11 Perform a reverse Cartesian product to get: a | b | c | d 1 | 3,5 | 7,8 | 11 1 | 6 | 8 | 11 I am currently working with scala, and my input/output data type is currently: ListBuffer[Array[Array[Int]]] I have come up with a solution (seen below), but feel it could be optimized. I am open to optimizations of my approach, and completely new approaches. Solutions in scala and c# are

Reverse Cartesian Product

我是研究僧i 提交于 2021-02-07 09:16:22
问题 Given the data set below: a | b | c | d 1 | 3 | 7 | 11 1 | 5 | 7 | 11 1 | 3 | 8 | 11 1 | 5 | 8 | 11 1 | 6 | 8 | 11 Perform a reverse Cartesian product to get: a | b | c | d 1 | 3,5 | 7,8 | 11 1 | 6 | 8 | 11 I am currently working with scala, and my input/output data type is currently: ListBuffer[Array[Array[Int]]] I have come up with a solution (seen below), but feel it could be optimized. I am open to optimizations of my approach, and completely new approaches. Solutions in scala and c# are

Reverse Cartesian Product

百般思念 提交于 2021-02-07 09:12:46
问题 Given the data set below: a | b | c | d 1 | 3 | 7 | 11 1 | 5 | 7 | 11 1 | 3 | 8 | 11 1 | 5 | 8 | 11 1 | 6 | 8 | 11 Perform a reverse Cartesian product to get: a | b | c | d 1 | 3,5 | 7,8 | 11 1 | 6 | 8 | 11 I am currently working with scala, and my input/output data type is currently: ListBuffer[Array[Array[Int]]] I have come up with a solution (seen below), but feel it could be optimized. I am open to optimizations of my approach, and completely new approaches. Solutions in scala and c# are

Ordering functions by Asymptotic Growth Rate

筅森魡賤 提交于 2021-02-07 09:00:41
问题 List the following functions in non-descending order of asymptotic growth rate. If two or more functions have the same asymptotic growth rate then group them together. g1(n) = n g2(n) = n^3 +4n g3(n) = 2n log(base 2) n g4(n) = 2^n g5(n) = 3 ^ (3 * log(base 3) n) g6(n) = 10^n I've been looking through several examples online but I have no idea how to do this, it just seems like a completely foreign concept to me. If anyone could help me, that would be much appreciated. how do I even calculate

Ordering functions by Asymptotic Growth Rate

亡梦爱人 提交于 2021-02-07 08:59:00
问题 List the following functions in non-descending order of asymptotic growth rate. If two or more functions have the same asymptotic growth rate then group them together. g1(n) = n g2(n) = n^3 +4n g3(n) = 2n log(base 2) n g4(n) = 2^n g5(n) = 3 ^ (3 * log(base 3) n) g6(n) = 10^n I've been looking through several examples online but I have no idea how to do this, it just seems like a completely foreign concept to me. If anyone could help me, that would be much appreciated. how do I even calculate

Specific Graph and need to more Creative solution

巧了我就是萌 提交于 2021-02-07 08:55:37
问题 Directed Graph (|V|=a, |E|=b) is given. each vertexes has specific weight. we want for each vertex (1..a) find a vertex with maximum weight that can be reachable from that vertex. Update 1: one nice answer is prepare by @Paul in O(b + a log a). but I search for O(a + b) algorithms, if any? Is there any different efficient or fastest any other ways for doing it? 回答1: Yes, it's possible to modify Tarjan's SCC algorithm to solve this problem in linear time. Tarjan's algorithm uses two node

Data structure with amortized O(1) delete and O(log n) search

萝らか妹 提交于 2021-02-07 08:29:29
问题 I need a data structure that supports two operations - delete and search. Now, the delete operation should run in amortized O(1) time, while search should run in O(log n) time. Search operation should work as follows: look for value specified and if it is here, return value itself. Otherwise, return the nearest greater value (return inorder successor). What could this data structure be? 回答1: It could be a pair of data structures: binary search tree, holding values hash table, holding pointers

Point of Maximum Overlap

为君一笑 提交于 2021-02-07 08:27:51
问题 Suppose that we wish to keep track of a point of maximum overlap in a set of intervals—a point that has the largest number of intervals in the database overlapping it. a. Show that there will always be a point of maximum overlap which is an endpoint of one of the segments. b. Design a data structure that efficiently supports the operations INTERVAL-INSERT, INTERVAL-DELETE, and FIND-POM, which returns a point of maximum overlap. (Hint: Keep a red-black tree of all the endpoints. Associate a

Generate all unique combinations of Items

扶醉桌前 提交于 2021-02-07 08:21:34
问题 I trying to generate all possible unique combination of items. Ex: item1, item2, item3 Combinations: item1+item2+item3 item1+item2 item1+item3 item2+item3 item1 item2 item3 I am unable to get an idea on how to solve this? for(int i=0;i<size;i++){ for(int j=i+1;j<size;j++){ System.out.println(list.item(i)+list.item(j)); } } The above code certainly works for all unique combination of two elements. But not for 3 element pair and more.. 回答1: If you have N items, count from 1 to 2^N-1. Each