Outline. CS38 Introduction to Algorithms. Graphs. Graphs. Graphs. Graph traversals

Similar documents
V = set of vertices (vertex / node) E = set of edges (v, w) (v, w in V)

10.2 Graph Terminology and Special Types of Graphs

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Greedy Algorithm. Algorithm Fall Semester

Lecture 13: Graphs I: Breadth First Search

Introduction. Example

Premaster Course Algorithms 1 Chapter 6: Shortest Paths. Christian Scheideler SS 2018

MITSUBISHI ELECTRIC RESEARCH LABORATORIES Cambridge, Massachusetts. Introduction to Matroids and Applications. Srikumar Ramalingam

Distance vector protocol

COMP108 Algorithmic Foundations

Paradigm 5. Data Structure. Suffix trees. What is a suffix tree? Suffix tree. Simple applications. Simple applications. Algorithms

Bayesian Networks: Directed Markov Properties (Cont d) and Markov Equivalent DAGs

Graph theory Route problems

Chapter 4 Fuzzy Graph and Relation

12/9/14. CS151 Fall 20124Lecture (almost there) 12/6. Graphs. Seven Bridges of Königsberg. Leonard Euler

Outline. CS38 Introduction to Algorithms. Administrative Stuff. Administrative Stuff. Motivation/Overview. Administrative Stuff

Adjacency. Adjacency Two vertices u and v are adjacent if there is an edge connecting them. This is sometimes written as u v.

Problem Final Exam Set 2 Solutions

Containers: Queue and List

Internet Routing. Reminder: Routing. CPSC Network Programming

Lecture 8: Graph-theoretic problems (again)

Lesson 4.4. Euler Circuits and Paths. Explore This

Lecture 12 : Topological Spaces

Class Overview. Database Design. Database Design Process. Database Design. Introduction to Data Management CSE 414

Ma/CS 6b Class 1: Graph Recap

Convex Hull Algorithms. Convex hull: basic facts

CMPUT101 Introduction to Computing - Summer 2002

CS 551 Computer Graphics. Hidden Surface Elimination. Z-Buffering. Basic idea: Hidden Surface Removal

Ma/CS 6b Class 1: Graph Recap

1 Disjoint-set data structure.

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries

Structure in solution spaces: Three lessons from Jean-Claude

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence Winter 2016

CS553 Lecture Introduction to Data-flow Analysis 1

Minimal Memory Abstractions

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs.

Elementary Graph Algorithms

Graph Contraction and Connectivity

s 1 t 4 s 2 4 t 2 a b r 2 r 8 r10 g 4

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence

Outline. Motivation Background ARCH. Experiment Additional usages for Input-Depth. Regular Expression Matching DPI over Compressed HTTP

Chapter 16. Shortest Paths Shortest Weighted Paths

Introduction to Algebra

Table-driven look-ahead lexical analysis

Today. CS 188: Artificial Intelligence Fall Recap: Search. Example: Pancake Problem. Example: Pancake Problem. General Tree Search.

Outline. Graphs Describing Precedence. Graphs Describing Precedence. Topological SorFng of DAGs. Graphs Describing Precedence 4/25/12. Part 10.

Suffix trees, suffix arrays, BWT

Graduate Algorithms CS F-15 Graphs, BFS, & DFS

Internet Routing. IP Packet Format. IP Fragmentation & Reassembly. Principles of Internet Routing. Computer Networks 9/29/2014.

ITEC2620 Introduction to Data Structures

Orthogonal line segment intersection

Partitioning for Parallelization Using Graph Parsing

Lesson6: Modeling the Web as a graph Unit5: Linear Algebra for graphs

Comparison-based Choices

Computational geometry

What are suffix trees?

A dual of the rectangle-segmentation problem for binary matrices

CS 241 Week 4 Tutorial Solutions

CS453 INTRODUCTION TO DATAFLOW ANALYSIS

Duality in linear interval equations

COMBINATORIAL PATTERN MATCHING

13.1 Weighted Graphs and Their Representation

Asurveyofpractical algorithms for suffix tree construction in external memory

CS311H: Discrete Mathematics. Graph Theory IV. A Non-planar Graph. Regions of a Planar Graph. Euler s Formula. Instructor: Işıl Dillig

Comparing Hierarchical Data in External Memory

22.3 Depth First Search

Data Structures and Algorithms

arxiv: v1 [math.co] 18 Sep 2015

A Tautology Checker loosely related to Stålmarck s Algorithm by Martin Richards

Union-Find Problem. Using Arrays And Chains. A Set As A Tree. Result Of A Find Operation

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

