Fundamental Algorithms 11

Similar documents
CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Odd independent transversals are odd

8. INTRACTABILITY I. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley. Last updated on 2/6/18 2:16 AM

NP-complete Problems

Algorithms: Lecture 12. Chalmers University of Technology

Polynomial-Time Reductions

CS 4407 Algorithms Lecture: Shortest Path Algorithms

8.5 Sequencing Problems

Chapter 8. NP and Computational Intractability

Breadth-First Search of Graphs

Graph Theory. Thomas Bloom. February 6, 2015

Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

NP and Computational Intractability

Algorithms and Theory of Computation. Lecture 22: NP-Completeness (2)

M-saturated M={ } M-unsaturated. Perfect Matching. Matchings

GRAPH ALGORITHMS Week 3 (16-21 October 2017)

Part V. Matchings. Matching. 19 Augmenting Paths for Matchings. 18 Bipartite Matching via Flows

CS 138b Computer Algorithm Homework #14. Ling Li, February 23, Construct the level graph

More on NP and Reductions

Even Cycles in Hypergraphs.

CS 301: Complexity of Algorithms (Term I 2008) Alex Tiskin Harald Räcke. Hamiltonian Cycle. 8.5 Sequencing Problems. Directed Hamiltonian Cycle

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Computational Intractability 2010/4/15. Lecture 2

Analysis of Algorithms. Outline. Single Source Shortest Path. Andres Mendez-Vazquez. November 9, Notes. Notes

Direct Proof Universal Statements

arxiv: v1 [math.co] 13 May 2016

Equivalence of DFAs and NFAs

5. Partitions and Relations Ch.22 of PJE.

8.5 Sequencing Problems. Chapter 8. NP and Computational Intractability. Hamiltonian Cycle. Hamiltonian Cycle

Lecture 4: NP and computational intractability

Part V. Intractable Problems

Algorithms: COMP3121/3821/9101/9801

Carmen s Core Concepts (Math 135)

Flows and Cuts. 1 Concepts. CS 787: Advanced Algorithms. Instructor: Dieter van Melkebeek

Out-colourings of Digraphs

CHAPTER 1. Relations. 1. Relations and Their Properties. Discussion

MATH 2200 Final Review

Technische Universität München, Zentrum Mathematik Lehrstuhl für Angewandte Geometrie und Diskrete Mathematik. Combinatorial Optimization (MA 4502)

REU 2007 Transfinite Combinatorics Lecture 9

Lecture 2: Network Flows 1

Design and Analysis of Algorithms

4/30/14. Chapter Sequencing Problems. NP and Computational Intractability. Hamiltonian Cycle

Chapter 8. NP and Computational Intractability. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Notes 6 : First and second moment methods

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Graph G = (V, E). V ={vertices}, E={edges}. V={a,b,c,d,e,f,g,h,k} E={(a,b),(a,g),( a,h),(a,k),(b,c),(b,k),...,(h,k)}

10.4 The Kruskal Katona theorem

Multi-coloring and Mycielski s construction

Coloring Vertices and Edges of a Path by Nonempty Subsets of a Set

The Small Progress Measures algorithm for Parity games

Automata Theory. Lecture on Discussion Course of CS120. Runzhe SJTU ACM CLASS

Lecture 5: Efficient PAC Learning. 1 Consistent Learning: a Bound on Sample Complexity

Algorithm Design and Analysis

Shortest paths with negative lengths

Properly colored Hamilton cycles in edge colored complete graphs

Midterm 1. Your Exam Room: Name of Person Sitting on Your Left: Name of Person Sitting on Your Right: Name of Person Sitting in Front of You:

α-acyclic Joins Jef Wijsen May 4, 2017

A note on three water cups

REALIZING TOURNAMENTS AS MODELS FOR K-MAJORITY VOTING

CSE 202 Homework 4 Matthias Springer, A

9. PSPACE 9. PSPACE. PSPACE complexity class quantified satisfiability planning problem PSPACE-complete

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

CSE 421 NP-Completeness

arxiv: v1 [math.co] 2 Dec 2013

9. PSPACE. PSPACE complexity class quantified satisfiability planning problem PSPACE-complete

Algorithms Exam TIN093 /DIT602

K 4 -free graphs with no odd holes

Lecture 24: April 12

Chapter 7. Network Flow. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

NP Completeness. CS 374: Algorithms & Models of Computation, Spring Lecture 23. November 19, 2015

Cross-Intersecting Sets of Vectors

EXERCISES SHORTEST PATHS: APPLICATIONS, OPTIMIZATION, VARIATIONS, AND SOLVING THE CONSTRAINED SHORTEST PATH PROBLEM. 1 Applications and Modelling

On improving matchings in trees, via bounded-length augmentations 1

HW 4 SOLUTIONS. , x + x x 1 ) 2

NP-Complete Reductions 2

Algebraic Methods in Combinatorics

On Variable-Weighted 2-SAT and Dual Problems

P and NP. Inge Li Gørtz. Thank you to Kevin Wayne, Philip Bille and Paul Fischer for inspiration to slides

Scribes: Po-Hsuan Wei, William Kuzmaul Editor: Kevin Wu Date: October 18, 2016

Problem set 1. (c) Is the Ford-Fulkerson algorithm guaranteed to produce an acyclic maximum flow?

College of Computer & Information Science Fall 2009 Northeastern University 22 September 2009

Relations Graphical View

Algorithms: COMP3121/3821/9101/9801

NP-Complete Problems

The Lovász Local Lemma : A constructive proof

Ngày 20 tháng 7 năm Discrete Optimization Graphs

Coloring Vertices and Edges of a Path by Nonempty Subsets of a Set

Trees. A tree is a graph which is. (a) Connected and. (b) has no cycles (acyclic).

Ma/CS 6a Class 28: Latin Squares

CSE613: Parallel Programming, Spring 2012 Date: May 11. Final Exam. ( 11:15 AM 1:45 PM : 150 Minutes )

CS60007 Algorithm Design and Analysis 2018 Assignment 1

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

6.045 Final Exam Solutions

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

A Little Logic. Propositional Logic. Satisfiability Problems. Solving Sudokus. First Order Logic. Logic Programming

Unmixed Graphs that are Domains

More NP-Complete Problems

(x 1 +x 2 )(x 1 x 2 )+(x 2 +x 3 )(x 2 x 3 )+(x 3 +x 1 )(x 3 x 1 ).

IMA Preprint Series # 2385

1.3 Vertex Degrees. Vertex Degree for Undirected Graphs: Let G be an undirected. Vertex Degree for Digraphs: Let D be a digraph and y V (D).

Transcription:

Technische Universität München WS 2013/14 Institut für Informatik Worksheet Scientific Computing in Computer Science 20.01.2014 Fundamental Algorithms 11 Exercise 1 Hypergraphs A hypergraph extends the concept of a graph in the sense that edges are allowed to connect an arbitrary number of vertices (instead of exactly two). Hence, a hypergraph is defined as a tuple (V, H), where V is a set of vertices and H is a set of hyperedges, where H P(H) \ { }, with P(H) the power set (i.e., the set of all possible subsets) of H. Let s assume a hypergraph where V is a set of authors, and each hyperedge h H contains all authors of a specific scientific article. Exercise 1a) Give a suitable definition of the concept of a path in a hyperedge. We may define that a path (of length n) exists between two vertices v and w in a hypergraph, if: We have a sequence of hyperedges h 1, h 2, h 3,..., h n and a sequence of vertices v 0, v 1, v 2,..., v n (where v = v 0 and w = v n ) such that v 0, v 1 h 1 and v 1, v 2 h 2 and... v n 1, v n h n Exercise 1b) Given is the hypergraph S = (V S, H S ) of all scientific articles. The Erdös number Er(a) of an author a V S is defined as the length of the shortest path in S that connects the specific vertex e V (e corresponds to the author Paul Erdös) to a. Write down an algorithm to determine Er(a). For a (non-directed, non-weighted) graph, breadth-first traversal will find the shortest path from a given root node to any reachable node in the graph. Hence, we adapt breadth-first search for hypergraphs. Instead of using an array Mark that just labels vertices as already visited, we use 1

