Unweighted Graphs & Algorithms

Similar documents
Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

LECTURE 17 GRAPH TRAVERSALS

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Weighted Graph Algorithms Presented by Jason Yuan

Data Structures and Algorithms for Engineers

Outline. Graphs. Divide and Conquer.

Homework 5: Graphs, Minimum Spanning Trees, and Dijkstra Shortest-Path

Chapter 9 Graph Algorithms

Homework Assignment #3 Graph

22.3 Depth First Search

Algorithm Design (8) Graph Algorithms 1/2

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

Konigsberg Bridge Problem

CS302 - Data Structures using C++

Chapter 9 Graph Algorithms

Graph. Vertex. edge. Directed Graph. Undirected Graph

Lecture 3: Graphs and flows

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

CSE 100: GRAPH ALGORITHMS

Test 1 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 2 points each t 1

Fundamental Algorithms

Graph Algorithms Using Depth First Search

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CS 310 Advanced Data Structures and Algorithms

Lecture 6 Basic Graph Algorithms

- Logic and Algorithms - Graph Algorithms

PATH FINDING AND GRAPH TRAVERSAL

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

INF280 Graph Traversals & Paths

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

Graph Traversal. Section Dr. Chris Mayfield. Oct 24, Department of Computer Science James Madison University

( ) n 3. n 2 ( ) D. Ο

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

Graph Algorithms (part 3 of CSC 282),

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory

CS350: Data Structures Dijkstra s Shortest Path Alg.

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

GRAPHICAL ALGORITHMS. UNIT _II Lecture-12 Slides No. 3-7 Lecture Slides No Lecture Slides No

ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Lecture 10 Graph Algorithms

LECTURE 11 TREE TRAVERSALS

Problem Score Maximum MC 34 (25/17) = 50 Total 100

Graph Representations and Traversal

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

Applications of BDF and DFS

Graph Algorithms. Definition

CS 206 Introduction to Computer Science II

Breadth First Search. cse2011 section 13.3 of textbook

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

Outline. Introduction. Representations of Graphs Graph Traversals. Applications. Definitions and Basic Terminologies

Elementary Graph Algorithms CSE 6331

CS 512: Comments on Graph Search 16:198:512 Instructor: Wes Cowan

Basic Graph Definitions

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

Undirected Graphs. Hwansoo Han

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

INF2220: algorithms and data structures Series 7

Graphs & Digraphs Tuesday, November 06, 2007

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

CSI 604 Elementary Graph Algorithms

implementing the breadth-first search algorithm implementing the depth-first search algorithm

國立清華大學電機工程學系. Outline

Lecture 9 Graph Traversal

Algorithms Sequential and Parallel: A Unified Approach; R. Miller and L. Boxer 3rd Graph Algorithms

Elementary Graph Algorithms: Summary. Algorithms. CmSc250 Intro to Algorithms

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

(Re)Introduction to Graphs and Some Algorithms

CS 125 Section #5 Graph Traversal and Linear Programs October 6, 2016

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

Agenda. Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

Index. stack-based, 400 A* algorithm, 325

GRAPHS (Undirected) Graph: Set of objects with pairwise connections. Why study graph algorithms?

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

CS4800: Algorithms & Data Jonathan Ullman

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

CS 2412 Data Structures. Chapter 9 Graphs

Total Score /1 /20 /41 /15 /23 Grader

n 2 ( ) ( ) + n is in Θ n logn

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

CSE 100: GRAPH SEARCH

GRAPHS Lecture 17 CS2110 Spring 2014

Test points UTA Student ID #

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

Transcription:

Unweighted Graphs & Algorithms Zachary Friggstad Programming Club Meeting

References Chapter 4: Graph (Section 4.2) Chapter 22: Elementary Graph Algorithms

Graphs Features: vertices/nodes/dots and edges/links/lines between vertices. Sometimes there are labels or numeric values associated with the items in the graph. The edges may or may not have directions. We will discuss over a couple of meetings how to model graphs and some graph algorithms.

Unweighted Graphs Note Many code snippets here use C++11 features. Compile with the flag -std=c++11 if using g++. Throughout, n = # vertices, m = # edges.

Unweighted Graphs Note Many code snippets here use C++11 features. Compile with the flag -std=c++11 if using g++. Throughout, n = # vertices, m = # edges. Adjacency List Representation of a Graph // without c ++11 you may need to add a space between >> typedef v e c t o r <v e c t o r <int>> graph ;... graph g ( n ) ; // create a graph with n vertices g [ u ]. push back ( v ) ; // add v as a neighbour of u For undirected graphs, just add both directions of an edge (u, v). Requires Θ(n + m) space.

Problem: Reachability Give a vertex v in a graph, find all vertices u that v can reach. That is, we can reach u by following a sequence of edges (in the right direction, if the graph is directed). Example The top, left vertex can only reach the other vertices in the square.

Depth-First Search Find all vertices reachable from vertex v. // the vertices that are reached in the search v e c t o r <bool> r e a c h e d ( n, false ) ; graph g ; void d f s ( int u ) { if (! r e a c h e d [ u ] ) { r e a c h e d [ u ] = true ; for ( auto w : g [ u ] ) d f s (w ) ; } }... d f s ( v ) ;

Depth-First Search Find all vertices reachable from vertex v. // the vertices that are reached in the search v e c t o r <bool> r e a c h e d ( n, false ) ; graph g ; void d f s ( int u ) { if (! r e a c h e d [ u ] ) { r e a c h e d [ u ] = true ; for ( auto w : g [ u ] ) d f s (w ) ; } }... d f s ( v ) ; If we record the vertex that discovered u, we can reconstruct paths. Runs in O(n + m) time.

Other Applications of DFS: Topological Sorting Order the vertices so all edges point left-to-right. v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8

Other Applications of DFS: Topological Sorting Order the vertices so all edges point left-to-right. v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8 Impossible to do if there is a cycle. Otherwise, the following works.

Other Applications of DFS: Topological Sorting Order the vertices so all edges point left-to-right. v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8 Impossible to do if there is a cycle. Otherwise, the following works. Begin a DFS. Just before returning from a recursive call (i.e. just after the for loop) push back the vertex u to the end of a vector.

Other Applications of DFS: Topological Sorting Order the vertices so all edges point left-to-right. v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8 Impossible to do if there is a cycle. Otherwise, the following works. Begin a DFS. Just before returning from a recursive call (i.e. just after the for loop) push back the vertex u to the end of a vector. Repeat, starting with an unvisited vertex each time, until all vertices are visited.

v e c t o r <int> o r d e r ; // initially empty void t o p o s o r t ( int u ) { if (! r e a c h e d [ u ] ) { r e a c h e d [ u ] = true ; for ( auto w : g [ u ] ) t o p o s o r t (w ) ; o r d e r. push back ( u ) ; } }... for ( int u = 0 ; u < n ; u++) if (! r e a c h e d [ u ] ) t o p o s o r t ( u ) ; r e v e r s e ( o r d e r. b e g i n ( ), o r d e r. end ( ) ) ; //# include <algorithm >

If u is ordered after w for some edge (u, w), it must be that the recursive call with w was on the call stack when u was being processed. (Why?) done searching u top of call stack start of dfs w

If u is ordered after w for some edge (u, w), it must be that the recursive call with w was on the call stack when u was being processed. (Why?) done searching u top of call stack start of dfs w If w is on the call stack when u is being processed, there is a path from w to u. Completing this path with the edge (u, w) yields a cycle.

If u is ordered after w for some edge (u, w), it must be that the recursive call with w was on the call stack when u was being processed. (Why?) done searching u top of call stack start of dfs w If w is on the call stack when u is being processed, there is a path from w to u. Completing this path with the edge (u, w) yields a cycle. Thus If the graph has no cycles, this will topologically sort all vertices.

Articulation Points & Bridges An articulation point in an undirected, connected graph is a vertex whose removal leaves a disconnected graph. A bridge is an edge whose removal leaves a disconnected graph.

Articulation Points & Bridges An articulation point in an undirected, connected graph is a vertex whose removal leaves a disconnected graph. A bridge is an edge whose removal leaves a disconnected graph. Can find all bridges and articulation points in O(n + m) time via DFS.

A bridge will always be a tree edge in a DFS (actually, in any spanning tree): one that is expanded along in the search. start u bridge Picture: no edge of a descendent of u in the search reached a non-descendent. So the parent edge of u is a bridge.

Run a DFS, record the order the vertices were discovered. Return the earliest discovery time of any vertex adjacent to a descendant of u. This indicates if some descendant is adjacent to a non-descendant. v e c t o r <int> found ( n, 1); // discovery time int cnt = 0 ; int b r i d g e s ( int u, int p ) { if ( found [ u ]!= 1) return found [ u ] ; int mn = found [ u ] = c n t ++; // record u s discovery for ( auto w : g [ u ] ) mn = min (mn, b r i d g e s (w, u ) ) ; if (mn == found [ u ] && p!= 2) // (p, u) is a bridge, process it how you want return mn ; }... b r i d g e s ( 0, 2); // start the search from any vertex time

Other DFS Applications Find all articulation points in a graph (good exercise). Find the strongly connected components of a directed graph. Compute pre/post order traversals of a tree. Deciding if a graph is bipartite. Simple code for augmenting a bipartite matching (later lecture). All of these can be implemented to run in O(n + m) time.

Breadth-First Search A breadth-first search will explore the vertices in increasing order of their shortest path distance from the start vertex. 2 2 1 1 0 1 1 2 3 3 Load up the start vertex in a queue q. While q is not empty, extract the front vertex and add all of its unvisited neighbours to the back of q.

queue<int> q ; //# include <queue > v e c t o r <int> p r e v ( n, 1); q. push ( v ) ; //v is the start vertex in the search p r e v [ v ] = 2; // signals "root of search" while (! q. empty ( ) ) { int c u r r = q. f r o n t ( ) ; q. pop ( ) ; for ( auto s ucc : g [ c u r r ] ) if ( p r e v [ s u c c ] == 1) { p r e v [ s u c c ] = c u r r ; q. push ( s u c c ) ; } } Now prev[u] for u v is the vertex prior to u on a shortest v u path. Also runs in O(n + m) time.

