minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

61

Upload: deborah-holt

Post on 13-Jan-2016

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6
Page 2: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 3: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 4: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 5: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 6: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 7: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 8: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Minimum weight spanning trees

Prim’s algorithm 1) S {1}2) add the cheapest edge {u,v} such that uS and vSC

S S{v} (until S=V)

Page 9: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Minimum weight spanning trees

1) S {1}2) add the cheapest edge {u,v} such that uS and vSC

S S{v} (until S=V)

P = MST output by PrimT = optimal MST

Is P = T ?

assume all the edgeweights different

Page 10: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Minimum weight spanning treesP = MST output by PrimT = optimal MST

P = T assuming all the edgeweights different

v1,v2,...,vn order added to S by Prim

smallest i such that the edge e E used by Prim to connect vi+1

is not in T.

Page 11: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Minimum weight spanning trees

S

smallest i such that the edge e E used by Prim to connect vi+1

is not in T.

S={v1,...,vi}

look at T

vi+1f

Page 12: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Minimum weight spanning trees

S

smallest i such that the edge e E used by Prim to connect vi+1

is not in T.

S={v1,...,vi}

look at T+e

e

f

w(f) > w(e)

vi+1

Page 13: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Minimum weight spanning trees

S

smallest i such that the edge e E used by Prim to connect vi+1

is not in T.

S={v1,...,vi}

look at T+e-f

e

f

w(f) > w(e)

vi+1

Page 14: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Prim’s algorithm

for i 1 to n do C[i] C[0] 0

S {}while S V do j with minimal C[j] over jSC S S { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]w[{j,u}]; P[u] j

Page 15: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

O(E+V log V)

for i 1 to n do C[i] C[0] 0

S {}while S V do j with minimal C[j] over jSC S S { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]w[{j,u}]; P[u] j

Extract-Min O(log n) time

Decrease-KeyO(1) time (amortized)

Prim’s algorithm

Page 16: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Shortest path problem

1

23

8

4

2

4

27

1

6A

B

Page 17: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Shortest path problem

1

23

8

4

2

4

27

1

6A

B

Page 18: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Longest path problem ?

1

23

8

4

2

4

27

1

6A

B

Page 19: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Longest path problem ?path from u to v = sequence v1,...,vn such that v1 = u, v2 = v, {vi,vi+1} E no repeated vertices

tour from u to v = sequence v1,...,vn such that v1 = u, v2 = v, {vi,vi+1} E no repeated edges

walk from u to v = sequence v1,...,vn such that v1 = u, v2 = v, {vi,vi+1} E

walk from u to v exists path from u to v

shortest path = shortest walk

longest path longest walk

Page 20: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Shortest path problem

1

23

8

4

2

4

27

1

6A

B

0

Page 21: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Shortest path problem

1

23

8

4

2

4

27

1

6A

B

0 1

Page 22: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Shortest path problem

1

23

8

4

2

4

27

1

6A

B

0 1

2

Page 23: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Shortest path problem

1

23

8

4

2

4

27

1

6A

B

0 1

23

Page 24: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Shortest path problem

1

23

8

4

2

4

27

1

6A

B

0 1

23

4

Page 25: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Shortest path problem

1

23

8

4

2

4

27

1

6A

0 1

23

4

B6

Page 26: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Dijkstra’s algorithm

for i 1 to n do T[i] T[0] 0

S {}while S V do j with minimal T[j] over jSC S S { j } for u neighbors of j do if T[j]+w[{j,u}] < T[u] then T[u]T[j]+w[{j,u}]; P[u] j

running time ?

Page 27: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Dijkstra’s algorithm (for s.p.)T[0] 0; S {}; for i 1 to n do T[i] while S V do j with minimal T[j] over jSC S S { j } for u neighbors of j do if T[j]+w[{j,u}] < T[u] then T[u]T[j]+w[{j,u}]; P[u] j

Prim’s algorithm (for MST)C[0] 0; S {}; for i 1 to n do C[i] while S V do j with minimal C[j] over jSC S S { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]w[{j,u}]; P[u] j

Page 28: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Dijkstra’s algorithm - correctnessT[0] 0; S {}; for i 1 to n do T[i] while S V do j with minimal T[j] over jSC S S { j } for u neighbors of j do if T[j]+w[{j,u}] < T[u] then T[u]T[j]+w[{j,u}]; P[u] j

Claim: After t steps we have 1. d(0,u) = T[u] for all uS 2. T[u] = dS(0,u) for all u, where dS(0,u) is the length of shortest path from 0 to u, such that all vertices (except possibly u) of the path are in S

Proof: induction on t.

Page 29: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Negative edge-weights?

2

23

8

4

2

4

-27

1

6A

B

Page 30: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Negative edge-weights?

2

23

8

4

2

4

-27

1

6A

2

B

Page 31: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

-1

-2-3

-8

-4

-2

-4

-2-7

-1

-6A

B

Shortest path problem

Page 32: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

-1

-2-3

-8

-4

-2

-4

-2-7

-1

-6A

B

Shortest path problem

1

23

8

4

2

4

27

1

6A

BLongest path problem

Page 33: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

-1

-2-3

-8

-4

-2

-4

-2-7

-1

-6A

B

Shortest path problem

1

23

8

4

2

4

27

1

6A

BLongest path problem

shortest path shortest walk (if negative)

Page 34: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Negative edge weights allowed

but no negative cycles

shortest path shortest walk (if negative)

Page 35: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

The Bellman-Ford algorithmD[v] = estimate on the distance from 0

Initially: D[0] = 0 D[i] = for all other vertices

Relaxation of an edge:

if D[u] + w(u,v) < D[v] then D[v] D[u] + w(u,v)

