CSE 373: More on graphs; DFS and BFS. Michael Lee Wednesday, Feb 14, 2018

Similar documents
CSE 373. Graphs 1: Concepts, Depth/Breadth-First Search reading: Weiss Ch. 9. slides created by Marty Stepp

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

CS 461, Lecture 17. Today s Outline. Example Run

CS200: Graphs. Graphs. Directed Graphs. Graphs/Networks Around Us. What can this represent? Sometimes we want to represent directionality:

CS 241 Analysis of Algorithms

Graphs. CSC 1300 Discrete Structures Villanova University. Villanova CSC Dr Papalaskari

Paths. Connectivity. Euler and Hamilton Paths. Planar graphs.

CSC Design and Analysis of Algorithms. Example: Change-Making Problem

Graphs. Graphs. Graphs: Basic Terminology. Directed Graphs. Dr Papalaskari 1

An undirected graph G = (V, E) V a set of vertices E a set of unordered edges (v,w) where v, w in V

Exam 1 Solution. CS 542 Advanced Data Structures and Algorithms 2/14/2013

CSE 373: AVL trees. Warmup: Warmup. Interlude: Exploring the balance invariant. AVL Trees: Invariants. AVL tree invariants review

CS61B Lecture #33. Administrivia: Autograder will run this evening. Today s Readings: Graph Structures: DSIJ, Chapter 12

Outline. 1 Introduction. 2 Min-Cost Spanning Trees. 4 Example

Graph Isomorphism. Graphs - II. Cayley s Formula. Planar Graphs. Outline. Is K 5 planar? The number of labeled trees on n nodes is n n-2

12/3/12. Outline. Part 10. Graphs. Circuits. Euler paths/circuits. Euler s bridge problem (Bridges of Konigsberg Problem)

5/9/13. Part 10. Graphs. Outline. Circuits. Introduction Terminology Implementing Graphs

N=4 L=4. Our first non-linear data structure! A graph G consists of two sets G = {V, E} A set of V vertices, or nodes f

Outline. Computer Science 331. Computation of Min-Cost Spanning Trees. Costs of Spanning Trees in Weighted Graphs

Announcements. Not graphs. These are Graphs. Applications of Graphs. Graph Definitions. Graphs & Graph Algorithms. A6 released today: Risk

Outline. Circuits. Euler paths/circuits 4/25/12. Part 10. Graphs. Euler s bridge problem (Bridges of Konigsberg Problem)

b. How many ternary words of length 23 with eight 0 s, nine 1 s and six 2 s?

Math 61 : Discrete Structures Final Exam Instructor: Ciprian Manolescu. You have 180 minutes.

Cycles and Simple Cycles. Paths and Simple Paths. Trees. Problem: There is No Completely Standard Terminology!

COMP108 Algorithmic Foundations

Module graph.py. 1 Introduction. 2 Graph basics. 3 Module graph.py. 3.1 Objects. CS 231 Naomi Nishimura

, each of which is a tree, and whose roots r 1. , respectively, are children of r. Data Structures & File Management

learning objectives learn what graphs are in mathematical terms learn how to represent graphs in computers learn about typical graph algorithms

5/7/13. Part 10. Graphs. Theorem Theorem Graphs Describing Precedence. Outline. Theorem 10-1: The Handshaking Theorem

Section 10.4 Connectivity (up to paths and isomorphism, not including)

Weighted graphs -- reminder. Data Structures LECTURE 15. Shortest paths algorithms. Example: weighted graph. Two basic properties of shortest paths

ECE 407 Computer Aided Design for Electronic Systems. Circuit Modeling and Basic Graph Concepts/Algorithms. Instructor: Maria K. Michael.

CSI35 Chapter 11 Review

Why the Junction Tree Algorithm? The Junction Tree Algorithm. Clique Potential Representation. Overview. Chris Williams 1.

Announcements. These are Graphs. This is not a Graph. Graph Definitions. Applications of Graphs. Graphs & Graph Algorithms

Planar Upward Drawings

The University of Sydney MATH2969/2069. Graph Theory Tutorial 5 (Week 12) Solutions 2008

Algorithmic and NP-Completeness Aspects of a Total Lict Domination Number of a Graph

10/30/12. Today. CS/ENGRD 2110 Object- Oriented Programming and Data Structures Fall 2012 Doug James. DFS algorithm. Reachability Algorithms

0.1. Exercise 1: the distances between four points in a graph

