Lecture 26: Graphs: Traversal (Part 1)

Similar documents
Lecture 28: Graphs: Shortest Paths (Part 1)

Lecture 9 Graph Traversal

CS2 Algorithms and Data Structures Note 9

CS 310 Advanced Data Structures and Algorithms

Copyright 2000, Kevin Wayne 1

Figure 1: A directed graph.

Algorithm Design and Analysis

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

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

Basic Graph Definitions

CSE 373: Data Structures and Algorithms

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

CSI 604 Elementary Graph Algorithms

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

Lecture 23: Priority Queues, Part 2 10:00 AM, Mar 19, 2018

TIE Graph algorithms

TIE Graph algorithms

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

CSE 100: GRAPH ALGORITHMS

Inf 2B: Graphs, BFS, DFS

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

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.

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

Chapter 9 Graph Algorithms

Algorithm Design and Analysis

Announcements Problem Set 4 is out!

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

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

IS 709/809: Computational Methods in IS Research. Graph Algorithms: Introduction

GRAPHS Lecture 17 CS2110 Spring 2014

Introduction to Graphs. common/notes/ppt/

Graph Representations and Traversal

Graph: representation and traversal

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

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

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

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

Lecture 5: Graphs & their Representation

12 Abstract Data Types

Graphs & Digraphs Tuesday, November 06, 2007

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

CSC263 Week 8. Larry Zhang.

Algorithms: Lecture 10. Chalmers University of Technology

Algorithm Design and Analysis

Lecture 22 Tuesday, April 10

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

Lecture 3: Graphs and flows

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Chapter 9 Graph Algorithms

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we

CS 4407 Algorithms Lecture 5: Graphs an Introduction

Graph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph

CS/COE

Chapter 9 Graph Algorithms

COT 6405 Introduction to Theory of Algorithms

CS24 Week 8 Lecture 1

CS 206 Introduction to Computer Science II

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

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

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Topic 12: Elementary Graph Algorithms

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

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

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

LECTURE 17 GRAPH TRAVERSALS

Homework Assignment #3 Graph

Data Structure. IBPS SO (IT- Officer) Exam 2017

W4231: Analysis of Algorithms


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

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

Graphs. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. What is a Graph? b d f h j

Representations of Graphs

Introductory Remarks

Graph Algorithms. Definition

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

Introduction to Graphs. CS2110, Spring 2011 Cornell University

Graphs - I CS 2110, Spring 2016

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Directed Acyclic Graphs & Topological Sort

Graph. Vertex. edge. Directed Graph. Undirected Graph

COMP 250 Fall graph traversal Nov. 15/16, 2017

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

Data Structures Question Bank Multiple Choice

Goals! CSE 417: Algorithms and Computational Complexity!

GRAPHS Lecture 19 CS2110 Spring 2013

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

Breadth First Search. cse2011 section 13.3 of textbook

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

Some major graph problems

Konigsberg Bridge Problem

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

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

Graphs. CS16: Introduction to Data Structures & Algorithms Spring 2018

INSTITUTE OF AERONAUTICAL ENGINEERING

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

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

Design and Analysis of Algorithms

Transcription:

CS8 Integrated Introduction to Computer Science Fisler, Nelson Lecture 6: Graphs: Traversal (Part ) 0:00 AM, Apr, 08 Contents Introduction. Definitions........................................... Representations....................................... Graph Traversal. BFS and DFS on Trees.................................. Objectives By the end of this lecture, you will know: ˆ what a directed graph is; ˆ how to represent directed graphs in a program; and ˆ the advantages and disadvantages of different representations By the end of this lecture, you will be able to: ˆ traverse the simplest kind of graph trees in breadth-first order Introduction A graph consists of a set of vertices, or nodes, and a set of edges, or links, among those nodes, often annotated with weights. As a modeling tool, graphs are pervasive, both inside and outside computer science. They are used to model telecommunication networks (e.g., the Internet), social networks (e.g., Facebook), maps (e.g., a road map), etc. Here are some more details about how graphs can be used to model these things, together with examples of the kind of problems we can solve using graphs as a modelling tool: ˆ Internet: nodes: web pages links: hyperlinks pointing from one web page to another application: which are the most authoritative web pages? ˆ Facebook:

nodes: people links: friendships application: how many degrees of separation are there between me and Barack Obama? ˆ Road Map: nodes: intersections links: streets and highways weights: distances application: what is the fastest way to drive from Providence to New York? In the first part of this lecture, we present graphs mathematically and discuss two viable computational representations of graphs. In the second part, we introduce some basic graph algorithms. Overall, the next few lectures should give you an understanding of how to work with graphs and a flavor of the kind of things you will learn about should you choose to take CSCI 70. Earlier versions of these notes were based on the Introduction to Algorithms textbook, by Cormen, Leiserson, Rivest, and Stein (00). The current version is only very loosely based on this source.. Definitions A graph G is a pair (V, E), where V is a finite set of vertices (sometimes called nodes) and E is a finite set of edges (sometimes called links). By convention, n = V and m = E. In a directed graph, E is a set of ordered pairs, while in an undirected graph, E is a set of unordered pairs. For example, the pair of sets ({,,,,, {(, ), (, ), (, ), (, ), (, ), (, ), (, )) is a directed graph, while the pair of sets is an undirected graph. ({,,,,, {{,, {,, {,, {,, {,, {, ) Figure : (Left) A sample directed graph. (Right) A sample undirected graph. An edge from a vertex to itself is called a self-loop. In an undirected graph, the maximum number of edges (not including self-loops) is ( n ) = n(n )/, the number of different ways to choose items among n. Including self-loops, it is ( n ) + n = n(n + )/. In a directed graph, the maximum number of edges including self-loops is n. Not including self-loops, it is n(n ). In all cases, m O(n ).

Figure : (Left) Another pictoral representation of the same sample directed graph. (Right) Ditto, for the sample undirected graph. We say that an edge (u, v) is incident upon the vertices u and v. We also say that u is an (immediate) predecessor of v and that v is an (immediate) successor of u. In addition, u and v are called neighbors. In an undirected graph, the degree of a vertex is the number of edges incident upon it. In a directed graph, the indegree of a vertex is the number of edges entering it, while the outdegree of a vertex is the number of edges exiting it. A path from vertex u to vertex u is a sequence of vertices u = v 0, v,..., v k = u such that (v i, v i+ ) E. By convention, the length of a path v 0, v,..., v k is k; so the length of a path from a vertex to itself is 0. If v 0 = v k, then the path is a cycle. If there is a path from u to v, then the vertices u and v are said to be connected. If there is a path from u to v for all u, v V, then the graph is said to be connected. A graph with no cycles is called an acyclic graph. A tree is a connected, acyclic graph. A directed graph with no cycles is called a directed acyclic graph, or a DAG, for short. DAGs can be used to represent jobs (vertices) that need to be scheduled when there are precedence constraints (edges) between (at least) some of them: e.g., you should tune your violin before practicing, but it doesn t really matter if you do your CS 8 homework before or after practicing. Often, the edges in a graph are annotated with weights; the weight on edge (u, v) is denoted by w(u, v). Sometimes these weights are called costs, and denoted by c(u, v) instead.. Representations There are two common ways to represent graphs. Perhaps the most straightforward is to use an n n adjacency matrix A. In an unweighted graph, a ij = if and only if (i, j) E; in a weighted graph, a ij = w(i, j). In undirected graphs, A T = A: i.e., A is symmetric along the main diagonal. The size of an adjacency matrix is O(n ). Figure shows an adjacency matrix representing the graphs in Figures and. Observe that the matrix is indeed symmetric for the undirected graph. In some cases (particularly, where space is an issue), it may make sense to store an undirected graph as an array of arrays of increasing lengths rather than as a matrix, so as to avoid storing the same information twice. For dense graphs, nearly all the cells in the adjacency matrix store data. But for sparse graphs, many of the cell s values are 0. In this latter case, an adjacency set is more space efficient. An adjacency set is an array of size n, in which each cell i stores the set of vertices that i connects

A 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 A 0 0 0 0 0 0 0 0 0 0 0 0 0 Figure : (Left) An adjacency matrix representing the directed graphs in Figures and. (Right) An adjacency matrix representing the undirected graphs in Figures and. to: that is, i s set includes all vertices j such that (i, j) E. (In a weighted graph, the sets also store weights.) The size of an adjacency set is O(n + m). A common implementation of an adjacency set is as an array of linked lists, as seen in Figure. Another popular choice is an array of hash tables. For weighted edges that are accessed in weight order, an array of heaps makes practical sense. Or, if the edges are Ordered, an array of binary search trees is a possibility. Nodes Linked Lists Figure : An adjacency set representing the undirected, unweighted graphs in Figures and. The choice of adjacency matrix vs. adjacency set is a prime example of the time vs. space trade-off which arises so often in computer science. In terms of time: Problem Adjacency Matrix Adjacency Linked List Adjacency Hash Table Is there an edge from u to v? O() O(m) O() How fast is insertion and deletion O() O(m) O() of edges (checking for duplicates)? What is the degree of vertex u? O(n) O() O() The time complexity for determining the degree of vertex u with an adjacency linked list assumes the data structure maintains a length attribute (rather than traverse the list to determine its length). Comparable, in Java.

If we include the restriction that a vertex can have at most n edges, then the other two adjacency linked list time complexities would drop to O(min{m, n). The time complexity for an adjacency hash table assumes a distribution of keys together with a hash function that uniformly distributes keys, and a hash table with sufficient capacity so that there are not too many collisions. But what about space? The adjacency matrix always takes O(n ) space: there s an entry for each possible edge, i.e., each pair of nodes. This is true no matter how many edges the graph has. In contrast, the adjacency list may take up far less space if m is small compared to n: there can only ever be m entries total across the lists for all nodes. Graph Traversal There are two principal ways to traverse a graph: breadth-first and depth-first. Suppose we are given a source (i.e., start) node and asked to traverse the entire graph (or at least, as much as can be reached!) from there. This is often cached in terms of searching for a particular sink (i.e., destination) node, but the general technique is far more broad. The key idea underlying breadth-first traversal is to visit nodes in order of distance from the source, where distance is measured as the number of edges traversed (as opposed to edge costs, in a weighted graph). The key idea underlying depth-first traversal is to visit nodes as deeply as possible first, and then to backtrack until a node is discovered whose neighbors have not yet all been visited. Breadth-first and depth-first traversals are often part of search routines (e.g., to search for a path from Providence to New York City in a graph that represents a road map), and we will refer to these algorithms as breadth-first search (BFS) and depth-first search (DFS), respectively.. BFS and DFS on Trees To begin with, let s look at an example of a tree and its corresponding breadth-first and depth-first traversals. While there are recursive formulations of both algorithms, here we will focus on their iterative variants. A B C D E F G H I J K L M N O Figure : Example Tree BFS is implemented using a queue. First, enqueue the root node A onto the queue. Then, dequeue A, and enqueue its children (typically, in left-to-right order) onto the queue. The queue now looks like B, C. Next, dequeue B, and enqueue its children onto the queue. The queue now looks like

C, D, E. Next, dequeue C, and enqueue its children onto the queue. The queue now looks like D, E, F, G. And so on. Indeed, BFS visits nodes in this tree in alphabetical order. In contrast, DFS is implemented using a stack. Here is how a DFS of the above tree proceeds. First, push the root node A onto the stack. Then, pop A, and push its children (typically, in left-to-right order) onto the stack. The stack now looks like B, C. Next, pop B, and push its children onto the stack. The stack now looks like D, E, C. Next, pop D, and push its children onto the stack. The stack now looks like H, I, E, C. Next, pop H; next, pop I. The stack now looks like E, C. Next, pop E, and push its children on to the stack. The stack now looks like J, K, C. And so on. Ultimately, DFS visits nodes in this tree (i.e., pops nodes off the stack) in the following order: A, B, D, H, I, E, J, K, C, F, L, M, G, N, O. BFS and DFS for Trees Here is pseudocode for these two types of search for a tree G starting from node v. Notice that they are essentially the same algorithm, varying only in the choice of data structure used to store the to do list. DFT(G, v) { S = new Stack() S.push(v) while(!s.empty()) { v = S.pop() for(w in G.adjs(v)) { S.push(w) BFT(G, v) { Q = new Queue() Q.add(v) while(!q.empty()) { v = Q.getFirst() Q.removeFirst() for(w in G.adjs(v)) { Q.add(w) Note that these versions are explicitly just for trees. We ll talk about why that is in the next lecture. There are actually multiple variations of DFS, depending on whether nodes are popped off the stack before or after its children are visited. The version we present here is called a pre-order traversal. 6

Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CS8 document by filling out the anonymous feedback form: http://cs.brown.edu/ courses/cs08/feedback. 7