FOUNDATION COMPUTER SCIENCE (Mtech)
FOUNDATION COMPUTER SCIENCE (Mtech)
There are the following algorithms which can be used to schedule the jobs.
1. First Come First Serve
It is the simplest algorithm to implement. The process with the minimal arrival time will get the CPU first. The lesser
the arrival time, the sooner will the process gets the CPU. It is the non-preemptive type of scheduling.
2. Round Robin
In the Round Robin scheduling algorithm, the OS defines a time quantum (slice). All the processes will get executed in
the cyclic way. Each of the process will get the CPU for a small amount of time (called time quantum) and then get
back to the ready queue to wait for its next turn. It is a preemptive type of scheduling.
P0 0-0=0
P1 5-1=4
P2 8-2=6
P3 16 - 3 = 13
P0 0 5 0
P1 1 3 5
P2 2 8 14
P3 3 6 8
3
P0 0-0=0
P1 5-1=4
P2 14 - 2 = 12
P3 8-3=5
P0 0 5 1 0
P1 1 3 2 11
P2 2 8 1 14
P3 3 6 3 5
4
P0 0-0=0
P1 11 - 1 = 10
P2 14 - 2 = 12
P3 5-3=2
P0 (0 - 0) + (12 - 3) = 9
P1 (3 - 1) = 2
P3 (9 - 3) + (17 - 12) = 11
1 Process is heavy weight or resource intensive. Thread is light weight, taking lesser resources
than a process.
2 Process switching needs interaction with operating Thread switching does not need to interact with
system. operating system.
3 In multiple processing environments, each process All threads can share same set of open files,
6
executes the same code but has its own memory and child processes.
file resources.
4 If one process is blocked, then no other process can While one thread is blocked and waiting, a
execute until the first process is unblocked. second thread in the same task can run.
5 Multiple processes without using threads use more Multiple threaded processes use fewer
resources. resources.
6 In multiple processes each process operates One thread can read, write or change another
independently of the others. thread's data.
Advantages of Thread
Threads minimize the context switching time.
Use of threads provides concurrency within a process.
Efficient communication.
It is more economical to create and context switch threads.
Threads allow utilization of multiprocessor architectures to a greater scale and efficiency.
Types of Thread
Threads are implemented in following two ways −
User Level Threads − User managed threads.
Kernel Level Threads − Operating System managed threads acting on kernel, an operating system core.
User Level Threads
In this case, the thread management kernel is not aware of the existence of threads. The thread library contains code for
creating and destroying threads, for passing message and data between threads, for scheduling thread execution and for
saving and restoring thread contexts. The application starts with a single thread.
Advantages
Thread switching does not require Kernel mode privileges.
User level thread can run on any operating system.
Scheduling can be application specific in the user level thread.
User level threads are fast to create and manage.
Disadvantages
In a typical operating system, most system calls are blocking.
Multithreaded application cannot take advantage of multiprocessing.
Kernel Level Threads
7
In this case, thread management is done by the Kernel. There is no thread management code in the application area.
Kernel threads are supported directly by the operating system. Any application can be programmed to be
multithreaded. All of the threads within an application are supported within a single process.
The Kernel maintains context information for the process as a whole and for individuals threads within the process.
Scheduling by the Kernel is done on a thread basis. The Kernel performs thread creation, scheduling and management
in Kernel space. Kernel threads are generally slower to create and manage than the user threads.
Advantages
Kernel can simultaneously schedule multiple threads from the same process on multiple processes.
If one thread in a process is blocked, the Kernel can schedule another thread of the same process.
Kernel routines themselves can be multithreaded.
Disadvantages
Kernel threads are generally slower to create and manage than the user threads.
Transfer of control from one thread to another within the same process requires a mode switch to the Kernel.
Multithreading Models
Some operating system provide a combined user level thread and Kernel level thread facility. Solaris is a good example
of this combined approach. In a combined system, multiple threads within the same application can run in parallel on
multiple processors and a blocking system call need not block the entire process. Multithreading models are three types
Many to many relationship.
Many to one relationship.
One to one relationship.
Many to Many Model
The many-to-many model multiplexes any number of user threads onto an equal or smaller number of kernel threads.
The following diagram shows the many-to-many threading model where 6 user level threads are multiplexing with 6
kernel level threads. In this model, developers can create as many user threads as necessary and the corresponding
Kernel threads can run in parallel on a multiprocessor machine. This model provides the best accuracy on concurrency
and when a thread performs a blocking system call, the kernel can schedule another thread for execution.
1 User-level threads are faster to create and manage. Kernel-level threads are slower to create and
manage.
2 Implementation is by a thread library at the user level. Operating system supports creation of Kernel
threads.
9
3 User-level thread is generic and can run on any operating Kernel-level thread is specific to the
system. operating system.
SYNCHRONIZATION TECHNIQUE
In computer science, synchronization refers to one of two distinct but related concepts: synchronization of processes,
and synchronization of data. Process synchronization refers to the idea that multiple processes are to join up
or handshake at a certain point, in order to reach an agreement or commit to a certain sequence of action. Data
synchronization refers to the idea of keeping multiple copies of a dataset in coherence with one another, or to
maintain data integrity. Process synchronization primitives are commonly used to implement data synchronization. The
need for synchronization does not arise merely in multi-processor systems but for any kind of concurrent processes;
even in single processor systems. Mentioned below are some of the main needs for synchronization:
Forks and Joins: When a job arrives at a fork point, it is split into N sub-jobs which are then serviced by n tasks. After
being serviced, each sub-job waits until all other sub-jobs are done processing. Then, they are joined again and leave
the system. Thus, parallel programming requires synchronization as all the parallel processes wait for several other
processes to occur.
Producer-Consumer: In a producer-consumer relationship, the consumer process is dependent on the producer process
till the necessary data has been produced.
Exclusive use resources: When multiple processes are dependent on a resource and they need to access it at the same
time, the operating system needs to ensure that only one processor accesses it at a given point in time. This reduces
concurrency.
Synchronization techniques among threads
1. Compare and swap.
2. Mutual exclusion (mutexes) and threads.
3. Semaphores and threads.
4. Condition variables and threads.
5. Threads as synchronization primitives.
6. Space location locks.
7. Object locks.
Process is categorized into two types on the basis of synchronization and these are given below:
Independent Process
Cooperative Process
Independent Processes
Two processes are said to be independent if the execution of one process does not affect the execution of another
process.
Cooperative Processes
Two processes are said to be cooperative if the execution of one process affects the execution of another process.
These processes need to be synchronized so that the order of execution can be guaranteed.
Process Synchronization
It is the task phenomenon of coordinating the execution of processes in such a way that no two processes can have
access to the same shared data and resources.
It is a procedure that is involved in order to preserve the appropriate order of execution of cooperative processes.
In order to synchronize the processes, there are various synchronization mechanisms.
Process Synchronization is mainly needed in a multi-process system when multiple processes are running together, and
more than one process try to gain access to the same shared resource or any data at the same time.
Race Condition
At the time when more than one process is either executing the same code or accessing the same memory or any shared
variable; in that condition, there is a possibility that the output or the value of the shared variable is wrong so for that
purpose all the processes are doing the race to say that my output is correct. This condition is commonly known as a
1
race condition. As several processes access and process the manipulations on the same data in a concurrent manner
0
and due to which the outcome depends on the particular order in which the access of data takes place.
Mainly this condition is a situation that may occur inside the critical section. Race condition in the critical section
happens when the result of multiple thread execution differs according to the order in which the threads execute. But
this condition is critical sections can be avoided if the critical section is treated as an atomic instruction. Proper thread
synchronization using locks or atomic variables can also prevent race conditions.
Entry Section
In this section mainly the process requests for its entry in the critical section.
Exit Section
This section is followed by the critical section. A solution to the critical section problem must satisfy the following
three conditions:
1. Mutual Exclusion
Out of a group of cooperating processes, only one process can be in its critical section at a given point of time.
2. Progress
If no process is in its critical section, and if one or more threads want to execute their critical section then any one of
these threads must be allowed to get into its critical section.
3. Bounded Waiting
After a process makes a request for getting into its critical section, there is a limit for how many other processes can get
into their critical section, before this process's request is granted. So after the limit is reached, the system must grant
the process permission to get into its critical section.
Synchronization Hardware
Many systems provide hardware support for critical section code. The critical section problem could be solved easily in
a single-processor environment if we could disallow interrupts to occur while a shared variable or resource is being
modified.
In this manner, we could be sure that the current sequence of instructions would be allowed to execute in order without
pre-emption. Unfortunately, this solution is not feasible in a multiprocessor environment.
Disabling interrupt on a multiprocessor environment can be time-consuming as the message is passed to all the
processors.
This message transmission lag delays the entry of threads into the critical section, and the system efficiency decreases.
Mutex Locks
As the synchronization hardware solution is not easy to implement for everyone, a strict software approach called
Mutex Locks was introduced. In this approach, in the entry section of code, a LOCK is acquired over the critical
resources modified and used inside the critical section, and in the exit section that LOCK is released.
As the resource is locked while a process executes its critical section hence no other process can access it.
Paging:
Paging is a method or technique which is used for non-contiguous memory allocation. It is a fixed-size partitioning
theme (scheme). In paging, both main memory and secondary memory are divided into equal fixed-size partitions.
The partitions of the secondary memory area unit and main memory area unit are known as pages and frames
respectively.
Paging is a memory management method accustomed fetch processes from the secondary memory into the main
memory in the form of pages. in paging, each process is split into parts wherever the size of every part is the same
1
as the page size. The size of the last half could also be but the page size. The pages of the process area unit hold on
2
within the frames of main memory relying upon their accessibility.
Segmentation:
Segmentation is another non-contiguous memory allocation scheme like paging. like paging, in segmentation, the
process isn’t divided indiscriminately into mounted(fixed) size pages. It is a variable-size partitioning theme. like
paging, in segmentation, secondary and main memory are not divided into partitions of equal size. The partitions of
secondary memory area units are known as segments. The details concerning every segment are hold in a table
known as segmentation table. Segment table contains two main data concerning segment, one is Base, which is the
bottom address of the segment and another is Limit, which is the length of the segment.
In segmentation, the CPU generates a logical address that contains the Segment number and segment offset. If the
segment offset is a smaller amount than the limit then the address called valid address otherwise it throws
miscalculation because the address is invalid.
The above figure shows the translation of a logical address to a physical address.
S.N
O Paging Segmentation
In paging, the program is divided into fixed or In segmentation, the program is divided into variable size
1. mounted size pages. sections.
2. For the paging operating system is accountable. For segmentation compiler is accountable.
3. Page size is determined by hardware. Here, the section size is given by the user.
1
S.N 3
O Paging Segmentation
5. Paging could result in internal fragmentation. Segmentation could result in external fragmentation.
In paging, the logical address is split into a page Here, the logical address is split into section number and
6. number and page offset. section offset.
Paging comprises a page table that encloses the While segmentation also comprises the segment table
7. base address of every page. which encloses segment number and segment offset.
In paging, the operating system must maintain a In segmentation, the operating system maintains a list of
9. free frame list. holes in the main memory.
In paging, the processor needs the page number, In segmentation, the processor uses segment number, and
11. and offset to calculate the absolute address. offset to calculate the full address.
14. This protection is hard to apply. Easy to apply for protection in segmentation.
17. Paging results in a less efficient system. Segmentation results in a more efficient system.
VIRTUAL MEMORY
Virtual Memory is a storage scheme that provides user an illusion of having a very big main memory. This is done by
treating a part of secondary memory as the main memory.
1
In this scheme, User can load the bigger size processes than the available main memory by having the illusion that the
4
memory is available to load the process. Instead of loading one big process in the main memory, the Operating System
loads the different parts of more than one process in the main memory.
By doing this, the degree of multiprogramming will be increased and therefore, the CPU utilization will also be
increased.
In modern word, virtual memory has become quite common these days. In this scheme, whenever some pages needs to
be loaded in the main memory for the execution and the memory is not available for those many pages, then in that
case, instead of stopping the pages from entering in the main memory, the OS search for the RAM area that are least
used in the recent times or that are not referenced and copy that into the secondary memory to make the space for the
new pages in the main memory.
Since all this procedure happens automatically, therefore it makes the computer feel like it is having the unlimited
RAM.
Demand Paging
Demand Paging is a popular method of virtual memory management. In demand paging, the pages of a process which
are least used, get stored in the secondary memory.
A page is copied to the main memory when its demand is made or page fault occurs. There are various page
replacement algorithms which are used to determine the pages which will be replaced. We will discuss each one of
them later in detail.
IN OTHER WORDS
Demand Paging:
The process of loading the page into memory on demand (whenever page fault occurs) is known as demand
paging.
The process includes the following steps :
1. If the CPU tries to refer to a page that is currently not available in the main memory, it generates an interrupt
indicating a memory access fault.
2. The OS puts the interrupted process in a blocking state. For the execution to proceed the OS must bring the
required page into the memory.
3. The OS will search for the required page in the logical address space.
4. The required page will be brought from logical address space to physical address space. The page replacement
algorithms are used for the decision-making of replacing the page in physical address space.
5. The page table will be updated accordingly.
6. The signal will be sent to the CPU to continue the program execution and it will place the process back into the
ready state.
Hence whenever a page fault occurs these steps are followed by the operating system and the required page is
brought into memory.
1
Advantages :
5
More processes may be maintained in the main memory: Because we are going to load only some of the pages of
any particular process, there is room for more processes. This leads to more efficient utilization of the processor
because it is more likely that at least one of the more numerous processes will be in the ready state at any particular
time.
A process may be larger than all of the main memory: One of the most fundamental restrictions in programming is
lifted. A process larger than the main memory can be executed because of demand paging. The OS itself loads
pages of a process in the main memory as required.
It allows greater multiprogramming levels by using less of the available (primary) memory for each process.
Page Fault Service Time :
The time taken to service the page fault is called page fault service time. The page fault service time includes the
time taken to perform all the above six steps.
Let Main memory access time is: m
Page fault service time is: s
Page fault rate is : p
Then, Effective memory access time = (p*s) + (1-p)*m
Swapping:
Swapping a process out means removing all of its pages from memory, or marking them so that they will be
removed by the normal page replacement process. Suspending a process ensures that it is not unable while it is
swapped out. At some later time, the system swaps back the process from the secondary storage to the main
memory. When a process is busy swapping pages in and out then this situation is called thrashing.
Thrashing :
1
At any given time, only a few pages of any process are in the main memory and therefore more processes can be
6
maintained in memory. Furthermore, time is saved because unused pages are not swapped in and out of memory.
However, the OS must be clever about how it manages this scheme. In the steady-state practically, all of the main
memory will be occupied with process pages, so that the processor and OS have direct access to as many processes
as possible. Thus when the OS brings one page in, it must throw another out. If it throws out a page just before it is
used, then it will just have to get that page again almost immediately. Too much of this leads to a condition called
Thrashing. The system spends most of its time swapping pages rather than executing instructions. So a good page
replacement algorithm is required.
In the given diagram, the initial degree of multiprogramming up to some extent of point(lambda), the CPU
utilization is very high and the system resources are utilized 100%. But if we further increase the degree of
multiprogramming the CPU utilization will drastically fall down and the system will spend more time only on the
page replacement and the time is taken to complete the execution of the process will increase. This situation in the
system is called thrashing.
Causes of Thrashing :
1. High degree of multiprogramming : If the number of processes keeps on increasing in the memory then the
number of frames allocated to each process will be decreased. So, fewer frames will be available for each process.
Due to this, a page fault will occur more frequently and more CPU time will be wasted in just swapping in and out
of pages and the utilization will keep on decreasing.
For example:
Let free frames = 400
Case 1: Number of process = 100
Then, each process will get 4 frames.
Case 2: Number of processes = 400
Each process will get 1 frame.
Case 2 is a condition of thrashing, as the number of processes is increased, frames per process are decreased.
Hence CPU time will be consumed in just swapping pages.
2. Lacks of Frames: If a process has fewer frames then fewer pages of that process will be able to reside in memory
and hence more frequent swapping in and out will be required. This may lead to thrashing. Hence sufficient amount
of frames must be allocated to each process in order to prevent thrashing.
Recovery of Thrashing :
Do not allow the system to go into thrashing by instructing the long-term scheduler not to bring the processes into
memory after the threshold.
If the system is already thrashing then instruct the mid-term scheduler to suspend some of the processes so that we
can recover the system from thrashing.
Snapshot of a virtual memory management system
Let us assume 2 processes, P1 and P2, contains 4 pages each. Each page size is 1 KB. The main memory contains 8
frame of 1 KB each. The OS resides in the first two partitions. In the third partition, 1 st page of P1 is stored and the
other frames are also shown as filled with the different pages of processes in the main memory.
The page tables of both the pages are 1 KB size each and therefore they can be fit in one frame each. The page tables
of both the processes contain various information that is also shown in the image.
The CPU contains a register which contains the base address of page table that is 5 in the case of P1 and 7 in the case
of P2. This page table base address will be added to the page number of the Logical address when it comes to accessing
the actual corresponding entry.
1
7
NFA
One important thing to note is, in NFA, if any path for an input string leads to a final state, then the input
string is accepted. For example, in the above NFA, there are multiple paths for the input string “00”. Since one of
the paths leads to a final state, “00” is accepted by the above NFA.
Some Important Points:
Justification:
Since all the tuples in DFA and NFA are the same except for one of the tuples, which is Transition Function (δ)
In case of DFA
δ : Q X Σ --> Q
In case of NFA
δ : Q X Σ --> 2Q
Now if you observe you’ll find out Q X Σ –> Q is part of Q X Σ –> 2 Q.
On the RHS side, Q is the subset of 2 Q which indicates Q is contained in 2 Q or Q is a part of 2Q, however, the
reverse isn’t true. So mathematically, we can conclude that every DFA is NFA but not vice-versa. Yet there is a
way to convert an NFA to DFA, so there exists an equivalent DFA for every NFA.
1. Both NFA and DFA have the same power and each NFA can be translated into a DFA.
2. There can be multiple final states in both DFA and NFA.
3. NFA is more of a theoretical concept.
4. DFA is used in Lexical Analysis in Compiler.
5. If the number of states in the NFA is N then, its DFA can have maximum 2 N number of states.
Finite Automata Model:
Finite automata can be represented by input tape and finite control.
Input tape: It is a linear tape having some number of cells. Each input symbol is placed in each cell.
Finite control: The finite control decides the next state on receiving particular input from input tape. The tape reader
reads the cells one by one from left to right, and at a time only one input symbol is read.
2
0
Types of Automata:
1. DFA
DFA refers to deterministic finite automata. Deterministic refers to the uniqueness of the computation. In the DFA, the
machine goes to one state only for a particular input character. DFA does not accept the null move.
2. NFA
NFA stands for non-deterministic finite automata. It is used to transmit any number of states for a particular input. It
can accept the null move.
A {[Tex]epsilon [/Tex]}
B [Tex]phi [/Tex]
C a*
D {[Tex]epsilon [/Tex],a}
Question 1 ANSWER(A):
L1 L2* U L1* Result of L1 L2* is .{ } indicates an empty language. Concatenation of with any other
language is . It works as 0 in multiplication. L1* = * which is { }. Union of and { } is {
}
2
QQuestion 2 1
A deterministic finite automation (DFA)D with alphabet {a,b} is given below
Which of the following finite state machines is a valid minimal DFA which accepts the same language as
D?
AA
BB
CC
DD
Question 2 Explanation:
Options (B) and (C) are invalid because they both accept ‘b’ as a string which is not accepted by give DFA. (D) is
invalid because it accepts "bba" which are not accepted by given DFA.
QQuestion 3
Let w be any string of length n is {0,1}*. Let L be the set of all substrings of w. What is the minimum number of states
in a non-deterministic finite automaton that accepts L?
A n-1
Bn
C n+1
D 2n-1
Question 3 Explanation:
We need minimum n+1 states to build NFA that accepts all substrings of a binary string.
2
2
For example, following NFA accepts all substrings of “010″ and it has 4 states.
QQuestion 4
Which one of the following languages over the alphabet {0,1} is described by the regular expression:
(0+1)*0(0+1)*0(0+1)* ?
A The set of all strings containing the substring 00.
B The set of all strings containing at most two 0’s.
C The set of all strings containing at least two 0’s.
D The set of all strings that begin and end with either 0 or 1.
Question 4 Explanations:
The regular expression has two 0′s surrounded by (0+1)* which means accepted strings must have at least 2 0′s. The
least possible string is ε 0 ε 0 ε = 00 the set of strings accepted is = {00, 000, 100, 0010, 0000, 00100, 1001001,} We
can see from the set of accepted strings that the entire have at least two zeros which is the least possible string. So
option (C) is correct.
QQuestion 5
Which one of the following is FALSE?
A There is unique minimal DFA for every regular language
B Every NFA can be converted to an equivalent PDA.
C Complement of every context-free language is recursive.
D Every nondeterministic PDA can be converted to an equivalent deterministic PDA.
Question 5 Explanations:
Power of Deterministic PDA is not same as the power of Non-deterministic PDA. Deterministic PDA cannot handle
languages or grammars with ambiguity, but NDPDA can handle languages with ambiguity and any context-free
grammar. So every non-deterministic PDA cannot be converted to an equivalent deterministic PDA.
Regular Expressions
Regular Expressions are used to denote regular languages. An expression is regular if:
ɸ is a regular expression for regular language ɸ.
ɛ is a regular expression for regular language {ɛ}.
If a ∈ Σ (Σ represents the input alphabet), a is regular expression with language {a}.
If a and b are regular expression, a + b is also a regular expression with language {a,b}.
If a and b are regular expression, ab (concatenation of a and b) is also regular.
If a is regular expression, a* (0 or more times a) is also regular.
Regular Expression Regular Languages
any no. of vowels followed by v*.c* ( where v – vowels { ε , a ,aou, aiou, b, abcd…..} where ε represent
any no. of consonants and c – consonants) empty string (in case 0 vowels and o consonants )
2
3
Regular Grammar : A grammar is regular if it has rules of form A -> a or A -> aB or A -> ɛ where ɛ is a special
symbol called NULL.
Regular Languages : A language is regular if it can be expressed in terms of regular expression.
Closure Properties of Regular Languages
Union : If L1 and If L2 are two regular languages, their union L1 ∪ L2 will also be regular. For example, L1 =
{an | n ≥ 0} and L2 = {bn | n ≥ 0}
L3 = L1 ∪ L2 = {an ∪ bn | n ≥ 0} is also regular.
Intersection : If L1 and If L2 are two regular languages, their intersection L1 ∩ L2 will also be regular. For
example,
L1= {am bn | n ≥ 0 and m ≥ 0} and L2= {a m bn ∪ bn am | n ≥ 0 and m ≥ 0}
L3 = L1 ∩ L2 = {am bn | n ≥ 0 and m ≥ 0} is also regular.
Concatenation : If L1 and If L2 are two regular languages, their concatenation L1.L2 will also be regular. For
example,
L1 = {an | n ≥ 0} and L2 = {bn | n ≥ 0}
L3 = L1.L2 = {am . bn | m ≥ 0 and n ≥ 0} is also regular.
Kleene Closure : If L1 is a regular language, its Kleene closure L1* will also be regular. For example,
L1 = (a ∪ b)
L1* = (a ∪ b)*
Complement : If L(G) is regular language, its complement L’(G) will also be regular. Complement of a language
can be found by subtracting strings which are in L(G) from all possible strings. For example,
L(G) = {an | n > 3}
L’(G) = {an | n <= 3}
Note : Two regular expressions are equivalent if languages generated by them are same. For example, (a+b*)* and
(a+b)* generate same language. Every string which is generated by (a+b*)* is also generated by (a+b)* and vice
versa.
Solution : Option A says that it must have substring 00. But 10101 is also a part of language but it does not contain
00 as substring. So it is not correct option.
Option B says that it can have maximum two 0’s but 00000 is also a part of language. So it is not correct option.
Option C says that it must contain atleast two 0. In regular expression, two 0 are present. So this is correct option.
Option D says that it contains all strings that begin and end with either 0 or 1. But it can generate strings which
start with 0 and end with 1 or vice versa as well. So it is not correct.
Solution : Two regular expressions are equivalent if languages generated by them are same.
Option (A) can generate all strings generated by 0*(10*)*. So they are equivalent.
Option (B) string null cannot generated by given languages but 0*(10*)* can. So they are not equivalent.
Option (C) will have 10 as substring but 0*(10*)* may or may not. So they are not equivalent.
Question 4 : The regular expression for the language having input alphabets a and b, in which two a’s do not come
together:
(A) (b + ab)* + (b +ab)*a
(B) a(b + ba)* + (b + ba)*
(C) both options (A) and (B)
(D) none of the above
Solution:
Option (C) stating both both options (A) and (B) is the correct regular expression for the stated question.
The language in the question can be expressed as L={&epsilon,a,b,bb,ab,aba,ba,bab,baba,abab,…}.
In option (A) ‘ab’ is considered the building block for finding out the required regular expression.(b + ab)* covers
all cases of strings generated ending with ‘b’.(b + ab)*a covers all cases of strings generated ending with a.
Identities Related to Regular Expressions
Given R, P, L, Q as regular expressions, the following identities hold –
Properties of Regular Sets
Property 1. The union of two regular set is regular.
Proof −
Let us take two regular expressions
RE1 = a(aa)* and RE2 = (aa)*
So, L1 = {a, aaa, aaaaa,.....} (Strings of odd length excluding Null)
and L2 ={ ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)
L1 ∪ L2 = { ε, a, aa, aaa, aaaa, aaaaa, aaaaaa,.......}
(Strings of all possible lengths including Null)
RE (L1 ∪ L2) = a* (which is a regular expression itself)
Hence, proved.
Property 2. The intersection of two regular set is regular.
Proof −
Let us take two regular expressions
RE1 = a(a*) and RE2 = (aa)*
So, L1 = { a,aa, aaa, aaaa, ....} (Strings of all possible lengths excluding Null)
L2 = { ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)
L1 ∩ L2 = { aa, aaaa, aaaaaa,.......} (Strings of even length excluding Null)
RE (L1 ∩ L2) = aa(aa)* which is a regular expression itself.
Hence, proved.
Property 3. The complement of a regular set is regular.
Proof −
Let us take a regular expression −
RE = (aa)*
2
So, L = {ε, aa, aaaa, aaaaaa, .......} (Strings of even length including Null)
5
Complement of L is all the strings that is not in L.
So, L’ = {a, aaa, aaaaa, .....} (Strings of odd length excluding Null)
RE (L’) = a(aa)* which is a regular expression itself.
Hence, proved.
Property 4. The difference of two regular set is regular.
Proof −
Let us take two regular expressions −
RE1 = a (a*) and RE2 = (aa)*
So, L1 = {a, aa, aaa, aaaa, ....} (Strings of all possible lengths excluding Null)
L2 = { ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)
L1 – L2 = {a, aaa, aaaaa, aaaaaaa, ....}
(Strings of all odd lengths excluding Null)
RE (L1 – L2) = a (aa)* which is a regular expression.
Hence, proved.
Property 5. The reversal of a regular set is regular.
Proof −
We have to prove LR is also regular if L is a regular set.
Let, L = {01, 10, 11, 10}
RE (L) = 01 + 10 + 11 + 10
LR = {10, 01, 11, 01}
RE (LR) = 01 + 10 + 11 + 10 which is regular
Hence, proved.
Property 6. The closure of a regular set is regular.
Proof −
If L = {a, aaa, aaaaa, .......} (Strings of odd length excluding Null)
i.e., RE (L) = a (aa)*
L* = {a, aa, aaa, aaaa , aaaaa,……………} (Strings of all lengths excluding Null)
RE (L*) = a (a)*
Hence, proved.
Property 7. The concatenation of two regular sets is regular.
Proof −
Let RE1 = (0+1)*0 and RE2 = 01(0+1)*
Here, L1 = {0, 00, 10, 000, 010, ......} (Set of strings ending in 0)
and L2 = {01, 010,011,.....} (Set of strings beginning with 01)
Then, L1 L2 = {001,0010,0011,0001,00010,00011,1001,10010,.............}
Set of strings containing 001 as a substring which can be represented by an RE − (0 + 1)*001(0 + 1)*
Hence, proved.
∅* = ε
2
ε* = ε
6
RR* = R*R
R*R* = R*
(R*)* = R*
RR* = R*R
(PQ)*P =P(QP)*
(a+b)* = (a*b*)* = (a*+b*)* = (a+b*)* = a*(ba*)*
R + ∅ = ∅ + R = R (The identity for union)
R ε = ε R = R (The identity for concatenation)
∅ L = L ∅ = ∅ (The annihilator for concatenation)
R + R = R (Idempotent law)
L (M + N) = LM + LN (Left distributive law)
(M + N) L = ML + NL (Right distributive law)
n order to find out a regular expression of a Finite Automaton, we use Arden’s Theorem along with the properties of
regular expressions.
Statement −
Let P and Q be two regular expressions.
If P does not contain null string, then R = Q + RP has a unique solution that is R = QP*
Proof −
R = Q + (Q + RP)P [After putting the value R = Q + RP]
= Q + QP + RPP
When we put the value of R recursively again and again, we get the following equation −
R = Q + QP + QP2 + QP3…..
R = Q (ε + P + P2 + P3 + …. )
R = QP* [As P* represents (ε + P + P2 + P3 + ….) ]
Hence, proved.
Assumptions for Applying Arden’s Theorem
The transition diagram must not have NULL transitions
It must have only one initial state
Method
Step 1 − Create equations as the following form for all the states of the DFA having n states with initial state q 1.
q1 = q1R11 + q2R21 + … + qnRn1 + ε
q2 = q1R12 + q2R22 + … + qnRn2
…………………………
…………………………
…………………………
…………………………
qn = q1R1n + q2R2n + … + qnRnn
Rij represents the set of labels of edges from qi to qj, if no such edge exists, then Rij = ∅
Step 2 − Solve these equations to get the equation for the final state in terms of Rij
Problem
Construct a regular expression corresponding to the automata given below −
2
7
Solution −
Here the initial state and final state is q1.
The equations for the three states q1, q2, and q3 are as follows −
q1 = q1a + q3a + ε (ε move is because q1 is the initial state0
q2 = q1b + q2b + q3b
q3 = q2a
Now, we will solve these three equations −
q2 = q1b + q2b + q3b
= q1b + q2b + (q2a)b (Substituting value of q3)
= q1b + q2(b + ab)
= q1b (b + ab)* (Applying Arden’s Theorem)
q1 = q1a + q3a + ε
= q1a + q2aa + ε (Substituting value of q3)
= q1a + q1b(b + ab*)aa + ε (Substituting value of q2)
= q1(a + b(b + ab)*aa) + ε
= ε (a+ b(b + ab)*aa)*
= (a + b(b + ab)*aa)*
Hence, the regular expression is (a + b(b + ab)*aa)*.
Problem
Construct a regular expression corresponding to the automata given below −
2
Solution −
8
Here the initial state is q1 and the final state is q2
Now we write down the equations −
q1 = q10 + ε
q2 = q11 + q20
q3 = q21 + q30 + q31
Now, we will solve these three equations −
q1 = ε0* [As, εR = R]
So, q1 = 0*
q2 = 0*1 + q20
So, q2 = 0*1(0)* [By Arden’s theorem]
Hence, the regular expression is 0*10*.
We can use Thompson's Construction to find out a Finite Automaton from a Regular Expression. We will reduce the
regular expression into smallest regular expressions and converting these to NFA and finally to DFA.
Some basic RA expressions are the following −
Case 1 − For a regular expression ‘a’, we can construct the following FA −
Method
Step 1 Construct an NFA with Null moves from the given regular expression.
2
Step 2 Remove Null transition from the NFA and convert it into its equivalent DFA.
9
Problem
Convert the following RA into its equivalent DFA − 1 (0 + 1)* 0
Solution
We will concatenate three expressions "1", "(0 + 1)*" and "0"
Now we will remove the ε transitions. After we remove the ε transitions from the NDFA, we get the following −
It is an NDFA corresponding to the RE − 1 (0 + 1)* 0. If you want to convert it into a DFA, simply apply the method
of converting NDFA to DFA discussed in Chapter 1.
Finite Automata with Null Moves (NFA-ε)
A Finite Automaton with null moves (FA-ε) does transit not only after giving input from the alphabet set but also
without any input symbol. This transition without input is called a null move.
An NFA-ε is represented formally by a 5-tuple (Q, ∑, δ, q0, F), consisting of
Q − a finite set of states
∑ − a finite set of input symbols
δ − a transition function δ : Q × (∑ ∪ {ε}) → 2Q
q0 − an initial state q0 ∈ Q
F − a set of final state/states of Q (F⊆Q).
Solution
Step 1 −
Here the ε transition is between q1 and q2, so let q1 is X and qf is Y.
Here the outgoing edges from qf is to qf for inputs 0 and 1.
Step 2 −
Now we will Copy all these edges from q1 without changing the edges from qf and get the following FA −
Step 3 −
Here q1 is an initial state, so we make qf also an initial state.
So the FA becomes −
Step 4 −
Here qf is a final state, so we make q1 also a final state.
So the FA becomes −
Context-Free Grammar
Definition − A context-free grammar (CFG) consisting of a finite set of grammar rules is a quadruple (N, T, P,
S) where
N is a set of non-terminal symbols.
T is a set of terminals where N ∩ T = NULL.
3
P is a set of rules, P: N → (N ∪ T)*, i.e., the left-hand side of the production rules P does have any right context or left
1
context.
S is the start symbol.
Example
The grammar ({A}, {a, b, c}, P, A), P : A → aA, A → abc.
The grammar ({S, a, b}, {a, b}, P, S), P: S → aSa, S → bSb, S → ε
The grammar ({S, F}, {0, 1}, P, S), P: S → 00S | 11F, F → 00F | ε
Generation of Derivation Tree
A derivation tree or parse tree is an ordered rooted tree that graphically represents the semantic information a string
derived from a context-free grammar.
Representation Technique
Root vertex − Must be labeled by the start symbol.
Vertex − Labeled by a non-terminal symbol.
Leaves − Labeled by a terminal symbol or ε.
If S → x1x2 …… xn is a production rule in a CFG, then the parse tree / derivation tree will be as follows −
If a partial derivation tree contains the root S, it is called a sentential form. The above sub-tree is also in sentential
form.
Leftmost and Rightmost Derivation of a String
Leftmost derivation − A leftmost derivation is obtained by applying production to the leftmost variable in each step.
Rightmost derivation − A rightmost derivation is obtained by applying production to the rightmost variable in each step.
Example
Let any set of production rules in a CFG be
X → X+X | X*X |X| a
over an alphabet {a}.
The leftmost derivation for the string "a+a*a" may be −
X → X+X → a+X → a + X*X → a+a*X → a+a*a
The stepwise derivation of the above string is shown as below −
3
3
Since there are two parse trees for a single string "a+a*a", the grammar G is ambiguous.
Context-free languages are closed under −
Union
Concatenation
Kleene Star operation
Union
Let L1 and L2 be two context free languages. Then L1 ∪ L2 is also context free.
Example
Let L1 = { anbn , n > 0}. Corresponding grammar G1 will have P: S1 → aAb|ab
Let L2 = { cmdm , m ≥ 0}. Corresponding grammar G2 will have P: S2 → cBb| ε
Union of L1 and L2, L = L1 ∪ L2 = { anbn } ∪ { cmdm }
The corresponding grammar G will have the additional production S → S1 | S2
Concatenation
If L1 and L2 are context free languages, then L1L2 is also context free.
Example
Union of the languages L1 and L2, L = L1L2 = { anbncmdm }
The corresponding grammar G will have the additional production S → S1 S2
Kleene Star
3
If L is a context free language, then L* is also context free.
5
Example
Let L = { anbn , n ≥ 0}. Corresponding grammar G will have P: S → aAb| ε
Kleene Star L1 = { anbn }*
The corresponding grammar G1 will have additional productions S1 → SS1 | ε
Context-free languages are not closed under −
Intersection − If L1 and L2 are context free languages, then L1 ∩ L2 is not necessarily context free.
Intersection with Regular Language − If L1 is a regular language and L2 is a context free language, then L1 ∩ L2 is
a context free language.
Complement − If L1 is a context free language, then L1’ may not be context free.
Finite control: The finite control has some pointer which points the current symbol which is to be read.
Stack: The stack is a structure in which we can push and remove the items from one end only. It has an infinite size. In
PDA, the stack is used to store the items temporarily.
Formal definition of PDA:
The PDA can be defined as a collection of 7 components:
Now when we read b, we will change the state from q0 to q1 and start popping corresponding 'a'. Hence,
1. δ(q0, b, a) = (q1, ε)
Thus this process of popping 'b' will be repeated unless all the symbols are read. Note that popping action occurs in
state q1 only.
1. δ(q1, b, a) = (q1, ε)
After reading all b's, all the corresponding a's should get popped. Hence when we read ε as input symbol then there
should be nothing in the stack. Hence the move will be:
1. δ(q1, ε, Z) = (q2, ε)
Where
PDA = ({q0, q1, q2}, {a, b}, {a, Z}, δ, q0, Z, {q2})
Now we will simulate this PDA for the input string "aaabbbbbb".
1. δ(q0, aaabbbbbb, Z) ⊢ δ(q0, aabbbbbb, aaZ)
2. ⊢ δ(q0, abbbbbb, aaaaZ)
3. ⊢ δ(q0, bbbbbb, aaaaaaZ)
4. ⊢ δ(q1, bbbbb, aaaaaZ)
5. ⊢ δ(q1, bbbb, aaaaZ)
6. ⊢ δ(q1, bbb, aaaZ)
7. ⊢ δ(q1, bb, aaZ)
8. ⊢ δ(q1, b, aZ)
9. ⊢ δ(q1, ε, Z)
10. ⊢ δ(q2, ε)
11. ACCEPT
Example 2:
Push all 0's onto the stack on encountering first 0's. Then if we read 1, just do nothing. Then read 0, and on each read
of 0, pop one 0 from the stack.
For instance:
3
7
Now we will simulate this PDA for the input string "0011100".
1. δ(q0, 0011100, Z) ⊢ δ(q0, 011100, 0Z)
2. ⊢ δ(q0, 11100, 00Z)
3. ⊢ δ(q0, 1100, 00Z)
4. ⊢ δ(q1, 100, 00Z)
5. ⊢ δ(q1, 00, 00Z)
6. ⊢ δ(q1, 0, 0Z)
7. ⊢ δ(q1, ε, Z)
8. ⊢ δ(q2, Z)
9. ACCEPT
We have already discussed finite automata. But finite automata can be used to accept only regular languages.
Pushdown Automata is a finite automaton with extra memory called stack which helps Pushdown automata to
recognize Context Free Languages.
Turnstile notation
3
⊢ sign is called a “turnstile notation” and represents one move.
8
⊢* sign represents a sequence of moves. Eg- (p, b, T) ⊢ (q, w, α)
This implies that while taking a transition from state p to state q, the input symbol ‘b’ is consumed, and the top of the
stack ‘T’ is replaced by a new string ‘α’
Example: Define the pushdown automata for language {anbn | n > 0}
Solution: M = where Q = { q0, q1 } and Σ = { a, b } and Γ = { A, Z } and δ is given by :
δ( q0, a, Z ) = { ( q0, AZ ) }
δ( q0, a, A) = { ( q0, AA ) }
δ( q0, b, A) = { ( q1, ∈) }
δ( q1, b, A) = { ( q1, ∈) }
δ( q1, ∈, Z) = { ( q1, ∈) }
Explanation : Initially, the state of automata is q0 and symbol on stack is Z and the input is aaabbb as shown in row 1.
On reading ‘a’ (shown in bold in row 2), the state will remain q0 and it will push symbol A on stack. On next ‘a’
(shown in row 3), it will push another symbol A on stack. After reading 3 a’s, the stack will be AAAZ with A on the
top. After reading ‘b’ (as shown in row 5), it will pop A and move to state q1 and stack will be AAZ. When all b’s are
read, the state will be q1 and stack will be Z. In row 8, on input symbol ‘∈’ and Z on stack, it will pop Z and stack will
be empty. This type of acceptance is known as acceptance by empty stack.
Note:
The above pushdown automaton is deterministic in nature because there is only one move from a state on an input
symbol and stack symbol.
The non-deterministic pushdown automata can have more than one move from a state on an input symbol and stack
symbol.
It is not always possible to convert non-deterministic pushdown automata to deterministic pushdown automata.
3
The expressive power of non-deterministic PDA is more as compared to expressive deterministic PDA as some
9
languages are accepted by NPDA but not by deterministic PDA which will be discussed in the next article.
The pushdown automata can either be implemented using acceptance by empty stack or acceptance by final state and
one can be converted to another.
TURING MACHINE
A Turing machine is a computational model, like Finite Automata (FA), Pushdown automata (PDA), which works on
unrestricted grammar. The Turing machine is the most powerful computation model when compared with FA and
PDA.
Formally, a Turing machine M can be defined as follows −
M = (Q, X, ∑, δ, q0, B, F)
Q represents the finite, non-empty set of states.
X represents the set of tape alphabets.
∑ represents the non-empty set of input alphabets.
δ is the transition function, whose mapping is given as, δ : Q x X → Q x X x {Left_shift, Right_shift}.
q0 is the initial state of the machine
B is the blank symbol
F is the set of final states or halt states.
A single tape Turing machine has a single infinite tape, which is divided into cells.
The tape symbols are present in these cells.
A finite control is present, which controls the working of Turing machines based on the given input.
The Finite control has a Read/write head, which points to a cell in tape.
A Turing machine can move both left and right from one cell to another.
Input and output tape symbols
…... B X1 X2 … Xi ... Xn B B ...
↑
Finite control
A Turing can have three types of action upon an input.
Print Si, move one square to the left (L) and go to state qj.
Print Si, move one square to the right (R) and go to state qj.
Print Si, do not move (N) and go to state qj.
Illustration
Let us see how this turing machine works for 0011. Initially head points to 0 which is underlined and state is q0 as:
The move will be δ(q0, 0) = (q1, X, R). It means, it will go to state q1, replace 0 by X and head will move to right as:
The move will be δ(q1, 0) = (q1, 0, R) which means it will remain in same state and without changing any symbol, it
will move to right as:
The move will be δ(q1, 1) = (q2, Y, L) which means it will move to q2 state and changing 1 to Y, it will move to left
as:
4
Working on it in the same way, the machine will reach state q3 and head will point to B as shown:
1
Note:
In non-deterministic turing machine, there can be more than one possible move for a given state and tape symbol, but
non-deterministic TM does not add any power.
Every non-deterministic TM can be converted into deterministic TM.
In multi-tape turing machine, there can be more than one tape and corresponding head pointers, but it does not add any
power to turing machine.
Every multi-tape TM can be converted into single tape TM.
Question: A single tape Turing Machine M has two states q0 and q1, of which q0 is the starting state. The tape
alphabet of M is {0, 1, B} and its input alphabet is {0, 1}. The symbol B is the blank symbol used to indicate end of an
input string. The transition function of M is described in the following table.
The table is interpreted as illustrated below. The entry (q1, 1, R) in row q0 and column 1 signifies that if M is in state
q0 and reads 1 on the current tape square, then it writes 1 on the same tape square, moves its tape head one position to
the right and transitions to state q1. Which of the following statements is true about M?
1. M does not halt on any string in (0 + 1)+
2. M does not halt on any string in (00 + 1)*
3. M halts on all string ending in a 0
4. M halts on all string ending in a 1
Solution: Let us see whether machine halts on string ‘1’. Initially state will be q0, head will point to 1 as:
Using δ(q0, 1) = (q1, 1, R), it will move to state q1 and head will move to right as:
Using δ(q1, B) = (q0, B, L), it will move to state q0 and head will move to left as:
It will run in the same way again and again and not halt.
Option D says M halts on all string ending with 1, but it is not halting for 1. So, option D is incorrect.
Let us see whether machine halts on string ‘0’. Initially state will be q0, head will point to 1 as:
Using δ(q0, 0) = (q1, 1, R), it will move to state q1 and head will move to right as:
4
2
Using δ(q1,B)=(q0,B,L), it will move to state q0 and head will move to left as:
It will run in the same way again and again and not halt.
Option C says M halts on all string ending with 0, but it is not halting for 0. So, option C is incorrect.
Option B says that TM does not halt for any string (00 + 1)*. But NULL string is a part of (00 + 1)* and TM will halt
for NULL string. For NULL string, tape will be,
Using δ(q0, B) = halt, TM will halt. As TM is halting for NULL, this option is also incorrect.
So, option (A) is correct.
This article is contributed by Sonal Tuteja. Please write comments if you find anything incorrect, or you want to share
more information about the topic discussed above
In computer science, there exist some problems whose solutions are not yet found, the problems are divided into
classes known as Complexity Classes. In complexity theory, a Complexity Class is a set of problems with related
complexity. These classes help scientists to groups problems based on how much time and space they require to solve
problems and verify the solutions. It is the branch of the theory of computation that deals with the resources required to
solve a problem.
The common resources are time and space, meaning how much time the algorithm takes to solve a problem and the
corresponding memory usage.
The time complexity of an algorithm is used to describe the number of steps required to solve a problem, but it can also
be used to describe how long it takes to verify the answer.
The space complexity of an algorithm describes how much memory is required for the algorithm to operate.
Complexity classes are useful in organizing similar types of problems.
Pictorial representation of all NP classes which includes NP, NP-hard, and NP-complete
NP Class
The NP in NP class stands for Non-deterministic Polynomial Time. It is the collection of decision problems that can
be solved by a non-deterministic machine in polynomial time.
Features:
1. The solutions of the NP class are hard to find since they are being solved by a non-deterministic machine but the
solutions are easy to verify.
2. Problems of NP can be verified by a Turing machine in polynomial time.
Example:
Let us consider an example to better understand the NP class. Suppose there is a company having a total of 1000
employees having unique employee IDs. Assume that there are 200 rooms available for them. A selection of 200
employees must be paired together, but the CEO of the company has the data of some employees who can’t work in
the same room due to some personal reasons.
This is an example of an NP problem. Since it is easy to check if the given choice of 200 employees proposed by a
coworker is satisfactory or not i.e. no pair taken from the coworker list appears on the list given by the CEO. But
generating such a list from scratch seems to be so hard as to be completely impractical.
It indicates that if someone can provide us with the solution to the problem, we can find the correct and incorrect pair
in polynomial time. Thus for the NP class problem, the answer is possible, which can be calculated in polynomial time.
This class contains many problems that one would like to be able to solve effectively:
1. Boolean Satisfiability Problem (SAT).
2. Hamiltonian Path Problem.
3. Graph coloring.
Co-NP Class
Co-NP stands for the complement of NP Class. It means if the answer to a problem in Co-NP is No, then there is proof
that can be checked in polynomial time.
Features:
1. If a problem X is in NP, then its complement X’ is also is in CoNP.
2. For an NP and CoNP problem, there is no need to verify all the answers at once in polynomial time, there is a need to
verify only one particular answer “yes” or “no” in polynomial time for a problem to be in NP or CoNP.
Some example problems for C0-NP are:
1. To check prime number.
2. Integer Factorization.
NP-hard class
An NP-hard problem is at least as hard as the hardest problem in NP and it is the class of the problems such that every
problem in NP reduces to NP-hard.
Features:
1. All NP-hard problems are not in NP.
4
2. It takes a long time to check them. This means if a solution for an NP-hard problem is given then it takes a long time to
4
check whether it is right or not.
3. A problem A is in NP-hard if, for every problem L in NP, there exists a polynomial-time reduction from L to A.
Some of the examples of problems in Np-hard are:
1. Halting problem.
2. Qualified Boolean formulas.
3. No Hamiltonian cycle.
NP-complete class
A problem is NP-complete if it is both NP and NP-hard. NP-complete problems are the hard problems in NP.
Features:
1. NP-complete problems are special as any problem in NP class can be transformed or reduced into NP-complete
problems in polynomial time.
2. If one could solve an NP-complete problem in polynomial time, then one could also solve any NP problem in
polynomial time.
Some example problems include:
1. Decision version of 0/1 Knapsack.
2. Hamiltonian Cycle.
3. Satisfiability.
4. Vertex cover.
Complexity
Class Characteristic feature
All NP-hard problems are not in NP and it takes a long time to check
NP-hard them.
A problem is in the class NPC if it is in NP and is as hard as any problem in NP. A problem is NP-hard if all
problems in NP are polynomial time reducible to it, even though it may not be in NP itself.
If a polynomial time algorithm exists for any of these problems, all problems in NP would be polynomial time
solvable. These problems are called NP-complete. The phenomenon of NP-completeness is important for both
theoretical and practical reasons.
Definition of NP-Completeness
A language B is NP-complete if it satisfies two conditions
B is in NP
Every A in NP is polynomial time reducible to B.
If a language satisfies the second property, but not necessarily the first one, the language B is known as NP-Hard.
Informally, a search problem B is NP-Hard if there exists some NP-Complete problem A that Turing reduces to B.
4
The problem in NP-Hard cannot be solved in polynomial time, until P = NP. If a problem is proved to be NPC, there is
5
no need to waste time on trying to find an efficient algorithm for it. Instead, we can focus on design approximation
algorithm.
NP-Complete Problems
Following are some NP-Complete problems, for which no polynomial time algorithm is known.
Determining whether a graph has a Hamiltonian cycle
Determining whether a Boolean formula is satisfiable, etc.
NP-Hard Problems
The following problems are NP-Hard
The circuit-satisfiability problem
Set Cover
Vertex Cover
Travelling Salesman Problem
In this context, now we will discuss TSP is NP-Complete
TSP is NP-Complete
The traveling salesman problem consists of a salesman and a set of cities. The salesman has to visit each one of the
cities starting from a certain one and returning to the same city. The challenge of the problem is that the traveling
salesman wants to minimize the total length of the trip
Relation of P and NP classes
1. P contains in NP
2. P=NP
1. Observe that P contains in NP. In other words, if we can solve a problem in polynomial time, we can indeed verify the
solution in polynomial time. More formally, we do not need to see a certificate (there is no need to specify the
vertex/intermediate of the specific path) to solve the problem; we can explain it in polynomial time anyway.
2. However, it is not known whether P = NP. It seems you can verify and produce an output of the set of decision-based
problems in NP classes in a polynomial time which is impossible because according to the definition of NP classes you
can verify the solution within the polynomial time. So this relation can never be held.
Reductions:
The class NP-complete (NPC) problems consist of a set of decision problems (a subset of class NP) that no one knows
how to solve efficiently. But if there were a polynomial solution for even a single NP-complete problem, then every
problem in NPC will be solvable in polynomial time. For this, we need the concept of reductions.
Suppose there are two problems, A and B. You know that it is impossible to solve problem A in polynomial time. You
want to prove that B cannot be explained in polynomial time. We want to show that (A ∉ P) => (B ∉ P)
Consider an example to illustrate reduction: The following problem is well-known to be NPC:
3-color: Given a graph G, can each of its vertices be labeled with one of 3 different colors such that two adjacent
vertices do not have the same label (color).
Coloring arises in various partitioning issues where there is a constraint that two objects cannot be assigned to the same
set of partitions. The phrase "coloring" comes from the original application which was in map drawing. Two countries
that contribute a common border should be colored with different colors.
It is well known that planar graphs can be colored (maps) with four colors. There exists a polynomial time algorithm
for this. But deciding whether this can be done with 3 colors is hard, and there is no polynomial time algorithm for it.
NP-Completeness
4
A decision problem L is NP-Hard if
6
L' ≤p L for all L' ϵ NP.
Definition: L is NP-complete if
1. L ϵ NP and
2. L' ≤ p L for some known NP-complete problem L.' Given this formal definition, the complexity classes are: