2020S1
Problem 1
Let be a finite alphabet (i.e., a finite set of letters), and be the empty sequence. We define the shuffle of two words as follows.
- For every ,
- For every and ,
Furthermore, for two languages , their shuffle is defined by:
For example, we have:
Answer the following questions.
-
Compute .
-
Suppose that deterministic finite automata and accept languages and , respectively. Here, and are respectively the set of states, the transition function, the initial state, and the set of final states of (i ). You may assume that the transition functions (i ) are total functions. Give a non-deterministic automaton that accepts .
-
Prove the correctness of your answer for question (2) above.
-
Let and . Prove that is not a context-free language. Here, you may use the pumping lemma for context-free languages.
设 为一个有限字母表(即,有限的字母集合), 为空序列。我们定义两个单词 的 shuffle 如下。
- 对于每个 ,
- 对于每个 和 ,
此外,对于两个语言 , 它们的 shuffle 定义为:
例如,我们有:
回答以下问题。
-
计算 。
-
假设确定有限自动机 和 分别接受语言 和 。这里, 和 分别是 的状态集、转移函数、初始状态和终止状态集(i )。你可以假设转移函数 (i ) 是全函数。给出一个接受 的非确定性自动机。
-
证明你在上述问题 (2) 中的答案的正确性。
-
设 和 。证明 不是上下文无关语言。这里,你可以使用上下文无关语言的抽气引理。
Problem 2
The following C code models the behavior of each philosopher in the dining philosophers problem.
There are five threads running concurrently on a multiprocessor system, each of which executes the philosopher
function. The argument is the index of each thread, where . In the philosopher
function, the eat
and think
functions are executed in turn repeatedly, while the pickup
and putdown
functions are used for synchronization between threads, respectively, before and after the execution of the eat
function so that two philosophers sitting side by side (namely, the -th and the -th threads for each ) cannot simultaneously execute the eat
function. Now consider the problem of implementing the pickup
and putdown
functions, using binary semaphores. Here, the P and V operations on a binary semaphore X are respectively expressed as wait(X)
and signal(X)
in C code, and the counter value of each binary semaphore is initialized to 1. Also, assume that the eat
and think
functions never cause a side effect that may influence the outside of each function.
Answer the following questions.
- For each , let be a binary semaphore. A deadlock may occur when the following
pickup
andputdown
functions are used. Describe how such a deadlock may occur.
- For each , let be a binary semaphore, and
state[i]
be a shared variable that represents the state of the -th thread. Also, letmutex
be a binary semaphore that is used to achieve a mutual exclusion on all the threads. In order for at least one thread to be able to execute theeat
andthink
functions repeatedly without a deadlock, thepickup
andputdown
functions are redefined as follows. The void-typetest
function setsstate[i]
toeating
and calls thesignal
function for , if a certain condition is satisfied. Describe thetest
function using C code. Note that you need not consider starvation of each thread. Also, assume that the initial value ofstate[i]
isthinking
.
- Regarding the C code in question (2), answer whether or not a thread may suffer from starvation, assuming that any enabled thread is eventually scheduled. If your answer is “yes”, describe how the starvation occurs and briefly explain how to modify the code to avoid the starvation. If your answer is “no”, then explain the reason.
以下 C 代码模拟了在哲学家进餐问题中每个哲学家的行为。
有五个线程在多处理器系统上并发运行,每个线程都执行 philosopher
函数。参数 是每个线程的索引,其中 。在 philosopher
函数中,eat
和 think
函数交替执行,而 pickup
和 putdown
函数分别用于在线程之间进行同步,以便在 eat
函数执行前后,使得并排的两个哲学家(即,对于每个 的 -th 和 -th 线程)不能同时执行 eat
函数。现在考虑使用二进制信号量实现 pickup
和 putdown
函数。这里,二进制信号量 X 的 P 和 V 操作分别在 C 代码中表示为 wait(X)
和 signal(X)
,每个二进制信号量的计数值初始化为 1。同样,假设 eat
和 think
函数永远不会产生可能影响每个函数外部的副作用。
回答以下问题。
- 对于每个 ,让 为二进制信号量。当使用以下
pickup
和putdown
函数时,可能会发生死锁。描述死锁可能如何发生。
- 对于每个 ,让 为二进制信号量,
state[i]
为表示第 个线程状态的共享变量。同时,让mutex
为一个二进制信号量,用于在所有线程上实现互斥。为了使至少一个线程能够反复执行eat
和think
函数而不出现死锁,pickup
和putdown
函数重新定义如下。test
函数设置state[i]
为eating
并在满足特定条件时调用signal
函数对 。使用 C 代码描述test
函数。注意,你不需要考虑每个线程的饥饿情况。同样,假设state[i]
的初始值是thinking
。
- 关于问题(2)中的 C 代码,回答线程是否可能遭遇饥饿,假设任何启用的线程最终都会被调度。如果你的答案是 “yes”,请描述饥饿是如何发生的,并简要解释如何修改代码以避免饥饿。如果你的答案是 “no”,请解释原因。
Problem 3
In this problem, the length of a string is written , and the -th character of is written , where the first character is . The string obtained by removing the first characters from is written . We assume in and . For example, if , then and . The set of characters consists of characters, where is an integer constant no less than 2, and for each character a distinct positive integer is defined. Suppose that the computation of for given and , and that of for given , take time. Also suppose that each of integer addition, multiplication and remainder takes time, and that overflow will never occur in integer operations.
We consider the following problem FIND: For given strings and , find the first position at which matches . In other words, is the least non-negative integer that satisfies
In case there is no such , we define . In the following, we assume .
For strings and with , let function return 1 if the first characters of equal , and return 0 otherwise. Suppose that the time complexity of is . The following algorithm solves the problem FIND:
Answer the following questions.
- Express the order of the worst-case time complexity of algorithm in terms of and .
In the following, the hash value of the first characters of string is defined by
where and are positive integer constants, and is assumed.
-
Assume that holds, and that and have been precomputed. Show an algorithm or an expression to compute in time.
-
Give an algorithm that finds the least non-negative integer that satisfies (but answers if no such exists) in time . Also, answer in what condition the algorithm outputs a value which is not the solution of problem FIND.
-
Give an algorithm that satisfies all of the following conditions: (a) it always answers the solution of problem FIND, (b) it searches for the answer by using hash and function , and (c) if we assume that the number of integers that satisfy for given and is independently of and , then the algorithm runs in time . In addition, show in what condition the time complexity of the algorithm is larger than . Also, answer the order of the worst-case time complexity of the algorithm in terms of and .
在这个问题中,字符串 的长度写作 ,并且 的第 个字符写作 ,其中第一个字符是 。通过去除 的前 个字符得到的字符串写作 。我们假设 在 和 中。例如,如果 ,则 并且 。字符集由 个字符组成,其中 是一个整数常数,不小于 2,并且对于每个字符 定义了一个不超过 的不同正整数 。假设给定 和 的 的计算和给定 的 的计算时间都是 。还假设每个整数加法、乘法和取模操作的时间都是 ,并且整数运算中不会出现溢出。
我们考虑以下问题 FIND:对于给定的字符串 和 ,找到第一个位置 ,使得 匹配 。换句话说, 是满足以下条件的最小非负整数:
如果没有这样的 ,我们定义 。在下面的内容中,我们假设 。
对于字符串 和 ,其中 ,让函数 返回 1,如果 的前 个字符等于 ,否则返回 0。假设 的时间复杂度是 。以下算法 解决了问题 FIND:
回答以下问题。
- 用 和 表示算法 的最坏情况下的时间复杂度。
在下文中,字符串 的前 个字符的哈希值 定义为
其中 和 是正整数常数,并且假设 。
-
假设 成立,并且 和 已经被预计算。展示一个算法或表达式,用于在 时间内计算 。
-
提出一个算法 ,它能找到满足 的最小非负整数 (但如果没有这样的 存在,则返回 ),时间复杂度为 。还要回答在什么情况下算法 输出的值不是问题 FIND 的解。
-
提出一个算法 ,满足以下所有条件:(a) 它总是能解答问题 FIND,(b) 它通过使用哈希 和函数 来搜索答案,(c) 如果我们假设满足 的整数 的数量对于给定的 和 是 而独立于 和 ,则算法 以时间
运行。此外,说明在什么情况下算法 的时间复杂度大于 。还要回答算法 的最坏情况时间复杂度的阶数,以 和 表示。