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

Similar documents
Algorithms: Lecture 10. Chalmers University of Technology

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

Algorithm Design and Analysis

W4231: Analysis of Algorithms

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

Searching in Graphs (cut points)

Graph Search Methods. Graph Search Methods

Lecture 3: Graphs and flows

Goals! CSE 417: Algorithms and Computational Complexity!

Algorithm Design and Analysis

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

Copyright 2000, Kevin Wayne 1

Definition Example Solution

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

CS 310 Advanced Data Structures and Algorithms

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

22.3 Depth First Search

Graph Representation

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

Info 2950, Lecture 16

Graph: representation and traversal

Design and Analysis of Algorithms

Algorithm Design and Analysis

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

CSE 421 DFS / Topological Ordering

GRAPHS Lecture 17 CS2110 Spring 2014

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

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

Design and Analysis of Algorithms

csci 210: Data Structures Graph Traversals

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

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

Graph Search Methods. Graph Search Methods

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

CSE 417: Algorithms and Computational Complexity

CS4800: Algorithms & Data Jonathan Ullman

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

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

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

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

3.4 Testing Bipartiteness

TIE Graph algorithms

Graph Algorithms Using Depth First Search

Elementary Graph Algorithms

COT 6405 Introduction to Theory of Algorithms

Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

LECTURE 17 GRAPH TRAVERSALS

CSI 604 Elementary Graph Algorithms

Trees. Arash Rafiey. 20 October, 2015

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Professor: Padraic Bartlett. Lecture 9: Trees and Art Galleries. Week 10 UCSB 2015

Graph Algorithms. Definition

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

CSE 100: GRAPH ALGORITHMS

csci 210: Data Structures Graph Traversals

TIE Graph algorithms

Fundamental Graph Algorithms Part Four

Applications of BDF and DFS

Graphs - II. Announcements. Where did I leave that book? Where did I leave that book? Where did I leave that book? CS 2110, Fall 2016

CS 206 Introduction to Computer Science II

Design and Analysis of Algorithms

Design and Analysis of Algorithms

Uninformed Search Strategies

Outline. 1 Introduction. 2 Algorithm. 3 Example. 4 Analysis. 5 References. Idea

CS4800: Algorithms & Data Jonathan Ullman

Some major graph problems

Algorithm Design and Analysis

Review: Graph Theory and Representation

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

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

Graphs and trees come up everywhere. We can view the internet as a graph (in many ways) Web search views web pages as a graph

Graph Algorithms. Andreas Klappenecker

Graphs and Graph Algorithms. Slides by Larry Ruzzo

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

15-451/651: Design & Analysis of Algorithms October 5, 2017 Lecture #11: Depth First Search and Strong Components last changed: October 17, 2017

22.1 Representations of graphs

Chapter 22. Elementary Graph Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms

CMPSC 250 Analysis of Algorithms Spring 2018 Dr. Aravind Mohan Shortest Paths April 16, 2018

Depth-First Search Depth-first search (DFS) is another way to traverse the graph.

Outlines: Graphs Part-2

Lecture 8: PATHS, CYCLES AND CONNECTEDNESS

The Konigsberg Bridge Problem

(Refer Slide Time: 05:25)

Lecture 9 Graph Traversal

DFS & STRONGLY CONNECTED COMPONENTS

Lecture 26: Graphs: Traversal (Part 1)

Lecture 10 Graph algorithms: testing graph properties

Announcements. CSEP 521 Applied Algorithms. Announcements. Polynomial time efficiency. Definitions of efficiency 1/14/2013

Lecture 4: Trees and Art Galleries

3.1 Basic Definitions and Applications

CMPSCI 250: Introduction to Computation. Lecture #24: General Search, DFS, and BFS David Mix Barrington 24 March 2014

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

Homework Assignment #3 Graph

Graphs. Motivations: o Networks o Social networks o Program testing o Job Assignment Examples: o Code graph:

CSE 421 Applications of DFS(?) Topological sort

Introduction to Algorithms. Lecture 11

COS 226 Lecture 19: Minimal Spanning Trees (MSTs) PFS BFS and DFS examples. Classic algorithms for solving a natural problem

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

Transcription:

CS 361 Data Structures & Algs Lecture 13 Prof. Tom Hayes University of New Mexico 10-5-2010 1

Last Time Spanning Trees BFS DFS Testing Bipartiteness 2

Today Inside BFS Running Time Implementation Degrees & Degree sums Properties of BFS and DFS trees Identifying trees in the wild 3

Breadth-First Search BFS finds the vertices in the connected component of the start vertex s one level at a time. Level Li = {vertices whose distance to s equals i} distance(v,w) = number of edges in the shortest path from v to w. Question: how to implement? 4

Plan of Attack We will first talk about a general search framework (GSF), broad enough to include BFS and some other search algorithms. Then focus on BFS in more detail. 5

GSF (includes BFS) Each node has 3 different states : U, A, I Initially: everything marked U (unfound). Change s to A (active). for (v : A) for (w : Adj[v]) if (w is U) change w to A change v to I (inactive) 6

