Algorithms and Complexity: The Sphere of Algorithmic Problems
Algorithms and Complexity: The Sphere of Algorithmic Problems
Traveling Salesman Problem (TSP): find the shortest route that passes though each of the nodes in a graph exactly once and returns to the starting node starting point
5 2 3 1 4 2 2 D 6 2 C B 3
Exhaustive search: compute all paths and select the the one with the minimum cost
Length(ABCDEA) = 2 + 3 + 6 + 2 + 5 = 18 Length(ACBDEA) = 3 + 3 + 2 + 2 + 5 = 15 Length(ABDECA) = 2 + 2 + 2 + 2 + 3 = 11 Length(AEBCDA) = 5 + 1 + 3 + 6 + 4 = 19
5 2 2 D
E.G.M. Petrakis
BEST PATH
starting point
2 B 3
3 1
2 6 C
Hard Problems: an exponential algorithm that solves the problem is known to exist E.g., TSP Is there a better algorithm? Until when do we try to find a better algorithm? Prove that the problem as at least as hard as another hard problem for which no better solution has even been found Then, stop searching for a better solution for the first problem
E.G.M. Petrakis Algorithms and Complexity 4
NP Complete problems (NPC): hard problems for which only exponential algorithms are known to exist
Polynomial solutions might exist but none has found yet! Examples:
Traveling Salesman Problem (TSP) Hamiltonian Path Problem 0-1 Knapsack Problem Non deterministic polynomial Completeness
Algorithms and Complexity
Properties of NP Completeness:
E.G.M. Petrakis
starting point
3 1 2 B 3 2
2 D
Completeness: if an optimal algorithm exists for one of the them, then a polynomial algorithm can be found for all
It is possible to transform each problem to another using a polynomial algorithm The complexity for solving a different problem is the complexity of transforming the problem to the original one (takes polynomial time) plus the complexity of solving the original problem (take polynomial time again)!
Algorithms and Complexity
E.G.M. Petrakis
Hamiltonian Path (HP) problem: is there a path that visits each node of a graph exactly once?
The exhaustive search algorithm checks 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 Algorithms and Complexity 8
2 G 1 1 1 1 1 2 1 2 2
Algorithms
Types of Heuristic algorithms Types of Optimal algorithms
Exhaustive Search (ES) Divide and Conquer (D&C) Branch and Bound (B&B) Dynamic Programming (DP)
Algorithms and Complexity
E.G.M. Petrakis
10
Greedy Algorithms: whenever they make a selection they choose to increase the cost by the minimum amount
E.g., TSP: the next node is the one closer to the current node
5 2 2 D 4 3 1 2
starting B point
3
2 6 C
Optimal: Length(BACEDB) = 2 + 3 + 2 + 2 + 2 = 11
E.G.M. Petrakis Algorithms and Complexity 11
Job-scheduling: which is the service order of n customers that minimizes the average waiting time?
Serve customers with the less service times first
5 2 2 D
2 3 1 2 6 C
B 3
MST |V|=5
2 D
1 2
2 B
Cost=7
C
E.G.M. Petrakis
Prims algorithm: at each step take the minimum cost edge and connect it to the tree Kruskals algorithm: at each step take the minimum cost edge that creates no cycles in the tree. Both algorithms are optimal!!
Algorithms and Complexity
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 Algorithms and Complexity 15
1 5 1 2 5 3 5 4 4 2 3 6 5 6 6 6 1 Step 1 1 3 1 1 5 3 Step 4 4 4 6
E.G.M. Petrakis
G = (V,E)
1 Step 2 1 3
4 6 2 3
1 1 Step 3 3 1 1 5 3 5
4 4 2
6 Step 5 4 4 6 2
16
Local Search
Heuristic algorithms that improve a non-optimal solution Local transformation that improves a solution Apply many times and as long as the solution improves Apply in difficult (NP problems) like TSP
E.g., a solution obtained by a greedy algorithm
E.g., apply a greedy algorithm to obtain a solution, improve this solution using local search
greedy
B 3
B B
64
starting point
cost=12
D 2 C
2 3 C B 2
D local cost = 5 2
D local cost = 4
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 Algorithms and Complexity 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 same partial result is needed more than once in solving the main problem DP is often based on a recursive formula for solving larger problems in terms of smaller Similar to Divide and Conquer but recursion in D&C doesnt reuse partial solutions
Algorithms and Complexity 22
E.G.M. Petrakis
F (n)
F (1) = 0 F (n) = F (n 1) + F (n 2) if n 2
E.G.M. Petrakis
23
0-1 Knapsack
0-1 knapsack problem: given a set of objects that vary in size and value, what is maximum value that can be carried in a knapsack of capacity C ?? How should we fill-up the capacity in order to achieve the maximum value?
E.G.M. Petrakis
24
Exhaustive (1)
The obvious method to solve the 0-1 knapsack problem is by trying all possible combinations of n objects
0 1 2 n 0 1 1 0
Each object corresponds to a cell If it is included its cell becomes 1 If it is left out its cell becomes 0 There are 2n different combinations
E.G.M. Petrakis Algorithms and Complexity 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 Algorithms and Complexity 26
DP Formulation
V(k,A) = max{V(k-1,A), V(k-1,A-sk)+vk}: solving the knapsack problem for the next object k, there are two choices:
We can include it or leave it out If it is left out, we can do better by choosing from among the previous k-1 objects If it is included, its value vk is added but the capacity of the knapsack in reduced by the capacity sk of the k-th object
E.G.M. Petrakis Algorithms and Complexity 27
Exhaustive (2)
If we try to compute V(n,c) by recursive substitution the complexity is (2n) Two values of V(n-1,A) must be determined for different A, Four values of V(n-2,A) and so on Number of partial solutions: number of nodes in a full binary tree of depth n: (2n)
Dynamic Programming
Many of these values are likely to be the same especially when C is small compared with 2n DP computes and stores these values in a table of n+1 rows and C columns
E.G.M. Petrakis
29
Dynamic Programming (DP) : compute and store V(0,A), V(1,), V(k,A), V(n,C) where 0 <= k <= n and 0 <= A <= C using V(k,A) = max{V(k-1,A), V(k1,A-sk)+vk}:
V(0,0) V(1,0) 0 V(1,A) C
n +1
V(n,C): solution
E.G.M. Petrakis Algorithms and Complexity 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
E.G.M. Petrakis
31
Shortest Path
find the shortest path from a given node to every other node in a graph G No better algorithm for single ending node Notation:
G = (V,E) : input graph C[i,j] : distance between nodes i, j V : starting node S : set of nodes for which the shortest path from v has been computed D(W) : length of shortest path from v to w passing through nodes in S
Algorithms and Complexity
E.G.M. Petrakis
32
1 30 10
starting point: v = 1
100 5 60 4 20
W 2 4 3 5
D(2) 10 10 10 10 10
D(3) oo 60 50 50 50
D(4) 30 30 30 30 30
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 Algorithms and Complexity 34
DP on TSP
The standard algorithm is ((n-1)!) If the same partial paths are used many times then DP can be faster! Notation:
Number the cities 1,2,n, 1 is the starting-ending point d(i,j) : distance from node i to j D(b,S) : length of shortest path that starts at b visits all cities in S in some order and ends at node 1 S is subset of {1,2,n} {1,b}
Algorithms and Complexity 35
E.G.M. Petrakis
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
aS
Its cost must be greater than the lower bound and lower than the upper bound
Algorithms and Complexity 38
E.G.M. Petrakis
Upper-Lower Bounds
The upper bound can be set to oo initially
Takes the cost of the first complete solution as soon as one is found A greedy algorithm can provide the initial upper bound (e.g., in TSP, SP etc) It is revised in later steps if better solutions are found Depends on the problem There is a theorem for TSP
E.G.M. Petrakis
39
a
3
7 2 9 2
d e
1
7 2 3 5 1
g h k
4 4 2
b c
4 2
3
a
s 2 3 b 7 4 c 3 b a 2 e
4
s 2 3 b 7 d 9 e 4
s 2 3 a b 3 2 7 9
c
2 e
2
2
s
3 4
b
3 2 e
2 f f
d
9
c
2
b f
a b E.G.M. Petrakis
40
E.G.M. Petrakis
41