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

Similar documents
Graph Representation

Graph: representation and traversal

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

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

csci 210: Data Structures Graph Traversals

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

Basic Graph Algorithms

Graph representation

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

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

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

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

Elementary Graph Algorithms

Graph Search. Adnan Aziz

W4231: Analysis of Algorithms

Algorithm Design and Analysis

Design and Analysis of Algorithms

Chapter 22. Elementary Graph Algorithms

Graph: representation and traversal

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

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

Representations of Graphs

CSI 604 Elementary Graph Algorithms

COT 6405 Introduction to Theory of Algorithms

csci 210: Data Structures Graph Traversals

Homework Assignment #3 Graph

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

Graph Algorithms. Definition

Review: Graph Theory and Representation

Graph implementations :

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Sample Solutions to Homework #4

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

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Outlines: Graphs Part-2

Announcements Problem Set 4 is out!

Minimum Spanning Trees Ch 23 Traversing graphs

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

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

DFS & STRONGLY CONNECTED COMPONENTS

Data Structures and Algorithms. Chapter 7. Graphs

Lecture 9 Graph Traversal

Graph Algorithms: Chapters Part 1: Introductory graph concepts

CSE 2331/5331. Topic 9: Basic Graph Alg. Representations Basic traversal algorithms Topological sort CSE 2331/5331

CSC263 Week 8. Larry Zhang.

Data Structures and Algorithms. Werner Nutt

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

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

Unit 2: Algorithmic Graph Theory

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

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

Chapter 22 Elementary Graph Algorithms

Graph Algorithms Using Depth First Search

Announcements. HW3 is graded. Average is 81%

Outline. Graphs. Divide and Conquer.

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

Introduction to Algorithms. Lecture 11

Algorithm Design and Analysis

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

TIE Graph algorithms

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

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

CS 310 Advanced Data Structures and Algorithms

22.3 Depth First Search

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

Figure 1: A directed graph.

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).

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

Graph Algorithms. Andreas Klappenecker

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

22.1 Representations of graphs

Copyright 2000, Kevin Wayne 1

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

TIE Graph algorithms

Elementary Graph Algorithms

CSE 421 DFS / Topological Ordering

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree

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

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

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

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

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

Inf 2B: Graphs, BFS, DFS

CSE 100: GRAPH ALGORITHMS

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.

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

Graph Representations and Traversal

Design and Analysis of Algorithms

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

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

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

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

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

Basic Graph Definitions

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

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

Topic 12: Elementary Graph Algorithms

Graph Search Methods. Graph Search Methods

Transcription:

Basic Graph Algorithms (CLRS B.-B.,.-.) Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices V and a finite set of edges E. Directed graphs: E is a set of ordered pairs of vertices (u,v) where u,v V V = {,,,,, } E = {(,), (,), (,), (,) (,), (,), (,), (,)} Undirected graph: E is a set of unordered pairs of vertices {u,v} where u,v V V = {,,,,, } E = {{,}, {,}, {,}, {,}} Edge (u,v) is incident to u and v Degree of vertex in undirected graph is the number of edges incident to it. In (out) degree of a vertex in directed graph is the number of edges entering (leaving) it. A path from u to u is a sequence of vertices < u =v,v,v,,v k =u > such that (v i,v i+ ) E (or {v i,v i+ } E) We say that u is reachable from u The length of the path is k It is a cycle if v = v k An undirected graph is connected if every pair of vertices are connected by a path The connected components are the equivalence classes of the vertices under the reachability relation. (All connected pair of vertices are in the same connected component).

A directed graph is strongly connected if every pair of vertices are reachable from each other The strongly connected components are the equivalence classes of the vertices under the mutual reachability relation. Graphs appear all over the place in all kinds of applications, e.g: Trees ( E = V ) Connectivity/dependencies (house building plans, WWW-page connections = internet graph) Often the edges (u,v) in a graph have weights w(u,v), e.g. Road networks (distances) Cable networks (capacity). Representation Adjacency-list representation: Array of V list of edges incident to each vertex. Examples: Note: For undirected graphs, every edge is stored twice. If graph is weighted, a weight is stored with each edge. Adjacency-matrix representation:

