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

DAA Solution

The document outlines the examination scheme for the 4th Semester B.C.A. Examination on Design and Analysis of Algorithms, including sections with questions on algorithms, efficiency classes, sorting methods, graph traversal, and problem-solving techniques. It provides definitions, examples, and explanations of key concepts such as brute-force methods, topological sorting, minimum cost spanning trees, and the N-Queens problem. The examination consists of multiple sections with varying marks, emphasizing both theoretical understanding and practical algorithm application.

Uploaded by

jmnkagency
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)
4 views20 pages

DAA Solution

The document outlines the examination scheme for the 4th Semester B.C.A. Examination on Design and Analysis of Algorithms, including sections with questions on algorithms, efficiency classes, sorting methods, graph traversal, and problem-solving techniques. It provides definitions, examples, and explanations of key concepts such as brute-force methods, topological sorting, minimum cost spanning trees, and the N-Queens problem. The examination consists of multiple sections with varying marks, emphasizing both theoretical understanding and practical algorithm application.

Uploaded by

jmnkagency
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/ 20

4th Semester B.C.A.

Examination, August/September 2023


Scheme of Valuation Design and Analysis of Algorithm
Time: 2 1/2 Hours Max. Marks: 60
SECTION - A
Answer any four questions. Each question carries two marks. (4x2=8)

1, Define an algorithm and mention its characteristics.

The step by step procedure to solve the given problem is known as algorithm. (1m)

The characteristics of algorithm are:

*Finiteness

*Definiteness - (1m)

*Input

*Effectiveness

* Versatility or Flexibility

2.List efficiency classes used in analysis of algorithm.

The efficiency classes used in algorithm are:

1. Time efficiency: A measure of amount of time for an algorithm to execute.


2. Space efficiency: A measure of the amount of memory needed for an algorithm to execute. – (2m)

3.State the Brute-Force method

Brute Force Method is a straight forward approach to solving a problem, usually directly based on the problem
statement and definitions of the concepts involved. It involves trying all possible solutions to a problem until the correct
one is found. – (2m)

4.Define topological sorting with example.

Topological sorting is a way to order the vertices in a DAG such that if there is an edge from vertex A to vertex B,
then A comes before B in the ordering. This is useful for determining the project or the order in which events
occurred in a timeline. – (1m)

Example
:
A

G C
E

B
D
- (1m)
5. What is minimum cost spanning tree of a graph? Give example.

In a weighted graph G = (V, E), the weight W of a subgraph is the sum of the weights of the edges in the subgraph. A
minimum spanning tree for a weighted graph is a spanning tree with minimum weight. – (1m)
Example:
A connected graph G which n=4 is as shown below

The spanning tree as follows:

- (1m)
The spanning trees with the costs are as follows.

W(T1) = 27; W(T2) = 23; W(T3) = 24 and W(T4) == 22

The cost of fourth tree is minimum.

6.What are NP and NP complete problems?

Class P is a class of decision problems that can be solved in polynomial time by algorithm. This class of problems
is called polynomial. - (1m)
Class NP is the class of decision problem that can be solved by non-deterministic polynomial algorithm.
This class of problem is called as non-deterministic polynomial. - (1m)

SECTION - B
Answer any four questions. Each question carries five marks. (4x5=20)

7. Explain the mathematical analysis of non-recursive algorithm with example.

Mathematical analysis of algorithms involves using mathematical tools and techniques to determine the efficiency of
an algorithm in terms of its time and space complexity. This type of analysis is theoretical nature, as it doesn't involve
actually running the algorithm, It predicts the algorithm performance based on the algorithm's structure and operations.
The efficiency of an algorithm is typically expressed using Big O notation, which provides an upper bound on the time
or space complexity in the worst-case scenario. - (2m)

Example: Consider a simple algorithm for finding the maximum element in an unsorted array:

FindMax (A, n)

max = A[0]

for i =1 To n - 1 DO

if A[i] > max then

max = A[i]

END FOR

return max - (3m)

In this algorithm, 'A' is an array of 'n' elements. The basic operation here is the comparison A[i] > max. This operation
occurs ‘n - 1' times (once for each element in the array after the first one).
Therefore, we can say that the time complexity of the algorithm is O(n) because the number of operations grows
linearly with the size of the input. That is, if we double the size of the array, we approximately double the number of
operations.

The space complexity of this algorithm is O(1) which signifies constant space. Regardless of the size of the input array,
we only use a fixed amount of additional space (to store the variable 'max' and the loop counter '1').

This is a simple example, but the process can get more complex when analysing more complex algorithms.
Mathematical analysis of algorithms is a fundamental skill in computer science, because it allows us to predict and
compare the efficiency of different algorithms without having to implement or extensively test them.

8. Write an algorithm to sort the array using bubble sort and obtain its time complexity.

Bubblesort ( A [0,.....,n-1])

A is an array of n elements to be sorted.

Step 1: for pass = 1 to n - 1

Step 2: for j = 0 to n - pass - 1

Step 3: if a[j] > a [j + 1] then

temp = a[j]

a[j] = a[j + 1]

a[j + 1] = temp

Step 4: End for

Step 5: End for - (4m)

We are running two loops, an outer loop which runs n - 1 times (pass = 1 to n - 1) , and an inner loop which iterates
over the unsorted part of the array G = 0 to n-pass-1). This ensures that after each pass, the largest element is bubbled
to the end of the array.

*when pass = 1, j ranges from 0 to (5 - 1 - 1) i.e., 0 to 3. So 4 comparisons takes place.

