Discussion #33 Adjacency Matrices. Topics Adjacency matrix for a directed graph Reachability Algorithmic Complexity and Correctness –Big Oh –Proofs of.

Slides:



Advertisements
Similar presentations
Algorithm Analysis Input size Time I1 T1 I2 T2 …
Advertisements

Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Chapter Matrices Matrix Arithmetic
8.3 Representing Relations Connection Matrices Let R be a relation from A = {a 1, a 2,..., a m } to B = {b 1, b 2,..., b n }. Definition: A n m  n connection.
Lecture 17 Path Algebra Matrix multiplication of adjacency matrices of directed graphs give important information about the graphs. Manipulating these.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
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.
Discussion #34 1/17 Discussion #34 Warshall’s and Floyd’s Algorithms.
 2004 SDU Lecture11- All-pairs shortest paths. Dynamic programming Comparing to divide-and-conquer 1.Both partition the problem into sub-problems 2.Divide-and-conquer.
Representing Relations Using Matrices
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Graph & BFS.
Discussion #36 Spanning Trees
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Introduction to Graphs
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
COMP171 Depth-First Search.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
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.
Adjacency Lists; Breadth-First Search & Depth-First Search.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Applied Discrete Mathematics Week 10: Equivalence Relations
Lecture 2 MAS 714 Hartmut Klauck
Dynamic Programming – Part 2 Introduction to Algorithms Dynamic Programming – Part 2 CSE 680 Prof. Roger Crawfis.
February 25, 2002Applied Discrete Mathematics Week 5: Mathematical Reasoning 1 Addition of Integers How do we (humans) add two integers? Example: 7583.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Reading and Writing Mathematical Proofs
Chapter 2 Graph Algorithms.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Data Strcutures.
Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation.
Targil 6 Notes This week: –Linear time Sort – continue: Radix Sort Some Cormen Questions –Sparse Matrix representation & usage. Bucket sort Counting sort.
Graph Definitions and Applications. Graphs: Very Useful Abstractions Graphs apply to many diverse areas: social sciences, linguistics, physical sciences,
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
1 Closures of Relations: Transitive Closure and Partitions Sections 8.4 and 8.5.
Data Structure Introduction.
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.
More on Correctness. Prime Factorization Problem: Write a program that computes all the prime factors of a given number Solution (Idea): Factors are less.
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
Matrices Section 2.6. Section Summary Definition of a Matrix Matrix Arithmetic Transposes and Powers of Arithmetic Zero-One matrices.
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
8.4 Closures of Relations Definition: The closure of a relation R with respect to property P is the relation obtained by adding the minimum number of.
CS 162 Intro to Programming II Insertion Sort 1. Assume the initial sequence a[0] a[1] … a[k] is already sorted k = 0 when the algorithm starts Insert.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Graphs. Graph Definitions A graph G is denoted by G = (V, E) where  V is the set of vertices or nodes of the graph  E is the set of edges or arcs connecting.
1 Closures of Relations Based on Aaron Bloomfield Modified by Longin Jan Latecki Rosen, Section 8.4.
Week 11 - Wednesday.  What did we talk about last time?  Graphs  Paths and circuits.
CS 285- Discrete Mathematics Lecture 11. Section 3.8 Matrices Introduction Matrix Arithmetic Transposes and Power of Matrices Zero – One Matrices Boolean.
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.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
Inductive Proof (the process of deriving generalities from particulars) Mathematical Induction (reasoning over the natural numbers)
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Mathematical Structures for Computer Science Chapter 6
Introduction to Graphs
CS202 - Fundamental Structures of Computer Science II
Graph Definitions and Applications
Basic Graph Algorithms
Data Structures & Algorithms Digraphs and DAGs
Discrete Mathematics for Computer Science
Directed Graphs (Part II)
Presentation transcript:

Discussion #33 Adjacency Matrices

Topics Adjacency matrix for a directed graph Reachability Algorithmic Complexity and Correctness –Big Oh –Proofs of correctness for algorithms Loop invariants Induction

