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

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

CS 241 Analysis of Algorithms

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)}

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

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

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:

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

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

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

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

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

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

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

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

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

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

COMP108 Algorithmic Foundations

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

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

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

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

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

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

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

Planar Upward Drawings

Problem solving by search

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

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

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

12. Traffic engineering

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

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

Constructive Geometric Constraint Solving

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

CS 103 BFS Alorithm. Mark Redekopp

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

CSI35 Chapter 11 Review

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

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

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

Round 7: Graphs (part I)

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

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

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

ECE COMBINATIONAL BUILDING BLOCKS - INVEST 13 DECODERS AND ENCODERS

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

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

GREEDY TECHNIQUE. Greedy method vs. Dynamic programming method:

Similarity Search. The Binary Branch Distance. Nikolaus Augsten.

Present state Next state Q + M N

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

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

Seven-Segment Display Driver

QUESTIONS BEGIN HERE!

CS September 2018

Outline. Binary Tree

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?

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

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

1 Introduction to Modulo 7 Arithmetic

Minimum Spanning Trees

EE1000 Project 4 Digital Volt Meter

QUESTIONS BEGIN HERE!

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

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

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

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

Trees as operads. Lecture A formalism of trees

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

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

Integration Continued. Integration by Parts Solving Definite Integrals: Area Under a Curve Improper Integrals

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

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

14 Shortest Paths (November 8)

Graph Contraction and Connectivity

Weighted Graphs. Weighted graphs may be either directed or undirected.

Lecture 20: Minimum Spanning Trees (CLRS 23)

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

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

IEEE TRANSACTIONS ON COMPUTATIONAL BIOLOGY AND BIOINFORMATICS, VOL. TK, NO. TK, MONTHTK YEARTK 1. Hamiltonian Walks of Phylogenetic Treespaces

Garnir Polynomial and their Properties

The Plan. Honey, I Shrunk the Data. Why Compress. Data Compression Concepts. Braille Example. Braille. x y xˆ

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

Last time: introduced our first computational model the DFA.

COMPLEXITY OF COUNTING PLANAR TILINGS BY TWO BARS

CIT 596 Theory of Computation 1. Graphs and Digraphs

Chapter 9. Graphs. 9.1 Graphs

Instructions for Section 1

Using the Printable Sticker Function. Using the Edit Screen. Computer. Tablet. ScanNCutCanvas

Numbering Boundary Nodes

(Minimum) Spanning Trees

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

Scientific Programming. Graphs

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

Grade 7/8 Math Circles March 4/5, Graph Theory I- Solutions

Organization. Dominators. Control-flow graphs 8/30/2010. Dominators, control-dependence. Dominator relation of CFGs

Analysis of Algorithms - Elementary graphs algorithms -

Transcription:

CSE 373 Grphs 1: Conpts, Dpth/Brth-First Srh ring: Wiss Ch. 9 slis rt y Mrty Stpp http://www.s.wshington.u/373/ Univrsity o Wshington, ll rights rsrv. 1

Wht is grph? 56 Tokyo Sttl Soul 128 16 30 181 140 Nw York Syny L.A. 2

Grphs grph: A t strutur ontining: st o vrtisv, (somtims ll nos) st o gse, whr n g rprsnts onntion twn 2 vrtis. Grph G= (V, E) n g is pir (v, w) whr v, wr in V th grph t right: V= {,,, } E= {(, ), (, ), (, ), (, )} gr: numr o gs touhing givn vrtx. t right: =1, =2, =3, =2 3

Grph xmpls For h, wht r th vrtis n wht r th gs? W pgs with links Mthos in progrm tht ll h othr Ro mps (.g., Googl mps) Airlin routs Fook rins Cours pr-rquisits Fmily trs Pths through mz 4

Pths pth: A pth rom vrtx to is squn o gs tht n ollow strting rom to rh. n rprsnt s vrtis visit, or gs tkn xmpl, on pth rom Vto Z: {, h} or {V, X, Z} Wht r two pths rom U to Y? pth lngth: Numr o vrtis or gs ontin in th pth. nighor or jnt: Two vrtis onnt irtly y n g. xmpl: V n X U V W X Y g h Z 5

Rhility, onntnss rhl: Vrtx is rhl rom i pth xists rom to. V onnt: A grph is onnti vry vrtx is rhl rom ny othr. Is th grph t top right onnt? U W X g Y h Z strongly onnt: Whn vry vrtx hs n g to vry othr vrtx. 6

Loops n yls yl: A pth tht gins n ns t th sm no. xmpl: {, g,,, } or {V, X, Y, W, U, V}. xmpl: {,, } or {U, W, V, U}. yli grph: On tht os not ontin ny yls. V loop: An g irtly rom no to itsl. Mny grphs on't llow loops. U W X g h Z Y 7

Wight grphs wight: Cost ssoit with givn g. Som grphs hv wight gs, n som r unwight. Egs in n unwight grph n thought o s hving qul wight (.g. ll 0, or ll 1, t.) Most grphs o not llow ngtiv wights. xmpl: grph o irlin lights, wight y mils twn itis: SFO 1843 ORD 849 142 PVD HNL 2555 337 LAX 1743 1233 802 DFW 1387 1120 LGA 1099 MIA 8

