5CS4-03 Analysis of Algorithm Loveleen-Kumar
5CS4-03 Analysis of Algorithm Loveleen-Kumar
A
Course File
on
(Analysis of Algorithms: 5CS4-05)
Programme: B. Tech
Semester: V
Session: 2021-22
Loveleen Kumar
(Assistant Professor)
CS
Page | 1
Swami Keshvanand Institute of Technology, Management &Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 2
Swami Keshvanand Institute of Technology, Management &Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
V1: Produce quality computer engineers trained in the latest tools and technologies.
V2: Be a leading department in the region and country by imparting in-depth knowledge to
the students in an emerging technologies in computer science & engineering.
Page | 3
Swami Keshvanand Institute of Technology, Management &Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Syllabus
III Year-V Semester: B.Tech. Computer Science and Engineering
5CS4-05: Analysis of Algorithms
Credit: 3 Max. Marks: 150(IA:30, ETE:120)
SN Contents Hours
1 Introduction: Objective, scope and outcome of the course 01
2 Background: Review of Algorithm, Complexity Order Notations: definitions and 06
calculating complexity.
Divide And Conquer Method: Binary Search, Merge Sort, Quick sort and Strassen's
matrix multiplication algorithms.
3 Greedy Method: Knapsack Problem, Job Sequencing, Optimal Merge Patterns and 10
Minimal Spanning Trees.
Dynamic Programming: Matrix Chain Multiplication. Longest Common
Subsequence and 0/1 Knapsack Problem.
4 Branch And Bound: Traveling Salesman Problem and Lower Bound Theory. 08
Backtracking Algorithms and queens problem.
Pattern Matching Algorithms: Naïve and Rabin Karp string matching algorithms,
KMP Matcher and Boyer Moore Algorithms.
5 Assignment Problems: Formulation of Assignment and Quadratic Assignment 08
Problem.
Randomized Algorithms- Las Vegas algorithms, Monte Carlo algorithms,
randomized algorithm for Min-Cut, randomized algorithm for 2- SAT. Problem
definition of Multicommodity flow, Flow shop scheduling and Network capacity
assignment problems
6 Problem Classes Np, Np-Hard And Np-Complete: Definitions of P, NP-Hard and 08
NP-Complete Problems. Decision Problems. Cook's Theorem. Proving NP- Complete
Problems - Satisfiability problem and Vertex Cover Problem. Approximation
Algorithms for Vertex Cover and Set Cover Problem.
Page | 4
Swami Keshvanand Institute of Technology, Management &Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Prerequisites
The analysis of algorithms, especially in the context of complexity theory in which you study the underlying
computational problem (if you're attempting to do something more substantial than "Big-Oh" notation), does
require a significant investment in time into graph theory and abstract algebra, all in addition to a huge dose of
innate cleverness.
Page | 5
Swami Keshvanand Institute of Technology, Management &Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
3. “Data Structures and Their Algorithms” by Harry R Lewis and Larry Denenberg
4. Computer Algorithms by Horowitz E., Sahni S., Rajasekaran S., Galgotia Publications, 2001
Reference Books:
3. “Data Structures and Algorithms Made Easy: Data Structures and Algorithmic Puzzles” by Narasimha
Karumanchi
Page | 6
Swami Keshvanand Institute of Technology, Management &Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Course Bloom
Course name Course Outcomes PO Indicators
Code Level
CO4:discuss randomized
algorithms for min-cut and 2- 1.1,1.2,2.3,2.4
5,6
PSO 1.1,1.2
SAT problem
Page | 7
Swami Keshvanand Institute of Technology, Management &Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
CO PO\PSO Mapping
PO PO PO PO PO PO PO PO PSO PSO PSO
PO6 PO7 PO8 PO9
1 2 3 4 5 10 11 12 1 2 3
CO1 2 1 3
CO2 3 2 2 3 3
CO3 2 2 3 3
CO4 2 1 3
CO5 3
Page | 8
Swami Keshvanand Institute of Technology, Management &Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
QUIZ
1.Consider a linked list of n elements. What is the time taken to insert an element after an element pointed by some
pointer?
A. O (1) B. O (n) C. O (log2 n) D .O (n log2 n)
2.An algorithm is made up of two independent time complexities f (n) and g (n). Then the
complexities of the algorithm is in the order of
A. f(n) x g(n) B. Max ( f(n),g(n)) C. Min (f(n),g(n)) D. f(n) + g(n)
Page | 9
Swami Keshvanand Institute of Technology, Management &Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
13. The sorting technique where array to be sorted is partitioned again and again in such a way that
all elements less than or equal to partitioning element appear before it and those which are greaterappear after it, is
called
A. Merge sort B. Quick sort C. Selection sort
D. None of these
15) Which of the following algorithm have same time complexity in Best, average andworst case:
a) Quick sort b) Merge sort c) Binary search d) all
16) Suppose the input array A[1…n] is already in sorted order (increasing or decreasing)
then it is _______ case situation for QUICKSORT algorithm
a) Best b) worst c) average d) may be best or worst
17) Which one of the following algorithm design techniques is used in Strassen‘smatrix multiplication algorithm?
(a) Dynamic programming (c) Greedy method
(b) Backtracking approach (d) D& C strategy
Page | 10
Swami Keshvanand Institute of Technology, Management &Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
COURSE PLAN
LECTURE NUMBER TOPICS TO BE COVERED
UNIT-1
1 Objective,Scope and Outcome of the Subject
UNIT-2
2 Review of Algorithm, Complexity Order Notations
3 Asymptotic Notations and Calculating Complexity
4 Complexity of Recurrence Relation
5 Complexity of Recurrence Relation cont..
6 Intro to Divide and Conquer, Binary Search
7 Merge Sort, Quick Sort
8 Quick Sort cont., Strassen Matrix Multiplication
UNIT-3
9 Intro. To Greedy method , Knapsack Problem
10 Job Sequencing
11 Optimal Merge Patterns
12 Minimal SpanningTrees
13 Intro. To Dynamic Programming, Matrix Chain Multiplication
14 Longest Common Subsequence
15 Longest Common Subsequence cont.
16 0/1 KnapsackProblem.
Unit-4
17 Intro. To Branch and Bound , Traveling Salesman Problem
18 Lower Bound Theory. Intro. To Backtracking Algorithms
19 Queens problem
20 Queens problem cont.
21 Pattern Matching, Naïve String Matching Algorithm
22 Rabin Karp string matching algorithms
23 KMP Matcher
24 Boyer MooreAlgorithms
UNIT-5
25 Formulation of Assignment and Quadratic Assignment Problem.
26 Las Vegas algorithms, Monte Carlo algorithms
27 Randomized algorithm for Min-Cut
28 Randomized algorithm for 2- SAT
29 Problem definition of Multicommodity flow
30 Flow shop scheduling
31 Flow shop Scheduling cont.
32 Network capacity assignment problems
UNIT-6
33 Definitions of P, NP-Hard and NP-Complete Problems
34 P, NP-Hard and NP-Complete Problems cont.
35 Decision Problems.
36 Cook's Theorem
37 Proving NP- Complete Problems - Satisfiability problem
38 Vertex Cover Problem
39 Approximation Algorithms
40 Approximation Algorithms for Vertex Cover andSet Cover Problem.
Page | 11
Swami Keshvanand Institute of Technology, Management &Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
COURSE COVERAGE
Lect.
Date Topic Covered
No.
1 Introduction to Analysis of Algorithm
2 Analysis of Algorithm, Space Complexity,Time Complexity
3 Time Complexity of Insertion Sort
4 Asymptotic Notations, Big Oh , Omega and theta notation
5 Recurrence Relation , Substitution Method,Iteration Method
6 Master’s Theorem , Recurrence Tree
7 Examples of Master’s Method , Example of Recurrence Tree
8 Introduction To Divide and Conquer
9 Practice Worksheet-1
10 Binary Search , Merge Sort
11 Quick Sort, Analysis of Quick Sort
12 Strassen’s Matrix Multiplication
13 Introduction to Greedy Approach, Fractional Knapsack problem
14 Job Sequencing Problem with deadline ,Optimal Merge Pattern
15 Minimum Spanning Tree,Prim’s Algorithm , Kruskal’s Algorithm
16 Dynamic Programming,Difference between Dynamic , Greedy and D &
C
17 0/1 Knapsack Problem, 0/1 Knapsack using set method
18 Longest Common Subsequence
19 Matrix Chain Multiplication
20 Practice Worksheet-2 and Live Quiz using Mentimeter
21 Introduction to Branch and Bound, Travelling Salesman problem
22 Travelling Salesman Problem
23 Introduction To Backtracking
24 Lower Bound Theory
25 N Queen’s problem
26 N Queen’s Problem Cont…
27 Pattern Matching: Naïve String matching Algorithm
28 Rabin Karp, KMP matcher
29 Boyer Moore Algorithm, Example of Pattern Matching
30 Unit test-I
31 Flow Shop Scheduling
32 Flow Shop Scheduling cont…..
33 Assignment problem using Branch and Bound
34 Revision Worksheet-3
35 Revision Worksheet-4
36 Revision Worksheet-4 cont..
37 Revision Worksheet-5
38 Revision Worksheet-5 cont..
39 Discussion of Question paper
Page | 12
Q. No Questions Marks CO BL
1 Define Space and Time Complexity. State three cases of Master’s theorem. 1 CO1 L1
7 Show all the steps to multiply two matrices using Strassen’s Matrix 2 CO2 L3
Multiplication.
9 Explain Prim’s algorithm and Kruskal’s Algorithm using the following 4 CO2 L3
graph
10 Find the optimal parenthesizing of Matrix chain product whose sequence of 4 CO2 L3
dimensions are <5,10,3,12,5>
Page 1 of 3
Swami Keshvanand Institute of Technology, Management &
Gramothan, Ramnagaria, Jagatpura, Jaipur-302017
Analysis of Assignment-I
Assignment-I, 2022-23
Branch/Semester: CSE/ V Subject: Analysis of Algorithm (AOA) Subject Code: 5CS4-05
Assignment: I Session (I/II/III): I Max Marks: 20
Submitted By: Mr. Loveleen Kumar
CO – Course Outcome
COs Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10
CO1 3 3 - - 3 3 - - - -
CO2 - - 3 3 - - 3 3 3 3
CO3 - - - - - - - - - -
CO4 - - - - - - - - - -
CO5 - - - - - - - - - -
BL4 CO4 -
BL5 CO5 -
BL6 - -
Page 2 of 3
Swami Keshvanand Institute of Technology, Management &
Gramothan, Ramnagaria, Jagatpura, Jaipur-302017
Analysis of Assignment-II
Assignment-II, 2022-23
Branch/Semester: CSE/ V Subject: Analysis of Algorithm (AOA) Subject Code: 5CS4-05
Assignment: II Session (I/II/III): I Max Marks: 40
Submitted By: Mr. Loveleen Kumar
Q. No Questions Marks CO BL
14 2 CO4 L3
Give Randomized Algorithm to find Min cut for the following Graph.
Page 1 of 4
Swami Keshvanand Institute of Technology, Management &
Gramothan, Ramnagaria, Jagatpura, Jaipur-302017
Analysis of Assignment-II
Assignment-II, 2022-23
17 Solve Assignment Problem using Branch and Bound for the following cost 5 CO3 L3
matrix.
4 7 5
Cost= 2 6 1
3 9 8
18 5 CO3 L3
Explain KMP Matcher along with lps construction for the
following example
txt[] = "AAAAABAAABA"
pat[] = "AAAA"
19 5 CO4 L2
Explain Flow shop scheduling with suitable example.
Page 2 of 4
Swami Keshvanand Institute of Technology, Management &
Gramothan, Ramnagaria, Jagatpura, Jaipur-302017
Analysis of Assignment-II
Assignment-II, 2022-23
Branch/Semester: CSE/ V Subject: Analysis of Algorithm(AOA) Subject Code: 5CS4-05
Assignment: II Session (I/II/III): I Max Marks: 40
Submitted By: Mr. Loveleen Kumar
CO – Course Outcome
COs Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10
CO1 - - - - - - - - - -
CO2 - - - - - - - - - -
CO3 - - - - - 3 3 - - -
CO4 - - - - 3 - - - - 3
CO5 3 3 3 3 - - - 3 3 -
COs Q11 Q12 Q13 Q14 Q15 Q16 Q17 Q18 Q19
CO1 - - - - - - - - -
CO2 - - - - - - - - -
CO3 - - 3 - - - 3 3 -
CO4 3 - - 3 - 3 - - 3
CO5 - 3 - - 3 - - - -
Page 3 of 4
Swami Keshvanand Institute of Technology, Management &
Gramothan, Ramnagaria, Jagatpura, Jaipur-302017
Analysis of Assignment-II
Assignment-II, 2022-23
Branch/Semester: CSE/ V Subject: Analysis of Algorithm(AOA) Subject Code: 5CS4-05
Assignment: II Session (I/II/III): I Max Marks: 40
Submitted By: Mr. Loveleen Kumar
BL6 - - -
Page 4 of 4
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Unit-1
Algorithm
An algorithm is a finite set of instructions, those if followed, accomplishes a particular task. It is
not language specific, we can use any language and symbols to represent instructions.
The criteria of an algorithm
• Input: Zero or more inputs are externally supplied to the algorithm.
• Output: At least one output is produced by an algorithm.
• Definiteness: Each instruction is clear and unambiguous.
• Finiteness: In an algorithm, it will be terminated after a finite number of steps for all
different cases.
• Effectiveness: Each instruction must be very basic, so the purpose of those instructions
must be very clear to us.
Analysis of algorithms
Algorithm analysis is an important part of computational complexities. The complexity theory
provides the theoretical estimates for the resources needed by an algorithm to solve any
computational task. Analysis of the algorithm is the process of analyzing the problem-solving
capability of the algorithm in terms of the time and size required (the size of memory for storage
while implementation). However, the main concern of the analysis of the algorithm is the required
time or performance.
Complexities of an Algorithm
The complexity of an algorithm computes the amount of time and spaces required by an algorithm
for an input of size (n). The complexity of an algorithm can be divided into two types. The time
complexity and the space complexity.
Time Complexity of an Algorithm
The time complexity is defined as the process of determining a formula for total time required
towards the execution of that algorithm. This calculation is totally independent of implementation
and programming language.
Space Complexity of an Algorithm
Space complexity is defining as the process of defining a formula for prediction of how much
memory space is required for the successful execution of the algorithm. The memory space is
generally considered as the primary memory.
Page | 1
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Asymptotic Notations
I. Asymptotic Analysis: Big-O Notation and More
In this tutorial, you will learn what asymptotic notations are. Also, you will learn about Big-O
notation, Theta notation and Omega notation.
The efficiency of an algorithm depends on the amount of time, storage and other resources
required to execute the algorithm. The efficiency is measured with the help of asymptotic
notations.
An algorithm may not have the same performance for different types of inputs. With the increase
in the input size, the performance will change.
The study of change in performance of the algorithm with the change in the order of the input size
is defined as asymptotic analysis.
Page | 2
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 3
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 4
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 5
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Recurrence Relation
A recurrence is an equation or inequality that describes a function in terms of its values on smaller
inputs. To solve a Recurrence Relation means to obtain a function defined on the natural numbers
that satisfy the recurrence.
For Example, the Worst-Case Running Time T(n) of the MERGE SORT Procedures is described
by the recurrence.
T (n) = θ (1) if n=1
2T + θ (n) if n>1
There are four methods for solving Recurrence:
1. Substitution Method
2. Iteration Method
3. Recursion Tree Method
4. Master Method
Substitution Method:
The Substitution Method Consists of two main steps:
1. Guess the Solution.
2. Use the mathematical induction to find the boundary condition and shows that the guess is
correct.
For Example1 Solve the equation by Substitution Method.
T (n) = T +n
We have to show that it is asymptotically bound by O (log n).
Solution:
Page | 6
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
T (n) = 2T + n n>1
Find an Asymptotic bound on T.
Solution:
Iteration Methods
It means to expand the recurrence and express it as a summation of terms of n and initial condition.
Example1: Consider the Recurrence
1. T (n) = 1 if n=1
2. = 2T (n-1) if n>1
Page | 7
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Solution:
T (n) = 2T (n-1)
= 2[2T (n-2)] = 22T (n-2)
= 4[2T (n-3)] = 23T (n-3)
= 8[2T (n-4)] = 24T (n-4) (Eq.1)
T (n) = 2i T (n-i)
Put n-i=1 or i= n-1 in (Eq.1)
T (n) = 2n-1 T (1)
= 2n-1 .1 {T (1) =1 .....given}
= 2n-1
Example2: Consider the Recurrence
1. T (n) = T (n-1) +1 and T (1) = θ (1).
Solution:
T (n) = T (n-1) +1
= (T (n-2) +1) +1 = (T (n-3) +1) +1+1
= T (n-4) +4 = T (n-5) +1+4
= T (n-5) +5= T (n-k) + k
Where k = n-1
T (n-k) = T (1) = θ (1)
T (n) = θ (1) + (n-1) = 1+n-1=n= θ (n).
Page | 9
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Step-02:
Determine cost of each level-
• Cost of level-0 = n
• Cost of level-1 = n/2 + n/2 = n
• Cost of level-2 = n/4 + n/4 + n/4 + n/4 = n and so on.
Step-03:
Determine total number of levels in the recursion tree-
• Size of sub-problem at level-0 = n/20
• Size of sub-problem at level-1 = n/21
• Size of sub-problem at level-2 = n/22
Continuing in similar manner, we have-
Size of sub-problem at level-i = n/2i
Suppose at level-x (last level), size of sub-problem becomes 1. Then-
Page | 10
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
n / 2x = 1
2x = n
Taking log on both sides, we get-
xlog2 = logn
x = log2n
∴ Total number of levels in the recursion tree = log2n + 1
Step-04:
Determine number of nodes in the last level-
• Level-0 has 20 nodes i.e. 1 node
• Level-1 has 21 nodes i.e. 2 nodes
• Level-2 has 22 nodes i.e. 4 nodes
Continuing in similar manner, we have-
Level-log2n has 2log2n nodes i.e. n nodes
Step-05:
Determine cost of last level-
Cost of last level = n x T(1) = θ(n)
Step-06:
Add costs of all the levels of the recursion tree and simplify the expression so obtained in terms of
asymptotic notation-
= n x log2n + θ (n)
= nlog2n + θ (n)
= θ (nlog2n)
Problem-02:
Solve the following recurrence relation using recursion tree method-
T(n) = T(n/5) + T(4n/5) + n
Solution-
Step-01:
Page | 11
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 12
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Step-02:
Determine cost of each level-
• Cost of level-0 = n
• Cost of level-1 = n/5 + 4n/5 = n
• Cost of level-2 = n/52 + 4n/52 + 4n/52 + 42n/52 = n
Step-03:
Determine total number of levels in the recursion tree. We will consider the rightmost sub tree as it
goes down to the deepest level-
• Size of sub-problem at level-0 = (4/5)0n
• Size of sub-problem at level-1 =(4/5)1n
• Size of sub-problem at level-2 =(4/5)2n
Continuing in similar manner, we have-
Size of sub-problem at level-i = (4/5)in
Suppose at level-x (last level), size of sub-problem becomes 1. Then-
(4/5)xn = 1
(4/5)x = 1/n
Taking log on both sides, we get-
xlog(4/5) = log(1/n)
x = log5/4n
∴ Total number of levels in the recursion tree = log5/4n + 1
Step-04:
Determine number of nodes in the last level-
Page | 13
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
= nlog5/4n + θ(nlog5/42)
= θ(nlog5/4n)
Problem-03:
Solve the following recurrence relation using recursion tree method-
T(n) = 3T(n/4) + cn2
Solution-
Step-01:
Draw a recursion tree based on the given recurrence relation-
Page | 14
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
(Here, we have directly drawn a recursion tree representing the cost of sub problems)
Step-02:
Determine cost of each level-
• Cost of level-0 = cn2
• Cost of level-1 = c(n/4)2 + c(n/4)2 + c(n/4)2 = (3/16)cn2
• Cost of level-2 = c(n/16)2 x 9 = (9/162)cn2
Step-03:
Determine total number of levels in the recursion tree-
• Size of sub-problem at level-0 = n/40
• Size of sub-problem at level-1 = n/41
• Size of sub-problem at level-2 = n/42
Continuing in similar manner, we have-
Size of sub-problem at level-i = n/4i
Suppose at level-x (last level), size of sub-problem becomes 1. Then-
n/4x = 1
4x = n
Taking log on both sides, we get-
xlog4 = logn
x = log4n
∴ Total number of levels in the recursion tree = log4n + 1
Step-04:
Determine number of nodes in the last level-
• Level-0 has 30 nodes i.e. 1 node
• Level-1 has 31 nodes i.e. 3 nodes
• Level-2 has 32 nodes i.e. 9 nodes
Continuing in similar manner, we have-
Level-log4n has 3log4n nodes i.e. nlog43 nodes
Step-05:
Determine cost of last level-
Cost of last level = nlog43 x T(1) = θ(nlog43)
Step-06:
Page | 15
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Add costs of all the levels of the recursion tree and simplify the expression so obtained in terms of
asymptotic notation-
Page | 16
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Merge sort Θ(nlgn) \Theta(n \lg n) Θ(nlgn) \Theta(n \lg n) Θ(nlgn) \Theta(n \lg n)
Θ(nlgn)\Theta, left Θ(nlgn)\Theta, left Θ(nlgn)\Theta, left
parenthesis, n, \lg, n, parenthesis, n, \lg, n, parenthesis, n, \lg, n,
right parenthesis right parenthesis right parenthesis
Quicksort Θ(n2) \Theta(n^2) Θ(nlgn) \Theta(n \lg n) Θ(nlgn) \Theta(n \lg n)
Θ(n2)\Theta, left Θ(nlgn)\Theta, left Θ(nlgn)\Theta, left
parenthesis, n, squared, parenthesis, n, \lg, n, parenthesis, n, \lg, n,
right parenthesis right parenthesis right parenthesis
Divide-and-conquer
Both merge sort and quicksort employ a common algorithmic paradigm based on recursion. This
paradigm, divide-and-conquer, breaks a problem into subproblems that are similar to the original
problem, recursively solves the subproblems, and finally combines the solutions to the
subproblems to solve the original problem. Because divide-and-conquer solves subproblems
recursively, each subproblem must be smaller than the original problem, and there must be a base
case for subproblems. You should think of a divide-and-conquer algorithm as having three parts:
1. Divide the problem into a number of subproblems that are smaller instances of the same
problem.
2. Conquer the subproblems by solving them recursively. If they are small enough, solve the
subproblems as base cases.
3. Combine the solutions to the subproblems into the solution for the original problem.
You can easily remember the steps of a divide-and-conquer algorithm as divide, conquer, combine.
Here's how to view one step, assuming that each divide step creates two subproblems (though
some divide-and-conquer algorithms create more than two):
Binary Search
Problem: Given a sorted array arr[] of n elements, write a function to search a given element x in
arr[] and return the index of x in the array.
Consider array is 0 base index.
Examples:
Input: arr[] = {10, 20, 30, 50, 60, 80, 110, 130, 140, 170}, x = 110
Output: 6
Explanation: Element x is present at index 6.
Input: arr[] = {10, 20, 30, 40, 60, 110, 120, 130, 170}, x = 175
Output: -1
Explanation: Element x is not present in arr[].
Linear Search Approach: A simple approach is to do a linear search. The time complexity of the
Linear search is O(n). Another approach to perform the same task is using Binary Search.
Binary Search Approach:
Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search
Page | 18
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
interval in half. The idea of binary search is to use the information that the array is sorted and
reduce the time complexity to O(Log n).
Binary Search Algorithm: The basic steps to perform Binary Search are:
• Begin with the mid element of the whole array as a search key.
• If the value of the search key is equal to the item then return an index of the search key.
• Or if the value of the search key is less than the item in the middle of the interval, narrow
the interval to the lower half.
• Otherwise, narrow it to the upper half.
• Repeatedly check from the second point until the value is found or the interval is empty.
Binary Search Algorithm can be implemented in the following two ways
1. Iterative Method
2. Recursive Method
1. Iteration Method
binarySearch(arr, x, low, high)
repeat till low = high
mid = (low + high)/2
if (x == arr[mid])
return mid
else
mid = (low + high) / 2
if x == arr[mid]
return mid
Page | 19
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 20
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
• At first, check if the left index of array is less than the right index, if yes then calculate its
mid point
• Now, as we already know that merge sort first divides the whole array iteratively into equal
halves, unless the atomic values are achieved.
• Here, we see that an array of 7 items is divided into two arrays of size 4 and 3 respectively.
Now, again find that is left index is less than the right index for both arrays, if found yes, then again
calculate mid points for both the arrays.
• Now, further divide these two arrays into further halves, until the atomic units of the array is
reached and further division is not possible.
Page | 21
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
• After dividing the array into smallest units, start merging the elements again based on
comparison of size of elements
• Firstly, compare the element for each list and then combine them into another list in a sorted
manner.
The following diagram shows the complete merge sort process for an example array {38, 27, 43, 3,
9, 82, 10}.
If we take a closer look at the diagram, we can see that the array is recursively divided into two
halves till the size becomes 1. Once the size becomes 1, the merge processes come into action and
start merging arrays back till the complete array is merged.
Page | 22
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Algorithm:
step 1: start
step 2: declare array and left, right, mid variable
step 3: perform merge function.
if left > right
return
mid= (left+right)/2
mergesort(array, left, mid)
mergesort(array, mid+1, right)
Page | 23
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
QuickSort
Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as a pivot and
partitions the given array around the picked pivot. There are many different versions of quickSort
that pick pivot in different ways.
• Always pick the first element as a pivot.
• Always pick the last element as a pivot (implemented below)
• Pick a random element as a pivot.
• Pick median as the pivot.
The key process in quickSort is a partition(). The target of partitions is, given an array and an
element x of an array as the pivot, put x at its correct position in a sorted array and put all smaller
elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this
should be done in linear time.
Page | 24
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Partition Algorithm:
There can be many ways to do partition, following pseudo-code adopts the method given in the
CLRS book. The logic is simple, we start from the leftmost element and keep track of the index of
smaller (or equal to) elements as i. While traversing, if we find a smaller element, we swap the
current element with arr[i]. Otherwise, we ignore the current element.
Pseudo Code for recursive QuickSort function:
/* low –> Starting index, high –> Ending index */
quickSort(arr[], low, high) {
if (low < high) {
/* pi is partitioning index, arr[pi] is now at right place */
pi = partition(arr, low, high);
quickSort(arr, low, pi – 1); // Before pi
quickSort(arr, pi + 1, high); // After pi
}
}
Pseudo code for partition()
/* This function takes last element as pivot, places the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot) to left of pivot and all greater elements to right of
pivot */
partition (arr[], low, high)
{
// pivot (Element to be placed at right position)
pivot = arr[high];
i = (low – 1) // Index of smaller element and indicates the
// right position of pivot found so far
Page | 25
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 26
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 27
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
• j = 5 : Since arr[j] <= pivot, do i++ and swap arr[i] with arr[j]
• i=3
• arr[] = {10, 30, 40, 50, 80, 90, 70} // 90 and 50 Swapped
Page | 28
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
• Now 70 is at its correct place. All elements smaller than 70 are before it and all elements greater than
70 are after it.
• Since quick sort is a recursive function, we call the partition function again at left and right partitions
Page | 29
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Problem Statement
Let us consider two matrices X and Y. We want to calculate the resultant matrix Z by multiplying X
and Y.
Naïve Method
First, we will discuss naïve method and its complexity. Here, we are calculating Z = X × Y. Using
Naïve method, two matrices (X and Y) can be multiplied if the order of these matrices are p × q and
q × r. Following is the algorithm.
Algorithm: Matrix-Multiplication (X, Y, Z)
for i = 1 to p do
for j = 1 to r do
Z[i,j] := 0
for k = 1 to q do
Z[i,j] := Z[i,j] + X[i,k] × Y[k,j]
Complexity
Here, we assume that integer operations take O(1) time. There are three for loops in this algorithm
and one is nested in other. Hence, the algorithm takes O(n3) time to execute.
Strassen’s Matrix Multiplication Algorithm
In this context, using Strassen’s Matrix multiplication algorithm, the time consumption can be
improved a little bit.
Strassen’s Matrix multiplication can be performed only on square matrices where n is a power of
2. Order of both of the matrices are n × n.
Divide X, Y and Z into four (n/2)×(n/2) matrices as represented below −
Z=[IKJL]
X=[ACBD] and Y=[EGFH]
Using Strassen’s Algorithm compute the following −
M1:=(A+C)×(E+F)
M2:=(B+D)×(G+H)
M3:=(A−D)×(E+H)
M4:=A×(F−H)
M5:=(C+D)×(E)
M6:=(A+B)×(H)
Page | 30
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
M7:=D×(G−E)
Then,
I:=M2+M3−M6−M7
J:=M4+M6
K:=M5+M7
L:=M1−M3−M4−M5
Analysis
T(n)={c7xT(n2)+dxn2ifn=1otherwise
where c and d are constants
Using this recurrence relation, we get T(n)=O(nlog7)
Hence, the complexity of Strassen’s matrix multiplication algorithm is O(nlog7)
Following is simple Divide and Conquer method to multiply two square matrices.
1. Divide matrices A and B in 4 sub-matrices of size N/2 x N/2 as shown in the below
diagram.
2. Calculate following values recursively. ae + bg, af + bh, ce + dg and cf + dh.
Page | 31
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Unit-2
Greedy Algorithm
In this tutorial, you will learn what a Greedy Algorithm is. Also, you will find an example of a
greedy approach.
A greedy algorithm is an approach for solving a problem by selecting the best option available at
the moment. It doesn't worry whether the current best result will bring the overall optimal result.
The algorithm never reverses the earlier decision even if the choice is wrong. It works in a top-
down approach.
This algorithm may not produce the best result for all the problems. It's because it always goes for
the local best choice to produce the global best result.
However, we can determine if the algorithm can be used with any problem if the problem has the
following properties:
1. Greedy Choice Property
If an optimal solution to the problem can be found by choosing the best choice at each step without
reconsidering the previous steps once chosen, the problem can be solved using a greedy approach.
This property is called greedy choice property.
2. Optimal Substructure
If the optimal overall solution to the problem corresponds to the optimal solution to its
subproblems, then the problem can be solved using a greedy approach. This property is called
optimal substructure.
Page | 1
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Knapsack Problem
Given a set of items, each with a weight and a value, determine a subset of items to include in a
collection so that the total weight is less than or equal to a given limit and the total value is as large
as possible.
The knapsack problem is in combinatorial optimization problem. It appears as a subproblem in
many, more complex mathematical models of real-world problems. One general approach to
difficult problems is to identify the most restrictive constraint, ignore the others, solve a knapsack
problem, and somehow adjust the solution to satisfy the ignored constraints.
Applications
In many cases of resource allocation along with some constraint, the problem can be derived in a
similar way of Knapsack problem. Following is a set of example.
• Finding the least wasteful way to cut raw materials
• portfolio optimization
• Cutting stock problems
Problem Scenario
A thief is robbing a store and can carry a maximal weight of W into his knapsack. There are n items
available in the store and weight of ith item is wi and its profit is pi. What items should the thief
take?
In this context, the items should be selected in such a way that the thief will carry those items for
which he will gain maximum profit. Hence, the objective of the thief is to maximize the profit.
Based on the nature of the items, Knapsack problems are categorized as
• Fractional Knapsack
• Knapsack
Fractional Knapsack
In this case, items can be broken into smaller pieces, hence the thief can select fractions of items.
According to the problem statement,
• There are n items in the store
• Weight of ith item wi>0
Profit for ith item pi>0
• and
• Capacity of the Knapsack is W
In this version of Knapsack problem, items can be broken into smaller pieces. So, the thief may take
only a fraction xi of ith item.
0⩽xi⩽1
Page | 2
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 3
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Example
Let us consider that the capacity of the knapsack W = 60 and the list of provided items are shown in
the following table −
Item A B C D
Profit 280 100 120 120
Weight 40 10 20 24
Ratio (piwi)
7 10 6 5
10 7 6 5
Solution
After sorting all the items according to piwi
. First all of B is chosen as weight of B is less than the capacity of the knapsack. Next, item A is
chosen, as the available capacity of the knapsack is greater than the weight of A. Now, C is chosen
as the next item. However, the whole item cannot be chosen as the remaining capacity of the
knapsack is less than the weight of C.
Hence, fraction of C (i.e. (60 − 50)/20) is chosen.
Now, the capacity of the Knapsack is equal to the selected items. Hence, no more item can be
selected.
The total weight of the selected items is 10 + 40 + 20 * (10/20) = 60
And the total profit is 100 + 280 + 120 * (10/20) = 380 + 60 = 440
This is the optimal solution. We cannot gain more profit selecting any different combination of
items.
Page | 4
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 5
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Example
Let us consider a set of given jobs as shown in the following table. We have to find a sequence of
jobs, which will be completed within their deadlines and will give maximum profit. Each job is
associated with a deadline and profit.
Job J1 J2 J3 J4 J5
Deadline 2 1 3 2 1
Profit 60 100 20 40 20
Solution
To solve this problem, the given jobs are sorted according to their profit in a descending order.
Hence, after sorting, the jobs are ordered as shown in the following table.
Job J2 J1 J4 J3 J5
Deadline 1 2 2 3 1
Profit 100 60 40 20 20
From this set of jobs, first we select J2, as it can be completed within its deadline and contributes
maximum profit.
• Next, J1 is selected as it gives more profit compared to J4.
• In the next clock, J4 cannot be selected as its deadline is over, hence J3 is selected as it
executes within its deadline.
• The job J5 is discarded as it cannot be executed within its deadline.
Thus, the solution is the sequence of jobs (J2, J1, J3), which are being executed within their deadline
and gives maximum profit.
Total profit of this sequence is 100 + 60 + 20 = 180.
Page | 6
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Where, f (i) represents the number of records in each file and d (i) represents the depth.
Algorithm for optimal merge pattern
Algorithm Tree(n)
//list is a global list of n single node
{
For i=1 to i= n-1 do
{
// get a new tree node
Pt: new treenode;
// merge two trees with smallest length
(Pt = lchild) = least(list);
(Pt = rchild) = least(list);
(Pt =weight) = ((Pt = lchild) = weight) = ((Pt = rchild) = weight);
Insert (list , Pt);
}
// tree left in list
Return least(list);
}
An optimal merge pattern corresponds to a binary merge tree with minimum weighted external path
length. The function tree algorithm uses the greedy rule to get a two- way merge tree for n files. The
algorithm contains an input list of n trees. There are three field child, rchild, and weight in each
Page | 7
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
node of the tree. Initially, each tree in a list contains just one node. This external node has lchild and
rchild field zero whereas weight is the length of one of the n files to be merged. For any tree in the
list with root node t, t = it represents the weight that gives the length of the merged file. There are
two functions least (list) and insert (list, t) in a function tree. Least (list) obtains a tree in lists whose
root has the least weight and return a pointer to this tree. This tree is deleted from the list. Function
insert (list, t) inserts the tree with root t into the list.
The main for loop in this algorithm is executed in n-1 times. If the list is kept in increasing order
according to the weight value in the roots, then least (list) needs only O(1) time and insert (list, t)
can be performed in O(n) time. Hence, the total time taken is O (n2). If the list is represented as a
minheap in which the root value is less than or equal to the values of its children, then least (list)
and insert (list, t) can be done in O (log n) time. In this condition, the computing time for the tree is
O (n log n).
Advertisement
Example:
Given a set of unsorted files: 5, 3, 2, 7, 9, 13
Now, arrange these elements in ascending order: 2, 3, 5, 7, 9, 13
After this, pick two smallest numbers and repeat this until we left with only one number.
Now follow following steps:
Step 1: Insert 2, 3
Step 2:
Step 3: Insert 5
Page | 8
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Step 4: Insert 13
Page | 9
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Step 6:
Page | 10
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Kruskal's Algorithm:
An algorithm to construct a Minimum Spanning Tree for a connected weighted graph. It is a Greedy
Algorithm. The Greedy Choice is to put the smallest weight edge that does not because a cycle in
the MST constructed so far.
If the graph is not linked, then it finds a Minimum Spanning Tree.
Steps for finding MST using Kruskal's Algorithm:
1. Arrange the edge of G in order of increasing weight.
2. Starting only with the vertices of G and proceeding sequentially add each edge which does
not result in a cycle, until (n - 1) edges are used.
3. EXIT.
MST- KRUSKAL (G, w)
1. A ← ∅
2. for each vertex v ∈ V [G]
3. do MAKE - SET (v)
4. sort the edges of E into non decreasing order by weight w
5. for each edge (u, v) ∈ E, taken in non decreasing order by weight
6. do if FIND-SET (μ) ≠ if FIND-SET (v)
7. then A ← A ∪ {(u, v)}
8. UNION (u, v)
9. return A
Analysis: Where E is the number of edges in the graph and V is the number of vertices, Kruskal's
Algorithm can be shown to run in O (E log E) time, or simply, O (E log V) time, all with simple
data structures. These running times are equivalent because:
• E is at most V2 and log V2= 2 x log V is O (log V).
• If we ignore isolated vertices, which will each their components of the minimum spanning
tree, V ≤ 2 E, so log V is O (log E).
Thus the total time is
Page | 11
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Solution: First we initialize the set A to the empty set and create |v| trees, one containing each
vertex with MAKE-SET procedure. Then sort the edges in E into order by non-decreasing weight.
There are 9 vertices and 12 edges. So MST formed (9-1) = 8 edges
Now, check for each edge (u, v) whether the endpoints u and v belong to the same tree. If they do
then the edge (u, v) cannot be supplementary. Otherwise, the two vertices belong to different trees,
and the edge (u, v) is added to A, and the vertices in two trees are merged in by union procedure.
Step1: So, first take (h, g) edge
Page | 12
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Step 3: then (a, b) and (i, g) edges are considered, and the forest becomes
Step 4: Now, edge (h, i). Both h and i vertices are in the same set. Thus it creates a cycle. So this
edge is discarded.
Then edge (c, d), (b, c), (a, h), (d, e), (e, f) are considered, and the forest becomes.
Step 5: In (e, f) edge both endpoints e and f exist in the same tree so discarded this edge. Then (b,
h) edge, it also creates a cycle.
Page | 13
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Step 6: After that edge (d, f) and the final spanning tree is shown as in dark lines.
Step 7: This step will be required Minimum Spanning Tree because it contains all the 9 vertices and
(9 - 1) = 8 edges
1. e → f, b → h, d → f [cycle will be formed]
Prim's Algorithm
It is a greedy algorithm. It starts with an empty spanning tree. The idea is to maintain two sets of
vertices:
• Contain vertices already included in MST.
• Contain vertices not yet included.
At every step, it considers all the edges and picks the minimum weight edge. After picking the
edge, it moves the other endpoint of edge to set containing MST.
Steps for finding MST using Prim's Algorithm:
1. Create MST set that keeps track of vertices already included in MST.
2. Assign key values to all vertices in the input graph. Initialize all key values as INFINITE
(∞). Assign key values like 0 for the first vertex so that it is picked first.
3. While MST set doesn't include all vertices.
Page | 14
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
1. Pick vertex u which is not is MST set and has minimum key value. Include 'u'to
MST set.
2. Update the key value of all adjacent vertices of u. To update, iterate through all
adjacent vertices. For every adjacent vertex v, if the weight of edge u.v less than the
previous key value of v, update key value as a weight of u.v.
MST-PRIM (G, w, r)
1. for each u ∈ V [G]
2. do key [u] ← ∞
3. π [u] ← NIL
4. key [r] ← 0
5. Q ← V [G]
6. While Q ? ∅
7. do u ← EXTRACT - MIN (Q)
8. for each v ∈ Adj [u]
9. do if v ∈ Q and w (u, v) < key [v]
10. then π [v] ← u
11. key [v] ← w (u, v)
Example: Generate minimum cost spanning tree for the following graph using Prim's algorithm.
Solution: In Prim's algorithm, first we initialize the priority Queue Q. to contain all the vertices and
the key of each vertex to ∞ except for the root, whose key is set to 0. Suppose 0 vertex is the root,
i.e., r. By EXTRACT - MIN (Q) procure, now u = r and Adj [u] = {5, 1}.
Page | 15
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Removing u from set Q and adds it to set V - Q of vertices in the tree. Now, update the key and π
fields of every vertex v adjacent to u but not in a tree.
Page | 16
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 17
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
1. u = EXTRACT_MIN (2, 6)
2. u = 2 [key [2] < key [6]]
Page | 18
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
3. 12 < 18
4. Now the root is 2
5. Adj [2] = {3, 1}
6. 3 is already in a heap
7. Taking 1, key [1] = 28
8. w (2,1) = 16
9. w (2,1) < key [1]
So update key value of key [1] as 16 and its parent as 2.
1. π[1]= 2
Now all the vertices have been spanned, Using above the table we get Minimum Spanning Tree.
1. 0 → 5 → 4 → 3 → 2 → 1 → 6
2. [Because Π [5] = 0, Π [4] = 5, Π [3] = 4, Π [2] = 3, Π [1] =2, Π [6] =1]
Thus the final spanning Tree is
Total Cost = 10 + 25 + 22 + 12 + 16 + 14 = 99
Page | 20
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Dynamic Programming
Dynamic Programming is a technique in computer programming that helps to efficiently solve a
class of problems that have overlapping subproblems and optimal substructure property.
If any problem can be divided into subproblems, which in turn are divided into smaller
subproblems, and if there are overlapping among these subproblems, then the solutions to these
subproblems can be saved for future reference. In this way, efficiency of the CPU can be enhanced.
This method of solving a solution is referred to as dynamic programming.
Such problems involve repeatedly calculating the value of the same subproblems to find the
optimum solution.
1. If n <= 1, return 1.
2. Else, return the sum of two preceding numbers.
We are calculating the fibonacci sequence up to the 5th term.
1. The first term is 0.
2. The second term is 1.
3. The third term is sum of 0 (from step 1) and 1(from step 2), which is 1.
4. The fourth term is the sum of the third term (from step 3) and second term (from step 2) i.e.
1 + 1 = 2.
5. The fifth term is the sum of the fourth term (from step 4) and third term (from step 3) i.e. 2
+ 1 = 3.
Hence, we have the sequence 0,1,1, 2, 3. Here, we have used the results of the previous steps as
shown below. This is called a dynamic programming approach.
F(0) = 0
F(1) = 1
F(2) = F(1) + F(0)
F(3) = F(2) + F(1)
Page | 21
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 23
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Let us proceed with working away from the diagonal. We compute the optimal solution for the
product of 2 matrices.
In Dynamic Programming, initialization of every method done by '0'.So we initialize it by '0'.It will
sort out diagonally.
We have to sort out all the combination but the minimum output combination is taken into
consideration.
Calculation of Product of 2 matrices:
1. m (1,2) = m1 x m2
= 4 x 10 x 10 x 3
= 4 x 10 x 3 = 120
Page | 24
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
2. m (2, 3) = m2 x m3
= 10 x 3 x 3 x 12
= 10 x 3 x 12 = 360
3. m (3, 4) = m3 x m4
= 3 x 12 x 12 x 20
= 3 x 12 x 20 = 720
4. m (4,5) = m4 x m5
= 12 x 20 x 20 x 7
= 12 x 20 x 7 = 1680
• We initialize the diagonal element with equal i,j value with '0'.
• After that second diagonal is sorted out and we get all the values corresponded to it
Now the third diagonal will be solved out in the same way.
Now product of 3 matrices:
M [1, 3] = M1 M2 M3
1. There are two cases by which we can solve this multiplication: ( M1 x M2) + M3, M1+ (M2x
M3 )
2. After solving both cases we choose the case in which minimum output is there.
M [1, 3] =264
As Comparing both output 264 is minimum in both cases so we insert 264 in table and ( M1 x M2) +
Page | 25
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
M [2, 4] = 1320
As Comparing both output 1320 is minimum in both cases so we insert 1320 in table and M2+(M3 x
M4) this combination is chosen for the output making.
M [3, 5] = M3 M4 M5
1. There are two cases by which we can solve this multiplication: ( M3 x M4) + M5, M3+ (
M4xM5)
2. After solving both cases we choose the case in which minimum output is there.
M [3, 5] = 1140
As Comparing both output 1140 is minimum in both cases so we insert 1140 in table and ( M3 x
M4) + M5this combination is chosen for the output making.
Page | 26
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
M [1, 4] =1080
As comparing the output of different cases then '1080' is minimum output, so we insert 1080 in the
table and (M1 xM2) x (M3 x M4) combination is taken out in output making,
M [2, 5] = M2 M3 M4 M5
There are three cases by which we can solve this multiplication:
1. (M2 x M3 x M4)x M5
2. M2 x( M3 x M4 x M5)
3. (M2 x M3)x ( M4 x M5)
After solving these cases we choose the case in which minimum output is there
M [2, 5] = 1350
As comparing the output of different cases then '1350' is minimum output, so we insert 1350 in the
table and M2 x( M3 x M4 xM5)combination is taken out in output making.
Page | 27
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
M [1, 5] = 1344
As comparing the output of different cases then '1344' is minimum output, so we insert 1344 in the
table and M1 x M2 x(M3 x M4 x M5)combination is taken out in output making.
Final Output is:
Step 3: Computing Optimal Costs: let us assume that matrix Ai has dimension pi-1x pi for i=1, 2,
3....n. The input is a sequence (p0,p1,......pn) where length [p] = n+1. The procedure uses an auxiliary
table m [1....n, 1.....n] for storing m [i, j] costs an auxiliary table s [1.....n, 1.....n] that record which
index of k achieved the optimal costs in computing m [i, j].
The algorithm first computes m [i, j] ← 0 for i=1, 2, 3.....n, the minimum costs for the chain of
length 1.
Algorithm of Matrix Chain Multiplication
MATRIX-CHAIN-ORDER (p)
1. n length[p]-1
2. for i ← 1 to n
3. do m [i, i] ← 0
4. for l ← 2 to n // l is the chain length
5. do for i ← 1 to n-l + 1
6. do j ← i+ l -1
7. m[i,j] ← ∞
8. for k ← i to j-1
9. do q ← m [i, k] + m [k + 1, j] + pi-1 pk pj
10. If q < m [i,j]
Page | 28
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
PRINT-OPTIMAL-PARENS (s, i, j)
1. if i=j
2. then print "A"
3. else print "("
4. PRINT-OPTIMAL-PARENS (s, i, s [i, j])
5. PRINT-OPTIMAL-PARENS (s, s [i, j] + 1, j)
6. print ")"
Page | 29
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 30
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Second Sequence
The following steps are followed for finding the longest common subsequence.
1. Create a table of dimension n+1*m+1 where n and m are the lengths of X and Y
respectively. The first row and the first column are filled with zeros.
Initialise a table
2. Fill each cell of the table using the following logic.
3. If the character correspoding to the current row and current column are matching, then fill
the current cell by adding one to the diagonal element. Point an arrow to the diagonal cell.
4. Else take the maximum value from the previous column and previous row element for
filling the current cell. Point an arrow to the cell with maximum value. If they are equal,
Page | 31
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
6. The value in the last row and the last column is the length of the longest common
subsequence. The bottom right corner is the length of the LCS
7. In order to find the longest common subsequence, start from the last element and follow the
direction of the arrow. The elements corresponding to () symbol form the longest common
subsequence. Create a path according to the arrows
Page | 32
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
LCS
How is a dynamic programming algorithm more efficient than the recursive algorithm while
solving an LCS problem?
The method of dynamic programming reduces the number of function calls. It stores the result of
each function call so that it can be used in future calls without the need for redundant calls.
In the above dynamic algorithm, the results obtained from each comparison between elements of X
and the elements of Y are stored in a table so that they can be used in future computations.
So, the time taken by a dynamic approach is the time taken to fill the table (ie. O(mn)). Whereas,
the recursion algorithm has the complexity of 2max(m, n).
Page | 33
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
that no item is picked. Since there are 4 items so possible combinations will be:
24 = 16; So. There are 16 possible combinations that can be made by using the above problem.
Once all the combinations are made, we have to select the combination that provides the maximum
profit.
Another approach to solve the problem is dynamic programming approach. In dynamic
programming approach, the complicated problem is divided into sub-problems, then we find the
solution of a sub-problem and the solution of the sub-problem will be used to find the solution of a
complex problem.
How this problem can be solved by using the Dynamic programming approach?
First,
we create a matrix shown as below:
012345678
In the above matrix, columns represent the weight, i.e., 8. The rows represent the profits and
weights of items. Here we have not taken the weight 8 directly, problem is divided into sub-
problems, i.e., 0, 1, 2, 3, 4, 5, 6, 7, 8. The solution of the sub-problems would be saved in the cells
and answer to the problem would be stored in the final cell. First, we write the weights in the
ascending order and profits according to their weights shown as below:
wi = {3, 4, 5, 6}
pi = {2, 3, 4, 1}
The first row and the first column would be 0 as there is no item for w=0
012345678
0000000000
10
20
30
40
Page | 35
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
012345678
0000000000
100
20
30
40
When i = 1, W = 2
w1 = 3; Since we have only one item in the set having weight 3, but the capacity of the knapsack is
2. We cannot fill the item of 3kg in the knapsack of capacity 2 kg so add 0 at M[1][2] shown as
below:
012345678
0000000000
1000
20
30
40
012345678
0000000000
10002
20
30
Page | 36
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
40
When i=1, W = 4
W1 = 3; Since we have only one item in the set having weight equal to 3, and weight of the knapsack
is 4; therefore, we can fill the knapsack with an item of weight equal to 3. We put profit
corresponding to the weight 3, i.e., 2 at M[1][4] shown as below:
012345678
0000000000
100022
20
30
40
When i=1, W = 5
W1 = 3; Since we have only one item in the set having weight equal to 3, and weight of the knapsack
is 5; therefore, we can fill the knapsack with an item of weight equal to 3. We put profit
corresponding to the weight 3, i.e., 2 at M[1][5] shown as below:
012345678
0000000000
1000222
20
30
40
012345678
0000000000
10002222
20
Page | 37
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
30
40
When i=1, W = 7
W1 = 3; Since we have only one item in the set having weight equal to 3, and weight of the knapsack
is 7; therefore, we can fill the knapsack with an item of weight equal to 3. We put profit
corresponding to the weight 3, i.e., 2 at M[1][7] shown as below:
012345678
0000000000
100022222
20
30
40
When i =1, W =8
W1 = 3; Since we have only one item in the set having weight equal to 3, and weight of the knapsack
is 8; therefore, we can fill the knapsack with an item of weight equal to 3. We put profit
corresponding to the weight 3, i.e., 2 at M[1][8] shown as below:
012345678
0000000000
1000222222
20
30
40
012345678
0000000000
1000222222
Page | 38
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
200
30
40
When i =2, W = 2
The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have only one item in the set
having weight equal to 4, and the weight of the knapsack is 2. We cannot put the item of weight 4 in
a knapsack, so we add 0 at M[2][2] shown as below:
012345678
0000000000
1000222222
2000
30
40
When i =2, W = 3
The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two items in the set having
weights 3 and 4, and the weight of the knapsack is 3. We can put the item of weight 3 in a
knapsack, so we add 2 at M[2][3] shown as below:
012345678
0000000000
1000222222
20002
30
40
When i =2, W = 4
The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two items in the set having
weights 3 and 4, and the weight of the knapsack is 4. We can put item of weight 4 in a knapsack as
the profit corresponding to weight 4 is more than the item having weight 3, so we add 3 at M[2][4]
shown as below:
012345678
0000000000
Page | 39
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
1000222222
200023
30
40
When i = 2, W = 5
The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two items in the set having
weights 3 and 4, and the weight of the knapsack is 5. We can put item of weight 4 in a knapsack
and the profit corresponding to weight is 3, so we add 3 at M[2][5] shown as below:
012345678
0000000000
1000222222
2000233
30
40
When i = 2, W = 6
The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two items in the set having
weights 3 and 4, and the weight of the knapsack is 6. We can put item of weight 4 in a knapsack
and the profit corresponding to weight is 3, so we add 3 at M[2][6] shown as below:
012345678
0000000000
1000222222
20002333
30
40
When i = 2, W = 7
The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two items in the set having
weights 3 and 4, and the weight of the knapsack is 7. We can put item of weight 4 and 3 in a
knapsack and the profits corresponding to weights are 2 and 3; therefore, the total profit is 5, so we
add 5 at M[2][7] shown as below:
012345678
Page | 40
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
0000000000
1000222222
200003335
30
40
When i = 2, W = 8
The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two items in the set having
weights 3 and 4, and the weight of the knapsack is 7. We can put item of weight 4 and 3 in a
knapsack and the profits corresponding to weights are 2 and 3; therefore, the total profit is 5, so we
add 5 at M[2][7] shown as below:
012345678
0000000000
1000222222
2000233355
30
40
012345678
0000000000
1000222222
2000233355
300
40
When i = 3, W = 2
The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the set
having weight 3, 4, and 5, and the weight of the knapsack is 1. We cannot put neither of the items in
Page | 41
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
012345678
0000000000
1000222222
2000233355
3000
40
When i = 3, W = 3
The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the set of
weight 3, 4, and 5 respectively and weight of the knapsack is 3. The item with a weight 3 can be put
in the knapsack and the profit corresponding to the item is 2, so we add 2 at M[3][3] shown as
below:
012345678
0000000000
1000222222
2000233355
30002
40
When i = 3, W = 4
The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the set of
weight 3, 4, and 5 respectively, and weight of the knapsack is 4. We can keep the item of either
weight 3 or 4; the profit (3) corresponding to the weight 4 is more than the profit corresponding to
the weight 3 so we add 3 at M[3][4] shown as below:
012345678
0000000000
1000222222
2000233355
300013
40
When i = 3, W = 5
Page | 42
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the set of
weight 3, 4, and 5 respectively, and weight of the knapsack is 5. We can keep the item of either
weight 3, 4 or 5; the profit (3) corresponding to the weight 4 is more than the profits corresponding
to the weight 3 and 5 so we add 3 at M[3][5] shown as below:
012345678
0000000000
1000222222
2000233355
3000133
40
When i =3, W = 6
The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the set of
weight 3, 4, and 5 respectively, and weight of the knapsack is 6. We can keep the item of either
weight 3, 4 or 5; the profit (3) corresponding to the weight 4 is more than the profits corresponding
to the weight 3 and 5 so we add 3 at M[3][6] shown as below:
012345678
0000000000
1000222222
2000233355
30001333
40
When i =3, W = 7
The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the set of
weight 3, 4, and 5 respectively, and weight of the knapsack is 7. In this case, we can keep both the
items of weight 3 and 4, the sum of the profit would be equal to (2 + 3), i.e., 5, so we add 5 at
M[3][7] shown as below:
012345678
0000000000
1000222222
2000233355
Page | 43
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
300013335
40
When i = 3, W = 8
The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the set of
weight 3, 4, and 5 respectively, and the weight of the knapsack is 8. In this case, we can keep both
the items of weight 3 and 4, the sum of the profit would be equal to (2 + 3), i.e., 5, so we add 5 at
M[3][8] shown as below:
012345678
0000000000
1000222222
2000233355
3000133355
40
012345678
0000000000
1000222222
2000233355
3000133355
400
When i = 4, W = 2
The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the set of
weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 2. The weight of all the items
is more than the weight of the knapsack, so we cannot add any item in the knapsack; Therefore, we
add 0 at M[4][2] shown as below:
012345678
Page | 44
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
0000000000
1000222222
2000233355
3000133355
4000
When i = 4, W = 3
The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the set of
weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 3. The item with a weight 3
can be put in the knapsack and the profit corresponding to the weight 4 is 2, so we will add 2 at
M[4][3] shown as below:
012345678
0000000000
1000222222
2000233355
3000133355
40002
When i = 4, W = 4
The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the set of
weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 4. The item with a weight 4
can be put in the knapsack and the profit corresponding to the weight 4 is 3, so we will add 3 at
M[4][4] shown as below:
012345678
0000000000
1000222222
2000233355
3000133355
400023
When i = 4, W = 5
The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the set of
weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 5. The item with a weight 4
Page | 45
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
can be put in the knapsack and the profit corresponding to the weight 4 is 3, so we will add 3 at
M[4][5] shown as below:
012345678
0000000000
1000222222
2000233355
3000133355
4000233
When i = 4, W = 6
The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the set of
weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 6. In this case, we can put the
items in the knapsack either of weight 3, 4, 5 or 6 but the profit, i.e., 4 corresponding to the weight
6 is highest among all the items; therefore, we add 4 at M[4][6] shown as below:
012345678
0000000000
1000222222
2000233355
3000133355
40002334
When i = 4, W = 7
The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the set of
weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 7. Here, if we add two items of
weights 3 and 4 then it will produce the maximum profit, i.e., (2 + 3) equals to 5, so we add 5 at
M[4][7] shown as below:
012345678
0000000000
1000222222
2000233355
3000233355
400023345
Page | 46
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
When i = 4, W = 8
The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the set of
weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 8. Here, if we add two items of
weights 3 and 4 then it will produce the maximum profit, i.e., (2 + 3) equals to 5, so we add 5 at
M[4][8] shown as below:
012345678
0000000000
1000222222
2000233355
3000233355
4000233455
As we can observe in the above table that 5 is the maximum profit among all the entries. The
pointer points to the last row and the last column having 5 value. Now we will compare 5 value
with the previous row; if the previous row, i.e., i = 3 contains the same value 5 then the pointer will
shift upwards. Since the previous row contains the value 5 so the pointer will be shifted upwards as
shown in the below table:
012345678
0000000000
1000222222
2000233355
3000233355
4000233455
Again, we will compare the value 5 from the above row, i.e., i = 2. Since the above row contains the
value 5 so the pointer will again be shifted upwards as shown in the below table:
012345678
0000000000
1000222222
2000233355
3000233355
4000233455
Page | 47
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Again, we will compare the value 5 from the above row, i.e., i = 1. Since the above row does not
contain the same value so we will consider the row i=1, and the weight corresponding to the row is
4. Therefore, we have selected the weight 4 and we have rejected the weights 5 and 6 shown below:
x = { 1, 0, 0}
The profit corresponding to the weight is 3. Therefore, the remaining profit is (5 - 3) equals to 2.
Now we will compare this value 2 with the row i = 2. Since the row (i = 1) contains the value 2;
therefore, the pointer shifted upwards shown below:
012345678
0000000000
1000222222
2000233355
3000233355
4000233455
Again we compare the value 2 with a above row, i.e., i = 1. Since the row i =0 does not contain the
value 2, so row i = 1 will be selected and the weight corresponding to the i = 1 is 3 shown below:
X = {1, 1, 0, 0}
The profit corresponding to the weight is 2. Therefore, the remaining profit is 0. We compare 0
value with the above row. Since the above row contains a 0 value but the profit corresponding to
this row is 0. In this problem, two weights are selected, i.e., 3 and 4 to maximize the profit.
Page | 48
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Lower Bound Theory uses a number of methods/techniques to find out the lower bound.
Concept/Aim: The main aim is to calculate a minimum number of comparisons required to execute
an algorithm.
Techniques:
1. Comparisons Trees.
1. Comparison trees:
In a comparison sort, we use only comparisons between elements to gain order information about
an input sequence (a1; a2......an).
• ai = aj equal to
To determine their relative order, if we assume all elements are distinct, then we just need to
consider ai ≤ aj '=' is excluded &, ≥,≤,>,< are equivalent.
Consider sorting three numbers a1, a2, and a3. There are 3! = 6 possible combinations:
Page | 1
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
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.
N! ≤2n
Page | 2
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
1. 1,2,3,4,5,6,7,8,9,10,11,12,13,14
Page | 3
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
1. 2, 4, 6, 8, 10, 12, 14
Thus, we will consider all the midpoints and we will make a tree of it by having stepwise
midpoints.
For Example
2k-1
Page | 4
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
23-1= 8-1=7
Where k = level=3
N! ≤ 2k-1
14 < 15
Where N = Nodes
Here, Internal Nodes will always be less than 2k in the Binary Search.
Step5:
n+1<= 2k
k >=
k >=log2(n+1)
Step6:
1. T (n) = k
Step7:
T (n) >=log2(n+1)
Here, the minimum number of Comparisons to perform a task of the search of n terms using Binary
Search
Another technique for obtaining lower bounds consists of making use of an "oracle."
Given some model of estimation such as comparison trees, the oracle tells us the outcome of each
comparison.
Page | 5
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
In order to derive a good lower bound, the oracle efforts it's finest to cause the algorithm to work as
hard as it might.
It does this by deciding as the outcome of the next analysis, the result which matters the most work
to be needed to determine the final answer.
And by keeping step of the work that is finished, a worst-case lower bound for the problem can be
derived.
Example: (Merging Problem) given the sets A (1: m) and B (1: n), where the information in A
and in B are sorted. Consider lower bounds for algorithms combining these two sets to give an
individual sorted set.
Consider that all of the m+n elements are specific and A (1) < A (2) < ....< A (m) and B (1) < B (2)
< ....< B (n).
Elementary combinatory tells us that there are C ((m+n), n)) ways that the A's and B's may merge
together while still preserving the ordering within A and B.
Thus, if we need comparison trees as our model for combining algorithms, then there will be C
((m+n), n)) external nodes and therefore at least log C ((m+n), m) comparisons are needed by any
comparison-based merging algorithm.
If we let MERGE (m, n) be the minimum number of comparisons used to merge m items with n
items then we have the inequality
The upper bound and lower bound can get promptly far apart as m gets much smaller than n.
1. State Space Method is a set of rules that show the possible states (n-tuples) that an algorithm can
assume from a given state of a single comparison.
2. Once the state transitions are given, it is possible to derive lower bounds by arguing that the
finished state cannot be reached using any fewer transitions.
4. Aim: When state changed count it that is the aim of State Space Method.
Page | 6
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
5. In this approach, we will count the number of comparison by counting the number of changes in
state.
6. Analysis of the problem to find out the smallest and biggest items by using the state space
method.
2. To find out largest & second largest elements from an array of an element.
9. For the largest item, we need 7 comparisons and what will be the second largest item?
Now we count those teams who lose the match with team A
10. In this no of comparisons are equal to the number of changes of states during the execution of
the algorithm.
Page | 7
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Firstly, A, B compare out or match between them. A wins and in C, D.C wins and so on. We can
assume that B wins and so on. We can assume that B wins in place of A it can be anything
depending on our self.
Page | 8
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Phase-3: This is a Phase in which teams which come under C-State are considered and there will be
matches between them to find out the team which is never winning at all.
In this Structure, we are going to move upward for denoting who is not winning after the match.
Page | 9
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Here H is the team which is never winning at all. By this, we fulfill our second aim to.
Page | 10
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Suppose we have 8 teams then states are there (as minimum) to find out which
one is never wins team.
Lower bound (L (n)) is a property of the particular issue i.e. the sorting problem, matrix
multiplication not of any particular algorithm solving that problem.
Lower bound theory says that no calculation can carry out the activity in less than that of (L (n))
times the units for arbitrary inputs i.e. that for every comparison based sorting algorithm must take
at least L (n) time in the worst case.
Trivial lower bounds are utilized to yield the bound best alternative is to count the number of
elements in the problems input that must be prepared and the number of output items that need to be
produced.
The lower bound theory is the method that has been utilized to establish the given algorithm in the
most efficient way which is possible. This is done by discovering a function g (n) that is a lower
bound on the time that any algorithm must take to solve the given problem. Now if we have an
algorithm whose computing time is the same order as g (n) , then we know that asymptotically we
cannot do better.
If f (n) is the time for some algorithm, then we write f (n) = Ω (g (n)) to mean that g (n) is the
lower bound of f (n) . This equation can be formally written, if there exists positive constants c and
n0 such that |f (n)| >= c|g (n)| for all n > n0. In addition for developing lower bounds within the
Page | 11
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
constant factor, we are more conscious of the fact to determine more exact bounds whenever this is
possible.
Deriving good lower bounds is more challenging than arrange efficient algorithms. This happens
because a lower bound states a fact about all possible algorithms for solving a problem. Generally,
we cannot enumerate and analyze all these algorithms, so lower bound proofs are often hard to
obtain.
Page | 12
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
N-Queens Problem
It can be seen that for n =1, the problem has a trivial solution, and no solution exists for n =2 and n
=3. So first we will consider the 4 queens problem and then generate it to n - queens problem.
Given a 4 x 4 chessboard and number the rows and column of the chessboard 1 through 4.
Since, we have to place 4 queens such as q1 q2 q3 and q4 on the chessboard, such that no two queens
attack each other. In such a conditional each queen must be placed on a different row, i.e., we put
queen "i" on row "i."
Now, we place queen q1 in the very first acceptable position (1, 1). Next, we put queen q2 so that
both these queens do not attack each other. We find that if we place q2 in column 1 and 2, then the
dead end is encountered. Thus the first acceptable position for q2 in column 3, i.e. (2, 3) but then no
position is left for placing queen 'q3' safely. So we backtrack one step and place the queen 'q2' in (2,
4), the next best possible solution. Then we obtain the position for placing 'q3' which is (3, 2). But
later this position also leads to a dead end, and no place is found where 'q4' can be placed safely.
Then we have to backtrack till 'q1' and place it to (1, 2) and then all other queens are placed safely
by moving q2 to (2, 4), q3 to (3, 1) and q4 to (4, 3). That is, we get the solution (2, 4, 1, 3). This is
one possible solution for the 4-queens problem. For another possible solution, the whole method is
repeated for all partial solutions. The other solutions for 4 - queens problems is (3, 1, 4, 2) i.e.
Page | 13
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
The implicit tree for 4 - queen problem for a solution (2, 4, 1, 3) is as follows:
Fig shows the complete state space for 4 - queens problem. But we can use backtracking method to
Page | 14
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
generate the necessary node and stop if the next node violates the rule, i.e., if two queens are
attacking.
It can be seen that all the solutions to the 4 queens problem can be represented as 4 - tuples (x1, x2,
x3, x4) where xi represents the column on which queen "qi" is placed.
Page | 15
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
6. Therefore, two queens lie on the duplicate diagonal if and only if |j-l|=|i-k|
Place (k, i) returns a Boolean value that is true if the kth queen can be placed in column i. It tests
both whether i is distinct from all previous costs x1, x2,....xk-1 and whether there is no other queen on
the same diagonal.
1. Place (k, i)
2. {
3. For j ← 1 to k - 1
4. do if (x [j] = i)
7. return true;
8. }
Place (k, i) return true if a queen can be placed in the kth row and ith column otherwise return is
false.
x [] is a global array whose final k - 1 values have been set. Abs (r) returns the absolute value of r.
1. N - Queens (k, n)
2. {
3. For i ← 1 to n
5. {
6. x [k] ← i;
7. if (k ==n) then
8. write (x [1....n));
Page | 16
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
9. else
11. }
12. }
Page | 17
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Naïve pattern searching is the simplest method among other pattern searching algorithms. It checks
for all character of the main string to the pattern. This algorithm is helpful for smaller texts. It does
not need any pre-processing phases. We can find substring by checking once for the string. It also
does not occupy extra space to perform the operation.
The time complexity of Naïve Pattern Search method is O(m*n). The m is the size of pattern and n
is the size of the main string.
Input:
Output:
Algorithm
naivePatternSearch(pattern, text)
Begin
for j := 0 to patLen, do
done
Page | 18
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
if j == patLen, then
done
End
Example
#include<iostream>
int j;
if(mainString[i+j] != pattern[j])
break;
(*index)++;
array[(*index)] = i;
Page | 19
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
int main() {
int locArray[mainString.size()];
Output
Page | 20
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
The Rabin-Karp-Algorithm
The Rabin-Karp string matching algorithm calculates a hash value for the pattern, as well as for
each M-character subsequences of text to be compared. If the hash values are unequal, the
algorithm will determine the hash value for next M-character sequence. If the hash values are equal,
the algorithm will analyze the pattern and the M-character sequence. In this way, there is only one
comparison per text subsequence, and character matching is only required when the hash values
match.
RABIN-KARP-MATCHER (T, P, d, q)
1. n ← length [T]
2. m ← length [P]
3. h ← dm-1 mod q
4. p ← 0
5. t0 ← 0
6. for i ← 1 to m
9. for s ← 0 to n-m
10. do if p = ts
Example: For string matching, working module q = 11, how many spurious hits does the Rabin-
Karp matcher encounters in Text T = 31415926535.......
1. T = 31415926535.......
2. P = 26
Page | 21
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Solution:
Page | 22
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 23
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Complexity:
The running time of RABIN-KARP-MATCHER in the worst case scenario O ((n-m+1) m but it
has a good average case running time. If the expected number of strong shifts is small O (1) and
prime q is chosen to be quite large, then the Rabin-Karp algorithm can be expected to run in time O
(n+m) plus the time to require to process spurious hits.
Page | 24
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Knuth-Morris and Pratt introduce a linear time algorithm for the string matching problem. A
matching time of O (n) is achieved by avoiding comparison with an element of 'S' that have
previously been involved in comparison with some element of the pattern 'p' to be matched. i.e.,
backtracking on the string 'S' never occurs
1. The Prefix Function (Π): The Prefix Function, Π for a pattern encapsulates knowledge about
how the pattern matches against the shift of itself. This information can be used to avoid a useless
shift of the pattern 'p.' In other words, this enables avoiding backtracking of the string 'S.'
2. The KMP Matcher: With string 'S,' pattern 'p' and prefix function 'Π' as inputs, find the
occurrence of 'p' in 'S' and returns the number of shifts of 'p' after which occurrences are found.
2. Π [1] ← 0
3. k ← 0
4. for q ← 2 to m
6. do k ← Π [k]
7. If P [k + 1] = P [q]
8. then k← k + 1
9. Π [q] ← k
10. Return Π
In the above pseudo code for calculating the prefix function, the for loop from step 4 to step 10 runs
'm' times. Step1 to Step3 take constant time. Hence the running time of computing prefix function is
O (m).
Page | 25
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Solution:
Π [1] = 0
k=0
Page | 26
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
The KMP Matcher with the pattern 'p,' the string 'S' and prefix function 'Π' as input, finds a match
of p in S. Following pseudo code compute the matching component of KMP algorithm:
KMP-MATCHER (T, P)
1. n ← length [T]
2. m ← length [P]
3. Π← COMPUTE-PREFIX-FUNCTION (P)
Page | 27
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
8. If P [q + 1] = T [i]
The for loop beginning in step 5 runs 'n' times, i.e., as long as the length of the string 'S.' Since step
1 to step 4 take constant times, the running time is dominated by this for the loop. Thus running
time of the matching function is O (n).
Let us execute the KMP Algorithm to find whether 'P' occurs in 'T.'
For 'p' the prefix function, ? was computed previously and is as follows:
Solution:
Initially: n = size of T = 15
m = size of P = 7
Page | 28
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 29
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 30
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 31
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Pattern 'P' has been found to complexity occur in a string 'T.' The total number of shifts that took
place for the match to be found is i-m = 13 - 7 = 6 shifts.
Page | 32
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Unit-4
Let there be N workers and N jobs. Any worker can be assigned to perform any job, incurring some
cost that may vary depending on the work-job assignment. It is required to perform all jobs by
assigning exactly one worker to each job and exactly one job to each agent in such a way that the
total cost of the assignment is minimized.
Page | 1
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
1. For each worker, we choose job with minimum cost from list of unassigned jobs (take
minimum entry from each row).
2. For each job, we choose a worker with lowest cost for that job from list of unassigned
workers (take minimum entry from each column).
Let’s take below example and try to calculate promising cost when Job 2 is assigned to worker A.
Since Job 2 is assigned to worker A (marked in green), cost becomes 2 and Job 2 and worker A
becomes unavailable (marked in red).
Page | 2
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Now we assign job 3 to worker B as it has minimum cost from list of unassigned jobs. Cost
becomes 2 + 3 = 5 and Job 3 and worker B also becomes unavailable.
Finally, job 1 gets assigned to worker C as it has minimum cost among unassigned jobs and job 4
gets assigned to worker C as it is only Job left. Total cost becomes 2 + 3 + 5 + 4 = 14.
Below diagram shows complete search space diagram showing optimal solution path in green.
Page | 3
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 4
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
An algorithm that uses random numbers to decide what to do next anywhere in its logic is called a
Randomized Algorithm. For example, in Randomized Quick Sort, we use a random number to pick
the next pivot (or we randomly shuffle the array). And in Karger’s algorithm, we randomly pick an
edge.
Some randomized algorithms have deterministic time complexity. For example, this
implementation of Karger’s algorithm has time complexity is O(E). Such algorithms are called
Monte Carlo Algorithms and are easier to analyse for worst case.
On the other hand, time complexity of other randomized algorithms (other than Las Vegas) is
dependent on value of random variable. Such Randomized algorithms are called Las Vegas
Algorithms. These algorithms are typically analysed for expected worst case. To compute expected
time taken in worst case, all possible values of the used random variable needs to be considered in
worst case and time taken by every possible value needs to be evaluated. Average of all evaluated
times is the expected worst case time complexity. Below facts are generally helpful in analysis os
such algorithms.
Linearity of Expectation
A Central Pivot is a pivot that divides the array in such a way that one side has at-least 1/4
elements.
// Sorts an array arr[low..high]
Page | 5
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
The important thing in our analysis is, time taken by step 2 is O(n).
How many times while loop runs before finding a central pivot?
The probability that the randomly chosen element is central pivot is 1/n.
Therefore, expected number of times the while loop runs is n
Thus, the expected time complexity of step 2 is O(n).
What is overall Time Complexity in Worst Case?
In worst case, each partition divides array such that one side has n/4 elements and other side has
3n/4 elements. The worst case height of recursion tree is Log 3/4 n which is O(Log n).
Page | 6
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Note that the above randomized algorithm is not the best way to implement randomized Quick Sort.
The idea here is to simplify the analysis as it is simple to analyse.
Page | 7
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
We strongly recommend to refer below post as a prerequisite of this. Randomized Algorithms | Set
1 (Introduction and Analysis)
Classification
Randomized algorithms are classified in two categories. Las Vegas: These algorithms always
produce correct or optimum result. Time complexity of these algorithms is based on a random value
and time complexity is evaluated as expected value. For example, Randomized QuickSort always
sorts an input array and expected worst case time complexity of QuickSort is O(nLogn). Monte
Carlo: Produce correct or optimum result with some probability. These algorithms have
deterministic running time and it is generally easier to find out worst case time complexity. For
example this implementation of Karger’s Algorithm produces minimum cut with probability greater
than or equal to 1/n2 (n is number of vertices) and has worst case time complexity as O(E). Another
example is Fermet Method for Primality Testing. Example to Understand Classification:
A Las Vegas algorithm for this task is to keep picking a random element until we find a 1. A Monte
Carlo algorithm for the same is to keep picking a random element until we either find 1 or we have
tried maximum allowed times say k. The Las Vegas algorithm always finds an index of 1, but time
complexity is determined as expect value. The expected number of trials before success is 2,
therefore expected time complexity is O(1). The Monte Carlo Algorithm finds a 1 with probability
[1 – (1/2)k]. Time complexity of Monte Carlo is O(k) which is deterministic
• Consider a tool that basically does sorting. Let the tool be used by many users and there are
few users who always use tool for already sorted array. If the tool uses simple (not
randomized) QuickSort, then those few users are always going to face worst case situation.
On the other hand if the tool uses Randomized QuickSort, then there is no user that always
gets worst case. Everybody gets expected O(n Log n) time.
• Load Balancing.
Page | 8
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
• Data Structures: Hashing, Sorting, Searching, Order Statistics and Computational Geometry.
• Algebraic identities: Polynomial and matrix identity verification. Interactive proof systems.
• Probabilistic existence proofs: Show that a combinatorial object arises with non-zero
probability among objects drawn from a suitable probability space.
Randomized algorithms are algorithms that use randomness as a key component in their
operation. They can be used to solve a wide variety of problems, including optimization, search,
and decision-making. Some examples of applications of randomized algorithms include:
1. Monte Carlo methods: These are a class of randomized algorithms that use random
sampling to solve problems that may be deterministic in principle, but are too complex to
solve exactly. Examples include estimating pi, simulating physical systems, and solving
optimization problems.
2. Randomized search algorithms: These are algorithms that use randomness to search for
solutions to problems. Examples include genetic algorithms and simulated annealing.
3. Randomized data structures: These are data structures that use randomness to improve their
performance. Examples include skip lists and hash tables.
4. Randomized load balancing: These are algorithms used to distribute load across a network
of computers, using randomness to avoid overloading any one computer.
5. Randomized encryption: These are algorithms used to encrypt and decrypt data, using
randomness to make it difficult for an attacker to decrypt the data without the correct key.
Page | 9
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Given an undirected and unweighted graph, find the smallest cut (smallest number of edges that
disconnects the graph into two components).
The input graph may have parallel edges.
For example consider the following example, the smallest cut has 2 edges.
A Simple Solution use Max-Flow based s-t cut algorithm to find minimum cut. Consider every pair
of vertices as source ‘s’ and sink ‘t’, and call minimum s-t cut algorithm to find the s-t cut. Return
minimum of all s-t cuts. Best possible time complexity of this algorithm is O(V5) for a graph.
[How? there are total possible V2 pairs and s-t cut algorithm for one pair takes O(V*E) time and E
= O(V2)].
Below is simple Karger’s Algorithm for this purpose. Below Karger’s algorithm can be
implemented in O(E) = O(V2) time.
c) Remove self-loops
Page | 10
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Let the next randomly picked edge be ‘d’. We remove this edge and combine vertices (0,1) and 3.
Now graph has two vertices, so we stop. The number of edges in the resultant graph is the cut
produced by Karger’s algorithm.
Karger’s algorithm is a Monte Carlo algorithm and cut produced by it may not be minimum. For
example, the following diagram shows that a different order of picking random edges produces a
min-cut of size 3.
Page | 11
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 12
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Problem1: Given a flow network G = (V, E), the maximum flow problem is to find a flow with
maximum value.
Problem 2: The multiple source and sink maximum flow problem is similar to the maximum flow
problem, except there is a set {s1,s2,s3.......sn} of sources and a set {t1,t2,t3..........tn} of sinks.
Fortunately, this problem is no solid than regular maximum flow. Given multiple sources and sink
flow network G, we define a new flow network G' by adding
• A super source s,
• A super sink t,
• For each si, add edge (s, si) with capacity ∞, and
Figure shows a multiple sources and sinks flow network and an equivalent single source and sink
flow network
Residual Networks: The Residual Network consists of an edge that can admit more net flow.
Suppose we have a flow network G = (V, E) with source s and sink t. Let f be a flow in G, and
examine a pair of vertices u, v ∈ V. The sum of additional net flow we can push from u to v before
exceeding the capacity c (u, v) is the residual capacity of (u, v) given by
Page | 13
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
When the net flow f (u, v) is negative, the residual capacity cf (u,v) is greater than the capacity c (u,
v).
For Example: if c (u, v) = 16 and f (u, v) =16 and f (u, v) = -4, then the residual capacity cf (u,v) is
20.
Given a flow network G = (V, E) and a flow f, the residual network of G induced by f is Gf = (V,
Ef), where
That is, each edge of the residual network, or residual edge, can admit a strictly positive net flow.
Augmenting Path: Given a flow network G = (V, E) and a flow f, an augmenting path p is a
simple path from s to t in the residual networkGf. By the solution of the residual network, each edge
(u, v) on an augmenting path admits some additional positive net flow from u to v without violating
the capacity constraint on the edge.
Let G = (V, E) be a flow network with flow f. The residual capacity of an augmenting path p is
The residual capacity is the maximal amount of flow that can be pushed through the augmenting
path. If there is an augmenting path, then each edge on it has a positive capacity. We will use this
fact to compute a maximum flow in a flow network.
Page | 14
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 15
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Ford-Fulkerson Algorithm
Initially, the flow of value is 0. Find some augmenting Path p and increase flow f on each edge of p
by residual Capacity cf (p). When no augmenting path exists, flow f is a maximum flow.
1. Initialize flow f to 0
4. Return f
FORD-FULKERSON (G, s, t)
2. do f [u, v] ← 0
3. f [u, v] ← 0
5. do cf (p)←min?{ Cf (u,v):(u,v)is on p}
8. f [u, v] ←-f[u,v]
Example: Each Directed Edge is labeled with capacity. Use the Ford-Fulkerson algorithm to find
the maximum flow.
Page | 16
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Solution: The left side of each part shows the residual network Gf with a shaded augmenting path
p,and the right side of each part shows the net flow f.
Page | 17
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Page | 18
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
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.
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:
Page | 1
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
NP Class
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
Page | 2
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
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:
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:
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:
Page | 3
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
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 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.
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:
Page | 4
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
NP Problem:
The NP problems set of problems whose solutions are hard to find but easy to verify and are
solved by Non-Deterministic Machine in polynomial time.
Page | 5
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
NP-Hard Problem:
NP-Complete Problem:
NP-hard NP-Complete
To solve this problem, it do not have to be in To solve this problem, it must be both NP and
NP . NP-hard problems.
Not all NP-hard problems are NP-complete. All NP-complete problems are NP-hard
Page | 6
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
In computational complexity theory, the Cook–Levin theorem, also known as Cook’s theorem,
states that the Boolean satisfiability problem is NP-complete. That is, it is in NP, and any problem
in NP can be reduced in polynomial time by a deterministic Turing machine to the Boolean
satisfiability problem.
Stephen Arthur Cook and L.A. Levin in 1973 independently proved that the satisfiability
problem(SAT) is NP-complete. Stephen Cook, in 1971, published an important paper titled ‘The
complexity of Theorem Proving Procedures’, in which he outlined the way of obtaining the proof
of an NP-complete problem by reducing it to SAT. He proved Circuit-SAT and 3CNF-SAT
problems are as hard as SAT. Similarly, Leonid Levin independently worked on this problem in
the then Soviet Union. The proof that SAT is NP-complete was obtained due to the efforts of these
two scientists. Later, Karp reduced 21 optimization problems, such as Hamiltonian tour, vertex
cover, and clique, to the SAT and proved that those problems are NP-complete.
Given a boolean expression F having n variables x1,x2,….,xn, and Boolean operators, is it possible
to have an assignment for variables true or false such that binary expression F is true?
This problem is also known as the formula – SAT. An SAT(formula-SAT or simply SAT) takes
a Boolean expression F and checks whether the given expression(or formula) is satisfiable. A
Boolean expression is said to be satisfactory for some valid assignments of variables if the
evaluation comes to be true. Prior to discussing the details of SAT, let us now discuss some
important terminologies of a Boolean expression.
• Boolean variable: A variable, say x, that can have only two values, true or false, is called a
boolean variable
• Literal: A literal can be a logical variable, say x, or the negation of it, that is x or x̄; x is
called a positive literal, and x̄ is called the negative literal
• Clause: A sequence of variables(x1,x2,….,xn) that can be separated by a logical OR
operator is called a clause. For example, (x1 V x2 V x3) is a clause of three literals.
Page | 7
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
• Expressions: One can combine all the preceding clauses using a Boolean operator to form
an expression.
• CNF form: An expression is in CNF form(conjunctive normal form) if the set of clauses
are separated by an AND (^), operator, while the literals are connected by an OR (v)
operator. The following is an example of an expression in the CNF form:
o f = (x1 V x̄2 V x3) ∧ (x1 V x̄3 V x2)
• 3 – CNF: An expression is said to be in 3-CNF if it is in the conjunctive normal form, and
every clause has exact three literals.
Thus, an SAT is one of the toughest problems, as there is no known algorithm other than the brute
force approach. A brute force algorithm would be an exponential-time algorithm, as 2n possible
assignments need to be tried to check whether the given Boolean expression is true or not. Stephen
Cook and Leonid Levin proved that the SAT is NP-complete.
Cook demonstrated the reduction of other hard problems to SATs. Karp provided proof of 21
important problems, Such as Hamiltonian tour, vertex cover, and clique, by reducing it to SAT
using Karp reduction.
Let us briefly introduce the three types of SATs. They are as follows:
1. Circuit- SAT: A circuit-SAT can be stated as follows: given a Boolean circuit, which is a
collection of gates such as AND, OR, and NOT, and n inputs, is there any input
assignments of Boolean variables so that the output of the given circuit is true? Again, the
difficulty with these problems is that for n inputs to the circuit. 2n possible outputs should
be checked. Therefore, this brute force algorithm is an exponential algorithm and hence this
is a hard problem.
2. CNF-SAT: This problem is a restricted problem of SAT, where the expression should be
in a conjunctive normal form. An expression is said to be in a conjunction form if all the
clauses are connected by the Boolean AND operator. Like in case of a SAT, this is about
assigning truth values to n variables such that the output of the expression is true.
3. 3-CNF-SAT(3-SAT): This problem is another variant where the additional restriction is
that the expression is in a conjunctive normal form and that every clause should contain
Page | 8
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
exactly three literals. This problem is also about assigning n assignments of truth values to
n variables of the Boolean expression such that the output of the expression is true. In
simple words, given an expression in 3-CNF, a 3-SAT problem is to check whether the
given expression is satisfiable.
These problems can be used to prove the NP-completeness of some important problems.
Page | 9
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
SAT Problem: SAT(Boolean Satisfiability Problem) is the problem of determining if there exists
an interpretation that satisfies a given boolean formula. It asks whether the variables of a given
boolean formula can be consistently replaced by the values TRUE or FALSE in such a way that
the formula evaluates to TRUE. If this is the case, the formula is called satisfiable. On the other
hand, if no such assignment exists, the function expressed by the formula is FALSE for all
possible variable assignments and the formula is unsatisfiable.
Problem: Given a boolean formula f, the problem is to identify if the formula f has a satisfying
assignment or not.
Explanation: An instance of the problem is an input specified to the problem. An instance of the
problem is a boolean formula f. Since an NP-complete problem is a problem which is both NP and
NP-Hard, the proof or statement that a problem is NP-Complete consists of two parts:
If the 2nd condition is only satisfied then the problem is called NP-Hard.
But it is not possible to reduce every NP problem into another NP problem to show its NP-
Completeness all the time i.e., to show a problem is NP-complete then prove that the problem is in
NP and any NP-Complete problem is reducible to that i.e. if B is NP-Complete and B ≤ PC For C in
NP, then C is NP-Complete. Thus, it can be verified that the SAT Problem is NP-Complete using
the following propositions:
SAT is in NP:
It any problem is in NP, then given a ‘certificate’, which is a solution to the problem and an
instance of the problem(a boolean formula f) we will be able to check (identify if the solution is
correct or not) certificate in polynomial time. This can be done by checking if the given
assignment of variables satisfies the boolean formula.
Page | 10
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
SAT is NP-Hard:
In order to prove that this problem is NP-Hard then reduce a known problem, Circuit-SAT in this
case to our problem. The boolean circuit C can be corrected into a boolean formula as:
This transformation can be done in linear time. The following propositions now hold:
• If there is a set of input, variable values satisfying the circuit then it can derive an
assignment for the formula f that satisfies the formula. This can be simulated by computing
the output of every gate in the circuit.
• If there is a satisfying assignment for the formula f, this can satisfy the boolean circuit after
the removal of the newly added variables.
Page | 11
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
Explanation –
First let us understand the notion of an instance of a problem. An instance of a problem is nothing
but an input to the given problem. An instance of the Vertex Cover problem is a graph G (V, E)
and a positive integer k, and the problem is to check whether a vertex cover of size at most k exists
in G. Since an NP Complete problem, by definition, is a problem which is both in NP and NP hard,
the proof for the statement that a problem is NP Complete consists of two parts:
If any problem is in NP, then, given a ‘certificate’ (a solution) to the problem and an instance
of the problem (a graph G and a positive integer k, in this case), we will be able to verify
(check whether the solution given is correct or not) the certificate in polynomial time.
The certificate for the vertex cover problem is a subset V’ of V, which contains the vertices in
the vertex cover. We can check whether the set V’ is a vertex cover of size k using the
following strategy (for a graph G(V, E)):
Page | 12
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
It is plain to see that this can be done in polynomial time. Thus the vertex cover problem is
in the class NP.
To prove that Vertex Cover is NP Hard, we take some problem which has already been proven
to be NP Hard, and show that this problem can be reduced to the Vertex Cover problem. For
this, we consider the Clique problem, which is NP Complete (and hence NP Hard).
“In computer science, the clique problem is the computational problem of finding cliques
(subsets of vertices, all adjacent to each other, also called complete subgraphs) in a graph.”
Here, we consider the problem of finding out whether there is a clique of size k in the given
graph. Therefore, an instance of the clique problem is a graph G (V, E) and a non-negative
integer k, and we need to check for the existence of a clique of size k in G.
Now, we need to show that any instance (G, k) of the Clique problem can be reduced to an
instance of the vertex cover problem. Consider the graph G’ which consists of all edges not in
G, but in the complete graph using all vertices in G. Let us call this the complement of G. Now,
the problem of finding whether a clique of size k exists in the graph G is the same as the
problem of finding whether there is a vertex cover of size |V| – k in G’. We need to show that
this is indeed the case.
Assume that there is a clique of size k in G. Let the set of vertices in the clique be V’. This
means |V’| = k. In the complement graph G’, let us pick any edge (u, v). Then at least one of u
or v must be in the set V – V’. This is because, if both u and v were from the set V’, then the
edge (u, v) would belong to V’, which, in turn would mean that the edge (u, v) is in G. This is
not possible since (u, v) is not in G. Thus, all edges in G’ are covered by vertices in the set V –
V’.
Now assume that there is a vertex cover V’’ of size |V| – k in G’. This means that all edges in
G’ are connected to some vertex in V’’. As a result, if we pick any edge (u, v) from G’, both of
Page | 13
Swami Keshvanand Institute of Technology, Management & Gramothan,
Ramnagaria, Jagatpura, Jaipur-302017, INDIA
Approved by AICTE, Ministry of HRD, Government of India
Recognized by UGC under Section 2(f) of the UGC Act, 1956
Tel. : +91-0141- 5160400Fax: +91-0141-2759555
E-mail: [email protected] Web: www.skit.ac.in
them cannot be outside the set V’’. This means, all edges (u, v) such that both u and v are
outside the set V’’ are in G, i.e., these edges constitute a clique of size k.
Thus, we can say that there is a clique of size k in graph G if and only if there is a vertex cover of
size |V| – k in G’, and hence, any instance of the clique problem can be reduced to an instance of
the vertex cover problem. Thus, vertex cover is NP Hard. Since vertex cover is in both NP and NP
Hard classes, it is NP Complete.
To understand the proof, consider the following example graph and its complement:
Page | 14