Unweighted directed graphs

Similar documents
22.1 Representations of graphs

Elementary Graph Algorithms

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

Cut vertices, Cut Edges and Biconnected components. MTL776 Graph algorithms

22.3 Depth First Search

Data Structure and Algorithm I Homework #4 Due: 5pm, Sunday, December 12, 2010

COT 6405 Introduction to Theory of Algorithms

Graph Representation

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

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Figure 1: A directed graph.

Slides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai

Outlines: Graphs Part-2

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

Chapter 22: Elementary Graph Algorithms. Definitions:

Elementary Graph Algorithms

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

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

Algorithm Design and Analysis

All Shortest Paths. Questions from exercises and exams

Graph Algorithms. Definition

Basic Graph Algorithms

Chapter 22. Elementary Graph Algorithms

Design and Analysis of Algorithms

Data Structure and Algorithm, Spring 2017 Final Examination

Chapter 24. Shortest path problems. Chapter 24. Shortest path problems. 24. Various shortest path problems. Chapter 24. Shortest path problems

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

W4231: Analysis of Algorithms

CS Yaxin.Huang.HWM10- Solution

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

Graph Representations and Traversal

Classical Shortest-Path Algorithms

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

Graph representation

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

Graph Search. Adnan Aziz

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

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

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Inf 2B: Graphs, BFS, DFS

Unit 2: Algorithmic Graph Theory

CSC263 Week 8. Larry Zhang.

Announcements Problem Set 4 is out!

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

Graph: representation and traversal

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.

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

Introduction to Algorithms. Lecture 11

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Announcements. HW3 is graded. Average is 81%

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

Representations of Graphs

Chapter 22 Elementary Graph Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

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

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

Graph Algorithms: Chapters Part 1: Introductory graph concepts

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

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

Sample Solutions to Homework #4

Graph Search Methods. Graph Search Methods

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

Graph Search Methods. Graph Search Methods

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

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

Homework No. 4 Answers

Graph implementations :

Graphs & Digraphs Tuesday, November 06, 2007

(Re)Introduction to Graphs and Some Algorithms

Fundamental Graph Algorithms Part Four

Design and Analysis of Algorithms

CS 206 Introduction to Computer Science II

Data Structures and Algorithms. Werner Nutt

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

Copyright 2000, Kevin Wayne 1

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

Definition Example Solution

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

Trees. Arash Rafiey. 20 October, 2015

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

Algorithm Design and Analysis

Lecture 3: Graphs and flows

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

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

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

CSE 100: GRAPH ALGORITHMS

Strongly Connected Components

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

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

CS 310 Advanced Data Structures and Algorithms

Breadth First Search. cse2011 section 13.3 of textbook

Algorithms: Lecture 10. Chalmers University of Technology

Data Structures and Algorithms. Chapter 7. Graphs

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

Lecture 25 Spanning Trees

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

Graphs: basic concepts and algorithms

Lecture 9 Graph Traversal

Algorithm Design and Analysis

Transcription:

Unweighted directed graphs

Announcements Midterm & gradescope - will get an email today to register (username name is your email) - tests should appear next Tuesday (nothing there now)

Graph A directed graph G is a set of edges and vertices: G = (V, E) Two common ways to represent a graph: a b -Adjacency matrix -Adjacency list c d

Graph An adjacency matrix has a 1 in row i and column j if you can go from node i to node j

Graph An adjacency list just makes lists out of each row (list of edges out from every vertex)

Graph Difference between adjacency matrix and adjacency list?

Graph Difference between adjacency matrix and adjacency list? Matrix is more memory O( V 2 ), less computation: O(1) lookup List is less memory O(E+V) if sparse, more computation: O(branch factor)

Graph Adjacency matrix, A=A 1, represents the number of paths from row node to column node in 1 step Prove: A n is the number of paths from row node to column node in n steps

Graph Proof: Induction Base: A 0 = I, 0 steps from i is i Induction: (Assume A n, show A n+1 ) Let a n i,j = ith row, j th column of A n Then a n+1 i,j = k an i,k a1 k,j This is just matrix multiplication

https://www.youtube.com/watch?v=ni0dt288vls Breadth First Search Overview Create first-in-first-out (FIFO) queue to explore unvisited nodes

Breadth First Search Overview Consider the graph below Suppose we wanted to get from a to c using breadth first search

