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

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

Greedy Algorithm. Algorithm Fall Semester

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

10.2 Graph Terminology and Special Types of Graphs

Distance vector protocol

Lecture 13: Graphs I: Breadth First Search

Graph theory Route problems

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

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

COMP108 Algorithmic Foundations

Internet Routing. Reminder: Routing. CPSC Network Programming

Lecture 8: Graph-theoretic problems (again)

Lesson 4.4. Euler Circuits and Paths. Explore This

Introduction. Example

Minimal Memory Abstractions

CMPUT101 Introduction to Computing - Summer 2002

COSC 6374 Parallel Computation. Dense Matrix Operations

CS 241 Week 4 Tutorial Solutions

UTMC APPLICATION NOTE UT1553B BCRT TO INTERFACE PSEUDO-DUAL-PORT RAM ARCHITECTURE INTRODUCTION ARBITRATION DETAILS DESIGN SELECTIONS

Lexical Analysis: Constructing a Scanner from Regular Expressions

5 ANGLES AND POLYGONS

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

Table-driven look-ahead lexical analysis

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

Problem Final Exam Set 2 Solutions

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

CS553 Lecture Introduction to Data-flow Analysis 1

Graph Contraction and Connectivity

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

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

In the last lecture, we discussed how valid tokens may be specified by regular expressions.

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

LINX MATRIX SWITCHERS FIRMWARE UPDATE INSTRUCTIONS FIRMWARE VERSION

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

Chapter 4 Fuzzy Graph and Relation

Network Layer: Routing Classifications; Shortest Path Routing

Finite Automata. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 21, 2015

Structure in solution spaces: Three lessons from Jean-Claude

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

Convex Hull Algorithms. Convex hull: basic facts

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

WORKSHOP 9 HEX MESH USING SWEEP VECTOR

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

Chapter 16. Shortest Paths Shortest Weighted Paths

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

Definition of Regular Expression

Pattern Matching. Pattern Matching. Pattern Matching. Review of Regular Expressions

Solids. Solids. Curriculum Ready.

Introduction to Algebra

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

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

13.1 Weighted Graphs and Their Representation

Ma/CS 6b Class 1: Graph Recap

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

Containers: Queue and List

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

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

Ma/CS 6b Class 1: Graph Recap

6.045J/18.400J: Automata, Computability and Complexity. Quiz 2: Solutions. Please write your name in the upper corner of each page.

Error Numbers of the Standard Function Block

Doubts about how to use azimuth values from a Coordinate Object. Juan Antonio Breña Moral

Fig.25: the Role of LEX

Shared Memory Architectures. Programming and Synchronization. Today s Outline. Page 1. Message passing review Cosmic Cube discussion

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

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

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Type Checking. Roadmap (Where are we?) Last lecture Context-sensitive analysis. This lecture Type checking. Symbol tables

Lecture 12 : Topological Spaces

PROBLEM OF APOLLONIUS

CS453 INTRODUCTION TO DATAFLOW ANALYSIS

COMP 423 lecture 11 Jan. 28, 2008

Comparing Hierarchical Data in External Memory

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

Distributed Systems Principles and Paradigms

Midterm Exam CSC October 2001

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

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

CSCI 446: Artificial Intelligence

Reducing a DFA to a Minimal DFA

CICS Application Design

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

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

All in One Kit. Quick Start Guide CONNECTING WITH OTHER DEVICES SDE-4003/ * 27. English-1

COSC 6374 Parallel Computation. Communication Performance Modeling (II) Edgar Gabriel Fall Overview. Impact of communication costs on Speedup

To access your mailbox from inside your organization. For assistance, call:

Rolling Back Remote Provisioning Changes. Dell Command Integration for System Center

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

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

Section 2.3 Functions. Definition: Let A and B be sets. A function (mapping, map) f from A to B, denoted f :A B, is a subset of A B such that

Graph Searching & Perfect Graphs

Final Exam Review F 06 M 236 Be sure to look over all of your tests, as well as over the activities you did in the activity book

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata

AI Adjacent Fields. This slide deck courtesy of Dan Klein at UC Berkeley

Section 5.3 : Finding Area Between Curves

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) *

WORKSHOP 19 GLOBAL/LOCAL MODELING USING FEM FIELDS

A distributed edit-compile workflow

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the

Can Pythagoras Swim?

Transcription:

Definitions G = (V, E) V = set of verties (vertex / noe) E = set of eges (v, w) (v, w in V) (v, w) orere => irete grph (igrph) (v, w) non-orere => unirete grph igrph: w is jent to v if there is n ege from v to w ege my e (v, w, ) where is ost omponent (e.g. istne) 05/12/2016 DFR - DSA - Grphs 1 1

Exmples 05/12/2016 DFR - DSA - Grphs 1 2

Mening & Use A grph is use to represent ritrry reltionships mong t ojets e.g. unirete grphs ommunitions network trnsport network (ro, ril, ir, se) with osts/istnes (trvelling slesmn prolem) e.g. irete grphs (igrph) flow of ontrol in omputer progrms University ourse plnning (epeneny grph) stte trnsition igrms 05/12/2016 DFR - DSA - Grphs 1 3

Other ADTs linke list irete yli grph (g) (g) tree 05/12/2016 DFR - DSA - Grphs 1 4

Terminology PATH: sequene of verties v 1, v 2, v n suh tht v 1 -> v 2, v 2 -> v 3, v n-1 -> v n re eges LENGTH: SIMPLE PATH: SIMPLE CYCLE: numer of eges in pth (v enotes pth length 0 from v to v) ll verties re istint (exept possily the first n the lst) simple pth of length >= 1 tht egins (irete grph) n ens t the sme vertex 05/12/2016 DFR - DSA - Grphs 1 5

Grphs & Cyles A yle is pth whih egins n ens t the sme vertex A grph with no yles is yli A irete grph with no yles is irete yli grph (DAG) DAG irete grphs unirete grphs 05/12/2016 DFR - DSA - Grphs 1 6

Unirete Grphs For yles in unirete grphs, the eges must e istint sine (u,v) n (v, u) re the sme ege onnete: if there exists pth from every vertex to every other vertex non-onnete: onnete: 05/12/2016 DFR - DSA - Grphs 1 7

Direte Grphs A onnete irete grph is lle strongly onnete i.e. there is pth from every vertex to every other vertex if the igrph is not strongly onnete BUT the unerlying grph, without istintion to the iretion, is onnete, then the grph is si to e wekly onnete strong: wek: not: 05/12/2016 DFR - DSA - Grphs 1 8

Complete Grph A grph is omplete if there is n ege etween every pir of verties 12 eges 6 eges n * (n - 1) n * (n - 1) / 2 05/12/2016 DFR - DSA - Grphs 1 9

Ajeny Mtrix For eh ege (u, v) set [u, v] = 1 storge => omeg(n 2 ) re in/ serh => O(n 2 ) 0 0 0 0 1 0 0 0 1 1 0 1 0 0 0 0 1 0 0 0 1 0 0 1 1 0 0 1 1 1 1 0 05/12/2016 DFR - DSA - Grphs 1

Ajeny List Use list of noes where eh noe points to list of jent noes (etter for sprse grphs) spe = O( V + E ) (for nme verties - use hsh tle) 05/12/2016 DFR - DSA - Grphs 1 11

Opertions insert remove fin vertex ege list opertions nvigte is_pth is_yle shortest pth spnning forest topologil sort 05/12/2016 DFR - DSA - Grphs 1 12

Shortest Pth 1 Dijkstr s lgorithm Single soure shortest pth (non-negtive osts) Determines the shortest pth from soure to every other vertex in the grph where the length of the pth is the sum of the osts of the eges S - set of verties; shortest istne from soure lrey known eh step s vertex v whose istne from S is s short s possile the visite verties (noes) speil pth: shortest pth from the soure to v pssing through u rry D: length of shortest speil pth to eh vertex C[i,j]: ost of v i to v j (no ege ost is infinite ) 05/12/2016 DFR - DSA - Grphs 1 13

Dijkstr s lgorithm priniples Given strt noe x, note the ege lengths from x to the remining noes in the grph. Choose the shortest ege from x to noe y. Mrk noes x n y s visite. S = {x,y} Chek to see if there is shorter pth to the remining (unvisite) noes, {V-S}, in the grph from x vi y. If so, upte the pth lengths so fr lulte. Repet the proess until ll noes hve een visite. y x {V-S} unvisite noes 05/12/2016 DFR - DSA - Grphs 1 14

Dijkstr - Exmple ( ) ( 30) ( e 0) ( 50) ( e ) ( 20) ( e 60) Strt visite {}, unvisite {,,, e}, shortest pth ( ) = infinity e Visite {, }, unvisite {,, e} D = [,, 30, 0] (-- 60) (-- ) (--e ) D = [, 60, 30, 0] Shortest pth (- 30) visite {,, }, unvisite {, e} (-- 50) (--e 90) D = [, 50, 30, 90] Shortest pth (- 50) visite {,,, }, unvisite {e} (--e 60) D = [, 50, 30, 60] Shortest pth (-e 60) visite {,,,, e}, unvisite { } No noes left! Finl nswer:- D = [, 50, 30, 60] 05/12/2016 DFR - DSA - Grphs 1 15

Dijkstr - Exmple Itertion S w D[] D[] D[] D[e] 0 initil {} - 30 0 1 {,} 60 30 0 2 {,,} 50 30 90 3 {,,,} 50 30 60 4 {,,,,e} e 50 30 60 See seprte notes on worke exmple:- (i) Revision notes (ii) stuy pln 50 30 20 e 60 05/12/2016 DFR - DSA - Grphs 1 16

Dijkstr Exmple - pitures 0 0 0 50 30 20 e 60 50 30 20 e e e D [,, 30, 0 ] D [,, 30, 0 ] D [, 60, 30, 0 ] E [,,, ] E [,,, ] E [,,, ] L [,, 30, 0 ] L [,, 30, 0 ] L [, 50, 30, 0 ] e 60 50 30 20 e 60 05/12/2016 DFR - DSA - Grphs 1 17

Dijkstr Exmple - pitures 0 0 0 50 30 20 e 60 50 30 20 e e e D [, 60, 30, 0 ] D [, 50, 30, 90 ] D [, 50, 30, 60 ] E [,,, ] E [,,, ] E [,,, ] L [, 50, 30, 0 ] L [, 20, 30, 60 ] L [, 20, 30, ] e 60 50 30 20 e 60 05/12/2016 DFR - DSA - Grphs 1 18

Dijkstr s Algorithm Grph (G) + Cost Mtrix (C) 0 30 e 50 60 20 e 30 0 50 20 60 e NB ount the numer of eges in the grph n the ost mtrix 05/12/2016 DFR - DSA - Grphs 1 19

Dijkstr s Algorithm Dijkstr () { S = {} // G = (V, E) for ( i in V-S) D[i] = C[, i] // initilistion while (!is_empty(v-s)) { hoose w in V-S suh tht D[w] is minimum S = S + {w} foreh ( v in V-S) D[v] = min(d[v], D[w]+C[w,v]) } // proess } // D[i] = istne; C[i,j] = ost mtrix; S = {visite noes} 05/12/2016 DFR - DSA - Grphs 1 20

Dijkstr s Algorithm Dijkstr () -- is the strt noe { S = {} -- S represents the noes visite for ( i in V-S) D[i] = C[, i] -- initilise D (pth lengths) -- from the strt noe while (!is_empty(v-s)) { hoose w in V-S suh tht D[w] is minimum -- unvisite noe with shortest pth from strt_noe S = S + {w} -- this noe to visite noes foreh ( v in V-S) D[v] = min(d[v], D[w]+C[w,v]) -- relulte pths vi w to unvisite noes } } 05/12/2016 DFR - DSA - Grphs 1 21

Dijkstr - Comment greey lgorithm - lol est solution is est overll hoose w in V-S suh tht D[w] is minimum (mening?) rell tht D[i] mens the length of the shortest pth to eh vertex note tht the lgorithm prtitions the noes into two spes S (initilly with the strt noe) n V-S (the remining noes) visite / unvisite foreh ( v in V-S) D[v] = min(d[v], D[w]+C[w,v]) (mening?) w is the noe with the minimum istne from the soure (not in S) D[v] mens the shortest (speil) pth length so fr lulte D[w] is the ost (so fr) to w (gin shortest (speil) pth length) C[w,v] is the ost from noe w to noe v (ege ost) 05/12/2016 DFR - DSA - Grphs 1 22

Dijkstr priniples revisite Choose the strt noe S = {} G = (V, E) S represents visite noes; V-S represents unvisite noes D represents the pth lengths from to the remining noes (V-) C[x,y] represents the ost mtrix the ost of the ege x y Algorithm Choose the shortest pth to n unvisite noe (my e n ege) A this noe (w) to the set of visite noes S Clulte n lterntive pth vi w to ll noes v in {V-S} hoose if istne D[w]+C[w,v] is shorter thn D[v] pth length - D[w] w C[w,v] - ege weight for eh v in {V-S} D[v] previous pth length 05/12/2016 DFR - DSA - Grphs 1 23

Dijkstr s Algorithm + Shortest Pth Tree Dijkstr () { S = {} for ( i in V-S) { D[i] = C[, i]; E[i] = ; L[i] = C[,i]; } while (!is_empty(v-s)) { hoose w in V-S suh tht D[w] is minimum } S = S + {w} foreh ( v in V-S) if (D[w]+C[w,v])< D[v] ) { D[v] = D[w]+C[w,v]; E[v] = w; L[v] = C[w,v]; } } 05/12/2016 DFR - DSA - Grphs 1 24

Dijkstr s Algorithm + Shortest Pth Tree Dijkstr () -- is the strt noe { S = {} -- S represents the noes visite for ( i in V-S) { D[i] = C[, i]; E[i] = ; L[i] = C[,i]; } -- initilise D + SPT (E + L) while (!is_empty(v-s)) { hoose w in V-S suh tht D[w] is minimum -- unvisite noe with shortest pth from strt_noe S = S + {w} foreh ( v in V-S) if (D[w]+C[w,v])< D[v] ) { D[v] = D[w]+C[w,v]; E[v] = w; L[v] = C[w,v]; } -- relulte pths n SPT (E + L) } } 05/12/2016 DFR - DSA - Grphs 1 25

Dijkstr Exmple - pitures 0 0 0 50 30 20 e 60 50 30 20 e e e D [,, 30, 0 ] D [,, 30, 0 ] D [, 60, 30, 0 ] E [,,, ] E [,,, ] E [,,, ] L [,, 30, 0 ] L [,, 30, 0 ] L [, 50, 30, 0 ] e 60 50 30 20 e 60 05/12/2016 DFR - DSA - Grphs 1 26

Dijkstr Exmple - pitures 0 0 0 50 30 20 e 60 50 30 20 e e e D [, 60, 30, 0 ] D [, 50, 30, 90 ] D [, 50, 30, 60 ] E [,,, ] E [,,, ] E [,,, ] L [, 50, 30, 0 ] L [, 20, 30, 60 ] L [, 20, 30, ] e 60 50 30 20 e 60 05/12/2016 DFR - DSA - Grphs 1 27

Shortest Pth 2 All pirs shortest pth prolem (i.e. Shortest pth etween ny two verties) pply Dijkstr s lgorithm to eh noe in turn pply Floy s lgorithm Floy given G = (V,E), non-negtive osts C[v,w], for eh orere pir (v,w) fin the shortest pth note the initil onitions use n rry A[i,j] whih is initilise to C[i,j], i.e. the initil ege osts if no ege exists C[i,j]= (infinite ost) for n verties there re n itertions over the rry A Floy is thus O(n 3 ) 05/12/2016 DFR - DSA - Grphs 1 28

