Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN AK IT: Softwarekonstruktion
Patterns The concept of patterns originates from architecture (Christopher Alexander, 1977): “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice” (Christopher Alexander e. a.: “A Pattern Language”. Oxford University Press, New York, 1977.) UCN/IT: Advanced Computer Studies 2 FEN 18/09/2007
(OO) Design Patterns A well known and widely accepted concept in software engineering Developed in the early 1990s and published by Gamma e.a. (“Gang of Four”, GoF) in 1995: “(…) design patterns (…) are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.” (Erich Gamma e.a.: ”Design Patterns. Elements of Reusable Object-Oriented Software”. Addison-Wesley ) UCN/IT: Advanced Computer Studies 3 FEN 18/09/2007
The benefits of patterns A pattern captures a proven good design: – A pattern is based on experience – A pattern is discovered – not invented It introduces a new (and higher) level of abstraction, which makes it easier: – to talk and reason about design on a higher level – to document and communicate design One doesn’t have to reinvent solutions over and over again Patterns facilitate reuse not only of code fragments, but of ideas. UCN/IT: Advanced Computer Studies 4 FEN 18/09/2007
Patterns as a learning tool It is often said that good skills in software construction require experience and talent …and neither can be taught or learned at school Patterns capture experience (and talent) in a way that is communicable and comprehensible …and hence experience can be taught (and learned) So we should rely heavily on patterns in our teaching UCN/IT: Advanced Computer Studies 5 FEN 18/09/2007
Algorithm Patterns Many different problems from many different problem domains may be solved by algorithms that possess a common structure – or a common pattern. By abstracting and formalizing this structure it becomes a reusable pattern with all the desired properties connected to patterns. Patterns have names – within the field of algorithms the following – among others – may be identified: – Sweep algorithms – Search algorithms – Merge algorithms – Divide and Conquer algorithms – Greedy algorithms – Backtracking algorithms – Dynamic programming etc. etc.… UCN/IT: Advanced Computer Studies 6 FEN 18/09/2007
The Sweep Algorithm Pattern Purpose: – inspects all elements in a collection (senselessly sweeping through the collection) and doing something according to the characteristics of the current element. Benefits: – separates operations depending on the collection (loop control) from operations depending on the actual problem at hand. UCN/IT: Advanced Computer Studies 7 FEN 18/09/2007
The Sweep Algorithm Pattern Examples: counting the number of students older than 25 years in of list of students increasing the value of a discount percentage by 10 on all elements with a balance of more than DKK 10,000 in a set of customers calculating the average number of words per sentence in a text etc. etc. UCN/IT: Advanced Computer Studies 8 FEN 18/09/2007
Sweep Algorithms on Sequences of Integers UCN/IT: Advanced Computer Studies 9 FEN 18/09/2007 visitedUS a: i Data representation (C#): int i; int a[]; ; int i = 0; while ( i < a.Length ) { ; i++; } // end while ; for (int i= 0 ; i < a.Length ; i++ ) { ; } // end for In C# a counter controlled loop may be written simpler using the foreach-statement.
Applying the sweep pattern UCN/IT: Advanced Computer Studies 10 FEN 18/09/2007 Counting zeros in an array: DO_INIT:int count= 0; DO:if (a[i] = = 0) count++; int count= 0; for (int i= 0 ; i < a.Length; i++){ if (a[i] = = 0) count++; } // end for ; for (int i= 0 ; i < a.Length ; i++ ) { ; } // end for
Applying the sweep pattern UCN/IT: Advanced Computer Studies 11 FEN 18/09/2007 Increasing all elements by one: DO_INIT:no concretising is needed. DO:a[i]++; ; for (int i= 0 ; i < a.Length ; i++ ) { ; } // end for for (int i= 0 ; i < a.Length; i++) { a[i]++; } // end for
The Search Algorithm Pattern Purpose: – The algorithm looks for an element (target, t) with some specified property in a collection Benefits: – The search terminates when the first occurrence of the target is discovered – Loop control is separated from the testing for the desired property Examples: – Searching for a customer with a balance greater than DKK 10,000 – Searching for a student older than 30 – Searching for the word “algorithm” in a text. UCN/IT: Advanced Computer Studies 12 FEN 18/09/2007
The Search Pattern - Structure Notation: CC: Candidate Collection c: Element to be examined t:The target element UCN/IT: Advanced Computer Studies 13 FEN 18/09/2007 ; bool found= false; while ( ! found && ) { ; if ( ) found = true; else { } Only the abstract operations (in red) are problem specific The structure is general and reusable
Applying the pattern to an int[] a UCN/IT: Advanced Computer Studies 14 FEN 18/09/2007 initialise:int i = 0 select:c = a[i] CC Ø:i < a.Length split:i ++ CC a: i int c; int i= 0; bool found= false; while ( !found && i<a.Length ) { c = a[i]; if (c == target) found= true; else i ++; } // end while Conditions connected to loop control Conditions connected to the actual search
Binary Search: A "smart" realisation of the search pattern on a sorted sequence The strategy: – Select an element in the middle of the candidate set: If this is the element we are looking for – we are done If the target comes after the middle element, then look in the upper part (remember the collection is sorted) If the target comes before the middle element, then look in the lower part (again remember the collection is sorted) – Repeat this until the target has been found or there are no more candidate elements UCN/IT: Advanced Computer Studies 15 FEN 18/09/2007
Binary Search: Applied to a sorted array of integers UCN/IT: Advanced Computer Studies 16 FEN 18/09/2007 CC a: lowhigh int low = 0; int high = a.Length -1; int c, middle; bool found = false; while ( ! found && low<=high ) { middle = (high + low) / 2; c= a[middle]; if (c == t) found= true; else if ( c<t )low = middle+1; else high= middle-1; } // end while INITIALISE:int low = 0; int high= a.length; SELECT:middle= (low´+high)/2 c = a[i] CC Ø:low <= high SPLIT:if (k<m) low= middle + 1; else high:= middle – 1; INITIALISE:int low = 0; int high= a.Length; SELECT:middle= (low+high)/2 c = a[middle] CC Ø:low <= high SPLIT:if (c<t) low= middle + 1; else high= middle – 1;
Binary Search Please note: – Binary search is very efficient (logarithmic in execution time), but: The realisation of SPLIT relies heavily on the precondition that the array is sorted. The realisation of SELECT requires that the data representation provides random access to elements. Binary search is not to be applied otherwise (don’t ever use it on linked lists) UCN/IT: Advanced Computer Studies 17 FEN 18/09/2007