0% found this document useful (0 votes)
30 views46 pages

FOUNDATION COMPUTER SCIENCE (Mtech)

The document discusses various process scheduling algorithms used by operating systems: 1) It describes six common algorithms: first come first served, shortest job first, priority scheduling, shortest remaining time, round robin, and multiple-level queues scheduling. 2) It provides examples of how to calculate waiting times for sample processes under each algorithm. 3) The algorithms can be preemptive, allowing a process to be interrupted, or non-preemptive, only completing a process once started. Shortest job first and priority scheduling are given as examples of non-preemptive algorithms.

Uploaded by

Ravi Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views46 pages

FOUNDATION COMPUTER SCIENCE (Mtech)

The document discusses various process scheduling algorithms used by operating systems: 1) It describes six common algorithms: first come first served, shortest job first, priority scheduling, shortest remaining time, round robin, and multiple-level queues scheduling. 2) It provides examples of how to calculate waiting times for sample processes under each algorithm. 3) The algorithms can be preemptive, allowing a process to be interrupted, or non-preemptive, only completing a process once started. Shortest job first and priority scheduling are given as examples of non-preemptive algorithms.

Uploaded by

Ravi Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

1

FOUNDATION COMPUTER SCIENCE (MTCS 101)


UNIT – I (DS OF BTECH) & UNIT –IV (DBMS OF BTECH)

UNIT II (Scheduling Algorithms in Operating System)


There are various algorithms which are used by the Operating System to schedule the processes on the processor in an
efficient way.
The Purpose of a Scheduling algorithm
1. Maximum CPU utilization
2. Fare allocation of CPU
3. Maximum throughput
4. Minimum turnaround time
5. Minimum waiting time
6. Minimum response time

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.

3. Shortest Job First


The job with the shortest burst time will get the CPU first. The lesser the burst time, the sooner will the process get the
CPU. It is the non-preemptive type of scheduling.

4. Shortest remaining time first


It is the preemptive form of SJF. In this algorithm, the OS schedules the Job according to the remaining time of the
execution.

5. Priority based scheduling


In this algorithm, the priority will be assigned to each of the processes. The higher the priority, the sooner will the
process get the CPU. If the priority of the two processes is same then they will be scheduled according to their arrival
time.

6. Highest Response Ratio Next


In this scheduling Algorithm, the process with highest response ratio will be scheduled next. This reduces the
starvation in the system.
A Process Scheduler schedules different processes to be assigned to the CPU based on particular scheduling
algorithms. There are six popular process scheduling algorithms which we are going to discuss in this chapter −
 First-Come, First-Served (FCFS) Scheduling
 Shortest-Job-Next (SJN) Scheduling
 Priority Scheduling
 Shortest Remaining Time
 Round Robin(RR) Scheduling
 Multiple-Level Queues Scheduling
These algorithms are either non-preemptive or preemptive. Non-preemptive algorithms are designed so that once a
process enters the running state; it cannot be preempted until it completes its allotted time, whereas the preemptive
scheduling is based on priority where a scheduler may preempt a low priority running process anytime when a high
priority process enters into a ready state.

First Come First Serve (FCFS)


2
 Jobs are executed on first come, first serve basis.
 It is a non-preemptive, pre-emptive scheduling algorithm.
 Easy to understand and implement.
 Its implementation is based on FIFO queue.
 Poor in performance as average wait time is high.

Wait time of each process is as follows −


Process Wait Time : Service Time - Arrival Time

P0 0-0=0

P1 5-1=4

P2 8-2=6

P3 16 - 3 = 13

Average Wait Time: (0+4+6+13) / 4 = 5.75


Shortest Job Next (SJN)
 This is also known as shortest job first, or SJF
 This is a non-preemptive, pre-emptive scheduling algorithm.
 Best approach to minimize waiting time.
 Easy to implement in Batch systems where required CPU time is known in advance.
 Impossible to implement in interactive systems where required CPU time is not known.
 The processer should know in advance how much time process will take.
Given: Table of processes, and their Arrival time, Execution time
Process Arrival Time Execution Time Service Time

