CS 310 Advanced Data Structures and Algorithms

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

Graph: representation and traversal

CSI 604 Elementary Graph Algorithms

Lecture 9 Graph Traversal

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

Figure 1: A directed graph.

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

Copyright 2000, Kevin Wayne 1

Graph Algorithms. Definition

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Algorithm Design and Analysis

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

Graph Representation

TIE Graph algorithms

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

Outline. Graphs. Divide and Conquer.

Graph. Vertex. edge. Directed Graph. Undirected Graph

Data Structures and Algorithms for Engineers

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

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.

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

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

Algorithms: Lecture 10. Chalmers University of Technology

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

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

TIE Graph algorithms

Graphs & Digraphs Tuesday, November 06, 2007

Basic Graph Algorithms

COT 6405 Introduction to Theory of Algorithms

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

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

Topic 12: Elementary Graph Algorithms

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

Goals! CSE 417: Algorithms and Computational Complexity!

CSE 373: Data Structures and Algorithms

Introduction to Graphs. CS2110, Spring 2011 Cornell University

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

Fundamental Graph Algorithms Part Four

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

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

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

W4231: Analysis of Algorithms

Announcements Problem Set 4 is out!

Algorithm Design and Analysis

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

Some major graph problems

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Minimum Spanning Trees Ch 23 Traversing graphs

CSE 100: GRAPH ALGORITHMS

Trees. Arash Rafiey. 20 October, 2015

(Re)Introduction to Graphs and Some Algorithms

Introduction to Algorithms

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

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

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

CS 491 CAP Introduction to Graphs and Search

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

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

Lecture 26: Graphs: Traversal (Part 1)

Applications of BDF and DFS

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

Data Structures and Algorithms. Chapter 7. Graphs

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

GRAPHS Lecture 17 CS2110 Spring 2014

Graph Representations and Traversal

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

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Announcements. HW3 is graded. Average is 81%

Design and Analysis of Algorithms

Basic Graph Definitions

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

22.3 Depth First Search

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

Graph Search. Adnan Aziz

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

CSC263 Week 8. Larry Zhang.

Chapter 28 Graphs and Applications. Objectives

12 Abstract Data Types

Breadth First Search. cse2011 section 13.3 of textbook

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

Representations of Graphs

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

Graph: representation and traversal

Elementary Graph Algorithms


CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS

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

Lecture 3: Graphs and flows

Graph Algorithms Using Depth First Search

Data Structures Brett Bernstein

Graph Search Methods. Graph Search Methods

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

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

Graphs Introduction and Depth first algorithm

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

Graph Traversals. CS200 - Graphs 1

Directed Graphs (II) Hwansoo Han

Algorithm Design and Analysis

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

Transcription:

CS 31 Advanced Data Structures and Algorithms Graphs July 18, 17 Tong Wang UMass Boston CS 31 July 18, 17 1 / 4

Graph Definitions Graph a mathematical construction that describes objects and relations between them A graph consists of a set of vertices and a set of edges that connect the vertices G = (V, E) where V is the set of vertices (nodes) and E is the set of edges (arcs) In a directed graph, each edge is an ordered pair (u, v) where u, v V In an undirected graph, each edge is a set {u, v} For weighted graphs (directed or undirected), each edge is associated with a weight W Vertex v is adjacent to vertex u if and only if (u, v) E for a directed graph, or {u, v} E for an undirected graph Tong Wang UMass Boston CS 31 July 18, 17 / 4

A Directed Graph Example V = {V, V 1, V, V 3, V 4, V 5, V 6 } E = {(V, V 1, ), (V, V 3, 1), (V 1, V 3, 3), (V 1, V 4, 1), (V 3, V 4, ), (V 3, V 6, 4), (V 3, V 5, 8), (V 3, V, ), (V, V, 4), (V, V 5, 5), (V 4, V 6, 6), (V 6, V 5, 1)} v 4 5 v 8 1 4 1 3 1 6 Tong Wang UMass Boston CS 31 July 18, 17 3 / 4

Definitions Path: a sequence of vertices,..., v n connected by edges such that {v i, v i+1 } E for each i = 1,..., n Number of vertices: n Number of edges: m Path length: the number of edges on the path Weighted path length: in a weighted graph, the sum of the costs of the edges on the path Cycle: a path that begins and ends at the same vertex and contains at least one edge Tong Wang UMass Boston CS 31 July 18, 17 4 / 4

Graph Representation Use a -dimensional array called adjacency matrix, a[u][v] = edge cost Nonexistent edges initialized to For sparse graphs, use an adjacency list that contains a list of adjacent indices and weights v 4 5 v 8 1 4 1 3 1 6 Tong Wang UMass Boston CS 31 July 18, 17 5 / 4

Adjacency Matrix or Adjacency List Comparison Winner Test if (x, y) is in graph? adjacency matrix Find the degree of a vertex? adjacency list Less memory on sparse graphs? adjacency list Θ(m + n) vs. Θ(n ) Edge insertion or deletion? adjacency matrices O(1) vs. O(d) Faster to traverse the graph? adjacency list Θ(m + n) vs. Θ(n ) Better for most problems? adjacency list Tong Wang UMass Boston CS 31 July 18, 17 6 / 4

Graph Traversal The most fundamental graph problem is to visit every edge and vertex in a graph in a systematic way Key idea: Mark each vertex when we first visit it, and keep track of what we have not completely explored Each vertex is in one of three states: 1 Undiscovered Discovered: The vertex has been found, but we have not yet checked out all its incident edges 3 Processed: We have visited all its incident edges A data structure is maintained to hold the vertices that we have discovered but not yet completely processed A queue for BFS A stack for DFS Tong Wang UMass Boston CS 31 July 18, 17 7 / 4

Outline of Graph Traversal Initially, only the start vertex s is considered to be discovered Put s in the data structure Remove a vertex u from the data structure of discovered vertices Inspect every edge incident upon u If an edge leads to an undiscovered vertex v, mark v as discovered and add it to the data structure If an edge lead to a processed vertex, ignore this edge If an edge leads to a discovered but not processed vertex, ignore this edge Tong Wang UMass Boston CS 31 July 18, 17 8 / 4

Breadth-First Search Sometimes we want to visit all the adjacent nodes to some node before we visit other nodes. This is called Breadth-first search (BFS). When searching an undirected graph by breadth-first, we assign a direction to each edge Vertex u is the parent of vertex v The start vertex is the root of the search tree All other vertices have exactly one parent Tong Wang UMass Boston CS 31 July 18, 17 9 / 4

Implementation of BFS Associate with a Vertex object A status data member, with possible values of undiscovered, discovered, and processed A parent data member Initialize a queue to hold only the start vertex Mark the start vertex as discovered, with no parent Loop Dequeue a vertex u, mark it as processed Loop through the adjacency list of u for each of the edges (u, v) If v is undiscovered, mark v as discovered and enqueue, and make u the parent of v O(n + m) running time Tong Wang UMass Boston CS 31 July 18, 17 1 / 4

BFS Example, starting at V v visit V (processed): Three outbound edges, to V, V 3 and V 5 visit edge V V (tree edge, since V unseen. Consider V seen now, but not yet processed) v v visit edge V -V 3 (tree edge, V 3 is now discovered) visit edge V -V 5 (tree edge, V 5 is now discovered) v Tong Wang UMass Boston CS 31 July 18, 17 11 / 4

BFS Example visit V (processed): Two outbound edges, to V 1 and V 3 v visit edge V -V 1 (tree edge, V 1 now discovered) v visit edge V -V 3 (non-tree edge, since V 3 discovered before) visit V 3 (processed): 3 outbound edges, to V 4, V 5, and V 6 visit edge V 3 -V 4 (tree edge) v visit edge V 3 -V 5 (non-tree edge, since V 4 discovered before) v visit edge V 3 -V 6 (tree edge) visit V 5 (...)... Tong Wang UMass Boston CS 31 July 18, 17 1 / 4

BFS Starting from V Initialize: Mark first vertex as reachable with edges The graph after all the vertices whose path length from the starting vertex is 1 have been found v 1 v v v 1 1 Tong Wang UMass Boston CS 31 July 18, 17 13 / 4

BFS Starting from V The graph after all the vertices whose path length from the starting vertex is have been found (final) 1 v v 1 1 Tong Wang UMass Boston CS 31 July 18, 17 14 / 4

BFS Example We end up with a BFS with levels, V on the top level, V, V 3 and V 5 on the second level, V 1, V 4 and V 6 on the third level. We have a tree defined by the BFS using a subset of edges from the graph. The nodes at the second level are all one hop away from the source node V. The nodes on the third level are all hops away from V. Thus we are finding how far, in hops, the various nodes are from V. Tong Wang UMass Boston CS 31 July 18, 17 15 / 4

Shortest Paths in Unweighted Graphs The BFS tree gives the shortest paths from the root to all other vertices in unweighted graphs To print the shortest path from root to x Start from x Follow the parent link by recursion, and print the vertex itself after recursion comes back Stop recursion when there is no parent (that is, the root) Tong Wang UMass Boston CS 31 July 18, 17 16 / 4

BFS Application: Connected Components A graph is connected if there is a path between any two vertices A connected component of an undirected graph is a maximal set of vertices such that there is a path between every pair of vertices To find connected components, do BFS and obtain one component, and then repeat with the remaining vertices until all vertices appear in a component Tong Wang UMass Boston CS 31 July 18, 17 17 / 4

Depth-First Search We start from a specific node (determined as a parameter) and go as deep as we can. When we can t go any deeper, we backtrack and continue traversing the rest of the graph depth-first. Replace the queue (FIFO) in BFS by a stack (LIFO) Instead of using a real stack, we can just use the recursive calls where the runtime system maintains the call stacks An edge of the graph is either a DFS tree edge or a back edge linking to an ancestor vertex That is, an edge is either a forward edge or a backward edge O(n + m) runtime Tong Wang UMass Boston CS 31 July 18, 17 18 / 4

Pseudocode for DFS Set up Set of unvisited vertices, initially containing all the vertices. for each vertex v if (v is in unvisited) dfs(v) dfs(vertex v) remove v from unvisited set for each vertex n adjacent to v if (n is in unvisited) dfs(n) Tong Wang UMass Boston CS 31 July 18, 17 19 / 4

Depth-First Search (DFS) DFS starting from V, etc. where we use the lower numbered adjacent vertex first. (We need some way of deciding which edge to visit first. v In implementations, we simply follow the order of elements on the adjacency list for the vertex.) See how we can follow out-edges down and down: v V V V 1 V 3 V 4 V 6 V 5 Tong Wang UMass Boston CS 31 July 18, 17 / 4

Depth-First Search (DFS) We re stuck with no place to go at V 5. We can backtrack back to V 6, back to V 4, back to V 3, see another adjacent vertex, V 6, but already visited, back to V 1, see another adj vertex, V 4, but already visited, back to V, see another adj vertex, V 3, but already visited, back to V, but V and V 5 were already visited. Done. v v Notice that we don t necessarily have to start from V. Tong Wang UMass Boston CS 31 July 18, 17 1 / 4

DFS Traversal Timestamp of a Vertex Let s keep a clock during DFS The clock ticks each time we enter or exit a vertex Each vertex has two additional data members: entry time and exit time If u is an ancestor of v, the entry and exit times of u properly encompass those of v Entering u earlier than v Exiting u later than v Half of the difference between the entry and exit times is the number of decendents in the DFS subtree Tong Wang UMass Boston CS 31 July 18, 17 / 4

Depth-First Search (DFS) We mark every node with two numbers. Whenever we make a step in the DFS we advance a clock by one step. The number on the left is the time step when we first encounter the node (entry time). The number on the right is the last time we encounter it (exit time). 3 /13 3/1 v v 1 v 4 5 1/14 v 4/11 5/1 7 6 7/8 6/9 Tong Wang UMass Boston CS 31 July 18, 17 3 / 4

Another DFS Example For a simpler graph, consider the following: A D B C All the edges point downward. The DFS starting from A visits A, then B, then backtracks and visits C, backtracks back to A and is done for one tree. It starts over at D, but finds no unvisited vertices, so D is alone as the second tree. So the DFS visitation order is A, B, C, D. Notify vertices in DFS visitation order Tong Wang UMass Boston CS 31 July 18, 17 4 / 4

DFS Application: Finding Cycles in Undirected Graphs When going from u to v 1 If v is undiscovered, the edge (u, v) becomes a tree edge We make a recursive DFS call If v is discovered and the parent of u, we would be following the tree edge backwards to where we just came from So don t go there just move on to the next edge in the adjacency list 3 If v is discovered but not the parent of u, we have found a cycle We can print the cycle using the parent links The second case is a spurious two-vertex cycle (v, u, v) Tong Wang UMass Boston CS 31 July 18, 17 5 / 4

DFS on Directed Graphs During DFS on a directed graph, an edge (u, v) is 1 Tree edge: parent[v] is u Back edge: discovered[v] &&!processed[v] 3 Forward edge: processed[v] && entrytime[v] > entrytime[u] 4 Cross edge: processed[v] && entrytime[v] < entrytime[u] During DFS of an undirected graph, every edge in the graph is either a tree edge or a back edge to an ancestor in the DFS tree The same DFS algorithm works for both undirected and directed graphs The graph has a cycle DFS has a back edge Tong Wang UMass Boston CS 31 July 18, 17 6 / 4

Acyclic Directed Graph (DAG) A cycle in a directed graph is a path that returns to its starting vertex An acyclic directed graph is also called a DAG These graphs show up in lots of applications Example, the graph of course prerequisites It is a DAG, since a cycle in prerequisites would be ridiculous CS11 CS1 CS31 CS4 Tong Wang UMass Boston CS 31 July 18, 17 7 / 4

DAGs A DAG induces a partial order on the nodes Not all element pairs have an order, but some do, and the ones that do must be consistent So CS11 < CS1 < CS31, and so CS11 < CS31, but CS1 and CS4 have no order between them Suppose a student takes only one course per term in CS A sequence that satisfies the partial order requirements, for example, is CS11, CS1, CS4, CS31 Another possible sequence is CS11, CS4, CS1, CS31 One of these fully ordered sequences that satisfy a partial order (DAG) is called a topological sort of the DAG Tong Wang UMass Boston CS 31 July 18, 17 8 / 4

Topological Sort A topological sort orders the nodes such that if there is a path from u to v, then u will appear before v The vertices of the graph are ordered on a straight line such that all edges point from left to right Each DAG has at least one topological sort A topological sort gives us an ordering to process each vertex before any of its successors Tong Wang UMass Boston CS 31 July 18, 17 9 / 4

Topological Sort Example CLRS.7 Tong Wang UMass Boston CS 31 July 18, 17 3 / 4

Use DFS to Find a Topological Sort Start DFS from a vertex with in-degree A back edge during DFS indicates that there is a loop The graph is not DAG Ordering the vertices in the reverse order that they are processed (reverse order of the exit time) is a topological sort Consider the edge (u, v) when we process the vertex u 1 If v is undiscovered, we will start a recursive DFS from v. So v will be completely processed before u. So u will appear before v in the listing, as it must. If v is processed, and because u is not yet completely processed, u will appear before v in the listing, as it must. 3 If v is discovered but not yet completely processed, then (u, v) is a back edge, which is forbidden in a DAG. Tong Wang UMass Boston CS 31 July 18, 17 31 / 4

DFS Example There is only one topological sort of this graph: (G, A, B, C, F, E, D) Tong Wang UMass Boston CS 31 July 18, 17 3 / 4

