Discrete Math for CS Chapter 8: Directed Graphs. Discrete Math for CS digraph: A digraph is a graph G = (V,E) where V is a finite set of vertices and.

Slides:



Advertisements
Similar presentations
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine.
Advertisements

CS1022 Computer Programming & Principles
CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
Discussion #33 Adjacency Matrices. Topics Adjacency matrix for a directed graph Reachability Algorithmic Complexity and Correctness –Big Oh –Proofs of.
Lecture 17 Path Algebra Matrix multiplication of adjacency matrices of directed graphs give important information about the graphs. Manipulating these.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
Applied Discrete Mathematics Week 12: Trees
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Introduction to Graphs
Graph Algorithms: Part 1
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
TCOM 501: Networking Theory & Fundamentals
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Directed graphs Definition. A directed graph (or digraph) is a pair (V, E), where V is a finite non-empty set of vertices, and E is a set of ordered pairs.
Operations Research Assistant Professor Dr. Sana’a Wafa Al-Sayegh 2 nd Semester ITGD4207 University of Palestine.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Network Optimization Chapter 3 Shortest Path Problems.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Chapter 2 Graph Algorithms.
Based on slides by Y. Peng University of Maryland
1 Closures of Relations: Transitive Closure and Partitions Sections 8.4 and 8.5.
Introduction to Algorithms Jiafen Liu Sept
The all-pairs shortest path problem (APSP) input: a directed graph G = (V, E) with edge weights goal: find a minimum weight (shortest) path between every.
© 2004 Goodrich, Tamasia Recall: Digraphs A digraph is a graph whose edges are all directed Short for “directed graph” Applications one-way streets flights.
Chapter 10 Graph Theory Eulerian Cycle and the property of graph theory 10.3 The important property of graph theory and its representation 10.4.
COSC 2007 Data Structures II Chapter 14 Graphs I.
Chapter 5 Graphs  the puzzle of the seven bridge in the Königsberg,  on the Pregel.
Vocabulary and Representations of Graphs. NC Standard Course of Study Competency Goal 1: The learner will use matrices and graphs to model relationships.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
Representing Relations Using Matrices A relation between finite sets can be represented using a zero-one matrix Suppose R is a relation from A = {a 1,
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Chapter 11 - Graph CSNB 143 Discrete Mathematical Structures.
Chapter 9: Graphs.
1 Closures of Relations Based on Aaron Bloomfield Modified by Longin Jan Latecki Rosen, Section 8.4.
GRAPH THEORY Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS )
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
رياضيات متقطعة لعلوم الحاسب MATH 226. Chapter 10.
Shortest Path -Prim’s -Djikstra’s. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected.
Aaron Bloomfield CS 202 Rosen, section 7.4
Graphs Lecture 19 CS2110 – Spring 2013.
Basic Concepts Graphs For more notes and topics visit:
COMP108 Algorithmic Foundations Greedy methods
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Shortest Path Graph represents highway system Edges have weights
All-Pairs Shortest Paths (26.0/25)
Discrete Maths 9. Graphs Objective
Graphs Lecture 18 CS2110 – Fall 2009.
Graphs Chapter 13.
Directed Graphs (Part II)
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
CS223 Advanced Data Structures and Algorithms
Chapter 9: Graphs Basic Concepts
Shortest path algorithm
Graph Algorithms DS.GR.1 Chapter 9 Overview Representation
Closures of Relations Epp, section 10.1,10.2 CS 202.
Chapter 9: Graphs Basic Concepts
Directed Graphs (Part II)
Presentation transcript:

Discrete Math for CS Chapter 8: Directed Graphs

Discrete Math for CS digraph: A digraph is a graph G = (V,E) where V is a finite set of vertices and E is a relation on V. If a, b ∈ V and aEb then there is a directed edge of E from a to b called an arc. If u, v ∈ V and (u,v) is an arc we write uv as the arc name. A simple digraph has no loops and no multiple edges. If uv is an arc then u is the antecedent of v.

Discrete Math for CS Example: ab dc V = { a, b, c, d } E = { ab, cb, dc, bd, db } a b c d a b c d

Discrete Math for CS More Notation: A path of length k in a digraph is a sequence of distinct vertices v 1, v 2, v 3,..., v k where v i-1 v i is an arc for i = 1,... k. A cycle is a path where v 1 = v k and no other vertices are the same. A graph without cycles is called acyclic. Directed acyclic graphs (DAGs) are some of the most important graphs. In task-scheduling problems a DAG is called a PERT chart.

Discrete Math for CS Example: A student needs to take 8 courses to satisfy a major. The courses and their prerequisites are given below. Draw a PERT chart showing the order in which the courses can be taken.

Discrete Math for CS Answer: A B C DE F G H

Discrete Math for CS Topological Sort Algorithm A topological sort algorithm produces a consistent labeling of the edges of the above graph. A labeling 1, 2, 3,..., n is consisent if uv is an arc, u has label i, v has label j and i < j.

Discrete Math for CS TSA: G = (V,E) is a digraph. Let A(v) = { all antecedents of v }. begin for v V do calculate A(v); label := 0; while unlabeled vertices v remain for which A(v) = ∅ do begin label := label + 1; u := a vertex with A(u) = ∅ ; assign label to u; for each unlabeled vertex v ∈ V do a(v) := A(v) \ {u} end

Discrete Math for CS Example: Find a consistent labeling of the previous graph. Step 0: A(A) = {B}, A(B) = {C}, A(C) = {H}, A(D) = {C}, A(E) = {D, G}, A(F) = {E}, A(G) = {C}, A(H) = ∅. Step 1: Assign label 1 to H since A(H) = ∅. A(A) = {B}, A(B) = {C}, A(C) = ∅, A(D) = {C}, A(E) = {D, G}, A(F) = {E}, A(G) = {C}, A(H) = ∅. Step 2: Assign label 2 to C since A(C) = ∅. A(A) = {B}, A(B) = ∅, A(C) = ∅, A(D) = ∅, A(E) = {D, G}, A(F) = {E}, A(G) = ∅, A(H) = ∅. Step 3: Choose one of the possibilities> Assign label 3 to B. A(A) = ∅, A(B) = ∅, A(C) = ∅, A(D) = ∅, A(E) = {D, G}, A(F) = {E}, A(G) = ∅, A(H) = ∅. Step 4: Assign label 4 to A. A(A) = ∅, A(B) = ∅, A(C) = ∅, A(D) = ∅, A(E) = {D, G}, A(F) = {E}, A(G) = ∅, A(H) = ∅. Step 5: Assign label 5 to D. A(A) = ∅, A(B) = ∅, A(C) = ∅, A(D) = ∅, A(E) = {G}, A(F) = {E}, A(G) = ∅, A(H) = ∅. Step 6: Assign label 6 to G. A(A) = ∅, A(B) = ∅, A(C) = ∅, A(D) = ∅, A(E) = ∅, A(F) = {E}, A(G) = ∅, A(H) = ∅. Step 7: Assign label 7 to E. A(A) = ∅, A(B) = ∅, A(C) = ∅, A(D) = ∅, A(E) = ∅, A(F) = ∅, A(G) = ∅, A(H) = ∅. Step 8: Assign label 8 to F. Consistent Labeling is H, C, B, A, D, G, E, F

Discrete Math for CS Paths in Digraphs Directed paths can represent things line airline routes or networked computers. We may need to know alternative routes if a link goes down. So we need to know if there exists a path between two vertices of a digraph. G = (V,E) with n vertices. Let M be the adjacency matrix. If m ij == 1 then there is an arc from vertex v i to vertex v j. An arc is a path of length 1. Consider M 2. The boolean product of M by M yields a matrix which shows paths of length 2 in the original graph.

Discrete Math for CS Example: ab dc V = { a, b, c, d } E = { ab, cb, dc, bd, db } a b c d a b c d a b c d a b c d M M2M2

Discrete Math for CS Reachability Matrix Calculate M* = M or M 2 or M 3 or... or M n M* contains all paths of all lengths so shows what vertices are reachable from what vertices. The reachability matrix of a graph is the graph of the transitive closure of its adjacency matrix.

Discrete Math for CS Example: ab dc a b c d a b c d a b c d a b c d M M2M2 a b c d a b c d M3M3 a b c d a b c d M4M4

Discrete Math for CS Example: ab dc a b c d a b c d M*

