51
erm 20 1 -29 0 -39 4 -49 5 -59 9 -69 7 70 3

Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7 70 3

Embed Size (px)

Citation preview

Page 1: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Midterm

< 20 120-29 030-39 440-49 550-59 960-69 770 3

Page 2: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Matchings

matching M = subgraph with vertices of degree 1

Page 3: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Matchings

= unmatched vertex (vertex not in M)

matching M = subgraph with vertices of degree 1

Page 4: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Matchings

matching M = subgraph with vertices of degree 1

= unmatched vertex (vertex not in M)

perfect matching = matching with no unmatched vertices (perfect world – everybody has a buddy)

Page 5: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Perfect matching

Page 6: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Maximal / maximum

maximal = no edge can be added to M to make a bigger matching

maximum = there is no matching with more edges

maximal, NOT maximum maximum maximal

Page 7: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting path

path connecting two unmatched vertices with alternating non-matching and matching edges

If there exists augmenting path then the matching is NOT maximum.

Page 8: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting path

path connecting two unmatched vertices with alternating non-matching and matching edges

Page 9: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting path

path connecting two unmatched vertices with alternating non-matching and matching edges

Lucky ?

Page 10: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting path

Lucky ? NO

Theorem:

If matching M is not maximum then there exists an augmenting path.

Page 11: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting pathTheorem:

If the matching M is not maximum then there exists an augmenting path.

M = matching, not maximumB = bigger matching

Page 12: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Proof

M = matching, not maximumB = bigger matching

MB

Page 13: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Proof

M = matching, not maximumB = bigger matching

MB

Page 14: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Proof

M = matching, not maximumB = bigger matching

MB

Page 15: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Proof

M = matching, not maximumB = bigger matching

MBvertices of degree 0,1,2 paths and cycles

no odd cycles

must have a path with more edges from Bthan from M augmenting path

Page 16: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

How to find maximum matching?

Mrepeat find augmenting path P M MPuntil no augmenting path

Page 17: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting path in bipartite graphs

augmenting path has odd length endpoints on different sides

call the one on the left start

non matching edges matching edges

Page 18: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting path in bipartite graphs

augmenting path has odd length endpoints on different sides

call the one on the left start

non matching edges matching edges

Page 19: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting path in bipartite graphs

augmenting path has odd length endpoints on different sides

call the one on the left start

non matching edges matching edges

Theorem: augmenting path exists there is a path from unmatched vertex on the left to an unmatched vertex on the right

Page 20: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting path in bipartite graphs

augmenting path has odd length endpoints on different sides

call the one on the left start

non matching edges matching edges

Theorem: augmenting path exists there is a path from the blue vertex on the left to the blue vertex on the right

Page 21: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting path in bipartite graphs

Finding augmenting path(or checking none exists)

one BFS (or DFS) computation

TIME = O(E)

Finding max-matching O( VE )

Mrepeat find augmenting path P M MP

until no augmenting path

Page 22: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Maximum-weight matchings

1010

10

55

5

5

55

Page 23: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Maximum-weight matchings

1010

10

55

5

5

55

weight = 30

maximum-weight matching doesn’t have to be maximum cardinality !!!

Page 24: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting path

path connecting two unmatched vertices with alternating non-matching and matching edges

+ - + +-

we > wf

e PMC f PM

must gain by the swap:

Page 25: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting greedily

Assume M = matching of the maximum-weight among matchings with k edges P = augmenting path with maximum gainThen MP = matching of the maximum weight among matchings with k+1 edges

THEOREM:

Page 26: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting greedilyAssume M = matching of the maximum-weight among matchings with k edges P = augmenting path with maximum gainThen MP = matching of the maximum weight among matchings with k+1 edges

Proof: B = max-weight with k+1 edgesMB

Page 27: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting greedilyAssume M = matching of the maximum-weight among matchings with k edges P = augmenting path with maximum gainThen MP = matching of the maximum weight among matchings with k+1 edges

Proof: B = max-weight with k+1 edgesMB

gain = 0