BFS Overview To keep track of which nodes we have seen, we will do: White nodes = never seen before Grey nodes = nodes in Q Black nodes = nodes that are done To keep track of who first saw nodes I will make red arrows (π in book)

BFS Overview First, we add the start to the queue, so Q = {a} Then we will repeatedly take the left-most item in Q and add all of its neighbors (that we haven't seen yet) to the Q on the right

BFS Overview Q = {a} Left-most = a White neighbors = b & d New Q = {b, d}

BFS Overview Q = {b, d} Left-most = b White neighbors = e New Q = {d, e}

BFS Overview Q = {d, e} Left-most = d White neighbors = c & f & g New Q = {e, c, f, g}

BFS Overview Q = {e, c, f, g} Left-most = e White neighbors = (none) New Q = {c, f, g}

BFS Overview Q = {c, f, g} Left-most = c Done! We found c, backtrack on red arrows to get path from a

Depth First Search Overview Create first-in-last-out (FILO) queue to explore unvisited nodes

Depth First Search Overview You can solve mazes by putting your left-hand on the wall and following it (i.e. left turns at every intersection)

Depth First Search Overview You can solve mazes by putting your left-hand on the wall and following it (i.e. left turns at every intersection)

Depth First Search Overview This is actually just depth first search D A F E C G I H B J

BFS and DFS in trees Solve problems by making a tree of the state space max min max

BFS and DFS in trees Often times, fully exploring the state space is too costly (takes forever) Chess: 10 47 states (tree about 10 123 ) Go: 10 171 states (tree about 10 360 ) At 1 million states per second... Chess: 10 109 years (past heat death Go: 10 346 years of universe)

BFS and DFS in trees BFS prioritizes exploring DFS prioritizes exploiting White to move Black to move

BFS and DFS in trees BFS benefits? DFS benefits?

BFS and DFS in trees BFS benefits? -if stopped before full search, can evaluate best found DFS benefits? -uses less memory on complete search

BFS and DFS in graphs BFS: shortest path from origin to any node DFS: find graph structure Both running time of O(V+E)

Breadth first search BFS(G,s) // to find shortest path from s for all v in V v.color=white, v.d=,v.π=nil s.color=grey, v.d=0 Enqueue(Q,s) while(q not empty) u = Dequeue(Q,s) for v in G.adj[u] if v.color == white v.color=grey, v.d=u.d+1, v.π=u Enqueue(Q,v) u.color=black

Breadth first search Let δ(s,v) be the shortest path from s to v After running BFS you can find this path as: v.π to (v.π).π to... s (pseudo code on p. 601, recursion)

BFS correctness Proof: contradiction Assume δ(s,v) v.d v.d > δ(s,v) (Lemma 22.2, induction) Thus v.d > δ(s,v) Let u be previous node on δ(s,v) Thus δ(s,v) = δ(s,u)+1 and δ(s,u) = u.d Then v.d > δ(s,v) = δ(s,u)+1 = u.d+1

BFS correctness v.d > δ(s,v) = δ(s,u)+1 = u.d+1 Cases on color of v when u dequeue, all cases invalidate top equation Case white: alg sets v.d = u.d + 1 Case black: already removed thus v.d < u.d (corollary 22.4) Case grey: exists w that dequeued v, v.d = w.d+1 < u.d+1 (corollary 22.4)

Depth first search DFS can be implemented with BFS We will mark both a start (colored grey) and finish (colored black) times This helps us quantify properties of graphs

Depth first search DFS(G) for all v in V v.color=white, v.π=nil time=0 for each v in V if v.color==white DFS-Visit(G,v)

Depth first search DFS-Visit(G,u) time=time+1 u.d=time, u.color=grey for each v in G.adj[u] if v.color == white v.π=u DFS-Visit(G,v) u.color=black, time=time+1, u.f=time

Depth first search Edge markers: Consider edge u to v B = Edge to grey node (u.f < v.f) F = Edge to black node (u.f > v.f) C = Edge to black node (u.d > v.f)

Depth first search DFS can do topographical sort Run DFS, sort in decreasing finish time

Depth first search DFS can find strongly connected components

Depth first search Let G T be G with edges reversed Then to get strongly connected: 1. DFS(G) to get finish times 2. Compute G T 3. DFS(G T ) on vertex in decreasing finish time 4. Each tree in forest SC component