Bratko, Chapter 9: Operations on Data Structures 9.1 Representing and sorting lists - lists are merely structures - not required to stick with []/nil notation - bubblesort (p213) - note use of cut, i.e., swap in only one way - insertsort (p214) - quicksort (p215, fig9.2) - quicksort with list difference-pair representation (p216, fig9.3) 9.2 Representing sets by binary trees - binary tree - t( Left, Root, Right) - 'nil' indicates empty tree - inefficient searches for elements in lower right or not in tree - binary dictionary (binary search tree) - for every tree and subtree t(L,X,R), elements(L) < X < elements(R) - in( X, Tree) (p221, fig9.7) 9.3 Insertion and deletion in binary dictionary - addleaf( Tree, X, NewTree) [p224, fig9.10] - del( Tree, X, NewTree) [p226, fig9.13] - add(Tree,X,NewTree) / addroot(Tree,X,NewTree) [p228, fig9.15] - nondeterministic binary dictionary add - deletion doesn't work 9.4 Displaying trees - show(Tree) [p230, fig9.17] 9.5 Graphs - representation - facts - connected(Node1,Node2) % undirected - arc(Node1,Node2,Label) % directed, labeled - structures: graph(Node,Edges), digraph(Nodes,Edges) - graph([a,b,c],[e(a,b),e(b,c)]) - digraph([s,t,u],[a(s,t,5),a(t,u,10)]) - adjacency list - G1 = [ a -> [b], b -> [a,c], c -> [b] ] - G2 = [ s -> [t/5], t -> [s/3, u/10] ] - choice depends on application - finding a path - acyclic paths [p234, fig9.20] - hamiltonian paths [p234] - not(node(N,Graph),not member(N,Path) - acyclic paths with costs [p235, fig9.21] - minimum cost path [p235] - path(...Cost...), not (path(...Cost2...), Cost2 < Cost). - finding a spanning tree - algorithmic program [p237, fig9.22] - add one edge at a time - declarative program [p238, fig9.23] - try subsets of edges, inefficient - includes power set code !!! Examples - write a Prolog program for interactive binary dictionary manipulation - define 'less than' for atoms Other Procedures - load_files(List) - @<, @>, @=, compare(Order,Term1,Term2) - format(Control,ArgList) - C-like HW - Exercise 9.15, page 238