Algorithm Design and Analysis

Similar documents
Algorithm Design and Analysis

Algorithm Design and Analysis

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

Algorithms: Lecture 10. Chalmers University of Technology

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

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

Copyright 2000, Kevin Wayne 1

3.1 Basic Definitions and Applications

Algorithm Design and Analysis

COT 6405 Introduction to Theory of Algorithms

Copyright 2000, Kevin Wayne 1

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

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

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

CS4800: Algorithms & Data Jonathan Ullman

Lecture 3: Graphs and flows

Graph Algorithms Using Depth First Search

Graph: representation and traversal

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

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

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

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

Graphs & Digraphs Tuesday, November 06, 2007

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)

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

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

Elementary Graph Algorithms

Review: Graph Theory and Representation

3. GRAPHS. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley Copyright 2013 Kevin Wayne. Last updated on Sep 8, :19 AM

Trees. Arash Rafiey. 20 October, 2015

Searching in Graphs (cut points)

Algorithm Design and Analysis

Design and Analysis of Algorithms

CS 361 Data Structures & Algs Lecture 13. Prof. Tom Hayes University of New Mexico

CS/COE

(Re)Introduction to Graphs and Some Algorithms

Introduction to Algorithms

3.1 Basic Definitions and Applications

Graph Algorithms: Chapters Part 1: Introductory graph concepts

CS 310 Advanced Data Structures and Algorithms

Graph Search Methods. Graph Search Methods

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

DFS & STRONGLY CONNECTED COMPONENTS

Graph Search Methods. Graph Search Methods

CS 206 Introduction to Computer Science II

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

CSI 604 Elementary Graph Algorithms

Introduction to Algorithms

Homework Assignment #3 Graph

Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search

Introduction to Algorithms

22.3 Depth First Search

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Lecture 9 Graph Traversal

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

Representations of Graphs

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

Graph Representation

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

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

Graph Algorithms. Definition

TIE Graph algorithms

Announcements Problem Set 4 is out!

CSC263 Week 8. Larry Zhang.

Breadth First Search. cse2011 section 13.3 of textbook

Lecture 10 Graph algorithms: testing graph properties

Goals! CSE 417: Algorithms and Computational Complexity!

Minimum Spanning Trees Ch 23 Traversing graphs

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

GRAPHS (Undirected) Graph: Set of objects with pairwise connections. Why study graph algorithms?

Topic 12: Elementary Graph Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Graph. Vertex. edge. Directed Graph. Undirected Graph

W4231: Analysis of Algorithms

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

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

Chapter 22. Elementary Graph Algorithms

Figure 1: A directed graph.

Broadcast: Befo re 1

TIE Graph algorithms

GRAPHS Lecture 17 CS2110 Spring 2014

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

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Data Structures and Algorithms. Chapter 7. Graphs

CSE 100: GRAPH ALGORITHMS

Objec&ves. Wrap up: Implemen&ng BFS and DFS Graph Applica&on: Bipar&te Graphs. Get out your BFS implementa&on handouts. Feb 2, 2018 CSCI211 - Sprenkle

Chapter 3. Graphs. 3.1 Basic Definitions and Applications

Fundamental Algorithms

CS 4407 Algorithms Lecture 5: Graphs an Introduction

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

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

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 6: BFS and Dijkstra s

Applications of BDF and DFS

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

22.1 Representations of graphs

1. Graph and Representation


Graphs and Graph Algorithms. Slides by Larry Ruzzo

Transcription:

Algorithm Design and Analysis LECTURE 5 Exploring graphs Adam Smith 9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Puzzles Suppose an undirected graph G is connected. True or false? G has at least n-1 edges. Suppose that an undirected graph G has exactly n-1 edges (and no self-loops) True or false? G is connected. What if G has n-1 edges and no cycles? 9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Paths and Connectivity Def. A path in an undirected graph G = (V, E) is a sequence P of nodes v 1, v 2,, v k-1, v k with the property that each consecutive pair v i, v i+1 is joined by an edge in E. Def. A path is simple if all nodes are distinct. Def. An undirected graph is connected if for every pair of nodes u and v, there is a path between u and v. 3

Cycles Def. A cycle is a path v 1, v 2,, v k-1, v k in which v 1 = v k, k > 2, and the first k-1 nodes are all distinct. cycle C = 1-2-4-5-3-1 4

Trees Def. An undirected graph is a tree if it is connected and does not contain a cycle. Theorem. Let G be an undirected graph on n nodes. Any two of the following statements imply the third. G is connected. G does not contain a cycle. G has n-1 edges. 5

