Approximation and Randomized Algorithms Lecturer: Shi Li Department - - PowerPoint PPT Presentation

approximation and randomized algorithms
SMART_READER_LITE
LIVE PREVIEW

Approximation and Randomized Algorithms Lecturer: Shi Li Department - - PowerPoint PPT Presentation

CSE 431/531: Analysis of Algorithms Approximation and Randomized Algorithms Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Outline Approximation Algorithms 1 Approximation Algorithms for Traveling


slide-1
SLIDE 1

CSE 431/531: Analysis of Algorithms

Approximation and Randomized Algorithms

Lecturer: Shi Li

Department of Computer Science and Engineering University at Buffalo

slide-2
SLIDE 2

2/58

Outline

1

Approximation Algorithms

2

Approximation Algorithms for Traveling Salesman Problem

3

2-Approximation Algorithm for Vertex Cover

4 7 8-Approximation Algorithm for Max 3-SAT 5

Randomized Quicksort Recap of Quicksort Randomized Quicksort Algorithm

6

2-Approximation Algorithm for (Weighted) Vertex Cover Via Linear Programming Linear Programming 2-Approximation for Weighted Vertex Cover

slide-3
SLIDE 3

3/58

Approximation Algorithms

An algorithm for an optimization problem is an α-approximation algorithm, if it runs in polynomial time, and for any instance to the problem, it outputs a solution whose cost (or value) is within an α-factor of the cost (or value) of the optimum solution.

slide-4
SLIDE 4

3/58

Approximation Algorithms

An algorithm for an optimization problem is an α-approximation algorithm, if it runs in polynomial time, and for any instance to the problem, it outputs a solution whose cost (or value) is within an α-factor of the cost (or value) of the optimum solution.

  • pt: cost (or value) of the optimum solution
slide-5
SLIDE 5

3/58

Approximation Algorithms

An algorithm for an optimization problem is an α-approximation algorithm, if it runs in polynomial time, and for any instance to the problem, it outputs a solution whose cost (or value) is within an α-factor of the cost (or value) of the optimum solution.

  • pt: cost (or value) of the optimum solution

sol: cost (or value) of the solution produced by the algorithm

slide-6
SLIDE 6

3/58

Approximation Algorithms

An algorithm for an optimization problem is an α-approximation algorithm, if it runs in polynomial time, and for any instance to the problem, it outputs a solution whose cost (or value) is within an α-factor of the cost (or value) of the optimum solution.

  • pt: cost (or value) of the optimum solution

sol: cost (or value) of the solution produced by the algorithm α: approximation ratio

slide-7
SLIDE 7

3/58

Approximation Algorithms

An algorithm for an optimization problem is an α-approximation algorithm, if it runs in polynomial time, and for any instance to the problem, it outputs a solution whose cost (or value) is within an α-factor of the cost (or value) of the optimum solution.

  • pt: cost (or value) of the optimum solution

sol: cost (or value) of the solution produced by the algorithm α: approximation ratio For minimization problems:

α ≥ 1 and we require sol ≤ α · opt

slide-8
SLIDE 8

3/58

Approximation Algorithms

An algorithm for an optimization problem is an α-approximation algorithm, if it runs in polynomial time, and for any instance to the problem, it outputs a solution whose cost (or value) is within an α-factor of the cost (or value) of the optimum solution.

  • pt: cost (or value) of the optimum solution

sol: cost (or value) of the solution produced by the algorithm α: approximation ratio For minimization problems:

α ≥ 1 and we require sol ≤ α · opt

For maximization problems, there are two conventions:

α ≤ 1 and we require sol ≥ α · opt α ≥ 1 and we require sol ≥ opt/α

slide-9
SLIDE 9

4/58

Outline

1

Approximation Algorithms

2

Approximation Algorithms for Traveling Salesman Problem

3

2-Approximation Algorithm for Vertex Cover

4 7 8-Approximation Algorithm for Max 3-SAT 5

Randomized Quicksort Recap of Quicksort Randomized Quicksort Algorithm

6

2-Approximation Algorithm for (Weighted) Vertex Cover Via Linear Programming Linear Programming 2-Approximation for Weighted Vertex Cover

slide-10
SLIDE 10

5/58

Recall: Traveling Salesman Problem

A salesman needs to visit n cities 1, 2, 3, · · · , n He needs to start from and return to city 1 Goal: find a tour with the minimum cost

4 6 2 3 1 2 2 3

slide-11
SLIDE 11

5/58

Recall: Traveling Salesman Problem

A salesman needs to visit n cities 1, 2, 3, · · · , n He needs to start from and return to city 1 Goal: find a tour with the minimum cost

4 6 2 3 1 2 2 3

slide-12
SLIDE 12

5/58

Recall: Traveling Salesman Problem

A salesman needs to visit n cities 1, 2, 3, · · · , n He needs to start from and return to city 1 Goal: find a tour with the minimum cost

4 6 2 3 1 2 2 3

slide-13
SLIDE 13

5/58

Recall: Traveling Salesman Problem

A salesman needs to visit n cities 1, 2, 3, · · · , n He needs to start from and return to city 1 Goal: find a tour with the minimum cost

4 6 2 3 1 2 2 3

slide-14
SLIDE 14

5/58

Recall: Traveling Salesman Problem

A salesman needs to visit n cities 1, 2, 3, · · · , n He needs to start from and return to city 1 Goal: find a tour with the minimum cost

4 6 2 3 1 2 2 3

slide-15
SLIDE 15

5/58

Recall: Traveling Salesman Problem

A salesman needs to visit n cities 1, 2, 3, · · · , n He needs to start from and return to city 1 Goal: find a tour with the minimum cost

4 6 2 3 1 2 2 3

slide-16
SLIDE 16

5/58

Recall: Traveling Salesman Problem

A salesman needs to visit n cities 1, 2, 3, · · · , n He needs to start from and return to city 1 Goal: find a tour with the minimum cost

4 6 2 3 1 2 2 3

slide-17
SLIDE 17

5/58

Recall: Traveling Salesman Problem

A salesman needs to visit n cities 1, 2, 3, · · · , n He needs to start from and return to city 1 Goal: find a tour with the minimum cost

4 6 2 3 1 2 2 3

Travelling Salesman Problem (TSP) Input: a graph G = (V, E), weights w : E → R≥0 Output: a traveling-salesman tour with the minimum cost

slide-18
SLIDE 18

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

slide-19
SLIDE 19

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-20
SLIDE 20

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-21
SLIDE 21

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-22
SLIDE 22

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-23
SLIDE 23

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-24
SLIDE 24

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-25
SLIDE 25

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-26
SLIDE 26

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-27
SLIDE 27

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-28
SLIDE 28

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-29
SLIDE 29

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-30
SLIDE 30

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-31
SLIDE 31

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-32
SLIDE 32

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-33
SLIDE 33

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-34
SLIDE 34

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-35
SLIDE 35

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-36
SLIDE 36

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-37
SLIDE 37

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-38
SLIDE 38

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-39
SLIDE 39

6/58

2-Approximation Algorithm for TSP

TSP1(G, w)

1

MST ← the minimum spanning tree of G w.r.t weights w, returned by either Kruskal’s algorithm or Prim’s algorithm.

2

Output tour formed by making two copies of each edge in MST.

a b d h e i c f g j k

slide-40
SLIDE 40

7/58

2-Approximation Algorithm for TSP

Lemma Algorithm TSP1 is a 2-approximation algorithm for TSP. Proof

slide-41
SLIDE 41

7/58

2-Approximation Algorithm for TSP