ECE COMBINATIONAL BUILDING BLOCKS - INVEST 13 DECODERS AND ENCODERS

Constructive Geometric Constraint Solving

Garnir Polynomial and their Properties

QUESTIONS BEGIN HERE!

1 Introduction to Modulo 7 Arithmetic

More Foundations. Undirected Graphs. Degree. A Theorem. Graphs, Products, & Relations

Improving Union. Implementation. Union-by-size Code. Union-by-Size Find Analysis. Path Compression! Improving Find find(e)

Instructions for Section 1

CMPS 2200 Fall Graphs. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

CS 103 BFS Alorithm. Mark Redekopp

CSE303 - Introduction to the Theory of Computing Sample Solutions for Exercises on Finite Automata

QUESTIONS BEGIN HERE!

Minimum Spanning Trees

Walk Like a Mathematician Learning Task:

12. Traffic engineering

(2) If we multiplied a row of B by λ, then the value is also multiplied by λ(here lambda could be 0). namely

Solutions for HW11. Exercise 34. (a) Use the recurrence relation t(g) = t(g e) + t(g/e) to count the number of spanning trees of v 1

a b c cat CAT A B C Aa Bb Cc cat cat Lesson 1 (Part 1) Verbal lesson: Capital Letters Make The Same Sound Lesson 1 (Part 1) continued...

CS September 2018

Numbering Boundary Nodes

CMSC 451: Lecture 2 Graph Basics Thursday, Aug 31, 2017

COMPLEXITY OF COUNTING PLANAR TILINGS BY TWO BARS

Chapter 9. Graphs. 9.1 Graphs

Similarity Search. The Binary Branch Distance. Nikolaus Augsten.

NP-Completeness. CS3230 (Algorithm) Traveling Salesperson Problem. What s the Big Deal? Given a Problem. What s the Big Deal? What s the Big Deal?

1. Determine whether or not the following binary relations are equivalence relations. Be sure to justify your answers.

Problem solving by search

Chapter 18. Minimum Spanning Trees Minimum Spanning Trees. a d. a d. a d. f c

Page 1. Question 19.1b Electric Charge II Question 19.2a Conductors I. ConcepTest Clicker Questions Chapter 19. Physics, 4 th Edition James S.

EE1000 Project 4 Digital Volt Meter

MAT3707. Tutorial letter 201/1/2017 DISCRETE MATHEMATICS: COMBINATORICS. Semester 1. Department of Mathematical Sciences MAT3707/201/1/2017

Greedy Algorithms, Activity Selection, Minimum Spanning Trees Scribes: Logan Short (2015), Virginia Date: May 18, 2016

Round 7: Graphs (part I)

S i m p l i f y i n g A l g e b r a SIMPLIFYING ALGEBRA.

Aquauno Video 6 Plus Page 1

# 1 ' 10 ' 100. Decimal point = 4 hundred. = 6 tens (or sixty) = 5 ones (or five) = 2 tenths. = 7 hundredths.

Scientific Programming. Graphs

XML and Databases. Outline. Recall: Top-Down Evaluation of Simple Paths. Recall: Top-Down Evaluation of Simple Paths. Sebastian Maneth NICTA and UNSW

Seven-Segment Display Driver

INTEGRALS. Chapter 7. d dx. 7.1 Overview Let d dx F (x) = f (x). Then, we write f ( x)

Graph Contraction and Connectivity

CS553 Lecture Register Allocation I 3

WORKSHOP 6 BRIDGE TRUSS

Section 3: Antiderivatives of Formulas

(a) v 1. v a. v i. v s. (b)

Solutions to Homework 5

Analysis of Algorithms - Elementary graphs algorithms -

Graph Theory. Vertices. Vertices are also known as nodes, points and (in social networks) as actors, agents or players.

Plan. I Gale-Shapley Running Time. I Graphs. I Motivation and definitions I Graph traversal: BFS and DFS

A Simple Code Generator. Code generation Algorithm. Register and Address Descriptors. Example 3/31/2008. Code Generation

Present state Next state Q + M N

Formal Concept Analysis

Lecture 20: Minimum Spanning Trees (CLRS 23)

Trees as operads. Lecture A formalism of trees

Analysis for Balloon Modeling Structure based on Graph Theory

Register Allocation. How to assign variables to finitely many registers? What to do when it can t be done? How to do so efficiently?

Analysis of Algorithms - Elementary graphs algorithms -

Preview. Graph. Graph. Graph. Graph Representation. Graph Representation 12/3/2018. Graph Graph Representation Graph Search Algorithms

Register Allocation. Register Allocation. Principle Phases. Principle Phases. Example: Build. Spills 11/14/2012

Transcription:

CSE 373: Mor on grphs; DFS n BFS Mihl L Wnsy, F 14, 2018 1

Wrmup Wrmup: Disuss with your nighor: Rmin your nighor: wht is simpl grph? Suppos w hv simpl, irt grph with x nos. Wht is th mximum numr of gs it n hv, in trms of x? Now, suppos w hv iffrnt simpl, unirt grph with y gs. Wht is th mximum numr of vrtis it n hv, in trms of y? 2

Wrmup Wrmup: Disuss with your nighor: Rmin your nighor: wht is simpl grph? A simpl grph is grph tht hs no slf-loops n no prlll gs. Suppos w hv simpl, irt grph with x nos. Wht is th mximum numr of gs it n hv, in trms of x? Eh vrtx n onnt to x 1 othr vrtis, so x(x 1). Now, suppos w hv iffrnt simpl, unirt grph with y gs. Wht is th mximum numr of vrtis it n hv, in trms of y? Infinit: just kp ing nos with no gs tth. 2

Wrmup: Som follow-up qustions: Suppos w hv simpl, unirt grph with x nos. Wht is th mximum numr of gs it n hv? Wht if th grph is not simpl? Now, suppos w hv iffrnt simpl, unirt grph with y gs. Wht is th mximum numr of vrtis it n hv? Wht if th grph is not simpl? 3

Wrmup: Som follow-up qustions: Suppos w hv simpl, unirt grph with x nos. Wht is th mximum numr of gs it n hv? Wht if th grph is not simpl? If th grph is simpl, th mx numr of gs is xtly hlf x(x 1) of wht it woul if th grph wr irt. So,. 2 If th grph is not simpl, it s infinit: ssuming x > 0, w n just kp ing mor n mor slf-loops. Not tht if x = 0, thr n t ny gs t ll. Now, suppos w hv iffrnt simpl, unirt grph with y gs. Wht is th mximum numr of vrtis it n hv? Wht if th grph is not simpl? Eithr wy, it s still infinit, for th sm rsons givn prviously. 3

Summry Wht i w lrn? In grphs with no rstritions, numr of gs n numr of vrtis r inpnnt. 4

Summry Wht i w lrn? In grphs with no rstritions, numr of gs n numr of vrtis r inpnnt. In simpl grphs, if w know V is som fix vlu, w lso know E O ( V 2), for oth irt n unirt grphs. 4

Summry Wht i w lrn? In grphs with no rstritions, numr of gs n numr of vrtis r inpnnt. In simpl grphs, if w know V is som fix vlu, w lso know E O ( V 2), for oth irt n unirt grphs. Dns grph If E Θ ( V 2), w sy th grph is ns. To put it nothr wy, ns grphs hv lots of gs 4

Summry Wht i w lrn? In grphs with no rstritions, numr of gs n numr of vrtis r inpnnt. In simpl grphs, if w know V is som fix vlu, w lso know E O ( V 2), for oth irt n unirt grphs. Dns grph If E Θ ( V 2), w sy th grph is ns. To put it nothr wy, ns grphs hv lots of gs Sprs grph If E O ( V ), w su th grph is sprs. To put it nothr wy, sprs grphs hv fw gs. 4

How o w rprsnt grphs in o? So, how o w tully rprsnt grphs in o? 5

How o w rprsnt grphs in o? So, how o w tully rprsnt grphs in o? Two ommon pprohs, with iffrnt troffs: Ajny mtrix Ajny list 5

Ajny mtrix Cor i: Assign h no numr from 0 to V 1 Crt V V nst rry of oolns or ints If (x, y) E, thn nstarry[x][y] == tru 6

Ajny mtrix Cor i: Assign h no numr from 0 to V 1 Crt V V nst rry of oolns or ints If (x, y) E, thn nstarry[x][y] == tru 6

Ajny list Wht is th worst-s runtim to: Gt out-gs: Gt in-gs: Di if n g xists: Insrt n g: Dlt n g: How muh sp o w us? Is this ttr for sprs or ns grphs? Cn w hnl slf-loops n prlll gs? 7

