Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description

Similar documents
Copyright 2000, Kevin Wayne 1

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

Lecture 3: Graphs and flows

Definition f(n) = O(g(n)) if there exists n 0, c such that for all n n 0, f(n) cg(n). g is an asymptotic upper bound on f.

Outline. Graphs. Divide and Conquer.

Graph Algorithms. Imran Rashid. Jan 16, University of Washington

CS 310 Advanced Data Structures and Algorithms

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Algorithm Design and Analysis

3.4 Testing Bipartiteness

Algorithms: Lecture 10. Chalmers University of Technology

Goals! CSE 417: Algorithms and Computational Complexity!

Introduction to Algorithms

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

Basic Graph Algorithms

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

CS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering

3.1 Basic Definitions and Applications

GRAPHS Lecture 17 CS2110 Spring 2014

CS 491 CAP Introduction to Graphs and Search

CSE 421 DFS / Topological Ordering

CS4800: Algorithms & Data Jonathan Ullman

Basic Graph Algorithms (CLRS B.4-B.5, )

W4231: Analysis of Algorithms

Graph Algorithms. Textbook reading. Chapter 3 Chapter 4. CSci 3110 Graph Algorithms 1/41

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

CSE 417: Algorithms and Computational Complexity

Lecture 10 Graph algorithms: testing graph properties

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

Graph. Vertex. edge. Directed Graph. Undirected Graph

CS Lunch. Grace Hopper??? O()? Slides06 BFS, DFS, Bipartite.key - October 3, 2016

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Graph Search Methods. Graph Search Methods

CS 361 Data Structures & Algs Lecture 13. Prof. Tom Hayes University of New Mexico

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

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

CS 361 Data Structures & Algs Lecture 15. Prof. Tom Hayes University of New Mexico

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Lecture 9 Graph Traversal

22.3 Depth First Search

Algorithm Design and Analysis

TIE Graph algorithms

Introduction to Graphs. CS2110, Spring 2011 Cornell University

Graph Representations and Traversal

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Directed Graphs (II) Hwansoo Han

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

Depth First Search (DFS)

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

(Refer Slide Time: 05:25)

GRAPHS Lecture 19 CS2110 Spring 2013

Fundamental Graph Algorithms Part Three

Graph Representation

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

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

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

Objec&ves. Wrap up: Implemen&ng BFS and DFS Graph Applica&on: Bipar&te Graphs. Get out your BFS implementa&on handouts. Feb 2, 2018 CSCI211 - Sprenkle

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

CSE 373: Data Structures and Algorithms

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

Graph: representation and traversal

Lecture 22 Tuesday, April 10

The Konigsberg Bridge Problem

Some major graph problems

Graph Traversals. CS200 - Graphs 1

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 28 Graphs and Applications. Objectives

CSI 604 Elementary Graph Algorithms

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

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

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

Graph Algorithms. Definition

LECTURE 17 GRAPH TRAVERSALS

Chapter 3. Graphs. 3.1 Basic Definitions and Applications

Elementary Graph Algorithms

CHAPTER 13 GRAPH ALGORITHMS ORD SFO LAX DFW

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

TIE Graph algorithms

Finding Strongly Connected Components

Graph representation

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

Announcements. HW3 is graded. Average is 81%

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

Design and Analysis of Algorithms

3.1 Basic Definitions and Applications

CS4800: Algorithms & Data Jonathan Ullman

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

More Graph Algorithms

CS302 - Data Structures using C++

Topic 12: Elementary Graph Algorithms

1 Connected components in undirected graphs

Searching in Graphs (cut points)

Graph Search. Algorithms & Models of Computation CS/ECE 374, Fall Lecture 15. Thursday, October 19, 2017

1 Bipartite maximum matching

Info 2950, Lecture 16

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

Trees. Arash Rafiey. 20 October, 2015

csci 210: Data Structures Graph Traversals

Transcription:

Plan CMPSCI 311: Introduction to Algorithms Akshay Krishnamurthy and Andrew McGregor University of Massachusetts Review: Breadth First Search Depth First Search Traversal Implementation and Running Time Traversal Applications Directed Graphs Last Compiled: September 15, 2016 Recall Adjacency List Representation Graph G = (V, E) Set of nodes V of size n Set of edges E of size m Adjacency List Representation. Nodes numbered 1,..., n. Adj[v] points to a list of all of v s neighbors. BFS Description DFS Descriptions Define layer L i = all nodes at distance exactly i from s. Layers L 0 = {s} L 1 = all neighbors of L 0 L 2 = all nodes with an edge to L 1 that don t belong to L 0 or L 1... L i+1 = nodes with an edge to L i that don t belong to any earlier layer. L i+1 = {v : (u, v) E, u L i, v / (L 0... L i )} Depth-first search: keep exploring from the most recently discovered node until you have to backtrack. DFS(u) Mark u as "Explored" for each edge (u, v) incident to u do if v is not marked "Explored" then Recursively invoke DFS(v)

Traversal Implementations Generic Graph Traversal Maintain set of explored nodes and discovered Explored = have seen this node and explored its outgoing edges Discovered = the frontier. Have seen the node, but not explored its outgoing edges. Let A = data structure of discovered nodes Note: one part of this algorithm seems really dumb. Why? Can put multiple copies of a single node in A. Generic Graph Traversal Interlude (Data Structures) Let A = data structure of discovered nodes BFS: A is a queue (FIFO) DFS: A is a stack (LIFO) Linked List: Head o 1 o 2 o 3 o 4 o 5 o 6 Always remove items from front (Head) Queue: Insert at Tail (FIFO) Stack: Insert at Head (LIFO) Insert/Removal are O(1) operations. Tail BFS Implementation BFS Running Time Let A = empty Queue structure of discovered nodes Is this actually BFS? Yes Running time: O(n + m) Naive O(n 2 ) Smarter O(n + m)

DFS Implementation Back to Connected Components Let A = empty Stack structure of discovered nodes Is this actually DFS? Yes What s the running time? FindCC(G) while There is some unexplored node s do BFS(s) Extract connected component C(s). Running time for finding connected components? Naive: O(m + n) for each component O(c(m + n)) if c components. Better: BFS on component C only works on nodes/edges in C. Running time is O( C V (C) + E(C) ) = O(m + n). Bipartite Graphs Bipartite Testing Definition Graph G = (V, E) is bipartite if V can be partitioned into sets X, Y such that every edge has one end in X and one in Y. Example Student-College Graph in stable matching Counter example Cycle of length k for k odd. Claim If G is bipartite then it cannot contain an odd cycle. Question Given G = (V, E), is G bipartite? How do we design an algorithm to test bipartiteness? BFS(s) for any s, keep track of layers. Nodes in odd layers get color blue, even get color red. After, check all edges have different colored endpoints. Running time? O(n + m). Analysis of Bipartite Testing Directed Graphs Claim After running BFS on a connected graph G, either, There are no edges between two nodes of the same layer G is bipartite. There is an edge between two nodes of the same layer G has an odd cycle, is not bipartite. G bipartite if and only if no odd cycles. Directed Graph G = (V, E). V is a set of vertices/nodes. E is a set of ordered pairs (u, v). Express asymmetrical relationship Examples Twitter network, course schedule, web graph.

Adjacency Lists Strong Connectivity Maintain two lists. Enter[v] contains all edges pointing to v. Leave[v] contains all edges pointing from v. Definition G is strongly connected if for every u, v V, there is a path from u to v and from v to u. Problem Test if G is strongly connected? Definition The strongly connected component containing vertex s is the set of all nodes with paths to and from s. Think about Can you find all SCCs in linear time? Directed Acyclic Graphs Definition A directed acyclic graph (DAG) is a directed graph with no cycles. Example Course prerequisites Topological Sorting CS220 Math132 CS311 CS220 CS240 Math132 CS311 CS187 CS383 CS187 CS240 CS383 CS250 Can you find a way to take all of the courses? CS250 Topological Sorting Topological sorting Definition A topological ordering of G = (V, E) is an ordering v 1, v 2,..., v n of the nodes, such that for all edges (v i, v j ) E, we must have i < j. M132 C187 C220 C240 C250 C311 C383 Claim If G has a topological ordering, then G is a DAG. Problem Given DAG G, compute a topological ordering for G. Does one always exist? topo-sort(g) while there are nodes remaining do Find a node v with no incoming edges Place v next in the order Delete v and all of its outgoing edges from G Running time? O(n 2 + m) easy, O(m + n) more clever.

Topological Sorting Analysis Graphs Summary In a DAG, there is always a node v with no incoming edges. Removing a node v from a DAG, produces a new DAG. Any node with no incoming edges can be first in topological ordering. Theorem G is a DAG if and only if G has a topological ordering. Graph Traversal BFS/DFS, Connected Components, Bipartite Testing Traversal Implementation and Analysis Directed Graphs Strong Connectivity Directed Acyclic Graphs Topological ordering