assignment no: 1 - wanivipin.files.wordpress.com€¦ · assignment no: 1 ----- ----- title of the...

23
Department of Information Technology Subject: SL-II SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS Specification Readability Assignment Assessment as per schedule Oral Total Dated Sign of Subject Teacher 6 4 4 2 4 20 Date of Performance:..................................... Expected Date of Completion:....................... Actual Date of Completion:............................ --------------------------------------------------------------------------------------------------------------------------------- Assignment No: 1 --------------------------------------------------------------------------------------------------------------------------------- Title of the Assignment: Strassen's Matrix Multiplication --------------------------------------------------------------------------------------------------------------------------------- Objective of the Assignment: Write a java program to implement matrix multiplication using Strassen's method. (Divide and Conquer) --------------------------------------------------------------------------------------------------------------------------------- Prerequisite: 1.Basic Algorithm for Matrix Multiplication 2.Algorithm Analysis techniques --------------------------------------------------------------------------------------------------------------------------------- Theory: Divide-and-Conquer Strategy: Given a function that has to compute on ‘n’ input the divide and conquer strategy suggest splitting k distinct subsets, 1 < K ≤n, that yields K sub problems. Each of these sub problems must be solved and the solutions are obtained. A method must be found to combine the sub solutions into a solution of a whole. If the sub problems are still relatively large, the divide and conquer strategy can be reapplied. The sub problems are of the same type as the original problem. We can write a control abstraction that gives a flow of the control but hose primary operations are specified by other procedures. The meaning of these procedures is left undefined. D and C (P) where P is the problem to be solved. A Boolean valued function that determines if the input size is small enough that the answer can be computed without splitting. If it is so the function S is invoked else the problem P is divided into smaller sub problems. The sub problems P1, P2,.....Pk can be solved by recursive applications of D and C. Combine is a function that determines the solution to P, using the solution the K sub problems. Algorithm D and C (P) { if small (p) then return S (P); else {

Upload: others

Post on 11-May-2020

74 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

Specification Readability Assignment Assessment as per schedule Oral Total Dated Sign of

Subject Teacher 6 4 4 2 4 20

Date of Performance:..................................... Expected Date of Completion:....................... Actual Date of Completion:............................ ---------------------------------------------------------------------------------------------------------------------------------

Assignment No: 1 --------------------------------------------------------------------------------------------------------------------------------- Title of the Assignment: Strassen's Matrix Multiplication --------------------------------------------------------------------------------------------------------------------------------- Objective of the Assignment: Write a java program to implement matrix multiplication using Strassen's method. (Divide and Conquer) --------------------------------------------------------------------------------------------------------------------------------- Prerequisite: 1.Basic Algorithm for Matrix Multiplication 2.Algorithm Analysis techniques --------------------------------------------------------------------------------------------------------------------------------- Theory: Divide-and-Conquer Strategy:

Given a function that has to compute on ‘n’ input the divide and conquer strategy suggest splitting k distinct subsets, 1 < K ≤n, that yields K sub problems. Each of these sub problems must be solved and the solutions are obtained. A method must be found to combine the sub solutions into a solution of a whole. If the sub problems are still relatively large, the divide and conquer strategy can be reapplied. The sub problems are of the same type as the original problem. We can write a control abstraction that gives a flow of the control but hose primary operations are specified by other procedures. The meaning of these procedures is left undefined. D and C (P) where P is the problem to be solved. A Boolean valued function that determines if the input size is small enough that the answer can be computed without splitting. If it is so the function S is invoked else the problem P is divided into smaller sub problems. The sub problems P1, P2,.....Pk can be solved by recursive applications of D and C. Combine is a function that determines the solution to P, using the solution the K sub problems. Algorithm D and C (P) { if small (p) then return S (P); else {

Page 2: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

divide P into smaller instances P1, P2,.....Pk K ≥1 Apply D and C to each to these sub problems. return combine (D and C (P1), D and C (P2) ....D and C (PK)); } } if the size of P is n and the size of K sub problems are n1, n2...nk respectively the computing time of D and

C is described by the recurrence relation. T(n) = { g(n) where n is the small T(n1) + T(n2).....+t(nk) + f(n) T(n) is the time for D and C is any input n. g(n) is the time to compute the sum directly for small inputs. f(n) is the time for dividing D and for combining the solutions for the sub problems. When the sub

problems are of the same type as the original problems we describe the algorithm using recursion. Strassen's Matrix Multiplication

Let A and B be two n x n matrices. The product matrix C = AB is also an n x n matrix. Whose ith

and jth

elements is formed by taking [i, j] the elements in the ith

column of B and multiplying them to give

c [i j] = ∑A [i k] B [k,j], 1 ≤ k < n for all i and j between 1 and n. To compute c[i, j] using this formula we require n 3 multiplications. The divide and conquer strategy suggest another way to compute the product of two n x n matrices. We will assume that n is a power of 2. in case n is not a power of 2 then enough rows and columns of zeros may be added to both A and B so that the resulting dimensions are a power of 2. Imagine that A and B are each partitioned into 4

2 sub matrices each having dimension n/2 x n/2 then the product AB can be computed by

using the above formula for the product of 2 x 2 matrices. To calculate the matrix product C = AB, Strassen's algorithm partitions the data to reduce the number of multiplications performed. This algorithm requires M, N and P to be powers of 2.The straightforward method needs 8 multiplications of numbers and 4 additions of numbers. In contrast, the Strassen's idea needs only 7 multiplications of numbers while it needs 18 additions of numbers. The idea consists of the following steps. 1. Partition A, B and and C into 4 equal parts:

Page 3: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

2. Evaluate the intermediate matrices

3. Construct C using the intermediate metrices:

Algorithm: Algorithm Matrix Strassen(Matrix A, Matrix B, int n) {

// Input: Two n x n matrices A = [a_ij] and B = [b_ij]

// Output: n x n matrix C = [c_ij] = AB Matrix C; Matrix A11; Matrix A12; Matrix A21; Matrix A22; // partition of A Matrix B11; Matrix B12;

Matrix B21; Matrix B22; // partition of B Matrix C11; Matrix C12; Matrix C21; Matrix C22; //

partition of C Matrix M1; Matrix M2; Matrix M3; Matrix M4; Matrix M5; Matrix M6; Matrix M7; if (n is not a power of 2) { expand A and B by padding rows and columns of all 0's until their size becomes the next

power of 2; update n accordingly; }; if (n=1) { // the simplest case return a 1 x 1 matrix C = [ a_11 * b_11 ]; } else

Page 4: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

{ M1 = Strassen(subtract(A12,A22,n/2),add(B21,B22,n/2),n/2); M2 = Strassen(add(A11,A22,n/2),add(B11,B22,n/2),n/2); M3 = Strassen(subtract(A11,A21,n/2),add(B11,B12,n/2),n/2); M4 = Strassen(add(A11,A12,n/2),B22,n/2); M5 = Strassen(A11,subtract(B12,B22,n/2),n/2); M6 = Strassen(A22,subtract(B21,B11,n/2),n/2); M7 = Strassen(add(A21,A22,n/2),B11,n/2); C11 = add(subtract(add(M1,M2,n/2),M4,n/2),M6,n/2); C12 = add(M4,M5,n/2); C21 = add(M6,M7,n/2); C22 = subtract(add(subtract(M2,M3,n/2),M5,n/2),M7,n/2); return C = [Cij] (1<=i<=2 and 1<=j<=2);

} }

Algorithm Matrix add(Matrix A, Matrix B, int n)

{ // Input: Two n x n matrices A = [a_ij] and B = [b_ij] // Output: n x n matrix C = [c_ij] = A + B

Matrix C;

for (i=1; i<=n; i++) {

for (j=1; j<=n; j++)

c_ij = a_ij + b_ij;

}

return C = [c_ij];

}

Algorithm Matrix subtract(Matrix A, Matrix B, int n)

{ // Input: Two n x n matrices A = [a_ij] and B = [b_ij] // Output: n x n matrix C = [c_ij] = A - B

Matrix C;

for (i=1; i<=n; i++) {

for (j=1; j<=n; j++)

c_ij = a_ij - b_ij; }

return C = [c_ij]; }

Page 5: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

Analysis of the Time Complexity:

The numbers of n/2×n/2 matrix multiplication may set back to 7 through the Strassen algorithm, when

compared with the general algorithm for matrix multiplication. This meantime, there are 18 matrix addition

or matrix subtraction. As n is a power of 2, if let k be nonnegative integer, then n is k-th power of 2. If k=0,

then the time complexity of matrix multiplication is 1. If k>0, generally we can explain time complexity of

matrix multiplication through the following recursive expression.

T(n) =7 T(n/2) ( )

( ) ( ) ( )

by solving this O(nlg 7

) = O(n2.81...

). Conclusion: Strassen's algorithm definitely performs better than the traditional matrix multiplication algorithm due to

the reduced number of multiplications and better memory separation.

FAQs: 1.Write down control abstraction for Divide and Conquer Method. 2.What are the limitations of Strassen’s

algorithm? Find out and explain. 3.Compare the Time Complexity of Streasen’s Multiplication with the Traditional Matrix Multiplication

Algorithm.

Page 6: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

Specification Readability Assignment Assessment as per schedule Oral Total Dated Sign of

Subject Teacher 6 4 4 2 4 20

Date of Performance:..................................... Expected Date of Completion:.......................

Actual Date of Completion:............................ ----------------------------------------------------------------------------------------------------------------------------------

Assignment No: 2 ---------------------------------------------------------------------------------------------------------------------------------- Title of the Assignment: Data Encoding And Decoding Using Huffman Algorithm. ---------------------------------------------------------------------------------------------------------------------------------- Objective of the Assignment: Write a java program to implement coding and decoding using Huffman method. (Greedy) Students are

expected to write the program with nlogn complexity and verify the same. ---------------------------------------------------------------------------------------------------------------------------------- Prerequisite: 1.Basic concept for data coding and decoding 2.Necessity of Data compression 3.Algorithm Analysis techniques ---------------------------------------------------------------------------------------------------------------------------------- Theory: Greedy Strategy: Greedy Algorithm solves problem by making the choice that seems best at the particular moment. Many

Optimization problems can be solved using a greedy algorithm. Some problems have no efficient solution,

but a greedy algorithm may provide an efficient solution that is close to optimal. A greedy algorithm works

if a problem exhibit the following two properties: 1. Greedy Choice Property: A globally optimal solution can be arrived at by making a locally optimal

solution. In other words, an optimal solution can be obtained by making “greedy” choices.

2. Optimal Substructure: Optimal solutions contains optimal sub solutions. In other words, solutions to sub problems of an optimal solution are optimal. Control Abstraction for Greedy algorithm : It works in stages , considers one input at a time .Inputs are considered in an order determined by some

selection procedure. At each stage, decision is made regarding whether a particular input is in optimal

solution. Inclusion of next input into partially constructed optimal solution should always result into feasible

solution. If not then, this input is not considered.Selection procedure is based on some optimization measure;

Page 7: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

which may be objective function.Greedy algorithm that makes use of optimization measures to generate

suboptimal solution is called „Subset Paradigm . Algorithm Greedy (a,n) // a[1…n] contains n inputs. { solution=0; //initialise the solution for( i=1 to n ) do { x= select(a); if(feasible(solution, x)) then solution= union(solution , x); } return solution; } Function select , selects an input from a[ ] and removes it. The selected inputs value is assigned to x. Function feasible is Boolean valued function that determines whether x can be included into solution vector. Function union combines x with solution and updates the objective function. Knapsack problem , job sequencing , minimum spanning trees comes under the subset

paradigm.Shortest path , Huffman codes comes under ordering paradigm.

Huffman Codes: Data can be encoded efficiently using Huffman codes. It is widely used and very effective technique for

compressing data; savings of 20% to 90% are typical, depending on the characteristics of the file being

compressed. Huffman's greedy algorithm uses a table of the frequencies of occurrence of each character to

build up an optimal way of representing each character as a binary string. Suppose the data file consist of 105

characters. For normal storage: 8 bits per character (ASCII)-8*105 bits in a file. But, we want to compress the

file and store it compactly. Suppose only 6 characters appear in the file:

Huffman s greedy algorithm computes frequency of occurrence of characters, and assigns binary strings to characters: the more frequent a character, the shorter the string Consider file of length 100; 000, containing only characters a,b,c,d,e,f, and the following frequencies (in thousands). Representation of Data in compact way: 1.Fixed Length Code: Each letter represented by an equal number of bits. With a fixed length code, at

least 3 bits per character:

Page 8: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

For eg.

a 000

b 001

c 010

d 011

e 100

f 101

For a file with 105 characters, we need 3*105 bits. 2.Variable Length Code: It can do considerably better than fixed -length code, by giving frequent

characters short code words and infrequent characters long code words.

Number of bits = (45*1+13*3+12*3+16*3+9*4+5*4)*1000 =2.24*105 bits. Thus, 224000 bits to represent the file, a saving of approximately 25%. 3.Prefix code: No codeword is prefix of some other codeword. Encoding is simple: just concatenate code

words. Using the above variable length code table the code for “deaf” is

111110101100. Prefix codes simplify decoding, they parse uniquely. Binary Tree Representation: For convenient representation for prefix code the binary tree is used. leaves represent characters, interpret binary codeword for a character as path from root to corresponding leaf; 0 means “left”, 1 means “right”.

a 0

b 101

c 100

d 111

e 1101

f 1100

Page 9: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

Example: fixed length code

character a b c d e f

frequency 45 13 12 16 9 5

codeword 000 001 010 011 100 101

Leaves are labeled with character and frequency, interior vertices with sum of frequencies of leaves in sub-

tree.

Example: variable length code

character a b c d e f

frequency 45 13 12 16 9 5

codeword 0 101 100 111 1101 1100

Page 10: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

Algorithm: The idea of Huffman s algorithm is as follows. Tree is built bottom-up. Begin with |C| leaves, then do |C| - 1 merging operations to create final tree. In each merger, extract two least-frequent objects to merge; Result: new object whose frequency is sum of frequencies of two merged objects.

Decoding Huffman-encoded Data: The decoding procedure starts with the first bit in the stream, one then uses successive bits from the stream

to determine whether to go left or right in the decoding tree. When we reach a leaf of the tree, we've

decoded a character, so we place that character onto the (uncompressed) output stream. The next bit in the

input stream is the first bit of the next character.

Analysis of the Time Complexity: The construction of Huffman tree is facilitated by priority queue that maintains the set of trees. Assuming

that a heap is used, each insert and extractMin operation will require time O(log n), where n is the number

of elements in the priority queue. Since these operations are performed a constant number of times in each

iteration of the main loop, and since O(n) iterations are carried out altogether, the total running time will be

O(n log n).

Conclusion:

Page 11: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

Huffman coding is a technique used to compress files for transmission. It Works well for text and fax

transmissions. FAQs: 1.For the given frequency occurrence of letters draw the Huffman Encoding Tree A: 10 B:

10 C: 25 D: 15 E: 30 F: 21 2.With respect to greedy method define the following terms a.Feasible

Solution b. Optimal Solution c. Subset Paradigm

Page 12: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

Specification Readability Assignment Assessment as per schedule Oral Total Dated Sign of

Subject Teacher 6 4 4 2 4 20

Date of Performance:..................................... Expected Date of Completion:....................... Actual Date of Completion:............................. --------------------------------------------------------------------------------------------------------------------------------

Assignment No: 3 -------------------------------------------------------------------------------------------------------------------------------- Title of the Assignment: Floyd’s And Warshal’s All Pairs Shortest Path Problem -------------------------------------------------------------------------------------------------------------------------------- Objective of the Assignment: Write a program to print shortest path and cost for the directed graph using Floyd and Warshal method. ( Dynamic Programming) and verify the complexity -------------------------------------------------------------------------------------------------------------------------------- Prerequisite: 1.Basic concept to find out Shortest path 2.Algorithm Analysis techniques -------------------------------------------------------------------------------------------------------------------------------- Theory: Dynamic Strategy: Dynamic Programming is Developed by Richard Bellman in the early 1950’s. IT is a method for

efficiently solving a broad range of search and optimization problems which exhibit the characteristics of

overlapping sub problems and optimal substructure.

Principle of Optimality: Definition: A problem is said to satisfy the Principle of Optimality if the sub solutions of an optimal

solution of the problem are themselves optimal solutions for their sub problems. Examples: The shortest path problem satisfies the Principle of Optimality. This is because if a,x1,x2,...,xn,b is a shortest path from node a to node b in a graph, then the portion of xi

to xj on that path is a shortest path from xi to xj.

Elements of Dynamic Programming 1.Optimal Substructure: An optimal solution to a problem contains within it an optimal solution to sub

problems Optimal solution to the entire problem is build in a bottom-up manner from optimal solutions to

sub problems.

Page 13: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

2. Overlapping Sub problems: If a recursive algorithm revisits the same sub problems over and over then it

proves that the problem has overlapping sub problems.

Steps of Dynamic Programming Dynamic programming design involves 4 major steps: 1.Develop a mathematical notation that can express any solution and sub solution for the problem at hand.

2.Prove that the Principle of Optimality holds. 3.Develop a recurrence relation that relates a solution to its sub solutions, using the math notation of step 1.

Indicate what the initial values are for that recurrence relation, and which term signifies the final solution. 4. Write an algorithm to compute the recurrence relation. All pair Shortest Path: The all pair shortest Path determination of the shortest graph distances between every pair of vertices in a given graph. The problem can be solved using applications of Dijkstra's algorithm or all at once using the Floyd-Warshall algorithm. Floyd-Warshall's algorithm is based upon the observation that a path linking any two vertices u and v may have zero or more intermediate vertices. The algorithm begins by disallowing all intermediate vertices. In this case, the partial solution is simply the initial weights of the graph or infinity if there is no edge. The algorithm proceeds by allowing an additional intermediate vertex at each step. For each introduction of a new intermediate vertex x, the shortest path between any pair of vertices u and v, x,u,v V, is the minimum of the previous best estimate of δ(u,v), or the combination of the paths from u→x and x→v. δ(u,v)←min(δ(u,v),δ(u,x)+δ(x,v)) Input: The weighted matrix wt[1...n,n...n] Output: The distance matrix D containing shortest path. Algorithm: FLOYD-WARSHALL(W) D→wt for k → 1 to n do { for i → 1 to n do { For j → 1 to n do { D[i ,j] → min {D[i,j] , (D[i,k]+D[k,j])}

} } } Return D Analysis of the Time Complexity: In the above algorithm the basic operation is D[i ,j] → min {D[i,j] , (D[i,k]+D[k,j])}

This operation is with in 3 nested loops, so the time complexity is Θ(n3

). Conclusion:

Page 14: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

A divide and conquer approach would repeatedly solve the common sub problems In contrast Dynamic

programming solves every sub problem just once and stores the answer in a table We can reduce the

asymptotic time complexity.

FAQs: 1.Difference between Dynamic programming and divide and Conquer Strategy.

2.Write down the recursive formulation for the Dynamic Algorithm of All pair shortest path.

Page 15: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

Specification Readability Assignment Assessment as per schedule Oral Total Dated Sign of

Subject Teacher 6 4 4 2 4 20

Date of Performance:..................................... Expected Date of Completion:....................... Actual Date of Completion:............................ ----------------------------------------------------------------------------------------------------------------------------------

Assignment No: 4 ---------------------------------------------------------------------------------------------------------------------------------- Title of the Assignment: Recursive N Queen Problem ---------------------------------------------------------------------------------------------------------------------------------- Objective of the Assignment: Write a recursive program to find the solution of placing n queens on chess board so that no queen takes each other (backtracking). ---------------------------------------------------------------------------------------------------------------------------------- Prerequisite: 1.Basic concept of recursion 2.Algorithm Analysis techniques ---------------------------------------------------------------------------------------------------------------------------------- Theory: Backtracking Useful technique for optimizing search under some constraints Express the desired solution as an n-tuple (x1,x2,……,xn) where each xi 2 Si, Si being a finite set The solution is based on finding one or more vectors that maximize, minimize, or satisfy a criterion function P(x1,x2,……,xn) Sorting an array a[n] Find an n-tuple where the

element xi is the index of ith

smallest element in array a[] Criterion function is given by a[xi] ≤ a[x

i+1] for 1 < i < n

Set Si is a finite set of integers in the range [1:n] Comparison between Brute Force Approach and Backtracking: Brute force approach Let the size of set Si be mi There are m = m1,m2,….,mn n-tuples that satisfy the criterion function P In brute force algorithm, you have to form all the m n-tuples to determine the optimal solutions by evaluating against P. Backtrack approach: Requires less than m trials to determine the solution Form a solution (partial vector) one component at a time, and check at every step if this has any chance of success If the solution at any point seems not-promising, ignore it If the

partial vector (x1; x2; : : : ; xi) does not yield an optimal solution, ignore mi+1,….,mn possible test vectors even without looking at them Effectively, find solutions to a problem that incrementally builds candidates to the solutions, and abandons each partial candidate that cannot possibly be completed to a valid solution Only applicable to problems which admit the

Page 16: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

concept of partial candidate solution and relatively quick test of whether the partial solution can grow into a complete solution. If a problem does not satisfy the above constraint, backtracking is not applicable. Backtracking is not very efficient to find a given value in an unordered list. All the solutions require a set of constraints divided into two categories: explicit and implicit constraints Explicit constraints are rules that restrict each xi to take on values only from a given set. Explicit constraints depend on the particular instance I of problem being solved . All tuples that satisfy the explicit constraints define a possible solution space for I Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy the criterion function. Implicit constraints describe the way in which the xis must relate to each other. N Queen Problem: Place eight queens on an 8 *8 chessboard so that no queen attacks another queen A queen attacks another queen if the two are in the same row, column, or diagonal They are on the same diagonal iff i - j = k - l or i + j = k + l or j - l = i - k or j - l =

k - i.

Input: Number of Queens-N Output: N queens are placed on N*N Chessboard with no two queens can attacking each other. Algorithm: Algorithm NQueens(k,n) //Using backtracking,this procedure prints all possible placements of //n queens on an n*n //chessboard so

that they are non attacking { For i:=1 to n do { if Place(k,i) then { x[k]=i If(k=n) then write (x[1:n]); else Nqueens(k+1,n); } } }

Page 17: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

Algorithm Place(k,i) { For j:=1 to k-1 do if((x[j]=i) //same column or (Abs(x[j]-i)=Abs(j-k)))//same diagonal then return false; Return true; } Analysis of the Time Complexity:

Four factors for efficiency of the backtracking algorithms Time to generate the next Xk Number of Xk

satisfying

the explicit constraints Time for the bounding function Bk

Number of Xk satisfying Bk The first three relatively independent of the problem instance, and the last dependent

For NQueen the Place (k,i) returns a Boolean value that is true if the kth

queen can be placed in column i. It

test for both whether i has been distinct to all previous values x[1],x[2],.....,x[k-1] and whether there is no other Queen on same Diagonal. Its computing time is O(k-1). Conclusion: Backtracking substructure requires criteria for determining promising sub problems to explore. The criteria

consistent with previous sub problems and does not violate problem constraints and it improves over brute-force

algorithms by only developing promising sub problems. Backtracking can be used in non-optimization problems,

that is, decision problems.

FAQs: 1.Write down Control Abstraction for Recursive Backtracking.

2.Derive Implicit and Explicit Constraint for NQueen Problem. 3.Draw the Fixed size and Variable Size State Space Tree for NQueen Problem.

Page 18: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

Specification Readability Assignment Assessment as per schedule Oral Total Dated Sign of

Subject Teacher 6 4 4 2 4 20

Date of Performance:..................................... Expected Date of Completion:....................... Actual Date of Completion:............................ ----------------------------------------------------------------------------------------------------------------------------------

Assignment No: 5 ---------------------------------------------------------------------------------------------------------------------------------- Title of the Assignment: NON Recursive Hamilton Cycle Problem ---------------------------------------------------------------------------------------------------------------------------------- Objective of the Assignment: Write a non-recursive program to check whether Hamiltonian path exists in undirected graph or not. If exists print it. ( backtracking) ---------------------------------------------------------------------------------------------------------------------------------- Prerequisite: 1.Basic concept of backtracking (Described in Assignment N0 4) 2.Algorithm Analysis techniques ------------------------------------------------------------------------------------------------------------------------ Theory: Hamiltonian Path in an undirected graph is a path that visits each vertex exactly once. A Hamiltonian cycle

(or Hamiltonian circuit) is a Hamiltonian Path such that there is an edge (in graph) from the last vertex to

the first vertex of the Hamiltonian Path. Determine whether a given graph contains Hamiltonian Cycle or

not. If it contains, then print the path. Let G=(V,E) be a connected graph with ‘n’ vertices. A HAMILTONIAN CYCLE is a round trip path along ‘n’ edges of G which every vertex once and returns to its starting position. If the Hamiltonian cycle begins at some vertex V1 belongs to G and the vertex are visited in the order of V1,V2…….Vn+1,then the edges are in E,1<=I<=n and the Vi are distinct except V1 and Vn+1 which are

equal. For example, a Hamiltonian Cycle in the following graph is { ,3,4,5,6,7,8,2,1}. There are more Hamiltonian

Cycles in the graph like {1,2,8,7,6,5,4,3,1}

Page 19: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

And the following graph doesn’t contain any Hamiltonian Cycle. Input: 2D array graph[V][V] where V is the number of vertices in graph and graph[V][V] is adjacency matrix

representation of the graph. A value graph[i][j] is 1 if there is a direct edge from i to j, otherwise

graph[i][j] is 0.

Output: An array path[V] that should contain the Hamiltonian Path. path[i] should represent the ith vertex in the

Hamiltonian Path. The code should also return false if there is no Hamiltonian Cycle in the graph Algorithm: Algorithm Hamiltonian (k) { repeat { Next value (k) If (x (k)=0) then return; If k=n then write (x1:xn) Else Hamiltonian (k+1); End if }until(false) } Algorithm Nextvalue (k) { Repeat { X [k]=(X [k]+1) mod (n+1); //next vertex If (X [k]=0) then return; If (G [X [k-1], X [k]] 0) then { For j=1 to k-1 do if (X [j]=X [k]) then break;

Page 20: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

If (j=k) then //if true then the vertex is distinct. If ((k<n) or ((k=n) and G [X [n], X [1]] 0)) then return; } } Until (false); } Analysis of the Time Complexity: In Hamiltonian cycle, in each recursive call one of the remaining vertices is selected in the worst case. In

each recursive call the branch factor decreases by 1. Recursion in this case can be thought of as n nested

loops where in each loop the number of iterations decreases by one. Hence the time complexity is given by: T(N) = N*(T(N-1) + O(1)) T(N) =

N*(N-1)*(N-2).. = O(N!) Conclusion: Hamilton Cycle gives the minimal tour of all vertices in which all vertices are distinct except first and last vertex.

This can relate with Travelling Sales Person Problem in which egde cost are identical.If a common edge cost is c

then cost of a tour is cn since there are n edges in cycle FAQs: 1.Write down Control Abstraction for Iterative Backtracking.

Page 21: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

Specification Readability Assignment Assessment as per schedule Oral Total Dated Sign of

Subject Teacher 6 4 4 2 4 20

Date of Performance:..................................... Expected Date of Completion:....................... Actual Date of Completion:............................ -----------------------------------------------------------------------------------------------------------------------------------

Assignment No: 6 ----------------------------------------------------------------------------------------------------------------------------------- Title of the Assignment: Travelling Sales Person Problem ----------------------------------------------------------------------------------------------------------------------------------- Objective of the Assignment: Write a program to solve the travelling salesman problem. Print the path and the cost. ( Branch and Bound) ----------------------------------------------------------------------------------------------------------------------------------- Prerequisite: 1.Terminology used in branch and bound technique. 2.Algorithm Analysis techniques ----------------------------------------------------------------------------------------------------------------------------------- Theory: The Branch and Bound refers to all state space search method in which all children are generated before any other live node becomes E-node. Branch and Bound is used to find optimal solution to many optimization problems, especially indiscrete and combinatorial optimization. It finds the Systematic enumeration of all candidate solutions, discarding large subsets of fruitless candidates by using upper and lower estimated bounds of quantity being optimized Both BFS and DFS generalize to branch-and bound strategies of live nodes is a queue DFS is an LIFO search in terms of live nodes in which List of live nodes is a stack Just like backtracking, bounding functions is used to avoid generating sub trees that do not contain an answer node. Bounding: Branch & Bound searches a state space tree using any search mechanism in which all the children of E-Node are generated, before any other node becomes E Node. Answer node has cost c(x) associated with it and minimum cost answer node is to be found. 3 Common Strategies are used for bounding FIFO, LIFO and LC A cost function (Cx) such that is used to provide lower bound on solution Cx =< cx obtainable from any node x. If the Upper is an upper Bound on the cost of minimum –cost solution then all live nodes x with C(x) > Upper may be killed. All answer nodes reachable from x have cost c(x) C(x) >Upper Starting value can be obtainable by heuristic

function or set as Infinity This Strategy is used for minimization problems.

.

Page 22: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

Least Cost Search: In both LIFO and FIFO branch and bound the selection rule for the next E-node is rather rigid and in a sense blind. The ideal way to assign ranks would be on the basis of the additional computational effort (or cost) needed to reach an answer node from the live node. For any node x, this cost could be (i) the number of nodes in the sub-tree x that need to be generated before an answer node is generated or more simply (ii) the number of levels the nearest answer node (in the sub-tree x) is from x. Problem with the above techniques to compute the cost at node x is that they involve the search of the sub tree at x involve the exploration of the sub tree. By the time the cost of a node is determined, that sub tree has been searched and there is no need to explore x again Above can be avoided by using an estimate function instead of actually expanding of the node. Travelling Sales Person Problem: Given a graph, the TSP Optimization problem is to find a tour, starting from any vertex, visiting every other vertex and returning to the starting vertex, with minimal cost. Input: No of vertices and Weighted matrix for the graph Output: Optimal Path and cost for the given graph Steps for Implementation: Step 1 Let G=(V,E) be a directed graph Step 2

Let Cij is cost of edge <i,j> Cij=∞ if i=j |V|=n Step 3 Every tour starts and ends at vertex 1. To use LCBB to search the travelling salesperson state space tree, We need to define a cost function c(.) &

two other function ĉ(.) & u(.) such that Ĉ(r) <=c(r) <=u(r) Step 4: For all nodes r -First construct a reduced cost matrix for corresponding to G. Condition for reduced cost matrix

1. Row (column) is said to be reduced if it contains at least one zero & all remaining entries are non-

negative. 2. Matrix is reduced if every row & column reduced. Step 5 Solving this problem by LCBB set upper=∞. Starting with root node as Enode nodes 2,3,4,5 are generated. Step 6 The Reduced matrix for these nodes are as follow using the the rules 1. Change all entries in row i & column j to ∞ 2. A(j,i) to ∞ 3. Reduce all rows & column in the resulting matrix. 4. Step 7 Find out the cost function for individual node. Ĉ(s)=ĉ(R)+A(i,j)+r r-is total amount subtracted for creating reduce matrix A(i,j)-is cost of going i to j

Page 23: Assignment No: 1 - wanivipin.files.wordpress.com€¦ · Assignment No: 1 ----- ----- Title of the Assignment: Strassen's Matrix Multiplication ... input size is small enough that

Department of Information Technology Subject: SL-II

SNJB’s Late Sau. K B Jain College of Engineering, Chandwad Dist. Nashik, MS

Ĉ(R)-cost of node R Analysis of the Time Complexity:

Worst Case Complexity of this algorithm is O(n2

2n

), the use of good bounding functions will enable this

Branch and Bound algorithm to solve some problem instance of Travelling Sales Person Problem in much less time. Conclusion: The Branch and Bound technique helps to cut down to futile search in those branches of search tree that

does not appear promising. So it reduce the time complexity of Travelling Sales Person Problem in compare

with Dynamic approach

FAQs: 1.Write down Control Abstraction for FIFO based Branch and Bound and LC Based Branch and Bound. 2.Explain Cost Function and Heuristic function in detail.