Ada Unit-5 - 1553921856

You might also like

You are on page 1of 55

P-NP CLASS PROBLEM

SOLUTION OF
ALGORITHMS

NON
POLYNOMIAL
POLYNOMIAL
TIME
TIME
P CLASS PROBLEM
• A problem which can be solved on polynomial time is
known as P-class problem
• Example:- All sorting and searching algorithms
NP CLASS PROBLEM
• A problem which cannot be solved on polynomial
time but it is verified in polynomial time is known as
NP-class problem or Non Deterministic Polynomial
• Example:- Su-Do-Ku , Travelling Salesman
• The NP Class problem ,it is verified in polynomial
time
• The P Class problems ,not only it is solved in
polynomial time but it is verified in polynomial time .
NP Class P Class
NP
Hard to solve Easy to solve
& &
Easy to Verify P Easy to Verify
Exponential time Polynomial time

untractable
tractable
Reduction
Def. Problem X reduces to problem Y
if you can use an algorithm that solves Y to help solve X
Cost of solving X=M*(cost of solving Y)+ cost of
reduction.
Graph Traversal
• Graph traversal is a technique used for
searching vertex in a graph. The graph traversal
is also used to decide the order of vertices to be
visited in the search process. A graph traversal
finds the edges to be used in the search process
without creating loops. That means using graph
traversal we visit all the vertices of graph
without getting into looping path.
There are two graph traversal techniques
• DFS (Depth First Search)
• BFS (Breadth First Search)
DFS (Depth First Search)
• DFS traversal of a graph produces a spanning tree as
final result. Spanning Tree is a graph without loops.
We use Stack data structure with maximum size of
total number of vertices in the graph to implement
DFS traversal.
Steps to implement DFS traversal...
Step 1 - Define a Stack of size total number of vertices
in the graph.
Step 2 - Select any vertex as starting point for traversal.
Visit that vertex and push it on to the Stack.
Step 3 - Visit any one of the non-visited adjacent
vertices of a vertex which is at the top of stack and
push it on to the stack.
Step 4 - Repeat step 3 until there is no new vertex to be
visited from the vertex which is at the top of the
stack.
Example
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
BFS (Breadth First Search)
• BFS traversal of a graph produces a spanning tree as
final result. Spanning Tree is a graph without loops.
We use Queue data structure with maximum size of
total number of vertices in the graph to implement
BFS traversal.
Steps to implement BFS traversal
Step 1 - Define a Queue of size total number of vertices in the
graph.
Step 2 - Select any vertex as starting point for traversal. Visit
that vertex and insert it into the Queue.
Step 3 - Visit all the non-visited adjacent vertices of the vertex
which is at front of the Queue and insert them into the
Queue.
Step 4 - When there is no new vertex to be visited from the
vertex which is at front of the Queue then delete that
vertex.
Step 5 - Repeat steps 3 and 4 until queue becomes empty.
Step 6 - When queue becomes empty, then produce final
spanning tree by removing unused edges from the graph
Example
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Lower Bound Theory
Lower Bound Theory Concept is based upon the
calculation of minimum time that is required to
execute an algorithm is known as a lower bound
theory or Base Bound Theory.
Lower Bound Theory uses a number of
methods/techniques to find out the lower bound.
The techniques which are used by lower Bound Theory
are:
 Comparisons Trees.
 Oracle and adversary argument
 State Space Method
Comparison trees
• In a comparison sort, we use only comparisons
between elements to gain order information about
an input sequence (a1; a2......an).
• The Comparison based algorithm defines a decision
tree.
• Decision Tree: A decision tree is a full binary tree
that shows the comparisons between elements that
are executed by an appropriate sorting algorithm
operating on an input of a given size.
• Control, data movement, and all other conditions of
the algorithm are ignored.
Comparison trees
• In a decision tree, there will be an array of
length n.
• So, total leaves will be n! (I.e. total number of
comparisons)
• If tree height is h, then surely
• n! ≤2n (tree will be binary)
Example
• Taking an Example of comparing a1, a2, and a3.
• Left subtree will be true condition i.e. ai ≤ aj
• Right subtree will be false condition i.e. ai >aj
Example
Decision Tree for Binary search
Decision Tree for Binary search
Parallel Algorithm - Introduction
• An algorithm is a sequence of steps that take inputs
from the user and after some computation, produces
an output.
• A parallel algorithm is an algorithm that can execute
several instructions simultaneously on different
processing devices and then combine all the
individual outputs to produce the final result.
Types of Algorithms
• While designing an algorithm, we should consider the
architecture of computer on which the algorithm will
be executed.
• Depending on the architecture of computers, we have
two types of algorithms
• Sequential Algorithm − An algorithm in which some
consecutive steps of instructions are executed in a
chronological order to solve a problem.
• Parallel Algorithm − The problem is divided into sub-
problems and are executed in parallel to get individual
outputs. Later on, these individual outputs are
combined together to get the final desired output.
PRAM Models
• The model where multiple processors are attached
to single memory block is PRAM Model. This model
contains:
• Set of processors of similar type
• A common memory unit is shared by all the
processors. The shared memory unit is used for
processors for communication.
• The processors are connected with the single shared
memory by the memory access unit (MAU).
PRAM Models
PRAM Models
• Independent operations can be performed by any
number of processors in a particular unit of time. This
facilitating in accessing the same memory location by
different processors.
• This problem is solved by enforcing the following
constraints on PRAM model -
• Exclusive Read Exclusive Write (EREW) – No two
processors are allowed to read or write at the same time
from the same memory.
• Exclusive Read Concurrent Write (ERCW) – No two
processors are allowed to read, but are allowed to write
at the same time from the same memory location.
Memory update Operations
• Concurrent Read Exclusive Write (CREW) − All the
processors are allowed to read from the same
memory location at the same time, but are not
allowed to write to the same memory location at the
same time.
• Concurrent Read Concurrent Write (CRCW) − All the
processors are allowed to read from or write to the
same memory location at the same time.
PRAM Model
• PRAM can be implemented in many methods. Some
of the important methods are:
• Shared memory model
• Message passing model
• Data parallel model
Shared Memory Model
• In this model more emphasis is given on data
parallelism than on control parallelism.
• On different processors, multiple processes execute
independently and a common memory space is shared.
• The rest of the processors can view any changes in the
memory location.
• As the same memory location is accessed by multiple
processors it may lead to instances where the same
memory location is accessed by more than one
processor. One may read on the location and the other
may write on the location at a time thus creating
confusion.
• This can be avoided by implementing a control
mechanism called lock / semaphore.
Shared Memory Model
Message Passing Model
• The common parallel programming approach used in
the distributed memory systems is Message passing.
The parallelism is determined by the programmer.
• All the processors contain their own local memory
unit and use a communication network for
exchanging data.
Methods are used by the processors
for communication
Point-to-Point Communication
• The simplest form of message passing is point-to-point
communication. The sending processor sends the
message to the receiving processor by using different
modes of transfer -
• Synchronous mode – Once the confirmation is
received that the message is delivered, the next
message is sent, thus maintaining message sequence.
• Asynchronous mode – The receipt of the confirmation
that the message is received is not required for sending
the next message.

You might also like