P0 0 5 0

P1 1 3 5

P2 2 8 14

P3 3 6 8
3

Waiting time of each process is as follows −


Process Waiting Time

P0 0-0=0

P1 5-1=4

P2 14 - 2 = 12

P3 8-3=5

Average Wait Time: (0 + 4 + 12 + 5)/4 = 21 / 4 = 5.25


Priority Based Scheduling
 Priority scheduling is a non-preemptive algorithm and one of the most common scheduling algorithms in batch
systems.
 Each process is assigned a priority. Process with highest priority is to be executed first and so on.
 Processes with same priority are executed on first come first served basis.
 Priority can be decided based on memory requirements, time requirements or any other resource requirement.
Given: Table of processes, and their Arrival time, Execution time, and priority. Here we are considering 1 is the lowest
priority.
Process Arrival Time Execution Time Priority Service Time

P0 0 5 1 0

P1 1 3 2 11

P2 2 8 1 14

P3 3 6 3 5
4

Waiting time of each process is as follows −


Process Waiting Time

P0 0-0=0

P1 11 - 1 = 10

P2 14 - 2 = 12

P3 5-3=2

Average Wait Time: (0 + 10 + 12 + 2)/4 = 24 / 4 = 6


Shortest Remaining Time
 Shortest remaining time (SRT) is the preemptive version of the SJN algorithm.
 The processor is allocated to the job closest to completion but it can be preempted by a newer ready job with shorter
time to completion.
 Impossible to implement in interactive systems where required CPU time is not known.
 It is often used in batch environments where short jobs need to give preference.
Round Robin Scheduling
 Round Robin is the preemptive process scheduling algorithm.
 Each process is provided a fix time to execute, it is called a quantum.
 Once a process is executed for a given time period, it is preempted and other process executes for a given time period.
 Context switching is used to save states of preempted processes.

Wait time of each process is as follows −


Process Wait Time : Service Time - Arrival Time

P0 (0 - 0) + (12 - 3) = 9

P1 (3 - 1) = 2

P2 (6 - 2) + (14 - 9) + (20 - 17) = 12

P3 (9 - 3) + (17 - 12) = 11

Average Wait Time: (9+2+12+11) / 4 = 8.5


Multiple-Level Queues Scheduling
5
Multiple-level queues are not an independent scheduling algorithm. They make use of other existing algorithms to
group and schedule jobs with common characteristics.
 Multiple queues are maintained for processes with common characteristics.
 Each queue can have its own scheduling algorithms.
 Priorities are assigned to each queue.
For example, CPU-bound jobs can be scheduled in one queue and all I/O-bound jobs in another queue. The Process
Scheduler then alternately selects jobs from each queue and assigns them to the CPU based on the algorithm assigned
to the queue.
What is Thread?
A thread is a flow of execution through the process code, with its own program counter that keeps track of which
instruction to execute next, system registers which hold its current working variables, and a stack which contains the
execution history.
A thread shares with its peer threads little information like code segment, data segment and open files. When one
thread alters a code segment memory item, all other threads see that.
A thread is also called a lightweight process. Threads provide a way to improve application performance through
parallelism. Threads represent a software approach to improving performance of operating system by reducing the
overhead thread is equivalent to a classical process.
Each thread belongs to exactly one process and no thread can exist outside a process. Each thread represents a separate
flow of control. Threads have been successfully used in implementing network servers and web server. They also
provide a suitable foundation for parallel execution of applications on shared memory multiprocessors. The following
figure shows the working of a single-threaded and a multithreaded process.

Difference between Process and Thread


S.N. Process Thread

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.

Many to One Model


Many-to-one model maps many user level threads to one Kernel-level thread. Thread management is done in user
space by the thread library. When thread makes a blocking system call, the entire process will be blocked. Only one
thread can access the Kernel at a time, so multiple threads are unable to run in parallel on multiprocessors.If the user-
level thread libraries are implemented in the operating system in such a way that the system does not support them,
then the Kernel threads use the many-to-one relationship modes.
8

One to One Model