Tight triangulations: a link between combinatorics and topology

COMMON FRACTIONS. or a / b = a b. , a is called the numerator, and b is called the denominator.

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

2 Computing all Intersections of a Set of Segments Line Segment Intersection

Today. Search Problems. Uninformed Search Methods. Depth-First Search Breadth-First Search Uniform-Cost Search

Calculus Differentiation

CSCI 446: Artificial Intelligence

Advanced Programming Handout 5. Enter Okasaki. Persistent vs. Ephemeral. Functional Queues. Simple Example. Persistent vs.

COSC 6374 Parallel Computation. Non-blocking Collective Operations. Edgar Gabriel Fall Overview

3D convex hulls. Convex Hull in 3D. convex polyhedron. convex polyhedron. The problem: Given a set P of points in 3D, compute their convex hull

Basic Graph Algorithms

Hash-based Subgraph Query Processing Method for Graph-structured XML Documents

Announcements. CS 188: Artificial Intelligence Fall Recap: Search. Today. Example: Pancake Problem. Example: Pancake Problem

MATH 25 CLASS 5 NOTES, SEP

Conversion of Binary Space Partitioning Trees. to Boundary Representation. Abstract. Binary Space Partitioning Trees (BSP-Trees) have been proposed

Width and Bounding Box of Imprecise Points

WORKSHOP 19 GLOBAL/LOCAL MODELING USING FEM FIELDS

Distributed Systems Principles and Paradigms. Chapter 11: Distributed File Systems

An Efficient Algorithm for the Physical Mapping of Clustered Task Graphs onto Multiprocessor Architectures

Compression Outline :Algorithms in the Real World. Lempel-Ziv Algorithms. LZ77: Sliding Window Lempel-Ziv

9 Graph Cutting Procedures

The Greedy Method. The Greedy Method

A distributed edit-compile workflow

Network Layer: Routing Classifications; Shortest Path Routing

Analysis of Algorithms Prof. Karen Daniels

Using SIMD Registers and Instructions to Enable Instruction-Level Parallelism in Sorting Algorithms

Distributed Systems Principles and Paradigms

Information Retrieval and Organisation

String comparison by transposition networks

Transcription:

Outline CS38 Introution to Algorithms Leture 2 April 3, 2014 grph trversls (BFS, DFS) onnetivity topologil sort strongly onnete omponents heps n hepsort greey lgorithms April 3, 2014 CS38 Leture 2 2 Grphs Grph G = (V, E) irete or unirete nottion: n = V, m = E (note: m n 2 ) jeny list or jeny mtrix 0 1 1 0 0 0 0 1 0 Grphs Grphs moel mny things physil networks (e.g. ros) ommunition networks (e.g. internet) inormtion networks (e.g. the we) soil networks (e.g. riens) epeneny networks (e.g. topis in this ourse) so mny unmentl lgorithms operte on grphs April 3, 2014 CS38 Leture 2 3 April 3, 2014 CS38 Leture 2 4 Grphs Grph terminology: n unirete grph is onnete i there is pth etween eh pir o verties tree is onnete, unirete grph with no yles; orest is olletion o isjoint trees irete grph is strongly onnete i there is pth rom x to y n rom y to x, 8 x,y2v DAG is Direte Ayli Grph Grph trversls Grph trversl lgorithm: visit some or ll o the noes in grph, leling them with useul inormtion reth-irst: useul or unirete, yiels onnetivity n shortest-pths inormtion epth-irst: useul or irete, yiels numering use or topologil sort strongly-onnete omponent eomposition April 3, 2014 CS38 Leture 2 5 April 3, 2014 CS38 Leture 2 6 1

Breth irst serh BFS(unirete grph G, strting vertex s) 1. or eh vertex v, v.olor = white, v.ist = 1, v.pre = nil 2. s.olor = grey, s.ist = 0, s.pre = nil 3. Q = ;; ENQUEUE(Q, s) 4. WHILE Q is not empty u = DEQUEUE(Q) 5. or eh v jent to u 6. IF v.olor = white THEN 7. v.olor = grey, v.ist = u.ist + 1, v.pre = u 8. ENQUEUE(Q, v) 9. u.olor = lk BFS exmple rom CLRS Lemm: BFS runs in time O(m + n), when G is represente y n jeny list. April 3, 2014 CS38 Leture 2 7 8 Breth irst serh Lemm: or ll v 2 V, v.ist = istne(s, v), n shortest pth rom s to v is shortest pth rom s to v.pre ollowe y ege (v.pre,v) Proo: prtition V into levels L 0 = {s} L i = {v : 9 u 2 L i-1 suh tht (u,v) 2 E} Oserve: istne(s,v) = i, v 2 L i s Breth irst serh eges only within lyers or etween jent lyers L 0 L 1 L 2 L n Clim: t ny point in opertion o lgorithm: 1. lk/grey verties extly L 0, L 1,, L i n prt o L i+1 2. Q = (v 0, v 1, v 2, v 3,, v k ) n ll hve v.ist = level o v s eges only within lyers or etween jent lyers L 0 L 1 L 2 L n level i level i+1 hols initilly: s.olor = grey, s.ist = 0, Q = (s) April 3, 2014 CS38 Leture 2 10 s Breth irst serh L 0 L 1 L 2 L n Clim: t ny point in opertion o lgorithm: 1. lk/grey verties extly L 0, L 1,, L i n prt o L i+1 2. Q = (v 0, v 1, v 2, v 3,, v k ) n ll hve v.ist = level o v level i level i+1 eges only within lyers or etween jent lyers ) level i+1 ) level i+1 1 step: equeue v 0 ; white nrs o v 0 w/ ist = v 0.ist + 1 Depth irst serh DFS(irete grph G) 1. or eh vertex v, v.olor = white, v.pre = nil 2. time = 0 3. or eh vertex u, IF u.olor = white THEN DFS-VISIT(G, u) DFS-VISIT(irete grph G, strting vertex u) 1. time = time +1, u.isovere = time, u.olor = grey 2. or eh v jent to u, IF v.olor = white THEN 3. v.pre = u, DFS-VISIT(G, v) 4. u.olor = lk; time = time + 1; u.inishe = time Lemm: DFS runs in time O(m + n), when G is represente y n jeny list. Proo? April 3, 2014 CS38 Leture 2 11 April 3, 2014 CS38 Leture 2 12 2

Depth irst serh DFS(irete grph G) 1. or eh vertex v, v.olor = white, v.pre = nil 2. time = 0 3. or eh vertex u, IF u.olor = white THEN DFS-VISIT(G, u) DFS-VISIT(irete grph G, strting vertex u) 1. time = time +1, u.isovere = time, u.olor = grey 2. or eh v jent to u, IF v.olor = white THEN 3. v.pre = u, DFS-VISIT(G, v) 4. u.olor = lk; time = time + 1; u.inishe = time Lemm: DFS runs in time O(m + n), when G is represente y n jeny list. Proo: DFS-VISIT lle or eh vertex extly one; its j. list snne one; O(1) work April 3, 2014 CS38 Leture 2 13 Depth irst serh DFS yiels orest: the DFS orest eh vertex lele with isovery time n inishing time eges o G lssiie s tree eges k eges (point k to n nestor) orwr eges (point orwr to esennt) ross eges (ll others) April 3, 2014 CS38 Leture 2 14 DFS exmple rom CLRS DFS pplition: topologil sort Given DAG, list verties v 0, v 1,., v n so tht no eges rom v i to v j (j < i) exmple: e e April 3, 2014 CS38 Leture 2 16 DFS pplition: topologil sort Theorem: listing verties in reverse orer o DFS inishing times yiels topologil sort o DAG G (n implement in liner time; how?) Proo: lim or ll (u,v) 2 E, v.inish < u.inish when (u,v) explore, v not grey sine then G woul hve yle [k-ege] v white ) esenent o u so v inishes irst v lk ) lrey one, so v.inish is set n u.inish will e set with lter time Strongly onnete omponents sy tht x» y i there is irete pth rom x to y n rom y to x in G equivlene reltion, equivlene lsses re strongly onnete omponents o G lso, mximl strongly onnete susets SCC struture is DAG (why?) April 3, 2014 CS38 Leture 2 17 April 3, 2014 CS38 Leture 2 18 3

