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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

CS 241 Analysis of Algorithms

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

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

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

1 Introduction to Modulo 7 Arithmetic

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

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

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

12. Traffic engineering

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

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

COMP108 Algorithmic Foundations

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

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

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

Numbering Boundary Nodes

Present state Next state Q + M N

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

Planar Upward Drawings

CS September 2018

Trees as operads. Lecture A formalism of trees

ECE COMBINATIONAL BUILDING BLOCKS - INVEST 13 DECODERS AND ENCODERS

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

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

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)

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

QUESTIONS BEGIN HERE!

EE1000 Project 4 Digital Volt Meter

Similarity Search. The Binary Branch Distance. Nikolaus Augsten.

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

COMPLEXITY OF COUNTING PLANAR TILINGS BY TWO BARS

Multipoint Alternate Marking method for passive and hybrid performance monitoring

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

In order to learn which questions have been answered correctly: 1. Print these pages. 2. Answer the questions.

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

Polygons POLYGONS.

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

Seven-Segment Display Driver

SOLVED EXAMPLES. be the foci of an ellipse with eccentricity e. For any point P on the ellipse, prove that. tan

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

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 23, April 5, 2016

Aquauno Video 6 Plus Page 1

Walk Like a Mathematician Learning Task:

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

Garnir Polynomial and their Properties

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

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

Constructive Geometric Constraint Solving

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

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

QUESTIONS BEGIN HERE!

Chem 104A, Fall 2016, Midterm 1 Key

DUET WITH DIAMONDS COLOR SHIFTING BRACELET By Leslie Rogalski

Chapter 9. Graphs. 9.1 Graphs

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?

Problem solving by search

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

Section 3: Antiderivatives of Formulas

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

Outline. Binary Tree

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

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

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

CSI35 Chapter 11 Review

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

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

TOPIC 5: INTEGRATION

12 - M G P L Z - M9BW. Port type. Bore size ø12, ø16 20/25/32/40/50/ MPa 10 C to 60 C (With no condensation) 50 to 400 mm/s +1.

Multi-Section Coupled Line Couplers

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

arxiv: v1 [math.mg] 5 Oct 2015

Binomials and Pascal s Triangle

Graph Contraction and Connectivity

Linear Algebra Existence of the determinant. Expansion according to a row.

Complete Solutions for MATH 3012 Quiz 2, October 25, 2011, WTT

Instructions for Section 1

UNCORRECTED SAMPLE PAGES 4-1. Naming fractions KEY IDEAS. 1 Each shape represents ONE whole. a i ii. b i ii

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

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

This chapter covers special properties of planar graphs.

Minimum Spanning Trees

Last time: introduced our first computational model the DFA.

Transcription:

Moul grph.py CS 231 Nomi Nishimur 1 Introution Just lik th Python list n th Python itionry provi wys of storing, ssing, n moifying t, grph n viw s wy of storing, ssing, n moifying t. Bus Python os not hv uilt-in support for grphs, I hv suppli moul for us in th ours. This oumnt provis kgroun on th typs of grphs support y th moul s wll s spifis of th funtions provi. Th moul is not intn to ovr ll kins of grphs nor to provi ll possil funtions. Dtils of th implmnttion of th funtions r not isuss hr; to lrn mor out suh implmnttions, onsir tking CS 234. At tims you my writing psuoo tht uss grph oprtions. To us grph funtion, simply trnslt from ot nottion n put th nm in ll pitl lttrs. For xmpl, inst of grph.g ll(on, two), writ Eg Ll(grph, on, two). 2 Grph sis A grph onsists of st of vrtis n st of gs, whr h g is pir of vrtis, ll th npoints of th g. W llow t most on g twn pir of vrtis, so th gs must ll istint. You n think of gs s rprsnting onntions twn vrtis. If two vrtis r npoints of n g, thy r nighours. Th st of nighours of prtiulr vrtx is its nighourhoo. Th numr of nighours of vrtx is th gr of th vrtx, not g(v) for vrtx v. W will rquir tht h g hv two iffrnt vrtis s its npoints, so vrtx will nvr its own nighour. Two vrtis r jnt if thy r npoints of n g, n two gs r inint if thy shr n npoint. Th trm inint is lso us to sri th rltionship twn n g n its npoints. A squn of vrtis forms pth if thr is n g twn h onsutiv pir of vrtis in th squn, n yl if thr is lso n g twn th first n lst vrtis in th squn. At tims w might tlk out grph rprsnting nothr grph. In tht s, w my us th trm no s synonym for vrtx. Typilly w will rfr to vrtis in th originl grph n nos in th rprsnttion grph. 3 Moul grph.py 3.1 Ojts Th grph moul mks us of thr iffrnt typs of ojts: Vrtx, Eg, n Grph. In th grphs us in th ours, h Vrtx ojt hs n ID, ll, wight, n olour,

CS 231: Moul grph.py 2 whr th wight is n intgr n ll othr ttriuts r strings; th fult vlus of th ll, wight, n olour r "non", 0, n "whit". Th ID of h Vrtx must uniqu. Eh Eg ojt onsists of th IDs of two Vrtx ojts, ll, wight, n olour. Noti tht gs o not hv IDs; h g n uniquly intifi y th IDs of its two npoints. 3.2 Crting grph from fil Th moul grph.py ontins th funtion mk grph, whih onsums string (th nm of fil) n prous n ojt of typ Grph. A fil ontining grph t shoul ontin th following informtion, in this orr: th numr of vrtis in th grph (on on lin), ID, ll, wight, n olour of vrtx (four vlus pr lin, whr wight is n intgr n th othr vlus r strings), n IDs of oth npoints, ll, wight, n olour of n g (fiv vlus pr lin, whr wight is n intgr n othr vlus r strings). For grph with n vrtis n m gs, thr will totl of 1 + n + m lins in th fil. Pls not tht whn rting grph from fil, you n to spify lls, wights, n olours, vn if you wish to us th fults. 3.3 Mthos Th tl low lists th mthos tht n us on ojts of th lss Grph. Py los ttntion to th typs onsum n prou y h mtho; somtims you will hnling ojts n somtims you will hnling string IDs. Th fil grphus.py givs n xmpl of th mthos ing us. You n us print to print singl Vrtx, Eg, or Grph. If you wish to print list of gs, rt for loop n print h g in th list. Du to th wy tht grph is implmnt, list prou y mtho my not hv th itms ppr in pritl orr. Pls s informtion on th moul quiv.py for funtions to us in suh situtions. You n to nsur tht IDs r istint for ll vrtis; th o will not hk for you. Similrly, you shoul nsur tht whn you supply th npoints of n Eg, suh n Eg xists. Th orr in whih th npoints r suppli is not importnt. Tht is, th Eg with npoints with IDs "" n "" is intil to th Eg with npoints with IDs "" n ""; th two orrs of npoints r two wys of rfrring to th sm Eg. Bus th moul is sign to llow you to implmnt o with grphs without onsiring th tils of how th grph is implmnt, th worst-s osts list in th tl r not intn to rflt th tul osts of this prtiulr implmnttion. Whn writing n lgorithm for grphs, on oftn hooss mong vrious options with iffring osts for oprtions. Th osts list hr r not th st possil, ut rsonl hoi tht you n us for nlysis. Hr w us n to not th numr of vrtis in grph, m to not th numr of gs in grph, n to not th gr of th vrtx with ID on; not tht O(n). In

CS 231: Moul grph.py 3 ition, you n ssum tht ss to ny of th ttriuts n omplish in onstnt tim. Mtho Wht it os Cost Grph() rts nw mpty grph print(grph) lists informtion out vrtis n gs in grph Θ(nm) grph.vrtis() prous list of IDs of vrtis in grph Θ(n) grph.gs() prous list of Eg ojts in grph Θ(m) grph.nighours(on) prous list of IDs of nighours of th vrtx with ID on Θ() grph.r jnt(on, two) prous Tru if vrtis with IDs on n two r jnt n Fls othrwis Θ() grph.vrtx ll(on) prous th ll of th vrtx with ID on grph.vrtx wight(on) prous th wight of th vrtx with ID on grph.vrtx olour(on) prous th olour of th vrtx with ID on grph.g ll(on, two) prous th ll of th g twn vrtis with IDs on n two Θ() grph.g wight(on, two) prous th wight of th g twn vrtis with IDs on n two Θ() grph.g olour(on, two) prous th olour of th g twn vrtis with IDs on n two Θ() grph. vrtx(on) s nw vrtx with ID on grph.l vrtx(on) lts th vrtx with ID on n ll gs with th vrtx s n npoint Θ(m) grph. g(on, two) s nw g twn vrtis with IDs on n two grph.l g(on, two) lts th g twn vrtis with IDS on n two Θ(m) grph.st vrtx ll(on, nw) upts th ll of th vrtx with ID on to nw grph.st vrtx wight(on, nw) upts th wight of th vrtx with ID on to nw grph.st vrtx olour(on, nw) upts th olour of th vrtx with ID on to nw grph.st g ll(on, two, nw) upts th ll of th g twn vrtis with IDs on n two to nw Θ() grph.st g wight(on, two, nw) upts th wight of th g twn vrtis with IDs on n two to nw Θ() grph.st g olour(on, two, nw) upts th olour of th g twn vrtis with IDs on n two to nw Θ() grph == grph prous Tru if grph n grph hv vrtis with th sm IDs n th sm s gs (ut lls, wights, n olours n not iffr) low

CS 231: Moul grph.py 4 For your onvnin, th moul lso llows you to hk for qulity of grphs (us this only for tsts, pls), whr vrtx IDs n gs muh mth ut lls, wights, n olours n iffr. Not: Mor pris nlysis my possil y using th osrvtion tht th sum of th grs of th vrtis in grph is qul to twi th numr of gs. (To s why this is tru, osrv tht th gr of vrtx is th numr of nighours it hs, whih is qul to th numr of gs for whih it is n npoint. Sin h g hs two npoints, summing ovr th grs ounts h g twi.) For ssignmnt n xm qustions in this ours, unlss stt othrwis, you my us th lss pris oun otin y using th ft tht O(n). 4 Using th moul to writ o 4.1 Writing your own grph funtions You n lso writ your own grph funtions y irtly ssing th ttriuts in th lsss Vrtx n Eg. In lulting osts, you n ssum tht ss ny of th ttriuts n omplish in onstnt tim. A Vrtx ojt hs th following ttriuts: i ( string) ll ( string) wight (n intgr) olour ( string) An Eg ojt hs th following ttriuts: vrtx u ( Vrtx ojt) vrtx v ( Vrtx ojt) ll ( string) wight (n intgr) olour ( string) It is not rommn tht you irtly ss th ttriuts of th lss Grph, s trmining th osts rquirs knowlg xtrnous to this ours. 4.2 Copying grphs If you wish to mk opy of grph, import th opy moul n us opy.popy.

CS 231: Moul grph.py 5 g f j h i Figur 1: Smpl grph 1 Figur 2: Smpl grph 2 5 Smpl grphs Smpl grphs hv n provi for you in th fils smplgrph1.txt, smplgrph2.txt, smplgrph3.txt, smplgrph4.txt, n smplgrph5.txt. For your onvnin, thy hv n illustrt hr. Co tht you writ for ssignmnts shoul work for ny grph, not just th smpls provi. Th vrtis r ll with th vrtx IDs. Th lls on th gs of th lst two smpl grphs r th wights of th gs.

CS 231: Moul grph.py 6 Figur 3: Smpl grph 3 10 20 6 5 40 30 Figur 4: Smpl grph 4 15 5 12 1 6 6 7 3 f 4 3 11 9 g 9 Figur 5: Smpl grph 5