Final Exam Solution - Test Paper Final Exam Solution - Test Paper
Final Exam Solution - Test Paper Final Exam Solution - Test Paper
Final Exam Solution - Test Paper Final Exam Solution - Test Paper
Final Examination
Semester 1 2017
Instructions:
2) 2FAOC is equivalent to
A. 195 084 in decimal
B. 001011111100011101100
C. Both A and B
D. None of the above
4) A page fault
A. Occurs when there is an error in a specific page.
B. Occurs when a program accesses a page of main memory.
C. Occurs when a program accesses a page not currently in main memory.
D. Occurs when a program accesses a page belonging to another program.
5) If an instruction takes 3 cycles for execution, then how many cycles are needed for executing 4
instructions of the same type in a sequence using a 3-stage pipeline? Assume that there are no
interrupts or exceptions while executing them.
A. 12 cycles
B. 6 cycles
C. 9 cycles
D. 4 cycles
6) Which of the following architecture has the fixed instruction length feature?
A. CISC
B. RISC
C. X86
D. None of the above
7) How do Direct Addressing Mode instructions compare with respect to the Indirect Addressing
Mode instructions?
A. Faster
B. Slower
C. No difference
D. None of the above
8) The first person who attempted to build a digital computer was _______________
A. Charles Babbage
B. Blaise Pascal
C. Baron Wilhelm
D. Alan Turing
10) When you transfer the record from a Big Endian system to a Little Endian system over the
network in order to get the original value, you must:
A. Reverse the byte within a word
B. Reverse the bytes in an integer
C. Reverse the characters in a word
D. There is no simple solution
x y z F(x,y,z)
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 0
1 0 1 1
1 1 0 1
1 1 1 0
a. Write the disjunctive normal form (DNF) for the above truth table. (2 marks)
b. Simplify the DNF form in part a) using Boolean algebra. (3 marks)
c. Draw the logic diagram for the simplified expression in part b). (2 marks)
d. Draw a multiplexor for the above truth table. (3 marks)
2. For the following three questions, assume that memory locations contain the values shown in
the following table:
Location Value
-------- -----
100 150
110 130
120 110
130 100
140 120
150 140
What value will be loaded into the accumulator if the following instructions are encountered? (3
marks)
a. Load immediate 100
b. Load direct 100
c. Load indirect 100
Answer:
1 mark each
a) 100
b) 150
c) 140
X = ((A-B)+((C-D)/E))*F
Answer:
a)
MOVE A,R0
SUB B,R0
MOVE C,R1
SUB D,R1
DIV E,R1
ADD R0R1
MUL F,R1
MOVE R1,X
PUSH A
PUSH B
SUB
PUSH C
PUSH D
SUB
PUSH E
DIV
ADD
PUSH F
MUL
POP X
Answer:
Answer:
0000 R1 R2 R3
…. 1 mark
1110 R1 R2 R3
1111 0000 r2 r3
& 1 mark
1111 1101 r2 r3
&. 1 mark
&& 1 mark
6. What is one major problem in Clocked SR latch and explain how a clocked D latch solves the
problem? ( 3 marks)
Answer:
An error (ambiguity) occurs in the SR latch when S and R inputs are both 1 (1.5 marks)
It prevents both inputs S and R being 1 at the same time by providing one input D and its
complement (1.5 marks)
7. List three different ways to achieve processor –level parallelism when designing computers.
(3 marks)
Answer:
Array computers
Multi processors
Multi computers
8. For the page references given below, answer the questions that follow.
A, B, C, D, B, E, C, G, D, A, G, D, B, E, C
a. How many page faults will occur if optimal page replacement algorithm is used? Show all
working. [2 marks]
b. How many page faults will occur if LRU page replacement algorithm is used? Show all
working. [2 marks]
c. How many page faults will occur if FIFO page replacement algorithm is used? Show all
working. [2 marks]
Answer:
9. Assume you have a superscalar CPU with in-order issue and in-order instructions that uses 8
registers (R0-R7). The usual rules include: up to two instructions can be issued in one cycle;
instructions have to complete in the order they are issued; an instruction attempting to write to
a register that is being read by any incomplete instruction cannot be issued until the incomplete
instruction completes; any instruction attempting to read a register that is being written to by
any incomplete instruction cannot be issued until the incomplete instruction retires; no new
instructions can be issued in a cycle when instructions are retiring; multiplication/division
instructions takes 3 cycles to complete while addition/subtraction instructions take only 2
cycles. Suppose you need to run the following four instructions:
1. R3 = R1 + R2
2. R1 = R4 * R5
3. R5 = R2 * R8
4. R6 = R1 + R4
a. How many cycles in total will the CPU take to complete the four instructions above. Use a
scoreboard as shown below to illustrate your answer. (4 marks)
b. How many cycles will the CPU take if out-of-order instruction issue and execution is allowed.
Use a scoreboard as shown above to illustrate your answer. (4 marks)
Answer:
a. 11 cycles (0.5 mark)
2
3 1 0.5
4 2 0.5
3 R5 = R2 * R8 -
5
6
7 2 0.5
8 3 0.5
4 R6 = R1 + R4 4
9
10
11 3 0.5
4
2 3 S2 = R2 * R8 3 1
4 R6 = R1 + R4 4
3 1 0.5
4 2 1
4
5 3 0.5
10. Suppose you are designing a computer from scratch and that your company’s budget allows a
very small amount of memory bandwidth. Which of the following characteristics would you
choose in the ISA and the microarchitecture? Explain your answer. (6 marks)
a. Variable length or fixed length instructions?
b. Complex instructions or simple instructions?
c. A large cache or a small cache?
Answer:
a. Variable length =>smaller code therefore less bandwidth to transfer instructions 2 marks
b. Complex instructions => smaller program because each instruction is more powerful
therefore less bandwidth to transfer instructions. 2 marks
c. Large cache => higher cache hit rate which will result in less need to access memory. 2
marks
11. Two processors with different ISA need performance evaluation. The first processor with ISA A
performs at the rate of 10 instructions per cycle where as the second processor with ISA B
performs at the rate of 2 instructions per cycle. The first processor has 500 MHz clock while the
second processor has 600 MHz clock.
a. What is the performance in MIPS of the processor implementing ISA A? (1 mark)
b. What is the performance in MIPS of the processor implementing ISA A? (1 mark)
c. Which is the higher performance processor? Explain your answer. (2 marks)
Answer:
a. 10 IPC X 500 MHz = 5000 MIPS 1 mark
b. 2 IPC X 600 MHz = 1200 MIPS 1 mark
c. Not enough information provided. We don’t know the number of instructions
executed on each processor, so we can’t compare performance. Processor A for
example may have very simple instructions . Processor B could be faster if its
instructions actually accomplish significantly more work than Processor A’s. The
MIPS metric does not take into account number of executed instructions, which also
affects the performance (i.e. execution time) of each processor. 2 marks
12. What would be the code word if you encode the data word 10011010 to allow one bit error
detection and correction using hamming code with even parity? (4 marks)
Answer:
_ _ 1 _ 0 0 1 _ 1 0 1 0 (0.5 mark)
13. The instruction execution cycle consists of six steps. The second step is <Increment the program
counter=. Explain the purpose for the second step? (2 marks)
Answer: Program counter points to the next instruction to execute. To make progress in
program execution.
14. A segmented address space uses paged virtual memory. Each virtual address has a 2 bit segment
number, a 2 bit page number and an 8 bit offset within the page. In the following page tables, a
<-= indicates that the page is not in memory, which has 8 page frames.
a. What is the lowest hexadecimal virtual address in segment 2 that will cause a page fault?
(2 marks)
b. Find the real memory address (in hexadecimal) if the virtual hexadecimal address is FAA.
(2 marks)
c. Find the virtual address (in decimal) if the real memory decimal address is 1000.
(2 marks)
a. 10 00 00000000 = 800
(1.5 marks) (0.5 mark)
15. Which value does this two’s complement binary number 11110100 represents? (2 marks)
Ans: 00001011
+ 1
---------------------
16. Suppose the hard disk above has 1024 cylinders, 8 tracks per cylinder, 32 sectors per track and
512 Bytes per sector. The maximum seek time is 450 msec, the time to move between adjacent
cylinders is 10 msec, the rotation time is 14. If the entire disk was full of data stored
consecutively, how much time would it take to read the entire disk if the read/write head is
already positioned on the first sector of the first track of the first cylinder of the disk? (3 marks)
Answer:
Total time = cylinder read + track switch + track switch wait + cylinder switch/wait
= (8*14)*1024 + (0) + (0) + (1023*14)
= 129010 msec
17. On computer 1, all instructions take 10 nsec to execute. On computer 2, they all take 5 nsec to
execute. Can you say for certain that computer 2 is faster? Explain your answer. (3 marks)
Answer:
You cannot say anything for sure. If computer 1 has a five-stage pipeline, it can issue up to 500 million
instructions/second. If computer 2 is not pipelined, it cannot do any better than 200 million
instructions/sec. Thus without more information, you cannot say which is faster.
18. A machine instruction is needed during execution. The instruction could be in an instruction
cache (with probability 0.65) or in memory (with probability 0.3) or on disk. The access times for
cache and disk are 50 ns and 6000 ns respectively. Assume each instruction source is checked
before a slower one to find the instruction. What minimum memory access time (in ns) will give
an expected access time of 800 ns? (3 marks)
Answer:
19. Why do you think John von Neumann’s original basic design of a computer, known as von
Neumann machine, is still the basis for nearly all digital computers? (2 marks)
Answer:
His machine has 5 basic parts (memory, control unti, ALU, input and output) which are still found and
are important components of a digital computer.
Extend the Wombat 3 accumulator based assembly language to the Wombat 4 addressing modes.
Addressing modes will be used only on STORE and LOAD machine instructions. There are three modes
that can be used: Direct, Indirect and Immediate. For example, LOAD IMM 122 will load the value 122
into the accumulator and STORE IND L2 will store the contents of the accumulator to address stored at
location L2.
Write a complete Wombat 3 assembly language main and subprogram. The main program tests the
subprogram: it reads in exactly one integer, checks that the integer is valid input, calls the subprogram
and writes out the result. The subprogram must compute the function: f(n) = 4 × n × f(n − 1)
where f(0) = 3. The program must be properly commented.
Answer:
What is AI?
- Cognitive psychology: the study of computations that make it possible to
perceive, reason and act. (Input Ñ> processing Ñ> output)
- AI: The study of how to build or program computers to enable them to do what
minds can do
Why AI in psychology?
- Psychology is one big inverse problem (too many ways to interpret something)
- Set of observations (behavior, psychophysiological measurements, EEG, fMRI)
- We then try to infer the processes producing such observations. Limited /
sometimes impossible to make
History of AI
- 90Õs Ôneural networksÕ, 70Õs Ôcomputer chessÕ; starts with a hype, followed by
disappointment, followed by lack of funding Ñ> AI winter
Philosophy of mind
- How does the physical brain give rise to the mental mind?
- RenŽ Decartes (1596-1650): dualism because the mind is not physical
- Materialists: All mental states are caused by (or identical to) physical states. If we
agree to this, it means copying a physical state means copying a mental state
(copying a brain copies someone consciousness).
1956 1974-1980Õs
Dartmouth AI winter
Conferences
1940Õs
- Warren McCulloch and Walter PittÕs 3 principles:
• Basic physiology
• Propositional logic
• TuringÕs theory of computation
- Any computable function can be computed by a network of neurons
(combinations of neurons and their connections can compute anything)
Weak AI
Turing test = human talks to 2 voices, one AI other human, determine which is which
- Non-sentient AI (chat bots)
- Imitation game: a machine is intelligent if we cant distinguish it from a human in
conversation
- How does a judge determine intelligence?
• Complex grammatical structures
• Realistic world knowledge (not too much, not too little)
Searle
- Collection of cells can lead to thought/action/consciousness
- Consciousness requires actual physical-chemical properties of human brains
- Only brains make minds
- Chinese room argument (1980):
• In a room containing only a large book and a door under which pieces of paper
can be passed
• Chinese people on the outside of the room can ask me questions by wiring
them down and passing pieces of paper under the door
• Large book contains every possible question + answer, so you can answer in
Chinese.
- Ñ> intelligence? no, looking up question (input) Ñ> processing it (looking up
the right answer) Ñ> writing it down (output). Rule-based manipulation of
symbols does not constitute intelligence: the inhabitant of the Chinese room
does not understand Chinese.
- Chinese room criticism: AI really is just an ongoing program showing Chinese
room is false
- No matter how intelligent machine behavior may seem, it does not reßect
true intelligence.
- Where do we draw the line between just a system and intelligence?
Strong AI
- ÒThe appropriately programmed computer with the right inputs and outputs would
thereby have a mind in exactly the same sense human beings have mindsÓ
1950s
Minsiy and EdmondsÕ SNARC
- Built the Þrst randomly wired neural network learning machine (SNARC)
- 40 neurons
Several successes:
- General problem solving using a physical symbol system.
- Computers playing checkers, proving theorems
- Invention of Lisp, the dominant high-level AI language
- Neural network (connectionist) research was pushed to the background
Symbolic AI
- Not concern itself with neurophysiology
- Human thinking is a kind of symbol manipulation (If A>B and B>C then A>C)
- Intelligence is thought of as symbols and the relations between them
- Knowledge-based, or expert systems were hugely successful
ELIZA
- Weizenbaum (1965): ELIZA was an early natural language processor
• Used simple techniques to create the illusion of understanding
• Regardless, some people felt like the computer did understand them
STRIPS
- Stanford Research Institute Problem Solver: an automated planner
- Realization of goals
- Divide the task into subgoals, identify necessary actions
- Early action planners were susceptible to the Sussman anomaly:
• Agent must stack the blocks with A on top of B and B on top of C. Only move 1
block at a time.
• 2 subgoals, but
either way, you
wonÕt get it with
the simple
planner.
Expert systems
- Emulates the decision-making ability of a human expert
- MYCIN (Stanford 70s) was designed to diagnose and recommend treatment for
certain blood infections
• Not used in practice due to ethical and legal difficulties (whoÕs to blame when it
goes wrong?)
60s-70s
- OverconÞdence in AI systems (too much faith in them)
- AI not as powerful as many thought Ñ> AI winter
- Many unanswered questions: how do we deal with perceptions robotics, learning
and pattern recognition?
Connectionist AI
- After the AI winter of the 70s, connectionism was revived
• Rumelhart & McClelland: the PDP research group
• Early PDP work: McClelland 1981
- Model of human memory
• Human memory is content-addressable
• First explicit theory on data storage in the
brain
- Visual demonstration
• Parallel distributed processing, connectionism, artiÞcial neural networks
- Biologically inspired
• Connectionism is based on the structure of the human brain
• Neurons receive input through dendrites and send output through axon
• Highly connected (1000s of synapses on axon and dendrites)
• 20x10^9 neocortical neurons, 15x10^13 cortical synapses
• Computation is massively parallel = efficient
- Lesion tolerant
• Lesioned or damaged networks can still process information
- Capable of generalization
• ANNs are capable of learning, and able to generalize rules to novel input
- Principles
• How do these neural networks compute anything?
• Neurons output a signal based on their input signal
• Multi-layer perceptrons are able to implement all logical operators, such as
AND, OR, XOR
80s-90s
- 1997: Þrst time a computer (IBM;s Deep Blue) beat a grandmaster at chess
- 2005: last time a human beat a top chess computer under tournament conditions
- 2009: HTC touch HD running chess software equals Deep BlueÕs performance
2000s-present
- Data mining offers huge quantities of data
- Deep learning offers representation at many levels
- Bayesian networks deal with uncertain knowledge
- Deep reinforcement learning can learn to act from rich, noisy data
Deep Networks
- Adding more layers adds to dimensionality of classiÞcation
- Multiple representations offer multiple levels of abstraction
- Recurrent connections can maintain context, temporal information
- Combination is hot topic: google is investigating motion classiÞcation and content
classiÞcation
GO
- Critics said chess was too simple
- Ancient Chinese game Ô goÕ has +- 10^170 legal positions vs 10^43 legal
positions in chess Ñ> never mastered by a computer
- 2016 Google DeepMindÕs AlphaGo defeated the worldÕs number one player
- Software used deep reinforcement learning
Machine learning
- If we donÕt want to preprogram all knowledge, systems should be able to learn
• How to construct computer programs that automatically improve with
experience (mitchell 1997)
- Unsupervised learning:
• Discover hidden structure in data without labeled data
- Reinforcement learning
• Learning from feedback signal
- ClassiÞcation
• Determining group membership based on input data
• Ô Does this MRI image of someoneÕs head show a brain tumor?Õ
- Regression
• Predict outcome data based on input data
• Given its location, surface ara, and number of rooms, can we predict the value
of this house?
Conclusion
Philosophical implications:
- Weak AI; machines can simulate human intelligence using clever tricks
- Strong AI: well-programmed machine that exactly emulates the human brain is a
mind, and thereby intelligent
Approaches to AI:
Lecture Notes
Artificial Intelligence
Course objectives
- how AI became a science
- What are the different flavours of AI research (symbolic, artificial neural network)
- What are the techniques behind intelligence software (search techniques)
- How can robots seem so intelligent?
- How can computer systems learn new things (supervised, unsupervised, reinforcement
learning)
- How is AI used in practise?
Acting humanly we don’t care about the cognitive system, as long as it behaves
Cognitive psychologists are also called computationists: the study of computations that make it
possible to perceive, reason, and act. AI is not psychology or computer science, but it draws from
these disciples.
AI does forward modelling instead of reverse modelling. We design a simple system and see
how that behaves.
1940s - 1960s:
- Turing test
- How far have we come? Think of chatbots, they suck
- Weak AI assumes we can create intelligent behavior through simple tricks
- John Searle says that computers will never be intelligent. Consciousness requires actual
physical -chemical properties of actual human brains! His argument was the Chinese Room
argument.
Strong AI:
- the appropriate programme computer with the right inputs and outputs would have a mind
exactly in the same sense of human beings have minds
- So AI proponents believe that intelligent systems can actually think
Problems:
- are we asking the right question? It is like asking whether a submarine can swim
- Does an accurately enough simulated human mind have all the same properties as an actual
human mind?
1950s
Symbolic AI = (GOFAI) does not concern with physiology, human thinking is a kind of symbol
manipulation. Intelligence is thought of as symbols and the relations between them.
1965
- PARRY - simulated a patient with paranoid schizophrenia, the replies are often inconsistent or
meaningless sentences but therefore realistic!
- 33 psychiatrists were asked to classify transcripts of conversations with PARRY or paranoid
schizophrenics; only 48% were correct
- MYCIN - expert systems which emulates the decisions making ability of a human expert
- MYCIN was used to diagnose and recommend treatment for certain blood infection
- They use simple “if-then” with certainty factors
- 69% accurate diagnoses which was better than physicians at the Stanford Medical School
- However it was never used in practise due to ethical and legal difficulties (if a computer gets an
incorrect diagnosis who is to blame?”
60s-70s
1986
- PDP handbook
____________________________________________________________________________________
Connectionist AI
Connectionist AI is
1. Biologically inspired
2. Lesion tolerant
3. Capable of generalisation
80s - 90s
2000s to present
- deep reinforcement learning can learn to act from rich, noisy data
- In 2016 Google DeepMIND alpha GO delegated the world number one player
Machine Learning
- supervised learning = external knowledgable supervisor presents the system with correctly
labeled training data
- Unsupervised learning = discover hidden structure in data without labelled data
- Reinforcement learning = learning from a feedback signal
Classification = determining group membership
based on input data, does the MRI image of
someone head show a brain tumour?
- the knowledge base represents facts about the world and about the way in which concepts
are related to each other, often using predicate logic; but it also represents more complex
relationships:
1. Forward chaining
2. Backward chaining
• Backward chaining is helpful in cases where not all facts are known yet
• Forward chaining is helpful when you want to know everything you can from a set of facts
- backtracking is used
- It requires 37,000 iterations (so can be solved under a millisecond), this is much better than
having a combinatorial explosion which is a naive brute force (1.09x10 to the power of 50 ways
to randomly put nine 1s, nine 2s, etc in 81 cells
- The backtracking method guarantees a valid result
What is search? The process of looking for a sequence of actions that reaches the goal
Input : problem
Output: solutions in the form of an action sequence
When a dead end is reached the algorithm backtracks to the previous choice point
We can check all paths of a given length before moving on to the next level
• DFS is preferred when you are confident that complete paths or dead ends are found after a
reasonable amount of steps
• BFS is preferred when you are working with very deep trees but not when you think that all
paths reach the goal at about the same depth (wasteful)
Hill climbing: an algorithm that is similar to DFS but order possible options based on heuristics,
so it is a DFS but with preference
Beam search: performs BFS moving downwards only through the best w nodes
Brand and bound: can give us the optimal solution without wasting resources, it works by
expanding nodes ordered by traveled distance. Once it has found a solution it ignores paths with
distances that are equal or greater than the distance for that solution.
A* procedure: combines branch and bound, distance estimates, and redundant path removal. It
guarantees finding an optimal result efficiently
Adversarial
search:
The alpha beta principle : if you have an idea that is surely bad, do not take time to see how truly
awful it is
The alpha beta algorithm does not consider moves that are considered useless
Machine learning: programs that learn by observation and experience instead of having their
knowledge pre-programmed
Supervised learning = generalising based on training data. We provide examples and expect the
system to make sense of a new example that is hasn’t seen yet.
-> fundamentally, supervised learning is function approximation
(Classification learning)
- overparameterzing a model leads to overfitting, decreasing training set error at the expense of
generalisability
Unsupervised learning:
- clustering
4. Cognitive robotics
Robots are an embodied computer, it has a body that moves around
- semi autonomous physical agent
3 primary categories:
1. Manipulators
2. Mobile robots
3. Humanoid robots
This is often fundamentally impossible (inverse problem): there are always different mechanism
with identical behavior
So lets work the other way around: by building humans instead of analysing them
5. Reinforcement learning
3. Reinforcement learning
Markov property: the conditional probability distribution for the system at the next time step
depends only on the current state
-> the effects of actions only depend on the current state, not the prior history (how the agent got
there)
Operant conditioning :
* occasionally performing a random action (e-greedy action selection) can improve performance
in the long run
6. Applications of AI
Dopamine can be used as a teaching signal (prediction error). Strong evidence for a reinforcement
learning mechanism in animals and humans
7. Q & A
#include <stdio.h>
#include <stdlib.h>
(b) Given is the declaration of some square 2-dimensional array: int square[N][N];
You may assume that the array is already filled with data. Write a program fragment that determines whether the array is a Latin square. This
means that each row and each column must contain the values 1, 2, ..., N with no repeats. The time complexity of your solution must
not exceed O(N 2 ).
islatin = 0;
} else {
present[square[row][col]-1] = 1;
}
}
for (col=0; col < N; col++) {
islatin = islatin && present[col];
}
}
Answer:
int mul(int a, int b) {
if (b == 0) {
return 0;
}
if (b > 0) {
return a + mul(a, b-1);
}
/* b < 0 */
return mul(a, b+1) - a;
}
(b) Consider the integer sequence s=[1, 4, 2, 6, 7, 3, 7, 8, 3, 9, 0]. The sequence t=[1,6,7,9] can be obtained from
the sequence t by crossing out elements from the sequence s:
[1, 4X, 2 X, 6, 7, 3 X, 7 X, 8X, 3X, 9, 0 X]
In fact, there is another way:
[1, 4X, 2 X, 6, 7X, 3 X, 7, 8 X, 3X, 9, 0 X]
These are the only two ways that we can obtain t from s. Note that the sequence [1,2,4] can not be obtained from s by crossing out
elements, since the elements occur in s in the wrong order.
Write a recursive function that determines the number of ways that one sequence (an int array) can be obtained from another sequence by
crossing out elements.
The following program fragment (it is not a complete program) is supposed to implement the quicksort algorithm. There are, however, five
errors in this implementation. Find them, and give of each error the line number and a correction.
(b) Given is the declaration of some square 2-dimensional array: int square[N][N];
You may assume that the array is already filled with data. Write a program fragment that determines whether the array is a Latin square. This
means that each row and each column must contain the values 1, 2, ..., N with no repeats. The time complexity of your solution must
not exceed O(N 2 ).
(b) Consider the integer sequence s=[1, 4, 2, 6, 7, 3, 7, 8, 3, 9, 0]. The sequence t=[1,6,7,9] can be obtained from
the sequence t by crossing out elements from the sequence s:
[1, 4X, 2 X, 6, 7, 3 X, 7 X, 8X, 3X, 9, 0 X]
In fact, there is another way:
[1, 4X, 2 X, 6, 7X, 3 X, 7, 8 X, 3X, 9, 0 X]
These are the only two ways that we can obtain t from s. Note that the sequence [1,2,4] can not be obtained from s by crossing out
elements, since the elements occur in s in the wrong order.
Write a recursive function that determines the number of ways that one sequence (an int array) can be obtained from another sequence by
crossing out elements.
opdracht os jaar 2
1 Introduction
In the first couple of lectures of the course, we have seen how to interface with the operating
system kernel through the use of system calls. We will explore this system call interface in this
first assignment, to learn how programs can interact with and request services from an operating
system kernel. Furthermore, we will learn how the kernel can signal events to a user-mode program.
The goal of the first assignment is to implement a system program that runs on POSIX systems
and uses POSIX system calls. This year, we will be writing our own, simple text-based shell. The
shell should print a command prompt and allow the user to enter a command. After entering this
command, the shell should properly execute the command, this includes supplying the provided
arguments to the program. The user can exit the shell using the exit command. The shell should
also have a functioning cd command to change the current working directory and the shell should
show the current working directory in the prompt.
On POSIX systems, commands such as ls and cat are commonly implemented as separate
system programs and are not part of the shell itself. Within this assignment, we will not implement
such a system program, but you can try for yourself that it is trivial to implement a simple version
of the ls command. As we have seen in the Programmeertechnieken course, composite commands
(pipelines) can be created using these simple system commands and the pipe character (|), in which
case the output of one command is sent to the input of another. As part of this assignment you will
also implement support for handling pipes, so that your shell is capable of executing commands
such as cat Makefile | grep gcc. The simple composite commands will be restricted to a single
pipe. Furthermore, input/output redirections for commands will not be implemented as part of
this assignment.
Finally, for achieving a grade beyond 8 you will need to implement a rudimentary form of job
control that can handle suspending processes with CTRL+Z, bringing suspended processes to the
foreground or background using the fg and bg commands, and listing currently active jobs with
a jobs command.
2 Specification
The assignment is to implement a shell program that conforms to the specification given in this sec-
tion. The program may be written in C or C++. Do note that you will have to use POSIX system
calls, which are C functions that commonly expect C-style strings (!), even if you are programming
your shell in C++. Your program should adhere to the following design specifications:
❼ Print a command prompt that also displays the current working directory.
❼ Allow the user to enter commands and execute these commands.
❼ The entered command string must be tokenized into an array of strings by removing the space
delimiters. Also delimiters consisting of more than one space must be handled correctly.
❼ Implement exit (to exit the shell) and cd (to change the current working directory) as
built-in commands. The cd should display errors if necessary.
❼ You must write a routine yourself to perform a search of the executable. This routine should
only be used in case the name of the specified executable is not preceded by an absolute
or relative path. You are not allowed to use execlp or execvp which automate this path
search. Do not use access.
Initially, you can add a hard-coded array of standard locations to your program (e.g. ./,
/usr/bin, /bin and /usr/local/bin), which will be searched.
As a second step, read and parse the PATH environment variable (use getenv). This variable
is a :-separated list of paths to search.
❼ Your program must be capable of executing commands and correctly pass the provided
arguments to this command. This must be achieved using fork, execv, etc. system calls
and not a higher level API.
❼ Execute commands that contain a pipe character by starting two new processes that are
interconnected with a pipe. Your shell should be able to handle data streams of arbitrary
length. In case the command contains more than one pipe character, simply log an error.
Note that in order to simplify implementation you only have to deal with the case that
the pipe character is separated by spaces, so the tokenizer you have to implement already
creates a separate token for the pipe character. For instance a command of the form cat
Makefile|wc -l does not have to be handled correctly by your program.
❼ A Makefile should be included to build the software. The Makefile should be of decent
quality and should include a clean target. For a short tutorial on writing Makefiles, we refer
to the slides of the course Programmeertechnieken.
❼ Support a maximum number of jobs (say 16). Maintain the jobs using a data structure (e.g.
an array). If the user attempts to start a new job when the maximum number of jobs has
already been reached, simply refuse and output an error.
❼ Jobs are numbered. When a new job is started the smallest unused job number is assigned.
❼ The current job needs to be maintained using a stack. The job launched last is the current
job. Do note that the job number of the current job may be smaller than the maximum job
number in use.
❼ New jobs need to be started in a new progress group for signal handling to behave well (see
setpgrp and setpgid).
❼ By suffixing a command with & the job is sent to the background upon startup (e.g.
xclock &).
❼ Pressing CTRL+Z will suspend the current job on the foreground. This requires SIGTSTP to
be handled and SIGSTOP to be sent to the current process.
❼ Pressing CTRL+C will terminate the current job on the foreground. But the shell must remain
active! This requires SIGINT to be handled.
❼ fg should bring the current process to the foreground. fg 4 will bring job number 4 to the
foreground. A process (group) can be resumed by sending it the SIGCONT signal.
❼ bg should bring the current process to the background. bg 3 will bring job number 3 to the
background.
❼ Background jobs that are terminated should be cleaned up properly. To do so, you need to
implement a SIGCHLD signal handler.
If in doubt, try out the features in an existing shell such as bash or zsh to get an idea how they
should function.
3 Submission
You may work in teams of at most 2 persons. Your submission should consist of the source code of
the implemented system program and a Makefile to build the program. Make sure all files contain
your names and student IDs. Put all files to deliver in a separate directory (e.g. assignment1)
and remove any object files, binaries and other unrelated files. Finally create a gzipped tar file of
this directory:
Substitute XXXXXXX and YYYYYYY with your student IDs. Submit the tar-archive through the
BrightSpace submission site. Also note your names and student IDs in the text box in the sub-
mission website. Please, ensure only one team member submits the assignment, such that there is
a single submission per team!
Notes:
❼ For those who need the weekend to finalize things: BrightSpace submission is open until
Sunday, March 12, 23:59. After this time, submissions will be considered late. Note that no
questions will be answered on the forum and mailing list after Friday 18:00.
❼ All source code that is submitted will be subjected to automatic plagiarism checks. Cases
of plagiarism will be reported to the board of examiners.
❼ As with all other course work, keep assignment solutions to yourself. Do not post the code
on public Git or code snippet repositories where it can be found by other students.
❼ In case you develop your shell on a macOS system (or Windows Subsystem for Linux), make
sure to also test it on a Linux-system before handing in! Preferably test on the university
Linux computers before handing in. In the case of disputes, the university Linux installation
is used as reference.
❼ We may always invite teams to elaborate on their submission in an interview in case parts
of the source code need further explanation.
4 Assessment
The maximum grade that can be obtained for this assignment is 10. The points are distributed
as follows:
For code quality the following is considered: good structure, consistent indentation, presence and
quality of Makefile, comments where these are required.
Error handling and correct memory handling are considered for each of the different components.
5 Approach
The key to success is to implement the different features step by step and to not try to implement
everything at once. So, start with the bare minimum, thoroughly test it and only after that move
on to add the next feature. For example, the first steps could be:
1. Write a couple of functions to read commands to be executed from the terminal and properly
tokenize these commands into argument vectors. Set up these argument vectors such that
these can later be passed to execv().
2. Write the functions required to find the binary of the command to be executed. So, you
want to write a routine that turns ls into the full path /bin/ls (or /usr/bin/ls depending
on your operating system).
3. Write a toy example program that starts another program using fork(), execv(), wait().
4. Now, incorporate the functions you have written for command line parsing and path search
into the toy example program.
5. Implement the exit and cd commands.
6. Implement pipelines.
7. and so on ...
Executing a program. To start a new process and execute a program the fork and execve
system calls should be all you need (like discussed in the lectures). Detailed information on these
calls can also be found using man (e.g. man 2 execve). You may also consider using the front-end
function execv described in man section 3.
When working with execv note that the first item in the argument vector indicates the program
to run. If this item does not contain a slash, it is not a relative or absolute path and you must
find out where this program is located in the file system. To do so, concatenate the name of the
program to each of the paths in the hard-coded path array and use, for example, the stat call to
test whether the file exists1 . This result is used as first argument to the execv function, note that
the first item of the argument vector must be left untouched.
Once a child process terminates it becomes a zombie. The parent must clean up such zombie
processes using the wait or waitpid functions. These calls also allow you to collect the exit code
of the child process.
Creating a pipe. To create a pipe you have to use the pipe system call. You need to pass an
array of two integers as an argument, which will be filled with the file descriptors of the input and
output end of the pipe. To reconnect standard input and output to the pipe, you will also need
the close and dup system calls. Also useful are the defines STDIN_FILENO and STDOUT_FILENO
that represent the file descriptors for stdin and stdout respectively.
Working with POSIX signals. You will need to implement a number of POSIX signal han-
dlers. Signal handlers are best installed using the sigaction function (see man 2 sigaction).
Initialize a sigaction structure and call the sigaction function for the appropriate signal num-
ber (for example SIGCHLD). General information on POSIX signals can be found in section 7 of
the manual: man 7 signal.
You can also send signals yourself. This is accomplished using the kill system call (man 2 kill)
or killpg to send the signal to a process group.
A final warning concerns the possibility of race conditions. Any signal handler that you install
may be invoked at any time. For example, while your program is running a function to manipulate
your jobs data structure, a signal handler may be invoked interrupting the current function. In
case this signal handler will update the same jobs data structure, the data structure will most
likely be corrupted! This must be avoided! Using sigaction you can block signals from being
delivered while a certain signal is being handled. Similarly, you may want to block signals while
modifying your jobs data structure from outside a signal handler. You can temporarily block
certain signals from being delivered using the sigprocmask call.
1 It is true that the state of the file system can change between the call to stat and execv, so checking the return
54) Suppose that a undirected graph G contains two distinct simple paths
from a vertex u to a vertex v. Show that G has a cycle.
Reminder:
maximal acyclic: after adding an edge, a cycle is created;
minimally connected: after removing an edge, the result is no longer
connected.
58) a. The undirected graphs G1 and G3 below are isomorphic. Give an
isomorphism between G1 and G3 .
b. G2 is not isomorphic to the other two graphs. Give a simple argu-
ment why this is not the case.
0
4 5 6 b
4 1
a f c
5
3 2 1 2 3 d e
G1 G2 G3
1 2
5 6
7
60) a. Draw the five non-isomorphic connected undirected graphs with five
vertices and five edges.
b. Indicate for each of the graphs in a. which are bipartite and which
are not. If a graph is bipartite, give a correct division of the vertices;
if not, please explain why not.
c. Do the graphs K2,4 (complete bipartite) and K6 (complete) have an
Eulerian circuit? If so, draw the graph, number the vertices and give
an Eulerian circuit; if not, indicate why not.
d. How many 5-regular graphs are there with 7 vertices? And how many
complete bipartite graphs with 7 vertices? Justify your answer and
draw them all.
61) Prove using mathematical induction that for all n ≥ 1 the number of
n (n − 1)
edges ℓ(n) in the complete graph Kn is .
2
Give the base case, formulate the induction hypothesis, and prove
the induction step.
Question 1. 10 P.
Draw the Venn diagrams for the following pairs of sets; determine the relationship of the pair: i.e. are they
equal, is one a subset of the other, are they disjoint, or incomparable (none of the previous relationships).
1. (A ⊕ B)c and Ac ⊕ B
2. (A ⊕ B) ∪ C and (A ∪ C) ⊕ (B ∪ C)
Hint: Draw a separate Venn diagram for each of the four expressions, and compare the shaded regions.
Question 2. 10 P.
Use the rules (laws/axioms) of set algebra to simplify the following expression as much as possible:
((B c ∪ A)c ∪ B) ∪ B.
Question 3. 6 P.
Consider the following binary relation R defined over the set {a, b, c, d}:
R = {(a, a), (a, b), (a, d), (b, b), (b, d), (d, a), (d, b), (d, d), (c, c)}.
Question 4. 12 P.
How many numbers from {1, . . . , 300} are not divisible by 2, 3 nor 5 (so by none of them)? Find the answer
by using the principle of inclusion and exclusion. Use a Venn diagram to illustrate the relevant sets and their
relationships.
Hints: For p ∈ N+ , let Dp = {k ∈ {1, . . . , 300} | p divides k} be the set of numbers between 1 and 300 that
are divisible by the number p. Then, the number of integers in {1, . . . , 300} that are divisible by either 2, 3 or
5 equals |D2 ∪ D3 ∪ D5 | (where |A| denotes the number of elements of A, i.e. the cardinality of A).
Note that |Dp | = 300
p , if p divides 300, and Dp ∩ Dq = Dp×q , if p and q are relatively prime, that is have no
common divisors. Specially, for p, q ∈ {2, 3, 5} and p 6= q, a number is divisible by p and q if and only if it is a
multiple of p × q.
Question 5. 12 P.
Let A = {1, 2, 3, 4} and B = {a, b, c, d}.
1. It is possible to find functions f : A → B, for which there exists a subset V ⊆ A such that f −1 (f (V )) 6= V .
Specify one such function, by giving all the pairs (x, f (x)) (so the set {(x, f (x)) | x ∈ A}) and also give
the corresponding set V ⊆ A.
2. It is possible to find functions g : A → B, for which there exists a subset W ⊆ B such that g(g −1 (W )) 6=
W . Specify one such function, by giving all the pairs (x, g(x)) (so the set {(x, g(x)) | x ∈ A}) and also
give the corresponding set W ⊆ B.
3. For the f and g you defined, determine if they are injective and or surjective.
Note that a function has to be total; it has a value for all the elements of its domain.
Exercises 9'
B F J
C D E G H K
L M
a. (i) Give the leaves of this tree. (ii) List the children of node B. (iii)
What are the ancestors of node M ? (iv) Give all the nodes at level
2, from left to right. (v) What is the depth of the tree?
b. Draw the corresponding binary tree T ′ [in left-child right-sibling rep-
resentation].
c. Given an ordered rooted tree T with root R and subtrees T1 , T2 , . . . , TM .
A postorder traversal of T is defined as follows:
(1) Traverse the subtrees T1 , T2 , . . . , TM (in that order) in postorder.
(2) Visit the root R.
Let T be the ordered rooted tree above. Traverse T in postorder and
list the contents of the nodes in that order.
d. Give a recursive definition for the preorder traversal of an ordered
rooted tree T and determine the preorder ordering of the nodes for
the example tree above.
e. Consider the binary tree T ′ from b. Perform a preorder, postorder
and symmetric (also called inorder) traversal of T ′ and list the nodes
in each of these three orderings. Compare these traversals with the
preorder and postorder traversal of the original ordered rooted tree T .
65) Given the following array of nodes: 48, 29, 59, 19, 43, 52, 100, 64, 49,
56, 45, 8, 99, 90, 20, 55, 84, 18, reconstruct the complete binary tree.
B C B C C B
D D D
T1 T2 T3
Identify those which represent the same:
a. rooted tree
b. ordered rooted tree
c. binary tree.
From Schaum, exercise 10.4.
67) a. The nodes of a binary tree T are given in pre-order as follows:
A, B, D, E, G, K, C, F, H, I. The symmetric ordering of the nodes of
T is: D, B, K, G, E, A, C, H, F, I. Reconstruct the binary tree T from
this and draw it. Explain clearly how you arrived at your answer.
b. From pre-order and post-order we cannot always reconstruct the
tree. Show this by drawing different binary trees for which a pre-
order traversal yields A, B, C, D, and a post-order traversal returns
D, C, B, A.
(3x − 5z)4
68) Consider the algebraic expression: E = .
a(2b + c2 )
a. Draw the corresponding ordered rooted tree T , using an arrow (↑)
for exponentiation, an asterisk (*) for multiplication, and a slash (/)
for division.
b. Use T to rewrite E in Polish prefix notation (pre-order).
c. Give the post-order and symmetric notation (postfix, respectively
infix).
d. From the prefix notation, reconstruct the expression tree from a.
FA1 FA2
Assignment # _______
Section :_______________
Previously most students didn’t follow these instructions I did not deduct marks for
this but this time I will deduct the marks for not following the instructions.
2G1505 Automata Theory
Solutions to Exam of: Dilian Gurov
16 October 2002, 14.oo - 19.oo KTH/IMIT/LECS
1. Give a DFA for the language defined by the regular expression a∗ a, and another one for a(ba)∗ . The 5p
union of the two DFAs defines a non-deterministic FA for the language a∗ a + a(ba)∗ .
(a) Apply the subset construction to this NFA to produce a DFA for this language. Omit the
inaccessible states. Draw the graph of the resulting DFA.
(b) Is this DFA minimal? If not, which states are equivalent?
..
Solution: The graphs to appear later. ^
2. Show that regular languages are closed under doubling: if language L is regular, then so is also 5p
∆ ∆
the language L2 = {two x | x ∈ L}, where string doubling is defined inductively by two = and
∆
two ax = aa · (two x).
Solution: Here is a standard solution using finite automata; alternative solutions exist using regular
expressions or homomorphisms.
Let L be regular. Then there is an NFA N = (QN , Σ, ∆N , SN , FN ) such that L(N ) = L. Define
a a
another NFA, N2 , as follows: start with N and replace every edge q −→ q 0 with two edges q −→ q 00
00 a 0 00
and q −→ q by inserting (for each edge!) a new state q . We can formalize this idea by taking as
states of N2 the states of N plus the edges of N , the latter represented for example as the set of triples
0 ) | q ∈ Q , q 0 ∈ ∆ (q , a)}. So, we can define N as follows:
{(qN , a, qN N N N N N 2
∆ 0 ) | q ∈ Q , q 0 ∈ ∆ (q , a)}
• QN2 = QN ∪ {(qN , a, qN N N N N N
• ∆N2 is given by the two defining equations:
∆ 0 ) | q 0 ∈ ∆ (q , a)} and
∆N2 (qN , a) = QN ∪ {(qN , a, qN N N N
∆
0 ), a) = if a = b then {q 0 } else ∅
∆N2 ((qN , b, qN N
∆
• SN2 = SN
∆
• FN 2 = FN
It is straightforward to show that, for the so constructed NFA, L(N2 ) = L2 , thus implying that L2 is
regular. Hence, regular languages are closed under doubling.
3. Consider the language L defined by the regular expression (a∗ + ba)∗ . Describe the equivalence classes 5p
of {a, b}∗ w.r.t. the Myhill–Nerode relation ≡L defined by: (cf. equation (16.1) on page 97)
∆
x1 ≡L x2 ⇐⇒ ∀y ∈ Σ∗ .(x1 · y ∈ L ⇔ x2 · y ∈ L)
Present these equivalence classes through regular expressions. Use ≡L to construct a minimal automa-
ton M≡L (cf. page 91) for the language L, and draw the graph of the automaton.
Solution: The states of the quotient automaton are:
Σ∗/≡L = {L((a + ba)∗ ), L((a + ba)∗ b), L((a + ba)∗ bb(a + b)∗ )}
The graph to appear later.
4. Consider the language: 15p
n o
∆
L = x · y ∈ {a, b}+ | y = rev x
where rev x denotes the reverse string of x (cf. HW 2.2, page 302).
5. Give a detailed description of a total Turing machine accepting the palindromes over {a, b}: that is, 5p
all strings x ∈ {a, b}∗ such that x = rev x.
Solution: We build a machine which repeatedly scans the tape from left to right, trying to match the
first input symbol (which is directly replaced by the blank symbol) with the last input symbol (also
directly replaced by the blank symbol).
Graph to appear later.
n is not decidable: othat is, that there is no total Turing machine MA accepting 5p
6. Argue that acceptance
∆
the language LA = M̂ ]x̂ | M accepts x , by reducing from the Halting problem.
Solution: A TM halts on input x if it either accepts x or otherwise rejects x. We use this observation
to show that the Halting problem (cf. Lecture 31) can be reduced to the above acceptance problem.
Assume that there is a total Turing machine MA accepting LA . We can then build a machine MR
which, on any input M̂ ]x̂, first swaps the values of t and r in M̂ (that is, swaps the accepting and
the rejecting states of M ), and then behaves exactly like MA . Hence MR is a total Turing machine
deciding rejection. We can now combine MA and MR to produce a total Turing machine MH deciding
the Halting problem: for example, on any input M̂ ]x̂, let MH first run as MA and accept if MA
accepts, but continue as MR if MA rejects. MH will thus accept M̂ ]x̂ if M halts on x, and will reject
M̂ ]x̂ otherwise.
But the Halting problem is undecidable, and therefore there is no total Turing machine MA accepting
LA . The acceptance problem is therefore undecidable.
2G1505 Automata Theory
Solutions to Exam of: Dilian Gurov
17 December 2003 KTH/IMIT/LECS
1. Convert the nondeterministic automaton given below to an equivalent deterministic one using the
subset construction. Omit inaccessible states. Draw the graph of the resulting DFA.
a b
→ q1 q1 , q 2 q3 F
q2 − q4 F
→ q3 q4 −
q4 − q1 , q 4
a b
→ {q1 , q3 } {q1 , q2 , q4 } {q3 } F
{q1 , q2 , q4 } {q1 , q2 } {q1 , q3 , q4 } F
{q1 , q3 , q4 } {q1 , q2 , q4 } {q1 , q3 , q4 } F
{q1 , q2 } {q1 , q2 } {q3 , q4 } F
{q3 , q4 } {q4 } {q1 , q4 }
{q1 , q4 } {q1 , q2 } {q1 , q3 , q4 } F
{q4 } {} {q1 , q4 }
{q3 } {q4 } {}
{} {} {}
2. For the deterministic automaton given below, apply the minimization algorithm of Lecture 14 to
compute the equvalence classes of the collapsing relation ≈ defined in Lecture 13. Show clearly the
computation steps. List the equivalence classes, and apply the quotient construction to derive a
minimized automaton. Draw its graph.
a b
→ q0 q0 q1
q1 q2 q3
q2 q2 q3
q3 q2 q4 F
q4 q0 q1
Solution: (incomplete) After applying the minimization algorithm we obtain that q0 ≈ q4 and q1 ≈ q2 .
Thus we obtain the following minimized automaton (given as a table).
a b
→ {q0 , q4 } {q0 , q4 } {q1 , q2 }
{q1 , q2 } {q1 , q2 } {q3 }
{q3 } {q1 , q2 } {q0 , q4 } F
3. Let A ⊆ Σ∗ be a language. We define its prefix closure as:
pref A = {x ∈ Σ∗ | ∃y ∈ Σ∗ . x · y ∈ A}
Prove that regular languages are closed under prefixing: if language A is regular, then so is pref A.
Solution: Let A ⊆ Σ∗ be regular. Then there must be a DFA MA = (Q, Σ, δ, s, F ) such that
L(MA ) = A. Now, based on MA , define another automaton M = (Q, Σ, δ, s, F 0 ) so that:
n o
F 0 = q ∈ Q | ∃y ∈ Σ∗ . δ̂(q, y) ∈ F
We will show that this automaton accepts the language pref A, and hence that pref A is regular.
Claim. L(M ) = pref A
Proof.
then a string is in A exactly when after reading it the stack contains only B’s (on top of ⊥). Note
that there must be at least one such B. So, we can obtain the desired behaviour by adding two
more productions:
b
hq, ⊥i ,→ hq, Bi
ε
hq, Bi ,→ hq, εi
5. Apply the Pumping Lemma for context–free languages (as a game with the Demon) to show that the
language: n o
A = an bn aj | j ≤ n
is not context–free.
Solution:
Since |vwx| ≤ k, vwx has either the shape al bm or bm al for some l, m such that l + m ≤ k. In the first
case xv 0 wx0 y must be of the shape ap bq ak for some p, q such that p + q < 2k, and thus is not in A.
In the second case, xv 2 wx2 y will either not be in L(a∗ b∗ a∗ ) at all, and thus not in A, or else xv 2 wx2 y
must be of the shape ak bp aq for some p, q such that p + q > 2k, and thus not in A. Hence, we win the
game in all cases, which shows that A is not context–free.
6. Give a detailed description (preferably as a graph) of a total Turing machine accepting the language:
n(n+1)
n
A= a b 2 |n≥0
Give solutions in English or Swedish, each problem beginning on a new sheet. Write your name
on all sheets. The maximal number of points is given for each problem. The course book and own
notes as well as reference material are admissible at the exam.
1. Convert the nondeterministic automaton given below to an equivalent deterministic one using the 2p
subset construction, textbook Lecture 6. Omit inaccessible states. Draw the graph of the resulting
DFA.
a b
→ q0 {q1 } {q2 }
q1 {q0 , q1 } {q0 }
q2 F ∅ {q1 , q2 }
2. For the deterministic automaton given below, apply the minimization algorithm of Lecture 14 of the 4p
textbook to compute the equvalence classes of the collapsing relation ≈ defined in Lecture 13.
a b
→ q0 F q1 q3
q1 q2 q3
q2 F q5 q2
q3 q4 q1
q4 F q5 q4
q5 q5 q5
Solution: As a table:
a b
→ {q0 } F {q1 , q3 } {q1 , q3 }
{q1 , q3 } {q2 , q4 } {q1 , q3 }
{q2 , q4 } F {q5 } {q2 , q4 }
{q5 } {q5 } {q5 }
3. Recall the Myhill–Nerode Theorem, textbook Lecture 16, with the equivalence relation ≡A on strings 4p
defined in equation (16.1). The latter gives rise to the following technique for proving that the minimal
DFA for a given regular language A over Σ has at least k states:
Then ≡A has at least k equivalence classes, and hence the desired result.
Now, let m be an arbitrary but fixed even number. Consider the language B over {a, b} consisting of
all strings of length at least m which have an equal number of a’s and b’s in the last m positions. Use
m
the technique described above to show that the minimal DFA for language B has at least 2 2 states.
Hint: Consider the strings of length m 2.
Solution: Consider the set X of strings of length m 2 over {a, b}. Let x, y ∈ X so that x 6= y. Let
x0 , y 0 be the shortest suffices of x and y respectively, such that |x0 | = |y 0 | and x0 6= y 0 . Then obviously
]a(x0 ) 6= ]a(y 0 ). Now let z = ar bs , where r = m 0
2 − ]a(x ) and s = m − (|x | +
0 r). Then x · z ∈ B while
m m
0 0
y · z 6∈ B, and hence x 6≡B y . Since X has 2 2 elements, ≡B has at least 2 2 equivalence classes, and
m
therefore the minimal DFA for language B has at least 2 2 states.
4. A context–free grammar G = (N, Σ, P, S) is called strongly right–linear (or SRLG for short) if all 6p
its productions are of shape A → aB or A → . Prove that SRLGs generate precisely the regular
languages.
Hint: Define appropriate transformations between SRLGs and Finite Automata – one for each direc-
tion! – and prove that these transformations are language preserving. State and prove appropriate
lemmas where needed to structure the proofs.
Solution: In two parts: the first part shows that the languages generated by SRLGs are regular,
while the second shows that every regular language is the language of some SRLG.
def
a) Let G = (N, Σ, P, S) be a SRLG. Define the NFA NG = (N, Σ, ∆, {S} , F ) where we define
def def
∆(X, a) = {Y ∈ N | (X → aY ) ∈ P } and F = {X ∈ N | (X → ) ∈ P }. Then:
x ∈ L(NG ) ⇔ ˆ
∆({S} , x) ∩ F 6= ∅ {Def. L(N )}
⇔ {X ∈ N | S →∗G xX} ∩ F 6= ∅ {Lemma A}
⇔ ∃X ∈ N. (S →∗G xX ∧ X →G ) {Def. F }
⇔ S →+G x {Def. →∗G }
⇔ x ∈ L(G) {Def. L(G)}
So L(G) = L(NG ) and hence L(G) is regular. In the proof, Lemma A states that
ˆ
∆({Y } , x) = {X ∈ N | Y →∗G xX}
} {Z ∈ N | (X → aZ) ∈ P } {Def. ∆}
S
= X∈{X∈N |Y →∗G xX
= {X ∈ N | Y →∗G xaX} {Def. →∗G }
b) This part is similar to the previous one and is only sketched here. Let A be a regular language.
def
Then there is a DFA MA = (Q, Σ, δ, s, F ) such that L(MA ) = A. We define the SLRG GA =
def
(Q, Σ, P, s) where P = {q1 → aq2 | δ(q1 , a) = q2 } ∪ {q → | q ∈ F }. We then show x ∈ L(GA ) ⇔
x ∈ L(MA ) = A, the proof of which is best structured by proving and using Lemma B:
q1 →∗GA xq2 ⇔ δ̂(q1 , x) = q2
5. Recall the Chomsky–Schützenberger Theorem, textbook Supplementary Lecture G. Show how this 3p
Theorem applies to the context–free language PAL of even–length palindromes over {a, b}, by identi-
fying a suitable number n, a regular language R, and a homomorphism (renaming) h.
Solution: PAL is equal to the language h(PARENn ∩ R) for n = 2, R = L(([1 +[2 )∗ (]1 +]2 )∗ ) and h
renaming [1 and ]1 to a and [2 and ]2 to b.
(a) Refer to the closure properties of regular languages to argue that A is not regular.
Solution: The regular lanuguages are closed under intersection, but A∩L(a∗ b∗ ) equals {an bn | n ≥ 0}
which is not regular, hence A cannot be regular.
(b) Refer to the closure properties of context–free languages to argue that A is context–free.
Solution: The context–free languages are closed under languagenconcatenation, o and since A can
be represented as the concatenation of the context–free languages a b | k ≥ 0 and {bm am | m ≥ 0},
k k
A must be context–free.
(c) Give a context–free grammar G generating A.
Solution: From the previous observation, and the construction on grammars we used to show
this closure property, we directly obtain the grammar:
S → AB
A → | aAb
B → | bBa
(d) Construct an NPDA accepting A − {} on empty stack. Explain your choice of productions.
7. Give a detailed description (preferably as a graph) of a Turing machine with input alphabet {a, b}, 4p
that on any input x halts with string y ∈ L(a∗ b∗ ) written on its tape, where ]a(x) = ]a(y) and
]b(x) = ]b(y). Explain how your Turing machine achieves its task.
Solution: (Idea) There are many possible solutions, but one simple idea is to use insertion sort:
Iteratively “swap” the left–most b with the first following a (using renaming), until there are no more
such a’s. (6 states suffice!)
8. Recall Rice’s Theorem, textbook Lecture 34. Explain why the trivial properties of the recursively 2p
enumerable sets are decidable, by suggesting suitable total Turing machines for these properties.
Solution: There are exactly 2 trivial properties: the empty set (of r.e. sets) and the set of all r.e.
sets. Since we represent every r.e. set by some (encoding of a) Turing machine accepting this set, the
two trivialnpropertieso can be represented, by using some
n fixedoencoding of Turing machines, as the
languages M̂ | false , which is the empty set, and M̂ | true , which is the set of all legal Turing
machine encodings.
A total Turing machine MF accepting the first language is simply one that upon reading ` immediately
enters its reject state, while a total Turing machine MT accepting the second language is one which
decides whether the input string is a legal encoding of some Turing machine according to the chosen
encoding scheme.
DD2371 Automata Theory
Examination Problems Dilian Gurov
with solutions KTH CSC
19 May 2008 08-790 81 98
1. Consider the language over the alphabet {a, b} consisting of all strings that do not contain 2 or E
more consecutive a’s and do not end with b.
(a) Construct a deterministic finite automaton (DFA) that accepts this language. Draw its
graph.
Solution: For example, see solution to problem 2(c).
(b) Suggest a regular expression that generates this language.
Solution: For example, the regular expression (a + )(bb∗ a)∗ .
2. For the deterministic automaton given below, apply the minimization algorithm of textbook E,D
Lecture 14 to compute the equvalence classes of the collapsing relation ≈ defined in textbook
Lecture 13.
a b
→ q0 F q1 q3
q1 F q2 q3
q2 q2 q5
q3 q4 q6
q4 F q2 q6
q5 q2 q5
q6 q7 q3
q7 F q5 q6
(a) Define a construction on finite automata that has the corresponding effect on the accepted D,C
language.
Solution: An important result about a deterministic finite automaton M = (Q, Σ, δ, s, F )
is that δ̂(s, x · y) = δ̂(δ̂(s, x), y). So, if both x and y are accepted by M , the unique path
from s to δ̂(s, x · y) ∈ F has to pass state δ̂(s, x) ∈ F . Then, to elimiminate all suffixes of
words in L(M ), one has to eliminate all paths starting (and ending) in accept states of M .
This is easily achieved by removing all outgoing edges from all accept states. Note that
this results in a nondeterminstic finite automaton! So, we define:
def
N = (Q, Σ, ∆, {s} , F )
def
∆(q, a) = {δ(q, a) | q 6∈ F }
After proving this Lemma, it is straightforward to prove that x ∈ L(N ) ⇔ x ∈ min L(M ).
4. Consider the regular language A over alphabet Σ = {a, b} defined through the regular expression D,C
(ab + b)∗ . Recall the Myhill–Nerode Theorem, textbook Lecture 16, with the equivalence relation
≡A on strings over Σ defined by: (cf. equation (16.1) on page 97)
x1 ≡A x2 ⇐⇒ ∀y ∈ Σ∗ .(x1 · y ∈ A ⇔ x2 · y ∈ A)
def
(a) Show the equivalence classes of Σ∗ w.r.t. equivalence ≡A , represented as regular expressions.
Solution: There are three equivalence classes. They can be represented by the regular
expressions: (ab + b)∗ , (ab + b)∗ a and (ab + b)∗ aa(a + b)∗ .
(b) For every pair of (different) equivalence classes A1 and A2 , give the shortest distinguishing
experiment, by means of a string y ∈ Σ∗ such that x1 · y ∈ A ⇔ x2 · y 6∈ A for any x1 ∈ A1
and x2 ∈ A2 .
Solution: (ab + b)∗ is distinguished from (ab + b)∗ a by , (ab + b)∗ is distinguished from
(ab + b)∗ aa(a + b)∗ by , and (ab + b)∗ a is distinguished from (ab + b)∗ aa(a + b)∗ by b.
5. Consider the language family B,A
Prove formally that for every n, the minimal DFA accepting An has exactly n + 2 states.
Solution: We know that the minimal DFA accepting An is unique up to isomorphism. Then,
one can prove the above result by exhibiting a DFA for An that has exactly n + 2 states, and is
minimal, in the sense that no two of its states are equivalent.
For a given n, define the DFA
def
Mn = ({q0 , . . . , qn+1 } , {a, b} , δ, q0 , {q0 , . . . , qn })
def def def def
where δ(qi , a) = qi+1 for 0 ≤ i ≤ n, δ(qi+1 , b) = qi for 0 ≤ i < n, δ(q0 , b) = qn+1 , δ(qn+1 , a) =
def
qn+1 , and δ(qn+1 , b) = qn+1 . Mn has n + 2 states, and it is easy to see that Mn accepts An .
We now show that Mn is minimal. For 0 ≤ i < j ≤ n, we have qi 6≈ qj with witness bi+1 (since
δ̂(qi , bi+1 ) = qn+1 6∈ F while δ̂(qj , bi+1 ) = qj−(i+1) ∈ F ). And for 0 ≤ i ≤ n, we have qi 6≈ qn+1
with the obvious witness .
S → | aS | Sb
(a) Refer to the closure properties of context–free languages to show that L is context–free. E
def def def
Solution: We have L = A · C ∪ C · B for languages A = B = L(aa∗ ),
and C = L(bb∗ ),
n n
{a b | n ≥ 0}, all of which are context–free. Since CFLs are closed under concatenation
and union, L must also be context–free.
(b) Guided by your answer, give a context–free grammar G generating L. E
Solution: Using the constructions used for proving the corresponding closure properties,
we easily obtain the grammar:
S → SA SC | SC SB
SA → a | aSA
SB → b | bSB
SC → | aSC b
(c) Construct a deterministic pushdown automaton (DPDA) that accepts L on final states. E,D
(Recall that DPDAs rewrite ⊥ only to strings of shape γ⊥, so they never halt because of
an empty stack.) Draw its graph and explain its workings.
Solution: (Sketch) It is not difficult to come up with a DPDA for this language having
6 control states. The key idea is to push onto the stack a letter A for the first a of the
input word, but push a different letter B for all following a’s. This allows to detect the b
“matching” the first a, and to move to a non-final control state.
(d) Recall the Chomsky–Schützenberger Theorem (textbook Supplementary Lecture G). Show D,C
how this theorem applies to the above language L, by identifying:
• a suitable natural number n,
• a regular language R over the alphabet Σn of the n–th balanced parentheses language
PARENn , and
• a homomorphism h : Σn → Σ∗ ,
so that you can argue that L = h(PARENn ∩ R) holds.
Solution: Again guided by the decomposition in (a), one can take n = 3, regular language
R = L([1 [1 ∗ ]1 ∗ [2 ∗ ]2 ∗ + [2 ∗ ]2 ∗ [3 ∗ ]3 ∗ [3 ) and homomorphism h defined by h([1 ) = h([2 ) = a,
h(]2 ) = h(]3 ) = b, and h(]1 ) = h([3 ) = .
9. Give a detailed description, preferably as a graph, of a total Turing machine accepting the E,D
language: n 2 o
A = an | n ≥ 0
Explain the underlying algorithm.
Solution: (Sketch) One idea is to procede in rounds, by marking the letters on the tape with
single or double dots, so that at the end of round k the tape contents are:
k
z }| {
` ää
| . . . ä{zȧȧ . . . ȧ} aa . . . a
k2
Noticing that (k + 1)2 = k 2 + 2k + 1 and that a block of k 2 a’s and another one of k a’s are
readily present after round k, it is not difficult to compute the tape contents needed at the end
of round k + 1, namely:
k+1
z }| {
` ää
| . . . ä{zȧȧ . . . ȧ} aa . . . a
k2 +2k+1
The machine accepts if after some completed round all a’s are marked, and rejects if all a’s are
marked before completion of the latest round.
10. Show that the problem of whether a Turing machine eventually writes a given letter on its tape B,A
for exactly 777 input strings is undecidable. Or, in other words, show that the set
n o
def
P = M̂ ]â | for 777 inputs, M eventually writes a on its tape
is not recursive.
Hint: Find a suitable problem P 0 on recursively enumerable sets, for which you:
(a) argue that P 0 is not trivial and hence, by Rice’s Theorem, is undecidable, and
(b) reduce P 0 to the original problem P , by describing how from a total TM for P you can
build a total TM for P 0 .
Solution: The bottom–line idea in many problems like this one is to reduce acceptance to the
given problem, in this case eventual writing of some letter to the tape.
The problem P 0 of whether a Turing machine M accepts exactly 777 strings (i.e. whether
|L(M )| = 777) is obviously a nontrivial problem on recursively enumerable sets, and hence, by
Rice’s Theorem, is undecidable. We will reduce this problem to the original problem P above.
Assume MP is a total TM for P . Construct Turing machine N as follows. On input M̂ ,
machine N :
• modifies M̂ to M̂ 0 by adding: a new symbol a to the input alphabet of M , a new state tnew
which is made the accept state of M 0 , and transitions from the original accepting state (of
M ) to tnew that on any tape symbol rewrite this symbol to a, and
• overwrites the input with M̂ 0 ]â, rewinds, and continues as MP .
Then, we have:
1. Convert the nondeterministic automaton given below to an equivalent deterministic one using the
subset construction (textbook Lecture 6). Omit inaccessible states. Draw the graph of the resulting
DFA.
a b
→ q0 {q2 } ∅
→ q1 F {q0 , q2 } ∅
q2 F ∅ {q1 , q2 }
a b
→ {q0 , q1 } F {q0 , q2 } ∅
{q0 , q2 } F {q2 } {q1 , q2 }
{q1 , q2 } F {q0 , q2 } {q1 , q2 }
{q2 } F ∅ {q1 , q2 }
∅ ∅ ∅
2. For the deterministic automaton given below, apply the minimization algorithm of textbook Lecture 14
to compute the equvalence classes of the collapsing relation ≈ defined in textbook Lecture 13.
a b
→ q0 q2 q0
q1 q0 q2
q2 F q4 q3
q3 q0 q4
q4 F q4 q1
a b
→ {q0 } {q2 , q4 } {q0 }
{q1 , q3 } {q0 } {q2 , q4 }
{q2 , q4 } F {q2 , q4 } {q1 , q3 }
3. A context–free grammar G = (N, Σ, P, S) is called strongly right–linear (or SRLG for short) if all its
productions are of shape A → aB or A → . Let us call a SRLG deterministic if for each non–terminal
A ∈ N and terminal a ∈ Σ there is exactly one production of shape A → aB (while productions of
shape A → are optional).
Define a complement construction on deterministic SRLGs. That is, for deterministic SRLGs G define
the complement G as a deterministic SRLG for which you show that L(G) = L(G).
Solution: Recall that there is a one–to–one corespondence beween DFAs and SRLGs, and recall the
complement construction on DFAs. Let G = (N, Σ, P, S) be a deterministic SRLG. We define the
def def
complement of G as the deterministic SRLG G = (N, Σ, P , S) where P = {A → aB | A → aB ∈ P } ∪
{A → | A → 6∈ P } is the set of productions of G. Then,
+
x ∈ L(G) ⇔ S →G x {Def. L(G)}
n o
+
⇔ ∃!A ∈ N. (S →G xA ∧ A → ∈ P ) G a deterministic SRLG
n o
⇔ ∃!A ∈ N. (S →+
G xA ∧ A → 6∈ P ) Def. G
⇔ not S →+G x {G a deterministic SRLG}
⇔ x 6∈ L(G) {Def. L(G)}
⇔ x ∈ L(G) {Set theory}
4. Recall the Chomsky–Schützenberger Theorem (textbook Supplementary Lecture G). Show how this
theorem applies to the context–free language
(a) Use the Pumping Lemma for regular languages (as a game with a Deamon) to prove that A is
not regular.
(b) Refer to the closure properties of context–free languages to argue that A is context–free. That
is, represent A as the result of some operation(s) on context–free languages (which we already
know to be context–free) under which CFLs are closed.
def def
Solution: A = B · C for B = {am bm | m ≥ 0} and C = {bn | n ≥ 0}, both of which we know to
be context–free, and we know CFLs to be closed under language concatenation.
(c) Give a context–free grammar G generating A.
Solution: One possibility is the grammar S → | aSb | Sb.
(d) Prove your grammar correct. You are allowed to reuse results proved in class.
(e) Construct an NPDA accepting A − {} on empty stack. Explain your choice of productions.
Solution: One solution is a NPDA with one control state q and productions:
a
hq, Si ,→ hq, SAi
a
hq, Si ,→ hq, Ai
b
hq, Ai ,→ hq, Ai
b
hq, Ai ,→ hq, i
The automaton uses the first production for reading all initial a’s but the last, then guesses the
last a and uses the second production to get rid of the S at the top of the stack. The stack now
has as many A’s as the a’s read so far. The automaton guesses the number of b’s in excess of a’s
in the string, and uses the third production that many times. The stack now has as many A’s as
there are remaining (unread) b’s. The automaton uses production four to empty the stack upon
reading the whole string.
(a) Describe a uniform, injective encoding of DFAs into strings over the alphabet {0, 1}.
Solution: Such an encoding was discussed in class.
(b) Let M̂ ∈ {0, 1}∗ denote the encoding of M . As we know from Cantor’s theorem, the set
n o
A = M̂ ∈ {0, 1}∗ | M̂ 6∈ L(M )
def
is not regular (since it is the inverted diagonal set). Argue, however, that A is recursive by giving
a (reasonably detailed) description of a total Turing machine accepting A.
Solution: (Sketch) The Turing machine T starts by modifying the input M̂ to M̂ ]qˆ0 ]M̂ where
q0 is the initial state of M . The added part qˆ0 ]M̂ represents the initial configuration of M (where
a configuration of a DFA is understood as a pair consisting of a state and the suffix of the input
string that remains to be read). T continues by simulating M on M̂ , by iteratively computing
and updating the current state of M until the input string M̂ has been completely consumed.
This is achieved by reading (and consuming) the next symbol of the input string M̂ of M , and
looking up from M̂ (always available in the first segment of the tape) the next state of M . Finally,
T checks whether the state in the final configuration of M is an accepting state, and rejects resp.
accepts accordingly. Thus, T is a total Turing machine accepting language A.