如果您想了解JavaMaps/DataStructures-如何让Value查看上一个Key的值,并下拉任何未更改的值?的相关知识,那么本文是一篇不可错过的文章,我们将为您提供关于811312ADat
如果您想了解Java Maps / Data Structures-如何让Value查看上一个Key的值,并下拉任何未更改的值?的相关知识,那么本文是一篇不可错过的文章,我们将为您提供关于811312A Data Structures、A Fast Wait-Free Simulation for Lock-Free Data Structures、a place you can learn algorithms and data structures(算法和数据结构) in swift、Algorithms & Data Structures的有价值的信息。
本文目录一览:- Java Maps / Data Structures-如何让Value查看上一个Key的值,并下拉任何未更改的值?
- 811312A Data Structures
- A Fast Wait-Free Simulation for Lock-Free Data Structures
- a place you can learn algorithms and data structures(算法和数据结构) in swift
- Algorithms & Data Structures
Java Maps / Data Structures-如何让Value查看上一个Key的值,并下拉任何未更改的值?
如何解决Java Maps / Data Structures-如何让Value查看上一个Key的值,并下拉任何未更改的值??
我正在使用Java中的Map解决问题,但是我没有看到如何将某个键的值与前一个键的值进行比较,然后将其拖到未更新的地方。
在下表中,我们看到涉及从1/1/2020到1/30/2020发生的4家公司的贷款掉期。 总而言之,无论互换如何,参与率总共需要达到100家公司的总和,例如:
- 交换日期之前:公司1(10)+公司2(60)+公司3(10)+ 公司4(20)= 100?
- 1/15/2020交换日期:公司1(10)+公司 2(30)+公司3(40)+公司4(20)= 100?
- 2020年1月21日交换日期:公司1(10)+公司2(50)+公司3(20)+公司4 (20)= 100?
- 2020年1月30日交换日期:公司1(0)+公司2(50)+ 公司3(20)+公司4(30)= 100?
那是我代码的目标。这是我目前的思维过程,但是如果由于交换而没有更改它们,我将停留在如何从先前的键值对中拉出值的问题上,尤其是如何用红色完成部分。
任何见识都会受到赞赏。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
811312A Data Structures
811312A Data Structures and Algorithms 2020-2021 Assignment, Instructions
This document describes both general and specific requirements for the assignment of the course
Data Structures and Algorithms.
- General Instructions
Everyone shall return one assignment implemented in C. Return the solution to course''s Moodle
workspace. The returned work will be graded as approved/disproved. If not approvable, a returned
solution can be revised one time, according to inspector''s advice.
The assignment is an individual work. You are not allowed to copy code from others.
However, the course materials, for instance exercise solutions and example programs can be used
freely. When utilizing already made solutions, referencing practices need to be followed and
explanation of how it has been modified. The solution must be provided with the standard features
of C language. The use of extra code libraries is not allowed.
The solution shall contain - program code and
- a report
in one zipped file.
The report must be written in English and it shall contain - description of the solution and
- analysis of the solution program.
Analysis contains both analysis of the solution algorithm and measurements of program
running times. Based on this, estimates for the performance of the solution algorithm should
be provided. The code should be commented to improve its readability, but no other
documentation is required. The following description of the assignment describes the contents of
the report in more detail.
The assignment for this instance of the course shall be returned at latest 9.4.2021 23.59 - Description of the Assignment
In this assignment, you shall find the 100 most frequently occurring words from a large text
file. The program shall be implemented in C language. A continuous string of characters a..z and
A..Z, with possible apostrophes ’, is considered a word. Words with uppercase and lowercase
letters are considered equal. For example, in the text
Herman Melville’s book Moby Dick starts, as we all know, with the sentence ”Call me Ishmael”.
the words are herman, melville’s, book, moby, dick, starts, as, we, all, know, with, the, sentence,
call, me, and ishmael
The name of the input text file shall be given as an input from the user. The program shall print the - most frequently occurring words and their frequencies. The words are printed in descending
order according to their frequencies.
Because the file can be very large, you need a suitable data structure to store words and their
frequencies. This can be, for instance, a hash table or a binary search tree. You can find the most
frequent words by sorting the structure with some fast algorithm.
There are examples of input files and corresponding outputs in Moodle. All the files are texts
written in English. You should use these to test your program. The size of the input is
measured as the number of words in the text file. Measure the running times of your program
with given test input files and make an estimate, how many words there can be in a file that you can
process in 15 seconds. Based on this, estimate the maximum size (in megabytes) of files that your
program can process in the mentioned time, when we know that the average word length in English
is 5.1 letters.
In addition to program code, you shall return a report that describes - the solution, and
- analysis of program’s performance as mentioned above.
Analysis contains thus the measurements of program’s running times and the mentioned estimates
for number of words and file size. The code shall be commented to improve readability, but no
other documentation is required. The report shall naturally contain student’s name and student
number. If you wish, you can also give feedback in your report.
Remember to zip the files before returning.
WX:codehelp
A Fast Wait-Free Simulation for Lock-Free Data Structures
基于CAS(compare-and-swap)操作的无锁算法,几乎都可以采用《无锁算法的无等待模拟》来转化为无等待算法,从而获得更强的progress。
相对于《无锁算法的无等待模拟》中的算法所采用的类似无等待队列,本文换成一种基于getAndAdd+CAS的无等待队列。
本文我用自己的方式来介绍下:
- 如何从一个基于CAS的正确(一致性)的无锁算法转化为无等待算法,针对CAS操作建模(无等待算法)
- 同时基于环形的线程专属数据环,由于依赖于具体的数据偏移,降低了公平性。所以使用一种更快速的类似wait-free队列,从而获得更好的公平性。
几乎所有的无锁算法(先不考虑垃圾回收),对于一个线程而言,都是一个如下的过程:
要么成功(本操作make progress)、要么失败(其他线程的操作make progress)从而本操作需要更新数据(preCasOp),再次重复直到成功。并且最后要执行必要的postCasOp。
那么,对于一个value,我们将它置于一个record里:
struct Record {
void* value;
};
struct Record* Head;
算法的关键在于,对一个head的原子改变:
lock-free-method(void* value) {
for(;;) {
before;
Record* head = Head;
Record* myRecord = Generate(head, value);
if(CAS(&Head, head, myRecord)) {
after;
return;
}
}
}
这里的问题在于,对于一个操作lock-free-method(val),它在这一行CAS(&Head, head, myRecord),我们需要在这一行取得成功才能算本操作make progress。但是我们无法确定什么时候能够取得成功。在操作系统内核的调度算法越来越好的情况下,我们很可能绝大多数情况下不会感受到某次操作的延迟。但是有没有办法能够确保,本操作能够在有限步之后完成。这种算法性质就叫做wait-freedom。
我们转化成的wait-free算法具有如下结构:
- 每个线程都先在fast-path中尝试MAX-fast次,假如某次成功则返回,假如成功的次数达到ENOUTH-fast(per thread),那么就通过shareMemory协助处于slow-path中的操作一次。
- 假如fast-path中尝试MAX-fast次仍未成功,则构造自己的请求信息放到shareMemroy,接着进入slow-path。
- 进入slow-path的线程,不断地尝试,最终会在有限次尝试内,自己完成操作,或者由其他线程帮助完成。
-
协助过程,可以抽象出取得一致性的三个接口:
HELP(handle) HELP_TRANSFER(head) HELP_TRANSFER_VALUE(head, handle)
对于如上这个具体操作,我们可以做如下操作:
首先,给Record结构增加一个Handle_t数据类型:
typedef struct Record {
void* value;
Handle_t* handle;
} Record;
typedef struct Handle_t {
void* value;
struct Wrapper* wrapper;
} Handle_t;
typedef struct Wrapper {
bool isFinish;
Record* record;
} Wrapper;
我们把lock-free-method进行改造:
- 原来的lock-free-method作为fast-path,给定两个阈值,MAX1:让它执行够次数就退出。构造原来的请求,放入共享内存中(可以是并发队列)。 MAX2: 在fast-path中执行成功的次数,从而主动help slow-path中的请求
- CAS操作不再像原来一样在在value的值上改变,而是根据record->handle是否为空来继续操作:为空则尝试自己的请求,不为空则尝试帮助handle对应的请求make progress。(同理,fast-path也进行修改。)
- handle中会有个指向Record的指针,以及代表本操作是否完成的bit和version。
所以,slow-path通过引入两个中间状态(states),把本来一个CAS操作变为了三个CAS操作:
- STEP 1: (exclusive)线程A尝试把原来的Record,指向请求所对应的Handle,make progress(state 1,该Record被某个线程锚定).
- STEP 2: (consisent)某个线程B(B为任意线程)从Record中取出Handle,并且将该Handle指向这个Record,同时设置finish bit,从而告知该次请求完成,make progress.(state 2,说明此事操作已经完成)
- STEP 3: 线程A从自己的handle中获知操作已完成(finish bit,stete 2导致的),并且获取自己锚定的Record,更新Record的值,从而完成收尾工作,并且返回自己的操作结果(state 3,此时Record的值又回到了原来的状态,与lock-free中的结果一致)
伪代码如下:
wait-free-method(void* value) {
// fast-path
for(int i = MAX1; i--;) {
before;
Record* head = Head;
if(head->handle == NULL) {
Record* myRecord = NewValueRecord(head, value);
if(CAS(&Head, head, myRecord)) {
after;
if((--meHandle->fastTimes) == 0) {
meHandle->fastTimes = MAX2;
Handle_t* handle = peekOneHandle(shareMemory);
if(handle != NULL)
HELP(handle);
}
return;
}
} else {
// help the Record head
HELP_TRANSFER(head);
}
}
// construct myself request into shareMemory
meHandle->value = value;
meHandle->wrapper->isFinish= false;
meHandle->wrapper->record = NULL;
putMeHandleIn(shareMemory, meHandle);
// slow-path
HELP(meHandle);
while(there is handle in shareMemory before meHandle)
HELP(handle);
}
HELP(Handle_t* handle) {
before;
Record* head = Head;
while(!handle->wrapper->isFinish) {
// Record* head = Head; 这一行应该在测试isFinish之前
if(head->handle == NULL) {
Record* myRecord = NewHandleRecord(head, handle);
if(!CAS(&Head, head, myRecord)) {
head = Head;
continue;
}
head = myRecord;
}
// help the Record head
HELP_TRANSFER(head);
before;
head = Head;
}
head = handle->wrapper->record;
HELP_TRANSFER_VALUE(head, handle);
}
HELP_TRANSFER(Record* head) {
Handle_t* handle = head->handle;
finishHandle(shareMemory, handle);
Wrapper* wrapper = handle->wrapper;
if(!wrapper->isFinish) {
//transfer to state 2.
Wrapper* newWrapper = NewWrapper(head, true);
CAS(&handle->wrapper, wrapper, newWrapper);
}
HELP_TRANSFER_VALUE(head, handle);
}
HELP_TRANSFER_VALUE(Record* head, Handle_t* handle) {
if(Head == head) {
//transfer to state 3.
Record* myRecord = NewValueRecord(head, handle->value);
if(CAS(&Head, head, myRecord))
after;
}
}
简化一下,如下伪代码:
wait-free-method(void* value) {
// fast-path
for(int i = MAX1; i--;) {
.......
if(!there is a requestHandle in Record) {
if(try myRequest) {
if(successTimes is enough) {
helpOneRecordInShareMemory();
}
return;
}
} else {
helpTheRecord();
}
}
// construct my request handle(value/Wrapper(Record*:NULL, isFinish:false));
construct myself request into shareMemory
// slow-path
while(my request is not finish) {
if(!there is a request in Record) {
if(!try myRequest)
continue;
assert(there is a request in Record: myRequest);
}
helpTheRecordFinish();
}
helpTheRecordFinishValue();
}
这里面的shareMemory,强烈建议改造链接描述中的队列来使用,这里需要的是个类似队列的数据结构。
从而达到对于shareMemory的操作也是无等待的,从而保证了整个算法最强的wait-freedom。
a place you can learn algorithms and data structures(算法和数据结构) in swift
https://github.com/raywenderlich/swift-algorithm-clubAlgorithms & Data Structures
Algorithms & Data Structures 2020/21
Coursework
Konrad Dabrowski & Matthew Johnson
Hand in by 15 January 2021 at 2pm on DUO.
Attempt all questions. Partial credit for incomplete solutions may be given. In written answers,
try to be as precise and concise as possible. Do however not just give us the what but also the
how or why.
The following instructions on submission are important. You need to submit a number of files
and we automate their downloading and some of the marking. If you do not make the submission
correctly some of your work might not be looked at and you could miss out on marks.
You should create a folder called ADS that contains the following files (it is important to get each
name correct):
• q1.ipynb containing the function hash
• q2.pdf containing the written answer to Question 2 and q2.ipynb containing the functions
floodfill stack and floodfill queue
• q3.ipynb containing the functions make palindrome, balanced code and targetsum
• q456.pdf containing your written answers to Questions 4, 5 and 6.
• q6.ipynb containing the functions InsertionSort, Merge3Way and HybridSort
You should not add other files or organise in subfolders. You should create ADS.zip and submit
this single file. Your written answers can be typed or handwritten, but in the latter case it is your
responsibility to make sure your handwriting is clear and easily readable. We will use Python 3
to test your submissions. Please remember that you should not share your work or make it
available where others can find it as this can facilitate plagiarism and you can be penalised. This
requirement applies until the assessment process is completed which does not happen until the
exam board meets in June 2021.
1
- This question requires you to create a python function to add keys to a hash table. See
the detailed instructions in ADSAssignmentQ1.ipynb. Your submission for this question
should be a single file q1.ipynb containing a function hash. It should not depend on anything
other than the provided class Hash Table() which you do not need to include in
your submission. [15 marks] - This question requires you to create two python functions that implement a floodfill algorithm
using stack and queues. See the detailed instructions in ADSAssignmentQ2.ipynb.
Your submission for this question should be a file q2.ipynb containing functions
floodfill stack and floodfill queue, and a document q2.pdf. The function should
not depend on anything other than the provided code which you do not need to include in
your submission. [15 marks] - This question requires you to create three python functions that implement recursive algorithms
to solve the three problems described briefly below. See the detailed instructions in
ADSAssignmentQ3.ipynb. Your submission for this question should be a file q3.ipynb
containing functions make palindrome, balanced code and targetsum.
(a) Recall that a palindrome is a string such as abba or radar that reads the same forwards
as backwards. The problem MAKEPALINDROME has as input a string and a nonnegative
integer k and returns true or false according to whether or not the string can
be turned into a palindrome by deleting at most k characters. For example, for inputs
(banana, 1), (apple, 3), (pear, 3), (broccoli, 4) and (asparagus, 4) it returns true because
the palindromes anana, pp, p, occo and saras can be obtained by deleting, respectively,
1, 3, 3, 4 and 4 characters, but for (kiwi, 0) and (spinach, 5) it returns false. [5 marks]
(b) The balanced code of size k is the collection of all binary strings of length 2k such that
for each string the number of zeros in the first k bits is the same as the number of zeros
in the second k bits.
For example the balanced code of size 1 is
00, 11,
the balanced code of size 2 is
0000, 0101, 0110, 1001, 1010, 1111,
and the balanced code of size 3 is
000000, 001001, 001010, 001100, 010001, 010010, 010100, 011011, 011101, 011110
100001, 100010, 100100, 101011, 101101, 101110, 110011, 110101, 110110, 111111.
[5 marks]
(c) The problem TARGETSUM has as input a collection of positive integers S and a further
integer t and requires that numbers from S are selected whose sum t. For example, if
S = 1, 2, 3 and t = 5
then the solution is 2, 3.
And if
S = 1, 4, 5, 8, 12, 16, 17, 20 and t = 23
then the solution is 1,5,17.
Note that the order of the numbers in the solution is not important and that if there is
more than one possible solution, only one needs to be found. [10 marks]
2
Provide your written answers for questions 4, 5 and 6 in a single file q456.pdf. (Note that
python files are also needed for Question 6.) - Prove or disprove each of the following statements. We will assume that x > 0, and all
functions are asymptotically positive. That is, for some constant k, f(x) > 0 for all x ≥ k.
You will get 1 mark for correctly identifying whether the statement is True or False, and 1
mark for a correct argument.
(a) 4x
4
is O(2x - 7x + 3). [2 marks]
(b) If f(x) is O(r(x)) and g(x) is O(s(x)) then f(x)/g(x) is O(r(x)/s(x)). [2 marks]
(c) 5x
- 7x + 3). [2 marks]
- 2x + 1 is ω(x
2
log x). [2 marks]
(d) 3
2x = Θ(3
x
). [2 marks]
(e) 4x
- 2x + 1 is ω(x
- 6x
- 1 is o(x
3
log x). [2 marks]
- 1 is o(x
- For each of the following recurrences, give an expression for the runtime T(n) if the recurrence
can be solved with the Master Theorem. Otherwise state why the Master Theorem
cannot be applied. You should justify your answers.
(a) T(n) = 25T(n/5) + n
√
n. [3 marks]
(b) T(n) = 16T(n/2) + n
5
. [3 marks]
(c) T(n) = nT(n/3) + n
3
log n. [3 marks]
(d) T(n) = 32T(n/2) − n log n. [3 marks]
(e) T(n) = 3T(n/3) + n log n. [3 marks] - This question requires you to create three python functions that together implement a recursive
sorting algorithm. See the detailed instructions in ADSAssignmentQ6.ipynb.
Your submission for this question should be a file q6.ipynb containing functions
InsertionSort, Merge3Way and HybridSort.
(a) Consider the MergeSort algorithm we have seen in lectures, but suppose we want to
change it in three ways, by changing the order of sorting (from largest to smallest
rather than smallest to largest), changing the base case (use InsertionSort for inputs of
length less than 4) and the number of sub-lists it recurses on (changing from 2 to 3).
Throughout this question, you may assume that no two elements in your list are equal.
• First, implement InsertionSort to sort elements of a list from largest to smallest.
This should work with any number of elements.
• Write a function Merge3Way that takes three lists as input, each sorted from largest
to smallest value and merges them into one list sorted from largest to smallest
value and returns it.
• Write a function HybridSort, which uses your Merge3Way and InsertionSort functions
to implement MergeSort recursing into three sub-lists rather than two. When
you recurse, the length of the three sub-lists must differ by at most 1. Instead of
recursively calling HybridSort until the list to be sorted has length 1, implement a
base case so that if HybridSort is called with fewer than four elements, then your
InsertionSort function is used instead. Your HybridSort function should work on
any length of list.
• You must at no time use any function for sorting that is not your own InsertionSort,
Merge3Way or HybridSort. [20 marks]
(b) What is the worst-case running time of this modified algorithm; find the best O(f(n))
that you can. What is the best-case running time of the algorithm; find the best
Ω(f(n)) that you can. Justify your answers. [5 marks]
WX:codehelp
今天关于Java Maps / Data Structures-如何让Value查看上一个Key的值,并下拉任何未更改的值?的讲解已经结束,谢谢您的阅读,如果想了解更多关于811312A Data Structures、A Fast Wait-Free Simulation for Lock-Free Data Structures、a place you can learn algorithms and data structures(算法和数据结构) in swift、Algorithms & Data Structures的相关知识,请在本站搜索。
本文标签: