0% found this document useful (0 votes)
17 views161 pages

5CS4-03 Analysis of Algorithm Loveleen-Kumar

The document outlines the course file for 'Analysis of Algorithms' (5CS4-05) at Swami Keshvanand Institute of Technology, detailing the vision and mission of the institute and its Computer Science department. It includes the course syllabus, prerequisites, and course outcomes, emphasizing algorithm analysis techniques such as divide and conquer, greedy methods, and dynamic programming. Additionally, it provides a quiz section to assess understanding of key concepts in algorithm efficiency and complexity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views161 pages

5CS4-03 Analysis of Algorithm Loveleen-Kumar

The document outlines the course file for 'Analysis of Algorithms' (5CS4-05) at Swami Keshvanand Institute of Technology, detailing the vision and mission of the institute and its Computer Science department. It includes the course syllabus, prerequisites, and course outcomes, emphasizing algorithm analysis techniques such as divide and conquer, greedy methods, and dynamic programming. Additionally, it provides a quiz section to assess understanding of key concepts in algorithm efficiency and complexity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 161

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

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

Vision and Mission of Institute

Vision:“To promote higher learning in technology and industrial research to


make our country a global player.”

Mission:“To promote quality education, training and research in the field of


engineering by establishing effective interface with industry and to encourage
the faculty to undertake industry sponsored projects for the students.”

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

Vision of CSE Department

Vision of CSE Department is to:

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.

Mission of CSE Department

Mission of CSE Department is to:

Delivering resources in IT enable domain through:


M1: Effective Industry interaction and project based learning.
M2:Motivating our students for employability,entrepreneurship,research and higher
education.
M3: Providing excellent engineering skills in a state-of-the-art infrastructure.

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

Students should have basic knowledge of:-


• Programming languages like C or C++ or Java or Python etc.
• Data Structure
• Mathematics.
Students should have basic knowledge of programming and mathematics. The student should know data structure
very well. Moreover, it is preferred if the students have basic understanding of Formal Language and Automata
Theory. A version of what is normally called discrete mathematics, combined with first-year (university) level
calculus are the primary requirements to understanding many (basic) algorithms and their analysis.
Specialized or advanced algorithms can require additional or advanced mathematical background, such as in
statistics / probability (scientific and financial programming), abstract algebra, and number theory (i.e. for
cryptography).
it is comfort and fluency with the mathematical formalism. Learn basic set terminology and the corresponding
formalism.

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

Text And Reference Book


Text Book:

1. Coremen,Leiserson,Rivest: "Introduction to Algorithm",Second Edition - PHI

2. Gilles Brassard,PaulBrately:- " Fundamental of Algorithmics" – PHI

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:

1. Jon KleinBerg, Eva Tardos: "Algorithm Design", Pearson Edition

2. Harsh Bhasin: "Algorithm Design a,nd Analysis", Oxford Higher Education.

3. “Data Structures and Algorithms Made Easy: Data Structures and Algorithmic Puzzles” by Narasimha
Karumanchi

4.“The Algorithm Design Manual” by Steven S Skiena

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 Outcomes Competency Level

Course Bloom
Course name Course Outcomes PO Indicators
Code Level

CO1: describe asymptotic


analysis concepts and use them
to evaluate the time- 1.1,1.2,1.5,3.1,3.9
1,2
PSO 1.1,1.2
complexity of different
algorithms

CO2:explain, apply and


analyze the divide and
1.1,1.2,1.4,1.5,2.1,2.3,2.
conquer, greedy method and 4, 2.5,2.6,2.7
3,6 ,3.1,3.6,3.7,
dynamic programming
3.8,3.9,5.2,5.3,5.5,5.6
techniques to solve various PSO 1.1,1.2
Analysis and engineering problems
5CS4-05 Design Of
Algorithm
CO3:discuss and use Branch 2.3,2.4,2.5,2.7,2.8
and Bound, and pattern- 3.1,3.6,3.7,3.8,3.9,
3,4,6
5.2,5.3,5.5,5.6 PSO
matching algorithms 1.1,1.2

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

CO5:understand the concepts


of NP-Hard and NP-Complete 2,5 PSO 1.1,1.2
problems

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)

3.Two main measures for the efficiency of an algorithm are


A. Processor and memory C. Time and space
B. Complexity and capacity D. Data and space

4.The total number of comparisons in a bubble sort is


A. 0(log n) B. 0(n log n) C. 0(n) D. None

5.The space factor when determining the efficiency of algorithm is measured by


A. Counting the maximum memory needed by the algorithm
B. Counting the minimum memory needed by the algorithm
C. Counting the average memory needed by the algorithm
D. Counting the maximum disk space needed by the algorithm

6 What's the minimum time needed to merge all n


Files ? If n=5 and (F1, F2, F3, F4, F5)= (20, 30, 10, 5, 30).
a. 140 b.270 c.205 d. none

7. The optimal solution to the knapsack instance n=3, M=15 (P1,P2,P3)=(25,24,15)


(W1,W2,W3) = (18,15,10) is
a) 28.2 b) 31.0 c) 31.5 d) 41.5

8. The solution set X=(X1,X2,X3) for the problem given in Q.7 is


a) )(1/15,2/15,0) b) (0,2/3,1) c)(0,1,1/2)
d) None of these

9. Total number of spanning tree in a complete graph with 5 nodes are


a)52 b)53 c)10 d) 100

10.The concept of order BigO is important because


A. It can be used to decide the best algorithm that solves a given problem
B. It determines the maximum size of a problem that can be solved in a given given amount of time
C. It is the lower bound of the growth rate of algorithm
D. Both A and B

11.The time factor when determining the efficiency of algorithm is measured by


A. Counting microseconds
B. Counting the number of key operations
C. Counting the number of statements
D. Counting the kilobytes of algorithm

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

12. In worst case Quick Sort has order


A. O (n log n) B. O (n2/2) C. O (log n) D. O (n2/4)

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

14. The best average behaviour is shown by


A. Quick Sort B. Merge Sort C. Insertion Sort
D. Heap Sort

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

18) Strassen‘s algorithm is able to perform matrix multiplication in time_______.


(a)O(n2.61) (b) O(n2.71) (c) O(n2.81) (d) O(n3)
19) Strassen‘s matrix multiplication algorithm (C = AB),if the matrices A and B are not of type 2 nX2n, the missing rows
and columns are filled with ___________.
(a)0’s (b) 1‘s (c) -1‘s (d) 2‘s
20) Strassen‘s matrix multiplication algorithm (C = AB), the matrix C can be computed using only 7 block
multiplications and 18 block additions or subtractions. How many additions and how many subtractions are there out of
18?
(a) 9 and 9 (b) 6 and 12 (c)12 and 6 (d) none
21) Which of following is Pattern Matching Algorithm.
a) Naive Matching b) RabinKarp c) KMP matching d) All of above
22)‘’This is a text “ Pattern=TEXT. Find the index of Pattern
a) 8 b)11 c)9 d.10
23)Hash Value is calculated by
a) RabinKarp b)KMPMatching c) Boyer Moyer d) None
24) Bad Match Table is constructed in
a) KMP b) Rabin Karp c) Boyer Moore d)None
25) Complexity of KMP in worst case

a) O(N) b)O(n+m) c) O(m) d none of above

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

40 14-10-2020 Quadratic Assignment Problem


41 16-10-2020 Randomized Algorithm
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 Subject Code: 5CS4-05
Assignment: I Session (I/II/III): I Max Marks: 20
Submitted By: Mr. Loveleen Kumar

A. Distribution of Course Outcome and Bloom’s Taxonomy in Question Paper

Q. No Questions Marks CO BL

1 Define Space and Time Complexity. State three cases of Master’s theorem. 1 CO1 L1

2 What are Asymptotic Notations? Name them. 1 CO1 L1

3 Differentiate Greedy and Dynamic Programming Technique. 1 CO2 L2


4 Differentiate Kruskal’s and Prim’s Algorithm. 1 CO2 L2
5 Explain Quick sort algorithm along with its analysis. 2 CO1 L2
6 Find complexity of T(n)=3T(n/4)+cn2 using Recurrence Tree. 2 CO1 L3

7 Show all the steps to multiply two matrices using Strassen’s Matrix 2 CO2 L3
Multiplication.

8 Explain Greedy approach with suitable example. Consider a Knapsack of 2 CO2 L2


capacity 60 and items with profits as (280,100,120,120) and weight
(40,10,20,24).What is the maximum Profit earned. (use greedy approach)

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>

BL – Bloom’s Taxonomy Level


(1- Remembering, 2- Understanding, 3 – Applying, 4 – Analyzing, 5 – Evaluating, 6 - Creating)

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

B. Questions and Course Outcomes (COs) Mapping in terms of correlation

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

1-Low Correlation; 2- Moderate Correlation; 3- Substantial Correlation

C. Mapping of Bloom’s Level and Course Outcomes with Question Paper

Bloom’s Level Mapping CO Mapping

Bloom’s Level Percentage CO Percentage

BL1 10.00% (2) CO1 30.00% (6)


BL2 30.00% (6) CO2 70.00% (14)
BL3 60.00% (12) CO3 -

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

A. Distribution of Course Outcome and Bloom’s Taxonomy in Question Paper

Q. No Questions Marks CO BL

1 Define Cook’s Theorem. 1 CO5 L1


2 Differentiate between Feasible and Optimal solution. 1 CO5 L2

3 What are Decision problems. 1 CO5 L1


4 What do you understand by Satisfiability problem(SAT). 1 CO5 L1
5 What is MIN-CUT? 1 CO4 L1
6 Name different String Matching Algorithms. 1 CO3 L1
7 Define Assignment problem. 1 CO3 L1
8 What is vertex cover? 1 CO5 L1
9 Differentiate between P and NP problem. 1 CO5 L2
10 What is Approximation Algorithm? 1 CO4 L1
11 Compare Las Vegas and Monte Carlo Approach. 2 CO4 L2
12 Explain Approximation Algorithm for Vertex Cover. 2 CO5 L2
13 Suppose T = 1011101110 P = 111. Find all the Valid Shift using 2 CO3 L3
Naïve String Matching Algorithm

14 2 CO4 L3
Give Randomized Algorithm to find Min cut for the following Graph.

15 Prove SAT is a NP complete Problem. 2 CO5 L2


16 Find Max flow using Ford Fulkerson method for the following 5 CO4 L3

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.

BL – Bloom’s Taxonomy Level


(1- Remembering, 2- Understanding, 3 – Applying, 4 – Analyzing, 5 – Evaluating, 6 - Creating)

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

B. Questions and Course Outcomes (COs) Mapping in terms of correlation

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

1-Low Correlation; 2- Moderate Correlation; 3- Substantial Correlation

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

C. Mapping of Bloom’s Level and Course Outcomes with Question Paper

Bloom’s Level Mapping CO Mapping

Bloom’s Level Percentage CO Percentage

BL1 20.00% (8) CO1 -

BL2 32.50% (13) CO2 -

BL3 47.50%(19) CO3 35.00% (14)

BL4 - CO4 40.00%(16)

BL5 - CO5 25.00%(10)

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.

II. Asymptotic Notations


Asymptotic notations are the mathematical notations used to describe the running time of an
algorithm when the input tends towards a particular value or a limiting value.
For example: In bubble sort, when the input array is already sorted, the time taken by the algorithm
is linear i.e. the best case.
But, when the input array is in reverse condition, the algorithm takes the maximum time
(quadratic) to sort the elements i.e. the worst case.
When the input array is neither sorted nor in reverse order, then it takes average time. These
durations are denoted using asymptotic notations.
There are mainly three asymptotic notations:
• Big-O notation
• Omega notation
• Theta notation

III. Big-O Notation (O-notation)


Big-O notation represents the upper bound of the running time of an algorithm. Thus, it gives the
worst-case complexity of an algorithm.

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

Big-O gives the upper bound of a function

O(g(n)) = { f(n): there exist positive constants c and n0


such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0 }
The above expression can be described as a function f(n) belongs to the set O(g(n)) if there exists a
positive constant c such that it lies between 0 and cg(n), for sufficiently large n.
For any value of n, the running time of an algorithm does not cross the time provided by O(g(n)).
Since it gives the worst-case running time of an algorithm, it is widely used to analyze an
algorithm as we are always interested in the worst-case scenario.

IV. Omega Notation (Ω-notation)


Omega notation represents the lower bound of the running time of an algorithm. Thus, it provides
the best case complexity of an algorithm.

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

Omega gives the lower bound of a function


Ω(g(n)) = { f(n): there exist positive constants c and n0
such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }
The above expression can be described as a function f(n) belongs to the set Ω(g(n)) if there exists a
positive constant c such that it lies above cg(n), for sufficiently large n.
For any value of n, the minimum time required by the algorithm is given by Omega Ω(g(n)).

V. Theta Notation (Θ-notation)


Theta notation encloses the function from above and below. Since it represents the upper and the
lower bound of the running time of an algorithm, it is used for analyzing the average-case
complexity of an algorithm.

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

Theta bounds the function within constants factors


For a function g(n), Θ(g(n)) is given by the relation:
Θ(g(n)) = { f(n): there exist positive constants c1, c2 and n0
such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }
The above expression can be described as a function f(n) belongs to the set Θ(g(n)) if there exist
positive constants c1 and c2 such that it can be sandwiched between c1g(n) and c2g(n), for
sufficiently large n.
If a function f(n) lies anywhere in between c1g(n) and c2g(n) for all n ≥ n0, then f(n) is said to be
asymptotically tight bound.

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

Example2 Consider the Recurrence

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)

Repeat the procedure for i times

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

Recursion Tree Method


Steps to Solve Recurrence Relations Using Recursion Tree Method-
Step-01:
Draw a recursion tree based on the given recurrence relation.
Step-02:
Determine-
• Cost of each level
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

• Total number of levels in the recursion tree


• Number of nodes in the last level
• Cost of the last level
Step-03:
Add cost of all the levels of the recursion tree and simplify the expression so obtained in terms of
asymptotic notation.
Following problems clearly illustrates how to apply these steps.
PRACTICE PROBLEMS BASED ON RECURSION TREE-
Problem-01:
Solve the following recurrence relation using recursion tree method-
T(n) = 2T(n/2) + n
Solution-
Step-01:
Draw a recursion tree based on the given recurrence relation.
The given recurrence relation shows-
• A problem of size n will get divided into 2 sub-problems of size n/2.
• Then, each sub-problem of size n/2 will get divided into 2 sub-problems of size n/4 and so
on.
• At the bottom most layer, the size of sub-problems will reduce to 1.
This is illustrated through following recursion tree-

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

The given recurrence relation shows-


• The cost of dividing a problem of size n into its 2 sub-problems and then combining its
solution is n.
• The cost of dividing a problem of size n/2 into its 2 sub-problems and then combining its
solution is n/2 and so on.
This is illustrated through following recursion tree where each node represents the cost of the
corresponding sub-problem-

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

Draw a recursion tree based on the given recurrence relation.


The given recurrence relation shows-
• A problem of size n will get divided into 2 sub-problems- one of size n/5 and another of
size 4n/5.
• Then, sub-problem of size n/5 will get divided into 2 sub-problems- one of size n/52 and
another of size 4n/52.
• On the other side, sub-problem of size 4n/5 will get divided into 2 sub-problems- one of
size 4n/52 and another of size 42n/52 and so on.
• At the bottom most layer, the size of sub-problems will reduce to 1.
This is illustrated through following recursion tree-

The given recurrence relation shows-


• The cost of dividing a problem of size n into its 2 sub-problems and then combining its
solution is n.
• The cost of dividing a problem of size n/5 into its 2 sub-problems and then combining its
solution is n/5.
• The cost of dividing a problem of size 4n/5 into its 2 sub-problems and then combining its
solution is 4n/5 and so on.
This is illustrated through following recursion tree where each node represents the cost of the
corresponding sub-problem-

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

• 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-log5/4n has 2log5/4n nodes
Step-05:
Determine cost of last level-
Cost of last level = 2log5/4n x T(1) = θ(2log5/4n) = θ(nlog5/42)
Step-06:
Add costs of all the levels of the recursion tree and simplify the expression so obtained in terms of
asymptotic notation-

= 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-

= cn2 { 1 + (3/16) + (3/16)2 + ……… } + θ(nlog43)


Now, { 1 + (3/16) + (3/16)2 + ……… } forms an infinite Geometric progression.
On solving, we get-
= (16/13)cn2 { 1 – (3/16)log4n } + θ(nlog43)
= (16/13)cn2 – (16/13)cn2 (3/16)log4n + θ(nlog43)
= O(n2)

Divide and conquer algorithms


The two sorting algorithms we've seen so far, selection sort and insertion sort, have worst-case
running times of Θ(n2) \Theta(n^2) Θ(n2)\Theta, left parenthesis, n, squared, right parenthesis.
When the size of the input array is large, these algorithms can take a long time to run. In this
tutorial and the next one, we'll see two other sorting algorithms, merge sort and quicksort, whose
running times are better. In particular, merge sort runs in Θ(nlg⁡n) \Theta(n \lg n) Θ(nlgn)\Theta,
left parenthesis, n, \lg, n, right parenthesis time in all cases, and quicksort runs in Θ(nlg⁡n)
\Theta(n \lg n) Θ(nlgn)\Theta, left parenthesis, n, \lg, n, right parenthesis time in the best case and
on average, though its worst-case running time is Θ(n2) \Theta(n^2) Θ(n2)\Theta, left parenthesis,
n, squared, right parenthesis. Here's a table of these four sorting algorithms and their running
times:
Algorithm Worst-case running Best-case running time Average-case running
time time
Selection Θ(n2) \Theta(n^2) Θ(n2) \Theta(n^2) Θ(n2) \Theta(n^2)
sort Θ(n2)\Theta, left Θ(n2)\Theta, left Θ(n2)\Theta, left
parenthesis, n, squared, parenthesis, n, squared, parenthesis, n, squared,
right parenthesis right parenthesis right parenthesis
Insertion Θ(n2) \Theta(n^2) Θ(n) \Theta(n) Θ(n2) \Theta(n^2)
sort Θ(n2)\Theta, left Θ(n)\Theta, left Θ(n2)\Theta, left
parenthesis, n, squared, parenthesis, n, right parenthesis, n, squared,
right parenthesis parenthesis right parenthesis

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 Θ(nlg⁡n) \Theta(n \lg n) Θ(nlg⁡n) \Theta(n \lg n) Θ(nlg⁡n) \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) Θ(nlg⁡n) \Theta(n \lg n) Θ(nlg⁡n) \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):

If we expand out two more recursive steps, it looks like this:


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

Because divide-and-conquer creates at least two subproblems, a divide-and-conquer algorithm


makes multiple recursive calls.

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 if (x > arr[mid]) // x is on the right side


low = mid + 1

else // x is on the left side


high = mid - 1
2. Recursive Method (The recursive method follows the divide and conquers approach)
binarySearch(arr, x, low, high)
if low > high
return False

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

else if x > arr[mid] // x is on the right side


return binarySearch(arr, x, mid + 1, high)

else // x is on the right side


return binarySearch(arr, x, low, mid - 1)
Illustration of Binary Search Algorithm:

Merge Sort Algorithm


The Merge Sort algorithm is a sorting algorithm that is based on the Divide and Conquer
paradigm. In this algorithm, the array is initially divided into two equal halves and then they are
combined in a sorted manner.
Merge Sort Working Process:
Think of it as a recursive algorithm continuously splits the array in half until it cannot be further
divided. This means that if the array becomes empty or has only one element left, the dividing will
stop, i.e. it is the base case to stop the recursion. If the array has multiple elements, split the array
into halves and recursively invoke the merge sort on each of the halves. Finally, when both halves
are sorted, the merge operation is applied. Merge operation is the process of taking two smaller
sorted arrays and combining them to eventually make a larger one.
Illustration:
To know the functioning of merge sort, lets consider an array arr[] = {38, 27, 43, 3, 9, 82, 10}

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.

• After the final merging, the list looks like this:

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

Recursive steps of merge sort

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

merge(array, left, mid, right)


step 4: Stop
Follow the steps below the solve the problem:
MergeSort(arr[], l, r)
If r > l
• Find the middle point to divide the array into two halves:
o middle m = l + (r – l)/2
• Call mergeSort for first half:
o Call mergeSort(arr, l, m)
• Call mergeSort for second half:
o Call mergeSort(arr, m + 1, r)
• Merge the two halves sorted in steps 2 and 3:
o Call merge(arr, l, m, r)

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

for (j = low; j <= high- 1; j++){


// If current element is smaller than the pivot
if (arr[j] < pivot){
i++; // increment index of smaller element
swap arr[i] and arr[j]
}
}
swap arr[i + 1] and arr[high])
return (i + 1)
}
Illustration of partition() :
Consider: arr[] = {10, 80, 30, 90, 40, 50, 70}
• Indexes: 0 1 2 3 4 5 6
• low = 0, high = 6, pivot = arr[h] = 70
• Initialize index of smaller element, i = -1

• Traverse elements from j = low to high-1


o j = 0: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
o i=0
• arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j are same
• j = 1: Since arr[j] > pivot, do nothing

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

• j = 2 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])


• i=1
• arr[] = {10, 30, 80, 90, 40, 50, 70} // We swap 80 and 30

• j = 3 : Since arr[j] > pivot, do nothing // No change in i and arr[]


• j = 4 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
• i=2
• arr[] = {10, 30, 40, 90, 80, 50, 70} // 80 and 40 Swapped

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

• We come out of loop because j is now equal to high-1.


• Finally we place pivot at correct position by swapping arr[i+1] and arr[high] (or pivot)
• arr[] = {10, 30, 40, 50, 70, 90, 80} // 80 and 70 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

• Again call function at right part and swap 80 and 90

Strassen’s Matrix Multiplication

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.

Advantages of Greedy Approach


• The algorithm is easier to describe.
• This algorithm can perform better than other algorithms (but, not in all cases).

Drawback of Greedy Approach


As mentioned earlier, the greedy algorithm doesn't always produce the optimal solution. This is the
major disadvantage of the algorithm

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

The ith item contributes the weight xi.wi


to the total weight in the knapsack and profit xi.pi
to the total profit.
Hence, the objective of this algorithm is to
maximize∑n=1n(xi.pi)
subject to constraint,
∑n=1n(xi.wi)⩽W
It is clear that an optimal solution must fill the knapsack exactly, otherwise we could add a fraction
of one of the remaining items and increase the overall profit.
Thus, an optimal solution can be obtained by
∑n=1n(xi.wi)=W
In this context, first we need to sort those items according to the value of piwi
, so that pi+1wi+1 ≤ piwi
. Here, x is an array to store the fraction of items.
Algorithm: Greedy-Fractional-Knapsack (w[1..n], p[1..n], W)
for i = 1 to n
do x[i] = 0
weight = 0
for i = 1 to n
if weight + w[i] ≤ W then
x[i] = 1
weight = weight + w[i]
else
x[i] = (W - weight) / w[i]
weight = W
break
return x
Analysis
If the provided items are already sorted into a decreasing order of piwi
, then the whileloop takes a time in O(n); Therefore, the total time including the sort is in O(n
logn).

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

As the provided items are not sorted based on piwi


. After sorting, the items are as shown in the following table.
Item B A C D
Profit 100 280 120 120
Weight 10 40 20 24
Ratio (piwi)

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

Job Sequencing with Deadline


Problem Statement
In job sequencing problem, the objective is to find a sequence of jobs, which is completed within
their deadlines and gives maximum profit.
Solution
Let us consider, a set of n given jobs which are associated with deadlines and profit is earned, if a
job is completed by its deadline. These jobs need to be ordered in such a way that there is maximum
profit.
It may happen that all of the given jobs may not be completed within their deadlines.
Assume, deadline of ith job Ji is di and the profit received from this job is pi. Hence, the optimal
solution of this algorithm is a feasible solution with maximum profit.
Thus, D(i)>0
for 1⩽i⩽n
.
Initially, these jobs are ordered according to profit, i.e. p1⩾p2⩾p3⩾...⩾pn
.
Algorithm: Job-Sequencing-With-Deadline (D, J, n, k)
D(0) := J(0) := 0
k := 1
J(1) := 1 // means first job is selected
for i = 2 … n do
r := k
while D(J(r)) > D(i) and D(J(r)) ≠ r do
r := r – 1
if D(J(r)) ≤ D(i) and D(i) > r then
for l = k … r + 1 by -1 do
J(l + 1) := J(l)
J(r + 1) := i
k := k + 1
Analysis
In this algorithm, we are using two loops, one is within another. Hence, the complexity of this
algorithm is O(n2)

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

Optimal Merge Pattern (Algorithm and Example)


Optimal merge pattern is a pattern that relates to the merging of two or more sorted files in a
single sorted file. This type of merging can be done by the two-way merging method.
If we have two sorted files containing n and m records respectively then they could be merged
together, to obtain one sorted file in time O (n+m).
There are many ways in which pairwise merge can be done to get a single sorted file. Different
pairings require a different amount of computing time.The main thing is to pairwise merge the n
sorted files so that the number of comparisons will be less.
The formula of external merging cost is:

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

Step 5: Insert 7 and 9

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:

So, The merging cost = 5 + 10 + 16 + 23 + 39 = 93

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

Methods of Minimum Spanning Tree


There are two methods to find Minimum Spanning Tree
1. Kruskal's Algorithm
2. Prim's Algorithm

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

1. O (E log E) = O (E log V).


For Example: Find the Minimum Spanning Tree of the following graph using Kruskal's algorithm.

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 2: then (g, f) edge.

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]

Minimum Cost MST

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.

1. Taking 0 as starting vertex


2. Root = 0
3. Adj [0] = 5, 1
4. Parent, π [5] = 0 and π [1] = 0
5. Key [5] = ∞ and key [1] = ∞
6. w [0, 5) = 10 and w (0,1) = 28
7. w (u, v) < key [5] , w (u, v) < key [1]
8. Key [5] = 10 and key [1] = 28
9. So update key value of 5 and 1 is:

Now by EXTRACT_MIN (Q) Removes 5 because key [5] = 10 which is minimum so u = 5.


1. Adj [5] = {0, 4} and 0 is already in heap

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

2. Taking 4, key [4] = ∞ π [4] = 5


3. (u, v) < key [v] then key [4] = 25
4. w (5,4) = 25
5. w (5,4) < key [4]
6. date key value and parent of 4.

Now remove 4 because key [4] = 25 which is minimum, so u =4


1. Adj [4] = {6, 3}
2. Key [3] = ∞ key [6] = ∞
3. w (4,3) = 22 w (4,6) = 24
4. w (u, v) < key [v] w (u, v) < key [v]
5. w (4,3) < key [3] w (4,6) < key [6]
Update key value of key [3] as 22 and key [6] as 24.
And the parent of 3, 6 as 4.
1. π[3]= 4 π[6]= 4

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 (3, 6) [key [3] < key [6]]


2. u = 3 i.e. 22 < 24
Now remove 3 because key [3] = 22 is minimum so u =3.

1. Adj [3] = {4, 6, 2}


2. 4 is already in heap
3. 4 ≠ Q key [6] = 24 now becomes key [6] = 18
4. Key [2] = ∞ key [6] = 24
5. w (3, 2) = 12 w (3, 6) = 18
6. w (3, 2) < key [2] w (3, 6) < key [6]
Now in Q, key [2] = 12, key [6] = 18, key [1] = 28 and parent of 2 and 6 is 3.
1. π [2] = 3 π[6]=3
Now by EXTRACT_MIN (Q) Removes 2, because key [2] = 12 is minimum.

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 by EXTRACT_MIN (Q) Removes 1 because key [1] = 16 is minimum.


1. Adj [1] = {0, 6, 2}
2. 0 and 2 are already in heap.
3. Taking 6, key [6] = 18
4. w [1, 6] = 14
5. w [1, 6] < key [6]
Update key value of 6 as 14 and its parent as 1.
1. Π [6] = 1
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

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.

Dynamic Programming Example


Let's find the fibonacci sequence upto 5th term. A fibonacci series is the sequence of numbers in
which each number is the sum of the two preceding ones. For example, 0,1,1, 2, 3. Here, each
number is the sum of the two preceding numbers.
Algorithm
Let n be the number of terms.

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

F(4) = F(3) + F(2)

How Dynamic Programming Works


Dynamic programming works by storing the result of subproblems so that when their solutions are
required, they are at hand and we do not need to recalculate them.
This technique of storing the value of subproblems is called memoization. By saving the values in
the array, we save time for computations of sub-problems we have already come across.
var m = map(0 → 0, 1 → 1)
function fib(n)
if key n is not in map m
m[n] = fib(n − 1) + fib(n − 2)
return m[n]
Dynamic programming by memoization is a top-down approach to dynamic programming. By
reversing the direction in which the algorithm works i.e. by starting from the base case and working
towards the solution, we can also implement dynamic programming in a bottom-up manner.
function fib(n)
if n = 0
return 0
else
var prevFib = 0, currFib = 1
repeat n − 1 times
var newFib = prevFib + currFib
prevFib = currFib
currFib = newFib
return currentFib

Recursion vs Dynamic Programming


Dynamic programming is mostly applied to recursive algorithms. This is not a coincidence, most
optimization problems require recursion and dynamic programming is used for optimization.
But not all problems that use recursion can use Dynamic Programming. Unless there is a presence
of overlapping subproblems like in the fibonacci sequence problem, a recursion can only reach the
solution using a divide and conquer approach.
That is the reason why a recursive algorithm like Merge Sort cannot use Dynamic Programming,
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

because the subproblems are not overlapping in any way.

Greedy Algorithms vs Dynamic Programming


Greedy Algorithms are similar to dynamic programming in the sense that they are both tools for
optimization.
However, greedy algorithms look for locally optimum solutions or in other words, a greedy choice,
in the hopes of finding a global optimum. Hence greedy algorithms can make a guess that looks
optimum at the time but becomes costly down the line and do not guarantee a globally optimum.
Dynamic programming, on the other hand, finds the optimal solution to subproblems and then
makes an informed choice to combine the results of those subproblems to find the most optimum
solution.

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

Matrix Chain Multiplication


Example: We are given the sequence {4, 10, 3, 12, 20, and 7}. The matrices have size 4 x 10, 10 x
3, 3 x 12, 12 x 20, 20 x 7. We need to compute M [i,j], 0 ≤ i, j≤ 5. We know M [i, i] = 0 for all i.

Let us proceed with working away from the diagonal. We compute the optimal solution for the
product of 2 matrices.

Here P0 to P5 are Position and M1 to M5 are matrix of size (pi to pi-1)


On the basis of sequence, we make a formula

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

M3 this combination is chosen for the output making.


M [2, 4] = M2 M3 M4
1. There are two cases by which we can solve this multiplication: (M2x M3)+M4, M2+(M3 x
M4 )
2. After solving both cases we choose the case in which minimum output is there.

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.

Now Product of 4 matrices:


M [1, 4] = M1 M2 M3 M4
There are three cases by which we can solve this multiplication:
1. ( M1 x M2 x M3) M4
2. M1 x(M2 x M3 x M4)
3. (M1 xM2) x ( M3 x M4)
After solving these cases we choose the case in which minimum output is there

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.

Now Product of 5 matrices:


M [1, 5] = M1 M2 M3 M4 M5
There are five cases by which we can solve this multiplication:
1. (M1 x M2 xM3 x M4 )x M5
2. M1 x( M2 xM3 x M4 xM5)
3. (M1 x M2 xM3)x M4 xM5
4. M1 x M2x(M3 x M4 xM5)
After solving these cases we choose the case in which minimum output is there

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

11. then m [i,j] ← q


12. s [i,j] ← k
13. return m and s.

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

Longest Common Subsequence


In this tutorial, you will learn how the longest common subsequence is found. Also, you will find
working examples of the longest common subsequence in C, C++, Java and Python.
The longest common subsequence (LCS) is defined as the longest subsequence that is common to
all the given sequences, provided that the elements of the subsequence are not required to occupy
consecutive positions within the original sequences.
If S1 and S2 are the two given sequences then, Z is the common subsequence of S1 and S2 if Z is a
subsequence of both S1 and S2. Furthermore, Z must be a strictly increasing sequence of the
indices of both S1 and S2.
In a strictly increasing sequence, the indices of the elements chosen from the original sequences
must be in ascending order in Z.
If
S1 = {B, C, D, A, A, C, D}
Then, {A, D, B} cannot be a subsequence of S1 as the order of the elements is not the same (ie. not
strictly increasing sequence).

Let us understand LCS with an example.


If
S1 = {B, C, D, A, A, C, D}
S2 = {A, C, D, B, A, C}
Then, common subsequences are {B, C}, {C, D, A, C}, {D, A, C}, {A, A, C}, {A, C}, {C, D}, ...
Among these subsequences, {C, D, A, C} is the longest common subsequence. We are going to find
this longest common subsequence using dynamic programming.
Before proceeding further, if you do not already know about dynamic programming, please go
through dynamic programming.

Using Dynamic Programming to find the LCS


Let us take two sequences:

The first sequence

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,

point to any of them. Fill the values


5. Step 2 is repeated until the table is filled. Fill all the values

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

Thus, the longest common subsequence is CA.

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

Longest Common Subsequence Algorithm


X and Y be two given sequences
Initialize a table LCS of dimension X.length * Y.length
X.label = X
Y.label = Y
LCS[0][] = 0
LCS[][0] = 0
Start from LCS[1][1]
Compare X[i] and Y[j]
If X[i] = Y[j]
LCS[i][j] = 1 + LCS[i-1, j-1]
Point an arrow to LCS[i][j]
Else
LCS[i][j] = max(LCS[i-1][j], LCS[i][j-1])
Point an arrow to max(LCS[i-1][j], LCS[i][j-1])

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

0/1 Knapsack problem


Here knapsack is like a container or a bag. Suppose we have given some items which have some
weights or profits. We have to put some items in the knapsack in such a way total value produces a
maximum profit.
For example, the weight of the container is 20 kg. We have to select the items in such a way that the
sum of the weight of items should be either smaller than or equal to the weight of the container, and
the profit should be maximum.
There are two types of knapsack problems:
• 0/1 knapsack problem
• Fractional knapsack problem
We will discuss both the problems one by one. First, we will learn about the 0/1 knapsack problem.
x
What is the 0/1 knapsack problem?
The 0/1 knapsack problem means that the items are either completely or no items are filled in a
knapsack. For example, we have two items having weights 2kg and 3kg, respectively. If we pick the
2kg item then we cannot pick 1kg item from the 2kg item (item is not divisible); we have to pick
the 2kg item completely. This is a 0/1 knapsack problem in which either we pick the item
completely or we will pick that item. The 0/1 knapsack problem is solved by the dynamic
programming.
What is the fractional knapsack problem?
The fractional knapsack problem means that we can divide the item. For example, we have an item
of 3 kg then we can pick the item of 2 kg and leave the item of 1 kg. The fractional knapsack
problem is solved by the Greedy approach.
Example of 0/1 knapsack problem.
Consider the problem having weights and profits are:
Weights: {3, 4, 6, 5}
Profits: {2, 3, 1, 4}
The weight of the knapsack is 8 kg
The number of items is 4
The above problem can be solved by using the following method:
xi = {1, 0, 0, 1}
= {0, 0, 0, 1}
= {0, 1, 0, 1}
The above are the possible combinations. 1 denotes that the item is completely picked and 0 means
Page | 34
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

When i=1, W=1


w1 = 3; Since we have only one item in the set having weight 3, but the capacity of the knapsack is
1. We cannot fill the item of 3kg in the knapsack of capacity 1 kg so add 0 at M[1][1] shown as
below:

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

When i=1, W=3


w1 = 3; Since we have only one item in the set having weight equal to 3, and weight of the knapsack
is also 3; 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][3] shown as below:

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

When i =1, W=6


W1 = 3; Since we have only one item in the set having weight equal to 3, and weight of the knapsack
is 6; 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][6] shown as below:

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

Now the value of 'i' gets incremented, and becomes 2.


When i =2, W = 1
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 1. We cannot put the item of weight 4 in
a knapsack, so we add 0 at M[2][1] shown as below:

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

Now the value of 'i' gets incremented, and becomes 3.


When i = 3, W = 1
The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the set
having weights 3, 4, and 5, and the weight of the knapsack is 1. We cannot put neither of the items
in a knapsack, so we add 0 at M[3][1] shown as below:

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

a knapsack, so we add 0 at M[3][2] shown as below:

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

Now the value of 'i' gets incremented and becomes 4.


When i = 4, W = 1
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 1. 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][1] shown as below:

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


Lower Bound Theory Concept is based upon the calculation of minimum time that is required to
execute an algorithm is known as a lower bound theory or Base Bound Theory.

Lower Bound Theory uses a number of methods/techniques to find out the lower bound.

Concept/Aim: The main aim is to calculate a minimum number of comparisons required to execute
an algorithm.

Techniques:

The techniques which are used by lower Bound Theory are:

1. Comparisons Trees.

2. Oracle and adversary argument

3. State Space Method

1. Comparison trees:

In a comparison sort, we use only comparisons between elements to gain order information about
an input sequence (a1; a2......an).

Given ai,aj from (a1, a2.....an)We Perform One of the Comparisons

• ai < aj less than

• ai ≤ aj less than or equal to

• ai > aj greater than

• ai ≥ aj greater than or equal to

• 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:

1. (a1, a2, a3), (a1, a3, a2),

2. (a2, a1, a3), (a2, a3, a1)

3. (a3, a1, a2), (a3, a2, a1)

The Comparison based algorithm defines a decision tree.

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.

In a decision tree, there will be an array of length n.

So, total leaves will be n! (I.e. total number of comparisons)

If tree height is h, then surely

n! ≤2n (tree will be binary)

Taking an Example of comparing a1, a2, and a3.

Left subtree will be true condition i.e. ai ≤ aj

Right subtree will be false condition i.e. ai >aj

Fig: Decision Tree

So from above, we got

N! ≤2n

Taking Log both sides

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

Comparison tree for Binary Search:

Example: Suppose we have a list of items according to the following Position:

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

And the last midpoint is:

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.

The Bold letters are Mid-Points Here

According to Mid-Point, the tree will be:

Step1: Maximum number of nodes up to k level of the internal node is 2k-1

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

Step2: Maximum number of internal nodes in the comparisons tree is n!

Note: Here Internal Nodes are Leaves.

Step3: From Condition1 & Condition 2 we get

N! ≤ 2k-1

14 < 15

Where N = Nodes

Step4: Now, n+1 ≤ 2k

Here, Internal Nodes will always be less than 2k in the Binary Search.

Step5:

n+1<= 2k

Log (n+1) = k log 2

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

2. Oracle and adversary argument:

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

1. Log C ((m+n), m) MERGE (m, n) m+n-1.

The upper bound and lower bound can get promptly far apart as m gets much smaller than n.

3. State Space Method:

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.

3. Given n distinct items, find winner and loser.

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.

7. State: It is a collection of attributes.

8. Through this we sort out two types of Problems:

1. Find the largest & smallest element from an array of elements.

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

Teams are: B, D, and E

So the total no of comparisons are: 7

Let n is the total number of items, then

Comparisons = n-1 (to find the biggest item)

No of Comparisons to find out the 2nd biggest item = log2n-1

10. In this no of comparisons are equal to the number of changes of states during the execution of
the algorithm.

Example: State (A, B, C, D)

1. No of items that can never be compared.

2. No of items that win but never lost (ultimate winner).

3. No of items that lost but never win (ultimate loser)

4. No of items who sometimes lost & sometimes win.

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.

In Phase-1 there are 4 states


If the team is 8 then 4 states

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

As if n team the n/2 states.

4 is Constant in C-State as B, D, F, H are lost teams that never win.

Thus there are 3 states in Phase-2,

If n is 8 then states are 3

If n teams that are states.

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.

Thus there are 3 states in Phase -3

If n teams that are states

Note: the total of all states value is always equal to 'n'.

Thus, by adding all phase's states we will get:-

Phase1 + Phase 2 + Phase 3

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.

Thus, Equation is:

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.

L (n) is the base overall conceivable calculation which is greatest finished.

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

N - Queens problem is to place n - queens in such a manner on an n x n chessboard that no queens


attack each other by being in the same row, column or diagonal.

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.

4 - Queens solution space with nodes numbered in DFS

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.

One possible solution for 8 queens problem is shown in fig:

1. Thus, the solution for 8 -queen problem for (4, 6, 8, 2, 7, 1, 3, 5).

2. If two queens are placed at position (i, j) and (k, l).

3. Then they are on same diagonal only if (i - j) = k - l or i + j = k + l.

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

4. The first equation implies that j - l = i - k.

5. The second equation implies that j - l = k - i.

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.

Using place, we give a precise solution to then n- queens problem.

1. Place (k, i)

2. {

3. For j ← 1 to k - 1

4. do if (x [j] = i)

5. or (Abs x [j]) - i) = (Abs (j - k))

6. then return false;

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

4. do if Place (k, i) then

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

10. N - Queens (k + 1, n);

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

Naive Pattern Searching

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 and Output

Input:

Main String: “ABAAABCDBBABCDDEBCABC”, pattern: “ABC”

Output:

Pattern found at position: 4

Pattern found at position: 10

Pattern found at position: 18

Algorithm

naivePatternSearch(pattern, text)

Input − The text and the pattern

Output − location, where patterns are present in the text

Begin

patLen := pattern Size

strLen := string size

for i := 0 to (strLen - patLen), do

for j := 0 to patLen, do

if text[i+j] ≠ pattern[j], then

break the loop

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

display the position i, as there pattern found

done

End

Example

#include<iostream>

using namespace std;

