Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Similar documents
COMP 182 Algorithmic Thinking. Breadth-first Search. Luay Nakhleh Computer Science Rice University

22.1 Representations of graphs

LECTURE 17 GRAPH TRAVERSALS

Graph Search Methods. Graph Search Methods

Outlines: Graphs Part-2

Fundamental Algorithms

(Re)Introduction to Graphs and Some Algorithms

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

Graph Representations and Traversal

Trees. Arash Rafiey. 20 October, 2015

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

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

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Lecture 3: Graphs and flows

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

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

Graph Search Methods. Graph Search Methods

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

3.1 Basic Definitions and Applications

Graph: representation and traversal

W4231: Analysis of Algorithms

COT 6405 Introduction to Theory of Algorithms

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

CS 206 Introduction to Computer Science II

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

CS 206 Introduction to Computer Science II

Graph Representation

Algorithms: Lecture 10. Chalmers University of Technology

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

Graph Algorithms. Definition

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

GRAPHS Lecture 17 CS2110 Spring 2014

Algorithm Design and Analysis

Breadth First Search. cse2011 section 13.3 of textbook

Copyright 2000, Kevin Wayne 1

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

Announcements Problem Set 4 is out!

Algorithm Design and Analysis

1 The Shortest Path Problem

Introduction to Algorithms. Lecture 11

Algorithms and Theory of Computation. Lecture 7: Priority Queue

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

CSC263 Week 8. Larry Zhang.

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

CSE 100: GRAPH ALGORITHMS

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

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

Lecture 8: PATHS, CYCLES AND CONNECTEDNESS

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

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

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

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

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

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

DESIGN AND ANALYSIS OF ALGORITHMS

Dijkstra s Algorithm and Priority Queue Implementations. CSE 101: Design and Analysis of Algorithms Lecture 5

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

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

Review: Graph Theory and Representation

Unweighted Graphs & Algorithms

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Graph implementations :

TIE Graph algorithms

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

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

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

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

Parallel Breadth First Search

Homework Assignment #3 Graph

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

Some major graph problems

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

Basic Graph Algorithms

Algorithm Design and Analysis

Unweighted directed graphs

Depth First Search (DFS)

Module 11: Additional Topics Graph Theory and Applications

Figure 1: A directed graph.

Outline. Graphs. Divide and Conquer.

CS 561, Lecture 10. Jared Saia University of New Mexico

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

Introduction to Graphs. CS2110, Spring 2011 Cornell University

CS Algorithms and Complexity

csci 210: Data Structures Graph Traversals

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

TIE Graph algorithms

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

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

CSE 100: GRAPH SEARCH

Applications of BDF and DFS

GRAPHS Lecture 19 CS2110 Spring 2013

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

Elementary Graph Algorithms

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Introduction to Algorithms

22.3 Depth First Search

Graph Search. CS/ECE 374: Algorithms & Models of Computation, Fall Lecture 15. October 18, 2018

Graph Search. Algorithms & Models of Computation CS/ECE 374, Fall Lecture 15. Thursday, October 19, 2017

Announcements. HW3 is graded. Average is 81%

Transcription:

Algorithms and Theory of Computation Lecture 3: Graph Algorithms Xiaohui Bei MAS 714 August 20, 2018 Nanyang Technological University MAS 714 August 20, 2018 1 / 18

Connectivity In a undirected graph G = (V, E) A path is a sequence P of vertices v 1, v 2,..., v k V such that {v i, v i+1 } E for any 1 i < k. A cycle is a path v 1, v 2,..., v k with k 2 and v 1 = v k. Graph G is connected if for every pair of vertices u and v, there is a path from u to v. A connected component containing u is the set of all vertices connected to u. All carry over naturally to directed graphs, except connectivity. A directed graph is strongly connected if, for every two vertices u and v, there is a path from u to v and a path from v to u. Nanyang Technological University MAS 714 August 20, 2018 2 / 18

Trees Tree An undirected graph G is a tree if it is connected and does not contain a cycle. 8 1 9 7 1 2 4 2 5 7 5 3 3 4 8 9 Proposition Figure: Two drawings of the same tree. The right one is rooted at vertex 1. Every tree has exactly n 1 edges. Nanyang Technological University MAS 714 August 20, 2018 3 / 18

Graph Traversal s t Connectivity Problem Given a graph G = (V, E) and two particular vertices u and v. Is there a path from u to v? Traversal Problem Given a graph G = (V, E) and a vertex v. Find all vertices that can be reached from v. Nanyang Technological University MAS 714 August 20, 2018 4 / 18

Basic Graph Search Algorithm Algorithm: Explore(u): Initialize R = {u}; while there is an edge (x, y) with x R and y / R do Add y to R; return R. Proposition Explore(u) returns the connected component that contains u. Naive search: O(m) time for each scan, O(mn) time in total. Nanyang Technological University MAS 714 August 20, 2018 5 / 18

Basic Graph Search Algorithm Algorithm: SmartExplore(u): Initialize R = {u}; Mark all vertices as unvisited; Mark u as visited; while R is not empty do Pick one vertex x in R, remove x from R; foreach vertex y Adj(x) do if y is not visited then Mark y as visited and add y to R; return the set of all visited vertices. Runs in O(m + n) time! How to determine which vertex to pick in R? Nanyang Technological University MAS 714 August 20, 2018 6 / 18

The Data Structure Alternative 1: Queue First in first out (FIFO) Breadth First Search (BFS) Exploring distances Alternative 2: Stack Last in first out (LIFO) Depth First Search (DFS) Exploring graph structure Nanyang Technological University MAS 714 August 20, 2018 7 / 18

Breadth First Search Queue A queue is a linked list with two operations Enqueue(Q, x): insert an element x at the rear of the queue Q. Dequeue(Q): remove the front element of the queue. Implementation: linked list with two pointers array with two pointers Nanyang Technological University MAS 714 August 20, 2018 8 / 18

Breadth First Search Algorithm: BFS(u): Initialize queue Q to be empty; Mark all vertices as unvisited; Initialize search tree T to be empty; Mark u as visited and enqueue(q, u); while Q is not empty do x = dequeue(q); foreach vertex y Adj(x) do if y is not visited then add edge (x, y) to T; Mark y as visited and enqueue(q, y); Proposition BFS(u) runs in O(m + n) time. Nanyang Technological University MAS 714 August 20, 2018 9 / 18

BFS: An Example 6 1 4 5 2 3 2 3 8 4 5 8 7 1 7 6 BFS tree is the set of black edges. Nanyang Technological University MAS 714 August 20, 2018 10 / 18

Breadth First Search with Distances Algorithm: BFS(u): Initialize queue Q to be empty; Mark all vertices as unvisited; set dist(v) = for each v; Initialize search tree T to be empty; Mark u as visited and enqueue(q, u); dist(u) = 0; while Q is not empty do x = dequeue(q); foreach vertex y Adj(x) do if y is not visited then add edge (x, y) to T; Mark y as visited and enqueue(q, y); dist(y) = dist(x) + 1; Nanyang Technological University MAS 714 August 20, 2018 11 / 18

Shortest Distance Properties 1 If dist(u) < dist(v), then u is visited before v. 2 If e = (u, v) is an edge of G, then dist(u) dist(v) 1. The shortest distance δ(u, v) between two vertices u and v in an unweighted graph G is the length of a shortest path (in terms of # of edges) from u to v. no path between u and v means δ(u, v) = Nanyang Technological University MAS 714 August 20, 2018 12 / 18

Shortest Distance Proposition Upon termination of BFS(u), for every vertex v, dist(v) = δ(u, v). Proof. Induction over the number of steps dist(v) δ(u, v): true for v when we enqueue(v) = also true for v s neighbors dist(v) δ(u, v): trivially true in the beginning assume true for all v with δ(u, v) k pick any vertex v with δ(u, v) = k + 1 let v be the predecessor of v on a shortest path u to v = δ(u, v) = δ(u, v ) + 1 dist(v ) δ(u, v ), dist(v) dist(v ) 1 = dist(v) δ(u, v) Nanyang Technological University MAS 714 August 20, 2018 13 / 18

BFS Intuition start with vertex u list all its neighbors (distance 1) list all their neighbors (distance 2) etc. Nanyang Technological University MAS 714 August 20, 2018 14 / 18

BFS Summary Runs in time O(m + n) on adjacency lists. Visit every vertex reachable from u. Can be used to compute shortest paths from u to all other vertices in unweighted graphs. Nanyang Technological University MAS 714 August 20, 2018 15 / 18

Depth First Search A versatile graph exploration strategy. Power of DFS to understand the structure of the graph is demonstrated by Hopcroft and Tarjan. Can be used to solve many nontrivial problems in linear time (O(m + n)). Finding cut-edges and cut-vertices of undirected graphs. Finding strong connected components of directed graphs. Testing whether a graph is planar. Basic Graph Search Algorithm with a stack. Nanyang Technological University MAS 714 August 20, 2018 16 / 18

Depth First Search Stack A stack is a linked list with two operations Push(S, x): insert an element at the front of the stack. Pop(S): remove the front element of the stack. Elements are processed in a last-in first-out (LIFO) order, different from the first-in first-out (FIFO) order for queues. Implementation: need to maintain only the pointer of the front of the stack. Useful to also have Peek(S): retrieve Nanyang Technological University MAS 714 August 20, 2018 17 / 18

Depth First Search with Visit Times Algorithm: DFS(u): Initialize stack S to be empty; Mark all vertices as unvisited; set Time = 0; Initialize search tree T to be empty; Mark u as visited and push(s, u); pre(u) = ++Time; while S is not empty do x = peek(s); foreach vertex y Adj(x) do if y is not visited then Mark y as visited and push(s, y); pre(y) = ++Time; if there is no such y then post(x) = ++Time; pop(s); Nanyang Technological University MAS 714 August 20, 2018 18 / 18