CSI 604 Elementary Graph Algorithms

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

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

Elementary Graph Algorithms

Graph Representation

Graph: representation and traversal

Design and Analysis of Algorithms

Basic Graph Algorithms

Chapter 22. Elementary Graph Algorithms

Representations of Graphs

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

COT 6405 Introduction to Theory of Algorithms

Graph Algorithms. Definition

Graph Search. Adnan Aziz

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

Review: Graph Theory and Representation

Graph representation

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Graph Algorithms: Chapters Part 1: Introductory graph concepts

DFS & STRONGLY CONNECTED COMPONENTS

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

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

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

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

Algorithm Design and Analysis

Graph implementations :

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

W4231: Analysis of Algorithms

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

22.3 Depth First Search

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

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

CS 310 Advanced Data Structures and Algorithms

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

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Outlines: Graphs Part-2

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices

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

GRAPHS Lecture 17 CS2110 Spring 2014

Graph: representation and traversal

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

Unit 2: Algorithmic Graph Theory

Algorithm Design and Analysis

csci 210: Data Structures Graph Traversals

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

Announcements. HW3 is graded. Average is 81%

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

Figure 1: A directed graph.


GRAPHS Lecture 19 CS2110 Spring 2013

Homework No. 4 Answers

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

Introduction to Algorithms. Lecture 11

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

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

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

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

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

Data Structures and Algorithms. Chapter 7. Graphs

Basic Graph Definitions

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

Homework Assignment #3 Graph

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

Lecture 5: Graphs & their Representation

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

(Re)Introduction to Graphs and Some Algorithms

Introduction to Graphs. CS2110, Spring 2011 Cornell University

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

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

Graph Representations and Traversal

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Data Structures and Algorithms. Werner Nutt

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

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

Graph. Vertex. edge. Directed Graph. Undirected Graph

Suggested Study Strategy

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Goals! CSE 417: Algorithms and Computational Complexity!

Sample Solutions to Homework #4

Lecture 3: Graphs and flows

csci 210: Data Structures Graph Traversals

Algorithm Design and Analysis

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

Announcements Problem Set 4 is out!

Final Exam. EECS 2011 Prof. J. Elder - 1 -

TIE Graph algorithms

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

Algorithm Design and Analysis

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

Lecture 9 Graph Traversal

Graduate Algorithms CS F-15 Graphs, BFS, & DFS

Topic 12: Elementary Graph Algorithms

3.1 Basic Definitions and Applications

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Graphs: basic concepts and algorithms

Solutions to Midterm 2 - Monday, July 11th, 2009

Transcription:

CSI 604 Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. (Second edition) 1 / 25

Graphs: Basic Definitions Undirected Graph G(V, E): V is set of nodes (or vertices) and E is the set of (undirected) edges. Undirected graphs represent symmetric binary relationships. Notation: n = V and m = E. (Vertices are generally numbered 1 through n.) Size of G = V + E = n + m. (Note that m = O(n 2 ).) Degree of a node in an undirected graph is the number of edges incident on that node. Note: We assume that graphs are simple (i.e., have no multi-edges or self-loops). 2 / 25

Graphs: Basic Definitions (continued) Directed Graph G(V, A): V is set of nodes and A is the set of directed edges (or arcs). Notation: n = V and m = A. Directed graphs generally represent non-symmetric binary relationships. In a directed graph, each node has an outdegree and an indegree. Some terminology: For dense graphs, m = Ω(n 2 ). For sparse graphs, m = o(n 2 ). (Typically, for sparse graphs, m = O(n).) 3 / 25

Graphs: Basic Definitions (continued) O( V ) and O(n) are used interchangeably; similarly, O( E ) (or O( A )) and O(m) are used interchangeably. The edge between nodes i and j in an undirected graph is denoted by {i, j}. The edge from node i to node j in a directed graph is denoted by (i, j). For an undirected graph G(V, E), degree(v) = 2m. v V For a directed graph G(V, A), Indegree(v) = Outdegree(v) = m. v V v V 4 / 25

Representing Graphs in a Computer I. Adjacency list: Adj[1.. n]: An array of pointers. Adj[i] points to a list of nodes; each node stores the number of a node that is adjacent to node i. For a directed graph, the list for each node stores the outgoing edges from that node. Edge weights can also be stored in the lists. Total number of nodes in all lists = 2m (for undirected graphs) and m (for directed graphs). Total storage = O(n + m): linear in the size of the graph. Example: To be presented in class. 5 / 25

Representing Graphs in a Computer (continued) Adjacency List: Additional Comments Advantages: 1 Size of representation is linear in the size of graph. 2 Particularly suitable for sparse graphs. Disadvantage: To check whether an edge {i, j} E, Θ(n) time is used in the worst case. II. Adjacency Matrix: Uses an n n Boolean matrix M. For an undirected graph: M[i, j] = 1 if {i, j} is an edge and 0 otherwise. (By convention, M[i, i] = 0, 1 i n.) 6 / 25

Representing Graphs in a Computer (continued) Adjacency Matrix (continued): For a directed graph: M[i, j] = 1 if (i, j) is a directed edge and 0 otherwise. (Here, M[i, i] = 1 when there is a self-loop around node i.) The adjacency matrix of an undirected graph is symmetric. The adjacency matrix for a directed graph may not be symmetric. If an edge {i, j} (or directed edge (i, j)) has a weight, a non-boolean matrix can be used instead. Total storage = O(n 2 ): size of the graph. may be quadratic in the Example: To be presented in class. 7 / 25

Representing Graphs in a Computer (continued) Adjacency Matrix: Advantages: Additional Comments 1 To check whether an edge {i, j} E (or a directed edge (i, j) A), O(1) time is sufficient. 2 Suitable for dense graphs. Disadvantage: If the graph is sparse, size of representation may be quadratic in the size of graph. Additional Terminology: An undirected graph G(V, E) is connected if there is a path between every pair of nodes in V. A disconnected graph contains two or more connected components. 8 / 25

Breadth-First Search (BFS) Given a connected graph G and a source node s, BFS systematically explores G to discover all the nodes reachable from s. Produces a tree (called BFS spanning tree) T such that the path from s to any node w in T is a shortest path (in terms of number of edges) in G from s to w. Idea: Expand the frontier between visited and unvisited nodes along the breadth of the frontier. A consequence: Nodes at a distance k from s are discovered before those at a distance of k + 1 or more. Example: To be presented in class. 9 / 25

Breadth-First Search (continued) Implementation of BFS: Adjacency list representation for graph. A color scheme to remember whether or not a node has been discovered. 1 White: A node that has not yet been discovered. (Initially, all nodes are white.) 2 Black: All nodes adjacent to a black node have been discovered (i.e., a black node has no adjacent white node). 3 Gray: A gray node may have some undiscovered (white) node adjacent to it. 10 / 25

Breadth-First Search (continued) Values stored for each node u: Adj[u]: List of adjacent nodes. d[u]: Distance of u from s. (Recall that s is the source node). Initially, d[s] = 0 and d[u] = for each node u V {s}. At the end, for each u V, d[u] gives the shortest path length from s to u. π[u]: Parent of u in the BFS tree. (π[u] is NULL if u has no parent.) Color[u]: Color of node u. 11 / 25

