0% found this document useful (0 votes)
4 views41 pages

Unit 4

The document outlines the syllabus for a B.Tech course on Analysis of Algorithms, covering topics such as algorithm complexity, divide and conquer methods, greedy algorithms, dynamic programming, and randomized algorithms. It includes specific algorithms like Binary Search, Merge Sort, and the Hungarian Method for assignment problems, as well as discussions on NP-Complete problems and flow shop scheduling. The course aims to equip students with the knowledge to analyze and implement various algorithms effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views41 pages

Unit 4

The document outlines the syllabus for a B.Tech course on Analysis of Algorithms, covering topics such as algorithm complexity, divide and conquer methods, greedy algorithms, dynamic programming, and randomized algorithms. It includes specific algorithms like Binary Search, Merge Sort, and the Hungarian Method for assignment problems, as well as discussions on NP-Complete problems and flow shop scheduling. The course aims to equip students with the knowledge to analyze and implement various algorithms effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

GEETANJALI INSTITUTE OF TECHNICAL STUDIES

Department of Computer Science & Engineering

SUBJECT : ANALYSIS OF ALGORITHMS

B.TECH V SEM
BY:
Somya Agrawal
Assistant Professor ,CSE

Somya Agrawal ,Assistant Professor ,CSE 1


SYLLABUS

SN Contents Hours
1 Introduction: Objective, scope and outcome of the course. 1
Background: Review of Algorithm, Complexity Order Notations: definitions and calculating complexity.
2 6
Divide And Conquer Method: Binary Search, Merge Sort, Quick sort and Strassen's matrix multiplication algorithms.

Greedy Method: Knapsack Problem, Job Sequencing, Optimal Merge Patterns and Minimal Spanning Trees.
3 10
Dynamic Programming: Matrix Chain Multiplication. Longest CommonSubsequence and 0/1 Knapsack Problem.

Branch And Bound: Traveling Salesman Problem and Lower Bound Theory. Backtracking Algorithms and queens problem.
4 8
Pattern Matching Algorithms: Naïve and Rabin Karp string matching algorithms, KMP Matcher and Boyer Moore Algorithms.

Assignment Problems: Formulation of Assignment and Quadratic Assignment Problem.


Randomized Algorithms: Las Vegas algorithms, Monte Carlo algorithms, randomized algorithm for Min-Cut, randomized
5 8
algorithm for 2-SAT. Problem definition of Multicommodity flow, Flow shop scheduling and Network capacity assignment
problems.

Problem Classes Np, Np-Hard And Np-Complete: Definitions of P, NP-Hard and NP-Complete Problems. Decision Problems.
6 Cook's Theorem. Proving NP-Complete Problems - Satisfiability problem and Vertex Cover Problem. Approximation 8
Algorithms for Vertex Cover and Set Cover Problem.

Somya Agrawal ,Assistant Professor ,CSE 2


UNIT-4

Branch And Bound: Traveling Salesman Problem and Lower Bound Theory. Backtracking
Algorithms and queens problem. Pattern Matching Algorithms: Naïve and Rabin Karp string
matching algorithms, KMP Matcher and Boyer Moore Algorithms.

Somya Agrawal ,Assistant Professor ,CSE 3


Assignment Problem
• The assignment problem is a special type of transportation problem, where the objective is to minimize the cost or time of
completing a number of jobs by a number of persons. Inother words, when the problem involves the allocation of n different
facilities to n different tasks, it is often termed asanassignment problem. The model's primary usefulness is for planning.
Thea ssignment problem also encompasses an important sub-class of so-called shortest (orlongest-) route models. The
assignment model is useful in solving problems such as, assignment of machines to jobs, assignment of salesmen o sales
territories, travelling sales manproblem, etc.

• It may be noted that with n facilities and n jobs, there are n! possible assignments.
One way of finding an optimal assignment is to write all the n! possible arrangements, evaluate their total cost, and select the
assignment with minimum cost. But, due to heavy computational burden, this method is not suitable.
This chapter concentrates on an efficient method for solving assignment problems that was developed by a Hungarian
mathematician, D. Konig.

Somya Agrawal ,Assistant Professor ,CSE 4


What an Assignment problem is?
• When we want to solve a linear programming problem with special characteristic such as a Square
Matrix (i.e. No. of destinations are same as no. of sources)

• When each source (resource facility) can be associated with one and only one job/destination (One
job to one machine)

• Association is to be done in such a way to maximize or minimize the total effectiveness.

Somya Agrawal ,Assistant Professor ,CSE 5


Example of an assignment
problem
• There are 4 jobs and four people .Consider the
assignment matrix given above