Strongly onnete omponents DFS tree rom v in G: ll noes rehle rom v DFS tree rom v in G T : ll noes tht n reh v v G with eges reverse Key: in sink SCC, this is extly the SCC April 3, 2014 CS38 Leture 2 19 Strongly onnete omponents v given v in sink SCC, run DFS strting there, then move to next in reverse topologil orer DFS orest woul give the SCCs Key #2: topologil orering onsistent with SCC DAG struture! (why?) April 3, 2014 CS38 Leture 2 20 Strongly onnete omponents Strongly onnete omponents SCC(irete grph G) 1. run DFS(G) 2. onstrut G T rom G 3. run DFS(G T ) ut in line 3, onsier verties in eresing orer o inishing times rom the irst DFS running time O(n + m) i G in j. list note: step 2 n e one in O(m + n) time trees in DFS orest o the seon DFS re the SCCs o G April 3, 2014 CS38 Leture 2 21 SCC(irete grph G) 1. run DFS(G) 2. onstrut G T rom G 3. run DFS(G T ) ut in line 3, onsier verties in eresing orer o inishing times rom the irst DFS Corretness (sketh): irst vertex is in sink SCC, DFS-VISIT olors lk, eetively removes next unvisite vertex is in sink ter removl n so on April 3, 2014 CS38 Leture 2 22 Summry O(m + n) time lgorithms or omputing BFS tree rom v in unirete G ining shortest pths rom v in unirete G omputing DFS orest in irete G omputing topologil orering o DAG ientiying the strongly onnete omponents o irete G (ll ssume G given in jeny list ormt) Heps A si t struture eyon stks n queues: hep rry o n elt/key pirs in speil orer min-hep or mx-hep opertions: INSERT(H, elt) INCREASE-KEY(H, i) EXTRACT-MAX(H) April 3, 2014 CS38 Leture 2 23 April 3, 2014 CS38 Leture 2 24 4

Heps A si t struture eyon stks n queues: hep rry o n elt/key pirs in speil orer min-hep or mx-hep opertions: time: INSERT(H, elt) O(log n) INCREASE-KEY(H, i) O(log n) EXTRACT-MAX(H) O(log n) Heps rry A represents inry tree tht is ull exept or possily lst row height = O(log n) prent(i) = i/2 let(i) = 2i right(i) = 2i+1 hep property: A[prent(i)] A[i] or ll i April 3, 2014 CS38 Leture 2 25 April 3, 2014 CS38 Leture 2 26 Heps key opertion: HEAPIFY-DOWN(H, i) Heps key opertion: HEAPIFY-UP(H, i) A[i] my violte hep property repetely swp with lrger hil running time? O(log n) or O(ht) A[i] my violte hep property repetely swp with lrger hil running time? O(log n) or O(ht) April 3, 2014 CS38 Leture 2 27 April 3, 2014 CS38 Leture 2 28 Heps How o you implement opertions: time: INSERT(H, elt) O(log n) INCREASE-KEY(H, i) O(log n) EXTRACT-MAX(H) O(log n) using HEAPIFY-UP n HEAPIFY-DOWN? BUILD-HEAP(A): re-orers rry A so tht it stisies hep property how o we o this? running time? Heps BUILD-HEAP(A): re-orers rry A so tht it stisies hep property ll HEAPIFY-DOWN(H, i) or i rom n ownto 1 running time O(n log n) more reul nlysis: O(n) April 3, 2014 CS38 Leture 2 29 April 3, 2014 CS38 Leture 2 30 5

Heps suies to show h 0 h/2 h = O(1) note: h 0 h = O(1) or < 1 oserve: (h+1)/2 h+1 = h/(2 h ) (1+1/h)/2 (1+1/h)/2 < 1 or h > 1 April 3, 2014 CS38 Leture 2 31 Hepsort Sorting n numers using hep BUILD-HEAP(A) O(n) repetely EXTRACT-MIN(H) n O(log n) totl O(n log n) Cn we o etter? O(n)? oserve tht only ever ompre vlues no eisions se on tul vlues o keys April 3, 2014 CS38 Leture 2 32 Sorting lower oun omprison-se sort: only inormtion out A use y lgorithm omes rom pirwise omprisons hepsort, mergesort, quiksort, visulize sequene o omprisons in tree: is A[i] A[j]? Greey Algorithms eh root-le pth onsistent with 1 perm. mximum pth length log(n!) = (n log n) April 3, 2014 CS38 Leture 2 33 April 3, 2014 CS38 Leture 2 34 Greey lgorithms Greey lgorithm prigm uil up solution inrementlly t eh step, mke the greey hoie Exmple: in unirete grph G = (V,E), vertex over is suset o V tht touhes every ege hr prolem: in the smllest vertex over e April 3, 2014 CS38 Leture 2 35 Dijkstr s lgorithm given irete grph G = (V,E) with non-negtive ege weights strting vertex s 2 V in shortest pths rom s to ll noes v note: unweighte se solve y BFS April 3, 2014 CS38 Leture 2 36 6

Dijkstr s lgorithm shortest pths exhiit optiml sustruture property optiml solution ontins within it optiml solutions to suprolems shortest pth rom x to y vi z ontins shortest pth rom x to z shortest pths rom s orm tree roote t s Min ie: mintin set S µ V with orret istnes nr u with smllest istne estimte April 3, 2014 CS38 Leture 2 37 7