void naivePatternSearch(string mainString, string pattern, int array[], int *index) {

int patLen = pattern.size();

int strLen = mainString.size();

for(int i = 0; i<=(strLen - patLen); i++) {

int j;

for(j = 0; j<patLen; j++) { //check for each character of pattern if it is matched

if(mainString[i+j] != pattern[j])

break;

if(j == patLen) { //the pattern is found

(*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() {

string mainString = "ABAAABCDBBABCDDEBCABC";

string pattern = "ABC";

int locArray[mainString.size()];

int index = -1;

naivePatternSearch(mainString, pattern, locArray, &index);

for(int i = 0; i <= index; i++) {

cout << "Pattern found at position: " << locArray[i]<<endl;

Output

Pattern found at position: 4

Pattern found at position: 10

Pattern found at position: 18

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

7. do p ← (dp + P[i]) mod q

8. t0 ← (dt0+T [i]) mod q

9. for s ← 0 to n-m

10. do if p = ts

11. then if P [1.....m] = T [s+1.....s + m]

12. then "Pattern occurs with shift" s

13. If s < n-m

14. then ts+1 ← (d (ts-T [s+1]h)+T [s+m+1])mod q

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

3. Here T.Length =11 so Q = 11

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

4. And P mod Q = 26 mod 11 = 4

5. Now find the exact match of P mod Q...

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

The Knuth-Morris-Pratt (KMP)Algorithm

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

Components of KMP Algorithm:

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.

The Prefix Function (Π)

Following pseudo code compute the prefix function, Π:

COMPUTE- PREFIX- FUNCTION (P)

1. m ←length [P] //'p' pattern to be matched

2. Π [1] ← 0

3. k ← 0

4. for q ← 2 to m

5. do while k > 0 and P [k + 1] ≠ P [q]

6. do k ← Π [k]

7. If P [k + 1] = P [q]

8. then k← k + 1

9. Π [q] ← k

10. Return Π

Running Time Analysis:

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

Example: Compute Π for the pattern 'p' below:

Solution:

Initially: m = length [p] = 7

Π [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

After iteration 6 times, the prefix function computation is complete:

The KMP Matcher:

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)

4. q ← 0 // numbers of characters matched

5. for i ← 1 to n // scan S from left to right

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

6. do while q > 0 and P [q + 1] ≠ T [i]

7. do q ← Π [q] // next character does not match

8. If P [q + 1] = T [i]

9. then q ← q + 1 // next character matches

10. If q = m // is all of p matched?

11. then print "Pattern occurs with shift" i - m

12. q ← Π [q] // look for the next match

Running Time Analysis:

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

Example: Given a string 'T' and pattern 'P' as follows:

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

Job Assignment Problem using Branch And Bound

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.

Let us explore all approaches for this problem.

Solution 1: Brute Force


We generate n! possible job assignments and for each such assignment, we compute its total cost
and return the less expensive assignment. Since the solution is a permutation of the n jobs, its
complexity is O(n!).

Solution 2: Hungarian Algorithm


The optimal assignment can be found using the Hungarian algorithm. The Hungarian algorithm has
worst case run-time complexity of O(n^3).

Solution 3: DFS/BFS on state space tree


A state space tree is a N-ary tree with property that any path from root to leaf node holds one of
many solutions to given problem. We can perform depth-first search on state space tree and but
successive moves can take us away from the goal rather than bringing closer. The search of state
space tree follows leftmost path from the root regardless of initial state. An answer node may never
be found in this approach. We can also perform a Breadth-first search on state space tree. But no
matter what the initial state is, the algorithm attempts the same sequence of moves like DFS.

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

Solution 4: Finding Optimal Solution using Branch and Bound


The selection rule for the next node in BFS and DFS is “blind”. i.e. the selection rule does not give
any preference to a node that has a very good chance of getting the search to an answer node
quickly. The search for an optimal solution can often be speeded by using an “intelligent” ranking
function, also called an approximate cost function to avoid searching in sub-trees that do not
contain an optimal solution. It is similar to BFS-like search but with one major optimization.
Instead of following FIFO order, we choose a live node with least cost. We may not get optimal
solution by following node with least promising cost, but it will provide very good chance of
getting the search to an answer node quickly.

There are two approaches to calculate the cost function:

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

In this article, the first approach is followed.

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

Randomized Algorithms | Set 1 (Introduction and Analysis)

What is a Randomized Algorithm?

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.

How to analyse Randomized Algorithms?

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

Expected Number of Trials until Success.

For example consider below a randomized version of QuickSort.

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]

randQuickSort(arr[], low, high)

1. If low >= high, then EXIT.

2. While pivot 'x' is not a Central Pivot.

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

(i) Choose uniformly at random a number from [low..high].

Let the randomly picked number number be x.

(ii) Count elements in arr[low..high] that are smaller

than arr[x]. Let this count be sc.

(iii) Count elements in arr[low..high] that are greater

than arr[x]. Let this count be gc.

(iv) Let n = (high-low+1). If sc >= n/4 and

gc >= n/4, then x is a central pivot.

3. Partition arr[low..high] around the pivot x.

4. // Recur for smaller elements

randQuickSort(arr, low, sc-1)

5. // Recur for greater elements

randQuickSort(arr, high-gc+1, high)

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

T(n) < T(n/4) + T(3n/4) + O(n)

T(n) < 2T(3n/4) + O(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

Solution of above recurrence is O(n Log n)

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

Randomized Algorithms | Set 2 (Classification and Applications)

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:

Consider a binary array where exactly half elements are 0

and half are 1. The task is to find index of any 1.

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

Applications and Scope:

• 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.

• Randomized algorithms have huge applications in Cryptography.

• 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

• Number-Theoretic Applications: Primality Testing

• Data Structures: Hashing, Sorting, Searching, Order Statistics and Computational Geometry.

• Algebraic identities: Polynomial and matrix identity verification. Interactive proof systems.

• Mathematical programming: Faster algorithms for linear programming, Rounding linear


program solutions to integer program solutions

• Graph algorithms: Minimum spanning trees, shortest paths, minimum cuts.

• Counting and enumeration: Matrix permanent Counting combinatorial structures.

• Parallel and distributed computing: Deadlock avoidance distributed consensus.

• Probabilistic existence proofs: Show that a combinatorial object arises with non-zero
probability among objects drawn from a suitable probability space.

• Derandomization: First devise a randomized algorithm then argue that it can be


derandomized to yield a deterministic algorithm.

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

Introduction and implementation of Karger’s algorithm for Minimum Cut

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.

1) Initialize contracted graph CG as copy of original graph

2) While there are more than 2 vertices.

a) Pick a random edge (u, v) in the contracted graph.

b) Merge (or contract) u and v into a single vertex (update

the contracted graph).

c) Remove self-loops

3) Return cut represented by two vertices.

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 us understand above algorithm through the example given.


Let the first randomly picked vertex be ‘a‘ which connects vertices 0 and 1. We remove this edge
and contract the graph (combine vertices 0 and 1). We get the following graph.

Let the next randomly picked edge be ‘d’. We remove this edge and combine vertices (0,1) and 3.

We need to remove self-loops in the graph. So we remove edge ‘c’

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

Network Flow Problems

The most obvious flow network problem is the following:

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

• For each ti,add edge (ti,t) with capacity ∞

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.

FORD-FULKERSON METHOD (G, s, t)

1. Initialize flow f to 0

2. while there exists an augmenting path p

3. do argument flow f along p

4. Return f

FORD-FULKERSON (G, s, t)

1. for each edge (u, v) ∈ E [G]

2. do f [u, v] ← 0

3. f [u, v] ← 0

4. while there exists a path p from s to t in the residual network Gf.

5. do cf (p)←min?{ Cf (u,v):(u,v)is on p}

6. for each edge (u, v) in p

7. do f [u, v] ← f [u, v] + cf (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

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

In computer science, there exist some problems whose solutions are not yet found, the problems
are divided into classes known as Complexity Classes. In complexity theory, a Complexity Class
is a set of problems with related complexity. These classes help scientists to groups problems
based on how much time and space they require to solve problems and verify the solutions. It is the
branch of the theory of computation that deals with the resources required to solve a problem.

The common resources are time and space, meaning how much time the algorithm takes to solve a
problem and the corresponding memory usage. The time complexity of an algorithm is used to
describe the number of steps required to solve a problem, but it can also be used to describe how
long it takes to verify the answer. The space complexity of an algorithm describes how much
memory is required for the algorithm to operate.

Complexity classes are useful in organizing similar types of problems.

Types of Complexity Classes

This article discusses the following complexity classes:

1. P Class
2. NP Class
3. CoNP Class
4. NP hard
5. NP complete

P Class

The P in the P class stands for Polynomial Time. It is the collection of decision
problems(problems with a “yes” or “no” answer) that can be solved by a deterministic machine in
polynomial time.

Features:

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. The solution to P problems is easy to find.


2. P is often a class of computational problems that are solvable and tractable. Tractable
means that the problems can be solved in theory as well as in practice. But the problems
that can be solved in theory but not in practice are known as intractable.

This class contains many natural problems like:

1. Calculating the greatest common divisor.


2. Finding a maximum matching.
3. Decision versions of linear programming.

NP Class

The NP in NP class stands for Non-deterministic Polynomial Time. It is the collection of


decision problems that can be solved by a non-deterministic machine in polynomial time.

Features:

1. The solutions of the NP class are hard to find since they are being solved by a non-
deterministic machine but the solutions are easy to verify.
2. Problems of NP can be verified by a Turing machine in polynomial time.

Example:

Let us consider an example to better understand the NP class. Suppose there is a company having a
total of 1000 employees having unique employee IDs. Assume that there are 200 rooms available
for them. A selection of 200 employees must be paired together, but the CEO of the company has
the data of some employees who can’t work in the same room due to some personal reasons.
This is an example of an NP problem. Since it is easy to check if the given choice of 200
employees proposed by a coworker is satisfactory or not i.e. no pair taken from the coworker list
appears on the list given by the CEO. But generating such a list from scratch seems to be so hard
as to be completely impractical.

It indicates that if someone can provide us with the solution to the problem, we can find the correct

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:

1. Boolean Satisfiability Problem (SAT).


2. Hamiltonian Path Problem.
3. Graph coloring.

Co-NP Class

Co-NP stands for the complement of NP Class. It means if the answer to a problem in Co-NP is
No, then there is proof that can be checked in polynomial time.

Features:

1. If a problem X is in NP, then its complement X’ is also is in CoNP.


2. For an NP and CoNP problem, there is no need to verify all the answers at once in
polynomial time, there is a need to verify only one particular answer “yes” or “no” in
polynomial time for a problem to be in NP or CoNP.

Some example problems for C0-NP are:

1. To check prime number.


2. Integer Factorization.

NP-hard class

An NP-hard problem is at least as hard as the hardest problem in NP and it is the class of the
problems such that every problem in NP reduces to NP-hard.

Features:

1. All NP-hard problems are not in NP.

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.

Some of the examples of problems in Np-hard are:

1. Halting problem.
2. Qualified Boolean formulas.
3. No Hamiltonian cycle.

NP-complete class

A problem is NP-complete if it is both NP and NP-hard. NP-complete problems are the hard
problems in NP.

Features:

1. NP-complete problems are special as any problem in NP class can be transformed or


reduced into NP-complete problems in polynomial time.
2. If one could solve an NP-complete problem in polynomial time, then one could also solve
any NP problem in polynomial time.

Some example problems include:

1. Decision version of 0/1 Knapsack.


2. Hamiltonian Cycle.
3. Satisfiability.
4. Vertex cover.

Complexity Class Characteristic feature


P Easily solvable in polynomial time.
NP Yes, answers can be checked in polynomial time.

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

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


NP-hard All NP-hard problems are not in NP and it takes a long time to check them.
NP-complete A problem that is NP and NP-hard is NP-complete.

Difference between NP hard and NP complete problem

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:

A Problem X is NP-Hard if there is an NP-Complete problem Y, such that Y is reducible to X in


polynomial time. NP-Hard problems are as hard as NP-Complete problems. NP-Hard Problem
need not be in NP class.

NP-Complete Problem:

A problem X is NP-Complete if there is an NP problem Y, such that Y is reducible to X in


polynomial time. NP-Complete problems are as hard as NP problems. A problem is NP-Complete
if it is a part of both NP and NP-Hard Problem. A non-deterministic Turing machine can solve
NP-Complete problem in polynomial time.

Difference between NP-Hard and NP-Complete:

NP-hard NP-Complete

NP-Hard problems(say X) can be solved if


NP-Complete problems can be solved by a non-
and only if there is a NP-Complete
deterministic Algorithm/Turing Machine in
problem(say Y) that can be reducible into X in
polynomial time.
polynomial time.

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.

Time is unknown in NP-Hard. Time is known as it is fixed in NP-Hard.

NP-hard is not a decision problem. NP-Complete is exclusively a decision problem.

Not all NP-hard problems are NP-complete. All NP-complete problems are NP-hard

Do not have to be a Decision problem. It is exclusively a Decision problem.

Example: Determine whether a graph has a


Example: Halting problem, Vertex cover Hamiltonian cycle, Determine whether a Boolean
problem, etc. formula is satisfiable or not, Circuit-satisfiability
problem, etc.

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

Cook–Levin theorem or Cook’s theorem

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.

Hence, an SAT is a significant problem and can be stated as follows:

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

Proof that SAT is NP Complete

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:

1. The problem itself is in NP class.


2. All other problems in NP class can be polynomial-time reducible to that.
(B is polynomial-time reducible to C is denoted as ≤ PC)

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:

• For every input wire, add a new variable yi.


• For every output wire, add a new variable Z.
• An equation is prepared for each gate.
• These sets of equations are separated by ∩ values and adding ∩Z at the end.

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.

For Example: If below is the circuit then:

Therefore, the SAT Problem is NP-Complete.

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

Proof that vertex cover is NP complete


Problem – Given a graph G(V, E) and a positive integer k, the problem is to find whether there is
a subset V’ of vertices of size at most k, such that every edge in the graph is connected to some
vertex in V’.

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:

1. Proof that vertex cover is in NP –

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

let count be an integer


set count to 0
for each vertex v in V’
remove all edges adjacent to v from set E
increment count by 1
if count = k and E is empty
then
the given solution is correct
else

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

the given solution is wrong

It is plain to see that this can be done in polynomial time. Thus the vertex cover problem is
in the class NP.

2. Proof that vertex cover is NP Hard –

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

You might also like