Ajny list Wht is th worst-s runtim to: Gt out-gs: O ( V ) Gt in-gs: O ( V ) Di if n g xists: O (1) Insrt n g: O (1) Dlt n g: O (1) How muh sp o w us? O ( V 2) Is this ttr for sprs or ns grphs? Dns ons Cn w hnl slf-loops n prlll gs? Slf-loops ys, prlll gs, not sily 7

Ajny list Cor i: Assign h no numr from 0 to V 1 Crt n rry of siz V Eh lmnt in th rry stors its out gs in list or st 8

Ajny list Cor i: Assign h no numr from 0 to V 1 Crt n rry of siz V Eh lmnt in th rry stors its out gs in list or st On highr lvl: rprsnt s IDitionry<Vrtx, Egs>. 8

Ajny list Cor i: Assign h no numr from 0 to V 1 Crt n rry of siz V Eh lmnt in th rry stors its out gs in list or st On highr lvl: rprsnt s IDitionry<Vrtx, Egs>. 8

Ajny list W n stor gs using ithr sts or lists. Answr ths qustions for oth. Wht is th worst-s runtim to: Gt out-gs: Gt in-gs: Di if n g xists: Insrt n g: Dlt n g: How muh sp o w us? Is this ttr for sprs or ns grphs? Cn w hnl slf-loops n prlll gs? 9

Whih o w pik? So whih o w pik? 10

Whih o w pik? So whih o w pik? Osrvtions: Most grphs r sprs If w implmnt jny lists using sts, w n gt omprl worst-s prformn 10

Whih o w pik? So whih o w pik? Osrvtions: Most grphs r sprs If w implmnt jny lists using sts, w n gt omprl worst-s prformn So y fult, pik jny lists. 10

Wlks n pths Wlk A wlk is list of vrtis v 0, v 1, v 2,..., v n whr if i is som int whr 0 i < v n, vry pir (v i, v i+1 ) E is tru. Mor intuitivly, wlk is on ontinous lin following th gs. 11

Wlks n pths Wlk A wlk is list of vrtis v 0, v 1, v 2,..., v n whr if i is som int whr 0 i < v n, vry pir (v i, v i+1 ) E is tru. Mor intuitivly, wlk is on ontinous lin following th gs. Pth A pth is wlk tht nvr visits th sm vrtx twi. 11

Wlks n pths Wlk A wlk is list of vrtis v 0, v 1, v 2,..., v n whr if i is som int whr 0 i < v n, vry pir (v i, v i+1 ) E is tru. Mor intuitivly, wlk is on ontinous lin following th gs. Pth A pth is wlk tht nvr visits th sm vrtx twi. Pth or wlk? Pth or wlk? 11

Wlks n pths Wlk A wlk is list of vrtis v 0, v 1, v 2,..., v n whr if i is som int whr 0 i < v n, vry pir (v i, v i+1 ) E is tru. Mor intuitivly, wlk is on ontinous lin following th gs. Pth A pth is wlk tht nvr visits th sm vrtx twi. Wlk Pth 11

Connt omponnts Connt grph A grph is onnt if vry vrtx is onnt to vry othr vrtx vi som pth. E.g.: if w pik up th grph n shk it, nothing flis off. 12

Connt omponnts Connt grph A grph is onnt if vry vrtx is onnt to vry othr vrtx vi som pth. E.g.: if w pik up th grph n shk it, nothing flis off. Connt or not onnt? Connt or not onnt? 12

Connt omponnts Connt grph A grph is onnt if vry vrtx is onnt to vry othr vrtx vi som pth. E.g.: if w pik up th grph n shk it, nothing flis off. Connt Not onnt Connt omponnt A onnt omponnt of grph is ny sugrph (prt of grph) whr ll vrtis r onnt to h othr. Not: A onnt grph hs only on onnt omponnt. 12

Trs vs grphs Is this grph or tr? f g 13

Trs vs grphs Is this grph or tr? f g Both! 13

Trs vs grphs Is this grph or tr? f g Both! Tr A tr is onnt n yli grph. 13

Trs vs grphs Is this grph or tr? Is this th sm thing? f Both! g g f Tr A tr is onnt n yli grph. 13

Trs vs grphs Is this grph or tr? Is this th sm thing? f g g f Both! Ys! (If is th root...) Tr A tr is onnt n yli grph. 13

Trs vs grphs Is this grph or tr? Is this th sm thing? f g g f Both! Ys! (If is th root...) Tr A tr is onnt n yli grph. Root tr A root tr is tr whr w ll on spil no th root. 13

Dtting if grph is onnt Qustion: How n w tll if grph is onnt or not? 14

