170 likes | 402 Views
Session 05: C# Patterns. Algorithm Patterns: Sweep Search. Patterns. The concept of patterns originates from architecture (Christopher Alexander, 1977):
E N D
Session 05:C# Patterns Algorithm Patterns: Sweep Search 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
(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. 1995.) UCN/IT: Advanced Computer Studies
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
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
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
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
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
visited US a: i Sweep Algorithms on Sequences of Integers Data representation (C#): inti; int a[]; < DO_INIT >; inti = 0; while ( i < a.Length) { < DO something to a[i]) >; i++; } // end while < DO_INIT >; for (inti= 0 ; i < a.Length; i++ ) { < DO something to a[i]) >; } // end for In C# a counter controlled loop may be written simpler using the foreach-statement. UCN/IT: Advanced Computer Studies
Applying the sweep pattern Counting zeros in an array: DO_INIT: int count= 0; DO: if(a[i] = = 0) count++; < DO_INIT >; for (inti= 0 ; i < a.Length; i++ ) { < DO something to a[i]) >; } // end for int count= 0; for (inti= 0 ; i < a.Length; i++){ if (a[i] = = 0) count++; }// end for UCN/IT: Advanced Computer Studies
Applying the sweep pattern Increasing all elements by one: DO_INIT: no concretising is needed. DO: a[i]++; < DO_INIT >; for (inti= 0 ; i < a.Length; i++ ) { < DO something to a[i]) >; } // end for for (inti= 0 ; i < a.Length; i++) { a[i]++; } // end for UCN/IT: Advanced Computer Studies
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
The Search Pattern - Structure Only the abstract operations (in red) are problem specific Notation: • CC: Candidate Collection • c: Element to be examined • t: The target element < InitialiseCC >; boolfound= false; while ( ! found && <CC ¹Ø > ) { < Select c from CC >; if ( < c==t >) found = true; else { < Split CC > } } The structure is general and reusable UCN/IT: Advanced Computer Studies
Applying the pattern to an int[] a initialise: inti = 0 select: c = a[i] CC¹ Ø: i < a.Length split: i ++ a: i CC int c; int i= 0; boolfound= 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 UCN/IT: Advanced Computer Studies
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
Binary Search: Applied to a sorted array of integers INITIALISE: intlow = 0; inthigh= a.Length; SELECT: middle= (low+high)/2 c = a[middle] CC¹ Ø: low <= high SPLIT: if (c<t) low= middle + 1; elsehigh= middle – 1; 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; 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 CC a: low high UCN/IT: Advanced Computer Studies
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