Breadth-First Search of Graphs

Similar documents
Algorithms: Lecture 12. Chalmers University of Technology

1 Matchings in Non-Bipartite Graphs

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

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

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

Single Source Shortest Paths

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

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

NATIONAL UNIVERSITY OF SINGAPORE CS3230 DESIGN AND ANALYSIS OF ALGORITHMS SEMESTER II: Time Allowed 2 Hours

Preliminaries. Graphs. E : set of edges (arcs) (Undirected) Graph : (i, j) = (j, i) (edges) V = {1, 2, 3, 4, 5}, E = {(1, 3), (3, 2), (2, 4)}

7.5 Bipartite Matching

CS 4407 Algorithms Lecture: Shortest Path Algorithms

CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk. 10/8/12 CMPS 2200 Intro.

Single Source Shortest Paths

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

directed weighted graphs as flow networks the Ford-Fulkerson algorithm termination and running time

Design and Analysis of Algorithms

Combinatorial Optimization

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

PATH PROBLEMS IN SKEW-SYMMETRIC GRAPHS ANDREW V. GOLDBERG COMPUTER SCIENCE DEPARTMENT STANFORD UNIVERSITY STANFORD, CA

Algorithms and Theory of Computation. Lecture 11: Network Flow

15.1 Matching, Components, and Edge cover (Collaborate with Xin Yu)

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

Bipartite Matchings and Stable Marriage

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

Algorithm Design and Analysis

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

Agenda. Soviet Rail Network, We ve done Greedy Method Divide and Conquer Dynamic Programming

Discrete Optimization 2010 Lecture 3 Maximum Flows

Mathematics for Decision Making: An Introduction. Lecture 13

Algorithm Design and Analysis

6. DYNAMIC PROGRAMMING II

Ma/CS 6b Class 3: Stable Matchings

MINIMALLY NON-PFAFFIAN GRAPHS

6. DYNAMIC PROGRAMMING II. sequence alignment Hirschberg's algorithm Bellman-Ford distance vector protocols negative cycles in a digraph

1 Some loose ends from last time

Connectivity of addable graph classes

Connectivity of addable graph classes

The Max Flow Problem

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

CMPSCI 611: Advanced Algorithms

6.889 Lecture 4: Single-Source Shortest Paths

Internet Routing Example

Introduction to Algorithms

Algorithm Design and Analysis

Running Time. Assumption. All capacities are integers between 1 and C.

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

An 1.75 approximation algorithm for the leaf-to-leaf tree augmentation problem

Fundamental Algorithms 11

COMP251: Bipartite graphs

NP and Computational Intractability

arxiv: v1 [cs.ds] 29 Aug 2015

Shortest Path Algorithms

List of Theorems. Mat 416, Introduction to Graph Theory. Theorem 1 The numbers R(p, q) exist and for p, q 2,

CS675: Convex and Combinatorial Optimization Fall 2014 Combinatorial Problems as Linear Programs. Instructor: Shaddin Dughmi

CS781 Lecture 3 January 27, 2011

Maximum Flow. Reading: CLRS Chapter 26. CSE 6331 Algorithms Steve Lai

MINIMALLY NON-PFAFFIAN GRAPHS

Introduction to Algorithms

Planar Graphs (1) Planar Graphs (3) Planar Graphs (2) Planar Graphs (4)

MA015: Graph Algorithms

1 Matroid intersection

Observation 4.1 G has a proper separation of order 0 if and only if G is disconnected.

A Faster Algorithm for the Maximum Even Factor Problem

Maximum Flow. Jie Wang. University of Massachusetts Lowell Department of Computer Science. J. Wang (UMass Lowell) Maximum Flow 1 / 27

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

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Observation 4.1 G has a proper separation of order 0 if and only if G is disconnected.

ne a priority queue for the nodes of G; ile (! PQ.is empty( )) select u PQ with d(u) minimal and remove it; I Informatik 1 Kurt Mehlhorn

Lecture 4: NP and computational intractability

A Separator Theorem for Graphs with an Excluded Minor and its Applications

CS 241 Analysis of Algorithms

Introduction to Algorithms

Polynomial-Time Reductions

COMP251: Bipartite graphs

Discrete Mathematics, Spring 2004 Homework 9 Sample Solutions

On shredders and vertex connectivity augmentation

The min cost flow problem Course notes for Optimization Spring 2007

Shortest paths: label setting

ALG 5.4. Lexicographical Search: Applied to Scheduling Theory. Professor John Reif

Longest paths in strong spanning oriented subgraphs of strong semicomplete multipartite digraphs

Contents Lecture 4. Greedy graph algorithms Dijkstra s algorithm Prim s algorithm Kruskal s algorithm Union-find data structure with path compression

Lecture 2: Network Flows 1

CSC 373: Algorithm Design and Analysis Lecture 12

Algorithms: COMP3121/3821/9101/9801

INVERSE SPANNING TREE PROBLEMS: FORMULATIONS AND ALGORITHMS

Types of Networks. Internet Telephone Cell Highways Rail Electrical Power Water Sewer Gas

Shortest paths with negative lengths

RECAP How to find a maximum matching?

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

d 2 -coloring of a Graph

6.046 Recitation 11 Handout

Directed Graphs (Digraphs) and Graphs

The min cost flow problem Course notes for Search and Optimization Spring 2004

Recall: Matchings. Examples. K n,m, K n, Petersen graph, Q k ; graphs without perfect matching

arxiv: v1 [cs.ds] 20 Nov 2016

A 1.8 approximation algorithm for augmenting edge-connectivity of a graph from 1 to 2

Ma/CS 6b Class 3: Stable Matchings

8.5 Sequencing Problems

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

Transcription:

Breadth-First Search of Graphs Analysis of Algorithms Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University

Applications of Breadth-First Search of Graphs a) Single Source Shortest Path b) Graph Matching

Reading on Breadth-First Search of Graphs Reading Selection: CLR, Chapter 24

Breadth-First Search Algorithm Input input : undirected graph G = (V,E) with root r V

Breadth-First Search Algorithm initialize : L 0 for each V do visit(v) false LEVEL(0) {r}; visit (r) true while LEVEL(L) {} do begin LEVEL(L+1) {} for each v LEVEL(L) do begin for each {v,u} E s.t. not visit(u) do add u to LEVEL(L+1) visit (u) true od end L L + 1 end

Breadth-First Search Algorithm Output output : LEVEL(0), LEVEL(1),..., LEVEL(L-1) O( V + E ) time cost

Edges in Breadth-First Search All edges {u,v} E have level distance 1 root r 1 Example LEVEL(0) 2 3 4 5 LEVEL(1) 6 7 8 LEVEL(2)

Breadth-First Search Tree Breadth First Search Tree T root r 1 LEVEL(0) ={1} 2 3 4 5 LEVEL(1)={2,3,4,5} 6 7 8 LEVEL(2)={6,7,8}

Single Source Shortest Paths Problem input : digraph G = (V,E) with root r V weighting d: E positive reals problem: For each vertex v, determine D(v) = min length path from root r to v

Dijkstra s Algorithm for Single Source Shortest Paths initialize : output: Q {} for each v V-{r} do D(v) D(r) 0 until no change do choose a vertex u V-Q with minimum D(u) add u to Q for each (u,v) E s.t. v V-Q do D(v) min (D(v), D(u) + d(u,v)) v V D(v) = weight of min. path from r to v

Example Single Source Shortest Paths Problem example 40 50 4 5 root r 1 10 30 2 3 20 100

Example Execution of Dijkstra s Algorithm Q u D(1) D(2) D(3) D(4) D(5) F 1 0 {1} 2 0 10 30 100 {1,2} 3 0 10 30 60 100 {1,2,3} 4 0 10 30 50 100 {1,2,3,4} 5 0 10 30 50 90