There is one-to-one relationship of user-level thread to the kernel-level thread. This model provides more concurrency
than the many-to-one model. It also allows another thread to run when a thread makes a blocking system call. It
supports multiple threads to execute in parallel on microprocessors.
Disadvantage of this model is that creating user thread requires the corresponding Kernel thread. OS/2, windows NT
and windows 2000 use one to one relationship model.

Difference between User-Level & Kernel-Level Thread


S.N. User-Level Threads Kernel-Level Thread

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.

4 Multi-threaded applications cannot take advantage of Kernel routines themselves can be


multiprocessing. multithreaded.

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.

Critical Section Problem


A Critical Section is a code segment that accesses shared variables and has to be executed as an atomic action. It means
that in a group of cooperating processes, at a given point of time, only one process must be executing its critical
section. If any other process also wants to execute its critical section, it must wait until the first one finishes. The entry
to the critical section is mainly handled by wait() function while the exit from the critical section is controlled by
the signal() function.

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.

Solutions for the Critical Section


The critical section plays an important role in Process Synchronization so that the problem must be solved.
Some widely used method to solve the critical section problem is as follows:
1. Peterson’s Solution
This is widely used and software-based solution to critical section problems. Peterson's solution was developed by a
computer scientist Peterson that's why it is named so.
1
With the help of this solution whenever a process is executing in any critical state, then the other process only executes
1
the rest of the code, and vice-versa can happen. This method also helps to make sure of the thing that only a single
process can run in the critical section at a specific time.
This solution preserves all three conditions:
 Mutual Exclusion is comforted as at any time only one process can access the critical section.
 Progress is also comforted, as a process that is outside the critical section is unable to block other processes from
entering into the critical section.
 Bounded Waiting is assured as every process gets a fair chance to enter the Critical section.

The above shows the structure of process Pi in Peterson's solution.


 Suppose there are N processes (P1, P2, ... PN) and as at some point of time every process requires to enter in
the Critical Section
 A FLAG[] array of size N is maintained here which is by default false. Whenever a process requires to enter in the
critical section, it has to set its flag as true. Example: If Pi wants to enter it will set FLAG[i]=TRUE.
 Another variable is called TURN and is used to indicate the process number that is currently waiting to enter into the
critical section.
 The process that enters into the critical section while exiting would change the TURN to another number from the list
of processes that are ready.
 Example: If the turn is 3 then P3 enters the Critical section and while exiting turn=4 and therefore P4 breaks out of the
wait loop.

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

4. It is faster in comparison to segmentation. Segmentation is slow.

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.

The page table is employed to keep up the page


8. data. Section Table maintains the section data.

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.

10. Paging is invisible to the user. Segmentation is visible to the user.

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.

It is hard to allow sharing of procedures between


12. processes. Facilitates sharing of procedures between the processes.

In paging, a programmer cannot efficiently handle


13 data structure. It can efficiently handle data structures.

14. This protection is hard to apply. Easy to apply for protection in segmentation.

The size of the page needs always be equal to the


15. size of frames. There is no constraint on the size of segments.

A page is referred to as a physical unit of


16. information. A segment is referred to as a logical unit of information.

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

Advantages of Virtual Memory


1. The degree of Multiprogramming will be increased.
2. User can run large application with less real RAM.
3. There is no need to buy more memory RAMs.
Disadvantages of Virtual Memory
1. The system becomes slower since swapping takes time.
2. It takes more time in switching between applications.
3. The user will have the lesser hard disk space for its use.
1
UNIT-III: AUTOMATA THEORY
8
Finite Automata(FA) is the simplest machine to recognize patterns. The finite automata or finite state machine is an
abstract machine that has five elements or tuples. It has a set of states and rules for moving from one state to
another but it depends upon the applied input symbol. Basically, it is an abstract model of a digital computer. The
following figure shows some essential features of general automation.

Figure: Features of Finite Automata


