IOI/ACM ICPC Training 4 June 2005.

Slides:



Advertisements
Similar presentations
Great Theoretical Ideas in Computer Science
Advertisements

Great Theoretical Ideas in Computer Science for Some.
CS202 - Fundamental Structures of Computer Science II
Great Theoretical Ideas in Computer Science.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity.
Chapter 10: Iterative Improvement Simplex Method The Design and Analysis of Algorithms.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
V Spanning Trees Spanning Trees v Minimum Spanning Trees Minimum Spanning Trees v Kruskal’s Algorithm v Example Example v Planar Graphs Planar Graphs v.
All Pair Shortest Path IOI/ACM ICPC Training June 2004.
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
Bipartite Matching. Unweighted Bipartite Matching.
Network Flows Chun-Ta, Yu Graduate Institute Information Management Dept. National Taiwan University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graphs Spring 2016CS202 - Fundamental Structures of Computer Science II1 Initially prepared by Dr. Ilyas Cicekli; improved by various Bilkent CS202 instructors.
Hamiltonian Graphs Graphs Hubert Chan (Chapter 9.5)
Graphs Chapter 20.
CS202 - Fundamental Structures of Computer Science II
Chapter 10 Iterative Improvement
Introduction to Algorithms
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
CS202 - Fundamental Structures of Computer Science II
Hamiltonian Graphs Graphs Hubert Chan (Chapter 9.5)
Algorithms and Networks
Lecture 16 Bipartite Matching
Maximum Flow - Best algorithms
Great Theoretical Ideas in Computer Science
Lecture 12 Algorithm Analysis
Introduction to Graphs
CS201: Data Structures and Discrete Mathematics I
Depth-First Search.
Algorithms and Complexity
CS202 - Fundamental Structures of Computer Science II
Various Graph Algorithms
More Graph Algorithms.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Discrete Mathematics for Computer Science
CSE 421: Introduction to Algorithms
Graphs Chapter 13.
CSE 373 Data Structures and Algorithms
Algorithms and Data Structures Lecture XII
Spanning Trees.
Lectures on Graph Algorithms: searching, testing and sorting
Graphs.
Lecture 12 Algorithm Analysis
CSE 421: Introduction to Algorithms
Richard Anderson Autumn 2016 Lecture 5
Minimum Spanning Trees
Richard Anderson Winter 2009 Lecture 6
Spanning Trees Longin Jan Latecki Temple University based on slides by
GRAPHS Lecture 17 CS2110 Spring 2018.
Lecture 12 Algorithm Analysis
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Lecture 5 Graph Theory
GRAPH – Definitions A graph G = (V, E) consists of
Richard Anderson Autumn 2015 Lecture 6
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Minimum Spanning Trees
Presentation transcript:

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!

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 2 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

Topological order b d a c e 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 b d e c a

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;

Example b d e c a b e c a b c a b a a d has no successor! Choose d! Both b and e have no successor! Choose e! b e c a Both b and c have no successor! Choose c! b c a Only b has no successor! Choose b! b a a Choose a! The topological order is a,b,c,e,d

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 Tell the students some application of spanning tree. 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); }

Example of generating spanning tree based on DFS stack v3 v2 v3, v2 v1 v3, v2, v1 backtrack v4 v3, v2, v4 v5 v3, v2, v4 , v5 empty v2 v3 v1 x x x x 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

Formal definition of minimum spanning tree Given a connected undirected graph G. Let T be a spanning tree of G. cost(T) = eTweight(e) The minimum spanning tree is a spanning tree T which minimizes cost(T) v1 v4 v3 v5 v2 5 2 3 7 8 4 Minimum spanning tree

Prim’s algorithm v2 v1 v4 v3 v5 5 2 3 7 8 4 v2 v1 v4 v3 v5 5 2 3 7 8 4 Start from v5, find the minimum edge attach to v5 v2 v1 v4 v3 v5 5 2 3 7 8 4 Find the minimum edge attach to v3 and v5 v2 v1 v4 v3 v5 5 2 3 7 8 4 Find the minimum edge attach to v2, v3 and v5 v2 v1 v4 v3 v5 5 2 3 7 8 4 v2 v1 v4 v3 v5 5 2 3 7 8 4 v2 v1 v4 v3 v5 5 2 3 7 8 4 Find the minimum edge attach to v2, v3 , v4 and v5

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

Unweighted Bipartite Matching

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. f g h i j 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 iff There 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 = MM’ = (MM’)-(MM’) 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

Augmenting Tree

Flip!

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

Weighted Matching Score: 6+3+1=10 3 4 6 6

Maximum Weighted Matching Score: 6+1+1+1+4=13 3 4 6 6

Augmenting Path (change of definition) Any alternating path such that total score of unmatched edges > that of matched edges The score of the augmenting path is Score of unmatched edges – that of matched edges 3 4 6 6 Note: augmenting path need not start and end at free vertices!

Idea for finding maximum weight matching Theorem: Let M be a matching of maximum weight among matchings of size |M|. If P is an augmenting path for M of maximum weight, Then, the matching formed by augmenting M by P is a matching of maximum weight among matchings of size |M|+1.

Overall Algorithm Start with an empty matching Repeat forever Find an augmenting path P with maximum score If the score > 0, then flip the edges Otherwise, stop and report the maximum weight matching.

Time analysis The same! Time required = O(|V|2 + |V| |E|)

Stable Marriage Problem IOI/ACM ICPC Training

Stable Marriage Problem Given N men and N women, each person list in order of preference all the people of the opposite sex who would like to marry. Problem: Engage all the women to all the men in such a way as to respect all their preferences as much as possible.

Stable? A set of marriages is unstable if two persons who are not married, but both of them prefer each other than their spouses E.g. Suppose we have A1 B3 C2 D4 E5. This is unstable since A prefer 2 more than 1 2 prefer A more than C A B C D E 2 5 1 3 4

Naïve solution Starting from a feasible solution. Check if it is stable. If yes, done! If not, remove an unstable couple. Is this work?

Naïve solution (2) Does not work! E.g. A1 B3 C2 D4 E5 A2 B3 C1 D4 E5

Solution Let X be the first man. X proposes to the best woman in the remaining on his list. (Initially, the first woman on his list!) If α is not engaged Pair up (X, α). Then, set X=next man and goto 1. If α prefers X more than her fiancee Y, Pair up (X, α). Then, set X=Y and goto 1. Goto 1

Example A B C D E 2 1 2 1 5 5 2 3 3 3 1 3 5 2 A B C D E 2 5 1 3 4 4

Time analysis If there are N men and N women, O(N2) time

Planar Graph IOI/ACM ICPC Training

Planar graph A graph is planar if we can draw the graph without crossing. K4 K4

Non-planar Below shows two non-planar graphs K3,3 K5

Theorem for planar graph A graph G is non-planar if and only if G contains K5 or K3,3.

Question How to check if a graph contains cycle or not? Consider a set S of fixed length sequences, says, {ACG, ATC, CAT, CGC, GCA}. S represents the string ACGCATC. Given any set S, can you give an algorith to determine if it represents a string or not?

Questions Please give an O(|V|+|E|) time algorithm to recover the Euler path Implement the spanning tree algorithm Please implement the labeling algorithm for maximum matching. Please give the pseudo-code for the stable marriage problem. Implement it.