*when pass = 2 j ranges from 0 to (5 - 2 - 1) i.e., 0 to 2. So 3 comparisons takes place.

*when pass = 3 ranges from 0 to (5 - 3 - 1) i.e., 0 to 1. So 2 comparisons takes place.

*when pass = 4 j ranges from 0 to (5 - 4 - 1) i.e., 0 to 0. So 1 comparisons takes place.

: . As the value of pass is varying from 1 to (n - 1) the value of comparisons from (n - 1) to 1 respectively.

* The if condition inside the inner loop is checking if the current element (a[j]) is greater than the next element

(a[j + 1]) . If it is, then it swaps them.

* temp is a temporary variable used to facilitate the swapping of elements.

* After n - 1 passes, all elements would have bubbled up to their correct positions, resulting in a sorted array.

The Time complexity of bubble sort is O(n2). - (1m)

9.Explain Breadth first search with suitable example.

Breadth-First Search (BFS) is a fundamental graph traversal algorithm used in computer science and mathematics
to explore all nodes of a graph or tree data structure. The strategy of BFS is to explore the graph "broadly" before
going "deep". That is, it visits all the nodes at the current level before moving on to the next level. – (2m)

The BFS algorithm starts at an arbitrary root node, then explores all the neighbour nodes at the present depth level,
and then moves on to nodes at the next depth level. It does this by using a queue data structure to keep track of nodes
to be visited. Nodes are visited and dequeued (removed), then all unvisited neighbours of the current node are
enqueue(inserted) and marked as visited.

Steps Visited Unvisited Minimum edge from S to G Illustrations


Vertices Vertices(s)
(s)
1 - {a,b,c,d,e,f} --

2 {a} {b,c,d,e,f} {(a,b),(a,c),(a,d),(a,e),(a,f)}


={3, ∞, ∞,6,5}=3
(a,b)

3 {a,b} {c,d,e,f} {(a,c),(a,d),(a,e),(a,f),


(b,c),(b,d),(b,e),(b,f)}
={∞,∞,6,5,1, ∞,∞,4}=1
(b,c)

4 {a,b,c} {d,e,f} {(a,d),(a,e),(a,f), (b,d),(b,e),(b,f),


(c,d),(c,e),(c,f)}
={∞,6,5, ∞,∞,4,6, ∞,4}=4
(c,f)
5 {a,b,c,f} {d,e} {(a,d),(a,e),(b,d),(b,e),(c,d),(c,e),(f,d)
,(f,e)}
={∞,6, ∞,∞,6, ∞,5,2}=2
(f,e)

6 {a,b,c,f,e {d} {(a,d),(b,d),(c,d),(e,d),(f,d)}


} ={∞,∞,6,8,5}=5
(f,d)

- (3m)

The term "breadth" in BFS refers to the breadth, or width, of the tree or graph. BFS explores all the neighbour nodes
at the current level before proceeding to nodes at the next level. refers to the process of visiting and examining each
node. In contrast, "search" refers to the process of visiting and examining each node.

For instance, imagine a family tree where BFS starts at the root of the tree (the oldest known ancestor). It first visits all
the children (level one descendants), then all the grandchildren (level two descendants), and so forth, until all
descendants at each level are visited. In other words, BFS visits nodes in a way that fills up each level of the family
tree from left to right before descending to the next level.

Example 1: BFS

Let's take an example of a tree graph:

2 3

4 5 6

BFS traversal of this graph would result in: 1, 2, 3, 4, 5, 6.

In this example, starting from node 1 (considered as the root), BFS first visits all the neighbours of node 1. which are
nodes 2 and 3. It then visits the neighbours of node 2 and node 3. So, it first visits nodes 4 and 5 which are the children
of node 2, and then it visits node 6 which is the child of node 3. Therefore, BFS fills up each level of the tree from left
to right before moving down to the next level.

10.Find the value of 8C5 using dynamic programming.

8C5 = 8!
(5!( 8-5)!)

8C5 = 8 x 7 x 6 x 5!
5! X 3!

8C5 = 8 x 7

8C5 = 56

10.Apply Prim's algorithm to obtain the minimum cost spanning tree for the following graph.

Solution:
Cost adjacency matrix is

C A B c d e f
A - 3 ∞ ∞ 6 5
B 3 - 1 ∞ ∞ 4
C ∞ 1 - 6 ∞ 4
D ∞ ∞ 6 - 8 5
E 6 ∞ ∞ 8 - 2
F 5 4 4 5 2 -
- (3m)
Minimum spanning tree is :

Cost of spanning tree = 3+1+4+2+5 = 15 - (2m)

11.Explain how 4-queen's problem can be solved using backtracking.


The N-Queens problem is a classic puzzle in computer science and mathematics. Its about placing N chess queens
on an N*N chessboard so that no two queens attack each other. This means that no two queens should be on the
same row, same column, or same diagonal.
Example: Let us consider the four-queens problem and solve it by the backtracking technique. The chess board for
the four queens problem is shown below.

1 2 3 4
1
 queen1
2
 queen2
3
 queen3
4
 queen4

Step 1: In the following steps we use x1 = 1 indicates that first queen placing in first column, x 1= 2 indicates that first
queen placing in second column and so on. Similarly, x2= 1 indicates that second queen placing in first column, x 2= 2
indicates that second queen placing in second column and so on.
Step 2: We start with placing queen 1 in first column. i.e., we choose x1 = 1

1 2 3 4 Each step 1m
1 Q1
All steps 5m
2

Step 3: Now we move to place queen 2. Place Q2 in first column and second row and which is not acceptable
position because- Q1 attacks Q2 since they are on the same column. (We have chosen x2 = 1 )
1 2 3 4
1 Q1

2 Q2

Step 4: Now place Q2 in second column i.e., choose x2 = 2 which is also not an acceptable position since Q1 and Q2
are diagonally opposite to each other.

1 2 3 4
1 Q1

2 Q2

Step 5: Place Q2 in third column i.e., choose x2 = 3 .Which is perfectly acceptable.

1 2 3 4
1 Q1

2 Q2

Step 6: The Q1 and Q2 are successfully placed and now we move to place Q3. Placing Q3 in first column (x3 = 1) is
not acceptable because Q1 and Q3 can not be in the same column.

1 2 3 4
1 Q1

2 Q2

3  Q3
4

Placing Q3 in second column ( x3 =2) is also not allowed since it is diagonally opposite to Q 2. Placing Q3 in third
column (x3 = 3 ) is also not allowed because Q2 and Q3 lies on the same column. Placing Q3 in fourth column is also
not acceptable ( x3=4) because it is diagonally opposite to Q2. So we can not place Q3 in any of the columns.

Step 7: Now the algorithm backtracks and puts Q2 in the fourth column as shown below.

1 2 3 4

1 Q1

2 Q2

4
We have placed Q2 in fourth column which is an acceptable position. Now placing Q 3 in first column (x3 = 1) is not
acceptable because Q1 and Q3 lies on the same column. Placing Q3 in second column (x3 = 2) is perfectly
acceptable. Therefore, put Q3 in second column i.e., in position (3, 2).

1 2 3 4
1 Q1

2 Q2

3 Q3

Step 8: Since all the first three queens are placed in perfect positions now, we are required to place the fourth queen.
Placing Q4 in first column (x4= 1) is not acceptable, placing Q4 in second column (x4 = 2) is also not acceptable,
placing Q4 in third column (x4 = 3) is also not acceptable and placing Q4 in fourth column (x4 = 4) is also not allowed.
Therefore, we cannot place Q4 in any of the columns.

1 2 3 4
1 Q1

2 Q2

3 Q3

4
Q4
Now we backtrack to row 3 and place Q3 in third column which is not acceptable position and place Q 3 in fourth
column which is also not acceptable position. Again, backtrack to row 2, in row 2 we have tried all the positions.
Therefore backtrack to row 1 and place Q1 in second column (x1 = 2) as shown below.

1 2 3 4
1 Q1

Step 9: Placing Q2 in first column (x2= 1) is not allowed because it becomes diagonally opposite to Q 1. Placing Q2 in
second column ( x2 =2) is also not allowed because Q1 and Q2 lies on the same column. Placing Q2 in third column
(x2= 3) is also not acceptable because it becomes diagonally opposite to Q1.
Placing Q2 in fourth column is perfectly acceptable. Therefore put Q2 in fourth column (x2 = 4).
1 2 3 4
1 Q1

2 Q2

Step 10: Now we will start placing Q3 in third row. Placing Q3 in first column (x3= 1) is perfectly acceptable. Therefore
put Q3 in first column (x3= 1).
1 2 3 4
1 Q1

2 Q2

Step 11: Now we will place Q4 in fourth row. Placing Q4 in first column (x4= 1) is not acceptable because Q3 and Q4
lies on the same column. Placing Q4 in second column (x4 = 2) is also not acceptable because Q3 and Q4 becomes
diagonally opposite. Placing Q4 in third column (x4 = 3) is perfectly acceptable. Therefore put Q4 in third column of
fourth row as shown below.

1 2 3 4
1 Q1

2 Q2

3 Q3 Therefore the solution Vector = [x1, x2, x3, x4]


= [2, 4, 1, 3]
4 Q4

SECTION – C
Answer any four questions. Each question carries eight marks. (4x8=32)

13.Explain different asymptotic notations in detail.


The order of growth of an algorithm cam be explained using 3 notations
1. Big Oh
2. Big Omega
3. Big Theta - (2m)
→ Big Oh Notation:
It is a measure of largest amount of time for the complete execution of an algorithm.

It gives the upper bounds of the algorithm running time or growth rate of functions
The f(n) and g(n) are the functions that is f(n) €O(g(n)) and c and n0 are satisfying constraints and positive
constraints that is f(n) ≤ c *g(n) for all n ≥ n0
-(2m)
→ Big Omega Notation:
It is a measure of least amount of time for the possibility to complete execution of an algorithm.
It gives the lower bounds of the algorithm running time or growth rate of functions
The f(n) and g(n) are the functions that is f(n) €Ω(g(n)) and c and n0 are satisfying constraints and positive
constraints that is f(n) ≥ c *g(n) for all n ≥ n0
-(2m)
-→ Big Theta Notation:
The function f(n) is said to be in ∂(g(n)) denoted by f(n)€ ∂(g(n))
The f(n) is bounded both above and below by some positive constraint multiples of g(n) for all large value of n
That positive constraints such as c1 and c2 and non-negative integer n0 such that
C1*g(n) ≤ f(n) ≤ C2*g(n) for all n ≥ n0

-(2m)
14.a) Discuss important problem types.

Problem Types:
Design and analysis of algorithms is not restricted to specific problem types. The steps to solve a problem can be
applied to any problem regardless of its complexity or whether it is in the computer field or another field.

The important problem types in computer science are given below.


a. Sorting
b. String Processing
c. Graph Problems
d. Numerical Problems
e. Searching
f. Combinatorial Problems
g. Geometric Problems
- (2m)
explanation – 2m
1. Sorting Problems:
Sorting means arranging a set of data in some order. In our daily life, we can see so many applications using the
data in sorted order as telephone directory, merit list, roll numbers etc. Sorting refers to the operation of arranging the
given data items either in ascending (numerically or lexicographically) or descending order.

