Uploaded by Sai Rohith

Algo HW4

advertisement
Chapter 6, Problem 7
Travelling salesman problem:
• The travelling salesman problem is defined to find the path from source to destination
that has lowest cost.
• The weighted graph will be provided to find the path and the cost.
Step 2/13
Consider the information:
• Refer to the information given in the Exercise.
• Analyze the algorithm 6.3 provided in the textbook to provide the optimal tour for the
given graph using the Travelling salesman problem using branch and bound.
Step 3/13
Optimal tour for the given graph:
The adjacency matrix for the given directed weighted graph is identified by finding the
edges between the vertices.
If there is an edge between the vertex, then the weight is provided as the value of the
matrix. Otherwise, zero is provided.
The adjacency matrix for the given graph is as follows.
…...(1)
The following steps are followed to find the optimal tour for the given matrix.
Step 4/13
Step 1:
Initially, assign the “minlength” to infinity and calculate bound for the initial matrix. The
first row represents v1, second row represents v2 and so on. The bound can be
calculated by adding the minimum value from each row.
v1 minimum (5,8,0) = 0
v2 minimum (4,4,0) = 0
v3 minimum (2,5,0) = 0
v4 minimum (7,0,0) = 0
v5 minimum (1,0,0) = 0
v6 minimum (6,2,0) = 0
v7 minimum (10,8,0) = 0
v8 minimum (5,4,0) = 0
Now, add the minimum values to find the lower bound which is equal to 0.
Step 5/13
Step 2:
From the vertex 1, there are seven possibility which are [1,2], [1,3], [1,4], [1,5], [1,6],
[1,7], and [1,8]. Now, compute lower bound for every node.
Lower bound for [1,2]:
Now, the vertex v1 row, v2 column and v2 to v1 are visited so it becomes infinity, so the
matrix becomes
Here, the v1 is visited so find the value from v1 to v2 from the initial matrix represented
in Equation (1) which is 5 and the minimum value for other vertices are computed as
follows.
v1 = 5
v2 minimum (4,4,0) = 0
v3 minimum (2,5,0) = 0
v4 minimum (7,0,0) = 0
v5 minimum (1,0,0) = 0
v6 minimum (6,2,0) = 0
v7 minimum (10,8,0) = 0
v8 minimum (5,4,0) = 0
Now, add the minimum values to find the lower bound for [1,2] which is equal to 5.
Step 6/13
Lower bound for [1,3]:
Now, the vertex v1 row, v3 column and v3 to v1 are visited so it becomes infinity, so the
matrix becomes
Here, the v1 is visited so find the value from v1 to v3 from the initial matrix represented
in Equation(1) which is 8 and the minimum value for other vertices are computed as
follows.
v1 = 8
v2 minimum (4,4,0) = 0
v3 minimum (2,5,0) = 0
v4 minimum (7,0,0) = 0
v5 minimum (1,0,0) = 0
v6 minimum (6,2,0) = 0
v7 minimum (10,8,0) = 0
v8 minimum (5,4,0) = 0
Now, add the minimum values to find the lower bound for [1,3] which is equal to 8.
Lower bound for [1,4], [1,5], [1,6], [1,7], and [1,8]:
Now, the bound value will be zero since all the minimum value of the vertices is 0.
Step 7/13
Step 3:
Find the node which has smallest lower bound. The node [1,2] has smallest lower
bound. Now, find the vertex which is traverse from 1 and 2. The nodes are [1,2,3],
[1,2,4], [1,2,5], [1,2,6], [1,2,7], and [1,2,8]. Now, compute lower bound for every node.
Step 8/13
Step 4:
Lower bound for [1,2,3]:
Now, the vertex v1 row, v3 column, v2 column, v2 to v1 and v3 to v1 are visited so it
becomes infinity, so the matrix becomes
Here, the v1 is visited so find the value from v1 to v2 from initial matrix represented in
Equation (1) which is 5 and v2 is visited so find the value from v2 to v3 from the initial
matrix represented in Equation(1) which is 4 and the minimum value for other vertices
are computed as follows.
v1 = 5
v2 =4
v3 minimum (2,5,0) = 0
v4 minimum (7,0,0) = 0
v5 minimum (1,0,0) = 0
v6 minimum (6,2,0) = 0
v7 minimum (10,8,0) = 0
v8 minimum (5,4,0) = 0
Now, add the minimum values to find the lower bound for [1,2,3] which is 5+4 is equal to
9.
Similarly, the lower bounds for [1,2,4], [1,2,5], [1,2,6], [1,2,7], and [1,2,8] is 5, 9, 5, 5,
and 5 respectively.
Step 9/13
Step 5:
Find the node which has first smallest lower bound. The node [1,2,3] has smallest lower
bound. Now, find the vertex, which is traversed from 1, 3 and 2. The nodes are [1,2,3,4],
[1,2,3,5], [1,2,3,6], [1,2,3,7], and [1,3,2,8]. Now, compute lower bound for every node.
Step 10/13
Step 6:
Lower bound for [1,2,3,4]:
After making the visited vertex as infinity, the matrix becomes
Here, the v1 is visited so find the value from v1 to v4 from the initial matrix represented
in Equation(1) which is 5 and v2 is visited so find the value from v2 to v3 from the initial
matrix represented in Equation (1), which is 4, v3 is visited so find the value from v3 to
v4 from the initial matrix represented in Equation (1) which is 2 and the minimum value
for other vertices are computed as follows.
v1 = 5
v2 =4
v3 =2
v4 minimum (7,0,0) = 0
v5 minimum (1,0,0) = 0
v6 minimum (6,2,0) = 0
v7 minimum (10,8,0) = 0
v8 minimum (5,4,0) = 0
Add the minimum values to find the lower bound for [1,3,2,4] which is 5+4+2 is equal to
11. Now, set the value of “minlength” to 11.
Similarly, the lower bounds for [1,2,3,5], [1,2,3,6], [1,2,3,7], and [1,3,2,8] is 9, 9, 14, and
9 respectively.
The adjacent matrix for the graph is:
v1
v2
v3
v1
0
5
8 infinity
0
4 infinity
v2
infinity
v3
infinity
infinity
v4
infinity
infinity
infinity
1 infinity
infinity
v5
v4
0
v6
v7
v8
infinity
infinity
infinity
infinity
4 infinity
infinity
infinity
2 infinity
infinity
0 infinity
infinity
infinity
infinity
0 infinity
infinity
infinity
6 infinity
infinity
2
0 infinity
infinity
2
8
v6
infinity
v7
infinity
infinity
infinity
infinity
v8
infinity
From to to to
infinity
infinity
infinity
v1
v5
infinity
5 infinity
infinity
7
0 infinity
4
v2
v3
v4
v5
v6
v7
v8
0
infinity
infinity
infinity
infinity
infinity
infinity
infinity
0
5
infinity
infinity
infinity
infinity
infinity
0
0
5
8
infinity
9
infinity
infinity
infinity
0
5
8
10
9
infinity
13
infinity
0
5
8
10
9
infinity
13
infinity
0
5
8
10
9
infinity
13
17
0
5
8
10
9
21
13
17
The optimal tour is:
The total length of the optimal tour is: 33
Step 11/13
Step 7:
The process continues for making the v5 infinity until v8. The minimum lower bound
value will be the value for optimal tour which will be 33.
Step 12/13
Step 8:
The final “minlength” is “33” for the node [1 2 3 4 8 7 6 5 1]. Now, the path will be 1 to 2,
2 to 3, 3 to 4, 4 to 8, 8 to 7, 7 to 6, 6 to 5 and 5 to 1.
So, the optimal tour length will be 33.
Step 13/13
Therefore, the optimal tour for the given matrix is provided using Travelling salesman
problem.
Chapter 6 Problem 10
introduction :
The Traveling Salesperson Problem (TSP) is a classic optimization problem in computer
science and mathematics that involves finding the most efficient route that visits a set of
cities and returns to the starting city. The goal is to minimize the total distance or cost of
the tour while visiting each city exactly once. The problem is NP-hard, meaning that as
the number of cities increases, the computation time required to find an optimal solution
grows exponentially.
In this context, the question at hand explores two different algorithms for solving the
TSP: a brute-force approach and a Best-First Search with a Branch-and-Bound Pruning
algorithm. The brute-force algorithm exhaustively considers all possible tours to find the
optimal solution, making it a straightforward but computationally expensive method. On
the other hand, the Best-First Search algorithm incorporates pruning techniques to
reduce the search space, potentially providing more efficient solutions, especially for
larger instances.
The task involves implementing and comparing these algorithms on TSP instances of
various sizes (6, 7, 8, 9, 10, 15, and 20). The code includes the implementation of the
brute-force algorithm and hints at the existence of another algorithm (Algorithm 6.3) that
needs to be implemented separately. The objective is to evaluate and compare the
performance of the two algorithms in terms of solution quality and execution time.
Step 2/3
Code :
let's start with the brute-force algorithm for the Traveling Salesperson Problem (TSP)
Brute-Force Algorithm :
from itertools import permutations
def brute_force_tsp(graph):
n = len(graph)
min_length = float('inf')
optimal_tour = None
# Generate all possible tours
all_tours = permutations(range(1, n))
for tour in all_tours:
current_tour = [0] + list(tour) + [0]
# Always start and end at
vertex 0
tour_length = 0
# Calculate the length of the current tour
for i in range(n):
tour_length += graph[current_tour[i]][current_tour[i + 1]]
# Update the minimum length and optimal tour if needed
if tour_length < min_length:
min_length = tour_length
optimal_tour = current_tour
return min_length, optimal_tour
# Example usage:
graph = [
[0, 10, 15, 20],
[10, 0, 35, 25],
[15, 35, 0, 30],
[20, 25, 30, 0]
]
min_length, optimal_tour = brute_force_tsp(graph)
print(f"Minimum Length: {min_length}")
print(f"Optimal Tour: {optimal_tour}")
let's proceed to implement the algorithm for instances of sizes 6, 7, 8, 9, 10, 15, and 20.
Implementation and Comparison:
import time
def generate_complete_graph(n):
# Generating a complete graph with random weights
import random
return [[random.randint(1, 100) for _ in range(n)] for _ in range(n)]
sizes = [6, 7, 8, 9, 10, 15, 20]
for size in sizes:
graph_instance = generate_complete_graph(size)
# Brute-force algorithm
start_time_brute = time.time()
min_length_brute, _ = brute_force_tsp(graph_instance)
end_time_brute = time.time()
elapsed_time_brute = end_time_brute - start_time_brute
print(f"Instance size: {size}")
print(f"Brute-Force Algorithm - Minimum Length: {min_length_brute},
Elapsed Time: {elapsed_time_brute} seconds")
# Implement and compare with Best-First Search with Branch-and-Bound
Pruning Algorithm
# ...
print("\n")
Step 3/3
Let's break down the provided code and its components.
Explanation:
Brute-Force Algorithm:
The brute-force algorithm for the Traveling Salesperson Problem (TSP) is a
straightforward approach that considers all possible tours and selects the one with the
minimum total length. Here's an explanation of the key components:
 brute_force_tsp function:
o Takes a weighted graph as input, where graph[i][j] represents the
weight of the edge from vertex i to vertex j.
o Generates all possible tours using permutations of vertices (excluding the
starting vertex, which is always 0).
o Calculates the length of each tour and keeps track of the minimum length
and the corresponding optimal tour.
o Returns the minimum length and the optimal tour.
Implementation and Comparison:
 generate_complete_graph function:
o Generates a complete graph with random weights for edges. This is used
for testing the algorithm.
 Instance Generation and Execution Loop:
o Iterates through different instance sizes (6, 7, 8, 9, 10, 15, and 20).
o For each instance size:
 Generates a complete graph using the generate_complete_graph
function.
 Applies the brute-force algorithm to find the minimum length and
optimal tour.
 Measures the elapsed time for the brute-force algorithm.
 Print Statements:
o Outputs information about each instance, including the instance size,
minimum length found by the brute-force algorithm, and the time it took to
execute.
What's Next (Not Implemented):
 Best-First Search with Branch-and-Bound Pruning Algorithm:
o The code mentions "Algorithm 6.3," which presumably is a Best-First
Search with Branch-and-Bound Pruning Algorithm for the TSP. This
algorithm is not provided in the code snippet.
o You would need to implement Algorithm 6.3 separately in your chosen
programming language.
o After implementation, you can integrate it into the loop and compare its
performance (minimum length and execution time) with the brute-force
algorithm.
Chapter 7 Problem 11
Permutation:
• The permutation of the very first n number of positive integers is defined as the
ordering of the integers.
• There will be “
” permutations for the above-specified number of positive integers.
Step 2/5
Inversion:
• In a permutation, an inversion can be defined as a pair,
, where
and
.
• For example, if [3, 1, 4, 2] is a permutation, then it consists of the inversions (3, 1), (3,
2), and (4, 2).
Step 3/5
Transpose:
• The transpose of a permutation can be defined as another permutation with elements
in reverse order to the order of elements in the initial permutation.
• Consider an example, the transpose of [3, 1, 4, 2] is [2, 4, 1, 3].
Step 4/5
To Show:
It is required to show that there is
number of inversions within a permutation
of distinct ordered elements concerning its transpose.
Step 5/5
Proof:
The proof is as follows.
• A permutation
order between
the transpose.
has an inversion (i, j) concerning its transpose if the
and
in the permutation is opposite of the same elements’ order in
• It is very simple to see that all the elements change the order in the transpose, so the
number of inversions is the number of distinct pairs (i, j).
• In section A.7 from chapter 7 in the textbook, it has already shown that the number of
combinations of n objects have took k at a time is
• For simplification, making
.
gives,
Thus, it is shown that there is
number of inversions within a permutation of
distinct ordered elements concerning its transpose.
Chapter 7 Problem 26
Approach to check binary tree as a heap:
• Define a Boolean function called isHeap() with a node n as the input parameter.
• If the left node and right node are null, return true.
• If the right node is null and the left node is greater than n, return False, else return
True.
• If the left node is greater than n or the right node is smaller than n, return false.
Step 2/4
Algorithm:
Problem: To check whether a binary tree, which is essentially complete, is a heap or
not.
Input: The argument n represents the nodes of the tree.
Output: Return True or False
//define the function
boolean isHeap(node n) {
//check whether the left and right nodes are null
//if the condition is satisfied, return it as true
if(n.left==null && n.right==null) return true;
//check whether the right node is null
if(n.right==null) {
//then check if the left node is greater than n
//return false the condition is true
if(n.left > n) return false;
//else return it as True
return true;
}
//check if the left node > n or the right node < n
//if so return false
if(n.left > n || n.right < n) return false;
//call the function
return isHeap(n.left) && isHeap(n.right);
}
Step 3/4
Explanation:
• The function “isHeap()” is created with the argument “n” representing the node in the
tree.
o The “if” statement will check whether the left node and the right node are null. If so,
“True” will be returned.
o Now, if the right node is null.
• It will check whether the left node is greater than “n”. If so, “False” will be returned.
• Else, “True” will be returned.
o Another “if” statement will check whether the left node is greater than “n” or the right is
lesser than “n”. If so, return “false”.
o Finally, call the defined function.
Note: A node in an essentially complete binary tree cannot have a null left child unless
the right child is also null.
Step 4/4
Analysis of the algorithm:
• This algorithm runs a pre-order traversal of the given tree, in which each node should
be visited exactly once.
• Thus, the time complexity of the algorithm is
of nodes present in the tree.
Chapter 7 Problem 35
, in this n represents the number
Step 1/9
Insertion sort:
• Insertion sort is one of the simple sorting algorithms.
• The insertion sort method functions similarly to how hands of playing cards are sorted.
• In this algorithm, a sorted and an unsorted portion of the data are effectively
separated.
• Values are selected and placed in the appropriate positions in the sorted section by
selecting them from the unsorted part.
Step 2/9
Selection sort:
• The selection sort is one of the efficient sorting algorithms.
• This algorithm separates the list into two halves, the sorted portion on the left and the
unsorted piece on the right.
• It is referred to as an in-place comparison-based algorithm.
• The cost of swapping is not important in the selection sort because each element must
be checked.
• The selection sort algorithm will be more effective when the size of the data list is
smaller.
Step 3/9
Mergesort:
• The merge sort is one of the popular sorting algorithms.
• It will use the divide and conquer technique.
• The merge sort algorithm will divide the list by two halves repeatedly until each
element is segregated individually.
• After, that, the segregated lists will be merged with their immediate list in sorted order
repeatedly until each element is merged into a single list.
• Finally, the list will be sorted.
Step 4/9
Quicksort:
• Quick sort is one of the popular sorting algorithms.
• The quick sort algorithm will select a pivot element to divide the data list into more
smaller portions.
• The pivot element should be positioned to split the data so that elements bigger than
the pivot are retained on the right side of the pivot and elements less than the pivot are
maintained on the left side.
• The left and right sublists are divided using the same technique. It will be repeated
until there is just one element left in each subarray.
• At this point, the data will be sorted.
Step 5/9
Heapsort:
• Heap sort works based on the Binary Heap data structure.
• The idea behind a heap sort is to take each element out of the list’s heap section one
by one.
• After that, the elements will be placed in sorted order.
• The heap sort will be done based on the minimum or maximum element.
Step 6/9
(a)
A suitable sorting algorithm for situation (a):
“Merge sort” is suitable for this type of situation.
Reason:
• The mergesort can be utilized to sort the long records with short keys.
• Because the mergesort uses the “divide and conquer” technique.
• Thus, the records will be segregated and sorted easily.
Step 7/9
(b)
A suitable sorting algorithm for situation (b):
“Selection sort” and “Insertion sort” are suitable for this type of situation.
Reason:
• Quadratic algorithms such as selection sort and insertion sort will perform for larger
records like 45,000.
• In this situation, the heapsort will be suitable because it is an in-place algorithm.
• Thus, it does not need any extra space.
Step 8/9
(c)
A suitable sorting algorithm for situation (c):
“Merge sort” is suitable for this type of situation.
Reason:
• Quicksort will “go quadratic” if the list is merely slightly out of order.
• Thus, quick sort is not suitable in this case.
• Mergesort defeats heapsort because it is twice as quick in comparisons and can be
improved in assignments by selecting the linked list variant when extra space is not a
concern.
Step 9/9
(d)
A suitable sorting algorithm(s) for situation (d):
“Quick sort” is suitable for this type of situation.
Reason:
• When a stable sort is not required an average-case performance is more important
than a worst-case performance, quick sort is the best option.
• A rapid sort typically takes
, and in the worst case, it takes
• For recursion, a suitable implementation employs stack space with
auxiliary storage.
.
Download