CSE703 Module1
CSE703 Module1
CSE703
• Algorithms, Analyzing Algorithms, Complexity of Algorithms, Growth
of Functions, Performance Measurements, Asymptotic Notations.
Recurrences- substitution method, recursion tree method, master
method
• Types of algorithm: Divide and Conquer with Examples Such as
Sorting, Matrix Multiplication, Convex Hull and Searching. Greedy
Methods with Examples Such as Optimal Reliability Allocation,
Knapsack, Minimum Spanning Trees – Prim’s and Kruskal’s
Algorithms, Single Source Shortest Paths – Dijkstra’s and Bellman
Ford Algorithms. Dynamic Programming with Examples Such as
Knapsack. All Pair Shortest Paths – Warshal’s and Floyd’s
Algorithms, Resource Allocation Problem. Backtracking, Branch and
Bound with Examples Such as Travelling Salesman Problem, Graph
Coloring, n-Queen Problem, Hamiltonian Cycles and Sum of
Subsets.
Amity School of Engineering & Technology
Th pp 2
urs
Amity School of Engineering & Technology
Th 3
Design
urs
and Analysis of Computer
Amity School of Engineering & Technology
Algorithm
Algorithm is a step-by-step procedure, which
defines a set of instructions to be executed in
a certain order to get the desired output.
Algorithms are generally created
independent of underlying languages, i.e. an
algorithm can be implemented in more than
one programming language.
Amity School of Engineering & Technology
Characteristics of an Algorithm
• Unambiguous − Algorithm should be clear and
unambiguous. Each of its steps (or phases), and their
inputs/outputs should be clear and must lead to only
one meaning.
• Input − An algorithm should have 0 or more well-
defined inputs.
• Output − An algorithm should have 1 or more well-
defined outputs, and should match the desired output.
• Finiteness − Algorithms must terminate after a finite
number of steps.
• Feasibility − Should be feasible with the available
resources.
• Independent − An algorithm should have step-by-step
directions, which should be independent of any
programming code.
Amity School of Engineering & Technology
Amity School of Engineering & Technology
Algorithm Complexity
Suppose X is an algorithm and n is the size of
input data, the time and space used by the
algorithm X are the two main factors, which
decide the efficiency of X.
• Time Factor − Time is measured by counting
the number of key operations such as
comparisons in the sorting algorithm.
• Space Factor − Space is measured by
counting the maximum memory space
required by the algorithm.
The complexity of an algorithm f(n) gives the
running time and/or the storage space required
by the algorithm in terms of n as the size of
input data.
Amity School of Engineering & Technology
Analysis of Algorithm
Analysis of algorithm is the process of
analyzing the problem-solving capability of
the algorithm in terms of the time and size
required (the size of memory for storage
while implementation). However, the main
concern of analysis of algorithms is the
required time or performance. Generally, we
perform the following types of analysis −
Amity School of Engineering & Technology
Asymptotic Notations
• Execution time of an algorithm depends on
the instruction set, processor speed, disk
I/O speed, etc. Hence, we estimate the
efficiency of an algorithm asymptotically.
• Time function of an algorithm is
represented by T(n), where n is the input
size.
Amity School of Engineering & Technology
Control Structures
1. Sequence logic or Sequential flow
Space Complexity
• S(P)= C + SP
Time Complexity
Analysis of Loops
1) O(1): Time complexity of a function (or
set of statements) is considered as O(1)
if it doesn’t contain loop, recursion and
call to any other non-constant time
function.
Amity School of Engineering & Technology
int fun(int n)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < n; j += i)
{
// Some O(1) task
}
}
}
Amity School of Engineering & Technology
Answer
• O(nlogn)
Amity School of Engineering & Technology
• O(n)
Amity School of Engineering & Technology
What is time complexity of fun()?
int fun(int n)
{
int count = 0;
for (int i = 0; i < n; i++)
for (int j = i; j > 0; j--)
count = count + 1;
return count;
}
Amity School of Engineering & Technology
Answer
• O( n2 )
Amity School of Engineering & Technology
What is time complexity of fun()?
• O(n)
Amity School of Engineering & Technology
What is time complexity ()?
int a = 0, i = N;
while (i > 0)
{
a += i;
i /= 2;
}
Amity School of Engineering & Technology
Answer
• O(log N)
Amity School of Engineering & Technology
Recurrences
• A recurrence is an equation or
inequality that describes a
function in terms of its value on
smaller inputs. Recurrences are
generally used in divide-and-
conquer paradigm.
Amity School of Engineering & Technology
• T (n) = 2T (n-1)
• = 2[2T (n-2)] = 22T (n-2)
• = 4[2T (n-3)] = 23T (n-3)
• = 8[2T (n-4)] = 24T (n-4) …………….(Eq.1)
• T (n) = T (n-1) +1
• = (T (n-2) +1) +1
• = (T (n-3) +1) +1+1
• = T (n-4) +4
• = T (n-5) +1+4 = T (n-5) +5
• = T (n-k) + k
• Where k = n-1 ,T (n-k) = T (1) = θ (1)
• T (n) = θ (1) + (n-1) = 1+n-1=n= θ (n).
Amity School of Engineering & Technology
Recursion Tree Method
1. Recursion Tree Method is a pictorial
representation of an iteration method which
is in the form of a tree where at each level
nodes are expanded.
2. In general, we consider the second term
in recurrence as root.
3. It is useful when the divide & Conquer
algorithm is used.
Amity School of Engineering & Technology
• O(n2 )
Amity School of Engineering & Technology
Consider the following
recurrence
T (n) = 4T(n/2) +n
• O(n2 )
Amity School of Engineering & Technology
Consider the following
recurrence
Amity School of Engineering & Technology
Answer
• O(n 2 )
Amity School of Engineering & Technology
Consider the following
recurrence
• T(n) = 2T(n/2) + n
Amity School of Engineering & Technology
Answer
• O(nlogn)
Amity School of Engineering & Technology
Consider the following
recurrence
• T(n) = T(n/5) + T(4n/5) + n
Amity School of Engineering & Technology
• O(nlog5/4n)
Amity School of Engineering & Technology
Master Method
• T(n)=9 T(n/3)+n
Amity School of Engineering & Technology
Answer
Θ(n2)
Amity School of Engineering & Technology
• T(n)=T(2n/3) +1
Amity School of Engineering & Technology
Answer
• Θ(log2 n)
Amity School of Engineering & Technology
• T(n)=3T(n/4)+n log2 n
Amity School of Engineering & Technology
Answer
• Θ (n log 2 n)
Amity School of Engineering & Technology
Solve following
• T (n) = 8 T(n/2)+1000n2
• T (n) = 2T(n/2)+ n2
Amity School of Engineering & Technology
Answer
• Θ (n3)
• Θ (n log n)
• Θ(n2)
Solve following using Master
Amity Method
School of Engineering & Technology
Answer
Amity School of Engineering & Technology
Amity School of Engineering & Technology
• Small instance.
Sort a list that has n <= 10 elements.
Find the minimum of n <= 2 elements.
• Large instance.
Sort a list that has n > 10 elements.
Find the minimum of n > 2 elements.
Amity School of Engineering & Technology
Conquer
• Often the smaller instances that result from the
divide step are instances of the original problem
(true for our sort and min problems). In this case,
If the new instance is a small instance, it is solved using
the method for small instances.
If the new instance is a large instance, it is solved using
the divide-and-conquer method recursively.
• Generally, performance is best when the smaller
instances that result from the divide step are of
approximately the same size.
Amity School of Engineering & Technology
-15,-6,0,7,9,23,54,82,101,112,125,131,142,151
Toal 14 elements a[1 : 14}
Low,high,mid we want to search x=151, x=- -14,x=9
1 14 7
8 14 11
12 14 13
14 14 14
Amity School of Engineering & Technology
• Iterative Binary search
• Algo Binsearch(a,n,x)
• {
• Low=1; high=n;
• While(low<=high)
• {
• Mid=(low + high)/2
• If (x <a[mid] then high=mid-1
• Else if (x>a[mid]) then low mid +1;
• Else return mid;
• }
• Return 0;
• }
The BinarySearch() Function
Amity School of Engineering & Technology
Divide-and-Conquer
• Divide-and conquer is a general algorithm
design paradigm:
– Divide: divide the input data S in two or more disjoint
subsets S1, S2, …
– Recur: solve the subproblems recursively
– Conquer: combine the solutions for S1, S2, …, into a
solution for S
• The base case for the recursion are
subproblems of constant size
• Analysis can be done using recurrence
equations
Divide and Conquer Matrix Amity School of Engineering & Technology
Multiply
A B = R
A0 A1 B0 B1 A0B0+A1B2 A0B1+A1B3
=
A2 A3 B2 B3 A2B0+A3B2 A2B1+A3B3
Multiply
A B = R
a0 b0 = a0 b0
Comparison
C11 = P1 + P4 - P5 + P7
= (A11+ A22)(B11+B22) + A22 * (B21 - B11) - (A11 + A12) *
B22+
(A12 - A22) * (B21 + B22)
= A11 B11 + A11 B22 + A22 B11 + A22 B22 + A22 B21 – A22 B11
-
A11 B22 -A12 B22 + A12 B21 + A12 B22 – A22 B21 – A22 B22
= A11 B11 + A12 B21
Amity School of Engineering & Technology
Strassen Algorithm
void matmul(int *A, int *B, int *R, int n) {
if (n == 1) {
(*R) += (*A) * (*B);
} else {
matmul(A, B, R, n/4);
matmul(A, B+(n/4), R+(n/4), n/4);
matmul(A+2*(n/4), B, R+2*(n/4), n/4);
matmul(A+2*(n/4), B+(n/4), R+3*(n/4), n/4);
matmul(A+(n/4), B+2*(n/4), R, n/4);
matmul(A+(n/4), B+3*(n/4), R+(n/4), n/4);
matmul(A+3*(n/4), B+2*(n/4), R+2*(n/4), n/4); Divide matrices in
matmul(A+3*(n/4), B+3*(n/4), R+3*(n/4), n/4); sub-matrices and
} recursively
multiply sub-
matrices
Amity School of Engineering & Technology
Time Analysis
Amity School of Engineering & Technology
Conclusions
• Strassen and Strassen-Winograd are too
unstable for some matrices
• However, they are faster than naïve
multiplication for relatively small n
• Ques. Write Stresen’s algo of matrices
multiplication and prove that it dose 6 n2.81 – 6
n2 multiplication operations on matrix enries
where n is powe of 2?
Amity School of Engineering & Technology
Mergesort
Algorithm:
• Split array A[1
A[1..
..n
n] in two and make
Sort arrays B and C
copies of each half in arrays B[1 B[1.. n/2 ]
Merge sorted arrays B and C into array A
and C[1
C[1.. n/2 ]
123
Using Divide and Conquer: Amity School of Engineering & Technology
Mergesort
• Mergesort Strategy
(first last)2
first last
Sorted Sorted
Merge
Sorted
Design and Analysis of Algorithms – Chapter 4 124
Amity School of Engineering & Technology
Merge sort(Divide and conquer)
• Array a[1 .10]
• =(310,285,179,652,351,423,861,254,450,520)
• Spliting into two sub array a[1..5] to a[6..10]
• Split a[1..3] to a[4..5]
• Split a[1..2] and a [3..3]
• (310 | 285 |179 | 652,351 | 423,861,254,450,520)
• a[1] and a[2]are compare
• 285,310 | 179 | 652,351 | 423,861,254,450,520
• 179,285,310 | 652,351 |423,861,254,450,520
Amity School of Engineering & Technology
1,5 6,10
1,1
2,2 6,6 7,7
Amity School of Engineering & Technology
Recurrences
• The expression:
c n 1
T ( n)
2T cn n 1
n
2
is a recurrence. T(n)=O(n log n)
– Recurrence: an equation that describes a
function in terms of its value on smaller
functions
Idea of Mergesort Amity School of Engineering & Technology
• 83297254
• 72164
• Would you like to play a game?
– Real people animation of mergesort
– Volunteers is needed!
– Try to answer the following questions?
• How many key comparisons needed?
• How much extra memory needed?
• Is the algorithm an in-place algorithm?
• Is the algorithm stable?
8 3 2 9 7 1 5 Amity
4 School of Engineering & Technology
8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
Amity School of Engineering & Technology
Analysis of Mergesort
A = {10, 5, 7, 6, 1, 4, 8, 3, 2, 9};
Amity School of Engineering & Technology
Analysis of Merge Sort
Statement Effort
MergeSort(A, left, right) { T(n)
if (left < right) { (1)
mid = floor((left + right) / 2); (1)
MergeSort(A, left, mid); T(n/2)
MergeSort(A, mid+1, right); T(n/2)
Merge(A, left, mid, right); (n)
}
}
0 n0 0 n0
s ( n) s ( n)
c s (n 1) n 0 n s (n 1) n 0
n 1
c c n 1
T ( n) T ( n)
2T c n 1
n n
2 aT cn n 1
b
Amity School of Engineering & Technology
The Divide, Conquer and Combine
Steps in Quicksort
• Divide: Partition array A[l..r] into 2 subarrays, A[l..s-1]
and A[s+1..r] such that each element of the first array is
≤A[s] and each element of the second array is ≥ A[s].
(computing the index of s is part of partition.)
– Implication: A[s] will be in its final position in the sorted array.
• Conquer: Sort the two subarrays A[l..s-1] and A[s+1..r]
by recursive calls to quicksort
• Combine: No work is needed, because A[s] is already in
its correct place after the partition is done, and the two
subarrays have been sorted.
QuicksortAmity School of Engineering & Technology
A[i]p A[i]p
ALGORITHM Quicksort(A[l..r])
//Sorts a subarray by quicksort
//Input: A subarray A[l..r] of A[0..n-1],defined by its
left and right indices l and r
//Output: The subarray A[l..r] sorted in
nondecreasing order
if l < r
s Partition (A[l..r]) // s is a split position
Quicksort(A[l..s-1])
Quicksort(A[s+1..r]
Partitioning Algorithm
Amity School of Engineering & Technology
Can ‘=’ be
removed from ‘’
and ‘’?
Number of comparisons: n or n + 1
Amity School of Engineering & Technology
Quicksort Example
15 22 13 27 22 10 20 25
• Improvements:
– better pivot selection: median-of-three
partitioning
– switch to insertion sort on small subfiles
– elimination of recursion
These combine to 20-25% improvement
Amity School of Engineering & Technology
Analyzing Quicksort
2 Knapsack Problems
1. 0-1 Knapsack Problem:
A thief robbing a store finds n items.
ith item: worth vi dollars
wi pounds
W, wi, vi are integers.
He can carry at most W pounds.
Which items
should I take?
2 Knapsack Problems Amity School of Engineering & Technology
(1) take 1 gold ingot + the most valuable way to fill W-8 pounds from 29
gold ingots, 20 silver ingots and 50 copper ingots
(2) take 1 silver ingot + the most valuable way to fill W-3 pounds from 30
gold ingots, 19 silver ingots and 50 copper ingots
(3) take 1 copper ingot + the most valuable way to fill W-5 pounds from 30
gold ingots, 20 silver ingots and 49 copper ingots
2 Knapsack Problems Amity School of Engineering & Technology
(1) take 1 pound of gold + the most valuable way to fill W-1 pounds from 29
pounds of gold, 20 pounds of silver, 50 pounds of copper
(2) take 1 pound of silver + the most valuable way to fill W-1 pounds from 30
pounds of gold, 19 pounds of silver, 50 pounds of copper
(3) take 1 pound copper + the most valuable way to fill W-1 pounds from 30
pounds of gold, 20 pounds of silver, 49 pounds of copper
2 Knapsack Problems Amity School of Engineering & Technology
By Greedy Strategy
Both problems are similar. But Fractional Knapsack Problem
can be solved in a greedy strategy.
By Greedy Strategy
Greedy Algorithms
2 techniques for solving optimization problems:
1. Dynamic Programming
2. Greedy Algorithms (“Greedy Strategy”)
3. Demonstrate that
an optimal solution to original problem = A good clue that
greedy choice + an optimal solution to the that a greedy
strategy will solve
subproblem the problem.
Amity School of Engineering & Technology
Huffman Codes
• For compressing data (sequence of characters)
• Widely used
• Very efficient (saving 20-90%)
• Use a table to keep frequencies of occurrence of
characters.
• Output binary string.
How do we find the optimal Huffman code (1952) was invented to solve it.
prefix code? A Greedy Approach.
25 30 a:45 55
c:12 b:13 14 d:16
25 30
f:5 e:9 c:12 b:13 14 d:16
b:13 c:12 14 d:16
f:5 e:9
e:9 f:5
Huffman Codes Amity School of Engineering & Technology
….
Greedy Algorithms
Summary
Casual Introduction: Two Knapsack Problems
An Activity-Selection Problem
Greedy Algorithm Design
Steps of Greedy Algorithm Design
Optimal Substructure Property
Greedy-Choice Property
Comparison with Dynamic Programming
Huffman Codes
Amity School of Engineering & Technology
00 010 011 10 11
a b c d e
a d e
• Given a text string X, we want to find a prefix code for the characters of
X that yields a small encoding for X
– Frequent characters should have long code-words
– Rare characters should have short code-words
• Example
– X = abracadabra
– T1 encodes X into 29 bits
– T2 encodes X into 24 bits
T1 T2
c d b a b r
a r c d
Greedy Method and Compression 170
Huffman’s Algorithm Amity School of Engineering & Technology
Algorithm HuffmanEncoding(X)
• Given a string X, Input string X of size n
Huffman’s algorithm Output optimal encoding trie for X
construct a prefix code
the minimizes the size C distinctCharacters(X)
of the encoding of X computeFrequencies(C, X)
• It runs in time Q new empty heap
O(nd log d), where n for all c C
is the size of X and d is T new single-node tree storing
the number of distinct c
characters of X Q.insert(getFrequency(c), T)
• A heap-based priority while Q.size() > 1
queue is used as an
f1 Q.minKey()
auxiliary structure
T1 Q.removeMin()
f2 Q.minKey()
T2 Q.removeMin()
T join(T1, T2)
Q.insert(f + f2, T)
Greedy Method and Compression 1 171
return Q.removeMin()
Example Amity School of Engineering & Technology
11
X = abracadabra a 6
Frequencies 2 4
a b c d r c d b r
5 2 1 1 2
6
2 4
a b c d r
5 2 1 1 2 a c d b r
5
2 2 4
a b c d r a c d b r
5 2 2 5
Greedy Method and Compression 172
Amity School of Engineering & Technology
– Objective: maximize
b (x / w )
iS
i i i
– Constraint:
x i W
iS Method and Compression
Greedy 174
Amity School of Engineering & Technology
Example
• Given: A set S of n items, with each item i having
– bi - a positive benefit
– wi - a positive weight
• Goal: Choose items with maximum total benefit but with
weight at most W.
“knapsack”
Solution:
• 1 ml of 5
Items: • 2 ml of 3
1 2 3 4 5
• 6 ml of 4
Weight: • 1 ml of 2
4 ml 8 ml 2 ml 6 ml 1 ml
Benefit: $12 $32 $40 $30 $50 10 ml
Value: 3 4 20 5 50
($ per ml) Greedy Method and Compression 175
The Fractional Knapsack Amity School of Engineering & Technology
Algorithm
• Greedy choice: Keep taking
item with highest value Algorithm fractionalKnapsack(S, W)
(benefit to weight ratio) Input: set S of items w/ benefit bi
– Since bi ( xi / wi ) (bi / wi ) xi and weight wi; max. weight
iS iS W Output: amount xi of each
– Run time: O(n log n). Why?
item i to maximize
• Correctness: Suppose there benefit w/ weight at most
is a better solution W
for each item i in S
– there is an item i with higher value
than a chosen item j, but xi<wi, xi 0
xj>0 and vi<vj vi bi / wi
– If we substitute some i with j, we {value}
get a better solution w
– How much of i: min{wi-xi, xj} 0
{total weight}
– Thus, there is no better solution
than the greedy one while w < W
remove item i w/ highest vi
Greedy Method and Compression 176
xi min{wi , W - w}
w w + min{wi , W - w}
Task Scheduling (not Amity School of Engineering & Technology
in book)
• Given: a set T of n tasks, each having:
– A start time, si
– A finish time, fi (where si < fi)
• Goal: Perform all the tasks using a minimum number of
“machines.”
Machine 3
Machine 2
Machine 1
1 2 3 4 5 6 7 8 9
Algorithm
• Greedy choice: consider tasks by
their start time and use as few
machines as possible with this
order. Algorithm taskSchedule(T)
– Run time: O(n log n). Why? Input: set T of tasks w/ start time si
and finish time fi
• Correctness: Suppose there is a
better schedule. Output: non-conflicting schedule
with minimum number of
– We can use k-1 machines
machines
– The algorithm uses k
m0
– Let i be first task scheduled on {no. of machines}
machine k
while T is not empty
– Machine i must conflict with k-1
remove task i w/ smallest si
other tasks
if there’s a machine j for i
– But that means there is no non-
then
conflicting schedule using k-1
machines schedule i on
machine j
Greedy Method and Compression else 178
mm+1
Example Amity School of Engineering & Technology
Machine 3
Machine 2
Machine 1
1 2 3 4 5 6 7 8 9
Algorithm
• Greedy choice: Keep taking
item with highest value Algorithm fractionalKnapsack(S, W)
(benefit to weight ratio) Input: set S of items w/ benefit bi
– Since bi ( xi / wi ) (bi / wi ) xi and weight wi; max. weight
iS iS W Output: amount xi of each
– Run time: O(n log n). Why?
item i to maximize
• Correctness: Suppose there benefit w/ weight at most
is a better solution W
for each item i in S
– there is an item i with higher value
than a chosen item j, but xi<wi, xi 0
xj>0 and vi<vj vi bi / wi
– If we substitute some i with j, we {value}
get a better solution w
– How much of i: min{wi-xi, xj} 0
{total weight}
– Thus, there is no better solution
than the greedy one while w < W
remove item i w/ highest vi
Greedy Method and Compression 180
xi min{wi , W - w}
w w + min{wi , W - w}
Amity School of Engineering & Technology
8 10 14
1 3 5 7
7 4 12 6 3
2
2 4 6 8
9
• Grow the tree one edge at a time until the tree has n - 1 edges (and hence
has all n vertices).
Amity School of Engineering & Technology
Sollin’s Method
8 1 14
1 3 5 7 1 3 5 7
0
7 12 6 3 4 6 3
2 4 2
2 4 6 8 2 4 6 8
9
• Each component that remains selects a least cost edge with which to
connect to another component.
• Beware of duplicate selections and cycles.
Greedy Minimum-Cost Spanning Tree Methods
Amity School of Engineering & Technology
Backtracking
General Concepts Amity School of Engineering & Technology
• Algorithm strategy
– Approach to solving a problem
– May combine several approaches
• Algorithm structure
– Iterative execute action in loop
– Recursive reapply action to subproblem(s)
• Problem type
– Satisfying find any satisfactory solution
– Optimization find best solutions (vs. cost metric)
Amity School of Engineering & Technology
A short list of categories
Start
Success!
Success!
Failure
Problem space consists of states (nodes) and actions
(paths that lead to new states). When in a node can
can only see paths to connected nodes
Problem
Amity School of Engineering & Technology
The backtracking algorithm
• 2-color
• 3-color
a
• Hamiltonian Circuit
(use alphabet order to
break the ties) c d
b e
Coloring a map Amity School of Engineering & Technology
• 8 Queens *
• Knight's Tour
• Knapsack problem / Exhaustive Search
– Filling a knapsack. Given a choice of items
with various weights and a limited carrying
capacity find the optimal load out. 50 lb.
knapsack. items are 1 40 lb, 1 32 lb. 2 22
lbs, 1 15 lb, 1 5 lb. A greedy algorithm
would choose the 40 lb item first. Then the
5 lb. Load out = 45lb. Exhaustive search
22 + 22 + 5 = 49.
Amity School of Engineering & Technology
Graph Coloring
A
• As an example:
– The vertices are B F
enumerated in order A-F
– The colors are given in
C E
order: R, G, B
D
Amity School of Engineering & Technology
Depth-First Search
A B C D E
A B C D E
A B C D E
Amity School of Engineering & Technology
Brute Force Approaches
• Input
– Capacity K
– n items with weights wi and values vi
• Goal
– Output a set of items S such that
• the sum of weights of items in S is at most K
• and the sum of values of items in S is maximized
Amity School of Engineering & Technology
Greedy Does Not Work
Bounding Function 1
• Because a tour must leave every vertex exactly once,
a lower bound on the length of a tour is b (lower
bound) minimum cost of leaving every vertex.
– The lower bound on the cost of leaving vertex v1 is
given by the minimum of all the nonzero entries in
row 1 of the adjacency matrix.
–…
– The lower bound on the cost of leaving vertex vn is
given by the minimum of all the nonzero entries in
row n of the adjacency matrix.
• Note: This is not to say that there is a tour with this
length. Rather, it says that there can be no shorter
tour.
• Assume that the tour starts with v1.
Traveling Salesman Problem—
Amity School of Engineering & Technology
Bounding Function 2
• Because every vertex must be entered and exited
exactly once, a lower bound on the length of a tour is
the sum of the minimum cost of entering and
leaving every vertex.
– For a given edge (u, v), think of half of its weight as
the exiting cost of u, and half of its weight as the
entering cost of v.
– The total length of a tour = the total cost of visiting(
entering and exiting) every vertex exactly once.
– The lower bound of the length of a tour = the lower
bound of the total cost of visiting (entering and
exiting ) every vertex exactly once.
• Calculation:
– for each vertex, pick top two shortest adjacent edges
(their sum divided by 2 is the lower bound of the total
cost of entering and exiting the vertex);
– add up these summations for all the vertices.
• Assume that the tour starts with vertex a and that b is
visited before c.
Traveling salesman example
Amity School 2Technology
of Engineering &
Details to fill in
Amity School of Engineering & Technology
Exercises- BackTracking
• Continue the backtracking search for a
solution to the four-queens problem to find
the second solution to the problem.
• n = 5, c = 8, w = [4,3,5,6,2], p = [9,7,10,9,3]
0 1 2 3 4 5 6 7 8
f[i][y]
5
4 i
3
y
Amity School of Engineering & Technology