Adjacency Matrix Definition: Let G = (V, E) be a simple digraph. Let V = {v 1, v 2,…v n } be the vertices (nodes). Order the vertices from v 1 to v n. The n  n matrix A whose elements are given by a ij = 1, if (v i, v j )  E 0, otherwise is the adjacency matrix of the graph G. Example: { v4v v3v v2v v1v1 v4v4 v3v3 v2v2 v1v1 A =

Space: Adjacency Lists vs. Matricies Space (n vertices and m edges) –matrix: n 2 + n  (vertex-node size) = matrix size + header size matrix can be bits, but bits are not directly addressable –list: n  (header-node size) + m  (list-node size) Sparse: few edges  0 in the extreme case –Matrix  fixed size: so no size benefit –List  variable size: as little as n  (vertex-node size) Dense: many edges  n 2 in the extreme case –Matrix  fixed size: so no size loss –List  variable size: as much as n  (header-node size) + n 2  (list-node size)

Operations: Adjacency Lists vs. Matricies Operations depend on sparse/dense and what’s being done. Examples (n nodes and m edges) –Is there an arc from x to y? Matrix: O(1)  check value at (x, y) List: O(n)  index to x, traverse list to y or end –Get successor nodes of a node. Matrix: O(n)  scan a row List: O(n)  traverse a linked list –Get predecessor nodes of a node. Matrix: O(n)  scan a column List: O(n+m)  traverse all linked lists, which could be as bad as O(n+n 2 ) = O(n 2 ).

Powers of adjacency matrices: A 2, A 3, … Can compute powers –Using ordinary arithmetic: –Using Boolean arithmetic: a ij =  k=1 n a ik  a kj Powers of Adjacency Matrices a ij =  k=1 n a ik  a kj

Powers Using Ordinary Arithmetic A = A 2 = A 3 =

Powers Using Ordinary Arithmetic (continued…) The element in the ith row and the jth column of A n is equal to the number of paths of length n from the ith node to the jth node A 4 =

Powers Using Ordinary Arithmetic (continued…) A = A 2 = A 3 = A 1 + A 2 = # of ways in 2 or fewer A 1 + A 2 + A 3 = # of ways in 3 or fewer A 1 + A 2 + A 3 + A 4 = # of ways in 4 or fewer = =

Powers Using Boolean Arithmetic Using Boolean arithmetic, we compute reachability. We can compute the paths of length 2 by A 2, 3 by A 3 … and of length n by A n. Since the longest possible path we care about for reachability is n, we obtain all paths by A  A 2  A 3  …  A n. The reachability matrix is the transitive closure of A. Algorithm: R = A; for (i = 2 to n) { compute A i ; R = R  A i ; } O(n 4 ) (n-1)  n  n  n for (j = 1 to n) { for (k = 1 to n) { a i jk = 0; for (m = 1 to n) { a i jk = a i jk  (a i-1 jm  a 1 mk ); }

Reachability A = A  A 2 = A  A 2  A 3 = A  A 2  A 3  A 4 = 1 st iteration 2 nd iteration 3 rd iteration

Timeout … for an Important CS Theme Programmatic problem solving –Understand  gather information, analyze, specify –Solve Choose “proper” data structures and algorithms Implement “Prove” correct Test  Prove exhaustively? mathematical proof? Reason & Test –Optimize Tweak? (Often not much here  compilers optimize) Can I reduce the Big Oh? Example –Can we prove that the reachability algorithm works? –Can we find a faster algorithm to compute reachability?

Algorithms: Proof of Correctness The algorithm terminates. –Loops terminate: each has a descending seq. on a well-founded poset. –Example: The algorithm produces the correct result. –No loops: Reason that steps lead to the desired conclusion. –Loops: Find an appropriate loop invariant  a T/F condition that stays the same as the loop executes, is based on the number of iterations, and leads to the conclusion we need. Prove by induction that the loop invariant holds as the loop executes. R = A; for (i = 2 to n) { compute A i ; R = R  A i ; } The sequence n  i for i = 2 to n is a descending sequence on the natural numbers. (For our example it is.)

Loop Invariant Example:  A[i] i=1 n sum = 0 for i = 1 to n sum = sum + A[i] Loop invariant: After k iterations, sum = A[1] + A[2] + … + A[k]. Induction proof: Basis: k = 0 (initialization, before the loop begins), sum = 0, which is the sum after 0 iterations. (Or, we could start with k=1: After 1 iteration, sum = A[1].) Induction: By the induction hypothesis, after k iterations, sum = A[1] + A[2] + … + A[k]. In the next iteration we add A[k+1]. Thus, after k+1 iterations, sum = A[1] + A[2] + … + A[k+1].

Loop Invariant Example: Selection Sort -- A is an array of n distinct integers for i = 1 to n find location j of the smallest integer in A[i] … A[n] swap A[i] and A[j] Loop invariant: After k iterations, A[1], …, A[k] are sorted  A[k] < all of A[k+1], …, A[n]. Induction proof: Basis: k = 0 (initialization, before the loop begins), A is an array of n distinct integers which may or may not be sorted. (Or, we could start with k=1: After 1 iteration A[1] is sorted  A[1] < all of A[1+1], …, A[n].) Induction: By the induction hypothesis, after k iterations, after k iterations, A[1], …, A[k] are sorted  A[k] < all of A[k+1], …, A[n]. Then since A[k] < all of A[k+1], …, A[n], and we find the smallest integer in A[k+1], …, A[n] to swap with A[k+1], A[1], …, A[k+1] are sorted  A[k+1] < all of A[(k+1)+1], …, A[n]

Proof of Correctness for Reachability Loop invariant: After z outer loops, a xy = 1 iff y is reachable from x along a path of length z+1 or less from x to y. Note: the loop terminates after n  1 outer loops since i runs from 2 to n. Thus, when the algorithm terminates, if the loop invariant holds, a xy = 1 iff y is reachable from x along a path of length (n  1)+1 = n or less from x to y, and thus is reachable since n is the longest a path can be (that starts at a node N, visits all other nodes once, and comes back to N). Inductive proof –Basis: z=0 (i.e. before entering the outer loop), R = A and a xy is 1 iff there is a path from x to y; thus a xy = 1 iff y is reachable from x along a path of length 0+1 = 1 or less from x to y. –Induction: By the induction hypothesis we have that after z outer loops, a xy = 1 iff y is reachable from x along a path of length z+1 or less from x to y. In the z+1 st iteration we compute A (z+2), in which a xy = 1 iff there is a path of length z+1 from x to some node q and an edge from q to y, i.e. a path of length z+2 from x to y, and we add these reachable possibilities for paths of length z+2 to the result R, making a xy = 1 if it is not already 1 because there exists a path from x to y that has length less than z+2. Hence, after z+1 outer loops, a xy = 1 iff y is reachable from x along a path of length (z+1)+1 = z+2 or less from x to y. R = A; for (i = 2 to n) { compute A i ; R = R  A i ; } for (j = 1 to n) { for (k = 1 to n) { a i jk = 0; for (m = 1 to n) { a i jk = a i jk  (a i-1 jm  a 1 mk ); }

Proof of Correctness for Reachability Basis: R = A Induction: After z outer loops, a xy = 1 iff y is reachable from x along a path of length z+1 or less from x to y.  After z+1 outer loops, a xy = 1 iff y is reachable from x along a path of length (z+1)+1 or less from x to y. R = A; for (i = 2 to n) { compute A i ; R = R  A i ; } R 0. A 1 1. A 1  A 2 … z. A 1  A 2  …  A z+1 z+1. A 1  A 2  …  A z+1  A z+2 … n-1. A 1  A 2  …  A n From the inner loops we know that a xy = 1 for a path of length i from x to y iff (x,y) is 1 in A i.