V V matrix A where Examples: a ij = { if (i,j) E otherwise j i j i Note: For undirected graphs, the adjacency matrix is symmetric along the main diagonal (A T = A). If graph is weighted, weights are stored instead of one s. Comparison of matrix and list representation: Adjacency list Adjacency matrix O( V + E ) space O( V ) space Good if graph sparse ( E << V ) Good if graph dense ( E V ) No quick access to (u,v) O() access to (u,v) We will use adjacency list representation unless stated otherwise (O( V + E ) space). Graph traversal There are two standard (and simple) ways of traversing all vertices/edges in a graph in a systematic way Breadth-first Depth-first We can use them in many fundamental algorithms, e.g finding cycles, connected components,...

. Breadth-first search (BFS) Main idea: Start at some source vertex s and visit, All vertices at distance, Followed by all vertices at distance, Followed by all vertices at distance,. BFS corresponds to computing shortest path distance (number of edges) from s to all other vertices. To control progress of our BFS algorithm, we think about coloring each vertex White before we start, Gray after we visit the vertex but before we have visited all its adjacent vertices, Black after we have visited the vertex and all its adjacent vertices (all adjacent vertices are gray). We use a queue Q to hold all gray vertices vertices we have seen but are still not done with. We remember from which vertex a given vertex v is colored gray i.e. the node that discovered v first; this is called parent[v]. Algorithm: BFS(s) color[s] = gray d[s] = ENQUEUE(Q, s) WHILE Q not empty DO OD DEQUEUE(Q, u) FOR (u,v) E DO IF color[v] = white THEN color[v] = gray d[v] = d[u] + parent[v] = u ENQUEUE(Q, v) FI color[u] = black

* + * + &' &',-,- () ()././ "# "# $ % $ % Algorithm runs in O( V + E ) time Example (for directed graph): a) b) a s!! d f Q : Q : c, a c) b c e g d) Q : a, d, e Q : d, e, b e) f) Q : e, b, f Q : b, f g) h) Q : f Q : g i) Q : Note: parent[v] forms a tree; BFS-tree. d[v] contains length of shortest path from s to v. (Prove by induction) We can use parent[v] to find the shortest path from s to a given vertex. If graph is not connected we have to try to start the traversal at all nodes. FOR each vertex u V DO IF color[u] = white THEN BFS(u) OD

Note: We can use algorithm to compute connected components in O( V + E ) time.. Depth-first search (DFS) If we use stack instead of queue Q we get another traversal order; depth-first We go as deep as possible, Go back until we find unexplored adjacent vertex, Go as deep as possible,. Often we are interested in start time and finish time of vertex u Start time (d[u]): indicates at what time vertex is first visited. Finish time (f[u]): indicates at what time all adjacent vertices have been visited. We can write DFS iteratively using the same algorithm as for BFS but with a STACK instead of a QUEUE, or, we can write a recursive DFS procedure We will color a vertex gray when we first meet it and black when we finish processing all adjacent vertices. Algorithm: DFS(u) color[u] = gray d[u] = time time = time + FOR (u,v) E DO OD IF color[v] = white THEN parent[v] = u DFS(v) FI color[u] = black f[u] = time time = time + Algorithm runs in O( V + E ) time As before we can extend algorithm to unconnected graphs and we can use it to detect cycles in O( V + E ) time.

XY XY RS RS FG FG NO NO Z[ Z[ bc bc lm lm pq pq xy xy ~ ~ PQ PQ HI HI \] \] no no } } JK JK LM LM ^_ ^_ `a `a z{ z{ VW VW TU TU :; :; rs rs 7 7 < = < = BC BC fg fg 89 89 >? >? wv wv Example: a) b) a / d f / b c e g / c) d) / / / / / / / e) / / / f) / / / / g) h) / / / / DE D E / / / / de d e / / @ A @ A hi h i jk j k / /7 / i) j) / / /9 / / /7 / /8 /9 / /7 /8 k) l) / / /9 tu t u / /7 /8 / / /9 / /7 /8 / /7 /8 7

ƒ ƒ ˆ ˆ Ž Ž žÿ žÿ Œ Œ œ œ Š Š š š m) n) / / / /9 / / / /9 / /7 /8 / / /7 /8 o) p) / / / /9 / / / /9 / / /7 /8 / / /7 /8 As previously parent[v] forms a tree; DFS-tree Note: If u is descendent of v in DFS-tree then d[v] < d[u] < f[u] < f[v] Topological sorting Definition: Topological sorting of directed acyclic graph G = (V, E) is a linear ordering of vertices V such that (u,v) E u appear before v in ordering. Topological ordering can be used in scheduling: Example: Dressing (arrow implies must come before ) Socks Underwear Shoes Watch Pants Belt Shirt Tie Jacket We want to compute order in which to get dressed. One possibility: 8

Socks Underwear Pants Shoes Watch Shirt Belt Tie Jacket 7 8 9 The given order is one possible topological order. Algorithm: Topological order just reverse DFS finish time ( O( V + E ) running time). Correctness: (u,v) E f(v) < f(u) Proof: When (u,v) is explored by DFS algorithm, v must be white or black (gray cycle). v white: v visited and finished before u is finished f(v) < f(u) v black: v already finished f(v) < f(u) Alternative algorithm: Count in-degree of each vertex and repeatedly number and remove in-degree vertex and its outgoing edges: Homework. 9