A Functional Graph Library

Similar documents
CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Data Structure [Question Bank]

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Converting a Number from Decimal to Binary

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Ordered Lists and Binary Trees

Sample Questions Csci 1112 A. Bellaachia

DATA STRUCTURES USING C

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Exam study sheet for CS2711. List of topics

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Analysis of a Search Algorithm

Algorithms and Data Structures

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

PES Institute of Technology-BSC QUESTION BANK

6 March Array Implementation of Binary Trees

Data Structures and Algorithms

Data Structures. Level 6 C Module Descriptor

Data Structures Fibonacci Heaps, Amortized Analysis

International Journal of Software and Web Sciences (IJSWS)

Any two nodes which are connected by an edge in a graph are called adjacent node.

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

5. Binary objects labeling

V. Adamchik 1. Graph Theory. Victor Adamchik. Fall of 2005

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Binary Heap Algorithms

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Full and Complete Binary Trees

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

10CS35: Data Structures Using C

Social Media Mining. Graph Essentials

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Symbol Tables. Introduction

Binary Search Trees CMPSC 122

CS711008Z Algorithm Design and Analysis

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Lecture Notes on Binary Search Trees

Analysis of Algorithms I: Binary Search Trees

Data Structures and Algorithms Written Examination

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Parallelization: Binary Tree Traversal

GRAPH THEORY LECTURE 4: TREES

Binary Heaps. CSE 373 Data Structures

recursion, O(n), linked lists 6/14

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

The Butterfly, Cube-Connected-Cycles and Benes Networks

Data Structures Using C++

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Persistent Data Structures and Planar Point Location

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Simplified External memory Algorithms for Planar DAGs. July 2004

The Goldberg Rao Algorithm for the Maximum Flow Problem

Algorithms and Data Structures

TREE BASIC TERMINOLOGIES

Lecture 15 An Arithmetic Circuit Lowerbound and Flows in Graphs

Big Data and Scripting. Part 4: Memory Hierarchies

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

GRAPH DATABASE SYSTEMS. h_da Prof. Dr. Uta Störl Big Data Technologies: Graph Database Systems - SoSe

6.852: Distributed Algorithms Fall, Class 2

CompSci-61B, Data Structures Final Exam

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

A Fast Algorithm For Finding Hamilton Cycles

Persistent Binary Search Trees

Analysis of Algorithms, I

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

File System Management

INTERSECTION OF LINE-SEGMENTS

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Lecture Notes on Binary Search Trees

ADTs,, Arrays, Linked Lists

Data storage Tree indexes

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Data Structures and Algorithms Lists

Partitioning under the hood in MySQL 5.5

Binary Trees (1) Outline and Required Reading: Binary Trees ( 6.3) Data Structures for Representing Trees ( 6.4)

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Connectivity and cuts

Section IV.1: Recursive Algorithms and Recursion Trees

Examination paper for MA0301 Elementær diskret matematikk

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Cpt S 223. School of EECS, WSU

Semantic Analysis: Types and Type Checking

Dynamic Programming. Lecture Overview Introduction

Lecture 1: Course overview, circuits, and formulas

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

How To Create A Tree From A Tree In Runtime (For A Tree)

Scalable Prefix Matching for Internet Packet Forwarding

Physical Data Organization

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Transcription:

A Functional Graph Library Based on Inductive Graphs and Functional Graph Algorithms by Martin Erwig Presentation by Christian Doczkal March 22 nd, 2006

Structure Motivation Inductive graph definition Implementation Binary search trees Version-tree implementation Algorithms I (DFS) Conclusions Algorithms II

Motivation Goals Find inductive model for graphs Provide efficient graph implementations that meet imperative time bounds Make functional languages suitable for teaching graph algorithms Increase overall acceptance of functional languages Benefits Inductive programming style gives clarity and elegance Inductive proofs over graph algorithms possible

Inductive graph definition type Node = Int type Adj b = [(b,node)] type Context a b = (Adj b, Node, a, Adj b) data Graph a b = Empty Context a b & Graph a b ([( down,2)],3,'c',[( up,1)]) & ([( right,1)],2,'b',[( left ),1]) & ([],1,'a',[]) & Empty

