DAA 2nd Unit Notes
DAA 2nd Unit Notes
Examples:
Sort 45,20,40,5,10
Bubble Sort Technique:
Sequential / Linear Search:
Linear search is also called as sequential search algorithm. It is the simplest
searching algorithm.
In Linear search, we simply traverse the list completely and match each element of
the list with the item whose location is to be found. If the match is found, then the
location of the item is returned; otherwise, the algorithm returns NULL.
Working of Linear search
Now, let's see the working of the linear search Algorithm.
Now, start from the first element and compare K with each element of the
array.
The value of K, i.e., 41, is not matched with the first element of the array.
So, move to the next element. And follow the same process until the
respective element is found.
Now, the element to be searched is found. So algorithm will return the index
of the element matched.
Exhaustive Search:
Travelling Salesman Problem (TSP)
The Travelling Salesman Problem (TSP) is a very well-known problem
in theoretical computer science and operations research.
Introduction to TSP
In the TSP, given a set of cities and the distance between each pair of cities, a salesman needs
to choose the shortest path to visit every city exactly once and return to the city from where he
started.
Here, the nodes represent cities, and the values in each edge denote the distance between one
city to another. Let’s assume a salesman starts the journey from the city . According to TSP,
the salesman needs to travel all the cities exactly once and get back to the city by choosing
the shortest path. Here the shortest path means the sum of the distance between each city
travelled by the salesman, and it should be less than any other path.
• Given “n” cities, a salesperson starts at a specified city(often called source), visit all “n-
1” cities only once and return to the city from where he has started.
• The objective of this problem is to find a route through the cities that minimizes the cost
thereby maximizing the profit
Consider an example of TSP
BFS algorithm
we will discuss the BFS algorithm in the data structure. Breadth-first search is a
graph traversal algorithm that starts traversing the graph from the root node and
explores all the neighboring nodes. Then, it selects the nearest node and explores
all the unexplored nodes. While using BFS for traversal, any node in the graph can
be considered as the root node.
There are many ways to traverse the graph, but among them, BFS is the most
commonly used approach. It is a recursive algorithm to search all the vertices of a
tree or graph data structure. BFS puts every vertex of the graph into two categories
- visited and non-visited. It selects a single node in a graph and, after that, visits all
the nodes adjacent to the selected node.
Algorithm
The steps involved in the BFS algorithm to explore a graph are given as
follows -
Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)
Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).
Step 5: Enqueue all the neighbors of N that are in the ready state (whose STATUS = 1) and
set
[END OF LOOP]
Step 6: EXIT
DFS (Depth First Search) algorithm
we will discuss the DFS algorithm in the data structure. It is a recursive
algorithm to search all the vertices of a tree data structure or a graph. The
depth-first search (DFS) algorithm starts with the initial node of graph G and
goes deeper until we find the goal node or the node with no children.
Algorithm
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Push the starting node A on the stack and set its STATUS = 2
(waiting state)
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed
state)
Step 5: Push on the stack all the neighbors of N that are in the ready state
(whose STATUS = 1) and set their STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
Difference between BFS & DFS:
Knapsack Problem
The knapsack problem states that − given a set of items, holding weights and profit values, one
must determine the subset of the items to be added in a knapsack such that, the total weight of
the items must not exceed the limit of the knapsack and its total profit value is maximum. It is
one of the most popular problems that take greedy approach to be solved. It is called as
the Fractional Knapsack Problem.
o The first approach is to select the item based on the maximum profit.
o The second approach is to select the item based on the minimum weight.
o The third approach is to calculate the ratio of profit/weight.
Objects: 1 2 3 4 5 6 7
Profit (P): 10 15 7 8 9 4
Weight(w): 1 3 5 4 1 3 2
n (no of items): 7
First approach:
3 15 5 15 - 5 = 10
2 10 3 10 - 3 = 7
6 9 3 7-3=4
5 8 1 4-1=3
7 7 * ¾ = 5.25 3 3-3=0
Second approach:
The second approach is to select the item based on the minimum weight.
Object Profit Weight Remaining weight
1 5 1 15 - 1 = 14
5 7 1 14 - 1 = 13
7 4 2 13 - 2 = 11
2 10 3 11 - 3 = 8
6 9 3 8-3=5
4 7 4 5-4=1
3 15 * 1/5 = 3 1 1-1=0
Third approach:
In the third approach, we will calculate the ratio of profit/weight.
Objects: 1 2 3 4 5 6 7
Profit (P): 5 10 15 7 8 9 4
Weight(w): 1 3 5 4 1 3 2
Object 1: 5/1 = 5
Object 2: 10/3 = 3. 33
Object 3: 15/5 = 3
Object 6: 9/3 = 3
Object 7: 4/2 = 2
In this approach, we will select the objects based on the maximum profit/weight ratio. Since the P/W of
object 5 is maximum so we select object 5.
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
3 15 5 10 - 5 = 5
6 9 3 5-3=2
7 4 2 2-2=0
As we can observe in the above table that the remaining weight is zero which
means that the knapsack is full. We cannot add more objects in the knapsack.
Therefore, the total profit would be equal to (8 + 5 + 10 + 15 + 9 + 4), i.e., 51
n the first approach, the maximum profit is 47.25. The maximum profit in the
second approach is 46. The maximum profit in the third approach is 51. Therefore,
we can say that the third approach, i.e., maximum profit/weight ratio is the best
approach among all the approaches.
A pseudo-code for solving knapsack problems using the greedy method is;
For j ← 1 to n do
X[j]← 0
profit ← 0 // Total profit of item filled in the knapsack
weight ← 0 // Total weight of items packed in knapsacks
j←1
While (Weight < M) // M is the knapsack capacity
}
Profit = profit + p[j] * X[j]
j++;
} // end of while
} // end of Algorithm