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

Similar documents
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 241 Analysis of Algorithms

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

COMP108 Algorithmic Foundations

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

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

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. These are Graphs. This is not a Graph. Graph Definitions. Applications of Graphs. Graphs & Graph Algorithms

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

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

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

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

Planar Upward Drawings

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

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

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

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

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

Scientific Programming. Graphs

Problem solving by search

12. Traffic engineering

CSI35 Chapter 11 Review

Round 7: Graphs (part I)

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

QUESTIONS BEGIN HERE!

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

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

Similarity Search. The Binary Branch Distance. Nikolaus Augsten.

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

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

ECE COMBINATIONAL BUILDING BLOCKS - INVEST 13 DECODERS AND ENCODERS

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

Garnir Polynomial and their Properties

Chapter 9. Graphs. 9.1 Graphs

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

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

Present state Next state Q + M N

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

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

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

GREEDY TECHNIQUE. Greedy method vs. Dynamic programming method:

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

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

QUESTIONS BEGIN HERE!

COMPLEXITY OF COUNTING PLANAR TILINGS BY TWO BARS

Constructive Geometric Constraint Solving

CS 103 BFS Alorithm. Mark Redekopp

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

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

Analysis of Algorithms - Elementary graphs algorithms -

Trees as operads. Lecture A formalism of trees

Analysis of Algorithms - Elementary graphs algorithms -

Numbering Boundary Nodes

Minimum Spanning Trees

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

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

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?

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

Nefertiti. Echoes of. Regal components evoke visions of the past MULTIPLE STITCHES. designed by Helena Tang-Lim

Spanning Trees. BFS, DFS spanning tree Minimum spanning tree. March 28, 2018 Cinda Heeren / Geoffrey Tien 1

COMP 250. Lecture 29. graph traversal. Nov. 15/16, 2017

Section 3: Antiderivatives of Formulas

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

FSA. CmSc 365 Theory of Computation. Finite State Automata and Regular Expressions (Chapter 2, Section 2.3) ALPHABET operations: U, concatenation, *

Lecture 20: Minimum Spanning Trees (CLRS 23)

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

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

CS553 Lecture Register Allocation I 3

1 Introduction to Modulo 7 Arithmetic

Graphs and Graph Searches

(Minimum) Spanning Trees

Graph Contraction and Connectivity

A 4-state solution to the Firing Squad Synchronization Problem based on hybrid rule 60 and 102 cellular automata

MULTIPLE-LEVEL LOGIC OPTIMIZATION II

d e c b a d c b a d e c b a a c a d c c e b

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

Analysis for Balloon Modeling Structure based on Graph Theory

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

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

Outline. Binary Tree

CSCI 104 Graph Representation and Traversals. Mark Redekopp David Kempe

DUET WITH DIAMONDS COLOR SHIFTING BRACELET By Leslie Rogalski

13. Binary tree, height 4, eight terminal vertices 14. Full binary tree, seven vertices v 7 v13. v 19

Walk Like a Mathematician Learning Task:

Transcription:

Aministrivi: CS61B Ltur #33 Autogrr will run this vning. Toy s Rings: Grph Struturs: DSIJ, Chptr 12 Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 1

Why Grphs? For xprssing non-hirrhilly rlt itms Exmpls: Ntworks: piplins, ros, ssignmnt prolms Rprsnting prosss: flow hrts, Mrkov mols Rprsnting prtil orrings: PERT hrts, mkfils Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 2

Som Trminology A grph onsists of A st of nos (k vrtis) A st of gs: pirs of nos. Nos with n g twn r jnt. Dpning on prolm, nos or gs my hv lls (or wights) Typilly ll no st V = {v 0,..., n g st E. If th gs hv n orr (first, son), thy r irt gs, n w hv irt grph (igrph), othrwis n unirt grph. Egs r inint to thir nos. Dirt gs xit on no n ntr th nxt. A yl is pth without rpt gs ling from no k to itslf (following rrows if irt). A grph is yli if it hs yl, ls yli. Arvition: Dirt Ayli Grph DAG. Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 3

Som Piturs Ayli: Dirt Unirt Cyli: 1 3 2 1 With Eg Lls: 1 3 2 0 Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 4

Trs r Grphs A grph is onnt if thr is (possily irt) pth twn vry pir of nos. Tht is, if on no of th pir is rhl from th othr. A DAG is (root) tr iff onnt, n vry no ut th root hs xtly on prnt. A onnt, yli, unirt grph is lso ll fr tr. Fr: w r fr to pik th root;.g., Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 5

Exmpls of Us Eg = Connting ro, with lngth. Dtroit 200 Chigo Eg = Must omplt for; No ll = tim to omplt. Et 1 hr Slp 8 hrs Eg = Bgt Mrtin Gorg Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 6

Eg = som rltionship Mor Exmpls ts lovs potstikrs John Mry Eg = nxt stt might (with proility) 0.9 0.4 0.6 0.4 0.1 ht th t in Eg = nxt stt in stt mhin, ll is triggring input. (Strt t s. Bing in stt 4 mns thr is sustring 001 somwhr in th input.) 1 0 s 0 0 1 2 3 4 0,1 1 Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 7

Rprsnttion Oftn usful to numr th nos, n us th numrs in gs. Eg list rprsnttion: h no ontins som kin of list (.g., link list or rry) of its sussors (n possily prssors). 1: (2,3) () 2: (3) (1) 3: () (1,2) Eg sts: Colltion of ll gs. For grph ov: {(1,2),(1,3),(2,3) Ajny mtrix: Rprsnt onntion with mtrix ntry: 1 2 3 1 2 3 0 1 1 0 0 1 0 0 0 Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 8

Trvrsing Grph Mny lgorithms on grphs pn on trvrsing ll or som nos. Cn t quit us rursion us of yls. Evn in yli grphs, n gt omintoril xplosions: 1 4 7 0 3 6... 3N 2 5 8 Trt 0 s th root n o rursiv trvrsl own th two gs out of h no: Θ(2 N ) oprtions! So typilly try to visit h no onstnt # of tims (.g., on). Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 9

Rursiv Dpth-First Trvrsl of Grph Cn fix looping n omintoril prolms using th r-rum mtho us in rlir lturs for mz. Tht is, mrk nos s w trvrs thm n on t trvrs prviously mrk nos. Mks sns to tlk out prorr n postorr, s for trs. voi prorrtrvrs(grph G, No v) { if (v is unmrk) { mrk(v); visit v; for (Eg(v, w) G) trvrs(g, w); voi postorrtrvrs(grph G, No v) { if (v is unmrk) { mrk(v); for (Eg(v, w) G) trvrs(g, w); visit v; Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 10

Rursiv Dpth-First Trvrsl of Grph (II) W r oftn intrst in trvrsing ll nos of grph, not just thos rhl from on no. So w n rpt th prour s long s thr r unmrk nos. voi prorrtrvrs(grph G) { for (v nos of G) { prorrtrvrs(g, v); voi postorrtrvrs(grph G) { for (v nos of G) { postorrtrvrs(g, v); Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 11

Topologil Sorting Prolm: Givn DAG, fin linr orr of nos onsistnt with th gs. Tht is, orr th nos v 0, v 1,... suh tht v k is nvr rhl from v k if k > k. Gmk os this. Also PERT hrts. A B C D E H F G A B D E C F G A C B D F E G C A F D B G E C F G A B D E H H H H Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 12

Sorting n Dpth First Srh Osrvtion: Suppos w rvrs th links on our grph. If w o rursiv DFS on th rvrs grph, strting from no H, for xmpl, w will fin ll nos tht must om for H. Whn th srh rhs no in th rvrs grph n thr r no sussors, w know tht it is sf to put tht no first. In gnrl, postorr trvrsl of th rvrs grph visits nos only ftr ll prssors hv n visit. A B C D E H F G A B D E H C 0 F 1 G 2 Numrs show postorr trvrsl orr strting from G: vrything tht must om for G. Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 13

Gnrl Grph Trvrsl Algorithm COLLECTION OF VERTICES fring; fring = INITIAL COLLECTION; whil (!fring.isempty()) { Vrtx v = fring.remove HIGHEST PRIORITY ITEM(); if (!MARKED(v)) { MARK(v); VISIT(v); For h g(v,w) { if (NEEDS PROCESSING(w)) A w to fring; Rpl COLLECTION OF VERTICES, INITIAL COLLECTION, t. with vrious typs, xprssions, or mthos to iffrnt grph lgorithms. Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 14

Exmpl: Dpth-First Trvrsl Prolm: Visit vry no rhl from v on, visiting nos furthr from strt first. Stk<Vrtx> fring; fring = stk ontining {v; whil (!fring.isempty()) { Vrtx v = fring.pop(); if (!mrk(v)) { mrk(v); VISIT(v); For h g(v,w) { if (!mrk(w)) fring.push(w); Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 15

Dpth-First Trvrsl Illustrt [] f [,] f [,,] f [,f,,] f [f,,] f [,,] f [,] f [] f Fring: z Mrk: Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 16

Topologil Sort in Ation C 0 fring C 0 C 0 C 0 A 0 D 2 F 1 A 0 D 1 F 1 A 0 D 0 F 0 A 0 D 0 F 0 B 1 E 3 G 1 B 0 E 3 G 1 B 0 E 3 G 1 B 0 E 2 G 1 H 1 Output: [] H 1 [A] H 1 [A,C] H 1 [A,C,B] C 0 C 0 C 0 A 0 D 0 F 0 A 0 D 0 F 0... A 0 D 0 F 0 B 0 E 1 G 0 B 0 E 0 G 0 B 0 E 0 G 0 H 1 [A,C,B,F] H 1 [A,C,B,F,D] H 0 [A,C,B,F,D,E,G,H] Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 17

Shortst Pths: Dijkstr s Algorithm Prolm: Givn grph (irt or unirt) with non-ngtiv g wights, omput shortst pths from givn sour no, s, to ll nos. Shortst = sum of wights long pth is smllst. For h no, kp stimt istn from s,......n of pring no in shortst pth from s. PriorityQuu<Vrtx> fring; For h no v { v.ist() = ; v.k() = null; s.ist() = 0; fring = priority quu orr y smllst.ist(); ll vrtis to fring; whil (!fring.isempty()) { Vrtx v = fring.rmovfirst(); For h g(v,w) { if (v.ist() + wight(v,w) < w.ist()) { w.ist() = v.ist() + wight(v,w); w.k() = v; Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 18

Exmpl C 5 4 A 0 2 B 2 2 3 5 3 7 D 4 E 1 F 3 6 2 G 1 H C 5 5 4 A 0 2 B 2 2 2 3 5 3 7 D 3 4 E 5 1 F 3 6 2 G 6 1 H 9 C 5 5 4 A 0 2 B 2 2 2 3 5 3 7 D 3 4 E 1 F 3 6 2 G 7 1 H C 5 5 4 A 0 2 B 2 2 2 3 5 3 7 D 3 4 E 5 1 F 7 3 6 2 G 6 1 H 9 C 5 5 4 A 0 2 B 2 2 2 3 5 3 7 D 3 4 E 5 1 F 3 6 2 G 7 1 H C 5 5 4 A 0 2 B 2 2 2 3 5 3 7 D 3 4 E 5 1 F 6 3 6 2 G 6 1 H 7 Finl rsult: C 5 5 4 A 0 2 B 2 2 2 3 5 3 7 D 3 4 E 5 1 F 6 3 6 2 G 6 1 H 7 Shortst-pth tr X pross no t istn Y no in fring t istn Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 19