CSC263 Week 8. Larry Zhang.

Similar documents
Announcements Problem Set 4 is out!

Announcements. HW3 is graded. Average is 81%

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

Outlines: Graphs Part-2

COT 6405 Introduction to Theory of Algorithms

Graphs & Digraphs Tuesday, November 06, 2007

Elementary Graph Algorithms

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

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

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

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory

Graph: representation and traversal

22.1 Representations of graphs

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

Graph Algorithms. Definition

Design and Analysis of Algorithms

Graph Representation

Graph Search. Adnan Aziz

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

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

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

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

Algorithm Design and Analysis

CS 310 Advanced Data Structures and Algorithms

Graph representation

CS61BL. Lecture 5: Graphs Sorting

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

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

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Outline. 1 Introduction. 2 Algorithm. 3 Example. 4 Analysis. 5 References. Idea

Representations of Graphs

Algorithm Design and Analysis

Basic Graph Algorithms

CSE 100: GRAPH ALGORITHMS

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

Chapter 22. Elementary Graph Algorithms

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

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

Algorithm Design and Analysis

Figure 1: A directed graph.

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

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

CSI 604 Elementary Graph Algorithms

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

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

CS 206 Introduction to Computer Science II

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

Review: Graph Theory and Representation

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

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

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

CS Algorithms and Complexity

W4231: Analysis of Algorithms

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Graph Representations and Traversal

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

CS/COE

Lesson 1 Introduction to Path Planning Graph Searches: BFS and DFS

COP 4531 Complexity & Analysis of Data Structures & Algorithms

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

GRAPHS Lecture 19 CS2110 Spring 2013

Announcements Problem Set 5 is out (today)!

CSE 373: Data Structures and Algorithms

CSE 100: GRAPH SEARCH

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Lecture 9 Graph Traversal

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

Graph implementations :

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

Lecture 26: Graphs: Traversal (Part 1)

Inf 2B: Graphs, BFS, DFS

csci 210: Data Structures Graph Traversals


Unit 2: Algorithmic Graph Theory

Module 11: Additional Topics Graph Theory and Applications

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

Graph. Vertex. edge. Directed Graph. Undirected Graph

Minimum Spanning Trees Ch 23 Traversing graphs

Graph Algorithms. Andreas Klappenecker

TIE Graph algorithms

Graphs. Terminology. Graphs & Breadth First Search (BFS) Extremely useful tool in modeling problems. Consist of: Vertices Edges

Week 12: Minimum Spanning trees and Shortest Paths

Unweighted directed graphs

Homework No. 4 Answers

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Outline. Graphs. Divide and Conquer.

TIE Graph algorithms

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

Homework Assignment #3 Graph

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

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

Outline. Computer Science 331. Analysis of Prim's Algorithm

Graph Algorithms: Chapters Part 1: Introductory graph concepts

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

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

Old final Exam Question Answer true or false and justify your answer: Since it takes at least n-1 key comparisons to find the min of n data items and

22.3 Depth First Search

Transcription:

CSC263 Week 8 Larry Zhang http://goo.gl/forms/s9yie3597b

Announcements (strike related) Lectures go as normal Tutorial this week everyone go to BA32 (T8, F2, F2, F3) Problem sets / Assignments are submitted as normal. marking may be slower Midterm: still being marked Keep a close eye on announcements

This week s outline Graph BFS

Graph A really, really important ADT that is used to model relationships between objects. Reference: http://steve-yegge.blogspot.ca/28/3/get-that-job-at-google.html

Things that can be modelled using graphs Web Facebook Task scheduling Maps & GPS Compiler (garbage collection) OCR (computer vision) Database Rubik s cube. (many many other things)

Definition G = (V, E) Set of vertices e.g., {a, b, c} Set of edges e.g., { (a, b), (c, a) }

Flavours of graphs

each edge is an unordered pair (u, v) = (v, u) Undirected each edge is an ordered pair (u, v) (v, u) Directed

2-3 Unweighted Weighted

Simple Non-simple No multiple edge, no self-loop

Acyclic Cyclic

Connected Disconnected

Dense Sparse

Path A path of length 3 Length of path = number of edges Read Appendix B.4 for more background on graphs.

Operations on a graph Add a vertex; remove a vertex Add an edge; remove an edge Get neighbours (undirected graph) Neighbourhood(u): all v V such that (u, v) E Get in-neighbours / out-neighbours (directed graph) Traversal: visit every vertex in the graph

Data structures for the graph ADT Adjacency matrix Adjacency list

Adjacency matrix A V x V matrix A

Adjacency matrix 2 4 2 3 2 3 4 3 4

Adjacency matrix How much space does it take? V ² 2 3 4 2 3 4

Adjacency matrix (undirected graph) 2 4 2 3 2 3 4 3 4 The adjacency matrix of an symmetric undirected graph is.

Adjacency matrix (undirected graph) How much space does it take? 2 V ² 3 4 2 3 4

Adjacency list

Adjacency list (directed graph) Each vertex vi stores a list A[i] of vj that satisfies (vi, vj) E 2 4 3 2 4 2 4 3 4 3

Adjacency list (directed graph) How much space does it take? V + E V E 2 4 2 4 3 4 3

Adjacency list (undirected graph) 2 4 3 2 4 2 3 3 2 4 3

Adjacency list (undirected graph) How much space does it take? V +2 E V 2 E 2 4 2 3 3 2 4 3

Matrix VS List In term of space complexity adjacency matrix is Θ( V ²) adjacency list is Θ( V + E ) Which one is more space-efficient? Adjacency list, if E V ², i.e., the graph is not very dense.

Matrix VS List Anything that Matrix does better than List? Check whether edge (vi, vj) is in E Matrix: just check if A[i, j] =, O() List: go through list A[i] see if j is in there, O(length of list)

Takeaway Adjacency matrix or adjacency list? Choose the more appropriate one depending on the problem.

CSC263 Week 8 Wednesday / Thursday

Announcements PS6 posted, due next Tuesday as usual Drop date: March 8th

Recap ADT: Graph Data structures Adjacency matrix Adjacency list Graph operations Add vertex, remove vertex,, edge query, Traversal

Graph Traversals BFS and DFS They are twins!

Graph traversals Visiting every vertex once, starting from a given vertex. The visits can follow different orders, we will learn about the following two ways Breadth First Search (BFS) Depth First Search (DFS)

Intuitions of BFS and DFS Consider a special graph -- a tree

The knowledge learning tree Science High School CS College PhD AI DB Physics String Theory Black hole Traversing this graph means learning all these subjects. Geology Rocks Sand

The Breadth-First ways of learning these subjects Level by level, finish high school, then all subjects at College level, then finish all subjects in PhD level. Science High School CS College PhD AI DB Physics String Theory Black hole Geology Rocks Sand

The Depth-First way of learning these subjects Go towards PhD whenever possible; only start learning physics after finishing everything in CS. Science High School CS College PhD AI DB Physics String Theory Black hole Geology Rocks Sand

Now let s seriously start studying BFS

Special case: BFS in a tree Review CSC48: BFS in a tree (starting from root) is a level-by-level traversal. (NOT preorder!) What ADT did we use for implementing the level-by-level traversal? Queue!

Special case: BFS in a tree Output: a b c d e f NOT_YET_BFS(root): Q Queue() Enqueue(Q, root) while Q not empty: x Dequeue(Q) print x for each child c of x: Enqueue(Q, c) a b d Queue: c e a b DQ DQ f c d e f DQ DQ DQ DQ EMPTY!

The real deal: BFS in a Graph r s t u v w x y If we just run NOT_YET_BFS(t) on the above graph. What problem would we have? It would want to visit some vertex twice (e.g., x), which shall be avoided! NOT_YET_BFS(root): Q Queue() Enqueue(Q, root) while Q not empty: x Dequeue(Q) print x for each neighbr c of x: Enqueue(Q, c)

How avoid visiting a vertex twice Remember you visited it by labelling it using colours. White: unvisited Gray: encountered Black: explored Initially all vertices are white Colour a vertex gray the first time visiting it Colour a vertex black when all its neighbours have been encountered Avoid visiting gray or black vertices In the end, all vertices are black (sort-of)

Some other values we want to remember during the traversal... pi[v]: the vertex from which v is encountered I was introduced as whose neighbour? d[v]: the distance value the distance from v to the source vertex of the BFS r s t u v w x y This d[v] is going to be really useful!

BFS(G=(V, E), s): Pseudocode: the real BFS for all v in V: 2 colour[v] white 3 d[v] 4 pi[v] NIL # Initialize vertices 5 Q Queue() 6 colour[s] gray # start BFS by encountering the source vertex 7 d[s] 8 Enqueue(Q, s) 9 while Q not empty: u Dequeue(Q) for each neighbour v of u: 2 if colour[v] = white 3 colour[v] gray 4 5 6 7 # distance from s to s is The blue lines are the same as NOT_YET_BFS # only visit unvisited vertices d[v] d[u] + # v is -level farther from s than u pi[v] u # v is introduced as u s neighbour Enqueue(Q, v) # all neighbours of u have been colour[u] black encountered, therefore u is explored

Let s run an example! r s t u v w x y BFS(G, s)

After initialization r s t u v w x y All vertices are white and have d =

Start by encountering the source d= r s t u v w x y Queue: s Colour the source gray and set its d =, and Enqueue it

Dequeue, explore neighbours r s t u v w x y DQ Queue: s r w The red edge indicates the pi[v] that got remembered

Colour black after exploring all neighbours r s t u v w x y DQ Queue: s r w

Dequeue, explore neighbours (2) r s t u v w x y 2 DQ DQ Queue: s r w v

Dequeue, explore neighbours (3) 2 r s t u v w x y 2 2 DQ DQ DQ Queue: s r w v t x

after a few more steps...

BFS done! 2 3 r s t u v w x y 2 2 3 DQ DQ DQ DQ DQ DQ DQ DQ Queue: s r w v t x u y

What do we get after doing all these?

2 3 r s t u v w x y 2 2 3 First of all, we get to visit every vertex once.

Did you know? The official name of the red edges are called tree edges. r s t u v w x y This is called the BFS-tree, it s a tree that connects all vertices, if the graph is connected.

Short path from u to s: u pi[u] pi[pi[u]] pi[pi[pi[u]]] s These d[v] values, we said they were going to be really useful. 2 3 r s t u v w x y 2 2 3 The value indicates the vertex s distance from the source vertex. Actually more than that, it s the shortest-path distance, we can prove it. How about finding short path itself? Follow the red edges, pi[v] comes in handy for this.

What if G is disconnected? 2 3 r s t u z After BFS(s), z is of white colour and d [v] = v w x y 2 2 3 The infinite distance value of z indicates that it is unreachable from the source vertex.

rr s t u v w w x y Runtime analysis! The total amount of work (use adjacency list): Visit each vertex once Enqueue, Dequeue, change colours, assign d[v],, constant work per vertex in total: O( V ) At each vertex, check all its neighbours (all its incident edges) Each edge is checked twice (by the two end vertices) Total runtime: in total: O( E ) O( V + E )

Summary of BFS Prefer to explore breadth rather than depth Useful for getting single-source shortest paths on unweighted graphs Useful for testing reachability Runtime O( V + E ) with adjacency list (with adjacency matrix it ll be different)

Next week DFS BFS http://goo.gl/forms/s9yie3597b