Examples of Sorting Algorithms:


a. Bubble Sort
b. Insertion Sort
c. Quick Sort
d. Merge Sort
e. Radix Sort

2.Searching Problems: Searching refers to finding for an item in any list of entries. It is one of the common
operation in data processing. Searching an employee details from the database or searching a telephone number
from the telephone directory are few of the daily life instances.

Examples of Searching Algorithms:


a. Linear Search
b. Binary Search
c. Depth-First Search
d. Hash Search
e. Breadth-First Search.

3.String Processing Problems: Strings are sequences of characters and String processing problems involve
manipulating strings to perform various operations such as searching, matching, and editing. There are many kinds
of strings like text made up of letters and numbers or even sequences of DNA which have four different "letters": A,
C, G, and T. String processing is a big part of computer science, and it's used in all sorts of fields, from creating
computer software to understanding DNA sequences in biology.

Examples of String Processing Problems:


a. Searching for a particular word or pattern in a text file.
b. Replacing all instances of one word with another in a text file. Counting the number of occurrences of a particular
character or substring in a string.

4. Combinatorial Problems:

Combinatorial problems are a type of problem in computer science and mathematics that involve counting or
generating combinations or permutations of objects. These problems often arise in various fields, including computer
science, statistics, and engineering. Combinatorial problems can be broadly classified into two categories:
enumeration problems and optimization problems.

Examples of Combinatorial problems:


* Traveling Salesman Problem
*Shortest Path Problem
* Generating all possible permutations of a set

5.Graph Problems:
Graph problems involve analyzing and manipulating graphs, which are mathematical structures consisting of nodes
(also called vertices) connected by edges. Graphs can be used to model a wide range of real-world systems
including social networks, transportation networks, and computer networks.

Examples of Graph Problems:


1. Shortest Path Problem
2. Minimum Spanning Tree Problem
3. Graph Coloring Problem

6. Geometric Problems:
Geometric problems involve analyzing and manipulating geometric objects such as points, lines, and polygons.
These problems can arise in a wide range of applications such as computer graphics, robotics, and tomography.

Examples of Geometric Problems:

1. Convex Hull Problem: Given a set of points in the plane, find the smallest convex polygon that contains all the
points. This problem is commonly used in computer graphics to render 3D objects.

2.Nearest Neighbour Problem: Given a set of points in the plane, find the closest point to a given query point. This
problem is commonly used in location-based services to find nearby places or people.

7. Numerical Problem:
Numerical problems involve computations with numbers. This could include tasks like finding roots of equations,
performing matrix operations, or solving differential equations. Various numerical methods and algorithms are used
to solve these types of problems. The majority of these problems can be solved only approximately. But these
problems play a vital role in many scientific and engineering applications. So the algorithms for this type of problems
should be more sophisticated and more focus is required while designing these algorithms.
Examples of Numerical Problems:
*Finding Roots of Equations
* Performing Matrix Operations,
* Solving Differential Equations

b) Explain empirical analysis of algorithm. (4+4)


Empirical analysis of algorithms is a process that involves implementing the algorithm in a specific programming
language, executing the program with various input sizes, and observing its performance.
This process is particularly important in assessing the practical efficiency of algorithms, This analysis can includes
several performance metrics such as:

1.Execution Time: It's the actual time taken by the algorithm to run from start to finish on a particular computing
system.
2. Memory Usage: It's the amount of computer memory (RAM) the algorithm uses during execution.
3.CPU Usage: It refers to the amount of computing resources used by the algorithm, which is especially important in
multitasking systems.
4. I/0 Operations: In case the algorithm involves reading from or writing to files or databases, the number and
efficiency of these operations are also considered.
- (2m)
It's important to note that empirical analysis requires the algorithm to be implemented in code, which means that the
characteristics of the programming language, the efficiency of the code, and the specific hardware and software
environment in which the program is run will all impact the results. Hence, the empirical analysis provides a practical
view of an algorithm's efficiency, but it's dependent on the specific implementation and the computational
environment.

Advantages of Empirical Analysis of Algorithms


1. Practical Validation: Empirical analysis provides actual data from real-world operation, validating or potentially
contradicting theoretical expectations. It can confirm if the algorithm's theoretical time and space complexity align
with its performance in practice.
2. Performance Comparison: It allows direct comparison of different algorithms solving the same problem. By
running multiple algorithms with the same set of inputs, we can identify which performs best in a specific scenario or
on average.
3. Consideration of System Factors: Empirical analysis can account for factors that might not be captured in the
theoretical analysis. These could include hardware capabilities, compiler optimizations, and other system or
environmental factors.
- (2m)
Disadvantages of Empirical Analysis of Algorithms
1. Implementation Dependent: Empirical analysis relies on an implemented version of an algorithm, typically written
in a specific programming language. The performance can thus be heavily influenced by the efficiency of the
implementation and the underlying programming language and compiler optimizations. A poorly implemented
algorithm might perform worse in practice than its theoretical efficiency would suggest.

2. Machine Dependent: The results obtained from empirical analysis are usually specific to the machine (hardware
and software environment) on which the tests are performed. This means results may not be representative of how
the algorithm will perform on different hardware or under different operating systems.
3. Input Data Dependent: Empirical analysis depends on the test data used. If the test data is not representative of
the actual inputs the algorithm will encounter, then the analysis may not provide a true reflection of its real-world
performance. This includes both the size and nature of the data.