Somya Agrawal ,Assistant Professor ,CSE 6


Solution :

Somya Agrawal ,Assistant Professor ,CSE 7


Somya Agrawal ,Assistant Professor ,CSE 8
Somya Agrawal ,Assistant Professor ,CSE 9
Somya Agrawal ,Assistant Professor ,CSE 10
Somya Agrawal ,Assistant Professor ,CSE 11
Somya Agrawal ,Assistant Professor ,CSE 12
Somya Agrawal ,Assistant Professor ,CSE 13
Hungarian Method
• Balanced Assignment Problem

Assignment problem is said to be balanced if number of rows is equal to number of columns


otherwise it is said to be unbalanced assignment problem.

Somya Agrawal ,Assistant Professor ,CSE 14


Example

Somya Agrawal ,Assistant Professor ,CSE 15


Somya Agrawal ,Assistant Professor ,CSE 16
Somya Agrawal ,Assistant Professor ,CSE 17
Somya Agrawal ,Assistant Professor ,CSE 18
Example 2

Somya Agrawal ,Assistant Professor ,CSE 19


Somya Agrawal ,Assistant Professor ,CSE 20
Somya Agrawal ,Assistant Professor ,CSE 21
Somya Agrawal ,Assistant Professor ,CSE 22
Randomized Algorithms
• Randomized algorithm is a different design approach taken by the standard algorithms where few
random bits are added to a part of their logic. They are different from deterministic algorithms;
deterministic algorithms follow a definite procedure to get the same output every time an input is
passed where randomized algorithms produce a different output every time theyre executed.

Determinstic Algorithms

Somya Agrawal ,Assistant Professor ,CSE 23


• Unlike deterministic algorithms, randomized algorithms consider randomized bits of the logic
along with the input that in turn contribute towards obtaining the output.

Figure 2: Randomized Algorithm

Somya Agrawal ,Assistant Professor ,CSE 24


Classification of Randomized Algorithms
•Las Vegas − The Las Vegas method of randomized algorithms
never gives incorrect outputs, making the time constraint as the
random variable. For example, in string matching algorithms, las
vegas algorithms start from the beginning once they encounter an
error. This increases the probability of correctness. Eg.,
Randomized Quick Sort Algorithm.
•Monte Carlo − The Monte Carlo method of randomized
algorithms focuses on finishing the execution within the given
time constraint. Therefore, the running time of this method is
deterministic. For example, in string matching, if monte carlo
encounters an error, it restarts the algorithm from the same point.
Thus, saving time. Eg., Kargers Minimum Cut Algorithm

Somya Agrawal ,Assistant Professor ,CSE 25


Randomized Algorithm for Minimum Cuts
• Minimum Cut is the removal of minimum number of edges in a graph (directed or undirected)
such that the graph is divided into multiple separate graphs or disjoint set of vertices.

• Let us look at an example for a clearer understanding of disjoint sets achieved

Somya Agrawal ,Assistant Professor ,CSE 26


Flow Shop Scheduling
• low shop scheduling problem:

• In flow shop, m different machines should process n jobs. Each job contains exactly n operations.
The ith operation of the job must be executed on the ith machine. Operations within one job must be
performed in the specified order.

• The first operation gets executed on the first machine, then the second operation on the second
machine, and so on. Jobs can be executed in any order. The problem is to determine the optimal
such arrangement, i.e. the one with the shortest possible total job execution makespan.

• With two machines, problem can be solved in O(nlogn) time using Johnson’s algorithm. For more
than 2 machines, the problem is NP hard. The goal is to minimize the sum of completion time of
all jobs.
Somya Agrawal ,Assistant Professor ,CSE 27
• The flow shop contains n jobs simultaneously available at time zero and to be processed by two
machines arranged in series with unlimited storage in between them. The processing time of all
jobs are known with certainty.

• It is required to schedule n jobs on machines so as to minimize makespan. The Johnson’s rule for
scheduling jobs in two machine flow shop is given below: In an optimal schedule, job i precedes
job j if min{pi1,pj2} < min{pj1,pi2}. Where as, pi1 is the processing time of job i on machine 1 and

pi2 is the processing time of job i on machine 2. Similarly, p j1 and pj2 are processing times of job j
on machine 1 and machine 2 respectively.

• We can find the optimal schduling using dynamic programming.

Somya Agrawal ,Assistant Professor ,CSE 28