Page 36: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

The Bellman-Ford algorithmD[v] = estimate on the distance from 0

Initially: D[0] = 0 D[i] = for all other vertices

Repeat |V| times relax every edge e

running time = O( V.E )

Page 37: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

The Bellman-Ford algorithm

Let S[u] be the number of edges in the shortest path from 0 to u

Claim: After t steps of the algorithm S[u] t D[u] = d(0,u)

Page 38: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

All-pairs shortest path problem

Dijkstra O( V2 log V + V.E )

(negative edges not allowed)

Page 39: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

The Floyd-Warshall algorithm

dynamic programming algorithmnegative edges allowed, no negative cycles

dijk = length of the shortest path from i to j

which only uses vertices 1...k

dijk= min {dij

k-1,dikk-1 + dkj

k-1}

Page 40: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

The Floyd-Warshall algorithm

dij0 wij

dijk= min {dij

k-1,dikk-1 + dkj

k-1}

for k from 1 to n do for i from 1 to n do for j from 1 to n do

for all i,j V

Time = ?

Space = ?

Page 41: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

The Floyd-Warshall algorithm

dij0 wij

dijk= min {dij

k-1,dikk-1 + dkj

k-1}

for k from 1 to n do for i from 1 to n do for j from 1 to n do

for all i,j V

Time = O(n3)

Space = O(n3)

Page 42: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

The Floyd-Warshall algorithm

dij wij

dij= min {dij,dik+ dkj}

for k from 1 to n do for i from 1 to n do for j from 1 to n do

for all i,j V

Time = O(n3)

Space = O(n2)

Page 43: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Single source shortest paths

Dijkstra = O(E+V log V)

Bellman-Ford = O(E.V) allows negative edge-weights (but not negative cycles)

All-pairs shortest paths

Dijkstra = O(EV+V2 log V)

Floyd-Warshall = O(V3) allows negative edge-weights (but not negative cycles)

Johnson = O(EV+V2 log V) allows negative edge-weights (but not negative cycles)

Page 44: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Johnson’s algorithm

Dijkstra = O(E+V log V)

Bellman-Ford = O(E.V) allows negative edge-weights (but not negative cycles)

w’(u,v) = w(u,v) + h(u) – h(v) doesn’t change the shortest paths

use Bellman-Ford to compute h such that w’ is non-negative

Page 45: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6
Page 46: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

M[i-1,j] d-1M[i,j] d M[i,j-1] d-1 M[i-1,j-1] d-1

M[i-1,j] d-1and M[i,j-1] d-1 andM[i-1,j-1] d-1 andA[i,j]=1

M[i,j] d

Page 47: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

if A[i,j] = 1 then M[i,j] 1+min(M[i-1,j],M[i,j-1],M[i-1,j-1])else M[i,j] 0

Page 48: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

If there exists a subset of size kthen the k largest numbers haveaverage at least k

Page 49: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

Find(A[l..r], n, s, B) if r l+1 then try all possibilities else m median(A[l..r]); p partition(A[l..r],m) s’ sum in A[p+1..r]; n’ (r-p) if (s+s’)/(n+n’) < B then Find(A[p+1..r], n, s,B) else Find(A[l..p],n+n’,s+s’,B)

T(n)=T(n/2)+O(n)

Page 50: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6
Page 51: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

b[k] = the largest subset of a1,...,ak

no three consecutive selected

b[k-1]b[k-2] + ak

b[k-3] + ak + ak-1

last not included in OPT

last included in OPT,second to last not

last 2 in OPT

Page 52: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

b[0] 0; b[1] a1; b[2] a1+a2; c[0] 1; c[1] 2; c[2] 3;for k from 3 to n do b[k] b[k-1]; c[k] 1; if ak+b[k-2] > b[k] then b[k] ak+b[k-1]; c[k] 2; if ak+ak-1+b[k-3] > b[k] then b[k] ak+ak-1+b[k-3]; c[k] 3;\k nwhile (k 0) do if c[k]=3 then print(ak,ak-1); k k-3; if c[k]=2 then print(ak); k k-2; if c[k]=1 then k k-1;

Page 53: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

P[0...M,0..k]P[S,i] = can obtain sum S using a1,...,ai

P[0,0] true; for i from to M do P[i,0] false;for i from 1 to n do for S from 0 to n do P[S,i] P[S,i-1] if ai S and P[S-ai,i-1] then P[S,i]true

return P[M,n]

Page 54: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6
Page 55: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6
Page 56: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

P[0...M,0..k]P[S,i] = k means can obtain sum S using k numbers from a1,...,ai

P[0,0] 0; for i from to M do P[i,0] ;for i from 1 to n do for S from 0 to n do P[S,i] P[S,i-1] if ai S then P[S,i]min(P[S,i-1],P[S-ai,i-1]+1)

return P[M,n]

Page 57: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6
Page 58: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

rev(G) H = empty graph on |V| vertices for vV do for each out-neighbor u of v do append(H,u,v) return H sort(G) = rev(rev(G))

undirected = compare lists rev(rev(rev(G))) and rev(rev(G))

Page 59: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

M[0] 0for i from 1 to n do for k from 1 to i1/2 do M[i]=min(M[i],M[i-k2])

Lagrange’s theorem: for all n we have M[n] 4

Legendre: if n 4k (8m + 7) M[n] 3

Page 60: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

M[i] = sum of the largest increasing subsequence which ends with the i-th element

for i from 1 to n do M[i] A[i] for j from 0 to n do if A[j]<A[i] then M[i] max(M[i],M[j]+A[i])

Output(n,u) if n>0 then i index with maximum M[i] subject to A[i]<u print(A[i]); Output(i-1,A[i])

Page 61: Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6