15.a) Discuss the Brute-Force string matching algorithm.


Brute-Force String Matching:
String Matching is a fundamental concept in computer science and is especially important in fields like text
processing, information retrieval, and bioinformatics. It refers to the process of finding one, or more occurrences of a
"pattern" string in a larger "text" string.

For example, we might want to find all occurrences of the word "pub" in a "skyward publishers". In this case, "pub" is
the pattern, and the "skyward publishers" is the text. String matching algorithms scan the text to find positions where
the pattern and the text align exactly.

Example
1. Text="Problem Solving Techniques"
Pattern =Problem
Pattern Found at 1st position
Occurrences of Pattern = 1 Time
-(2m)
2. Text : A A B A A C A A D A A B A A B A
Pattern: A A B A
Pattern Found at 1st , 10th and 13th positions
Occurrences of Pattern = 3 Times

Algorithm: BruteForceStringMatch (TEXT [0..n - 1], PATTERN [0.. m – 11]

// Implements brute-force string matching.


// Input: An array TEXT [0.. n - 1 ] of n characters representing a text and an array PATTERN [0.. m - 1 ] of m
characters representing a pattern
// Output: returns the position of first occurrence of pattern or -1 if the pattern not found

Step 1: Declare variables i, j and initialize its value to 0


Step 2: for i = 0 to (n - m) do
for j = 0 to m do
if TEXT[i+j] not equal PATTERN[j] then break the loop

end for

if j == patLen then
The pattern found at the position i+1
return (i + 1)
end if
end for

Step 3: return -1;

* This algorithm operates by conducting an iterative comparison of each character in the pattern to each
corresponding character in the text.

*The variables - i, and i are declared and set to zero. These variables will be used to keep track of the positions in
the text and pattern during comparison (i and j),

* The outer loop runs from 0 to the difference of the lengths of the text and the pattern (n - m) This ensures that every
position in the text where the pattern could possibly fit is checked. The inner loop then compares each character in
the pattern with the corresponding character in the text at the current position.

*If at any point, a pair of characters are found that do not match, the inner loop is terminated, and control goes back
to the outer loop to proceed with the next position in the text. Conversely, if a complete pattern match is found (i.e.,
the inner loop completes without finding any mismatches), the position of the pattern match in the text is returned.

*After the outer loop has completed, indicating that every possible starting position in the text has been checked for
the pattern and pattern is not found. It return -1.
- (2m)
b) Explain how decrease and conquer method is applied to sort the elements of the array using insertion sort.
(4+4)
Decrease and Conquer: The Decrease and Conquer method is a problem-solving technique that involves breaking
down a problem into smaller subproblems, solving each subproblem independently, and then combining the
solutions to the subproblems to solve the original problem.
The name "decrease and conquer" comes from the fact that this technique involves reducing the size of the problem
at each step until it becomes small enough to solve directly.
The decrease and conquer method is often used in computer science and mathematics to solve complex problems
efficiently. By breaking down a large problem into smaller sub-problems, it becomes easier to manage and solve
each part independently. This can lead to faster computation times and more efficient use of resources.
The Insertion sorts inserts each element in appropriate position. This is same as playing cards, in which we insert a
card in proper position. If an array A contains n elements and we place each element of an array at proper place in
the previously sorted element list.
-(2m)
The first pass starts with comparison of 1st element with 0th element. i.e., A[1] with A[0] .In the second pass 2nd
element is compared with 0th and 1st element, i.e. ,A[2] with A[0] and A[1]. In general, in every pass an element is
compared with all elements before it.
Let us take an array A of N elements. The process of inserting each element in proper place is as shown below:
Pass 0: A[0] is already sorted because of only one element.
Pass 1: A[1] is inserted before or after Lambda[0] So A[0] and A[1] are sorted.
Pass 2: A[2] is inserted before A[0] or after A[1] or in between A[0] and A[1]. So A[0], A[1] and A[2] are sorted.
Pass 3: A[3] is inserted into its proper place in A[0] , A[1] , A[2]
So A [0] , A[1], A[2] and A[3] are sorted.
……………………………………………………..
……………………………………………………..
………………………………………………………
Pass N - 1 : A[N - 1] is inserted into its proper place in A[0],A[1]....... A[N - 2] So A[0], A[1], A[2] ........... A[N - 1] are
sorted.

Any element to be inserted in between the ith element and i+ 1th element is possible only when element >=ith element
and element < = (i + 1)th element.
- (2m)
16.Explain different tree traversal algorithm with example.
Graph Traversals:
Graph traversal is the process of visiting all the vertices and edges of a graph in a systematic way. It involves
exploring the graph from a starting vertex and visiting all its neighbours, then moving to their neighbours, and so on
until all vertices have been visited. It's like visiting each room in a house, where the rooms are the vertices and the
doors between them are the edges.
The term "exhaustive search"can also be applied to two very important algorithms that systematically process all
vertices and edges of a graph. These two graph traversal algorithms are Depth-First The term Search (DFS) and
Breadth-First Search (BFS). These algorithms have proved to be very useful for many applications involving graphs
in artificial intelligence and operations research. Graph traversal algorithms are used to solve various problems such
as finding connected components, detecting cycles, computing shortest paths etc.,
Depth First Search (DFS):
Depth-First Search (DFS) is a graph traversal technique used in computer science and mathematics to explore all
the vertices of a graph or tree data structure. The process is called "Depth-First" because it is used to search deeper
into the graph whenever possible and only backtracks to a previous node when it has fully explored a branch.
The algorithm starts at an arbitrary node and visits all its neighbours, then moves to the next unvisited neighbour
and repeats the process until no more unvisited neighbours are found. Then it backtracks to the previous node and
continues exploring its unvisited neighbours until all nodes have been visited.
The "depth" in DFS refers to how far a path has been explored from the root node, while the "search" refers to the
process of visiting and exploring each node. In other words, DFS is like navigating a maze by always choosing an
unexplored path and only turning back when reaching a dead-end.
For example, in a family tree, DFS would start at the root (the oldest ancestor), and first explore the first child and
their descendants as far as it can before moving to the second child and their descendants.
DFS is a powerful algorithm for solving problems related to graph and tree structures, including detecting cycles,
path finding, and topological sorting
Example 1:
Let's take an example of a tree graph:

4m
2 3

5
4 6
DFS traversal of this graph would result in: 1, 2, 4, 5, 3, 6,

In this example, starting from node 1 (considered as the root), DFS goes to node 2, and then it goes to node 4. Since
node 4 has no children, it backtracks to node 2 and visits node 5. Again, as node 5 has no further nodes to visit, it
backtracks to node 1. Finally, it visits node 3 and node 6.

Breadth-First Search (BFS) is a fundamental graph traversal algorithm used in computer science and mathematics
to explore all nodes of a graph or tree data structure. The strategy of BFS is to explore the graph "broadly" before
going "deep". That is, it visits all the nodes at the current level before moving on to the next level.

The BFS algorithm starts at an arbitrary root node, then explores all the neighbour nodes at the present depth level,
and then moves on to nodes at the next depth level. It does this by using a queue data structure to keep track of
nodes to be visited. Nodes are visited and dequeued (removed), then all unvisited neighbours of the current node
are enqueue(inserted) and marked as visited.

The term "breadth" in BFS refers to the breadth, or width, of the tree or graph. BFS explores all the neighbour nodes
at the current level before proceeding to nodes at the next level. refers to the process of visiting and examining each
node. In contrast, "search" refers to the process of visiting and examining each node.

For instance, imagine a family tree where BFS starts at the root of the tree (the oldest known ancestor). It first visits
all the children (level one descendants), then all the grandchildren (level two descendants), and so forth, until all
descendants at each level are visited. In other words, BFS visits nodes in a way that fills up each level of the family
tree from left to right before descending to the next level.

Example 1: BFS

Let's take an example of a tree graph:

4m
1

3
2

5
4 6

BFS traversal of this graph would result in: 1, 2, 3, 4, 5, 6.

In this example, starting from node 1 (considered as the root), BFS first visits all the neighbours of node 1. which are
nodes 2 and 3. It then visits the neighbours of node 2 and node 3. So, it first visits nodes 4 and 5 which are the
children of node 2, and then it visits node 6 which is the child of node 3. Therefore, BFS fills up each level of the tree
from left to right before moving down to the next level.

17. Write Warshall algorithm to compute transitive closure of a directed graph. Apply the same on the following
graph.
The adjacency matrix is shown below
a b c d
a 0 1 0 0
b 0 0 0 1
C=D0 =
c 0 0 0 0
d 1 0 1 0

We know the formula that


Dk [i,j]=Dk-1 [i,j] OR [Dk-1[i, k] AND D k-1 [k, j]]
Step 1:
Let k = a and obtain D1
D1 [a,a] = D0 [a,a] v [D0[a,a] ^ D 0 [a,a]] = 0 v [0 ^ 0] = 0
D1 [a,b] = D0 [a,b] v [D0[a,a] ^ D 0 [a,b]] = 1 v [0 ^ 1] = 1
D1 [a,c] = D0 [a,c] v [D0[a,a] ^ D 0 [a,c]] = 0 v [0 ^ 0] = 0
D1 [a,d] = D0 [a,d] v [D0[a,a] ^D 0 [a,d]] = 0 v [0 ^ 0] = 0

D1 [b,a] = D0 [b,a] v [D0[b,a] ^ D 0 [a,a]] = 0 v [0 ^ 0] = 0


D1 [b,b] = D0 [b,b] v [D0[b,a] ^ D 0 [a,b]] = 0 v [0 ^ 1] = 0 Each Step – 2m
D1 [b,c] = D0 [b,c] v[D0[b,a] ^ D 0 [a,c]] = 0 v [0 ^ 1] = 0 Total – 8m
D1 [b,d] = D0 [b,d] v [D0[b,a] ^ D [a,d]]
0 = 1 v [0 ^ 1] = 1

D1 [c,a] = D0 [c,a] v [D0[c,a] ^ D 0 [a,a]] = 0 v [0 ^ 0] = 0


D1 [c,b] = D0 [c,b] v [D0[c,a] ^ D 0 [a,b]] = 0 v [0 ^ 1] = 0
D1 [c,c] = D0 [c,c] v [D0[c,a] ^ D 0 [a,c]] = 0 v [0 ^ 0] = 0
D1 [c,d] = D0 [c,d] v [D0[c,a] ^ D 0 [a,d]] = 0 v [0 ^ 0] = 0

D1 [d,a] = D0 [d,a] v [D0[d,a] ^ D 0 [a,a]] = 1 v [1 ^ 0] = 1


D1 [d,b] = D0 [d,b] v [D0[d,a] ^ D 0 [a,b]] = 0 v [1 ^ 1] = 1
D1 [d,c] = D0 [d,c] v [D0[d,a] ^ D 0 [a,c]] = 1 v [1 ^ 0] = 1
D1 [d,d] = D0 [d,d] v [D0[d,a] ^ D 0 [a,d]] = 0 v [1 ^ 0] = 0

The above 16 values will be put in a table called D1 as shown.

a b c d
a 0 1 0 0
C=D1 = b 0 0 0 1
c 0 0 0 0
d 1 1 1 0

Step 2:
Let k = b and obtain D2
D2 [a,a] = D1 [a,a] v [D1 [a,b] ^ D1 [b,a]] = 0 v [1 ^ 0] = 0
D2 [a,b] = D1 [a,b] v [D1 [a,b] ^ D1 [b,b]] = 1 v [1^ 0] = 1
D2 [a,c] = D1 [a,c] v [D1 [a,b] ^ D1 [b,c]] = 0 v [1 ^ 0] = 0
D2 [a,d] = D1 [a,d] v [D1 [a,b] ^ D1 [b,d]] = 0 v [1 ^ 1] = 1

D2 [b,a] = D1 [b,a] v [D1 [b,b] ^ D1 [b,a]] = 0 v [0 ^ 0] = 0


D2 [b,b] = D1 [b,b] v [D1 [b,b] ^ D1 [b,b]] = 0 v [0 ^ 0] = 0
D2 [b,c] = D1 [b,c] v[D1 [b,b] ^ D1 [b,c]] = 0 v [0 ^ 0] = 0
D2 [b,d] = D1 [b,d] v [D1 [b,b] ^ D1 [b,d]] = 1 v [0 ^ 1] = 1

D2 [c,a] = D1 [c,a] v [D1 [c,b] ^ D1 [b,a]] = 0 v [0 ^ 0] = 0


D2 [c,b] = D1 [c,b] v [D1 [c,b] ^ D1 [b,b]] = 0 v [0 ^ 0] = 0
D2 [c,c] = D1 [c,c] v [D1 [c,b] ^ D1 [b,c]] = 0 v [0 ^ 0] = 0
D2 [c,d] = D1 [c,d] v [D1 [c,b] ^ D1 [b,d]] = 0 v [ 0^ 1] = 0

D2 [d,a] = D1 [d,a] v [D1 [d,b] ^ D1 [b,a]] = 1 v [1 ^ 0] = 1


D2 [d,b] = D1 [d,b] v [D1 [d,b] ^ D1 [b,b]] = 1 v [1 ^ 0] = 1
D2 [d,c] = D1 [d,c] v [D1[d,b] ^ D1 [b,c]] = 1 v [1 ^ 0] = 1
D2 [d,d] = D1 [d,d] v [D1 [d,b] ^ D1 [b,d]] = 0 v [1 ^ 1] = 1

The above 16 values will be put in a table called D2 as shown.

a b c d
A 0 1 0 1
C=D2 = B 0 0 0 1
C 0 0 0 0
D 1 1 1 1

Step 3:
Let k = c and obtain D3
D3 [a,a] = D2 [a,a] v [D2 [a,c] ^ D2 [c,a]] = 0 v [0 ^ 0] = 0
D3 [a,b] = D2 [a,b] v [D2 [a,c] ^ D2 [c,b]] = 1 v [0 ^ 0] = 1
D3 [a,c] = D2 [a,c] v [D2 [a,c] ^ D2 [c,c]] = 0 v [0 ^ 0] = 0
D3 [a,d] = D2 [a,d] v [D2 [a,c] ^ D2 [c,d]] = 1 v [0 ^ 0] = 1

D3 [b,a] = D2 [b,a] v [D2 [b,c] ^ D2 [c,a]] = 0 v [0 ^ 0] = 0


D3 [b,b] = D2 [b,b] v [D2 [b,c] ^ D2 [c,b]] = 0 v [0 ^ 0] = 0
D3 [b,c] = D2 [b,c] v[D2 [b,c] ^ D2 [c,c]] = 0 v [0 ^ 0] = 0
D3 [b,d] = D2 [b,d] v [D2 [b,c] ^ D2 [c,d]] = 1 v [0 ^ 0] = 1

D3 [c,a] = D2 [c,a] v [D2 [c, c] ^ D2 [c,a]] = 0 v [0 ^ 0] = 0


D3 [c,b] = D2 [c,b] v [D2 [c,c] ^ D2 [c,b]] = 0 v [0 ^ 0] = 0
D3 [c,c] = D2 [c,c] v [D2 [c,c] ^ D2 [c,c]] = 0 v [0 ^ 0] = 0
D3 [c,d] = D2 [c,d] v [D2 [c,c] ^ D2 [c,d]] = 0 v [ 0^ 0] = 0
D3 [d,a] = D2 [d,a] v [D2 [d,c] ^ D2 [a,a]] = 1 v [1 ^ 0] = 1
D3 [d,b] = D2 [d,b] v [D2 [d,c] ^ D2 [c,b]] = 1 v [1 ^ 0] = 1
D3 [d,c] = D2 [d,c] v [D2 [d,c] ^ D2 [c,c]] = 1 v [1 ^ 0] = 1
D3 [d,d] = D2 [d,d] v [D2 [d,c] ^ D2 [c,d]] = 0 v [1 ^ 0] = 1

The above 16 values will be put in a table called D1 as shown.

a b c d
A 0 1 0 1
C=D3 = B 0 0 0 1
C 0 0 0 0
D 1 1 1 1

Step 4:
Let k = d and obtain D4
D4 [a,a] = D3 [a,a] v [D3 [a,d] ^ D3 [d,a]] = 0 v [1 ^ 1] = 1
D4[a,b] = D3 [a,b] v [D3 [a,d] ^ D3 [d,b]] = 1 v [1 ^ 1] = 1
D4 [a,c] = D3 [a,c] v [D3 [a,d] ^ D3 [d,c]] = 0 v [1 ^ 1] = 1
D4 [a,d] = D3 [a,d] v [D3 [a,d] ^ D3 [d,d]] = 1 v [1 ^ 1] = 1

