2018S1

Problem 1

Consider the problem of finding the shortest paths in a weighted directed graph using Dijkstra’s algorithm. Denote the set of vertices as , the number of vertices as , the set of edges as , and the number of edges as .

Answer the following questions.

  1. Depict an example input data (with ) for which Dijkstra’s algorithm does not correctly find the shortest paths.

  2. Below is a pseudo-code of the algorithm that computes the length of the shortest path from the start node s to each node v. Answer code to fill in the blank .

Dijkstra( graph G = (V,E), start node s, length d(u,v) of each edge (u,v) ) {
    c = an empty array;    Q = an empty set;
    for ( v ∈ V )
        c[v] = ∞;
    c[s] = 0;
    for ( v ∈ V )
        add v to Q;
    while ( Q ≠ ∅ ) {
        v = a vertex v ∈ Q that minimizes c[v];   ]
        remove v from Q;                          ] . . . . (i)
        for ( u ∈ {destinations of edges outgoing from v} )
            [    a    ]                           ] . . . . (ii)
    }
}
  1. Consider the following graph with as the start node. Show how the values stored in the array change at each iteration of the while statement when the above algorithm is applied to the graph.
graph LR
    S((S)) -->|9| E((E))
    S -->|3| B((B))
    A((A)) -->|2| E
    B -->|2| A
    S -->|6| A
  1. For each of the code fragments (i) and (ii) in the above pseudo-code, answer the total time spent in the code fragment during the whole run of the algorithm, using big notation. Here assume that it takes time to execute code fragment (i) once.

  2. One can reduce the computational complexity of the algorithm by using a priority queue (binary heap) as . In that case, for each of the code fragments (i) and (ii) in the above pseudo-code, answer the total time spent in the code fragment during the whole run of the refined algorithm, using big notation.


考虑使用 Dijkstra 算法在加权有向图中寻找最短路径的问题。将顶点集表示为 ,顶点数表示为 ,边集表示为 ,边数表示为

回答以下问题。

  1. 描绘一个输入数据示例(),使得 Dijkstra 算法无法正确找到最短路径。

  2. 下面是计算从起始节点 到每个节点 的最短路径长度 的算法伪代码。回答填写空白处 的代码。

Dijkstra( 图 G = (V,E), 起始节点 s, 每条边 (u,v) 的长度 d(u,v) ) {
    c = 空数组;    Q = 空集合;
    for ( v ∈ V )
        c[v] = ∞;
    c[s] = 0;
    for ( v ∈ V )
        将 v 加入 Q;
    while ( Q ≠ ∅ ) {
        v = Q 中使 c[v] 最小的顶点;               ]
        从 Q 中移除 v;                            ] . . . . (i)
        for ( u ∈ {v 的出边目标节点集} )
            [    a    ]                           ] . . . . (ii)
    }
}
  1. 考虑以下图,其中 S 为起始节点。展示当上述算法应用于该图时,数组 c 中存储的值在 while 语句的每次迭代中如何变化。
graph LR
    S((S)) -->|9| E((E))
    S -->|3| B((B))
    A((A)) -->|2| E
    B -->|2| A
    S -->|6| A
  1. 对于上述伪代码中的代码片段 (i) 和 (ii),使用大 表示法回答在算法整个运行过程中在每个代码片段中花费的总时间。这里假设执行代码片段 (i) 一次需要 时间。

  2. 可以通过使用优先队列(二叉堆)作为 来降低算法的计算复杂度。在这种情况下,对于上述伪代码中的代码片段 (i) 和 (ii),使用大 表示法回答在改进后的算法整个运行过程中在每个代码片段中花费的总时间。


Problem 2

For a context-free grammar , we write for the language generated by . For a word , we write for its length. We write for an empty word. Answer the following questions.

(1) Let be the context-free grammar consisting of the production rules:

where is the start symbol. A derivation of in can be represented by the following syntax tree:

graph TB
	S --- A1[A] & A2[A]
	A1 --- a & A3[A] & b
	A3 --- c1[c]
	A2 --- c2[c]

Draw a syntax tree that represents a derivation of in .

(2) For the grammar in question (1), obtain words that satisfy all of the following three conditions. (i) , (ii) for every , and (iii) . Here, note that some of may be empty words .