The above figure shows the following features of automata:
1. Input
2. Output
3. States of automata
4. State relation
5. Output relation
A Finite Automata consists of the following:
Q : Finite set of states.
Σ : set of Input Symbols.
q : Initial state.
F : set of Final States.
δ : Transition Function.
Formal specification of machine is
{ Q, Σ, q, F, δ }
FA is characterized into two types:
1) Deterministic Finite Automata (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.
DFA consists of 5 tuples {Q, Σ, q, F, δ}.
Q : set of all states.
Σ : set of input symbols. ( Symbols which machine takes as input )
q : Initial state. ( Starting state of a machine )
F : set of final state.
δ : Transition Function, defined as δ : Q X Σ --> Q.
In a DFA, for a particular input character, the machine goes to one state only. A transition function is defined on
every state for every input symbol. Also in DFA null (or ε) move is not allowed, i.e., DFA cannot change state
without any input character.
For example, below DFA with Σ = {0, 1} accepts all strings ending with 0.
1
9

Figure: DFA with Σ = {0, 1}


One important thing to note is, there can be many possible DFAs for a pattern. A DFA with a minimum number
of states is generally preferred.
2) Nondeterministic Finite Automata (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.
NFA is similar to DFA except following additional features:
1. Null (or ε) move is allowed i.e., it can move forward without reading symbols.
2. Ability to transmit to any number of states for a particular input.
However, these above features don’t add any power to NFA. If we compare both in terms of power, both are
equivalent.
Due to the above additional features, NFA has a different transition function, the rest is the same as DFA.
δ: Transition Function
δ: Q X (Σ U ε ) --> 2 ^ Q.
As you can see in the transition function is for any input including null (or ε), NFA can go to any state number of
states. For example, below is an NFA for the above problem.

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:

There are two types of finite automata:


1. DFA(deterministic finite automata)
2. NFA(non-deterministic finite 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.

Some important points about DFA and NFA:


1. Every DFA is NFA, but NFA is not DFA.
2. There can be multiple final states in both NFA and DFA.
3. DFA is used in Lexical Analysis in Compiler.
4. NFA is more of a theoretical concept.
NUMERICAL OF AUTOMETA
Question 1
Consider the languages L1 = and L2 = {a}. Which one of the following represents L1 L2* U L1*

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

set of vovels (a∪e∪i∪o∪u) {a, e, i, o, u}

a followed by 0 or more b (a.b*) {a, ab, abb, abbb, abbbb,….}

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.

How to solve problems on regular expression and regular languages?


Question 1 : 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.

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.

Question 2 : Which of the following languages is generated by given grammar?


S -> aS | bS | ∊
(A) {an bm | n,m ≥ 0}
(B) {w ∈ {a,b}* | w has equal number of a’s and b’s}
(C) {an | n ≥ 0} ∪ {bn | n ≥ 0} ∪ {an bn | n ≥ 0}
(D) {a,b}*
Solution : Option (A) says that it will have 0 or more a followed by 0 or more b. But S -> bS => baS => ba is also
a part of language. So (A) is not correct.
Option (B) says that it will have equal no. of a’s and b’s. But But S -> bS => b is also a part of language. So (B) is
not correct.
Option (C) says either it will have 0 or more a’s or 0 or more b’s or a’s followed by b’s. But as shown in option
(A), ba is also part of language. So (C) is not correct.
Option (D) says it can have any number of a’s and any numbers of b’s in any order. So (D) is correct.

Question 3 : The regular expression 0*(10*)* denotes the same set as


2
(A) (1*0)*1*
4
(B) 0 + (0 + 10)*
(C) (0 + 1)* 10(0 + 1)*
(D) none of these

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 −

Case 2 − For a regular expression ‘ab’, we can construct the following FA −

Case 3 − For a regular expression (a+b), we can construct the following FA −

Case 4 − For a regular expression (a+b)*, 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).

The above (FA-ε) accepts a string set − {0, 1, and 01}


Removal of Null Moves from Finite Automata
If in an NDFA, there is ϵ-move between vertex X to vertex Y, we can remove it using the following steps −
 Find all the outgoing edges from Y.
 Copy all these edges starting from X without changing the edge labels.
 If X is an initial state, make Y also an initial state.
 If Y is a final state, make X also a final state.
Problem
Convert the following NFA-ε to NFA without Null move.
3
0

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 −

There are two different approaches to draw a derivation tree −


Top-down Approach −
 Starts with the starting symbol S
 Goes down to tree leaves using productions
Bottom-up Approach −
 Starts from tree leaves
 Proceeds upward to the root which is the starting symbol S
Derivation or Yield of a Tree
The derivation or the yield of a parse tree is the final string obtained by concatenating the labels of the leaves of the
tree from left to right, ignoring the Nulls. However, if all the leaves are Null, derivation is Null.
Example
Let a CFG {N,T,P,S} be
N = {S}, T = {a, b}, Starting symbol = S, P = S → SS | aSb | ε
One derivation from the above CFG is “abaabb”
S → SS → aSbS → abS → abaSb → abaaSbb → abaabb
3
2

Sentential Form and Partial Derivation Tree


A partial derivation tree is a sub-tree of a derivation tree/parse tree such that either all of its children are in the sub-tree
or none of them are in the sub-tree.
Example
If in any CFG the productions are −
S → AB, A → aaA | ε, B → Bb| ε
the partial derivation tree can be the following −

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

The rightmost derivation for the above string "a+a*a" may be −


X → X*X → X*a → X+X*a → X+a*a → a+a*a
The stepwise derivation of the above string is shown as below −

Left and Right Recursive Grammars


In a context-free grammar G, if there is a production in the form X → Xa where X is a non-terminal and ‘a’ is a string
of terminals, it is called a left recursive production. The grammar having a left recursive production is called a left
recursive grammar.
And if in a context-free grammar G, if there is a production is in the form X → aX where X is a non-terminal
and ‘a’ is a string of terminals, it is called a right recursive production. The grammar having a right recursive
production is called a right recursive grammar.
Ambiguity in Context-Free Grammars
3
If a context free grammar G has more than one derivation tree for some string w ∈ L(G), it is called an ambiguous
4
grammar. There exist multiple right-most or left-most derivations for some string generated from that grammar.
Problem
Check whether the grammar G with production rules −
X → X+X | X*X |X| a
Is ambiguous or not.
Solution
Let’s find out the derivation tree for the string "a+a*a". It has two leftmost derivations.
Derivation 1 − X → X+X → a +X → a+ X*X → a+a*X → a+a*a
Parse tree 1 −

Derivation 2 − X → X*X → X+X*X → a+ X*X → a+a*X → a+a*a


Parse tree 2 −

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.

Introduction of Pushdown Automata


Pushdown Automata (PDA)
o Pushdown automata are a way to implement a CFG in the same way we design DFA for a regular grammar. A DFA
can remember a finite amount of information, but a PDA can remember an infinite amount of information.
o Pushdown automata are simply an NFA augmented with an "external stack memory". The addition of stack is used to
provide a last-in-first-out memory management capability to Pushdown automata. Pushdown automata can store an
unbounded amount of information on the stack. It can access a limited amount of information on the stack. A PDA can
push an element onto the top of the stack and pop off an element from the top of the stack. To read an element into the
stack, the top elements must be popped off and are lost.
o A PDA is more powerful than FA. Any language which can be acceptable by FA can also be acceptable by PDA. PDA
also accepts a class of language which even cannot be accepted by FA. Thus PDA is much more superior to FA.
PDA Components:
Input tape: The input tape is divided in many cells or symbols. The input head is read-only and may only move from
left to right, one symbol at a time.

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:

Q: the finite set of states


∑: the input set
Γ: a stack symbol which can be pushed and popped from the stack
q0: the initial state
Z: a start symbol which is in Γ.
F: a set of final states
δ: mapping function which is used for moving from current state to next state.
Instantaneous Description (ID)
ID is an informal notation of how a PDA computes an input string and make a decision that string is accepted or
rejected.

An instantaneous description is a triple (q, w, α) where:


q describes the current state.
w describes the remaining input.
α describes the stack contents, top at the left.
Turnstile Notation:
⊢ sign describes the turnstile notation and represents one move.
⊢* sign describes a sequence of moves.
For example,
(p, b, T) ⊢ (q, w, α)
In the above example, while taking a transition from state p to q, the input symbol 'b' is consumed, and the top of the
stack 'T' is represented by a new string α.
Example 1:
Design a PDA for accepting a language {anb2n | n>=1}.
3
Solution: In this language, n number of a's should be followed by 2n number of b's. Hence, we will apply a very
6
simple logic, and that is if we read single 'a', we will push two a's onto the stack. As soon as we read 'b' then for every
single 'b' only one 'a' should get popped from the stack.

The ID can be constructed as follows:


1. δ(q0, a, Z) = (q0, aaZ)
2. δ(q0, a, a) = (q0, aaa)

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})