D4 [b,a] = D3 [b,a] v [D3 [b,d] ^ D3 [d,a]] = 0 v [1 ^ 1] = 1


D4 [b,b] = D3 [b,b] v [D3 [b,d] ^ D3 [d,b]] = 0 v [1 ^ 1] = 1
D4 [b,c] = D3 [b,c] v[D3 [b,d] ^ D3[d,c]] = 0 v [1 ^ 1] = 1
D4 [b,d] = D3 [b,d] v [D3 [b,d] ^ D3 [d,d]] = 1 v [1 ^ 1] = 1

D4 [c,a] = D3 [c,a] v [D3 [c,d] ^ D3 [d,a]] = 0 v [0 ^ 1] = 0


D4 [c,b] = D3 [c,b] v [D3 [c,d] ^ D3 [d,b]] = 0 v [0 ^ 1] = 0
D4 [c,c] = D3 [c,c] v [D3 [c,d] ^ D3 [d,c]] = 0 v [0 ^ 1] = 0
D4 [c,d] = D3 [c,d] v [D3 [c,d] ^ D3 [d,d]] = 0 v [ 0 ^ 1] = 0

D4 [d,a] = D3 [d,a] v [D3 [d,d] ^ D3 [d,a]] = 1 v [1 ^ 1] = 1


D4 [d,b] = D3 [d,b] v [D3 [d,d] ^ D3 [d,b]] = 1 v [1 ^ 1] = 1
D4 [d,c] = D3 [d,c] v [D3 [d,d] ^ D3 [d,c]] = 1 v [1 ^ 1] = 1
D4 [d,d] = D3 [d,d] v [D3 [d,d] ^ D3 [d,d]] = 1 v [1 ^ 1] = 1

The above 16 values will be put in a table called D1 as shown.

a b c d
A 1 1 1 1
C=D4 = B 1 1 1 1
C 0 0 0 0
d 1 1 1 1

18. What is Knapsack problem ? Solve the following instance of Knapsack problem using Branch-and-Bound method
where n = 4, m = 10, P = (40, 42, 25, 12) and
Solution:
Firstly, find out the pi / wi values and arrange them in descending order
𝑃𝑖 40
= = 10
𝑊𝑖 4

𝑃𝑖 42
= =6
𝑊𝑖 7

𝑃𝑖 25
= =5
𝑊𝑖 5

𝑃𝑖 12
= =4
𝑊𝑖 3

Objest Weight Profit 𝑷𝒊


𝑾𝒊
1 4 40 10
2 7 42 6
3 5 25 5
4 3 12 4

-(2m)
Total Knapsack capacity M = 10
At the root of state – space tree , no objects have been selected as yet. Hence, w and p value is set to 0. The upper bound va
ub can be computed as
Ub: 40 + (10 - 4)*10 = 100

W=0, p=0
Ub=100

Node 1 represents the subsets that include object 1. Hence, we set w, = 4 and p = 40 respectively:
𝑃2
The = 6 value of upper bound is
𝑊2 Each node – ½ m

Total – 6m
Ub: 40+ (10 - 4) 6 = 76

Node 2 represents the subsets that dose not include object 1hence, we set w = 0 and p = 0 respectively; the value of upper
bound is
Ub: 0(10-0)6 = 60

Node 3 represents the subsets that included object 1 and object 2. The value of w is sum of weights of object1 and object 2 a
𝑃3
the value of p is sum of profits of object 1 and object 2. The = 5 The upper bound value is
𝑊3

Ub: 82+(10-11)5 = not possible because M=10 only

Node 5 represents the subsets that include object 1. Hence, we set w, = 4 and p = 40 respectively:
𝑃2
The = 5 value of upper bound is
𝑊2

Ub: 42+(10-7)6 = 60

Node 6 represents the subsets that dose not include object 1 and object 2 hence, we set w = 0 and p = 0 respectively; the val
of upper bound is
Ub: 0+(10-0)5 = 50

Node 7 represents the subsets that included object 1 and object 2 , and object 3. The value of w is sum of weights of object1
𝑃3
and object 2 and object 3 and the value of p is sum of profits of object 1 and object 2. The = 4 The upper bound value is
𝑊3
not possible because M=10 only

Node 8 represents the subsets that does not include object 1 and object 2 hence, the value of upper bound is
not possible because M=10 only
Node 9 represents the subsets that included object 1 and object 2 , and object 3. The value of w is sum of weights of object1
𝑃3
and object 2 and object 4 and the value of p is sum of profits of object 1 and object 2. The = 1 The upper bound value is
𝑊3

not possible because M=10 only


Node 10 represents the subsets that include object 1 and object 2 hence, the value of upper bound is
not possible because M=10 only

Node 11 represents the subsets that included object 2 , and object 3. The value of w is sum of weights of object1 and object 2
𝑃3
and object 4 and the value of p is sum of profits of object 1 and object 2. The = 1 The upper bound value is
𝑊3

not possible because M=10 only

Node 12 represents the subsets that include object 2 hence, the value of upper bound is
Ub : 42+(10-7)1 = 45

W=0, p=0
Ub=100
With 1 with out 1

W=4, p=40 W=0, p=0


Ub=76 Ub=60
With2 w o/t 2 with 2 w o/t 2

W=11, p=82 W=4, p=40 W=7, p=42 W=0, p=0


Ub= Ub=70 Ub=57 Ub=50

Not feasible

You might also like