Lemma Algorithm TSP1 is a 2-approximation algorithm for TSP. Proof mst = cost of the minimum spanning tree

slide-42
SLIDE 42

7/58

2-Approximation Algorithm for TSP

Lemma Algorithm TSP1 is a 2-approximation algorithm for TSP. Proof mst = cost of the minimum spanning tree tsp = cost of the optimum travelling salesman tour

slide-43
SLIDE 43

7/58

2-Approximation Algorithm for TSP

Lemma Algorithm TSP1 is a 2-approximation algorithm for TSP. Proof mst = cost of the minimum spanning tree tsp = cost of the optimum travelling salesman tour then mst ≤ tsp, since removing one edge from the optimum travelling salesman tour results in a spanning tree

slide-44
SLIDE 44

7/58

2-Approximation Algorithm for TSP

Lemma Algorithm TSP1 is a 2-approximation algorithm for TSP. Proof mst = cost of the minimum spanning tree tsp = cost of the optimum travelling salesman tour then mst ≤ tsp, since removing one edge from the optimum travelling salesman tour results in a spanning tree sol = cost of tour given by algorithm TSP1

slide-45
SLIDE 45

7/58

2-Approximation Algorithm for TSP

Lemma Algorithm TSP1 is a 2-approximation algorithm for TSP. Proof mst = cost of the minimum spanning tree tsp = cost of the optimum travelling salesman tour then mst ≤ tsp, since removing one edge from the optimum travelling salesman tour results in a spanning tree sol = cost of tour given by algorithm TSP1 sol = 2 · mst ≤ 2 · tsp.

slide-46
SLIDE 46

8/58

1.5-Approximation for TSP

  • Def. Given G = (V, E), a set U ⊆ V of even number of

vertices in V , a matching M over U in G is a set of |U|/2 paths in G, such that every vertex in U is one end point of some path.

  • Def. The cost of the matching M, denoted as cost(M) is the

total cost of all edges in the |U|/2 paths (counting multiplicities). Theorem Given G = (V, E), a set U ⊆ V of even number of verticies, the minimum cost matching over U in G can be found in polynomial time.

slide-47
SLIDE 47

9/58

1.5-Approximation for TSP

Lemma Let T be a spanning tree of G = (V, E); let U be the set of odd-degree vertices in MST (|U| must be even, why?). Let M be a matching over U, then, T ⊎ M gives a traveling salesman’s tour. Proof. Every vertex in T ⊎ M has even degree and T ⊎ M is connected (since it contains the spanning tree). Thus T ⊎ M is an Eulerian graph and we can find a tour that visits every edge in T ⊎ M exactly once.

slide-48
SLIDE 48

10/58

1.5-Approximation for TSP

Lemma Let U be a set of even number of vertices in G. Then the cost of the cheapest matching over U in G is at most

1 2tsp. points in U

  • ptimum TSP

Proof. Take the optimum TSP

slide-49
SLIDE 49

10/58

1.5-Approximation for TSP

Lemma Let U be a set of even number of vertices in G. Then the cost of the cheapest matching over U in G is at most

1 2tsp. points in U

  • ptimum TSP

Proof. Take the optimum TSP Breaking into read matching and blue matching over U

slide-50
SLIDE 50

10/58

1.5-Approximation for TSP

Lemma Let U be a set of even number of vertices in G. Then the cost of the cheapest matching over U in G is at most

1 2tsp. points in U

  • ptimum TSP

Proof. Take the optimum TSP Breaking into read matching and blue matching over U cost(blue matching)+ cost(red matching) = tsp

slide-51
SLIDE 51

10/58

1.5-Approximation for TSP

Lemma Let U be a set of even number of vertices in G. Then the cost of the cheapest matching over U in G is at most

1 2tsp. points in U

  • ptimum TSP

Proof. Take the optimum TSP Breaking into read matching and blue matching over U cost(blue matching)+ cost(red matching) = tsp Thus, cost(blue matching) ≤ 1

2tsp or

cost(red matching) ≤ 1

2tsp

slide-52
SLIDE 52

10/58

1.5-Approximation for TSP

Lemma Let U be a set of even number of vertices in G. Then the cost of the cheapest matching over U in G is at most

1 2tsp. points in U

  • ptimum TSP

Proof. Take the optimum TSP Breaking into read matching and blue matching over U cost(blue matching)+ cost(red matching) = tsp Thus, cost(blue matching) ≤ 1

2tsp or

cost(red matching) ≤ 1

2tsp

cost(cheapeast matching) ≤ 1

2tsp

slide-53
SLIDE 53

11/58

Outline

1

Approximation Algorithms

2

Approximation Algorithms for Traveling Salesman Problem

3

2-Approximation Algorithm for Vertex Cover

4 7 8-Approximation Algorithm for Max 3-SAT 5

Randomized Quicksort Recap of Quicksort Randomized Quicksort Algorithm

6

2-Approximation Algorithm for (Weighted) Vertex Cover Via Linear Programming Linear Programming 2-Approximation for Weighted Vertex Cover

slide-54
SLIDE 54

12/58

Vertex Cover Problem

  • Def. Given a graph G = (V, E), a vertex cover of G is a subset

S ⊆ V such that for every (u, v) ∈ E then u ∈ S or v ∈ S .

slide-55
SLIDE 55

12/58

Vertex Cover Problem

  • Def. Given a graph G = (V, E), a vertex cover of G is a subset

S ⊆ V such that for every (u, v) ∈ E then u ∈ S or v ∈ S . Vertex-Cover Problem Input: G = (V, E) Output: a vertex cover S with minimum |S|

slide-56
SLIDE 56

13/58

First Try: Greedy Algorithm

Greedy Algorithm for Vertex-Cover

1

E′ ← E, S ← ∅

2

while E′ = ∅

3

let v be the vertex of the maximum degree in (V, E′)

4

S ← S ∪ {v},

5

remove all edges incident to v from E′

6

  • utput S
slide-57
SLIDE 57

13/58

First Try: Greedy Algorithm

Greedy Algorithm for Vertex-Cover

1

E′ ← E, S ← ∅

2

while E′ = ∅

3

let v be the vertex of the maximum degree in (V, E′)

4

S ← S ∪ {v},

5

remove all edges incident to v from E′

6

  • utput S

Theorem Greedy algorithm is an O(lg n)-approximation for vertex-cover.

slide-58
SLIDE 58

13/58

First Try: Greedy Algorithm

Greedy Algorithm for Vertex-Cover

1

E′ ← E, S ← ∅

2

while E′ = ∅

3

let v be the vertex of the maximum degree in (V, E′)

4

S ← S ∪ {v},

5

remove all edges incident to v from E′

6

  • utput S

Theorem Greedy algorithm is an O(lg n)-approximation for vertex-cover. We are not going to prove the theorem

slide-59
SLIDE 59

13/58

First Try: Greedy Algorithm

Greedy Algorithm for Vertex-Cover

1

E′ ← E, S ← ∅

2

while E′ = ∅

3

let v be the vertex of the maximum degree in (V, E′)

4

S ← S ∪ {v},

5

remove all edges incident to v from E′

6

  • utput S

Theorem Greedy algorithm is an O(lg n)-approximation for vertex-cover. We are not going to prove the theorem Instead, we show that the O(lg n)-approximation ratio is tight for the algorithm

slide-60
SLIDE 60

14/58

Bad Example for Greedy Algorithm

|L| = n′

L: n′ vertices

slide-61
SLIDE 61

14/58

Bad Example for Greedy Algorithm

R2 |L| = n′

L: n′ vertices R2: ⌊n′/2⌋ vertices, each connected to 2 vertices in L

slide-62
SLIDE 62

14/58

Bad Example for Greedy Algorithm

R2 R3 |L| = n′

L: n′ vertices R2: ⌊n′/2⌋ vertices, each connected to 2 vertices in L R3: ⌊n′/3⌋ vertices, each connected to 3 vertices in L

slide-63
SLIDE 63

14/58

Bad Example for Greedy Algorithm

R2 R3 R4 |L| = n′

L: n′ vertices R2: ⌊n′/2⌋ vertices, each connected to 2 vertices in L R3: ⌊n′/3⌋ vertices, each connected to 3 vertices in L R4: ⌊n′/4⌋ vertices, each connected to 4 vertices in L

slide-64
SLIDE 64

14/58

Bad Example for Greedy Algorithm

R2 R3 R4 R5 |L| = n′ Rn′

L: n′ vertices R2: ⌊n′/2⌋ vertices, each connected to 2 vertices in L R3: ⌊n′/3⌋ vertices, each connected to 3 vertices in L R4: ⌊n′/4⌋ vertices, each connected to 4 vertices in L · · · Rn′: 1 vertex, connected to n′ vertices in L

slide-65
SLIDE 65

14/58

Bad Example for Greedy Algorithm

R2 R3 R4 R5 |L| = n′ R Rn′

L: n′ vertices R2: ⌊n′/2⌋ vertices, each connected to 2 vertices in L R3: ⌊n′/3⌋ vertices, each connected to 3 vertices in L R4: ⌊n′/4⌋ vertices, each connected to 4 vertices in L · · · Rn′: 1 vertex, connected to n′ vertices in L R = R2 ∪ R3 ∪ · · · ∪ Rn′

slide-66
SLIDE 66

15/58

Bad Example for Greedy Algorithm

R2 R3 R4 R5 |L| = n′ R Rn′

slide-67
SLIDE 67

15/58

Bad Example for Greedy Algorithm

R2 R3 R4 R5 |L| = n′ R Rn′

Optimum solution is L, where |L| = n′

slide-68
SLIDE 68

15/58

Bad Example for Greedy Algorithm

R2 R3 R4 R5 |L| = n′ R Rn′

Optimum solution is L, where |L| = n′ Greedy algorithm picks Rn′, Rn′−1, · · · , R2 in this order

slide-69
SLIDE 69

15/58

Bad Example for Greedy Algorithm

R2 R3 R4 R5 |L| = n′ R Rn′

Optimum solution is L, where |L| = n′ Greedy algorithm picks Rn′, Rn′−1, · · · , R2 in this order Thus, greedy algorithm outputs R

slide-70
SLIDE 70

15/58

Bad Example for Greedy Algorithm

R2 R3 R4 R5 |L| = n′ R Rn′

Optimum solution is L, where |L| = n′ Greedy algorithm picks Rn′, Rn′−1, · · · , R2 in this order Thus, greedy algorithm outputs R |R| =

n

  • i=2

n′ i

n

  • i=1

n′ i − n′ − (n′ − 1) = n′H(n′) − (2n′ − 1) = Ω(n′ lg n′)

slide-71
SLIDE 71

15/58

Bad Example for Greedy Algorithm

R2 R3 R4 R5 |L| = n′ R Rn′

Optimum solution is L, where |L| = n′ Greedy algorithm picks Rn′, Rn′−1, · · · , R2 in this order Thus, greedy algorithm outputs R |R| =

n

  • i=2

n′ i

n

  • i=1

n′ i − n′ − (n′ − 1) = n′H(n′) − (2n′ − 1) = Ω(n′ lg n′) where H(n′) = 1 + 1

2 + 1 3 + · · · + 1 n′ = Θ(lg n′) is the n′-th

number in the harmonic sequence.

slide-72
SLIDE 72

16/58

Bad Example for Greedy Algorithm

R2 R3 R4 R5 |L| = n′ R Rn′

slide-73
SLIDE 73

16/58

Bad Example for Greedy Algorithm

R2 R3 R4 R5 |L| = n′ R Rn′

Let n = |L ∪ R| = Θ(n′ lg n′)

slide-74
SLIDE 74

16/58

Bad Example for Greedy Algorithm

R2 R3 R4 R5 |L| = n′ R Rn′

Let n = |L ∪ R| = Θ(n′ lg n′) Then lg n = Θ(lg n′)

slide-75
SLIDE 75

16/58

Bad Example for Greedy Algorithm

R2 R3 R4 R5 |L| = n′ R Rn′

Let n = |L ∪ R| = Θ(n′ lg n′) Then lg n = Θ(lg n′)

|R| |L| = Ω(n′ lg n′) n′

= Ω(lg n′) = Ω(lg n).

slide-76
SLIDE 76

16/58

Bad Example for Greedy Algorithm

R2 R3 R4 R5 |L| = n′ R Rn′

Let n = |L ∪ R| = Θ(n′ lg n′) Then lg n = Θ(lg n′)

|R| |L| = Ω(n′ lg n′) n′

= Ω(lg n′) = Ω(lg n). Thus, greedy algorithm does not do better than O(lg n).

slide-77
SLIDE 77

17/58

Greedy algorithm is a very natural algorithm, which might be the first algorithm some one can come up with

slide-78
SLIDE 78

17/58

Greedy algorithm is a very natural algorithm, which might be the first algorithm some one can come up with However, the approximation ratio is not so good

slide-79
SLIDE 79

17/58

Greedy algorithm is a very natural algorithm, which might be the first algorithm some one can come up with However, the approximation ratio is not so good We now give a somewhat “counter-intuitive” algorithm,

slide-80
SLIDE 80

17/58

Greedy algorithm is a very natural algorithm, which might be the first algorithm some one can come up with However, the approximation ratio is not so good We now give a somewhat “counter-intuitive” algorithm, for which we can prove a 2-approximation ratio.

slide-81
SLIDE 81

18/58

2-Approximation Algorithm for Vertex Cover

1

E′ ← E, S ← ∅

2

while E′ = ∅

3

let (u, v) be any edge in E′

4

S ← S ∪ {u, v},

5

remove all edges incident to u and v from E′

6

  • utput S
slide-82
SLIDE 82

18/58

2-Approximation Algorithm for Vertex Cover

1

E′ ← E, S ← ∅

2

while E′ = ∅

3

let (u, v) be any edge in E′

4

S ← S ∪ {u, v},

5

remove all edges incident to u and v from E′

6

  • utput S

The counter-intuitive part: adding both u and v to S seems to be wasteful

slide-83
SLIDE 83

18/58

2-Approximation Algorithm for Vertex Cover

1

E′ ← E, S ← ∅

2

while E′ = ∅

3

let (u, v) be any edge in E′

4

S ← S ∪ {u, v},

5

remove all edges incident to u and v from E′

6

  • utput S

The counter-intuitive part: adding both u and v to S seems to be wasteful Intuition for the 2-approximation ratio: the optimum solution must cover the edge (u, v), using either u or v. If we select both, we are always ahead of the optimum solution. The approximation factor we lost is at most 2.

slide-84
SLIDE 84

19/58

2-Approximation Algorithm for Vertex Cover

1

E′ ← E, S ← ∅

2

while E′ = ∅

3

let (u, v) be any edge in E′

4

S ← S ∪ {u, v},

5

remove all edges incident to u and v from E′

6

  • utput S
slide-85
SLIDE 85

19/58

2-Approximation Algorithm for Vertex Cover

1

E′ ← E, S ← ∅

2

while E′ = ∅

3

let (u, v) be any edge in E′

4

S ← S ∪ {u, v},

5

remove all edges incident to u and v from E′

6

  • utput S

Let E∗ be the set of edges (u, v) considered in Statement 3

slide-86
SLIDE 86

19/58

2-Approximation Algorithm for Vertex Cover

1

E′ ← E, S ← ∅

2

while E′ = ∅

3

let (u, v) be any edge in E′

4

S ← S ∪ {u, v},

5

remove all edges incident to u and v from E′

6

  • utput S

Let E∗ be the set of edges (u, v) considered in Statement 3 Observation: E∗ is a matching and |S| = 2|E∗|

slide-87
SLIDE 87

19/58

2-Approximation Algorithm for Vertex Cover

1

E′ ← E, S ← ∅

2

while E′ = ∅

3

let (u, v) be any edge in E′

4

S ← S ∪ {u, v},

5

remove all edges incident to u and v from E′

6

  • utput S

Let E∗ be the set of edges (u, v) considered in Statement 3 Observation: E∗ is a matching and |S| = 2|E∗| To cover all edges in E∗, the optimum solution needs |E∗| vertices

slide-88
SLIDE 88

19/58

2-Approximation Algorithm for Vertex Cover

1

E′ ← E, S ← ∅

2

while E′ = ∅

3

let (u, v) be any edge in E′

4

S ← S ∪ {u, v},

5

remove all edges incident to u and v from E′

6

  • utput S

Let E∗ be the set of edges (u, v) considered in Statement 3 Observation: E∗ is a matching and |S| = 2|E∗| To cover all edges in E∗, the optimum solution needs |E∗| vertices Theorem The algorithm is a 2-approximation algorithm for vertex-cover.

slide-89
SLIDE 89

20/58

Outline

1

Approximation Algorithms

2

Approximation Algorithms for Traveling Salesman Problem

3

2-Approximation Algorithm for Vertex Cover

4 7 8-Approximation Algorithm for Max 3-SAT 5

Randomized Quicksort Recap of Quicksort Randomized Quicksort Algorithm

6

2-Approximation Algorithm for (Weighted) Vertex Cover Via Linear Programming Linear Programming 2-Approximation for Weighted Vertex Cover

slide-90
SLIDE 90

21/58

Max 3-SAT Input: n boolean variables x1, x2, · · · , xn m clauses, each clause is a disjunction of 3 literals from 3 distinct variables Output: an assignment so as to satisfy as many clauses as possible Example: clauses: x2 ∨ ¬x3 ∨ ¬x4, x2 ∨ x3 ∨ ¬x4, ¬x1 ∨ x2 ∨ x4, x1 ∨ ¬x2 ∨ x3, ¬x1 ∨ ¬x2 ∨ ¬x4 We can satisfy all the 5 clauses: x = (1, 1, 1, 0, 1)

slide-91
SLIDE 91

22/58

Randomized Algorithm for Max 3-SAT

Simple idea: randomly set each variable xu = 1 with probability 1/2, independent of other variables

slide-92
SLIDE 92

22/58

Randomized Algorithm for Max 3-SAT

Simple idea: randomly set each variable xu = 1 with probability 1/2, independent of other variables Lemma Let m be the number of clauses. Then, in expectation,

7 8m number of clauses will be satisfied.

slide-93
SLIDE 93

22/58

Randomized Algorithm for Max 3-SAT

Simple idea: randomly set each variable xu = 1 with probability 1/2, independent of other variables Lemma Let m be the number of clauses. Then, in expectation,

7 8m number of clauses will be satisfied.

Proof.

slide-94
SLIDE 94

22/58

Randomized Algorithm for Max 3-SAT

Simple idea: randomly set each variable xu = 1 with probability 1/2, independent of other variables Lemma Let m be the number of clauses. Then, in expectation,

7 8m number of clauses will be satisfied.

Proof. for each clause Cj, let Zj = 1 if Cj is satisfied and 0

  • therwise
slide-95
SLIDE 95

22/58

Randomized Algorithm for Max 3-SAT

Simple idea: randomly set each variable xu = 1 with probability 1/2, independent of other variables Lemma Let m be the number of clauses. Then, in expectation,

7 8m number of clauses will be satisfied.

Proof. for each clause Cj, let Zj = 1 if Cj is satisfied and 0

  • therwise

Z = m

j=1 Zj is the total number of satisfied clauses

slide-96
SLIDE 96

22/58

Randomized Algorithm for Max 3-SAT

Simple idea: randomly set each variable xu = 1 with probability 1/2, independent of other variables Lemma Let m be the number of clauses. Then, in expectation,

7 8m number of clauses will be satisfied.

Proof. for each clause Cj, let Zj = 1 if Cj is satisfied and 0

  • therwise

Z = m

j=1 Zj is the total number of satisfied clauses

E[Zj] = 7/8: out of 8 possible assignments to the 3 variables in Cj, 7 of them will make Cj satisfied

slide-97
SLIDE 97

22/58

Randomized Algorithm for Max 3-SAT

Simple idea: randomly set each variable xu = 1 with probability 1/2, independent of other variables Lemma Let m be the number of clauses. Then, in expectation,

7 8m number of clauses will be satisfied.

Proof. for each clause Cj, let Zj = 1 if Cj is satisfied and 0

  • therwise

Z = m

j=1 Zj is the total number of satisfied clauses

E[Zj] = 7/8: out of 8 possible assignments to the 3 variables in Cj, 7 of them will make Cj satisfied E[Z] = E m

j=1 Zj

  • = m

j=1 E[Zj] = m j=1 7 8 = 7 8m, by

linearity of expectation.

slide-98
SLIDE 98

23/58

Randomized Algorithm for Max 3-SAT

Lemma Let m be the number of clauses. Then, in expectation,

7 8m number of clauses will be satisfied.

slide-99
SLIDE 99

23/58

Randomized Algorithm for Max 3-SAT

Lemma Let m be the number of clauses. Then, in expectation,

7 8m number of clauses will be satisfied.

Since the optimum solution can satisfy at most m clauses, lemma gives a randomized 7/8-approximation for Max-3-SAT.

slide-100
SLIDE 100

23/58

Randomized Algorithm for Max 3-SAT

Lemma Let m be the number of clauses. Then, in expectation,

7 8m number of clauses will be satisfied.

Since the optimum solution can satisfy at most m clauses, lemma gives a randomized 7/8-approximation for Max-3-SAT. Theorem ([Hastad 97]) Unless P = NP, there is no ρ-approximation algorithm for MAX-3-SAT for any ρ > 7/8.

slide-101
SLIDE 101

24/58

Outline

1

Approximation Algorithms

2

Approximation Algorithms for Traveling Salesman Problem

3

2-Approximation Algorithm for Vertex Cover

4 7 8-Approximation Algorithm for Max 3-SAT 5

Randomized Quicksort Recap of Quicksort Randomized Quicksort Algorithm

6

2-Approximation Algorithm for (Weighted) Vertex Cover Via Linear Programming Linear Programming 2-Approximation for Weighted Vertex Cover

slide-102
SLIDE 102

25/58

Outline

1

Approximation Algorithms

2

Approximation Algorithms for Traveling Salesman Problem

3

2-Approximation Algorithm for Vertex Cover

4 7 8-Approximation Algorithm for Max 3-SAT 5

Randomized Quicksort Recap of Quicksort Randomized Quicksort Algorithm

6

2-Approximation Algorithm for (Weighted) Vertex Cover Via Linear Programming Linear Programming 2-Approximation for Weighted Vertex Cover

slide-103
SLIDE 103

26/58

Quicksort vs Merge-Sort

Merge Sort Quicksort Divide Trivial Separate small and big numbers Conquer Recurse Recurse Combine Merge 2 sorted arrays Trivial

slide-104
SLIDE 104

27/58

Quicksort Example

Assumption We can choose median of an array of size n in O(n) time.

15 82 75 69 38 17 94 64 25 76 29 92 37 45 85

slide-105
SLIDE 105

27/58

Quicksort Example

Assumption We can choose median of an array of size n in O(n) time.

15 82 75 69 38 17 94 64 25 76 29 92 37 45 85 64

slide-106
SLIDE 106

27/58

Quicksort Example

Assumption We can choose median of an array of size n in O(n) time.

15 82 75 69 38 17 94 64 25 76 29 92 37 45 85 64 15 82 75 69 38 17 94 25 76 29 92 37 45 85 64

slide-107
SLIDE 107

27/58

Quicksort Example

Assumption We can choose median of an array of size n in O(n) time.

15 82 75 69 38 17 94 64 25 76 29 92 37 45 85 64 15 82 75 69 38 17 94 25 76 29 92 37 45 85 64 29

slide-108
SLIDE 108

27/58

Quicksort Example

Assumption We can choose median of an array of size n in O(n) time.

15 82 75 69 38 17 94 64 25 76 29 92 37 45 85 64 15 82 75 69 38 17 94 25 76 29 92 37 45 85 64 29 15 82 75 69 38 17 94 25 76 92 37 45 85 64 29

slide-109
SLIDE 109

28/58

Quicksort

quicksort(A, n)

1

if n ≤ 1 then return A

2

x ← lower median of A

3

AL ← elements in A that are less than x \\ Divide

4

AR ← elements in A that are greater than x \\ Divide

5

BL ← quicksort(AL, AL.size) \\ Conquer

6

BR ← quicksort(AR, AR.size) \\ Conquer

7

t ← number of times x appear A

8

return the array obtained by concatenating BL, the array containing t copies of x, and BR

slide-110
SLIDE 110

28/58

Quicksort

quicksort(A, n)

1

if n ≤ 1 then return A

2

x ← lower median of A

3

AL ← elements in A that are less than x \\ Divide

4

AR ← elements in A that are greater than x \\ Divide

5

BL ← quicksort(AL, AL.size) \\ Conquer

6

BR ← quicksort(AR, AR.size) \\ Conquer

7

t ← number of times x appear A

8

return the array obtained by concatenating BL, the array containing t copies of x, and BR Recurrence T(n) ≤ 2T(n/2) + O(n)

slide-111
SLIDE 111

28/58

Quicksort

quicksort(A, n)

1

if n ≤ 1 then return A

2

x ← lower median of A

3

AL ← elements in A that are less than x \\ Divide

4

AR ← elements in A that are greater than x \\ Divide

5

BL ← quicksort(AL, AL.size) \\ Conquer

6

BR ← quicksort(AR, AR.size) \\ Conquer

7

t ← number of times x appear A

8

return the array obtained by concatenating BL, the array containing t copies of x, and BR Recurrence T(n) ≤ 2T(n/2) + O(n) Running time = O(n lg n)

slide-112
SLIDE 112

29/58

n n/2 n/2 n/4 n/4 n/4 n/4 n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8 . . . . . . . . . . . . . . . . . . . . . . . .

Each level has total running time O(n) Number of levels = O(lg n) Total running time = O(n lg n)

slide-113
SLIDE 113

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

slide-114
SLIDE 114

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17

slide-115
SLIDE 115

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17

slide-116
SLIDE 116

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 i j 17

slide-117
SLIDE 117

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 i j 17

slide-118
SLIDE 118

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 i j 17 17 64

slide-119
SLIDE 119

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 i j

slide-120
SLIDE 120

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 i j 82 64

slide-121
SLIDE 121

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 82 64 i j

slide-122
SLIDE 122

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 82 64 i j 37 64

slide-123
SLIDE 123

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 82 64 37 64 i j

slide-124
SLIDE 124

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 82 64 37 64 i j 75 64

slide-125
SLIDE 125

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 82 64 37 64 75 64 j i

slide-126
SLIDE 126

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 82 64 37 64 75 64 j i 15 64

slide-127
SLIDE 127

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 82 64 37 64 75 64 15 64 i j

slide-128
SLIDE 128

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 82 64 37 64 75 64 15 64 i j 94 64

slide-129
SLIDE 129

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 82 64 37 64 75 64 15 64 94 64 i j

slide-130
SLIDE 130

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 82 64 37 64 75 64 15 64 94 64 i j 25 64

slide-131
SLIDE 131

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 82 64 37 64 75 64 15 64 94 64 25 64 i j

slide-132
SLIDE 132

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 82 64 37 64 75 64 15 64 94 64 25 64 i j 64 69

slide-133
SLIDE 133

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 82 64 37 64 75 64 15 64 94 64 25 64 64 69 j i

slide-134
SLIDE 134

30/58

Quicksort Can Be Implemented as an “In-Place” Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses “small” extra space.

15 82 75 69 38 94 25 76 92 37 45 85 64 29 17 17 64 82 64 37 64 75 64 15 64 94 64 25 64 64 69 j i

To partition the array into two parts, we only need O(1) extra space.

slide-135
SLIDE 135

31/58

Outline

1

Approximation Algorithms

2

Approximation Algorithms for Traveling Salesman Problem

3

2-Approximation Algorithm for Vertex Cover

4 7 8-Approximation Algorithm for Max 3-SAT 5

Randomized Quicksort Recap of Quicksort Randomized Quicksort Algorithm

6

2-Approximation Algorithm for (Weighted) Vertex Cover Via Linear Programming Linear Programming 2-Approximation for Weighted Vertex Cover

slide-136
SLIDE 136

32/58

Randomized Quicksort Algorithm

quicksort(A, n)

1

if n ≤ 1 then return A

2

x ← a random element of A (x is called a pivot)

3

AL ← elements in A that are less than x \\ Divide

4

AR ← elements in A that are greater than x \\ Divide

5

BL ← quicksort(AL, AL.size) \\ Conquer

6

BR ← quicksort(AR, AR.size) \\ Conquer

7

t ← number of times x appear A

8

return the array obtained by concatenating BL, the array containing t copies of x, and BR

slide-137
SLIDE 137

33/58

Variant of Randomized Quicksort Algorithm

quicksort(A, n)

1

if n ≤ 1 then return A

2

repeat

3

x ← a random element of A (x is called a pivot)

4

AL ← elements in A that are less than x \\ Divide

5

AR ← elements in A that are greater than x \\ Divide

6

unitl AL.size ≤ 3n/4 and AR.size ≤ 3n/4

7

BL ← quicksort(AL, AL.size) \\ Conquer

8

BR ← quicksort(AR, AR.size) \\ Conquer

9

t ← number of times x appear A

10 return the array obtained by concatenating BL, the array

containing t copies of x, and BR

slide-138
SLIDE 138

34/58

Analysis of Variant

3

x ← a random element of A

4

AL ← elements in A that are less than x

5

AR ← elements in A that are greater than x Q: What is the probability that AL.size ≤ 3n/4 and AR.size ≤ 3n/4?

slide-139
SLIDE 139

34/58

Analysis of Variant

3

x ← a random element of A

4

AL ← elements in A that are less than x

5

AR ← elements in A that are greater than x Q: What is the probability that AL.size ≤ 3n/4 and AR.size ≤ 3n/4? A: At least 1/2

slide-140
SLIDE 140

35/58

Analysis of Variant

2

repeat

3

x ← a random element of A

4

AL ← elements in A that are less than x

5

AR ← elements in A that are greater than x

6

unitl AL.size ≤ 3n/4 and AR.size ≤ 3n/4 Q: What is the expected number of iterations the above procedure takes?

slide-141
SLIDE 141

35/58

Analysis of Variant

2

repeat

3

x ← a random element of A

4

AL ← elements in A that are less than x

5

AR ← elements in A that are greater than x

6

unitl AL.size ≤ 3n/4 and AR.size ≤ 3n/4 Q: What is the expected number of iterations the above procedure takes? A: At most 2

slide-142
SLIDE 142

36/58

Suppose an experiment succeeds with probability p ∈ (0, 1], independent of all previous experiments.

1

repeat

2

run an experiment

3

until the experiment succeeds Lemma The expected number of experiments we run in the above procedure is 1/p.

slide-143
SLIDE 143

37/58

Fact For q ∈ (0, 1), we have ∞

i=0 qi = 1 1−q.

slide-144
SLIDE 144

38/58

Lemma The expected number of experiments we run in the above procedure is 1/p. Proof Expectation = p + (1 − p)p × 2 + (1 − p)2p × 3 + (1 − p)3p × 4 + · · · = p

  • i=1

(1 − p)i−1i = p

  • j=1

  • i=j

(1 − p)i−1 = p

  • j=1

(1 − p)j−1 1 1 − (1 − p) =

  • j=1

(1 − p)j−1 = (1 − p)0 1 1 − (1 − p) = 1/p

slide-145
SLIDE 145

39/58

Variant Randomized Quicksort Algorithm

quicksort(A, n)

1

if n ≤ 1 then return A

2

repeat

3

x ← a random element of A (x is called a pivot)

4

AL ← elements in A that are less than x \\ Divide

5

AR ← elements in A that are greater than x \\ Divide

6

unitl AL.size ≤ 3n/4 and AR.size ≤ 3n/4

7

BL ← quicksort(AL, AL.size) \\ Conquer

8

BR ← quicksort(AR, AR.size) \\ Conquer

9

t ← number of times x appear A

10 return the array obtained by concatenating BL, the array

containing t copies of x, and BR

slide-146
SLIDE 146

40/58

Analysis of Variant

Divide and Combine: takes O(n) time Conquer: break an array of size n into two arrays, each has size at most 3n/4. Recursively sort the 2 sub-arrays.

n ≤ 3n/4 . . . . . . . . . . . . . . . . . . . . . . . . O(n) O(n) O(n) O(n) ≤ 9n/16 ≤ 27n/64

Number of levels ≤ lg4/3 n = O(lg n)

slide-147
SLIDE 147

41/58

Randomized Quicksort Algorithm

quicksort(A, n)

1

if n ≤ 1 then return A

2

x ← a random element of A (x is called a pivot)

3

AL ← elements in A that are less than x \\ Divide

4

AR ← elements in A that are greater than x \\ Divide

5

BL ← quicksort(AL, AL.size) \\ Conquer

6

BR ← quicksort(AR, AR.size) \\ Conquer

7

t ← number of times x appear A

8

return the array obtained by concatenating BL, the array containing t copies of x, and BR Intuition: the quicksort algorithm should be better than the variant.

slide-148
SLIDE 148

42/58

Analysis of Randomized Quicksort Algorithm

T(n): an upper bound on the expected running time of the randomized quicksort algorithm on n elements

slide-149
SLIDE 149

42/58

Analysis of Randomized Quicksort Algorithm

T(n): an upper bound on the expected running time of the randomized quicksort algorithm on n elements Assuming we choose the element of rank i as the pivot.

slide-150
SLIDE 150

42/58

Analysis of Randomized Quicksort Algorithm

T(n): an upper bound on the expected running time of the randomized quicksort algorithm on n elements Assuming we choose the element of rank i as the pivot. The left sub-instance has size at most i − 1

slide-151
SLIDE 151

42/58

Analysis of Randomized Quicksort Algorithm

T(n): an upper bound on the expected running time of the randomized quicksort algorithm on n elements Assuming we choose the element of rank i as the pivot. The left sub-instance has size at most i − 1 The right sub-instance has size at most n − i

slide-152
SLIDE 152

42/58

Analysis of Randomized Quicksort Algorithm

T(n): an upper bound on the expected running time of the randomized quicksort algorithm on n elements Assuming we choose the element of rank i as the pivot. The left sub-instance has size at most i − 1 The right sub-instance has size at most n − i Thus, the expected running time in this case is

  • T(i − 1) + T(n − i)
  • + O(n)
slide-153
SLIDE 153

42/58

Analysis of Randomized Quicksort Algorithm

T(n): an upper bound on the expected running time of the randomized quicksort algorithm on n elements Assuming we choose the element of rank i as the pivot. The left sub-instance has size at most i − 1 The right sub-instance has size at most n − i Thus, the expected running time in this case is

  • T(i − 1) + T(n − i)
  • + O(n)

Overall, we have T(n) = 1 n

n

  • i=1
  • T(i − 1) + T(n − i)
  • + O(n)
slide-154
SLIDE 154

42/58

Analysis of Randomized Quicksort Algorithm

T(n): an upper bound on the expected running time of the randomized quicksort algorithm on n elements Assuming we choose the element of rank i as the pivot. The left sub-instance has size at most i − 1 The right sub-instance has size at most n − i Thus, the expected running time in this case is

  • T(i − 1) + T(n − i)
  • + O(n)

Overall, we have T(n) = 1 n

n

  • i=1
  • T(i − 1) + T(n − i)
  • + O(n)

= 2 n

n−1

  • i=0

T(i) + O(n)

slide-155
SLIDE 155

42/58

Analysis of Randomized Quicksort Algorithm

T(n): an upper bound on the expected running time of the randomized quicksort algorithm on n elements Assuming we choose the element of rank i as the pivot. The left sub-instance has size at most i − 1 The right sub-instance has size at most n − i Thus, the expected running time in this case is

  • T(i − 1) + T(n − i)
  • + O(n)

Overall, we have T(n) = 1 n

n

  • i=1
  • T(i − 1) + T(n − i)
  • + O(n)

= 2 n

n−1

  • i=0

T(i) + O(n) Can prove T(n) ≤ c(n lg n) for some constant c by reduction

slide-156
SLIDE 156

43/58

Analysis of Randomized Quicksort Algorithm

The induction step of the proof: T(n) ≤ 2 n

n−1

  • i=0

T(i) + c′n ≤ 2 n

n−1

  • i=0

ci lg i + c′n ≤ 2c n  

⌊n/2⌋−1

  • i=0

i lg n 2 +

n−1

  • i=⌊n/2⌋

i lg n   + c′n ≤ 2c n n2 8 lg n 2 + 3n2 8 lg n

  • + c′n

= c n 4 lg n − n 4 + 3n 4 lg n

  • + c′n

= cn lg n − cn 4 + c′n ≤ cn lg n if c ≥ 4c′

slide-157
SLIDE 157

44/58

Exercise: Coupon Collector

Coupon Collector Each box of cereal contains a coupon. There are n different types of coupons. Assuming all boxes are equally likely to contain each coupon, in expectation, how many boxes before you have all coupon types? Break into n stages 1, 2, 3, · · · , n Stage i terminates when we have collected i coupon types Xi: number of coupons collected in stage i X = n

i=1 Xi: total number of coupons collected

slide-158
SLIDE 158

45/58

Exercise: Coupon Collector

Xi: number of coupons collected in stage i X = n

i=1 Xi: total number of coupons collected

In stage i: with probability n−(i−1)

n

, a random coupon has type different from the i − 1 types already seen Thus, E[Xi] =

n n−(i−1).

By linearity of expectation: E[X] =

n

  • i=1

n n − (i − 1) =

n

  • i=1

n i = nH(n), where H(n) = 1 + 1

2 + 1 3 + · · · + 1 n = Θ(lg n) is called the

n-th Harmonic number. E[X] = Θ(n lg n).

slide-159
SLIDE 159

46/58

Outline

1

Approximation Algorithms

2

Approximation Algorithms for Traveling Salesman Problem

3

2-Approximation Algorithm for Vertex Cover

4 7 8-Approximation Algorithm for Max 3-SAT 5

Randomized Quicksort Recap of Quicksort Randomized Quicksort Algorithm

6

2-Approximation Algorithm for (Weighted) Vertex Cover Via Linear Programming Linear Programming 2-Approximation for Weighted Vertex Cover

slide-160
SLIDE 160

47/58

Outline

1

Approximation Algorithms

2

Approximation Algorithms for Traveling Salesman Problem

3

2-Approximation Algorithm for Vertex Cover

4 7 8-Approximation Algorithm for Max 3-SAT 5

Randomized Quicksort Recap of Quicksort Randomized Quicksort Algorithm

6

2-Approximation Algorithm for (Weighted) Vertex Cover Via Linear Programming Linear Programming 2-Approximation for Weighted Vertex Cover

slide-161
SLIDE 161

48/58

Example of Linear Programming

min 4x1 + 5x2 s.t. 2x1 + x2 ≥ 6 x1 + 2x2 ≥ 4 x1, x2 ≥ 0

1 2 3 4 1 2 3 4 5 6 x1 x2 7 8 5 Feasible Region

slide-162
SLIDE 162

48/58

Example of Linear Programming

min 4x1 + 5x2 s.t. 2x1 + x2 ≥ 6 x1 + 2x2 ≥ 4 x1, x2 ≥ 0

1 2 3 4 1 2 3 4 5 6 x1 x2 7 8 5 Feasible Region

slide-163
SLIDE 163

48/58

Example of Linear Programming

min 4x1 + 5x2 s.t. 2x1 + x2 ≥ 6 x1 + 2x2 ≥ 4 x1, x2 ≥ 0

1 2 3 4 1 2 3 4 5 6 x1 x2 7 8 5 Feasible Region

slide-164
SLIDE 164

48/58

Example of Linear Programming

min 4x1 + 5x2 s.t. 2x1 + x2 ≥ 6 x1 + 2x2 ≥ 4 x1, x2 ≥ 0

1 2 3 4 1 2 3 4 5 6 x1 x2 7 8 5 Feasible Region

slide-165
SLIDE 165

48/58

Example of Linear Programming

min 4x1 + 5x2 s.t. 2x1 + x2 ≥ 6 x1 + 2x2 ≥ 4 x1, x2 ≥ 0

1 2 3 4 1 2 3 4 5 6 x1 x2 7 8 5 Feasible Region

slide-166
SLIDE 166

48/58

Example of Linear Programming

min 4x1 + 5x2 s.t. 2x1 + x2 ≥ 6 x1 + 2x2 ≥ 4 x1, x2 ≥ 0

  • ptimum point: x1 = 8

3, x2 = 2 3 1 2 3 4 1 2 3 4 5 6 x1 x2 7 8 5 Feasible Region

slide-167
SLIDE 167

48/58

Example of Linear Programming

min 4x1 + 5x2 s.t. 2x1 + x2 ≥ 6 x1 + 2x2 ≥ 4 x1, x2 ≥ 0

  • ptimum point: x1 = 8

3, x2 = 2 3

value = 4 × 8

3 + 5 × 2 3 = 14 1 2 3 4 1 2 3 4 5 6 x1 x2 7 8 5 Feasible Region

slide-168
SLIDE 168

49/58

Standard Form of Linear Programming

min c1x1 + c2x2 + · · · + cnxn s.t.

  • A1,1x1 + A1,2x2 + · · · + A1,nxn ≥ b1
  • A2,1x1 + A2,2x2 + · · · + A2,nxn ≥ b2

. . . . . . . . . . . .

  • Am,1x1 + Am,2x2 + · · · + Am,nxn ≥ bm

x1, x2, · · · , xn ≥ 0

slide-169
SLIDE 169

50/58

Standard Form of Linear Programming

Let x =      x1 x2 . . . xn      , c =      c1 c2 . . . cn      , A =      A1,1 A1,2 · · · A1,n A2,1 A2,2 · · · A2,n . . . . . . . . . . . . Am,1 Am,2 · · · Am,n      , b =      b1 b2 . . . bm      . Then, LP becomes min cTx s.t. Ax ≥ b x ≥ 0 ≥ means coordinate-wise greater than or equal to

slide-170
SLIDE 170

51/58

Linear programmings can be solved in polynomial time Algorithms for Solving LPs Simplex method: exponential time in theory, but works well in practice Ellipsoid method: polynomial time in theory, but slow in practice Internal point method: polynomial time in theory, works well in practice

slide-171
SLIDE 171

52/58

Outline

1

Approximation Algorithms

2

Approximation Algorithms for Traveling Salesman Problem

3

2-Approximation Algorithm for Vertex Cover

4 7 8-Approximation Algorithm for Max 3-SAT 5

Randomized Quicksort Recap of Quicksort Randomized Quicksort Algorithm

6

2-Approximation Algorithm for (Weighted) Vertex Cover Via Linear Programming Linear Programming 2-Approximation for Weighted Vertex Cover

slide-172
SLIDE 172

53/58

  • Def. Given a graph G = (V, E), a vertex cover of G is a subset

S ⊆ V such that for every (u, v) ∈ E then u ∈ S or v ∈ S .

slide-173
SLIDE 173

53/58

  • Def. Given a graph G = (V, E), a vertex cover of G is a subset

S ⊆ V such that for every (u, v) ∈ E then u ∈ S or v ∈ S .

slide-174
SLIDE 174

53/58

  • Def. Given a graph G = (V, E), a vertex cover of G is a subset

S ⊆ V such that for every (u, v) ∈ E then u ∈ S or v ∈ S . Weighted Vertex-Cover Problem Input: G = (V, E) with vertex weights {wv}v∈V Output: a vertex cover S with minimum

v∈S wv

slide-175
SLIDE 175

54/58

Integer Programming for Weighted Vertex Cover

For every v ∈ V , let xv ∈ {0, 1} indicate whether we select v in the vertex cover S The integer programming for weighted vertex cover: (IPWVC) min

  • v∈V

wvxv s.t. xu + xv ≥ 1 ∀(u, v) ∈ E xv ∈ {0, 1} ∀v ∈ V (IPWVC) ⇔ weighted vertex cover Thus it is NP-hard to solve integer programmings in general

slide-176
SLIDE 176

55/58

Integer programming for WVC: (IPWVC) min

  • v∈V

wvxv s.t. xu + xv ≥ 1 ∀(u, v) ∈ E xv ∈ {0, 1} ∀v ∈ V

slide-177
SLIDE 177

55/58

Integer programming for WVC: (IPWVC) min

  • v∈V

wvxv s.t. xu + xv ≥ 1 ∀(u, v) ∈ E xv ∈ {0, 1} ∀v ∈ V Linear programming relaxation for WVC: (LPWVC) min

  • v∈V

wvxv s.t. xu + xv ≥ 1 ∀(u, v) ∈ E xv ∈ [0, 1] ∀v ∈ V

slide-178
SLIDE 178

55/58

Integer programming for WVC: (IPWVC) min

  • v∈V

wvxv s.t. xu + xv ≥ 1 ∀(u, v) ∈ E xv ∈ {0, 1} ∀v ∈ V Linear programming relaxation for WVC: (LPWVC) min

  • v∈V

wvxv s.t. xu + xv ≥ 1 ∀(u, v) ∈ E xv ∈ [0, 1] ∀v ∈ V let IP = value of (IPWVC), LP = value of (LPWVC)

slide-179
SLIDE 179

55/58

Integer programming for WVC: (IPWVC) min

  • v∈V

wvxv s.t. xu + xv ≥ 1 ∀(u, v) ∈ E xv ∈ {0, 1} ∀v ∈ V Linear programming relaxation for WVC: (LPWVC) min

  • v∈V

wvxv s.t. xu + xv ≥ 1 ∀(u, v) ∈ E xv ∈ [0, 1] ∀v ∈ V let IP = value of (IPWVC), LP = value of (LPWVC) Then, LP ≤ IP

slide-180
SLIDE 180

56/58

Algorithm for Weighted Vertex Cover

Algorithm for Weighted Vertex Cover

1

Solving (LPWVC) to obtain a solution {x∗

u}u∈V

2 3

slide-181
SLIDE 181

56/58

Algorithm for Weighted Vertex Cover

Algorithm for Weighted Vertex Cover

1

Solving (LPWVC) to obtain a solution {x∗

u}u∈V

2

Thus, LP =

u∈V wux∗ u ≤ IP

3

slide-182
SLIDE 182

56/58

Algorithm for Weighted Vertex Cover

Algorithm for Weighted Vertex Cover

1

Solving (LPWVC) to obtain a solution {x∗

u}u∈V

2

Thus, LP =

u∈V wux∗ u ≤ IP

3

Let S = {u ∈ V : xu ≥ 1/2} and output S

slide-183
SLIDE 183

56/58

Algorithm for Weighted Vertex Cover

Algorithm for Weighted Vertex Cover

1

Solving (LPWVC) to obtain a solution {x∗

u}u∈V

2

Thus, LP =

u∈V wux∗ u ≤ IP

3

Let S = {u ∈ V : xu ≥ 1/2} and output S Lemma S is a vertex cover of G.

slide-184
SLIDE 184

56/58

Algorithm for Weighted Vertex Cover

Algorithm for Weighted Vertex Cover

1

Solving (LPWVC) to obtain a solution {x∗

u}u∈V

2

Thus, LP =

u∈V wux∗ u ≤ IP

3

Let S = {u ∈ V : xu ≥ 1/2} and output S Lemma S is a vertex cover of G. Proof.

slide-185
SLIDE 185

56/58

Algorithm for Weighted Vertex Cover

Algorithm for Weighted Vertex Cover

1

Solving (LPWVC) to obtain a solution {x∗

u}u∈V

2

Thus, LP =

u∈V wux∗ u ≤ IP

3

Let S = {u ∈ V : xu ≥ 1/2} and output S Lemma S is a vertex cover of G. Proof. Consider any edge (u, v) ∈ E: we have x∗

u + x∗ v ≥ 1

slide-186
SLIDE 186

56/58

Algorithm for Weighted Vertex Cover

Algorithm for Weighted Vertex Cover

1

Solving (LPWVC) to obtain a solution {x∗

u}u∈V

2

Thus, LP =

u∈V wux∗ u ≤ IP

3

Let S = {u ∈ V : xu ≥ 1/2} and output S Lemma S is a vertex cover of G. Proof. Consider any edge (u, v) ∈ E: we have x∗

u + x∗ v ≥ 1

Thus, either x∗

u ≥ 1/2 or x∗ v ≥ 1/2

slide-187
SLIDE 187

56/58

Algorithm for Weighted Vertex Cover

Algorithm for Weighted Vertex Cover

1

Solving (LPWVC) to obtain a solution {x∗

u}u∈V

2

Thus, LP =

u∈V wux∗ u ≤ IP

3

Let S = {u ∈ V : xu ≥ 1/2} and output S Lemma S is a vertex cover of G. Proof. Consider any edge (u, v) ∈ E: we have x∗

u + x∗ v ≥ 1

Thus, either x∗

u ≥ 1/2 or x∗ v ≥ 1/2

Thus, either u ∈ S or v ∈ S.

slide-188
SLIDE 188

57/58

Algorithm for Weighted Vertex Cover

Algorithm for Weighted Vertex Cover

1

Solving (LPWVC) to obtain a solution {x∗

u}u∈V

2

Thus, LP =

u∈V wux∗ u ≤ IP

3

Let S = {u ∈ V : xu ≥ 1/2} and output S Lemma S is a vertex cover of G.

slide-189
SLIDE 189

57/58

Algorithm for Weighted Vertex Cover

Algorithm for Weighted Vertex Cover

1

Solving (LPWVC) to obtain a solution {x∗

u}u∈V

2

Thus, LP =

u∈V wux∗ u ≤ IP

3

Let S = {u ∈ V : xu ≥ 1/2} and output S Lemma S is a vertex cover of G. Lemma cost(S) :=

u∈S wu ≤ 2 · LP.

slide-190
SLIDE 190

57/58

Algorithm for Weighted Vertex Cover

Algorithm for Weighted Vertex Cover

1

Solving (LPWVC) to obtain a solution {x∗

u}u∈V

2

Thus, LP =

u∈V wux∗ u ≤ IP

3

Let S = {u ∈ V : xu ≥ 1/2} and output S Lemma S is a vertex cover of G. Lemma cost(S) :=

u∈S wu ≤ 2 · LP.

Proof. cost(S) =

  • u∈S

wu ≤

  • u∈S

wu · 2x∗

u = 2

  • u∈S

wu · x∗

u

≤ 2

  • u∈V

wu · x∗

u = 2 · LP.

slide-191
SLIDE 191

58/58

Algorithm for Weighted Vertex Cover

Algorithm for Weighted Vertex Cover

1

Solving (LPWVC) to obtain a solution {x∗

u}u∈V

2

Thus, LP =

u∈V wux∗ u ≤ IP

3

Let S = {u ∈ V : x∗

u ≥ 1/2} and output S

Lemma S is a vertex cover of G. Lemma cost(S) :=

u∈S wu ≤ 2 · LP.

slide-192
SLIDE 192

58/58

Algorithm for Weighted Vertex Cover

Algorithm for Weighted Vertex Cover

1

Solving (LPWVC) to obtain a solution {x∗

u}u∈V

2

Thus, LP =

u∈V wux∗ u ≤ IP

3

Let S = {u ∈ V : x∗

u ≥ 1/2} and output S

Lemma S is a vertex cover of G. Lemma cost(S) :=

u∈S wu ≤ 2 · LP.

Theorem Algorithm is a 2-approximation algorithm for WVC.

slide-193
SLIDE 193

58/58

Algorithm for Weighted Vertex Cover

Algorithm for Weighted Vertex Cover

1

Solving (LPWVC) to obtain a solution {x∗

u}u∈V

2

Thus, LP =

u∈V wux∗ u ≤ IP

3

Let S = {u ∈ V : x∗

u ≥ 1/2} and output S

Lemma S is a vertex cover of G. Lemma cost(S) :=

u∈S wu ≤ 2 · LP.

Theorem Algorithm is a 2-approximation algorithm for WVC. Proof. cost(S) ≤ 2 · LP ≤ 2 · IP = 2 · cost(best vertex cover).