Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.

Slides:



Advertisements
Similar presentations
Chapter 5: Tree Constructions
Advertisements

Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
Lecture 15. Graph Algorithms
Advanced Topics in Algorithms and Data Structures
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8.
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
CSE 373, Copyright S. Tanimoto, 2001 Graphs Graphs 2 Incidence and Adjacency Representing a graph with an adjacency matrix, an incidence matrix,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Graph Searching CSE 373 Data Structures Lecture 20.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
GRAPHS AND GRAPH TRAVERSALS 9/26/2000 COMP 6/4030 ALGORITHMS.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Graphs CS3240, L. grewe.
CS 261 – Recitation 9 & 10 Graphs & Final review
Transform & Conquer Lecture 08 ITS033 – Programming & Algorithms
Jeffrey D. Ullman Stanford University Flow Graph Theory.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Putting Laziness to Work. Why use laziness Laziness has lots of interesting uses – Build cyclic structures. Finite representations of infinite data. –
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. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
Testing for Connectedness and Cycles
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
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.
Graph Traversals CSC 172 SPRING 2002 LECTURE 26. Traversing graphs Depth-First Search like a post-order traversal of a tree Breath-First Search Less like.
מבנה המחשב – מבוא למחשבים ספרתיים Foundations of Combinational Circuits תרגול מספר 3.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT TDDB56 – DALGOPT-D Algorithms and optimization Lecture 11 Graphs.
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.
Busby, Dodge, Fleming, and Negrusa. Backtracking Algorithm Is used to solve problems for which a sequence of objects is to be selected from a set such.
Search Related Algorithms. Graph Code Adjacency List Representation:
Tree Searching Breadth First Search Dept First Search.
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.
1 Section 1.4 Graphs and Trees A graph is set of objects called vertices or nodes where some pairs of objects may be connected by edges. (A directed graph.
Advanced Functional Programming Tim Sheard 1 Lecture 14 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Chap 8 Trees Def 1: A tree is a connected,undirected, graph with no simple circuits. Ex1. Theorem1: An undirected graph is a tree if and only if there.
CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
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.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
Graph Connectivity This discussion concerns connected components of a graph. Previously, we discussed depth-first search (DFS) as a means of determining.
Strongly Connected Components for Directed Graphs Kelley Louie Credits: graphs by /demo/graphwin/graphwin.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Hw. 6: Algorithm for finding strongly connected components. Original digraph as drawn in our book and in class: Preorder label : Postorder label Nodes:
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
CSC 172 DATA STRUCTURES.
Tracing An Algorithm for Strongly Connected Components that uses Depth First Search Graph obtained from Text, page a-al: Geetika Tewari.
Graph Algorithms Using Depth First Search
More Graph Algorithms.
Graphs Chapter 13.
Advanced Algorithms Analysis and Design
Search Related Algorithms
Backtracking.
Graph Search in C++ Andrew Lindsay.
Presentation transcript:

Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture 17: Erwig style - Functional Graphs DFS – style of depth first search

Advanced Functional Programming Tim Sheard 2 Lecture 17 References The style of representing graphs, and most of the examples, come from the work of Martin Erwig Functional Programming with Graphs 2nd ACM SIGPLAN Int. Conf. on Functional Programming (ICFP'97), 52-65, 1997(ICFP'97) Inductive Graphs and Functional Graph AlgorithmsInductive Graphs and Functional Graph Algorithms, Martin Erwig Journal of Functional Programming,

Advanced Functional Programming Tim Sheard 3 Lecture 17 Graphs Nodes = 1,2,3 Edges = (1,2),(2,1),(3,1),(2,3) Node Labels = a,b,c Edge Labels = right,left,up,down 1 a c b 2 3 right left down up

Advanced Functional Programming Tim Sheard 4 Lecture 17 Types type Node = Int type LNode a = (Node,a) type UNode = LNode () type Edge = (Node,Node) type LEdge b = (Node,Node,b) type UEdge = LEdge ()

Advanced Functional Programming Tim Sheard 5 Lecture 17 More Types data Graph a b -- Abstract Type --unlabled Graph type UGraph = Graph () () type Path = [Node] type LPath a = [LNode a] type UPath = [UNode]

Advanced Functional Programming Tim Sheard 6 Lecture 17 Making Graphs ex1 = mkGraph [(1,"a"),(2,"b"),(3,"c")] [(1,2,"right"),(2,1,"left"), (2,3,"down"),(3,1,"up")] Main> ex1 1:"a"->[("right",2)] 2:"b"->[("left",1),("down",3)] 3:"c"->[("up",1)] 1 a c b 2 3 right left down up

Advanced Functional Programming Tim Sheard 7 Lecture 17 Looking inside noNodes :: Graph a b -> Int nodes :: Graph a b -> [Node] labNodes :: Graph a b -> [LNode a] -- [(Node,a)] edges :: Graph a b -> [Edge] --[(Node,Node)] labEdges :: Graph a b -> [LEdge b] -– [(Node,Node,b)]

Advanced Functional Programming Tim Sheard 8 Lecture 17 Examples Main> noNodes ex1 3 Main> nodes ex1 [1,2,3] Main> labEdges ex1 [(1,2,"right"),(2,1,"left"), (2,3,"down"),(3,1,"up")] 1 a c b 2 3 right left down up

Advanced Functional Programming Tim Sheard 9 Lecture 17 Decomposing Graphs type Adj b = [(b,Node)] type Context a b = (Adj b,Node,a,Adj b) type MContext a b = Maybe (Context a b) type Decomp a b = (MContext a b,Graph a b) type GDecomp a b = (Context a b,Graph a b) type UContext = ([Node],Node,[Node]) type UDecomp = (Maybe UContext,UGraph) match :: Node -> Graph a b -> Decomp a b edge label Its predecessors Its succesors The node Its label

Advanced Functional Programming Tim Sheard 10 Lecture 17 (Just(ps,n,lab,ss),ex2) = match 2 ex1 Main> ps [("right",1)] --predecessors Main> ss --successors [("left",1),("down",3)] Main> n –- the node 2 Main> lab -- its label "b" Main> ex2 1:"a"->[] 3:"c"->[("up",1)] 1 a c b 2 3 right left down up

Advanced Functional Programming Tim Sheard 11 Lecture 17 (Just(ps2,n2,lab2,ss2),ex3) = match 3 ex2 Main> ps2 [] Main> n2 3 Main> lab2 "c" Main> ss2 [("up",1)] Main> ex3 1:"a"->[] 1 a c 3 up

Advanced Functional Programming Tim Sheard 12 Lecture 17 Constructing Graphs Inductively empty :: Graph a b embed :: Context a b -> Graph a b -> Graph a b infixr & c & g = embed c g ex4 = (ps2,n2,lab2,ss2) & ex3 Main> ex4 1:"a"->[] 3:"c"->[("up",1)] 1 a c 3 up

Advanced Functional Programming Tim Sheard 13 Lecture 17 Operations on graphs -- maps on nodes nmap :: (a->c) -> Graph a b -> Graph c b -- maps on edges emap :: (b->c) -> Graph a b -> Graph a c -- graph reversal grev :: Graph a b -> Graph a b

Advanced Functional Programming Tim Sheard 14 Lecture 17 Example ex5 = nmap (ord. head) ex1 Main> ex5 1:97->[("right",2)] 2:98->[("down",3),("left",1)] 3:99->[("up",1)] Main> ex1 1:"a"->[("right",2)] 2:"b"->[("left",1),("down",3)] 3:"c"->[("up",1)]

Advanced Functional Programming Tim Sheard 15 Lecture 17 Example 2 ex6 = emap length ex1 Main> ex6 1:"a"->[(5,2)] 2:"b"->[(4,3),(4,1)] 3:"c"->[(2,1)] Main> ex1 1:"a"->[("right",2)] 2:"b"->[("left",1),("down",3)] 3:"c"->[("up",1)]

Advanced Functional Programming Tim Sheard 16 Lecture 17 Roll your own mymap f g | isEmpty g = empty | otherwise = let ns = nodes g (Just(ps,n,lab,ss),g2) = match (head ns) g in (ps,n,f lab,ss) & (mymap f g2)

Advanced Functional Programming Tim Sheard 17 Lecture 17 Depth First Search data Tree a = Branch a [Tree a] deriving Show df :: Node -> Graph b a -> (Tree b,Graph b a) df root g = let (Just(_,v,lab,ss), g') = match root g (r,g1) = dff ss g' in (Branch lab r,g1) dff :: [(c,Node)] -> Graph b c -> ([Tree b],Graph b c) dff [] g = ([],g) dff ((lab,v):l) g = let (x,g1) = df v g (y,g2) = dff l g1 in (x:y,g2)

Advanced Functional Programming Tim Sheard 18 Lecture 17 Example ex7 = fst (df 1 ex1) Main> ex7 Branch "c" [Branch "b" [Branch "a" []]]

Advanced Functional Programming Tim Sheard 19 Lecture 17 Launchbury & King “Structuring Depth-First Search Algorithms in Haskell” David King and John Launchbury - POPL ’95 – pp Graph algorithms based upon Depth First Search. Asymtotically efficient – uses lazy state

Advanced Functional Programming Tim Sheard 20 Lecture 17 Representing Graphs import ST import qualified 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[]

Advanced Functional Programming Tim Sheard 21 Lecture 17 Functions on graphs type Vertex = Int type Edge = (Vertex,Vertex) vertices :: Graph -> [Vertex] indices :: Graph -> [Int] edges :: Graph -> [Edge]

Advanced Functional Programming Tim Sheard 22 Lecture 17 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) ]

Advanced Functional Programming Tim Sheard 23 Lecture 17 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

Advanced Functional Programming Tim Sheard 24 Lecture 17 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))

Advanced Functional Programming Tim Sheard 25 Lecture 17 Sets of nodes already visited type Set s = 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

Advanced Functional Programming Tim Sheard 26 Lecture 17 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) }

Advanced Functional Programming Tim Sheard 27 Lecture 17 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)