Proof of Dijkstra s Algorithm Use induction hypothesis: (1) v V, D(v) is weight of the minimum cost of path p from r to v, where p visits only vertices of Q {v} (2) v Q, D(v) = minimum cost path from r to v basis D(r) = 0 for Q ={r}

Proof of Dijkstra s Algorithm (cont d) induction step if D(u) is minimum for all u V-Q then claim: (1) D(u) is minimum cost of path from r to u in G suppose not: then path p with weight < D(u) and such that p visits a vertex w V-(Q {u}). Then D(w) < D(u), contradiction. (2) is satisfied by D(v) = min (D(v), D(u) + d(u,v)) (u,v) for all v Q {u} E

Time Cost of Dijkstra s Algorithm on a RAM Model Time cost: per iteration "- O(log V ) to find u V-Q # $ with min D(u) # %- O(degree(u)) to update weights Since there are V iterations, Total Time O( V (log V ) + E )

Graph Matching Graph G = (V,E) Graph Matching M is a subset of E if e 1, e 2 distinct edges in M Then they have no vertex in common example Vertex v is matched if v is in an edge of M

Graph Matching Problem Graph Matching Problem: Find a maximum size matching Suppose: G = (V,E) has matching M Goal: find a larger matching

Augmenting Path in G given Graph Matching M An augmenting path p = (e 1, e 2,, e k ) require! begins and ends at " unmatched vertices # " e 1, e 3, e 5,..., e k E-M " % e 2, e 4,..., e k-1 M

Graph Matching (cont d) Initial matching M in G 1 5 2 3 6 7 M = 2 4 8 Augmenting path p = ((5,2), (2,6), (6,4), (4,7), (7,3))

Graph Matching (cont d) New matching M = P M = (P M) (P M) 1 5 2 3 6 7 P M = 3 4 8

Characterization of a Maximum Graph Matching via Lack of Augmented Path Theorem M is maximum matching iff there is no augmenting path relative to M

Proof of Characterization of Maximum Graph Matching (cont d) Proof (1) If M is smaller matching and p is an augmenting path for M, then M P is a matching size > M (2) If M, M' are matchings with M < M'

Claim: M M' contains an augmenting path for M. Proof The graph G' = (V, M M' ) has only paths with edges alternating between M and M'. Repeatedly delete a cycle in G ' (with equal number of edges in M, M ') Since M < M' must eventually get augmenting path remaining for M.

Maximum Matching Algorithm Algorithm input graph G = (V,E) [1] M {} [2] while there exists an augmenting path p relative to M do M M P [3] output maximum matching M

Maximum Matching (cont d) Remaining problem: Find augmenting path Assume weighting d: E R + = pos. reals

Maximum Weighted Matching Algorithm Assume weighting d: E R + = positive reals Theorem Let M be maximum weight among matchings of size M. Let P be an augmenting path for M of maximum weight. Then matching M P is of maximum weight among matchings of size M +1.

