Putting Laziness to Work. Why use laziness Laziness has lots of interesting uses – Build cyclic structures. Finite representations of infinite data. –

Slides:



Advertisements
Similar presentations
Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
Advertisements

Representing Boolean Functions for Symbolic Model Checking Supratik Chakraborty IIT Bombay.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8.
Topological Sort and Hashing
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
Graph Searching CSE 373 Data Structures Lecture 20.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Rectilinear Pattern Recognition Dan J. Nardi Masters Thesis April 11, 2003.
CPSC 322, Lecture 9Slide 1 Search: Advanced Topics Computer Science cpsc322, Lecture 9 (Textbook Chpt 3.6) January, 23, 2009.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
Implementation of Graph Decomposition and Recursive Closures Graph Decomposition and Recursive Closures was published in 2003 by Professor Chen. The project.
Graphs CS-240/341. Uses for Graphs computer networks and routing airline flights geographic maps course prerequisite structures tasks for completing a.
CS2420: Lecture 37 Vladimir Kulyukin Computer Science Department Utah State University.
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.
Cse536 Functional Programming Lecture #14, Nov. 10, 2004 Programming with Streams –Infinite lists v.s. Streams –Normal order evaluation –Recursive streams.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
COMP171 Depth-First Search.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Depth-first search COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar to pre-order.
Depth-First Search Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
Abstract Interpretation (Cousot, Cousot 1977) also known as Data-Flow Analysis.
Search Related Algorithms. Graph Code Adjacency List Representation:
Graph Search Computing 2 COMP s1. P ROBLEMS ON G RAPHS What kinds of problems do we want to solve on/via graphs? Is there a simple path from A to.
Tree Searching Breadth First Search Dept First Search.
Data Flow Analysis Compiler Baojian Hua
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Chapter 2 Graph Algorithms.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Trees By Charl du Plessis. Contents Basic Terminology Basic Terminology Binary Search Trees Binary Search Trees Interval Trees Interval Trees Binary Indexed.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
CS 415 – A.I. Slide Set 5. Chapter 3 Structures and Strategies for State Space Search – Predicate Calculus: provides a means of describing objects and.
CSE373: Data Structures & Algorithms Lecture 10: Disjoint Sets and the Union-Find ADT Lauren Milne Spring 2015.
Advanced Functional Programming Tim Sheard 1 Lecture 14 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
MA/CSSE 473 Day 28 Dynamic Programming Binomial Coefficients Warshall's algorithm Student questions?
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
ADA: 9. Graph Search1 Objective o describe and compare depth-first and breadth- first graph searching, and look at the creation of spanning trees.
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 A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets and the Union-Find ADT Lauren Milne Summer 2015.
7. Graph Traversal Describe and compare depth-first and breadth-first graph searching, and look at the creation of spanning trees Contest Algorithms: 7.
Graph Connectivity This discussion concerns connected components of a graph. Previously, we discussed depth-first search (DFS) as a means of determining.
MA/CSSE 473 Day 30 B Trees Dynamic Programming Binomial Coefficients Warshall's algorithm No in-class quiz today Student questions?
Hashing & Hash Tables. Sets/Dictionaries Set - Our best efforts to date:
Graph Concepts and Algorithms Using LEDA By Caroline Moore and Carmen Frerichs (252a-at and 252a-ao) each graph in the presentation was created using gw_basic_graph_algorithms.
Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Graphs and Shortest Paths Using ADTs and generic programming.
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Hw. 6: Algorithm for finding strongly connected components. Original digraph as drawn in our book and in class: Preorder label : Postorder label Nodes:
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
CSE373: Data Structures & Algorithms Lecture 14: Topological Sort / Graph Traversals Dan Grossman Fall 2013.
11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?
Search Related Algorithms
Elementary Graph Algorithms
Algorithms: Design and Analysis
Presentation transcript:

Putting Laziness to Work

Why use laziness Laziness has lots of interesting uses – Build cyclic structures. Finite representations of infinite data. – Do less work, compute only those values demanded by the final result. – Build infinite intermediate data structures and actually materialize only those parts of the structure of interest. Search based solutions using enumerate then test. – Memoize or remember past results so that they don’t need to be recomputed

Cyclic structures cycles:: [Int] cycles = 1 : 2 : 3 : cycles 123

Cyclic Trees data Tree a = Tip a | Fork (Tree a) (Tree a) t2 = Fork (Fork (Tip 3) (Tip 4)) (Fork (Tip 9) t2) Fork Tip 3Tip 4Tip 9

Mutually Cyclic (t3,t4) = ( Fork (Fork (Tip 11) t3) t4, Fork (Tip 21) (Fork (Tip 33) t3) ) Fork Tip 11 Tip 21 Fork Tip 33

Prime numbers and infinite lists primes :: [Integer] primes = sieve [2..] where sieve (p:xs) = p : sieve [x | x<-xs, x `mod` p /= 0]

Dynamic Programming Consider the function fib :: Integer -> Integer fib 0 = 1 fib 1 = 1 fib n = fib (n-1) + fib (n-2) LazyDemos> :set +s LazyDemos> fib ( reductions, cells, 1 garbage collection) takes about 9 seconds on my machine!