Dirt grphs irt grph ("igrph"): On whr gs r on-wy onntions twn vrtis. I grph is irt, vrtx hs sprt in/out gr. A igrph n wight or unwight. Is th grph low onnt? Why or why not? g 9

Digrph xmpl Vrtis = UW CSE ourss (inomplt list) Eg (, ) = is prrquisit or 120 131 142 140 450 143 154 403 331 312 311 344 332 341 351 352 333 373 374 431 446 444 421 440 451 410 415 413 414 417 452 10

Link Lists, Trs, Grphs A inry tris grph with som rstritions: Th tr is n unwight, irt, yli grph (DAG). Eh no's in-gr is t most 1, n out-gr is t most 2. Thr is xtly on pth rom th root to vry no. A link listis lso grph: F Unwight DAG. In/out gr o t most 1 or ll nos. B K A B C D A E H G J 11

Srhing or pths Srhing or pth rom on vrtx to nothr: Somtims, w just wnt ny pth (or wnt to know thr is pth). Somtims, w wnt to minimiz pth lngth(# o gs). Somtims, w wnt to minimiz pth ost(sum o g wights). Wht is th shortst pth rom MIA to SFO? Whih pth hs th minimum ost? HNL $130 $250 SFO $60 LAX $70 $170 $120 $500 $80 DFW ORD $50 $140 $110 MIA PVD $200 LGA $100 12

Dpth-irst srh pth-irst srh(dfs): Fins pth twn two vrtis y xploring h possil pth s r s possil or ktrking. Otn implmnt rursivly. Mny grph lgorithms involv visiting or mrking vrtis. Dpth-irst pths rom to ll vrtis (ssuming ABC g orr): to :{, } to : {,,,, } to :{, } to :{,, } to : {,,, } to g:{,, g} to h: {,, g, h} g h 13

DFS psuoo untion s(v 1, v 2 ): s(v 1, v 2, { }). untion s(v 1, v 2, pth): pth+= v 1. mrk v 1 s visit. i v 1 is v 2 : pth is oun! g h or h unvisit nighor no v 1 : i s(n, v 2, pth) ins pth: pth is oun! pth-= v 1. // pth is not oun. Th pthprm ov is us i you wnt to hv th pth vill s list on you r on. Tr s(, ) in th ov grph. 14

DFS osrvtions isovry: DFS is gurnt to in pth i on xists. rtrivl: It is sy to rtriv xtly wht th pth is (th squn o gs tkn) i w in it g h optimlity: not optiml. DFS is gurnt to in pth, not nssrily th st/shortst pth Exmpl: s(, ) rturns {,,, } rthr thn {,, }. 15

Brth-irst srh rth-irst srh(bfs): Fins pth twn two nos y tking on stp own ll pths n thn immitly ktrking. Otn implmnt y mintining quu o vrtis to visit. BFS lwys rturns th shortst pth (th on with th wst gs) twn th strt n th n vrtis. to :{, } to : {,,, } to :{, } to :{, } to : {,, } to g:{,, g} to h: {,, h} g h 16

BFS psuoo untion s(v 1, v 2 ): quu:= {v 1 }. mrk v 1 s visit. whil quu is not mpty: v:= quu.rmovfirst(). i vis v 2 : pth is oun! g h or h unvisit nighor no v: mrk ns visit. quu.lst(n). // pth is not oun. Tr s(, ) in th ov grph. 17

BFS osrvtions optimlity: lwys ins th shortst pth (wst gs). in unwight grphs, ins optiml ost pth. In wight grphs, not lwys optiml ost. g h rtrivl: hrr to ronstrut th tul squn o vrtis or gs in th pth on you in it onptully, BFS is xploring mny possil pths in prlll, so it's not sy to stor pth rry/list in progrss solution: W n kp trk o th pth y storing prssors or h vrtx (h vrtx n stor rrn to prvious vrtx). DFS uss lss mmory thn BFS, sir to ronstrut th pth on oun; ut DFS os not lwys in shortst pth. BFS os. 18

DFS, BFS runtim Wht is th xpt runtim o DFS n BFS, in trms o th numr o vrtis V n th numr o gs E? Answr: O( V + E ) whr V = numr o vrtis, E = numr o gs Must potntilly visit vry no n/or xmin vry g on. why not O( V * E )? Wht is th sp omplxity o h lgorithm? (How muh mmory os h lgorithm rquir?) 19

BFS tht ins pth untion s(v 1, v 2 ): quu:= {v 1 }. mrk v 1 s visit. whil quu is not mpty: v:= quu.rmovfirst(). g h i vis v 2 : pth is oun! (ronstrut it y ollowing.prvk to v 1.) or h unvisit nighor no v: mrk ns visit. (st n.prv= v.) quu.lst(n). // pth is not oun. prv By storing som kin o "prvious" rrn ssoit with h vrtx, you n ronstrut your pth k on you in v 2. 20