Dtting if grph is onnt Qustion: How n w tll if grph is onnt or not? I: Lt s just fin out! Pik no n s if thr s pth to vry othr no! 14

Brth-first srh (BFS) Brth-first trvrsl, or i: 1. Pik som no n mrk it (or sv it in st, t...) 15

Brth-first srh (BFS) Brth-first trvrsl, or i: 1. Pik som no n mrk it (or sv it in st, t...) 2. Exmin h nighor n visit h on (not: sv th ons w hvn t visit yt in som t strutur, lik quu?) 15

Brth-first srh (BFS) Brth-first trvrsl, or i: 1. Pik som no n mrk it (or sv it in st, t...) 2. Exmin h nighor n visit h on (not: sv th ons w hvn t visit yt in som t strutur, lik quu?) 3. Dquu som no from th t strutur. Go to stp 1. 15

Brth-first srh (BFS) Brth-first trvrsl, or i: 1. Pik som no n mrk it (or sv it in st, t...) 2. Exmin h nighor n visit h on (not: sv th ons w hvn t visit yt in som t strutur, lik quu?) 3. Dquu som no from th t strutur. Go to stp 1. 4. Kp going until th t strutur is mpty. 15

Brth-first srh (BFS) Brth-first trvrsl, or i: 1. Pik som no n mrk it (or sv it in st, t...) 2. Exmin h nighor n visit h on (not: sv th ons w hvn t visit yt in som t strutur, lik quu?) 3. Dquu som no from th t strutur. Go to stp 1. 4. Kp going until th t strutur is mpty. Psuoo, vrsion 1: srh(v): visit = mpty st quu.nquu(v) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) 15

Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:, 16

Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu: 16

Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:,, 16

Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:, 16

Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:,,,, 16

Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:,,, 16

Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:,,, f, g, 16

Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:,, f, g, 16

Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:, f, g, Wht wnt wrong? 16

A rokn trvrsl Prolm: W r r-visiting nos w lry visit! 17

A rokn trvrsl Prolm: W r r-visiting nos w lry visit! A fix: Kp trk of nos w v lry visit in st! 17

Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu:, Visit:, 18

Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu: Visit:, 18

Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu:,, Visit:,,, 18

Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu:, Visit:,,, 18

Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu:,,, Visit:,,,,, 18

Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu:,, Visit:,,,,, 18

Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu:,, f, g, Visit:,,,,, f, g, 18

Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu:, f, g, Visit:,,,,, f, g, 18

Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu: f, g, Visit:,,,,, f, g, 18

Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu: f, g, h, Visit:,,,,, f, g, h, 18

Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: f Quu: g, h, Visit:,,,,, f, g, h, 18

Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: g Quu: h, Visit:,,,,, f, g, h, 18

Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: g Quu: h, i, Visit:,,,,, f, g, h, i, 18

Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: h Quu: i, Visit:,,,,, f, g, h, i, 18

Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: i Quu: Visit:,,,,, f, g, h, i, 18

An intrsting proprty... Not: W visit th nos in rings mintin grully growing frontir of nos. f i g j h 19

An intrsting proprty... Not: W visit th nos in rings mintin grully growing frontir of nos. f i g j h 19

An intrsting proprty... Not: W visit th nos in rings mintin grully growing frontir of nos. f i g j h 19

An intrsting proprty... Not: W visit th nos in rings mintin grully growing frontir of nos. f i g j h 19

An intrsting proprty... Not: W visit th nos in rings mintin grully growing frontir of nos. f i g j h 19

An intrsting proprty... Not: W visit th nos in rings mintin grully growing frontir of nos. f i g j h 19

BFS Psuoo srh(v): visit = mpty st quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) 20

BFS nlysis Qustions: Wht is th worst-s runtim? (Lt V th numr of vrtis, lt E th numr of gs) Wht is th worst-s mount of mmory us? 21

BFS nlysis Qustions: Wht is th worst-s runtim? (Lt V th numr of vrtis, lt E th numr of gs) W visit h vrtx on, n h g on, so O ( V + E ). Wht is th worst-s mount of mmory us? Whtvr th lrgst horizon siz is. In th worst s, th horizon will ontin V 1 nos, so O ( V ). Not: O ( V + E ) is lso ll grph linr. 21

Othr pplitions of BFS Dsri how you woul us or moify BFS to solv th following: Dtrmin if som grph is lso tr. Print out ll th lmnts in tr lvl y lvl. Fin th shortst pth from on no to nothr. 22