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

Similar documents
CSI 604 Elementary Graph Algorithms

Graph Representation

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

Basic Graph Algorithms

Graph: representation and traversal

Design and Analysis of Algorithms

Chapter 22. Elementary Graph Algorithms

Representations of Graphs

COT 6405 Introduction to Theory of Algorithms

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

DFS & STRONGLY CONNECTED COMPONENTS

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Review: Graph Theory and Representation

Graph representation

Graph Search. Adnan Aziz

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

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

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

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Graph Algorithms. Definition

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

Minimum Spanning Trees Ch 23 Traversing graphs

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Algorithm Design and Analysis

Graph implementations :

W4231: Analysis of Algorithms

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

22.3 Depth First Search

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

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

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

Announcements. HW3 is graded. Average is 81%

CS 310 Advanced Data Structures and Algorithms

Graph: representation and traversal

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

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

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

Unit 2: Algorithmic Graph Theory

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

Homework Assignment #3 Graph

csci 210: Data Structures Graph Traversals

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Algorithm Design and Analysis

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

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

Introduction to Algorithms. Lecture 11

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

Homework No. 4 Answers

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

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

Outlines: Graphs Part-2

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

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u).

GRAPHS Lecture 17 CS2110 Spring 2014

Figure 1: A directed graph.

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

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

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

Algorithm Design and Analysis

Data Structures and Algorithms. Chapter 7. Graphs

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

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

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

Graph Representations and Traversal

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

Lecture 3: Graphs and flows

Algorithm Design and Analysis

Definition Example Solution

Data Structures and Algorithms. Werner Nutt

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

Design and Analysis of Algorithms

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

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

GRAPHS Lecture 19 CS2110 Spring 2013

Graph Search Methods. Graph Search Methods

Sample Solutions to Homework #4

Strongly Connected Components

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

csci 210: Data Structures Graph Traversals

Graph Search Methods. Graph Search Methods


Elementary Graph Algorithms

Chapter 22 Elementary 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.

Suggested Study Strategy

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

Theory of Computing. Lecture 4/5 MAS 714 Hartmut Klauck

(Re)Introduction to Graphs and Some Algorithms

Analysis of Algorithms Prof. Karen Daniels

Topic 12: Elementary Graph Algorithms

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

Lecture 9 Graph Traversal

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

TIE Graph algorithms

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

Copyright 2000, Kevin Wayne 1

3.1 Basic Definitions and Applications

Transcription:

Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. Representing a graph: Graph G(V, E): V set of nodes (vertices); E set of edges. Notation: n = V and m = E. (Vertices are numbered 1 through n.) Size of G = V + E = n + m. (Note that m = O(n 2 ).) Degree of a node is the number of edges incident on that node. In a directed graph, each node has an out degree and an in degree. For dense graphs, m = Θ(n 2 ). For sparse graphs, m = o(n 2 ). (Typically, for a sparse graph, m = O(n).) 1

Notes: 1. O( V ) and O(n) are used interchangeably; similarly, O( E ) and O(m) are used interchangeably. 2. Edge between nodes i and j in an undirected graph is denoted by {i, j}. 3. Edge from node i to node j in a directed graph is denoted by (i, j). 4. For an undirected graph G(V, E), v V degree[v] = 2m. 5. For a directed graph G(V, A), Indegree[v] = v V v V (a) Adjacency list representation: Adj[1.. n]: An array of pointers. Outdegree[v] = m. Adj[i] points to a list of nodes; each node stores the number of a node that is adjacent to node i. 2

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. Total storage = O(n + m): linear in the size of the graph. Example: To be presented in class. 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. (b) Adjacency Matrix representation: Uses an n n Boolean matrix M. M[i, j] = 1 if {i, j} is an edge and 0 otherwise. (By convention, M[i, i] = 0, 1 i n.) 3

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} has a weight, a non-boolean matrix can be used instead. Total storage = O(n 2 ): may be quadratic in the size of the graph. Example: To be presented in class. Advantages: 1. To check whether an edge {i, j} E, 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. 4

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. BFS produces a tree (called BFS 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. BFS expands the frontier between visited and unvisited nodes along the breadth of the frontier; that is, 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. Implementation of BFS: Adjacency list representation for graph. A color scheme to remember whether or not a node has been discovered. (a) White: A node that has not yet been discovered. (Initially, all nodes are white.) 5

(b) Black: All nodes adjacent to a black node have been discovered (i.e., a black node has no adjacent white node). (b) Gray: A gray node may have some undiscovered (white) node adjacent to it. Values stored for each node u: Adj[u]: List of adjacent nodes. d[u]: Distance of u from s. Initially, d[u] = for all nodes in V {s} (s: source node). 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. Auxiliary data structure: FIFO Queue Q (to enforce the breadth-first nature of the search). Pseudocode: See handout. Example: To be presented in class. 6

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). BFS and shortest paths: To be discussed in class. Generating shortest paths from BFS tree: See pseudocode for Print-Path in the handout. Running time of Print-Path = O(n). (Each recursive call shortens the path being considered by 1.) Depth-First Search (DFS): The idea is to search deeper in 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. 7

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. For each node u, instead of distance, two time stamp values are stored. (a) d[u]: Time when u is first discovered (i.e., time when the color of u changes to gray). (b) 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 needed 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. 8

Pseudocode for DFS: See handout. Example for DFS: To 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( v V degree(v)) = O(m). So, the running time of DFS is O(n + m). Classification of edges through DFS 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. 9

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). Back edges: Such 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. Self loops in directed graphs are considered to be back edges. Forward edges: Such edges are also not in the DFS tree (or forest). Forward edges are possible only in directed graphs. A forward edge (u, v) joins a node u to a descendant v in the DFS tree. 10

Cross edges: Any edge that is not a tree edge or a back edge or a forward edge. Cross edges are possible only in directed graphs. A cross edge may join a pair of nodes as long as one is not an ancestor of another in the DFS tree. 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. (a) If Color[v] is white, then (u, v) is a tree edge. (Reason: Vertex v is just being discovered.) (b) If Color[v] is 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.) (c) If Color[v] is black, then (u, v) is a forward or cross edge. 11

Example for Case (c): To be presented in class. Note: In Case (c), (u, v) is a forward edge if d[u] < d[v]. (u, v) is a cross edge if d[u] > d[v]. Theorem 1: In a DFS of 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. Applications of DFS: 1. 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. 12

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: See handout. Running time: O(m+n) (because it involves just a DFS). 2. Topological sort of a DAG: Note: DAG Directed Acyclic Graph. 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. Thus, 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. 13

Topological sort is used in situations where an order for a set of events needs to be determined given some precedence constraints. Examples: To be discussed in class. 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. 14