700 likes | 720 Views
IOI/ACM ICPC Training. 4 June 2005. Euler Tour. IOI/ACM ICPC Training. Can we draw the following figure in one line?. Can we draw the following figure in one line?. Fail!. 2. 3. 3. 2. 2. How to determine if a graph can be drawn in one line?. Theorem:
E N D
IOI/ACM ICPC Training 4 June 2005
Euler Tour IOI/ACM ICPC Training
2 3 3 2 2 How to determine if a graph can be drawn in one line? Theorem: • A graph G has an Euler path if and only if • Exactly two vertices u satisfies: |indegree(u)-outdegree(u)| = 1 • All other vertices v satisfies: indegree(v)=outdegree(v) 3 3 3 3
Time analysis • We can compute the degrees of all vertices in O(|E|) time. • Then, it requires O(|V|) time to check if the vertices satisfies the previous theorem. • In total, O(|V|+|E|) time.
Topological sort IOI/ACM ICPC Training
b d a c e Topological order • Consider the prerequisite structure for courses: • Each node x represents a course x • (x, y) represents that course x is a prerequisite to course y • Note that this graph should be a directed graph without cycles. • A linear order to take all 5 courses while satisfying all prerequisites is called a topological order. • E.g. • a, c, b, e, d • c, a, b, e, d
Topological sort • Arranging all nodes in the graph in a topological order • Applications: • Schedule tasks associated with a project
Topological sort algorithm Algorithm topSort1 n = |V|; Let R[0..n-1] be the result array; for i = 1 to n { select a node v that has no successor; R[n-i] = v; delete node v and its edges from the graph; } return R;
b a c e • d has no successor! Choose d! • Both b and e have no successor! Choose e! b b d b a a a a c c e • Both b and c have no successor! Choose c! • Only b has no successor! Choose b! • Choose a! • The topological order is a,b,c,e,d Example
Time analysis • Finding a node with no successor takes O(|V|+|E|) time. • We need to repeat this process |V| times. • Total time = O(|V|2 + |V| |E|). • We can implement the above process using DFS. The time can be improved to O(|V| + |E|).
Algorithm based on DFS Algorithm topSort2 s.createStack(); for (all nodes v in the graph) { if (v has no predecessors) { s.push(v); mark v as visited; } } while (s is not empty) { let v be the node on the top of the stack s; if (no unvisited nodes are children to v) {// i.e. v has no unvisited successor aList.add(1, v); s.pop();// blacktrack } else { select an unvisited child u of v; s.push(u); mark u as visited; } } return aList;
Spanning Tree IOI/ACM ICPC Training
Spanning Tree • Given a connected undirected graph G, a spanning tree of G is a subgraph of G that contains all of G’s nodes and enough of its edges to form a tree. v2 v3 v1 v5 v4 Spanning tree Spanning tree is not unique!
DFS spanning tree • Generate the spanning tree edge during the DFS traversal. Algorithm dfsSpanningTree(v) mark v as visited; for (each unvisited node u adjacent to v) { mark the edge from u to v; dfsSpanningTree(u); }
x x x x Example of generating spanning tree based on DFS v2 v3 v1 x v5 v4 G
BFS spanning tree • Similar to DFS, the spanning tree edges can be generated based on BFS traversal.
Minimum Spanning Tree IOI/ACM ICPC Training
Telephone line placing problem • Consider a connected undirected graph where • Each node x represents a country x • Each edge (x, y) has a number which measures the cost of placing telephone line between country x and country y • Problem: connecting all countries while minimizing the total cost • Solution: find a spanning tree with minimum total weight, that is, minimum spanning tree
v2 v3 v1 2 5 Minimum spanning tree 4 3 7 v5 8 v4 Formal definition of minimum spanning tree • Given a connected undirected graph G. • Let T be a spanning tree of G. • cost(T) = eTweight(e) • The minimum spanning tree is a spanning tree T which minimizes cost(T)
v2 v2 v2 v2 v2 v1 v1 v1 v1 v1 2 2 2 2 2 v3 v3 v3 v3 v3 5 5 5 5 5 4 4 4 4 4 3 3 3 3 3 7 7 7 7 7 8 8 8 8 8 v4 v4 v4 v4 v4 v5 v5 v5 v5 v5 Start from v5, find the minimum edge attach to v5 Find the minimum edge attach to v3 and v5 Find the minimum edge attach to v2, v3 and v5 Find the minimum edge attach to v2, v3 , v4 and v5 Prim’s algorithm
Prim’s algorithm (II) Algorithm PrimAlgorithm(v) • Mark node v as visited and include it in the minimum spanning tree; • while (there are unvisited nodes) { • find the minimum edge (v, u) between a visited node v and an unvisited node u; • mark u as visited; • add both v and (v, u) to the minimum spanning tree; }
Bipartite Matching IOI/ACM ICPC Training
Definitions Matching Free Vertex
Definitions • Maximum Matching: matching with the largest number of edges
Definition • Note that maximum matching is not unique.
Intuition • Let the top set of vertices be men • Let the bottom set of vertices be women • Suppose each edge represents a pair of man and woman who like each other • Maximum matching tries to maximize the number of couples!
Applications • Matching has many applications. For examples, • Comparing Evolutionary Trees • Finding RNA structure • … • This lecture lets you know how to find maximum matching.
Alternating Path • Alternating between matching and non-matching edges. a c d e b f h i j g d-h-e: alternating path a-f-b-h-d-i: alternating path starts and ends with free vertices f-b-h-e: not alternating path e-j: alternating path starts and ends with free vertices
Idea • “Flip” augmenting path to get better matching • Note: After flipping, the number of matched edges will increase by 1!
Idea • Theorem (Berge 1975): A matching M in G is maximum iffThere is no augmenting path Proof: • () If there is an augmenting path, clearly not maximum. (Flip matching and non-matching edges in that path to get a “better” matching!)
Proof for the other direction • () Suppose M is not maximum. Let M’ be a maximum matching such that |M’|>|M|. • Consider H = MM’ = (MM’)-(MM’)i.e. a set of edges in M or M’ but not both • H has two properties: • Within H, number of edges belong to M’ > number of edges belong to M. • H can be decomposed into a set of paths. All paths should be alternating between edges in M and M’. • There should exist a path with more edges from M’. Also, it is alternating.
Idea of Algorithm • Start with an arbitrary matching • While we still can find an augmenting path • Find the augmenting path P • Flip the edges in P
Labeling Algorithm • Start with arbitrary matching
Labeling Algorithm • Pick a free vertex in the bottom
Labeling Algorithm • Run BFS
Labeling Algorithm • Alternate unmatched/matched edges
Labeling Algorithm • Until a augmenting path is found
Repeat • Pick another free vertex in the bottom
Repeat • Run BFS
Repeat • Flip
Answer • Since we cannot find any augmenting path, stop!
Overall algorithm • Start with an arbitrary matching (e.g., empty matching) • Repeat forever • For all free vertices in the bottom, • do bfs to find augmenting paths • If found, then flip the edges • If fail to find, stop and report the maximum matching.
Time analysis • We can find at most |V| augmenting paths (why?) • To find an augmenting path, we use bfs! Time required = O( |V| + |E| ) • Total time: O(|V|2 + |V| |E|)
Improvement • We can try to find augmenting paths in parallel for all free nodes in every iteration. • Using such approach, the time complexity is improved to O(|V|0.5 |E|)
Weighted Bipartite Graph 3 4 6 6