Inductive graph definition Fact 1 (Completeness): Each labeled multi-graph can be represented by a Graph term Fact 2 (Choice of Representation) For each graph g and each node v contained in g there exist p,l,s and g' such that (p,v,l,s) & g' denotes g.

Implementation Requirements Construction Empty Graph (Empty) Add context (&) Decomposition Test for Empty Graph (Empty-match) Extract arbitrary context (&-match) Extract specific context (&v -match) Definitions for time bounds G = (V,E): n:= V m := E c v := suc v pred v c :=max {v V /c v }

Binary search trees Graph is represented as pair (t,m) t = binary search tree of (node,(predecessor,label,successor)) m = highest node occurring in t Predecessors/successors stored as binary search trees Time bounds Node insertion: Node deletion: &/& v -match: } O c v log c log n O nlog 2 n

Array version tree Implementation for functional arrays Implementation Inward directed tree of (index, value) pairs Original Array is the root of the tree New versions inserted as children of the version they are derived from ( O 1 ) Every version is a pointer to some node in the tree Lookup follows tree structure terminating at root ( O u where u is the number of updates to the array)

Version-tree representation context array version tree root (v0) imperative cache array version 1 version 2 imperative cache array version 1.1 version 1.2 imperative cache array version 1.2.1

Version-tree optimizations Avoiding Node Deletion positive integer stamps for nodes and edges node deletion negate integer for that node adjacency ignores non matching stamps insertion negate again and increment stamp &-match, Empty-match and insertion k := V so Empty-match k=0 elem array stores partition of deleted and inserted nodes index array stores position of nodes in elem array &-match & elem[1] -match

ADT version-tree time bounds Test for Empty Graph (Empty-match) Extract arbitrary context (&-match) Extract specific context (& v -match) Add context (&) }O 1 O c v log c Multi threaded usage adds a factor u corresponding to number of previous updates

Algorithms I (DFS) Depth first search dfs :: [Node] > Graph a b > [Node] dfs [] g = [] dfs vs Empty = [] dfs (v:vs) (c & v g) = v : dfs (suc c ++ vs) g dfs (v:vs) g = dfs vs g Breadth first search: bfs (v:vs) (c & v g) = v : dfs (vs ++ suc c) g (or queue implementation for efficiency)

Conclusions Goals met? Code shows both clarity and elegance Same time complexity as imperative implementations Problems Double representation of edges and cache arrays cause a lot of memory overhead. time complexity met only on single threaded graph usage

Algorithms II DF Spanning Forest: data Tree a = Br a [Tree a] postorder (Br v ts) = concatmap postorder ts ++ v df :: [Node] > Graph a b > ([Tree Node], Graph a b) df [] g = ([],g) df (v:vs) (c & v g) = (Br v f:f',g2) where (f,g1) = df (suc c) g (f',g2) = df vs g1 df (v:vs) g = df vs g dff :: [Node] > Graph a b > [Tree Node] dff vs g = fst (df vs g)

Algorithms II Strongly connected groups: topsort :: Graph a b > [Node] topsort g = reverse.concatmap postorder.(dff (nodes g) g) scc :: Graph a b > [Tree Node] scc g = dff (topsort g) (grev g)

Algorithms II (Dijkstra) type Lnode a = (Node, a) type Lpath a = [Lnode a] type LRTree a = [Lpath a] instance Eq a => Eq (Lpath a) where ((_,x):_) == ((_,y):_) = x == y instance Ord a => Ord (Lpath a) where ((_,x):_) < ((_,y):_) = x < y getpath Node > LRTree a > Path getpath = reverse. map fst. first (\((w,_):_) > w == v) sssp :: Real b => Node > Node > Graph a b > Path sssp s t = getpath t. dijkstra (unitheap [(s,0)])

Algorithms II (Dijkstra) Dijkstra SSSP: expand :: Real b => b > LPath b > Context a b > [Heap(LPath b)] expand d p (_,_,_,s) = map(\(l,v) > unitheap((v,l+d):p)) s dijkstra :: Real b => Heap(LPath b) > Graph a b > LRTree b dijkstra h g isemptyheap h isempty g = [] dijkstra (p@((v,d):_) << h) (c & v g) = p:dijkstra (mergeall (h:expand d p c)) g dijkstra (_ << h) g = dijkstra h g