an array Erd that contains the Erdös number of a already visited vertices (non-visited vertices will be initialized to contain 1 in the beginning). BF Erdos ( e : HyperNode, x : HyperNode, n : Integer ) : Integer { / / Input : e i s t h e v e r t e x t h a t c o r r e s p o n d s t o Erdos / / x i s t h e v e r t e x t h a t c o r r e s p o n d s t o t h e a u t h o r / / n i s t h e number o f nodes o f t h e graphs / / Output : Erdos number o f x ( or 1 i f no path i f found ) / / use an a r r a y Erd t o mark v i s i t e d nodes and t o s t o r e t h e i r Erdos number Array Erd [ 1.. n ] ; for i from 1 to n do { Erd [ i ] = 1 } ; Erd [ e. key ] = 0 ; / / Erdos has Erdos number 0 / / i n i t i a l i z e a queue a c t i v e f o r b r e a d t h f i r s t t r a v e r s a l Queue a c t i v e = {e } ; / / i n i t i a l node i s e = Erdos } while a c t i v e <> {} do / / t a k e f i r s t v e r t e x from queue : v = remove ( a c t i v e ) ; / / i f v e r t e x i s a u t h o r x then r e t u r n Erdos number i f v = x then return Erd [ v. key ] ; / / d e t e r m i n e a l l a u t h o r s with a j o i n t p u b l i c a t i o n coauthors = { } ; / / s t a r t with empty s e t f o r a l l a r t i c l e A u t h o r s in v. hyperedges do coauthors = coauthors union a r t i c l e A u t h o r s / / expand graph s e a r c h by a l l c o a u t h o r s f o r a l l author in coauthors do i f Erd [ author. key ] < 0 then / / s e t Erdos number o f a u t h o r Erd [ author. key ] = Erd [ v. key ] + 1 ; append ( active, author ) ; end i f ; end while ; / / a t t h i s p o i n t, no p ath has been found > no Erdos number : return 1; In this algorithm, HyperNode needs to be a suitable data structure to represent a node of a hypergraph. Here, we assume that a HyperNode represent one vertex and stores the set of hyperedges that contain this vertex (or a reference to these hyperedges). Each hyperedge is a set of vertices. Exercise 1c) Try to formulate the problem of 1b) as a graph problem! 2

There is a simple solution, once we realize that it is insignificant for the Erdös number which articles are actually responsible for the connection. Hence, we may define a graph V S, E S, where two authors a, b V S are connected by an edge, i.e. (a, b) E S, iff there exists a hyperedge H S with a H S and b H S. Then we can use the regular breadth-first traversal on graphs. However, scientists who are interested in their Erdös number actually do want to know which articles define the connection, so we need to set up a different graph (V, E): the nodes are given as V := V S H S, i.e., each author and each articles define a vertex; E contains an edge between an author a V S and an article p H S, iff a was an author of p, i.e., a p. Now, a shortest-path search in the resulting graph (using breadth-first search) will deliver the Erdös number and the connecting papers. Note that the constructed graph is an example for a bipartite graph (see Exercise 2). Exercise 2 Bipartite Graphs The following exercise is based on the concept of so-called bipartite graphs: A graph (V, E) is called bipartite, if there exist V 0 and V 1 with V 0 V and V 1 = V \ V 0, and for all (v, w) E there is either v V 0 and w V 1, or w V 0 and v V 1. To put it simpler: for a bipartite graph, it is possible to attribute each node v V with one of two colors, say red and black, such that any edge e E will connect a red and a black node (and no edge will connect edges of the same color). Exercise 2a Give a prove to the following claim: If a graph (V, E) is bipartite, then it cannot contain an odd cycle (i.e., a cycle of odd length). We proof this by contradiction: Assume that a bipartite graph contains a cycle (v 0, v 1 ), (v 1, v 2 ),..., (v n 1, v n = v 0 ), where n is odd. Assume that v 0 is coloured black, then v 1 has to be red (as it is connected to the black v 0 ), v 2 has to be black (as it is connected to the red v 1 ), etc. Thus all v 2i are black and all v 2i+1 are red. However, if n is odd, i.e. n = 2i + 1 for some i, then v n is red, and thus cannot be equal to v 0, which is black. This contradicts our initial assumption. 3

Exercise 2b Try to find an algorithm that tests whether a given graph is bipartite. Hint: you can build such an algorithm by extending one of the graph traversals we discussed in the lecture! We can change breadth-first traversal into a colouring algorithm. Assume that we have n nodes with distinct key values {1,..., n}, then we can use an array M to store the colouring status: 0 means uncoloured, 1 is for black, and 2 for red. B F b i p a r t i t e ( x : Node) { b i p a r t i t e = true ;! u s e s queue o f a c t i v e nodes Queue a c t i v e = { x } ; Mark [ x. key ] = 1 ; while a c t i v e <> {} do! remove f i r s t node from queue V = remove ( a c t i v e ) ;! d e t e r m i n e t h e o p p o s i t e c o l o u r o f V: i f Mark [V. key ]=1 then newcolour=2 else newcolour=1 end i f ;! v i s i t a l l nodes W c o n n e c t t o V by an edge (V,W) : f o r a l l (V,W) in V. edges do! a s s i g n a c o l o u r t o W, i f i t i s s t i l l u n c o l o u r e d i f Mark [W. key ] = 0 then Mark [W. key ] = newcolour ;! c h e c k a l l nodes c o n n e c t e d t o W f o r a c o l o u r c o n f l i c t : f o r a l l (W, Y) in V. edges do i f Mark [ y. key ] = newcolour then b i p a r t i t e = f a l s e ;! f o r BFT : append W t o queue o f a c t i v e nodes : append ( a ctive, W) ; end i f ; end while ; return b i p a r t i t e ; } Note: instead of the assignment bipartite = false, we could also immediately return false, and thus not necessarily traverse the entire graph (and thus be much more efficient). However, we implement a full traversal here, as we will discuss a quite similar algorithm in the next exercise. 4

Exercise 2c Try to give a prove for the following claim (using the algorithm from Exercise 2): If a graph (V, E) is not bipartite, then it will contain an odd cycle. The algorithm of Exercise 3 contains a standard breadth-first search; we can simply change it by assigning a distance to the starting node s to each node, instead of marking it as black or red. We obtain the following algorithm: BFdistance ( x : Node) {! a r r a y Mark c o n t a i n s v a l u e 1 in e v e r y e l e m e n t a t s t a r t b i p a r t i t e = true ;! u s e s queue o f a c t i v e nodes Queue a c t i v e = { x } ; Mark [ x. key ] = 0 ; while a c t i v e <> {} do! remove f i r s t node from queue V = remove ( a c t i v e ) ;! d e t e r m i n e t h e o p p o s i t e c o l o u r o f V: d i s t := Mark [V. key ] ;! v i s i t a l l nodes W c o n n e c t t o V by an edge (V,W) : f o r a l l (V,W) in V. edges do! a s s i g n a d i s t a n c e t o W, i f i t i s s t i l l u n c o l o u r e d i f Mark [W. key ] = 1 then Mark [W. key ] = d i s t +1;! f o r BFT : append W t o queue o f a c t i v e nodes : append ( a ctive, W) ; end i f ; end while ; return b i p a r t i t e ; } Note: as 0 is a valid distance (which is correct for the start node x), we have to use a different value (which is -1) to mark nodes that have not yet been visited. Due to the identical breadth-first traversal structure of algorithms BFbipartite and BFdistance, it is obvious that nodes with an odd distance are marked red, while nodes with an even distance are marked black. We now have two opposite situations: 1. There is no edge in the graph that connects two nodes with even distance (which would both be marked black by algorithm 2), and no edge that connects two nodes with odd distance (which would be red). In that case, we have a bipartite graph. 2. There is an edge in the graph that connects two nodes with odd or two nodes with even 5

distances. As both nodes are connected to the starting node (via different paths) the edge between them generates a cycle. The length of this cycle is 1 plus either the sum of two odd numbers or the sum of two even numbers. In any case, the cycle length is an odd number. Hence, a graph can either be bipartite or have an odd cycle. Reference for Exercises 1a 1c: Kleinberg, Tardos: Algorithm Design, Pearson Education, 2006. Exercise 2d Consider the following graph obtained from a Cartesian discretization mesh. Is this a bipartite graph? x i,j+1 x i 1,j x i,j x i+1,j x i,j 1 h y h x This algorithm is, of course, inspired by the so-called red-black ordering frequently used in numerical algorithms (such as the red-black Gauß-Seidel relaxation). If we colour the grid points in a red-black checkerboard fashion. then the four direct (left, right, top, down) neighbours of each black node will be red (and vice versa). Consider a system of equations where unknowns (the x ij in the image) only depend on their direct neighbour. For relaxation methods, an important consequence is then that for an update of a red unknown x ij, only the values of black unknowns (in addition to x ij itself) are accessed. Hence, all red unknowns may be updated in parallel. 6