Breadth-First Search (continued) Auxiliary data structure: FIFO Queue Q (to enforce the breadth-first nature of the search). Pseudocode: class.) See handout. (An example will be presented in Running time of BFS: Step 1: O(n) time. Steps 2 and 3: O(1) time. Step 4: O(n + m) time. (Explanation in class.) Overall running time of BFS = O(n + m). 12 / 25

Breadth-First Search (continued) BFS and shortest paths: To be discussed in class. Generating shortest paths from a BFS tree: See pseudocode for Print-Path in the handout. Running time of Print-Path = O(n). (Reason: Each recursive call shortens the path being considered by 1.) 13 / 25

Depth-First Search (DFS) Idea: Search deeper into the graph whenever possible. The graph generated by BFS is a depth-first spanning tree if the graph G is connected; otherwise, it is a forest of trees. Example: To be presented in class. Implementation of DFS: Same color-coding scheme as BFS. Global variable time is used to generate the values of time stamps. 14 / 25

Depth-First Search (continued) Implementation of DFS (continued): For each node u, instead of distance, two time stamp values are stored. d[u]: Time when u is first discovered (i.e., time when the color of u changes to gray). f [u]: Time when the search finishes examining u s adjacency list (i.e., time when the color of u changes to black). Notes on timestamps: 1 Timestamps are integers in the range [1.. 2n]. (2n timestamp values are used because each node gets discovered once and finished once.) 2 For each node u, d[u] < f [u]. 3 The color of u is white before d[u], gray between d[u] and f [u], and black thereafter. 15 / 25

Depth-First Search (continued) Pseudocode for DFS: See handout. (An example will be presented in class.) Running time of DFS: Steps 1 and 2 (Initialization steps): O(n) time. DFS-Visit is called exactly once for each node. The call DFS-Visit(v) takes O (degree(v)) time. So, total time for all calls to DFS-Visit is ( ) O degree(v) = O(m). v V So, the overall running time of DFS is O(n + m). 16 / 25

Classification of edges through DFS I. Tree edges: Edges in the DFS tree (or forest). DFS on an undirected graph: Edge {u, v} is a tree edge if v was discovered when u s adjacency list was being processed or u was discovered when v s adjacency list was being processed. DFS on a directed graph: Edge (u, v) is a tree edge if v was discovered when u s adjacency list was being processed (i.e., while the edge (u, v) was being explored). 17 / 25

Classification of edges through DFS (continued) II. Back edges: Back edges edges are not in the DFS tree (or forest). A back edge joins a node v to an ancestor of v in the DFS tree. Convention: Self loops in directed graphs are considered to be back edges. 18 / 25

Classification of edges... (continued): III. Forward edges: Forward edges are also not in the DFS tree (or forest). Possible only in directed graphs. A forward edge (u, v) joins a node u to a descendant v in the DFS tree. 19 / 25

Classification of edges... (continued): IV. Cross edges: Any edge that is not a tree edge or a back edge or a forward edge. Possible only in directed graphs. A cross edge may join a pair of nodes as long as one is not an ancestor of the other in the DFS tree. 20 / 25

Modifying DFS to Classify Edges For directed graphs only. The modification won t distinguish between forward and cross edges. Uses the colors of nodes. Suppose the directed edge (u, v) is encountered. 1 If Color[v] = white, then (u, v) is a tree edge. (Reason: Node v is just being discovered.) 2 If Color[v] = gray, then (u, v) is a back edge. (Reason: Gray nodes are in the (recursion) stack and v is deeper in the stack than u.) 3 If Color[v] = black, then (u, v) is a forward or cross edge. 21 / 25

Modifying DFS to Classify Edges (continued) Example for Case 3: To be presented in class. Note: In Case 3: (u, v) is a forward edge if d[u] < d[v]. (u, v) is a cross edge if d[u] > d[v]. Theorem 1: When DFS is carried out on an undirected graph G(V, E), every edge in E is either a tree edge or a back edge. Proof: To be presented in class. 22 / 25

Applications of DFS I. Finding connected components: For undirected graphs only. Suppose G(V, E) has t connected components, numbered 1 through t. Algorithm produces array CC[1.. n], where CC[u] is the number of the connected component containing nodes u. Idea: Do a DFS. Whenever the recursive calls to DFS-Visit are completed and a new exploration is started, a new connected component begins. Pseudocode: Running time: See handout. O(m + n) (because it involves just a DFS). 23 / 25

Applications of DFS (continued) II. Topological sort of a Directed Acyclic Graph (DAG): Definition: Given a DAG G, a topological sort of G is a linear arrangement of the nodes so that for each directed edge (u, v), u appears before v. A topological sort is a listing of the nodes on a line so that each directed edge goes from left to right. Such an ordering is not possible if the directed graph contains a cycle. Topological sort is used in situations where a set of events needs to be ordered given some precedence constraints. Examples: To be presented in class. 24 / 25

Topological sort of a DAG (continued) Pseudocode: See handout. Running time: O(m + n). Correctness of the algorithm: Observation 2: When DFS is carried out on a dag, no back edges can arise. Reason: Any back edge indicates the presence of a directed cycle. Theorem 3: When DFS is carried out on a dag, for every directed edge (u, v), f [u] > f [v]. Proof: To be presented in class. 25 / 25