4P03Week7Slides
4P03Week7Slides
5, K&S)
Backtracking algorithms are most useful for generating or enumerating all
solutions.
If we only want one (possibly optimal) solution, then backtracking may not be
efficient:
We may “waste” a lot of time before even one solution is found
To verify that a solution is optimal may require looking at a lot of the search tree.
(Note: if we need to verify it’s optimal, then we don’t have a choice)
Sometimes it’s sufficient to find a feasible solution that is “good” (nearly
optimal).
→ don’t want to waste time looking at the entire tree
→ heuristic algorithms may be more suitable here.
COSC 4P03 WEEK 7 1
GENERIC OPTIMIZATION PROBLEM
Generally:
Take a given solution or partial solution, then apply some
modification to it to obtain a new solution/partial solution.
Given:
A finite set X
An objective function (profit) P(x) for x in X
Feasibility functions (constraints) gj(x) I ≤ j ≤ m
Find:
The maximum value of P(x) such that gj(x) ≥ 0 for I ≤ j ≤ m
COSC 4P03 WEEK 7 2
NEIGHBOURHOOD FUNCTIONS
In constructing a heuristic we use a neighbourhood function N(x) that defines
when an element is “close to” element x.
Example:
Nd(x) = {y in X: dist(x,y) ≤ d}
𝑛
Then the neighbourhood has size |Nd(x)| = σ𝑑
𝑖=0 𝑖
How can we find feasible solutions in the neighbourhood of a given feasible
solution?
Exhaustive search
Randomized search (usually faster)
Output:
Another feasible solution, or
“Fail”, indicating we did not find a feasible solution in the neighbourhood.
COSC 4P03 WEEK 7 3
NEIGHBOURHOOD SEARCH
STRATEGIES – SEE CHAPTER 5, KS
Given a feasible solution x, with neighbourhood N(x), and a profit function P(x):
Find a feasible solution y in N(x) such that P(y) is maximized; return “fail” if no
feasible solutions exist.
Find a feasible solution y in N(x) such that P(y) is maximized; if P(y) > P(x)
then return y, else return “fail”. (“steepest ascent”)
Find any feasible solution y in N(x)
Find any feasible solution y in N(x); if P(y) > P(x) then return y, else return
“fail”.
In general a heuristic hN for improving our current solution x could be:
A single neighbourhood search, or
A sequence of neighbourhood searches – each successive solution is obtained from the
previous solution by a neighbourhood search.
COSC 4P03 WEEK 7 4
GENERIC HEURISTIC SEARCH – SEE
SECTION 5.1, KS
c = 0;
select a feasible solution X;
Xbest = X;
while(c <= cmax)
{
Y = hN(X); //using chosen search strategy
if(Y != fail)
{
X = Y;
if(P(X) > P(Xbest))
Xbest = X;
}
c++;
}
return Xbest;
COSC 4P03 WEEK 7 5
UNIFORM GRAPH PARTITION (SECTION 5.1.1, KS)
Given:
A weighted graph G = (V,E) on 2n vertices (for some n)
Find the minimum cost of a partition [X0, X1] of G, where
V = X0 U X1,
|X0| = |X1| = n,
cost([X0, X1]) = Σ (weight(u,v)) where (u,v) E and u X0
and v X1 (the cost of all edges “crossing” the partition)
COSC 4P03 WEEK 7 6
UNIFORM GRAPH PARTITION (UGP)
EXAMPLE
X0 = {0,2,5,7}, X1 = {1,3,4,6} 0 9 2
Cost of this partition =
weight(1,2) + weight(2,4) + 8
8 2
weight(2,6) + weight(3,5) 2
= 8 + 7 + 2 + 4 = 21. 5 9
7 7 6 1
The set of all possible solutions is 4 6
{[X0, X1] such that |X0| = |X1| = n} 7
Neighbourhood of a given partition
[X0, X1]: set of partitions in which one 3 9 4
element in X0 has been swapped with
one element in X1.
Any design strategy for a heuristic algorithm involves decisions about how to deal
with result of a neighbourhood search.
We’re going to look at a simple hill-climber, just to give an overview.
Others in the book: simulated annealing, tabu search and genetic algorithms
Hill-Climbing:
Given initial solution X, perform an exhaustive neighbourhood search to find Y in
N(X).
We must have P(Y)>P(X) for any Y in N(X) returned by the search algorithm.
No such Y exists → search algorithm must return “fail”.
This strategy tends to find local optimal solutions rather than a global optimal
solution. It’s very dependent on the initial solution.
SelectPartition()
{
r = random(0, (2n choose n) – 1);
X0 = KSubsetLexUnrank(r, n, 2n);
//note change from book
X1 = V – X0;
}
COSC 4P03 WEEK 7 11
UGP NEIGHBOURHOOD SEARCH
Ascend([X0, X1])
{
g = 0; // gain
for each i in X0
{ for each j in X1
{ t = gain([X0, X1], i, j);
if(t > g) // current best gain
{ x = i; y = j; g = t; }
} }
if g > 0 // improved
{ Y0 = (X0 U {y}) – {x};
Y1 = (X1 U {x}) – {y};
fail = false;
return ([Y0, Y1]);
}
else // no improvement
{ fail = true;
return ([X0, X1]);
} }
COSC 4P03 WEEK 7 12
HILL CLIMBING FOR UGP
UGP(cmax)
{
[X0, X1] = SelectPartition();
for(c = 0; c < cmax; c++)
{
[Y0, Y1] = Ascend([X0, X1]);
if(!fail)
// use new partition and try again
{
X0 = Y0;
X1 = Y1;
}
else return; // couldn’t improve
}
}
COSC 4P03 WEEK 7 13
CRYPTOGRAPHY
a b c d e f g h i j k l m n o p q r s t u v w x y z
X N Y A H P O G Z Q WB T S F L R C V MU E K J D I
Key = 1 2 3 4 5
3 1 5 2 4
Inverse Permutation:
1 2 3 4 5
2 4 1 5 3
Plaintext: “meetatnoon”
Corresponding ciphertext: “EMAETOTNNO”
K=8
Plaintext: r e n d e z v o u s
17 4 13 3 4 25 21 14 20 18
Keystream: 8 17 4 13 3 4 25 21 14 20
Addition: 25 21 17 16 7 3 20 9 8 12
Ciphertext: Z V R Q H D U J I M
COSC 4P03 WEEK 7 29
CRYPTANALYSIS
Cryptanalysis is the process Oscar (the opponent) uses to find the key
Kerckhoff’s Principle: Oscar knows the cryptosystem being used
Allows us to evaluate the security of the cryptosystem itself, rather than
relying on Oscar not knowing which cryptosystem is used.
Types of attacks on cryptosystems:
Ciphertext-only: Oscar has a string of ciphertext
Known plaintext: Oscar has a string of plaintext and its corresponding
ciphertext
Chosen plaintext: Oscar chooses a string of plaintext and is able to construct
the corresponding ciphertext
Chosen ciphertext: Oscar chooses a string of ciphertext, and is able to
construct the corresponding plaintext
COSC 4P03 WEEK 7 30
STATISTICAL INFORMATION
YIFQFMZRWQFYVECFMDZPCVMRZWNMDZVEJBTXCDDUMJ
NDIFEFMDZCDMQZKCEYFCJMYRNCWJCSZREXCHZUNMXZ
NZUCDRJXYYSMRTMEYIFZWDYVZVYFZUMRZCRWNZDZJJ
XZWGCHSMRNMDHNCMFQCHZJMXJZWIEJYUCFWDJNZDIR
------end---------e----ned---e------------
YIFQFMZRWQFYVECFMDZPCVMRZWNMDZVEJBTXCDDUMJ
--------e----e---------n--d---en----e----e
NDIFEFMDZCDMQZKCEYFCJMYRNCWJCSZREXCHZUNMXZ
-e---n------n------ed---e---e--ne-nd-e-e--
NZUCDRJXYYSMRTMEYIFZWDYVZVYFZUMRZCRWNZDZJJ
-ed-----n-----------e----ed-------d---e--n
XZWGCHSMRNMDHNCMFQCHZJMXJZWIEJYUCFWDJNZDIR
COSC 4P03 WEEK 7 35
GUESS N=H, C=A
------end-----a---e-a--nedh--e------a-----
YIFQFMZRWQFYVECFMDZPCVMRZWNMDZVEJBTXCDDUMJ
h-------ea---e-a---a---nhad-a-en--a-e-h--e
NDIFEFMDZCDMQZKCEYFCJMYRNCWJCSZREXCHZUNMXZ
he-a-n------n------ed---e---e--neandhe-e--
NZUCDRJXYYSMRTMEYIFZWDYVZVYFZUMRZCRWNZDZJJ
-ed-a---nh---ha---a-e----ed-----a-d--he--n
XZWGCHSMRNMDHNCMFQCHZJMXJZWIEJYUCFWDJNZDIR
COSC 4P03 WEEK 7 36
GUESS M=I
-----iend-----a-i-e-a-inedhi-e------a---i-
YIFQFMZRWQFYVECFMDZPCVMRZWNMDZVEJBTXCDDUMJ
h-----i-ea-i-e-a---a-i-nhad-a-en--a-e-hi-e
NDIFEFMDZCDMQZKCEYFCJMYRNCWJCSZREXCHZUNMXZ
he-a-n-----in-i----ed---e---e-ineandhe-e--
NZUCDRJXYYSMRTMEYIFZWDYVZVYFZUMRZCRWNZDZJJ
-ed-a--inhi--hai--a-e-i--ed-----a-d--he--n
XZWGCHSMRNMDHNCMFQCHZJMXJZWIEJYUCFWDJNZDIR
COSC 4P03 WEEK 7 37
GUESS Y=O, D=S, F=R, H=C, J=T
o-r-riend-ro--arise-a-inedhise--t---ass-it
YIFQFMZRWQFYVECFMDZPCVMRZWNMDZVEJBTXCDDUMJ
hs-r-riseasi-e-a-orationhadta-en--ace-hi-e
NDIFEFMDZCDMQZKCEYFCJMYRNCWJCSZREXCHZUNMXZ
he-asnt-oo-in-i-o-redso-e-ore-ineandhesett
NZUCDRJXYYSMRTMEYIFZWDYVZVYFZUMRZCRWNZDZJJ
-ed-ac-inhischair-aceti-ted--to-ardsthes-n
XZWGCHSMRNMDHNCMFQCHZJMXJZWIEJYUCFWDJNZDIR
COSC 4P03 WEEK 7 38
SUBSTITUTION CIPHER – PLAINTEXT
“Our friend from Paris examined his empty glass with surprise, as if
evaporation had taken place while he wasn’t looking. I poured some
more wine and he settled back in his chair, face tilted up towards the
sun”