37
Graph Algorithms Maximum Flow - Best algorithms Adapted from R.Solis-Oba]

Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Embed Size (px)

DESCRIPTION

Graph Algorithms Bipartite Matching [Adapted from Z.Hu]

Citation preview

Page 1: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsMaximum Flow - Best algorithms

[Adapted from R.Solis-Oba]

Page 2: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph Algorithms Graph matching problem

[Adapted from R.Diestel]

Edmonds algorithm:

(|V|1/2 |E|) (“easy bond” – (|V|4))

Mucha-Sankowski algorithm:

(|V|2.376)

Page 3: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching

[Adapted from Z.Hu]

Page 4: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching

[Adapted from Z.Hu]

Page 5: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching

ProblemFind maximum number of disjoint edges between vertices from two disjoint sets.

2 pairs in the 1st case and 3 in the 2nd.

Can we find more?

Page 6: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching - Idea

[Adapted from Z.Hu]

Page 7: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching - Idea

[Adapted from Z.Hu]

Page 8: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching

[Adapted from Z.Hu]

Page 9: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching

[Adapted from Z.Hu]

Page 10: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching - Complexity

The size of network (values |V| and |E|) are of the same orderas in initial graph.

Edmods-Karp: T(V,E) = O(V E2)

Can we do better?

Page 11: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching - Complexity

Note, that | f*| |V|.

Thus (independently from how we chose augmenting paths) T(V,E) = O(| f*| E) = O(V E).

Of course, this holds also for Edmods-Karp algorithm.

Can we do better?

Page 12: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching - Complexity

Theorem

Let M be any matching, and let a maximum matching have|M| + k edges. Then M has at least k disjoint augmenting paths.

Proof

M - matchingM' - a maximum matching

Consider M and M' together.This is a graph (actually, multigraph)with vertex degree at most 2.Divide this graph in connected components.

Page 13: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching - Complexity

Consider M and M' together and divide this graph in connectedcomponents. There are 3 possibilities:

1.A non-augmenting path (or possibly a closed circuit) with an equal number of edges from M and M'.

2.An augmenting path where the first and the last edge belongs to M'.

3.A diminishing path which begins and ends with an edge from M.

Essentially, one should notice that all components will consist of edges alternating from M and M'.

Page 14: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching - Complexity

There is one more edge from M' than M in an augmenting path, and one edge less in a diminishing path.

Hence