2 2 1 1 0 1 1 2 3 3 A thick arrow from u to w indicates prev[w] = u. The unique path using thick arrows from the start vertex (dark) to any vertex is a shortest path in the graph.

2 2 1 1 0 1 1 2 3 3 A thick arrow from u to w indicates prev[w] = u. The unique path using thick arrows from the start vertex (dark) to any vertex is a shortest path in the graph. Though we illustrated with an undirected graph, the same algorithm also finds shortest paths in directed graphs.

To Come... Next week Algorithms for weighted graphs. Dijkstra s algorithm for shortest paths. Floyd-Warshall for all-pairs shortest paths. Bellmand-Ford: handling negative weight cycles. Minimum Spanning Trees: Kruskal s Algorithm. Eulerian Circuits Later in the term Bipartite matching: unweighted. Network flow: max-flow/min-cut.

Example Problem: Kattis - coast https://open.kattis.com/problems/coast

Example Problem: Kattis - coast https://open.kattis.com/problems/coast View as a graph: vertices are squares and edges between adjacent squares.

Example Problem: Kattis - coast https://open.kattis.com/problems/coast View as a graph: vertices are squares and edges between adjacent squares. Run a dfs from an outside vertex.

Example Problem: Kattis - coast https://open.kattis.com/problems/coast View as a graph: vertices are squares and edges between adjacent squares. Run a dfs from an outside vertex. Return the number of coastlines seen from each recursive call.

Example Problem: Kattis - coast https://open.kattis.com/problems/coast View as a graph: vertices are squares and edges between adjacent squares. Run a dfs from an outside vertex. Return the number of coastlines seen from each recursive call. Do not continue the search if you try to cross to a land vertex, just return 1.

Example Problem: Kattis - coast https://open.kattis.com/problems/coast View as a graph: vertices are squares and edges between adjacent squares. Run a dfs from an outside vertex. Return the number of coastlines seen from each recursive call. Do not continue the search if you try to cross to a land vertex, just return 1. Tip: Pad the grid with water tiles on each edge, so you know (0, 0) is a water tile and you only have to run 1 dfs. Running time: O(N M), which is linear in the size of the grid.

Example Problem: Kattis - eulerianpath https://open.kattis.com/problems/eulerianpath Let δ v be the difference between the outdegree and indegree of v. Start a search at some s with δ s = +1 (pick any vertex s if there is no such vertex). When processing v in the search, do the following. While there is an unused outgoing edge vw, recursively search from w. When all edges exiting v are used, append v to the tour and return. Check that the tour contains m + 1 nodes (so all edges are traversed). If so, reverse it to get an Eulerian tour starting at s. Running time: O(n + m). Tip: Emulate the DFS with a stack if the recursion can go too deep.

Example Problem: Kattis - coloring https://open.kattis.com/problems/coloring

Example Problem: Kattis - coloring https://open.kattis.com/problems/coloring A dynamic programming over subsets approach.

Example Problem: Kattis - coloring https://open.kattis.com/problems/coloring A dynamic programming over subsets approach. For each set of vertices, determine if S is independent (no edge between nodes). Can be done in O(n 2 n ) time.

Example Problem: Kattis - coloring https://open.kattis.com/problems/coloring A dynamic programming over subsets approach. For each set of vertices, determine if S is independent (no edge between nodes). Can be done in O(n 2 n ) time. Next, for each S determine the fewest colours to colour S by trying each independent set as one colour, removing it, and recursing.

Example Problem: Kattis - coloring https://open.kattis.com/problems/coloring A dynamic programming over subsets approach. For each set of vertices, determine if S is independent (no edge between nodes). Can be done in O(n 2 n ) time. Next, for each S determine the fewest colours to colour S by trying each independent set as one colour, removing it, and recursing. i.e. χ( ) = 0 and for S χ(s) = 1 + min T independent set T χ(s T ).

Example Problem: Kattis - coloring https://open.kattis.com/problems/coloring A dynamic programming over subsets approach. For each set of vertices, determine if S is independent (no edge between nodes). Can be done in O(n 2 n ) time. Next, for each S determine the fewest colours to colour S by trying each independent set as one colour, removing it, and recursing. i.e. χ( ) = 0 and for S Running time O(2 2n ). χ(s) = 1 + min T independent set T χ(s T ).

Example Problem: Kattis - coloring https://open.kattis.com/problems/coloring A dynamic programming over subsets approach. For each set of vertices, determine if S is independent (no edge between nodes). Can be done in O(n 2 n ) time. Next, for each S determine the fewest colours to colour S by trying each independent set as one colour, removing it, and recursing. i.e. χ( ) = 0 and for S χ(s) = 1 + min T independent set T χ(s T ). Running time O(2 2n ). If in step 1 we only keep maximal independent sets (i.e. not contained in a larger ind. set), the running time reduces to O(2 4n/3 ) because # of maximal independent sets in any graph is O(2 n/3 ).