1 / 70

Part 1 The Prolog Language Chapter 9 Operations on Data Structure

Part 1 The Prolog Language Chapter 9 Operations on Data Structure. 9.1 Sorting lists. A list can be sorted if there is an ordering relation between the items in the list. Assume that there is an ordering relation gt( X, Y) meaning the X is greater than Y.

nemesio
Télécharger la présentation

Part 1 The Prolog Language Chapter 9 Operations on Data Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Part 1 The Prolog LanguageChapter 9 Operations on Data Structure

  2. 9.1 Sorting lists • A list can be sorted if there is an ordering relation between the items in the list. • Assume that there is an ordering relation gt( X, Y) meaning the X is greater than Y. • If our items are numbers then the gt relation is defined as: gt( X, Y) :- X > Y. • If the items are atoms then we can define: gt( X, Y) :- X @> Y. • Remember that this relation also orders compound terms.

  3. 9.1 Sorting lists • Let sort( List, Sorted) denote a relation where List is a list of items and Sorted is a list of the same items sorted in the ascending order according to the gt relation.

  4. 9.1 Sorting lists • To sort a list, List: • Find two adjacent elements, X and Y, in List such that gt(X, Y) and swap X and Y in List, obtaining List1; then sort List1. • If there is no pair of adjacent elements, X and Y, in List such that gt(X, Y), then List is already sorted. • Bubble sort: bubblesort( List, Sorted) :- swap( List, List1), !, bubblesort( List1, Sorted). bubblesort( Sorted, Sorted). swap([X, Y| Rest], [Y, X| Rest]) :- gt( X, Y). swap([Z|Rest],[Z|Rest1]) :- swap( Rest, Rest1).

  5. | ?- bubblesort([3,5,2,4], L). 1 1 Call: bubblesort([3,5,2,4],_24) ? 2 2 Call: swap([3,5,2,4],_93) ? 3 3 Call: gt(3,5) ? 4 4 Call: 3>5 ? 4 4 Fail: 3>5 ? 3 3 Fail: gt(3,5) ? 3 3 Call: swap([5,2,4],_80) ? 4 4 Call: gt(5,2) ? 5 5 Call: 5>2 ? 5 5 Exit: 5>2 ? 4 4 Exit: gt(5,2) ? 3 3 Exit: swap([5,2,4],[2,5,4]) ? 2 2 Exit: swap([3,5,2,4],[3,2,5,4]) ? 6 2 Call: bubblesort([3,2,5,4],_24) ? 7 3 Call: swap([3,2,5,4],_223) ? 8 4 Call: gt(3,2) ? 9 5 Call: 3>2 ? 9 5 Exit: 3>2 ? 8 4 Exit: gt(3,2) ? 7 3 Exit: swap([3,2,5,4],[2,3,5,4]) ? 10 3 Call: bubblesort([2,3,5,4],_24) ? 11 4 Call: swap([2,3,5,4],_326) ? 12 5 Call: gt(2,3) ? 13 6 Call: 2>3 ? 13 6 Fail: 2>3 ? 12 5 Fail: gt(2,3) ? 12 5 Call: swap([3,5,4],_313) ? 13 6 Call: gt(3,5) ? 14 7 Call: 3>5 ? 14 7 Fail: 3>5 ? 13 6 Fail: gt(3,5) ? 13 6 Call: swap([5,4],_339) ? 14 7 Call: gt(5,4) ? 15 8 Call: 5>4 ? 15 8 Exit: 5>4 ? 14 7 Exit: gt(5,4) ? 13 6 Exit: swap([5,4],[4,5]) ? 12 5 Exit: swap([3,5,4],[3,4,5]) ? 11 4 Exit: swap([2,3,5,4],[2,3,4,5]) ? 16 4 Call: bubblesort([2,3,4,5],_24) ? 17 5 Call: swap([2,3,4,5],_483) ? 18 6 Call: gt(2,3) ? 19 7 Call: 2>3 ? 19 7 Fail: 2>3 ? 18 6 Fail: gt(2,3) ? 18 6 Call: swap([3,4,5],_470) ? 19 7 Call: gt(3,4) ? 20 8 Call: 3>4 ? 20 8 Fail: 3>4 ? 19 7 Fail: gt(3,4) ? 19 7 Call: swap([4,5],_496) ? 20 8 Call: gt(4,5) ? 21 9 Call: 4>5 ? 21 9 Fail: 4>5 ? 20 8 Fail: gt(4,5) ? 20 8 Call: swap([5],_522) ? 21 9 Call: swap([],_548) ? 21 9 Fail: swap([],_548) ? 20 8 Fail: swap([5],_522) ? 19 7 Fail: swap([4,5],_496) ? 18 6 Fail: swap([3,4,5],_470) ? 17 5 Fail: swap([2,3,4,5],_471) ? 16 4 Exit: bubblesort([2,3,4,5],[2,3,4,5]) ? 10 3 Exit: bubblesort([2,3,5,4],[2,3,4,5]) ? 6 2 Exit: bubblesort([3,2,5,4],[2,3,4,5]) ? 1 1 Exit: bubblesort([3,5,2,4],[2,3,4,5]) ? L = [2,3,4,5] (94 ms) yes {trace} 9.1 Sorting lists

  6. 9.1 Sorting lists • To sort a non-empty list, L = [X|T]: • Sort the tail T of L. • Insert the head, X, of L into the sorted tail at such a position that the resulting list is sorted. The result is the whole sorted list. • Insertion sort: insertsort( [], []). insertsort( [X|Tail], Sorted) :- insertsort( Tail, SortedTail), insert( X, SortedTail, Sorted). insert(X, [Y| Sorted], [Y| Sorted1]) :- gt( X, Y), !, insert( X, Sorted, Sorted1). insert(X, Sorted, [X|Sorted]).

  7. | ?- insertsort([3,5,2,4], L). 1 1 Call: insertsort([3,5,2,4],_24) ? 2 2 Call: insertsort([5,2,4],_93) ? 3 3 Call: insertsort([2,4],_117) ? 4 4 Call: insertsort([4],_141) ? 5 5 Call: insertsort([],_165) ? 5 5 Exit: insertsort([],[]) ? 6 5 Call: insert(4,[],_191) ? 6 5 Exit: insert(4,[],[4]) ? 4 4 Exit: insertsort([4],[4]) ? 7 4 Call: insert(2,[4],_220) ? 8 5 Call: gt(2,4) ? 9 6 Call: 2>4 ? 9 6 Fail: 2>4 ? 8 5 Fail: gt(2,4) ? 7 4 Exit: insert(2,[4],[2,4]) ? 3 3 Exit: insertsort([2,4],[2,4]) ? 8 3 Call: insert(5,[2,4],_249) ? 9 4 Call: gt(5,2) ? 10 5 Call: 5>2 ? 10 5 Exit: 5>2 ? 9 4 Exit: gt(5,2) ? 11 4 Call: insert(5,[4],_236) ? 12 5 Call: gt(5,4) ? 13 6 Call: 5>4 ? 13 6 Exit: 5>4 ? 12 5 Exit: gt(5,4) ? 14 5 Call: insert(5,[],_313) ? 14 5 Exit: insert(5,[],[5]) ? 11 4 Exit: insert(5,[4],[4,5]) ? 8 3 Exit: insert(5,[2,4],[2,4,5]) ? 2 2 Exit: insertsort([5,2,4],[2,4,5]) ? 15 2 Call: insert(3,[2,4,5],_24) ? 16 3 Call: gt(3,2) ? 17 4 Call: 3>2 ? 17 4 Exit: 3>2 ? 16 3 Exit: gt(3,2) ? 18 3 Call: insert(3,[4,5],_421) ? 19 4 Call: gt(3,4) ? 20 5 Call: 3>4 ? 20 5 Fail: 3>4 ? 19 4 Fail: gt(3,4) ? 18 3 Exit: insert(3,[4,5],[3,4,5]) ? 15 2 Exit: insert(3,[2,4,5],[2,3,4,5]) ? 1 1 Exit: insertsort([3,5,2,4],[2,3,4,5]) ? L = [2,3,4,5] (94 ms) yes {trace} 9.1 Sorting lists

  8. [5,3,7,8,1,4,7,6] Delete X, X=5 [3,7,8,1,4,7,6] split all<=5 all > 5 [3,1,4] [7,8,7,6] sort sort [1,3,4] [6,7,7,8] addX concatenate [1,3,4,5,6,7,7,8] 9.1 Sorting lists • The sorting procedures bubblesort and insertsort are simple, but inefficient. (time complexity is n2). • A much better sorting algorithm is quicksort. • For example:

  9. 9.1 Sorting lists • To sort a non-empty list, L: • Delete some element X from L and split the rest of L into two lists, called Small and Big, as follows: • All elements in L that are greater then X belong to Big, • And all others to Small. • Sort Small obtaining SortedSmall. • Sort Big obtaining SortedBig. • The whole sorted list is the concatenation of SortedSmall and [X| SortedBig].

  10. 9.1 Sorting lists • Quick sort: % Figure 9.2 Quicksort. quicksort( [], []). quicksort( [X|Tail], Sorted) :- split( X, Tail, Small, Big), quicksort( Small, SortedSmall), quicksort( Big, SortedBig), conc( SortedSmall, [X|SortedBig], Sorted). split( X, [], [], []). split( X, [Y|Tail], [Y|Small], Big) :- gt( X, Y), !, split( X, Tail, Small, Big). split( X, [Y|Tail], Small, [Y|Big]) :- split( X, Tail, Small, Big).

  11. | ?- quicksort([3,5,2,4], L). 1 1 Call: quicksort([3,5,2,4],_24) ? 2 2 Call: split(3,[5,2,4],_95,_96) ? 3 3 Call: gt(3,5) ? 4 4 Call: 3>5 ? 4 4 Fail: 3>5 ? 3 3 Fail: gt(3,5) ? 3 3 Call: split(3,[2,4],_123,_82) ? 4 4 Call: gt(3,2) ? 5 5 Call: 3>2 ? 5 5 Exit: 3>2 ? 4 4 Exit: gt(3,2) ? 6 4 Call: split(3,[4],_110,_82) ? 7 5 Call: gt(3,4) ? 8 6 Call: 3>4 ? 8 6 Fail: 3>4 ? 7 5 Fail: gt(3,4) ? 7 5 Call: split(3,[],_110,_188) ? 7 5 Exit: split(3,[],[],[]) ? 6 4 Exit: split(3,[4],[],[4]) ? 3 3 Exit: split(3,[2,4],[2],[4]) ? 2 2 Exit: split(3,[5,2,4],[2],[5,4]) ? 8 2 Call: quicksort([2],_257) ? 9 3 Call: split(2,[],_283,_284) ? 9 3 Exit: split(2,[],[],[]) ? 10 3 Call: quicksort([],_308) ? 10 3 Exit: quicksort([],[]) ? 11 3 Call: quicksort([],_333) ? 11 3 Exit: quicksort([],[]) ? 12 3 Call: conc([],[2],_361) ? 12 3 Exit: conc([],[2],[2]) ? 8 2 Exit: quicksort([2],[2]) ? 13 2 Call: quicksort([5,4],_387) ? 14 3 Call: split(5,[4],_413,_414) ? 15 4 Call: gt(5,4) ? 16 5 Call: 5>4 ? 16 5 Exit: 5>4 ? 15 4 Exit: gt(5,4) ? 17 4 Call: split(5,[],_400,_491) ? 17 4 Exit: split(5,[],[],[]) ? 14 3 Exit: split(5,[4],[4],[]) ? 18 3 Call: quicksort([4],_517) ? 19 4 Call: split(4,[],_543,_544) ? 19 4 Exit: split(4,[],[],[]) ? 20 4 Call: quicksort([],_568) ? 20 4 Exit: quicksort([],[]) ? 21 4 Call: quicksort([],_593) ? 21 4 Exit: quicksort([],[]) ? 22 4 Call: conc([],[4],_621) ? 22 4 Exit: conc([],[4],[4]) ? 18 3 Exit: quicksort([4],[4]) ? 23 3 Call: quicksort([],_647) ? 23 3 Exit: quicksort([],[]) ? 24 3 Call: conc([4],[5],_675) ? 25 4 Call: conc([],[5],_662) ? 25 4 Exit: conc([],[5],[5]) ? 24 3 Exit: conc([4],[5],[4,5]) ? 13 2 Exit: quicksort([5,4],[4,5]) ? 26 2 Call: conc([2],[3,4,5],_24) ? 27 3 Call: conc([],[3,4,5],_719) ? 27 3 Exit: conc([],[3,4,5],[3,4,5]) ? 26 2 Exit: conc([2],[3,4,5],[2,3,4,5]) ? 1 1 Exit: quicksort([3,5,2,4],[2,3,4,5]) ? L = [2,3,4,5] ? (78 ms) yes {trace} 9.1 Sorting lists

  12. 9.1 Sorting lists • Quick sort: • If the list is split into two lists of approximately equal lengths then the time complexity of this sorting procedure is of the order nlogn, where n is the length of the list to be sorted. • If splitting always results in one list far bigger than the other, then the complexity is in the order of n2. • The program in Figure 9.2 is not a good implementation because using the concatenation operation. • The program in Figure 9.3 is a more efficient implementation of quicksort using difference-pair representation for list.

  13. Z1 A2 Z2 A1 L1 L2 L3 9.1 Sorting lists • To use the difference-pair representation in the sorting procedure, the list in the program of Figure 9.2 can be represented by pairs of lists of the form A-Z as follows (see Page 186): SortedSmall is represented by A1 – Z1 SortedBig is represented by A2 – Z2 • The resulting concatenated list is represented by A1 – Z2 (and Z1 = [X|A2])

  14. 9.1 Sorting lists % Figure 9.3 A more efficient implementation of quicksort using difference-pair representation for lists. quicksort( List, Sorted) :- quicksort2( List, Sorted - [] ). quicksort2( [], Z - Z). quicksort2( [X | Tail], A1 - Z2) :- split( X, Tail, Small, Big), quicksort2( Small, A1 - [X | A2] ), quicksort2( Big, A2 - Z2). split( X, [], [], []). split( X, [Y|Tail], [Y|Small], Big) :- gt( X, Y), !, split( X, Tail, Small, Big). split( X, [Y|Tail], Small, [Y|Big]) :- split( X, Tail, Small, Big).

  15. |?- quicksort([3,5,2,4], L). 1 1 Call: quicksort([3,5,2,4],_24) ? 2 2 Call: quicksort2([3,5,2,4],_24-[]) ? 3 3 Call: split(3,[5,2,4],_122,_123) ? 4 4 Call: gt(3,5) ? 5 5 Call: 3>5 ? 5 5 Fail: 3>5 ? 4 4 Fail: gt(3,5) ? 4 4 Call: split(3,[2,4],_150,_109) ? 5 5 Call: gt(3,2) ? 6 6 Call: 3>2 ? 6 6 Exit: 3>2 ? 5 5 Exit: gt(3,2) ? 7 5 Call: split(3,[4],_137,_109) ? 8 6 Call: gt(3,4) ? 9 7 Call: 3>4 ? 9 7 Fail: 3>4 ? 8 6 Fail: gt(3,4) ? 8 6 Call: split(3,[],_137,_215) ? 8 6 Exit: split(3,[],[],[]) ? 7 5 Exit: split(3,[4],[],[4]) ? 4 4 Exit: split(3,[2,4],[2],[4]) ? 3 3 Exit: split(3,[5,2,4],[2],[5,4]) ? 9 3 Call: quicksort2([2],_24-[3|_250]) ? 10 4 Call: split(2,[],_315,_316) ? 10 4 Exit: split(2,[],[],[]) ? 11 4 Call: quicksort2([],_24-[2|_306]) ? 11 4 Exit: quicksort2([],[2|_306]-[2|_306]) ? 12 4 Call: quicksort2([],_306-[3|_250]) ? 12 4 Exit: quicksort2([],[3|_250]-[3|_250]) ? 9 3 Exit: quicksort2([2],[2,3|_250]-[3|_250]) ? 13 3 Call: quicksort2([5,4],_250-[]) ? 14 4 Call: split(5,[4],_428,_429) ? 15 5 Call: gt(5,4) ? 16 6 Call: 5>4 ? 16 6 Exit: 5>4 ? 15 5 Exit: gt(5,4) ? 17 5 Call: split(5,[],_415,_506) ? 17 5 Exit: split(5,[],[],[]) ? 14 4 Exit: split(5,[4],[4],[]) ? 18 4 Call: quicksort2([4],_250-[5|_498]) ? 19 5 Call: split(4,[],_563,_564) ? 19 5 Exit: split(4,[],[],[]) ? 20 5 Call: quicksort2([],_250-[4|_554]) ? 20 5 Exit: quicksort2([],[4|_554]-[4|_554]) ? 21 5 Call: quicksort2([],_554-[5|_498]) ? 21 5 Exit: quicksort2([],[5|_498]-[5|_498]) ? 18 4 Exit: quicksort2([4],[4,5|_498]-[5|_498]) ? 22 4 Call: quicksort2([],_498-[]) ? 22 4 Exit: quicksort2([],[]-[]) ? 13 3 Exit: quicksort2([5,4],[4,5]-[]) ? 2 2 Exit: quicksort2([3,5,2,4],[2,3,4,5]-[]) ? 1 1 Exit: quicksort([3,5,2,4],[2,3,4,5]) ? L = [2,3,4,5] ? (31 ms) yes {trace} 9.1 Sorting lists

  16. 9.2 Representing sets by binary trees • A disadvantage of using a list for representing a set is that the set membership testing is relatively inefficient. • Using the predicate member( X, L) to find X in a list L is very inefficient because this procedure scans the list element by element until X is found or the end of the list is encountered. • For representing sets, there are various tree structures that facilitate more efficient implementation of the set membership relation. • We will here consider binary trees.

  17. root a Right subtree Left subtree b c d 9.2 Representing sets by binary trees • A binary tree is either empty or it consists of three things: • A root; • A left subtree; • A right subtree. The root can be anything, but the subtrees have to be binary tree again.

  18. a t( L, X, R) X b c d L R 9.2 Representing sets by binary trees • The representation of a binary tree: • Let the atom nil represent the empty tree. • Let the functor be t so the tree that has a root X, a left subtree L, and a right subtree R is represented by the term t(L, X, R). t( t( nil, b, nil), a, t( t( nil, d, nil), c, nil))

  19. 9.2 Representing sets by binary trees • Let use consider the set membership relation in. A goal in( X, T) is true if X is a node in a tree T. • X is in tree T if • The root of T is X, or • X is in the left subtree of T, or • X is in the right subtree of T. in( X, t( _, X, _)). in( X, t( L, _, _)) :- in( X, L). in( X, t( _, _, R)) :- in( X, R). • The goal in( X, nil) will fail for any X.

  20. 9.2 Representing sets by binary trees | ?- T = t( t( nil, b, nil), a, t( t( nil, d, nil), c, nil)), in( X, T). T = t(t(nil,b,nil),a,t(t(nil,d,nil),c,nil)) X = a ? ; T = t(t(nil,b,nil),a,t(t(nil,d,nil),c,nil)) X = b ? ; T = t(t(nil,b,nil),a,t(t(nil,d,nil),c,nil)) X = c ? ; T = t(t(nil,b,nil),a,t(t(nil,d,nil),c,nil)) X = d ? ; (15 ms) no | ?- T = t( t( nil, b, nil), a, t( t( nil, d, nil), c, nil)), in(a, T). T = t(t(nil,b,nil),a,t(t(nil,d,nil),c,nil)) ? (16 ms) yes | ?- T = t( t( nil, b, nil), a, t( t( nil, d, nil), c, nil)), in(e, T). no

  21. 9.2 Representing sets by binary trees • The above representation is also inefficient. • We can improve it by using a binary dictionary. (a binary search tree) • In binary dictionary, the data in the tree can be ordered from left to right according to an ordering relation. • A non-empty tree t( Left, X, Right) is ordered from left to right if: • all the nodes in the left subtree, Left, are less than X, and • all the nodes in the right subtree, Right, are greater than X; and • both subtrees are also ordered. • The advantage of ordering: to search for an object in a binary dictionary, it is always sufficient to search at most one subtree.

  22. 5 3 8 1 6 4 9 7 9.2 Representing sets by binary trees A binary dictionary • To find an item X in a dictionary D: • if X is the root of D then X has been found, otherwise • if X is less than the root of D then search for X in the left subtree of D, otherwise • search for X in the right subtree of D; • if D is empty the search fails.

  23. 9.2 Representing sets by binary trees % Figure 9.7 Finding an item X in a binary dictionary. in( X, t( _, X, _) ). in( X, t( Left, Root, Right) ) :- gt( Root, X), in( X, Left). in( X, t( Left, Root, Right) ) :- gt( X, Root), in( X, Right). • The relation gt( X, Y) means X is greater than Y. • The in procedure itself can be also used for constructing a binary dictionary. For example: | ?- in( 5, D), in( 3, D), in( 8, D). D = t(t(_,3,_),5,t(_,8,_)) ? (16 ms) yes

  24. 3 5 5 3 8 8 9.2 Representing sets by binary trees | ?- in( 5, D), in(3, D), in(8, D). D = t(t(_,3,_),5,t(_,8,_)) ? (16 ms) yes | ?- in( 3, D), in(5, D), in(8, D). D = t(_,3,t(_,5,t(_,8,_))) ? yes

  25. 9.2 Representing sets by binary trees • A tree is (approximately) balanced if, for each node in the tree, its two subtrees accommodate(容納) an approximately equal number of items. • If a dictionary with n nodes is nicely balanced then its height is proportional to logn. • If the tree gets out of balance its performance will degrade. • In extreme cases of totally unbalanced trees, a tree is reduced to a list. In such a case the tree’s height is n, and the tree’s performance is equally poor as that of a list.

  26. 5 5 D2 D1 3 8 3 8 add(D1, 6, D2) 6 9.3 Insertion and deletion in a binary trees • When maintaining a dynamic set of data we may insert new items into the set and delete some old items from the set. in( X, S) X is a member of S add( S, X, S1) Add X to S giving S1 del( S, X, S1) Delete X from S giving S1 • “add” relation: Insert nodes into a binary dictionary at the leaf level

  27. 5 5 3 8 3 8 6 6 5 7 3 8 6 4 7 9.3 Insertion and deletion in a binary trees • “add” relation: Insert nodes into a binary dictionary at the leaf level D3 D2 add(D2, 7, D3) D4 add(D3, 4, D4)

  28. 9.3 Insertion and deletion in a binary trees • Let us call this kind of insertion addleaf( D, X, D1). • Rules for adding at the leaf level are: • The result of adding X to the empty tree is the tree t( nil, X, nil). • If X is the root of D then D1 = D (no duplicate item gets inserted). • If the root of D is greater than X then insert X into the left subtree of D; if the root of D is less than X then insert X into the right subtree.

  29. 9.3 Insertion and deletion in a binary trees % Figure 9.10 Inserting an item as a leaf into the binary dictionary. addleaf( nil, X, t( nil, X, nil)). addleaf( t( Left, X, Right), X, t( Left, X, Right)). addleaf( t( Left, Root, Right), X, t( Left1, Root, Right)) :- gt( Root, X), addleaf( Left, X, Left1). addleaf( t( Left, Root, Right), X, t( Left, Root, Right1)) :- gt( X, Root), addleaf( Right, X, Right1).

  30. 5 3 8 6 5 3 8 9.3 Insertion and deletion in a binary trees D1 D2 add(D1, 6, D2) | ?- consult('C:/GNU-Prolog/Prologcode/programs/atest.pl'). … | ?- consult('C:/GNU-Prolog/Prologcode/programs/fig9_7.pl'). … | ?- consult('C:/GNU-Prolog/Prologcode/programs/fig9_10.pl'). … | ?- in( 5, D1), in(3, D1), in(8, D1), addleaf(D1, 6, D2). D1 = t(t(A,3,B),5,t(nil,8,C)) D2 = t(t(A,3,B),5,t(t(nil,6,nil),8,C)) ? yes

  31. 5 5 3 8 3 8 6 6 7 9.3 Insertion and deletion in a binary trees D2 D3 add(D2, 7, D3) | ?- in(5, D1), in(3, D1), in(8, D1), addleaf(D1, 6, D2), addleaf(D2, 7, D3). D1 = t(t(A,3,B),5,t(nil,8,C)) D2 = t(t(A,3,B),5,t(t(nil,6,nil),8,C)) D3 = t(t(A,3,B),5,t(t(nil,6,t(nil,7,nil)),8,C)) ? yes | ?- in(5, D1), in(3, D1), in(8, D1), addleaf(D1, 6, D2), addleaf(D2, 7, D3), addleaf(D3, 4, D4). D1 = t(t(A,3,nil),5,t(nil,8,B)) D2 = t(t(A,3,nil),5,t(t(nil,6,nil),8,B)) D3 = t(t(A,3,nil),5,t(t(nil,6,t(nil,7,nil)),8,B)) D4 = t(t(A,3,t(nil,4,nil)),5,t(t(nil,6,t(nil,7,nil)),8,B)) ? yes

  32. A A X ? Left Right Left Right 9.3 Insertion and deletion in a binary trees • Consider “delete” operation: • It is easy to delete a leaf, but deleting an internal node is more complicated. • The deletion of a leaf can be in fact defined as the inverse operation of inserting at the leaf level: delleaf( D1, X, D2) :- addleaf( D2, X, D1) • What happens if X is an internal node? Delete X

  33. Y transfer Y remove X X Y Left Right1 Left Right Left Right 9.3 Insertion and deletion in a binary trees • Solutions: • If one of the subtrees Left and Right is empty then the solution is simple: the non-empty subtree is connected to A. • If they are both non-empty then the left-most node of Right, Y, is transferred from its current position upwards to fill the gap after X.

  34. 9.3 Insertion and deletion in a binary trees % Figure 9.13 Deleting from the binary dictionary. del( t( nil, X, Right), X, Right). del( t( Left, X, nil), X, Left). del( t( Left, X, Right), X, t( Left, Y, Right1)) :- delmin( Right, Y, Right1). del( t( Left, Root, Right), X, t( Left1, Root, Right)) :- gt( Root, X), del( Left, X, Left1). del( t( Left, Root, Right), X, t( Left, Root, Right1)) :- gt( X, Root), del( Right, X, Right1). delmin( t( nil, Y, R), Y, R). delmin( t( Left, Root, Right), Y, t( Left1, Root, Right)) :- delmin( Left, Y, Left1).

  35. 9.3 Insertion and deletion in a binary trees • Another elegant(簡練確切的) solution of add: • The add relation can be defined non-deterministically so that a new item is inserted at any level of the tree, not just at the leaf level. • To add X to a binary dictionary D either: • Add X at the root of D (so that X becomes the new root), or • If the root of D is greater than X than insert X into the left subtree of D, otherwise insert X into the right subtree of D.

  36. X X Y Y R2 L1 R L L2 R1 9.3 Insertion and deletion in a binary trees • Insert X at the root of D: addroot( D, X, D1) where X is the item to be inserted at the root of D and D1 is the resulting dictionary with X as its root. D Y R L add X at root X < Y Y < X D1

  37. 9.3 Insertion and deletion in a binary trees • What are the subtree L1 and L2 in Figure 9.14 (or R1 and R2 alternatively)? • L1 and L2 must be binary dictionaries; • The set of nodes in L1 and L2 is equal to the set of nodes in L; • All the nodes in L1 are less than X, and all the nodes in L2 are greater than X. • If X were added as the root into L, then the subtrees of the resulting tree would be just L1 and L2. • In Prolog, L1 and L2, must satisfy the goal: addroot( L, X, t(L1, X, L2)) • The same constraints apply to R1 and R2: addroot( R, X, t(R1, X, R2))

  38. 9.3 Insertion and deletion in a binary trees % Figure 9.15 Insertion into the binary dictionary at any level of the tree. add( Tree, X, NewTree) :- addroot( Tree, X, NewTree). add( t( L, Y, R), X, t( L1, Y, R)) :- gt( Y, X), add( L, X, L1). add( t( L, Y, R), X, t( L, Y, R1)) :- gt( X, Y), add( R, X, R1). addroot( nil, X, t( nil, X, nil)). addroot( t( L, Y, R), X, t( L1, X, t( L2, Y, R))) :- gt( Y, X), addroot( L, X, t( L1, X, L2)). addroot( t( L, Y, R), X, t( t( L, Y, R1), X, R2)) :- gt( X, Y), addroot( R, X, t( R1, X, R2)).

  39. 9.3 Insertion and deletion in a binary trees • The nice thing about this insertion procedure is that there is no restriction on the level of insertion. • Therefore add can be used in the inverse direction in order to delete an item from the dictionary. • For example, the following goal list constructs a dictionary D containing the items 3, 5, 1, 6, and then deletes 5 yielding a dictionary DD: add(nil, 3, D1), add(D1, 5, D2), add(D2, 1, D3), add( D3, 6, D), add(DD, 5, D).

  40. 6 6 1 1 3 5 3 1 1 6 6 5 3 3 9.3 Insertion and deletion in a binary trees | ?- add(nil, 3, D1), add(D1, 5, D2), add(D2, 1, D3), add( D3, 6, D), add(DD, 5, D). D = t(t(nil,1,t(t(nil,3,nil),5,nil)),6,nil) D1 = t(nil,3,nil) D2 = t(t(nil,3,nil),5,nil) D3 = t(nil,1,t(t(nil,3,nil),5,nil)) DD = t(t(nil,1,t(nil,3,nil)),6,nil) ? ; D = t(nil,1,t(t(t(nil,3,nil),5,nil),6,nil)) D1 = t(nil,3,nil) D2 = t(t(nil,3,nil),5,nil) D3 = t(nil,1,t(t(nil,3,nil),5,nil)) DD = t(nil,1,t(t(nil,3,nil),6,nil)) ? ; D = t(nil,1,t(t(nil,3,nil),5,t(nil,6,nil))) D1 = t(nil,3,nil) D2 = t(t(nil,3,nil),5,nil) D3 = t(nil,1,t(t(nil,3,nil),5,nil)) DD = t(nil,1,t(t(nil,3,nil),6,nil)) ? ; ...

  41. 5 3 8 1 6 4 9 9 Right subtree 7 8 7 6 5 4 3 Left subtree 1 9.4 Display trees • The goal write( T) will output all the information of a binary tree T, but will not graphically indicate the actual tree structure. • There is a simple method for displaying trees in graphical forms. The trick is to display a tree growing from left to right, and not from top to bottom as trees are usually pictured. • For example:

  42. 9 Right subtree 8 7 6 5 4 3 Left subtree 1 9.4 Display trees • The procedure show( T) will display a tree T in the graphical form. • To show a non-empty tree, T: (1) show the right subtree of T, indicated by some distance, H, to the right; (2) write the root of T; (3) show the left subtree of T indented by distance H to the right. • The indentation distance H is an additional parameter for displaying trees. show2( T, H) displays T indented H spaces from the left margin.

  43. 9.4 Display trees % Figure 9.17 Displaying a binary tree. show( Tree) :- show2( Tree, 0). show2( nil, _). show2( t( Left, X, Right), Indent) :- Ind2 is Indent + 2, show2( Right, Ind2), tab( Indent), write( X), nl, show2( Left, Ind2).

  44. 6 6 1 1 5 5 3 3 1 1 6 6 5 5 3 3 9.3 Insertion and deletion in a binary trees | ?- add(nil, 3, D1), add(D1, 5, D2), add(D2, 1, D3), add(D3, 6, D), show( D). 6 5 3 1 D = t(t(nil,1,t(t(nil,3,nil),5,nil)),6,nil) D1 = t(nil,3,nil) D2 = t(t(nil,3,nil),5,nil) D3 = t(nil,1,t(t(nil,3,nil),5,nil)) ? ; 6 5 3 1 D = t(nil,1,t(t(t(nil,3,nil),5,nil),6,nil)) D1 = t(nil,3,nil) D2 = t(t(nil,3,nil),5,nil) D3 = t(nil,1,t(t(nil,3,nil),5,nil)) ? ;

  45. 9.3 Insertion and deletion in a binary trees 6 5 3 1 D = t(nil,1,t(t(nil,3,nil),5,t(nil,6,nil))) D1 = t(nil,3,nil) D2 = t(t(nil,3,nil),5,nil) D3 = t(nil,1,t(t(nil,3,nil),5,nil)) ? ; 6 5 3 1 D = t(t(t(nil,1,t(nil,3,nil)),5,nil),6,nil) D1 = t(nil,3,nil) D2 = t(t(nil,3,nil),5,nil) D3 = t(t(nil,1,t(nil,3,nil)),5,nil) ? ;

  46. 9.3 Insertion and deletion in a binary trees 6 5 3 1 D = t(t(nil,1,t(nil,3,nil)),5,t(nil,6,nil)) D1 = t(nil,3,nil) D2 = t(t(nil,3,nil),5,nil) D3 = t(t(nil,1,t(nil,3,nil)),5,nil) ? ; 6 5 3 1 D = t(t(t(t(nil,1,nil),3,nil),5,nil),6,nil) D1 = t(nil,3,nil) D2 = t(t(nil,3,nil),5,nil) D3 = t(t(t(nil,1,nil),3,nil),5,nil) ? ; ...

  47. t b 3 1 s v 5 2 a c 2 u d 9.5 Graphs9.5.1 Representing graphs • A graph is defined by a set of nodes and a set of edges, where each edge is a pair of nodes. • When the edges are directed they are also called arcs. Arcs are represented by ordered pairs. Such a graph is a directed graph. Undirected graph Directed graph

  48. t b 3 1 s v 5 2 a c 2 u d 9.5 Graphs9.5.1 Representing graphs • The representation of graphs: • Method 1: • connected( a, b), connected( b, c),… • arc( s, t, 3), arc( t, v, 1),… • Method 2: • G1 = graph([a,b,c,d], [e(a,b), e(b,d), e(b,c), e(c,d)]) • G2 = digraph([s, t, u, v], [a(s,t,3), a(t,v,1), a(t,u,5), a(u,t,2), a(v,u,2)]) • Method 3: • G1 = [a->[b], b->[a,c,d], c->[b,d], d->[b,c]] • G2 = [s->[t/3], t->[u/5,v/1], u->[t/2],v->[u/2]] • The symbols ’->’ and ’/’ are infix operators. G1 G2

  49. 9.5 Graphs9.5.1 Representing graphs • What is the most suitable representation? • Depending on the application and on operations to be performed on graphs. • Two typical operations are: • Find a path between two given nodes; • Find a subgraph, with some specified properties, of a graph.

  50. b a c d 9.5.2 Finding a path • Let G be a graph, and A and Z two nodes in G. Let us define the relation: path( A, Z, G, P) where P is an acyclic path between A and Z in G. • For example: path( a, d, G, [a,b,d]) path( a, d, G, [a,b,c,d]) • To find an acyclic path, P, between A and Z in a graph, G: If A = Z then P = [A], otherwise find an acyclic path, P1, from some node Y to Z, and find a path from A to Y avoiding the nodes in P1.

More Related