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

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

CS 241 Analysis of Algorithms

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

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

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

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

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

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

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

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

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

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

COMP108 Algorithmic Foundations

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

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

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

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

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

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

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.

Minimum Spanning Trees

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

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

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

1 Introduction to Modulo 7 Arithmetic

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

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

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

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

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

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

Problem solving by search

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

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

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

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

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

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?

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

Garnir Polynomial and their Properties

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

Similarity Search. The Binary Branch Distance. Nikolaus Augsten.

CSI35 Chapter 11 Review

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

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

Planar Upward Drawings

Lecture 20: Minimum Spanning Trees (CLRS 23)

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

Trees as operads. Lecture A formalism of trees

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

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

Outline. Binary Tree

Solutions to Homework 5

Graph Contraction and Connectivity

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

CS 103 BFS Alorithm. Mark Redekopp

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

Present state Next state Q + M N

Numbering Boundary Nodes

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

Scientific Programming. Graphs

ECE COMBINATIONAL BUILDING BLOCKS - INVEST 13 DECODERS AND ENCODERS

Constructive Geometric Constraint Solving

Round 7: Graphs (part I)

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

CS September 2018

Designing A Concrete Arch Bridge

QUESTIONS BEGIN HERE!

12. Traffic engineering

Seven-Segment Display Driver

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

14 Shortest Paths (November 8)

GREEDY TECHNIQUE. Greedy method vs. Dynamic programming method:

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

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

QUESTIONS BEGIN HERE!

(Minimum) Spanning Trees

Last time: introduced our first computational model the DFA.

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

EE1000 Project 4 Digital Volt Meter

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

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

Analysis for Balloon Modeling Structure based on Graph Theory

Chapter 9. Graphs. 9.1 Graphs

DUET WITH DIAMONDS COLOR SHIFTING BRACELET By Leslie Rogalski

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

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

Section 3: Antiderivatives of Formulas

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

COMPLEXITY OF COUNTING PLANAR TILINGS BY TWO BARS

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

Walk Like a Mathematician Learning Task:

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

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

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

16.unified Introduction to Computers and Programming. SOLUTIONS to Examination 4/30/04 9:05am - 10:00am

RAM Model. I/O Model. Real Machine Example: Nehalem : Algorithms in the Real World 4/9/13

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

Transcription:

Prim s Algorithm CS 461, Ltur 17 Jr Si Univrsity o Nw Mxio In Prim s lgorithm, th st A mintin y th lgorithm orms singl tr. Th tr strts rom n ritrry root vrtx n grows until it spns ll th vrtis in V At h stp, light g is to th tr A whih onnts A to n isolt vrtx o G A = (V, A) By our Corollry, this rul s only s gs to A, so whn th lgorithm trmints, it will rturn MST 2 Toy s Outlin Exmpl Run 2 3 18 12 18 12 2 3 2 3 18 3 14 14 4 Prim s Algorithm Brth First Srh Dpth First Srh Prim s lgorithm run on th xmpl grph, strting with th ottom vrtx. At h stg, thik gs r in A, n rrow points long A s s g, n sh gs r uslss. 1 3

An Implmnttion Prim s To implmnt Prim s lgorithm, w kp ll gs jnt to A in hp Whn w pull th minimum-wight g o th hp, w irst hk to s i oth its npoints r in A I not, w th g to A n thn th nighoring gs to th hp I w implmnt Prim s lgorithm this wy, its running tim is O( E log E ) = O( E log V ) Howvr, w n o ttr W will rk up th lgorithm into two prts, Prim-Init n Prim-Loop Prim(V,E,s){ Prim-Init(V,E,s); Prim-Loop(V,E,s); 4 6 Prim s Algorithm Prim-Init W n sp things up y notiing tht th lgorithm visits h vrtx only on Rthr thn kping th gs in th hp, w will kp hp o vrtis, whr th ky o h vrtx v is th wight o th minimum-wight g twn v n A (or ininity i thr is no suh g) Eh tim w nw g to A, w my n to rs th ky o som nighoring vrtis Prim-Init(V,E,s){ or h vrtx v in V - {s{ i ((v,s) is in E){ g(v) = (v,s); ky(v) = w((v,s)); ls{ g(v) = NULL; ky(v) = ininity; Hp-Insrt(v); 5 7

Prim-Loop Not Prim-Loop(V,E,s){ A = {; or (i = 1 to V - 1){ v = Hp-ExtrtMin(); g(v) to A; or (h g (u,v) in E){ i (u is not in A AND ky(u) > w(u,v)){ g(u) = (u,v); Hp-DrsKy(u,w(u,v)); rturn A; This nlysis ssums tht it is st to in ll th gs tht r inint to givn vrtx W hv not yt isuss how w n o this This rings us to isussion o how to rprsnt grph in omputr 8 Runtim? Grph Rprsnttion Th runtim o Prim s is omint y th ost o th hp oprtions Insrt, ExtrtMin n DrsKy Insrt n ExtrtMin r h ll O( V ) tims DrsKy is ll O( E ) tims, t most twi or h g I w us Fioni Hp, th mortiz osts o Insrt n DrsKy is O(1) n th mortiz ost o ExtrtMin is O(log V ) Thus th ovrll run tim o Prim s is O( E + V log V ) This is str thn Kruskl s unlss E = O( V ) Thr r two ommon t struturs us to xpliity rprsnt grphs Ajny Mtris Ajny Lists 9 11

Ajny Mtrix Th jny mtrix o grph G is V V mtrix o 0 s n 1 s For n jny mtrix A, th ntry A[i, j] is 1 i (i, j) E n 0 othrwis For unirt grphs, th jny mtrix is lwys symmtri: A[i, j] = A[j, i]. Also th igonl lmnts A[i, i] r ll zros Exmpl Rprsnttions g h i 0 1 1 0 0 0 0 0 0 1 0 1 1 1 0 0 0 0 1 1 0 1 1 0 0 0 0 0 1 1 0 1 1 0 0 0 0 1 1 1 0 1 0 0 0 0 0 0 1 1 0 0 0 0 g 0 0 0 0 0 0 0 1 0 h 0 0 0 0 0 0 1 0 1 i 0 0 0 0 0 0 1 1 0 g h i h g g Ajny mtrix n jny list rprsnttions or th xmpl grph. i i h 12 14 Exmpl Grph Ajny Mtrix g h i Givn n jny mtrix, w n i in Θ(1) tim whthr two vrtis r onnt y n g. W n lso list ll th nighors o vrtx in Θ( V ) tim y snning th row orrsponing to tht vrtx This is optiml in th worst s, howvr i vrtx hs w nighors, w still n to xmin vry ntry in th row to in thm ll Also, jny mtris rquir Θ( V 2 ) sp, rgrlss o how mny gs th grph hs, so it is only sp iint or vry ns grphs 13 15

Ajny Lists Tk Awy For sprs grphs grphs with rltivly w gs w r ttr o with jny lists An jny list is n rry o link lists, on list pr vrtx Eh link list stors th nighors o th orrsponing vrtx I w us th right typ o hp n th right grph rprsnttion, thn Prim s lgorithm tks O( E + V log V ) This omprs vorly with Kruskl s lgorithm whih tks O( E log V ) Kruskl s n Prims lgorithms r th two min lgorithms or ining th minimum spnning tr o onnt grph Thr r mny, mny othr typs o prolms in on grphs... 18 Ajny Lists Trvrsing Grph Th totl sp rquir or n jny list is O( V + E ) Listing ll th nighors o no v tks O(1+g(v)) tim W n trmin i (u, v) is n g in O(1 + g(u)) tim y snning th nighor list o u Not tht w n sp things up y storing th nighors o no not in lists ut rthr in hsh tls Thn w n trmin i n g is in th grph in xpt O(1) tim n still list ll th nighors o no v in O(1 + g(v)) tim Suppos w wnt to visit vry no in onnt grph (rprsnt ithr xpliitly or impliitly) Th simplst wy to o this is n lgorithm ll pth-irst srh W n writ this lgorithm rursivly or itrtivly - it s th sm oth wys, th itrtiv vrsion just mks th stk xpliit Both vrsions o th lgorithm r initilly pss sour vrtx v 17 19

Rursiv DFS Gnri Trvrs RursivDFS(v){ i ($v$ is unmrk){ mrk $v$; or h g (v,w){ RursivDFS(w); DFS is on instn o gnrl mily o grph trvrsl lgorithms This gnri grph trvrsl lgorithm stors st o nit gs in t strutur w ll ll g A g is just somthing w n put stu into n ltr tk stu out o - stks, quus n hps r ll xmpls o gs. 20 22 Itrtiv DFS Gnri Trvrs ItrtivDFS(s){ Push(s); whil (stk not mpty){ v = Pop(); i (v is unmrk){ mrk v; or h g (v,w){ Push(w); Trvrs(s){ put (nil,s) in g; whil (th g is not mpty){ tk som g (p,v) rom th g i (v is unmrk) mrk v; prnt(v) = p; or h g (v,w){ put (v,w) into th g; 21 23

Anlysis Proo Noti tht w r kping gs in th g inst o vrtis This is us w wnt to rmmr whn w visit vrtx v or th irst tim, whih prviously-visit vrtx p put v into th g This vrtx p is ll th prnt o v It s ovious tht no no is mrk mor thn on W nxt show tht h vrtx is mrk t lst on. Lt v s vrtx n lt s u v th pth rom s to v with th minimum numr o gs. (Sin th grph is onnt suh pth lwys xists) I th lgorithm mrks u, thn it must put (u, v) in th g, so it must ltr tk (u, v) out o th g, t whih point v must mrk Thus y inution on th shortst-pth istn rom s, th lgorithm mrks vry vrtx in th grph 24 Lmm Proo Trvrs(s) mrks h vrtx in onnt grph xtly on, n th st o gs (v, prnt(v)), with prnt(v) not nil, orm spnning tr o th grph. Cll n g (v, prnt(v)) with prnt(v) nil prnt g It now rmins to shown tht th prnt gs orm spnning tr o th grph For ny no v, th pth o prnt gs v prnt(v) prnt(prnt(v)) vntully ls k to s, so th st o prnt gs orm onnt grph. Sin vry no xpt s hs uniqu prnt g, th totl numr o prnt gs is xtly on lss thn th totl numr o vrtis Thus th prnt gs orm spnning tr (w ll show this in th in-lss xris) 25 27

DFS n BFS DFS vs BFS I w implmnt th g y using stk, w hv Dpth First Srh I w implmnt th g y using quu, w hv Brth First Srh Not tht DFS trs tn to long n skinny whil BFS trs r short n t In ition, th BFS tr ontins shortst pths rom th strt vrtx s to vry othr vrtx in its onnt omponnt. (hr w in th lngth o pth to th numr o gs in th pth) 28 Anlysis Finl Not Not tht i w us jny lists or th grph, th ovrh or th or loop is only onstnt pr g (no mttr how w implmnt th g) I w implmnt th g using ithr stks or quus, h oprtion on th g tks onstnt tim Hn th ovrll runtim is O( V + E ) = O( E ) Now ssum th gs r wight I w implmnt th g using priority quu, lwys xtrting th minimum wight g rom th g, thn w hv vrsion o Prim s lgorithm Eh xtrtion rom th g now tks O( E ) tim so th totl running tim is O( V + E log E ) 29 31

Exmpl A pth-irst spnning tr n rth-irst spnning tr o on omponnt o th xmpl grph, with strt vrtx. 32 In Clss Exris Consir onnt grph tht hs n vrtis n n 1 gs. Prov y inution on n tht suh grph is tr. Q: Wht is th s s? Q: Wht is th inutiv hypothsis? Q: Wht is th inutiv stp? 33