Page 28: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting greedilyAssume M = matching of the maximum-weight among matchings with k edges P = augmenting path with maximum gainThen MP = matching of the maximum weight among matchings with k+1 edges

Proof: B = max-weight with k+1 edgesMB

gain = 0

Page 29: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting greedilyAssume M = matching of the maximum-weight among matchings with k edges P = augmenting path with maximum gainThen MP = matching of the maximum weight among matchings with k+1 edges

Proof: B = max-weight with k+1 edgesMB

gain = 0

Page 30: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Augmenting greedilyAssume M = matching of the maximum-weight among matchings with k edges P = augmenting path with maximum gainThen MP = matching of the maximum weight among matchings with k+1 edges

Proof: B = max-weight with k+1 edgesMB

gain = wt(B) – wt(M)

Page 31: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

How to find maximum-weight matching?

Mrepeat find augmenting path P with maximum gain M MPuntil no augmenting path

only need O(V) iterations

Page 32: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

How to find augmenting path withthe maximum gain?

+

-

Page 33: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

SHORTEST PATH PROBLEM

How to find augmenting path withthe maximum gain?

+

-

Page 34: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

NEGATIVE CYCLE?

How to find augmenting path withthe maximum gain?

+

-

Page 35: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

NEGATIVE CYCLE?

How to find augmenting path withthe maximum gain?

+

-

impossible – would contradict maximality (among matchings of cardinality k)

Page 36: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

How to find augmenting path withthe maximum gain?

no negative cycles

can use Bellman-Fordto find the shortest path(and hence get augmenting pathwith the maximum gain)

Page 37: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Putting it all together

Mrepeat find augmenting path P with maximum gain M MPuntil no augmenting path

only need O(V) iterations

O(VE) time per iteration (Bellman-Ford)O(V2 E) time total

Page 38: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Putting it all together

Mrepeat find augmenting path P with maximum gain M MPuntil no augmenting path

only need O(V) iterations

O(Vlog V + E) time per iteration (Dijkstra)O(V2log V + VE) time total

Page 39: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Back to midterm

Page 40: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3
Page 41: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3
Page 42: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Linear ordering of vertices of a directed acyclic graph such thatno edges point backward.

Page 43: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

2log n = n

n1/log n = (2log n )1/log n = 2 (log n)/log n = 2n2/log n = (2log n )2/log n = 2 2(log n)/log n = 4

1

n1 + 1/log n = n . n1/log n = 2nn

n2

binomial(n,2)=n(n-1)/2

Page 44: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Mergesort reduces an instance ofsize n to 2 instances of size n/2and the reduction takes time (n).

Page 45: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Assume that all degrees 2. Then

2 |E| = deg(v) 2 = 2 nvV vV

Hence there is a vertex of degree 1.

There cannot be a vertex of degree 0,since G is connected and |V|2.

Hence there is a vertex of degree = 1.

Page 46: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

max { E[ i ], O[ j ] + 1}

max { O[ i ], E[ j ] + 1}

Page 47: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

max { B[i-1], B[i-2] + ai }

Page 48: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

for i from 1 to n do for j from 1 to n do A[i,j] (A[i,j] + (i+j)) mod 2

M1 largest all-ones squareM2 largest all-zeros squarereturn max (M1, M2)

Page 49: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

for i from 1 to n do for j from 1 to n do M[i,j] 1 if i>1 and j>1 then if (A[i,j]=A[i-1,j-1]) and (A[i,j] A[i-1,j]) and (A[i,j] A[i,j-1])

M[i,j] 1+min(M[i-1,j],M[i,j-1],M[i-1,j-1])

return max M[i,j]i,j {1,...,n}

Page 50: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

Extract-Min(H) min H[1] H[1] H[n] n n-1 Heapify(H,1) return min

H = heapn = size of the heap

Page 51: Midterm < 20 1 20-29 0 30-39 4 40-49 5 50-59 9 60-69 7  70 3

input: weighted, undirected graph G (given by adjacency list)

output: minimum-cost spanning tree of G (cost of a tree is the sum of the weights of its edges)