(# of augmenting paths) − (# of diminishing paths) = k,

and so there must be at least k augmenting paths.Augmenting path - starts and ends with edges from M'. Corresponds to augmenting path in flow graph for M.

Diminishing path - starts and ends with edges from M. Corresponds to augmenting path in flow graph for M'.

Page 15: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching - Complexity

TheoremLet M be any matching, and let a maximum matching have|M| + k edges. Then M has at least k disjoint augmenting paths.

CorollaryIf k edges can be added to the matching then there is anaugmenting path of length at most |V| /k.

CorollaryIf there are no augmenting paths of length ≤ |V|1/2, then thereare at most |V|1/2 augmenting paths remaining before the maximum matching is found.

Page 16: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching - Complexity

The problem is to find |V| augmenting paths. If we lookfor each of them seperately, each such step requires time |E|.

Can we join some steps?

Start with M and add all shortest disjoint augmenting paths that can be found using BFS. Lets assume, the length of shortest paths is d and the resulting matching is M'.

Page 17: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching - Complexity

Do BFS up to the depth d. Select at this depth all vertices from "sink side" from which there is path to t. Then, use justgreedy strategy to select as many disjoint augmenting pathsas possible (start at selected vertices and backtrack, until vertex s is reached, or a vertex already in use and distinct froms is found).

Each such step will require time O(d ·(# of paths) + |E|).

d

Page 18: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching - Complexity

Start with M and add all shortest disjoint augmenting paths that can be found using BFS. Lets assume, the length of shortest paths is d and the resulting matching is M'.

PropositionThe length of shortest augmenting path in M' is at least d+1.

ProofFirst, note that for sink-side vertices there is at most one outgoing edge for each vertex.Similarly, for source-side vertices there is at most one incoming edge for each vertex.

(We don't count edges connecting with s or t).

Page 19: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching - Complexity

Assume there is an augmenting path in M' of length smallerthan d+1.

It must intersect with at least one of augmenting paths selected in previous step (otherwise, it should be selectedby greedy strategy already in previous step).

If intersection vertex is on source-side, path shares theprevious edge with an augmenting path from previous step.

If intersection vertex is on sink-side, path shares thenext edge with an augmenting path from previous step.

In any case, there will be a shared adge between this path andone used in previous step.

Page 20: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching - Complexity

In any case, there will be a shared adge between this path andone used in previous step.

x x+1

x' 1 x'

We should have: x'+1 x' and x+2 x' + 1. Taking d = x+2and d'= x'1, we get d x' > x'1 d'.

x+1 x'x

x+2 x' + 1

Page 21: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBipartite Matching - Complexity

The problem is to find |V| augmenting paths, if we lookfor each of them seperately, each such step requires time |E|.

Can we join some steps?

Idea:Run BFS looking for paths with length d = 1... |V|1/2.

Time for each step: O(d ·(# of paths) + |E|) Total time: O(|V|1/2·|V| + |E|·|V|1/2) = O(|E|·|V|1/2)

Some paths could remain, but at most |V|1/2. To findthem requires time at most O(|E|·|V|1/2).

Time complexity for bipartite matching: O(|E|·|V|1/2).

Page 22: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph Algorithms”Stable mariage” problem

Given bipartite graph with edge weights w: E R.

Problem 1Find a matching, which doesn’t contain two edges (a,a’) and(b,b’), such that w(a,a’)<w(a,b’) and w(b,b’)<w(b,a’).

It is commonly stated as (ok this is slightly different version of the problem):

Given n men and n women, where each person has ranked all members of the opposite sex with a unique number between 1 and n in order of preference, marry the men and women together such that there are no two people of opposite sex who would both rather have each other than their current partners. If there are no such people, all the marriages are "stable".

Page 23: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph Algorithms”Stable mariage” problem

Trivial “greedy search” approach:

While there is a blocking pairDo Switch the blocking pair

Depending from the exact formulation of the problem this strategy can be either comparatively non-efficient or even not terminate at all.

Page 24: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph Algorithms”Stable mariage” problem

Gale-Shapley algorithm (1962)

•Fix some ordering on the men•Repeat until everyone is matched

•Let X be the first unmatched man in the ordering•Find woman Y such that Y is the most desirable woman in X’s list such that Y is unmatched, or Y is currently matched to a Z and X is more preferable to Y than Z.•Match X and Y; possible this turns Z to be unmatched

The algorithm finds a stable matching in O(n2) time

Page 25: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph Algorithms”Stable mariage” problem

Gale-Shapley algorithm

•Once a woman is matched, she stays matched (her partner can change).

•When the partner of a woman changes, this is to a more preferable partner for her: at most n – 1 times.

•Every step, either an unmatched woman becomes matched, or a matched woman changes partner: at most n2 steps.

Page 26: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph Algorithms”Stable mariage” problem

Gale-Shapley algorithm

Optimality (the marriages are stable):

Let Alice be a woman and Bob be a man who are both engaged, but not to each other. Upon completion of the algorithm, it is not possible for both Alice and Bob to prefer each other over their current partners. If Bob prefers Alice to his current partner, he must have proposed to Alice before he proposed to his current partner. If Alice accepted his proposal, yet is not married to him at the end, she must have dumped him for someone she likes more, and therefore doesn't like Bob more than her current partner. If Alice rejected his proposal, she was already with someone she liked more than Bob.

Page 27: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsWeighted Bipartite Matching

Given bipartite graph with edge weights w: E R.

Problem 1Find a matching with minimal sum of edge weights.

Problem 2Find a matching with minimal sum of edge weights from the set of all matchings with maximal cardinality (maximum matchings).

If we have an algorithm that in i-th step produces the cheapestmatching with cardinality i, we effectively can deal with bothof these problems...

Page 28: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsWeighted Bipartite MatchingWhich flow graph should we use?

Add source and sink vertices s and t as in non-weighted case, however:

- use unlimited capacities for edges connecting s and t;- when looking for augmenting paths do not use edges connecting s or t with a vertex that has already been included in a matching.

So actually the weighted bipartite matching problem can not be reduced to finding of maximal flow, but there is some similarity of these algorithms.

Page 29: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsWeighted Bipartite Matching

Theorem

Let M be a cheapest matching with k edges. A cheapest matching with k + 1 edges can be obtained by augmentingM with the cheapest augmenting path.

Proof

M' - a cheapest matching with k + 1 edges.Consider M M' .

As before, we get 3 types of connected components:

Page 30: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsWeighted Bipartite Matching

Consider all 3 types of connected components of M M':

1.Non-augmenting paths with even number of edges.

2.Augmenting paths with one edge more from M' than M.

3.Diminishing paths with one edge more from M'than M'.

In a non-augmenting path, the total weight of edges in M must be equal to the total weight of edges in M'. Otherwise one of the matchings could be made cheaper by replacing the edges in the path by those in the other matching.

Page 31: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsWeighted Bipartite Matching

The number of augmenting paths is one greater than the number of diminishing paths.

Consider a pair consisting of one augmenting path and one diminishing path.

The total weight of the edges in both paths that belong to M must equal the weight of the edges in M' in the two paths. Otherwise we could switch the edges in M and M' and obtain a cheaper matching of size k or k + 1.

Page 32: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsWeighted Bipartite Matching

We have to show that there is a cheapest matching with k + 1 edges that can be obtained by augmenting M with a cheapestaugmenting path.

Modify M' as follows:- in components of type 1, replace all M' edges with M edges(except for two vertex components, i.e. when these matchingscoincide).- for each augmenting-diminishing path pair, replace all M' edges with M edges.

Resulting matching M'' will be a cheapest matching with k + 1 edges and obtainable by augmenting M with a cheapest augmenting path.

Page 33: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsWeighted Bipartite Matching - ComplexityWe can solve the problem of finding the cheapest maximummatching, in the same way than finding maximum non-weighted matching, except, that we have to look forthe cheapest augmenting paths, instead of any.

Up to |V| steps, each of which takes time O(|V||E|) (using Bellman-Ford).

Total time complexity: O(|V|2|E|).

There is a historical Hungarian method (Harold Kuhn, 1955) also with running time O(|V|2|E|).

Time can be reduced to O(|V|2log |V| + |V||E|) (using reweightingand Dijkstra’s algorithm).

Page 34: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsTravelling Salesman Problem

TSP (Travelling Salesman Problem)For a given undirected weighted graph, find a minimalweight cycle containing all the vertices.

Known to be NP-hard.

Page 35: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph Algorithms Travelling Salesman Problem

TSP tour of Sweden

24978 cities

length 72500 km

solved in 2004

Page 36: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsBranch and bound algorithms

- generate each potential solution just once- try to find some rules that allows to cut some branchesof serach tree

Page 37: Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]

Graph AlgorithmsMinimal weight cycle cover

TSP (Travelling Salesman Problem)For a given undirected weighted graph, find a minimalweight cycle containing all the vertices.

Cheapest second degree graph For a given undirected weighted graph, find a set of cycleswith minimal total weight containing all the vertices.

Heuristic methods fot TSP:In each step chose the extension of a partial path with minimalweight cycle cover for remaining part of the graph.