Discrete Math for CS Large Matrices This calculation is labourious for big matrices. Warshall's Algorithm calculates M* more efficiently. G = (V,E). with vertices v 1, v 2,..., v n. Warshall's Algorithm generates matrices W 0 = M, W 1, W 2,..., W n. For k >= 1, W k (i,j) = 1 iff there is a path of any length from v i to v j where the intermediary vertices in the path lie in the set {v 1,... v k }. W n = M*. Warshall's Algorithm is efficient by a clever use of for-loops. Successive passes of the outer loop calculate W 1, W 2,..., W n.

Discrete Math for CS Warshall's Algorithm G = (V,E). M is the adjacency matrix. Calculates W = M*. begin W := M; for k = 1 to n do for i = 1 to n do for j = 1 to n do W(i,j) = W(i,j) or W(i,k) and W(k,j); end Note: On each pass of outer loop the algorithm generates W k. This is done by updating entries in W k-1. To find i th row of W k we evaluate W(i,j) = W(i,j) or W(i,k) and W(k,j); (*)‏ for various values of j. If W(i,k) = 0 then (W(i,k) and W(k,j)) = 0 and so (*) reduces to W(i,j); ie, row i of the matrix remains unchanged. Otherwise W(i,k) = 1 and (*) reduces to W(i,j) or W(k,j). In this case row i becomes the logical or of the current row i and current row k.

Discrete Math for CS Warshall's Algorithm: So Warshall's Algorithm reduces to calculating W k from W k-1 as follows:  Consider the k th column of W k.  For each row with a 0 entry in this column, copy the row from W k-1.  For each row with a 1 entry form the logical or of that row with row k and write the resulting row in W k.

Discrete Math for CS Example (Warshall's Algorithm): W0W W1W1 copy rows 1,2, W1W1 row 3 or row W1W1 row 5 or row 1

Discrete Math for CS Example (Warshall's Algorithm): W1W W2W W2W2

Discrete Math for CS Example (Warshall's Algorithm): W2W W3W W3W3 Note: W 4 = W 3 so we are done.

Discrete Math for CS Shortest Paths Find the shortest path between two vertices in a weighted digraph. Typical situations – transportation networks, communications networks

Discrete Math for CS Shortest Paths: A BC F E D Find the shortest path from A to any other vertex. weight matrix: w(u,v) = 0 if u = v ∞ if uv is not an arc d if uv is an arc of weight d A B C D E F A 0 2 ∞ 3 ∞ ∞ B ∞ 0 1 ∞ 4 ∞ C ∞ ∞ 0 ∞ ∞ 5 D ∞ ∞ ∞ 0 2 ∞ E ∞ ∞ ∞ ∞ 0 1 F ∞ ∞ ∞ ∞ ∞ O

Discrete Math for CS Idea: Initially define d[v] to be the weight of an arc from A to v. d[v] = ∞ if there is no arc. We traverse the vertices and improve d[v] as we go. We mark a value for d[u] once we know for sure the shortest route to u from A. For the remaining vertices, w, we assign the min of the current value of d[w] and the distance to w via the last marked vertex, u. The algorithm terminates once all vertices that can be marked are marked.

Discrete Math for CS Step 0: Mark A and let the first row represent the initial values of d[v]. Step 1: Mark B since it is closest to A. Calculate the distances to unmarked vertices via B. If a shorter distance is found, use it.. Vertices not adjacent to the last marked have their d[v] values unchanged. Step 2: Next mark D (we could mark C too). Calculate the remaining distances Step 3: Mark C. F can now be accessed. Step 4 and 5: Mark E and F.

Discrete Math for CS Dijkstra's Algorithm G = (V,E) is a weighted digraph, A is a vertex. The algorithm finds the shortest path from A to v as well as d[v].  w(u,v) is the weight of arc uv  PATHTO(v) lists the vertices on the shortest path to v from A. begin for each v in V do begin d[v] = w(A,v); PATHTO(v) := A; end Mark vertex A while unmarked vertices remain do begin u:= unmarked vertex closest to A Mark u; end for each unmarked vertex v with uv in E do begin d' := d[u] + w(u,v); if d' < d[v] then d[v] := v'; PATHTO(v) := PATHTO(u), v end

Discrete Math for CS Exercise: Use Dijkstra's Algorithm with the following graph: BE F D A C

Discrete Math for CS Answer

Discrete Math for CS