Somya Agrawal ,Assistant Professor ,CSE 29
Somya Agrawal ,Assistant Professor ,CSE 30
Randomized algoriathm for 2-SAT
• Randomized algorithm for 2-SAT involves starting with an arbitrary truth assignment and
repeatedly flipping the value of a variable in a randomly chosen unsatisfied clause until a
satisfying assignment is found or a maximum number of iterations is reached. This approach
utilizes a random walk on the solution space, where flipping a variable can either increase or
decrease the number of satisfied clauses. While it doesn't guarantee finding a solution if one exists,
it offers a good probability of success, particularly for instances with a high density of satisfying
assignments.

• The SAT problem, k-SAT problem is the problem of examining a given CNF (or k-CNF)
expression and deciding whether or not it has a satisfying assignment.

Somya Agrawal ,Assistant Professor ,CSE 31


• Example of a SAT question: (x1 ∨ x8 ∨ x¯6) ∧ (x¯4 ∨ x¯7) ∧ (x5 ∨ x7 ∨ x4 ∨ x2).

• ▶ For the formula above, easy to see there is a satisfying assignment (any with x1 = 1, x4 = 0, x2
= 1 would do).

• ▶ In general, the SAT problem is NP-complete (we believe there is no polynomial-time


algorithm). Example of a 2-SAT question: (x1 ∨ x¯2) ∧ (x¯1 ∨ x¯3) ∧ (x1 ∨ x2) ∧ (x4 ∨ x¯3) ∧
(x4 ∨ x¯1).

• ▶ There is a polynomial-time algorithm (either randomized, as we see today, or deterministic) to


solve 2-SAT.

• ▶ The 3-SAT problem, and k-SAT for all k > 3, are all NP-complete.

Somya Agrawal ,Assistant Professor ,CSE 32


2-SAT Randomized Algorithm
We will design a simple randomized algorithm for 2-SAT, and analyse its performance by analogy to
a Markov chain.
Algorithm 2SATRANDOM(n; C1 ∧ C2 ∧ . . . ∧ Cℓ)
1. Assign arbitrary values to each of the xi variables.
2. 2. t ← 0
3. 3. while (t < 2mn2 and some clause is unsatisfied) do
4. 4. Choose an arbitrary Ch from all unsatisfied clauses;
5. 5. Choose one of the 2 literals in Ch uniformly at random and flip the value of its variable;
6. 6. if (we end with a satisfying assignment) then
7. 7. return this assignment to the x1, . . . xn else
8. 8. return FAILED.

Somya Agrawal ,Assistant Professor ,CSE 33


• 2-SAT Randomized Algorithm –

• Analysis Consider an (unknown so far) satisfying assignment S ∈ {0, 1} n that makes our 2SAT
formula ϕ true (satisfies all the clauses).

• Our “measure of progress” will be the number of indices k such that xk = Sk , (x1, . . . , xn) being
the current assignment.

• We will analyse the expected number of steps before (x1, . . . , xn) becomes S.

• ▶ Note that if ϕ does not have any satisfying assignment, Algorithm 2SATRANDOM always returns
FAILED (as it should).

• ▶ Let’s first assume that the formula ϕ has some satisfying assignment.

• ▶ (x t 1 , . . . , x t n ) is the assignment at time step t.


Somya Agrawal ,Assistant Professor ,CSE 34
Network capacity assignment problems
• Ford-Fulkerson Algorithm for Maximum Flow Problem

• he Ford-Fulkerson algorithm is a widely used algorithm to solve the maximum flow problem in a
flow network. The maximum flow problem involves determining the maximum amount of flow
that can be sent from a source vertex to a sink vertex in a directed weighted graph, subject to
capacity constraints on the edges.

• The algorithm works by iteratively finding an augmenting path, which is a path from the source to
the sink in the residual graph, i.e., the graph obtained by subtracting the current flow from the
capacity of each edge. The algorithm then increases the flow along this path by the maximum
possible amount, which is the minimum capacity of the edges along the path.
Somya Agrawal ,Assistant Professor ,CSE 35
• Problem:

• Given a graph which represents a flow network where every edge has a capacity. Also, given two
vertices source 's' and sink 't' in the graph, find the maximum possible flow from s to t with the
following constraints:

• Flow on an edge doesn't exceed the given capacity of the edge.

• Incoming flow is equal to outgoing flow for every vertex except s and t.

Somya Agrawal ,Assistant Professor ,CSE 36


Step 1 Step 2

Somya Agrawal ,Assistant Professor ,CSE 37


Step 3 : Step 4:

Somya Agrawal ,Assistant Professor ,CSE 38


Step 5: Step 6:

Somya Agrawal ,Assistant Professor ,CSE 39


Somya Agrawal ,Assistant Professor ,CSE 40
Thank You!!!

Somya Agrawal ,Assistant Professor ,CSE 41

You might also like