Another Method to Find a Topological Sort The textbook presents a non-recursive algorithm for finding a topological sort of a DAG, checking that it really has no cycles The first step of this algorithm is to determine the in-degree of all vertices in the graph The in-degree of a vertex is the number of edges in the graph with this vertex as the destination-vertex Once we have all the in-degrees for the vertices, we look for a vertex with in-degree Because it has no incoming edges, it can be the vertex at the start of a topological sort, like CS11 Tong Wang UMass Boston CS 31 July 18, 17 33 / 4

Finding a Topological Sort Note that there must be a node with in-degree If there weren t, then we could start a path anywhere, extend backwards along some in-edge from another vertex and from there to another, etc Eventually we would have to start repeating vertices For example, if we have managed to avoid repeating vertices and have visited all the vertices, then the last vertex still has an in-edge not yet used, and it goes to another vertex, completing a cycle Thus the lack of an in-degree- vertex is a sure sign of a cycle and a DAG doesn t have any cycles Now we have the very first vertex, but what about the rest? Think recursively! Tong Wang UMass Boston CS 31 July 18, 17 34 / 4

Topological Sort Example 1 v 1 v 1 v 3 v 1 3 v v v 1 v 1 3 4 Tong Wang UMass Boston CS 31 July 18, 17 35 / 4

Topological Sort Example v v v v 5 1 1 6 1 v v v v 7 8 The topological order is: V, V, V 1, V 3, V 4, V 6, V 5 Tong Wang UMass Boston CS 31 July 18, 17 36 / 4

Topological Sorting Using an Array of In-degree Values V V 1 V V 3 V 4 V 5 V 6 output 1 1 3 3 V 1 V 1 V1 1 V3 1 1 V4 1 V6 V5 v v Tong Wang UMass Boston CS 31 July 18, 17 37 / 4

Pseudocode for Topological Sort 1 Create a queue q and enqueue all vertices of in-degree Create an empty list t for topologically-sorted vertices 3 Loop while q is not empty 1 Dequeue from q a vertex u and append it to list t Loop over vertices v adjacent to u 3 Decrement in-degree of v 4 If v s in-degree becomes, enqueue v in q 4 Return topologically-sorted list t Tong Wang UMass Boston CS 31 July 18, 17 38 / 4

What Happens If There is a Cycle? The presence of a loop means in-degree contributions of one for all members of the cycle, so the cycle protects the whole group from being put on the queue. For example, consider A B C A and see in-degree = 1 for all nodes. Each pass of the loop dequeues an element from the queue, so all that can happen is that the queue goes empty with the cycle members still un-enqueued. Use a counting trick to add cycle-detection to this algorithm Tong Wang UMass Boston CS 31 July 18, 17 39 / 4

Pseudocode for Topological Sort with Cycle Detection 1 Create a queue q and enqueue all vertices of in-degree Create an empty list t for topologically-sorted vertices 3 Set loopcount to 4 Loop while q is not empty 1 Increment loopcount Dequeue from q a vertex u and append it to list t 3 Loop over vertices v adjacent to u 4 Decrement in-degree of v 5 If v s in-degree becomes, enqueue v in q 5 If loopcount < number of nodes in graph, return cycle-in-graph 6 Return topologically-sorted list t Tong Wang UMass Boston CS 31 July 18, 17 4 / 4

DFS Some Comments We can do DFS of any directed (or undirected) graph, and if it s acyclic, DFS yields a topological sort If there is a cycle in the graph, it doesn t cause an infinite loop because DFS doesn t revisit a vertex In general DFS works in phases, finding trees, so the whole thing finds a forest The ability of DFS to turn a graph into a forest of trees is useful in many algorithms Trees are a lot easier to work with than general graphs Tong Wang UMass Boston CS 31 July 18, 17 41 / 4

DFS versus BFS DFS is like preorder tree traversal, plunging further and away from the source node until we can t go any further, then back Can detect cycles Can do topological sort of an acyclic graph BFS: explore nodes adjacent to the source node, then nodes adjacent to those (that haven t been visited yet), and so on Good for finding all neighbors, all neighbors of neighbors, etc. hop counts Tong Wang UMass Boston CS 31 July 18, 17 4 / 4