(3) Prove that, for every context-free grammar , there exists an integer that satisfies the following property:

For every word , if , then there exist words that satisfy all of the following four conditions: (i) , (ii) for every , (iii) , and (iv) .

Here, you may assume that is in Chomsky normal form, i.e., each production rule is one of the forms , , and (where is a non-terminal symbol, and are non-terminal symbols other than , is a terminal symbol, and is the start symbol).

(4) Prove that there exists no context-free grammar that generates the language: . You may use the result of question (3) above.


对于上下文无关文法 ,我们用 表示 生成的语言。对于一个单词 ,我们用 表示它的长度。我们用 表示空单词。回答以下问题。

(1) 设 为包含以下产生式规则的上下文无关文法:

其中 是起始符号。 中的推导可以用以下语法树表示:

graph TB
	S --- A1[A] & A2[A]
	A1 --- a & A3[A] & b
	A3 --- c1[c]
	A2 --- c2[c]

绘制一个语法树表示 中的推导。

(2) 对于问题 (1) 中的文法 ,找出满足以下三个条件的单词 。 (i) ,(ii) 对于每个 ,以及 (iii) 。这里注意, 中有些可能是空单词

(3) 证明,对于每一个上下文无关文法 ,存在一个整数 ,使得以下性质成立:

对于每个单词 ,如果 ,那么存在单词 ,使得以下四个条件全部满足:(i) ,(ii) 对于每个 ,(iii) ,以及 (iv)

这里你可以假设 是乔姆斯基范式的,即每个产生式规则的形式为 ,以及 (其中 是非终结符号, 以外的非终结符号, 是终结符号,而 是起始符号)。

(4) 证明不存在生成语言 的上下文无关文法。你可以使用上述问题 (3) 的结果。


Problem 3

In this problem, we construct SRAM with 2-bit address width and 4-bit data width. All symbols for inputs and outputs represent 1-bit signals taking values of 0 or 1. We use memory cells with the following specifications. A memory cell has inputs I, W, and S and an output O, and stores a 1-bit value. At a falling edge of W, the value of I is stored in the memory cell. While S is 1, the stored value is output to O, and otherwise 0 is output to O. In your circuit designs, you can use AND, OR, NOT, and XOR gates in addition to the specified ones. Answer the following questions.

(1) Design a 2-bit decoder. It should have inputs and , and outputs , , , and . outputs 1 if , and outputs 0 otherwise.

(2) Let be the memory cell for the -th bit of data stored in address . Design a circuit to read the stored data, assuming that values are already stored in the memory cells. The circuit has inputs and , and outputs , , , and . If , then the value stored at is output as . You can use the decoder designed in question (1).

(3) Add the functionality of storing data to the circuit designed in question (2). Inputs , , , , and should be added. At a falling edge of , the value of is stored in the memory cell for . In this case, the other memory cells keep the stored values. Assume that the values of and are kept unchanged while is 1. You may answer only the differences from your answer to question (2).


在这个问题中,我们构建了一个 2 位地址宽度和 4 位数据宽度的 SRAM。所有输入和输出的符号表示 1 位信号,取值为 0 或 1。我们使用的存储单元具有以下规格。一个存储单元有输入 I、W 和 S 以及输出 O,并存储一个 1 位值。在 W 的下降沿时,I 的值存储在存储单元中。当 S 为 1 时,存储的值输出到 O,否则输出 0 到 O。在您的电路设计中,您可以使用 AND、OR、NOT 和 XOR 门以及指定的那些。回答以下问题。

(1) 设计一个 2 位解码器。它应该有输入 ,输出 。当 时, 输出 1,否则输出 0。

(2) 设 为地址为 存储的数据的第 位的存储单元。设计一个电路读取已存储在存储单元中的数据。该电路有输入 ,输出 。如果 ,则存储在 的值作为 输出。您可以使用问题 (1) 中设计的解码器。

(3) 为问题 (2) 中设计的电路增加存储数据的功能。添加输入 。在 的下降沿时, 的值存储在 的存储单元中,对于 。在这种情况下,其他存储单元保持存储的值。假设当 为 1 时, 的值保持不变。您可以仅回答与问题 (2) 的答案不同之处。