Professional Documents
Culture Documents
2
3 1
4
2
B
3
Length(ABCDEA) = 2 + 3 + 6 + 2 + 5 = 18
Length(ACBDEA) = 3 + 3 + 2 + 2 + 5 = 15
2
D
E.G.M. Petrakis
starting point
3 1
2
6
B
3
(n-1)! paths
Complexity: (2n)
E.G.M. Petrakis
Non-deterministic polynomial:a
polynomial algorithm doesnt
guarantee optimality
The polynomial algorithm is non
2
E.G.M. Petrakis
2
D
starting point
3 1
2
B
2
6
Length(ABEDCA) =
2 + 1 + 2 + 6 + 3 = 14
Non optimal
E.G.M. Petrakis
n! paths
HP is NP Complete
It is easy to transform HP to TSP in
polynomial time
Create a full graph G having cost 1 in
edges that exist in G and cost 2 in edges
that dont belong to G
E.G.M. Petrakis
2 G
1 1
1
1
1
2
1
2
2
E.G.M. Petrakis
Algorithms
Types of Heuristic algorithms
Greedy
Local search
Types of Optimal algorithms
Exhaustive Search (ES)
Divide and Conquer (D&C)
Branch and Bound (B&B)
Dynamic Programming (DP)
E.G.M. Petrakis
10
2
D
3 1
2
6
starting
B point
3
Greedy algorithm:
Length(BECADB) =
1 + 2 + 3 + 4 + 2 = 12
Non optimal
Optimal: Length(BACEDB) = 2 + 3 + 2 + 2 + 2 = 11
E.G.M. Petrakis
11
E.G.M. Petrakis
12
E.G.M. Petrakis
13
2
D
2
3 1
2
6
B
3
MST
|V|=5
2
B
Cost=7
14
Prims algorithm:
graph G=(V,E),V={1,2,,n}
function Prim(G:graph, MST: set of edges)
U: set of edges; u, v: vertices;
{
T = 0; U = {1};
while (U != V) {
(u,v) = min. cost edge: u in U, v in V
T = T + {(u,v)};
U = U + {v};
}
}
Complexity: O(n2) why??
The tree contains n 1 edges
E.G.M. Petrakis
15
1
5
1
2 5 3 5 4
4 2
3 6
5 6 6
6
G = (V,E)
1
Step 2 1
3
1
Step 1 1
3
1
1
Step 3
3
4
6
1
1
5
3
Step 4
4
4
6
E.G.M. Petrakis
2
3
1
1
5
3
5
4
4
6
Step 5
4
4
6
2
16
Local Search
Heuristic algorithms that improve a non-optimal
solution
Local transformation that improves a solution
E.g., a solution obtained by a greedy algorithm
Apply many times and as long as the solution
improves
Apply in difficult (NP problems) like TSP
E.g., apply a greedy algorithm to obtain a solution,
improve this solution using local search
E.G.M. Petrakis
17
greedy
2
3 1
64
cost=12
starting
point
2
1
D
local cost = 5
2
D
D
local cost = 4
2
B
E.G.M. Petrakis
B
B
18
E.G.M. Petrakis
19
Merge sort
list mergesort(list L, int n)
{
if (n == 1) return (L);
L1 = lower half of L;
L2 = upper half of L;
return merge (mergesort(L1,n/2),
mergesort(L2,n/2) );
}
n: size of array L (assume L some power of 2)
merge: merges the sorted L1, L2 in a sorted array
E.G.M. Petrakis
20
Complexity: O(nlogn)
E.G.M. Petrakis
21
Dynamic Programming
The original problem is split into smaller
sub-problems
Solve the sub-problems
Store their solutions
Reuse these solutions several times when the
E.G.M. Petrakis
22
F (n)
F (1) = 0
F (n) = F (n 1) + F (n 2) if n 2
E.G.M. Petrakis
F(1)
F(n-1)
F(n)
23
0-1 Knapsack
0-1 knapsack problem: given a set of
E.G.M. Petrakis
24
Exhaustive (1)
The obvious method to solve the 0-1
E.G.M. Petrakis
25
Notation
n objects
s1, s2, s3, sn: capacities
v1, v2, v3, . vn: values
C: knapsack capacity
Let 0 <= i <= n and A <= C
V(k,A) : maximum value that can be carried in a
knapsack of capacity A given that we choose its
contents from among the first k objects
V(n,C) : maximum value that can be carried in the
original knapsack when we choose from among all
objects
V(k,A) = 0 if k = 0 or A <= 0 for any k
E.G.M. Petrakis
26
DP Formulation
V(k,A) = max{V(k-1,A), V(k-1,A-sk)+vk}:
E.G.M. Petrakis
27
Exhaustive (2)
If we try to compute V(n,c) by recursive
E.G.M. Petrakis
28
Dynamic Programming
Many of these values are likely to be
E.G.M. Petrakis
29
V(0,0)
V(1,0)
0
V(1,A)
n +1
V(n,C): solution
E.G.M. Petrakis
30
DP on Knapsack
Each partial solution V(k,A) takes constant time to
compute
The entire table is filled with values
The total time to fill the table is proportional to
its size => the complexity of the algorithm is O(nC)
Faster than exhaustive search if C << 2n
Which objects are included?
Keep this information in a second table Xi(k,A)
Xi(k,A) = 1 if object i is included, 0 otherwise
E.G.M. Petrakis
31
Shortest Path
find the shortest path from a given node
E.G.M. Petrakis
32
starting point: v = 1
10
100
30
50
10
5
60
4
20
step
D(2)
D(3)
D(4)
D(5)
{1}
10
oo
30
100
{1,2}
10
60
30
100
{1,2,4}
10
50
30
90
{1,2,4,3}
10
50
30
60
{1,2,4,3,5}
10
50
30
60
E.G.M. Petrakis
33
Dijkstras Algorithm
function Dijkstra(G: graph, int v)
{
S = {1};
for i = 2 to n: D[i] = C[i,j];
while (S != V) {
choose w from V-S: D[w] = minimum
S = S + {w};
for each v in VS: D[v] = min{D[v], D[w]+[w,v]}*;
}
* If D[w]+C[w,v] < D[v] then P[v] = w: keep path in array P
Complexity: O(n2)
E.G.M. Petrakis
34
DP on TSP
The standard algorithm is ((n-1)!)
If the same partial paths are used many
E.G.M. Petrakis
35
DP Solution on TSP
TSP needs to compute D(1,{2,n}) and the order of
nodes
DP computes and stores D(b,S) for all bs
36
E.G.M. Petrakis
37
(bounds)
Lower Bound: min. possible value of solution
Upper Bound: max. possible value of solution
The problem is split into sub-problems
Each sub-problems is expanded until a
solution is obtained as long as its cost
doesnt exceed the bounds
Its cost must be greater than the lower bound
and lower than the upper bound
E.G.M. Petrakis
38
Upper-Lower Bounds
The upper bound can be set to oo initially
E.G.M. Petrakis
39
a
3
2
2
a
b
E.G.M. Petrakis
upper bound
by greedy:
length(saefkt)=8
7
3
d
b
2 3
a
2
e
s
2 3
5
s
2 3
3 2
g
2
a
b
3 2 7
9
b
7
9
d
b
f
c
2
e
2
f
40
E.G.M. Petrakis
41