We can summarize the ID as:


1. δ(q0, a, Z) = (q0, aaZ)
2. δ(q0, a, a) = (q0, aaa)
3. δ(q0, b, a) = (q1, ε)
4. δ(q1, b, a) = (q1, ε)
5. δ(q1, ε, 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:

Design a PDA for accepting a language {0n1m0n | m, n>=1}.


Solution: In this PDA, n number of 0's are followed by any number of 1's followed n number of 0's. Hence the logic
for design of such PDA will be as follows:

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

This scenario can be written in the ID form as:


1. δ(q0, 0, Z) = δ(q0, 0Z)
2. δ(q0, 0, 0) = δ(q0, 00)
3. δ(q0, 1, 0) = δ(q1, 0)
4. δ(q0, 1, 0) = δ(q1, 0)
5. δ(q1, 0, 0) = δ(q1, ε)
6. δ(q0, ε, Z) = δ(q2, Z) (ACCEPT state)

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.

A Pushdown Automata (PDA) can be defined as:


 Q is the set of states
 ∑is the set of input symbols
 Γ is the set of pushdown symbols (which can be pushed and popped from stack)
 q0 is the initial state
 Z is the initial pushdown symbol (which is initially present in stack)
 F is the set of final states
 δ is a transition function which maps Q x {Σ ∪ ∈} x Γ into Q x Γ*. In a given state, PDA will read input symbol and
stack symbol (top of the stack) and move to a new state and change the symbol of stack.
Instantaneous Description (ID)
Instantaneous Description (ID) is an informal notation of how a PDA “computes” a input string and make a decision
that string is accepted or rejected.
A ID is a triple (q, w, α), where:
1. q is the current state.
2. w is the remaining input.
3.α is the stack contents, top at the left.

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, ∈) }