Rooted Trees Rooted tree. Given a tree T, choose a root node r and orient each edge away from r. Importance. Models hierarchical structure. root r parent of v v child of v a tree the same tree, rooted at 1 6

Phylogeny Trees Phylogeny trees. Describe evolutionary history of species. 7

Exploring a graph Classic problem: Given vertices s,t V, is there a path from s to t? Idea: explore all vertices reachable from s Two basic techniques: Breadth-first search (BFS) Explore children in order of distance to start node Depth-first search (DFS) How to convert these descriptions to precise algorithms? Recursively explore vertex s children before exploring siblings 9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Breadth First Search BFS intuition. Explore outward from s in all possible directions, adding nodes one "layer" at a time. BFS algorithm. L 0 = { s }. L 1 = all neighbors of L 0. s L 1 L 2 L n-1 L 2 = all nodes that do not belong to L 0 or L 1, and that have an edge to a node in L 1. L i+1 = all nodes that do not belong to an earlier layer, and that have an edge to a node in L i. Theorem. For each i, L i consists of all nodes at distance exactly i from s. There is a path from s to t iff t appears in some layer. 9

Breadth First Search Property. Let T be a BFS tree of G = (V, E), and let (x, y) be an edge of G. Then the level of x and y differ by at most 1. L 0 L 1 L 2 L 3 10

Connected Component Connected component. Find all nodes reachable from s. Connected component containing node 1 = { 1, 2, 3, 4, 5, 6, 7, 8 }. 11

Flood Fill Flood fill. Given lime green pixel in an image, change color of entire blob of neighboring lime pixels to blue. Node: pixel. Edge: two neighboring lime pixels. Blob: connected component of lime pixels. recolor lime green blob to blue 12

Flood Fill Flood fill. Given lime green pixel in an image, change color of entire blob of neighboring lime pixels to blue. Node: pixel. Edge: two neighboring lime pixels. Blob: connected component of lime pixels. recolor lime green blob to blue 13

Connected Component Connected component. Find all nodes reachable from s. s R u v it's safe to add v Theorem. Upon termination, R is the connected component containing s. BFS = explore in order of distance from s. DFS = explore in a different way. 14

Implementing Traversals Generic traversal algorithm 1. R = {s} 2. While there is an edge (u,v) where u R and v R, Add v to R To implement this, need to choose Graph representation Data structures to track Vertices already explored Edge to be followed next These choices affect the order of traversal 9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Adjacency-matrix representation The adjacency matrix of a graph G = (V, E), where V = {1, 2,, n}, is the matrix A[1.. n, 1.. n] given by A[i, j] = A 1 2 3 4 1 if (i, j) E, 0 if (i, j) E. 2 1 1 2 0 1 1 0 0 0 1 0 Storage: ϴ(V 2 ) Good for dense graphs. 3 4 3 4 0 0 0 0 0 0 1 0 Lookup: O(1) time List all neighbors: O( V ) 9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Adjacency list representation An adjacency list of a vertex v V is the list Adj[v] of vertices adjacent to v. 2 1 3 4 Adj[1] = {2, 3} Adj[2] = {3} Adj[3] = {} Adj[4] = {3} For undirected graphs, Adj[v] = degree(v). For digraphs, Adj[v] = out-degree(v). How many entries in lists? 2 E Total ϴ(V + E) storage good for sparse graphs. Typical notation: n = V = # vertices m = E = # edges 9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

BFS with adjacency list rep. Discovered[1.. n]: array of bits (explored or not) initialized to all zeros Queue Q initialized to empty Tree T initialized to empty 9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

BFS pseudocode BFS(s): 1. Set Discovered[s]=1 2. Add s to Q 3. While (Q not empty) a) Dequeue (u) b) For each edge (u,v) adjacent to u a) IF Discovered[v]= false then a) Set Discovered[v] =true b) Add edge (u,v) to tree T c) Add v to Q 9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Theorem: BFS takes O(m+n) time BFS(s): 1. Set Discovered[s]=1 2. Add s to Q 3. While (Q not empty) a) Dequeue (u) b) For each edge (u,v) adjacent to u a) IF Discovered[v]= false then a) Set Discovered[v] =true b) Add edge (u,v) to tree T c) Add v to Q O(1) time, run once overall. O(1) time, run once per vertex O(1) time per execution, run at most twice per edge Total: O(m+n) time (linear in input size) 9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Notes If s is the roof of BFS tree For every vertex u, path in BFS tree from s to u is a shortest path in G depth in BFS tree = distance from u to s Proof of BFS correctness: see KT, Chapter 3. 9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne