Inf 2B: Graphs, BFS, DFS

Similar documents
I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V.

CS2 Algorithms and Data Structures Note 9

Inf 2B: Graphs II - Applications of DFS

Basic Graph Definitions

TIE Graph algorithms

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

Mystery Algorithm! ALGORITHM MYSTERY( G = (V,E), start_v ) mark all vertices in V as unvisited mystery( start_v )

Graph traversal is a generalization of tree traversal except we have to keep track of the visited vertices.

CS302 - Data Structures using C++

CSE 100: GRAPH ALGORITHMS

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

TIE Graph algorithms

Module 11: Additional Topics Graph Theory and Applications

Lecture 9 Graph Traversal

Basic Graph Algorithms

Data Structure Lecture#23: Graphs (Chapter 11) U Kang Seoul National University

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

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

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

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

csci 210: Data Structures Graph Traversals

LECTURE 17 GRAPH TRAVERSALS

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

COT 6405 Introduction to Theory of Algorithms

22.3 Depth First Search

CS 206 Introduction to Computer Science II

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

Elementary Graph Algorithms

23 DECOMPOSITION OF GRAPHS

Lecture 26: Graphs: Traversal (Part 1)

Data Structures and Algorithms

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

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

Graph Traversals BFS & DFS 1 CS S-16

Graph Search. Adnan Aziz


I want an Oompa-Loompa! screamed Veruca. Roald Dahl, Charlie and the Chocolate Factory

Graph Representations and Traversal

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

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

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

CSI 604 Elementary Graph Algorithms

Graph Representation

To list all vertices connected to a vertex u in graph G we can use the following loop:

Topic 12: Elementary Graph Algorithms

Announcements Problem Set 4 is out!

W4231: Analysis of Algorithms

Representations of Graphs

CSC263 Week 8. Larry Zhang.

CSE 373: Data Structures and Algorithms

(Re)Introduction to Graphs and Some Algorithms

Introduction to Graphs. CS2110, Spring 2011 Cornell University

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

Unit 2: Algorithmic Graph Theory

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

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

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

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Unweighted directed graphs

CS 310 Advanced Data Structures and Algorithms

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

Chapter 22 Elementary Graph Algorithms

Graph. Vertex. edge. Directed Graph. Undirected Graph

Chapter 22. Elementary Graph Algorithms

Graph implementations :

Graphs & Digraphs Tuesday, November 06, 2007

Graph Traversals. Ric Glassey

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

Algorithm Design and Analysis

Algorithm Design and Analysis

22.1 Representations of graphs

Design and Analysis of Algorithms

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

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Algorithm Design and Analysis

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Graph Traversals. CS200 - Graphs 1

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Spanning Trees. Lecture 22 CS2110 Spring 2017

CSE 332: Data Abstractions Lecture 15: Topological Sort / Graph Traversals. Ruth Anderson Winter 2013

Graph Algorithms. Definition

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Computational Optimization ISE 407. Lecture 19. Dr. Ted Ralphs

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

Data Structures and Algorithms. Chapter 7. Graphs

Announcements. HW3 is graded. Average is 81%

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

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

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

Outline. Graphs. Divide and Conquer.

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

Minimum Spanning Trees Ch 23 Traversing graphs

Review: Graph Theory and Representation

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Unweighted Graphs & Algorithms

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

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

Graph: representation and traversal

Transcription:

Inf 2B: Graphs, BFS, DFS Kyriakos Kalorkoti School of Informatics University of Edinburgh

Directed and Undirected Graphs I A graph is a mathematical structure consisting of a set of vertices and a set of edges connecting the vertices. I Formally: G =(V, E), where V is a set and E V V. I For edge e =(u, v) we say that e is directed from u to v. I G =(V, E) undirected if for all v, w 2 V : (v, w) 2 E () (w, v) 2 E. Otherwise directed. Directed arrows (one-way) Undirected lines (two-way) I We assume V is finite, hence E is also finite.

A directed graph G = (V, E), V = 0, 1, 2, 3, 4, 5, 6, E = (0, 2), (0, 4), (0, 5), (1, 0), (2, 1), (2, 5), (3, 1), (3, 6), (4, 0), (4, 5), (6, 3), (6, 5). 0 1 2 3 4 5 6

An undirected graph a b c e d g f

Examples I Road Maps. Edges represent streets and vertices represent crossings (junctions). I Computer Networks. Vertices represent computers and edges represent network connections (cables) between them. I The World Wide Web. Vertices represent webpages, and edges represent hyperlinks. I...

Adjacency matrices Let G =(V, E) be a graph with n vertices. Vertices of G are numbered 0,...,n 1. The adjacency matrix of G is the n n matrix A =(a ij ) 0applei,japplen 1 with a ij = ( 1, if there is an edge from vertex i to vertex j; 0, otherwise.

Adjacency matrix (Example) 0 4 2 3 5 6 1 0 B @ 0 0 1 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 C A

Adjacency lists Array with one entry for each vertex v, which is a list of all vertices adjacent to v. Example 0 1 0 2 5 4 1 0 2 1 5 2 3 3 1 6 4 0 5 4 5 6 5 6 5 3

Quick Question Given: graph G =(V, E), with n = V, m = E. For v 2 V, we write in(v) for in-degree, out(v) for out-degree. Which data structure has faster (asymptotic) worst-case running-time, for checking if w is adjacent to v, for a given pair of vertices? 1. Adjacency list is faster. 2. Adjacency matrix is faster. 3. Both have the same asymptotic worst-case running-time. 4. It depends. Answer: 2. For an Adjacency Matrix we can check in (1) time. An adjacency list structure takes (1 + out(v)) time.

Quick Question Given: graph G =(V, E), with n = V, m = E. For v 2 V, we write in(v) for in-degree, out(v) for out-degree. Which data structure has faster (asymptotic) worst-case running-time, for visiting all vertices w adjacent to v, for a given vertex v? 1. Adjacency list is faster. 2. Adjacency matrix is faster. 3. Both have the same asymptotic worst-case running-time. 4. It depends. Answer: 3. Adjacency matrix requires (n) time always. Adjacency list requires (1 + out(v)) time. In worst-case out(v) = (n).

Adjacency Matrices vs Adjacency Lists adjacency matrix adjacency list Space (n 2 ) (n + m) Time to check if w (1) (1 + out(v)) adjacent to v Time to visit all w (n) (1 + out(v)) adjacent to v. Time to visit all edges (n 2 ) (n + m)

Sparse and dense graphs G =(V, E) graph with n vertices and m edges Observation: m apple n 2 I G dense if m close to n 2 I G sparse if m much smaller than n 2

Graph traversals A traversal is a strategy for visiting all vertices of a graph while respecting edges. BFS = breadth-first search DFS = depth-first search General strategy: 1. Let v be an arbitrary vertex 2. Visit all vertices reachable from v 3. If there are vertices that have not been visited, let v be such a vertex and go back to (2)

Graph Searching (general Strategy) Algorithm searchfromvertex(g, v) 1. mark v 2. put v onto schedule S 3. while schedule S is not empty do 4. remove a vertex v from S 5. for all w adjacent to v do 6. if w is not marked then 7. mark w 8. put w onto schedule S Algorithm search(g) 1. initialise schedule S 2. for all v 2 V do 3. if v is not marked then 4. searchfromvertex(g, v)

Three colour view of vertices I Previous algorithm has vertices in one of two states: unmarked and marked. Progression is unmarked! marked I Can also think of them as being in one of three states (represented by colours): I White: not yet seen (not yet investigated). I Grey: put on schedule (under investigation). I Black: taken off schedule (completed). Progression is white! grey! black We will use the three colour scheme when studying an algorithm for topological sorting of graphs.

BFS Visit all vertices reachable from v in the following order: I v I all neighbours of v I all neighbours of neighbours of v that have not been visited yet I all neighbours of neighbours of neighbours of v that have not been visited yet I etc.

BFS (using a Queue) Algorithm bfs(g) 1. Initialise Boolean array visited, setting all entries to FALSE. 2. Initialise Queue Q 3. for all v 2 V do 4. if visited[v] =FALSE then 5. bfsfromvertex(g, v)

BFS (using a Queue) Algorithm bfsfromvertex(g, v) 1. visited[v] =TRUE 2. Q.enqueue(v) 3. while not Q.isEmpty() do 4. v Q.dequeue() 5. for all w adjacent to v do 6. if visited[w] =FALSE then 7. visited[w] =TRUE 8. Q.enqueue(w)

0 1 2 3 4 5 6

Quick Question Given a graph G =(V, E) with n = V, m = E, what is the worst-case running time of BFS, in terms of m, n? 1. (m + n) 2. (n 2 ) 3. (mn) 4. Depends on the number of components. Answer: 1. To see this need to be careful about bounding running time for the loop at lines 5 8. Must use the Adjacency List structure. Answer: 2. if we use adjacency matrix represetnation.

DFS Visit all vertices reachable from v in the following order: I v I some neighbour w of v that has not been visited yet I some neighbour x of w that has not been visited yet I etc., until the current vertex has no neighbour that has not been visited yet I Backtrack to the first vertex that has a yet unvisited neighbour v 0. I Continue with v 0, a neighbour, a neighbour of the neighbour, etc., backtrack, etc.

DFS (using a stack) Algorithm dfs(g) 1. Initialise Boolean array visited, setting all to FALSE 2. Initialise Stack S 3. for all v 2 V do 4. if visited[v] =FALSE then 5. dfsfromvertex(g, v)

DFS (using a stack) Algorithm dfsfromvertex(g, v) 1. S.push(v) 2. while not S.isEmpty() do 3. v S.pop() 4. if visited[v] =FALSE then 5. visited[v] =TRUE 6. for all w adjacent to v do 7. S.push(w) 0 1 2 3 4 5 6

Recursive DFS Algorithm dfs(g) 1. Initialise Boolean array visited by setting all entries to FALSE 2. for all v 2 V do 3. if visited[v] =FALSE then 4. dfsfromvertex(g, v) Algorithm dfsfromvertex(g, v) 1. visited[v] TRUE 2. for all w adjacent to v do 3. if visited[w] =FALSE then 4. dfsfromvertex(g, w)

Analysis of DFS G =(V, E) graph with n vertices and m edges Without recursive calls: I dfs(g): time (n) I dfsfromvertex(g, v): time (1 + out-degree(v)) Overall time: T (n, m) = (n)+ P v2v (1 + out-degree(v)) = n + P v2v (1 + out-degree(v)) = n + n + P v2v out-degree(v) = n + P v2v out-degree(v) = (n + m)