Let us see how this automata works for aaabbb.

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.

Push Down Automata State Diagram:

State Diagram for above Push Down Automata

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.

Question: Which of the following pairs have DIFFERENT expressive power?


A. Deterministic finite automata(DFA) and Non-deterministic finite automata(NFA)
B. Deterministic push down automata(DPDA)and Non-deterministic push down automata(NPDA)
C. Deterministic single-tape Turing machine and Non-deterministic single-tape Turing machine
D. Single-tape Turing machine and the multi-tape Turing machine
Solution : Every NFA can be converted into DFA. So, there expressive power is same. As discussed above, every
NPDA can’t be converted to DPDA. So, the power of NPDA and DPDA is not the same. Hence option (B) is correct.

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.

Turing Machine in TOC


Turing Machine was invented by Alan Turing in 1936 and it is used to accept Recursive Enumerable Languages
(generated by Type-0 Grammar).
A turing machine consists of a tape of infinite length on which read and writes operation can be performed. The tape
consists of infinite cells on which each cell either contains input symbol or a special symbol called blank. It also
consists of a head pointer which points to cell currently being read and it can move in both directions.

Figure: Turing Machine


4
A TM is expressed as a 7-tuple (Q, T, B, ∑, δ, q0, F) where:
0
 Q is a finite set of states
 T is the tape alphabet (symbols which can be written on Tape)
 B is blank symbol (every cell is filled with B except input alphabet initially)
 ∑ is the input alphabet (symbols which are part of input alphabet)
 δ is a transition function which maps Q × T → Q × T × {L,R}. Depending on its present state and present tape alphabet