Analysis of GSF Each node has 3 different states : U, A, I Initially: everything marked U (unfound). Change s to A (active). for (v : A) for (w : Adj[v]) How many times? if (w is U) change w to A change v to I (inactive) 7

Analysis of GSF Each node has 3 different states : U, A, I Initially: everything marked U (unfound). Change s to A (active). for (v : A) for (w : Adj[v]) How many times? if (w is U) change w to A 2 (#edges) change v to I (inactive) 8

Analysis of GSF Lemma: Each node is initially U, then A for some time, then I, and never goes back to an earlier label. Proof. By inspection, there is no line of code which could turn a node marked A or I into U, or I into A. Corollary: Loop variable v never repeats an earlier value. 9

Degree of a node Let v be a vertex in a graph G=(V,E). The degree of v equals the number of neighbors of v, that is, the length of the adjacency list of v. Answer: What is the sum, v V degree(v) 10

Degree of a node Let v be a vertex in a graph G=(V,E). The degree of v equals the number of neighbors of v, that is, the length of the adjacency list of v. Theorem: v V degree(v) = 2 E 11

Analysis of GSF Each node has 3 different states : U, A, I Initially: everything marked U (unfound). Change s to A (active). 1 (#nodes) for (v : A) for (w : Adj[v]) if (w is U) change w to A change v to I (inactive) 2 (#edges) (#nodes) Total: O(#nodes + #edges) 12

Linear Time Algorithms We say an algorithm runs in linear time if the running time is O(input size). For a graph in adjacency list format, the input size is Θ(#nodes + #edges). So, for algorithms where the input is a graph, linear time means O( V + E ). In particular, BFS is a linear-time algorithm. 13

From GSF to BFS BFS wants to explore level by level. GSF iterates through active set A in any order. BFS wants to explore from all nodes in level i before any in level (i+1). Insight: BFS also discovers all nodes in level i before any in level (i+1). Solution: Store A in a queue (FIFO)! In Java: AbstractQueue. 14

GSF (includes BFS) Each node has 3 different states : U, A, I Initially: everything marked U (unfound). Change s to A (active). for (v : A) for (w : Adj[v]) if (w is U) change w to A change v to I (inactive) 15

BFS Initially: A = empty queue. Initially: U[each vertex] = true A.add(s) (enqueue s) while (A nonempty) v = A.remove() for (w : Adj[v]) if (U[w]) {U[w]=false, A.add(w)} 16

Notes Originally, state U,A,I, was a single attribute of each node. Helped prevent conflicts (a state marked both U and A). When A became a Queue, we made U into a boolean array, then dropped I entirely. Care needed to avoid conflicts. Didn t have to keep track of current Level explicitly, since Queue keeps the nodes in the right order. Suppose we want to. How can we do it? 17

Edges in BFS Theorem: Each time BFS looks at an edge, it is either: (a) joining a found node (level Li) to an unfound node (level Li+1). Becomes an edge in the tree, or (b) joining a found node (level Li) to an already found node (level Li or Li+1). Why only these? 18

Rephrased: Theorem: If T is a BFS tree for a graph G, then every edge in G either: (a) is in T, and joins adjacent levels, or (b) is not in T, and joins nodes in the same or adjacent levels. Level: equal distance from the root. 19

Is this a BFS tree? 20

Is this a BFS tree? Where is the root? 21

Is this a BFS tree? Yes, and root must be here! 22

Is this a BFS tree? 2 23

Is this a BFS tree? 2 Not even a tree! (cycle) 24

Is this a BFS tree? 3 25

Is this a BFS tree? 3 Where is the root? 26

Is this a BFS tree? 3 root can only be here or here (level 1 must include all neighbors of root) 27

Is this a BFS tree? 3 can root be here? 28

Is this a BFS tree? 3 can root be here? No! Blue node would be in level 2 of tree. 29

Is this a BFS tree? 3 can root be here? 30

Is this a BFS tree? 3 can root be here? No! Blue node would be in level 2 of tree. 31

Is this a BFS tree? 3 Not a BFS tree! 32

Is this a BFS tree? 4 33

Is this a BFS tree? 4 Where is the root? 34

Is this a BFS tree? 4 Check: Yes, with 3 possible roots. 35

Depth-First Search DFS: explores fully from each vertex, before backing up to try another one. DFS(s): Mark s as found. For each unfound neighbor v of s: add edge (v,s) to T. DFS(v) 36

DFS Trees DFS: explores fully from each vertex, before backing up to try another one. Theorem: Each time DFS looks at an edge, it either: (a) is added to the tree, or (b) is a back-edge to an ancestor of the current node. (see (3.7) on page 85) Lemma: Active nodes are always a path from the root. 37

DFS example 38

DFS example 39

DFS example 40

DFS example 41

DFS example 42

DFS example 43

DFS example 44

DFS example 45

DFS example 46

DFS example 47

DFS example 48

DFS example 49

DFS example 50

DFS example 51

Is this a DFS tree? 52

Is this a DFS tree? Where is the root? 53

Is this a DFS tree? Non-edges must join a node to its ancestor. 54

Is this a DFS tree? Yes, a DFS tree. Root must be here 55