Floy s Algorithm Floy ( ) { A[i,k] A[i,j] A[k,j] for (i in 1..n) for (j in 1..n) if (i <> j) A[i, j] = C[i, j] for (i in 1..n) A[i, i] = 0 -- initilistion for (k in 1..n) for (i in 1..n) for (j in 1..n) } if ( A[i, k] + A[k, j] < A[i, j]) A[i, j] = A[i, k] + A[k, j] 05/12/2016 DFR - DSA - Grphs 1 29

Floy - Exmple A 0 [i,j] = 0 8 5 A 1 [i,j] = 0 8 5 3 0 3 0 8 2 0 2 0 A 2 [i,j] = 0 8 5 A 3 [i,j] = 0 7 5 3 0 8 3 0 8 5 2 0 5 2 0 = infinity 2 1 3 2 2 3 8 5 05/12/2016 DFR - DSA - Grphs 1 30

Floy - Comment Initilistion is the osts in C (i.e. Initil ege osts) with the igonl (i.e. v => v) set to 0 for eh noe (k = 1..n) go through the rry (i, j = 1..n) n ompute osts - i.e. hek if there is heper pth from noe i to noe j vi noe k - if so hnge A[i, j] if ( A[i, k] + A[k, j] < A[i, j]) A[i, j] = A[i, k] + A[k, j] 05/12/2016 DFR - DSA - Grphs 1 31

Trnsitive Closure & Wrshll s Algorithm Determine if pth exists from vertex i to vertex j C[i, j] = 1 if n ege exists (i <> j), otherwise = 0 ompute A[i, j], suh tht A[i, j] = 1 if there exists pth of length 1 or more from vertex i to vertex j A is lle the trnsitive losure of the jeny mtrix Note tht this is speil se of Floy s where we re not iretly intereste in the osts 05/12/2016 DFR - DSA - Grphs 1 32

Wrshll s Algorithm Wrshll ( ) { A[i,k] A[i,j] A[k,j] for (i in 1..n) for (j in 1..n) A[i, j] = C[i, j] for (i in 1..n) A[i, i] = 0 -- initilistion for (k in 1..n) for (i in 1..n) for (j in 1..n) if (A[i, j] = 0) A[i, j] = A[i, k] n A[k, j] } 05/12/2016 DFR - DSA - Grphs 1 33

Wrshll - Exmple A 0 [i,j] = 0 1 1 A 1 [i,j] = 0 1 1 1 0 0 1 1 1 0 1 0 0 1 0 A 2 [i,j] = 1 1 1 A 3 [i,j] = 1 1 1 1 1 1 1 1 1 2 1 3 2 2 8 5 1 1 1 1 1 1 3 05/12/2016 DFR - DSA - Grphs 1 34

Wrshll - Comment if (A[i, j] = 0) A[i, j] = A[i, k] n A[k, j] - (mening?) i.e. there is pth from noe i to noe j IF there is pth from noe i to noe k AND pth from noe k to noe j t vrious stges in the lultion for k, i, j, the ifferent pths re isovere (1, 2, 2) - A[2,2] =A[2,1] n A[1,2] - i.e. 2 to 1 to 2 => 2 to 2 (1, 2, 3) - A[2,3] =A[2,1] n A[1,3] - i.e. 2 to 1 to 3 => 2 to 3 (2, 3, 1) - A[3,1] =A[3,2] n A[2,1] - i.e. 3 to 2 to 1 => 3 to 1 (2, 3, 3) - A[3,3] =A[3,2] n A[2,3] - i.e. 3 to 2 to 3 => 3 to 3 05/12/2016 DFR - DSA - Grphs 1 35

Summry irete grphs Definitions & implementtions Algorithms Dijkstr single noe shortest pth Dijkstr-SPT + shortest pth tree Floy ll pirs shortest pth Wrshll trnsitive losure is there pth from to? 05/12/2016 DFR - DSA - Grphs 1 36