Why does it take so long? Consider (fib 6) fib 6 fib 5 fib 4 fib 2 fib 1fib 0 fib 3 fib 2 fib 1fib 0 fib 1 fib 3 fib 2 fib 1fib 0 fib 1 fib 4 fib 2 fib 1fib 0 fib 3 fib 2 fib 1fib 0 fib 1

What if we could remember past results? Strategy – Create a data structure – Store the result for every (fib n) only if (fib n) is demanded. – If it is ever demanded again return the result in the data structure rather than re-compute it Laziness is crucial Constant time access is also crucial – Use of functional arrays

Lazy Arrays import Data.Array table = array (1,5) [(1,'a'),(2,'b'),(3,'c'),(5,'e'),(4,'d')] The array is created once Any size array can be created Slots cannot be over written Slots are initialized by the list Constant access time to value stored in every slot

Taming the duplication fib2 :: Integer -> Integer fib2 z = f z where table = array (0,z) [ (i, f i) | i <- range (0,z) ] f 0 = 1 f 1 = 1 f n = (table ! (n-1)) + (table ! (n-2)) LazyDemos> fib (4055 reductions, 5602 cells) Result is instantaneous on my machine

Can we abstract over this pattern? Can we write a memo function that memoizes another function. Allocates an array Initializes the array with calls to the function But, We need a way to intercept recursive calls

A fixpoint operator does the trick fix f = f (fix f) g fib 0 = 1 g fib 1 = 1 g fib n = fib (n-1) + fib (n-2) fib1 = fix g

Generalizing memo :: Ix a => (a,a) -> ((a -> b) -> a -> b) -> a -> b memo bounds g = f where arrayF = array bounds [ (n, g f n) | n <- range bounds ] f x = arrayF ! x fib3 n = memo (0,n) g n fact = memo (0,100) g where g fact n = if n==0 then 1 else n * fact (n-1)

Representing Graphs import ST import qualified Data.Array as A type Vertex = Int -- Representing graphs: type Table a = A.Array Vertex a type Graph = Table [Vertex] -- Array Int [Int] Index for each node Edges (out of) that index [2,3] 2[7,4] 3[5] 4[6,9,7] 5[8] 6[9] 7 8[10] 9 10[]

Functions on graphs type Vertex = Int type Edge = (Vertex,Vertex) vertices :: Graph -> [Vertex] indices :: Graph -> [Int] edges :: Graph -> [Edge]

Building Graphs buildG :: Bounds -> [Edge] -> Graph graph = buildG (1,10) [ (1, 2), (1, 6), (2, 3), (2, 5), (3, 1), (3, 4), (5, 4), (7, 8), (7, 10), (8, 6), (8, 9), (8, 10) ]

DFS and Forests data Tree a = Node a (Forest a) type Forest a = [Tree a] nodesTree (Node a f) ans = nodesForest f (a:ans) nodesForest [] ans = ans nodesForest (t : f) ans = nodesTree t (nodesForest f ans) Note how any tree can be spanned by a Forest. The Forest is not always unique

DFS The DFS algorithm finds a spanning forest for a graph, from a set of roots. dfs :: Graph -> [Vertex] -> Forest Vertex dfs g vs = prune (A.bounds g) (map (generate g) vs) generate :: Graph -> Vertex -> Tree Vertex generate g v = Node v (map (generate g) (g `aat` v)) Array indexing An infinite cyclic tree

Sets of nodes already visited import qualified Data.Array.ST as B type Set s = B.STArray s Vertex Bool mkEmpty :: Bounds -> ST s (Set s) mkEmpty bnds = newSTArray bnds False contains :: Set s -> Vertex -> ST s Bool contains m v = readSTArray m v include :: Set s -> Vertex -> ST s () include m v = writeSTArray m v True Mutable array

Pruning already visited paths prune :: Bounds -> Forest Vertex -> Forest Vertex prune bnds ts = runST (do { m <- mkEmpty bnds; chop m ts }) chop :: Set s -> Forest Vertex -> ST s (Forest Vertex) chop m [] = return [] chop m (Node v ts : us) do { visited <- contains m v ; if visited then chop m us else do { include m v ; as <- chop m ts ; bs <- chop m us ; return(Node v as : bs) }

Topological Sort postorder :: Tree a -> [a] postorder (Node a ts) = postorderF ts ++ [a] postorderF :: Forest a -> [a] postorderF ts = concat (map postorder ts) postOrd :: Graph -> [Vertex] postOrd = postorderF. Dff dff :: Graph -> Forest Vertex dff g = dfs g (vertices g)

A BC DE G H I J F A BC DE G H I J F (0,””) (1,A) (5,A) (2,CA) (3,ECA) (4,HECA) (6,BA) (7,DBA) (8,GDBA) (9,DBA) A. Control Flow Graph a :: Graph Char a B. DFS Labeled Graph b :: Graph Char (Int,[Char]) dfsnum :: v->Int dfsnum v = fst(apply b v) dfspath:: v->[v] dfspath v = snd(apply b v) A BC DE G H I J F (A)(A) (A)(A) (A)(A) (C)(C) (C)(C) (A)(A) (B)(B) (B)(B) (D)(D) (D)(D) C. Semi-Dominator Labeled Graph c :: Graph Char Char semi :: v->v semi = apply c A BC D GE J H FI D. Semi-Dominator Graph d :: Graph Char a A BC D GE J H F I E. Dominator Graph e :: Graph Char a dfslabel f map g induce