(pointed by head pointer), it will move to new state, change the tape symbol (may or may not) and move head pointer
to either left or right.
 q0 is the initial state
 F is the set of final states. If any state of F is reached, input string is accepted.
Let us construct a turing machine for L={0^n1^n|n>=1}

 Q = {q0,q1,q2,q3} where q0 is initial state.


 T = {0,1,X,Y,B} where B represents blank.
 ∑ = {0,1}
 F = {q3}
Transition function δ is given in Table 1 as:

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

Using move δ(q3, B) = halt, it will stop and accepted.

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

Types of Complexity Classes | P, NP, CoNP, NP hard and NP complete

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.

Types of Complexity Classes

This article discusses the following complexity classes:


1. P Class
2. NP Class
3. CoNP Class
4. NP hard
5. NP complete
P Class
The P in the P class stands for Polynomial Time. It is the collection of decision problems (problems with a “yes” or
“no” answer) that can be solved by a deterministic machine in polynomial time.
Features:
1. The solution to P problems is easy to find.
2. P is often a class of computational problems that are solvable and tractable. Tractable means that the problems can be
solved in theory as well as in practice. But the problems that can be solved in theory but not in practice are known as
intractable.
This class contains many natural problems like:
1. Calculating the greatest common divisor.
2. Finding a maximum matching.
3. Decision versions of linear programming.
4
Definition of NP-complete class: - A problem is in NP-complete, if
3
1. It is in NP
2. It is NP-hard

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

P Easily solvable in polynomial time.

NP Yes, answers can be checked in polynomial time.

Co-NP No, answers can be checked in polynomial time.

All NP-hard problems are not in NP and it takes a long time to check
NP-hard them.

NP-complete A problem that is NP and NP-hard is NP-complete.

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.

Fig: Example of 3-colorable and non-3-colorable graphs.


Polynomial Time Reduction:
We say that Decision Problem L1 is Polynomial time Reducible to decision Problem L 2 (L1≤p L2) if there is a
polynomial time computation function f such that of all x, xϵL1 if and only if xϵL2.

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:

P: is the set of decision problems that are solvable in polynomial time.


NP: is the set of decision problems that can be verified in polynomial time.
NP-Hard: L is NP-hard if for all L' ϵ NP, L' ≤p L. Thus if we can solve L in polynomial time, we can solve all NP
problems in polynomial time.
NP-Complete L is NP-complete if
1. L ϵ NP and
2. L is NP-hard
If any NP-complete problem is solvable in polynomial time, then every NP-Complete problem is also solvable in
polynomial time. Conversely, if we can prove that any NP-Complete problem cannot be solved in polynomial time,
every NP-Complete problem cannot be solvable in polynomial time.
Reductions
Concept: - If the solution of NPC problem does not exist then the conversion from one NPC problem to another NPC
problem within the polynomial time. For this, you need the concept of reduction. If a solution of the one NPC problem
exists within the polynomial time, then the rest of the problem can also give the solution in polynomial time (but it's
hard to believe). For this, you need the concept of reduction.
Example: - 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 solved in polynomial time. So you can convert the
problem A into problem B in polynomial time.
Example of NP-Complete problem
NP problem: - Suppose a DECISION-BASED problem is provided in which a set of inputs/high inputs you can get
high output.

Criteria to come either in NP-hard or NP-complete.


1. The point to be noted here, the output is already given, and you can verify the output/solution within the polynomial
time but can't produce an output/solution in polynomial time.
2. Here we need the concept of reduction because when you can't produce an output of the problem according to the
given input then in case you have to use an emphasis on the concept of reduction in which you can convert one
problem into another problem.
Note1:- If you satisfy both points then your problem comes into the category of NP-complete class
Note2:- If you satisfy the only 2nd points then your problem comes into the category of NP-hard class
So according to the given decision-based NP problem, you can decide in the form of yes or no. If, yes then you have to
do verify and convert into another problem via reduction concept. If you are being performed, both then decision-based
NP problems are in NP competing.

Here we will emphasize NPC.

You might also like