Proof of Maximum Weighted Matching Algorithm Proof Let M' be any maximum weight matching of size M + 1. Consider the graph G' = (V, M M' ). Then the maximum weight augmenting path p in G' gives a matching M P of the same weight as M'.

Bipartite Graph Bipartite Graph G = (V,E) V = V V, V V = Φ 1 2 1 2 E is a subset of { {u,v} u V, v V } 1 2 V 1 V 2

Breadth-First Search Algorithm for Augmented Path Assume G is bipartite graph with matching M. Use Breadth-First Search: LEVEL(0) = some unmatched vertex r for odd L > 0, LEVEL(L) = {u {v,u} E M For even L > 0, LEVEL(L) = {u {v,u} M when v LEVEL(L -1) and when u in no lower level} when v LEVEL(L -1) and u in no lower level}

Proof of Breadth-First Search Algorithm for Augmented Path Cases (1) If for some odd L >0, LEVEL(L) contains an unmatched vertex u then the Breadth First Search tree T has an augmenting path from r to u (2) Otherwise no augmenting path exists, so M is maximal.

Finding a Maximal Matching in a Bipartite Graph Theorem If G = (V,E) is a bipartite graph, Then the maximum matching can be constructed in O( V ( V + E ) ) time. Proof Each stage requires O( V + E ) time for Breadth First Search construction of augmenting path.

Generalizations of Matching Algorithm Generalizations:!(1) Compute Edge Weighted Maximum " Matching " #(2) Edmonds gives a polynomial time " algorithm for maximum matching of " "$ any graph

Computing Augmented Paths in General Graphs Let M be matching in general graph G Fix starting vertex r to be an unmatched vertex Let vertex v V be even if even length augmenting path from r to v and odd if odd length augmenting path from r to v.

Why Algorithm for Augmented Paths in Bipartite Graphs does not work for General Graphs Case Case G is bipartite G is no not vertex is both even and odd bipartite some vertices may be both even and odd!

Edmond s Algorithm for Augmented Paths in General Graphs r STEM P is augmenting path from r to v is subpath of p from r to v t STEM v even vertex BLOSSOM is subpath of p from v to w plus edge {w,v} Base w even vertex BLOSSOM t w, v Shrink Blossom t' t'

Blossom Shrinking Maintains the Existence of Augmented Paths Theorem If G' is formed from G by shrinking of blossom B, then G contains an augmenting path iff G' does.

Proof of Blossom Shrinking Proof (1) If G' contains an augmenting path p, then if p visits blossom B we can insert an augmenting subpath p' within blossom into p to get a new augmenting path for G (2) If G contains an augmenting path, then apply Edmond s blossom shrinking algorithm to find an augmenting path in G'.

Edmond s Blossom Shrinking Algorithm input Graph G=(V,E) with matching M initialization E = {(v,w), (w,v) {v,w} E} Main Ideas of Edmond s algorithm: The algorithm incrementally constructs a forest of trees whose paths are partial augmenting paths. If a cycle is formed, contract it to a vertex. Try to link two partial augmenting paths of distinct trees to form a full augmenting path.

Edmond s Blossom Shrinking Algorithm (cont d) Note: We will let P(v) = parent of vertex v #[0] for each unmatched vertex v V $ $ do label v as "even" $ [1] for each matched v V $ % do label v "unreached" set p(v) = $ if v is matched edge {v,w} $ $ then mate (v) w $ & od null

Main Loop Edmond s Main Loop: Choose an unexplored edge (v,w) E where v is "even" ( if none exists, then terminate and output current matching M, since there is no augmenting path)

Main Loop (cont d) Case 1 if w is odd then do nothing. Case 2 if w is unreached and matched then set w odd and set mate (w) even Set P(w) v, P(mate (w)) w even odd even v w mate(w)

Main Loop (cont d) Case 3 if w is even and v, w are in distinct trees T, T' then output augmenting path p from root of T to v, through {v,w}, in T' to root. Unmatched root T T' Unmatched root even v Unmatched edge w even

Main Loop (cont d) Case 4 w is even and v,w in same tree T then {v,w} forms a blossom B containing all vertices which are both (i) a descendant of LCA(v,w) and (ii) an ancestor of v or w where LCA(v,w) = least common ancestor of v,w in T

Main Loop (cont d) LCA (v,w) * v Blossom w * Shrink all vertices of B to a single vertex b. Define p(b) = p(lca(v,w)) and p(x) = b for all x B

Proof Edmond s blossom-shrinking algorithm succeeds Lemma Edmond s blossom-shrinking algorithm succeeds iff an augmenting path in G Proof Uses an induction on blossom shrinking stages

Time Bounds for Matching in General Graphs Edmond s blossom-shrinking algorithm costs time O(n 4 ) [Gabow and Tarjan] implement in time O(nm) all O(n) stages of matching algorithmtaking O(m) time per stage for blossom shrinking [Micali and Vazirani] using network flow to find augmented paths and reduce time to O(n 1/2 m) for unweighted matching in general graphs

Breadth-First Search of Graphs Analysis of Algorithms Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University