session 05: c# patterns algorithm patterns: sweep search fen 2013-03-021ak it: softwarekonstruktion

17
Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-02 1 AK IT: Softwarekonstruktion

Upload: adam-hutchinson

Post on 27-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

AK IT: Softwarekonstruktion 1

Session 05:C# Patterns

Algorithm Patterns:

SweepSearch

FEN 2013-03-02

Page 2: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK 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

2FEN 18/09/2007

Page 3: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

(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

3FEN 18/09/2007

Page 4: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

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

4FEN 18/09/2007

Page 5: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

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

5FEN 18/09/2007

Page 6: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

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

6FEN 18/09/2007

Page 7: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

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

7FEN 18/09/2007

Page 8: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

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

8FEN 18/09/2007

Page 9: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

Sweep Algorithms on Sequences of Integers

UCN/IT:Advanced Computer Studies

9FEN 18/09/2007

visited US a:

i

Data representation (C#):

int i; int a[];

< DO_INIT >;

int i = 0;

while ( i < a.Length ) {

< DO something to a[i]) >;

i++;

} // end while

< DO_INIT >;

for (int i= 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.

Page 10: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

Applying the sweep pattern

UCN/IT:Advanced Computer Studies

10FEN 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

< DO_INIT >;

for (int i= 0 ; i < a.Length ; i++ ) {

< DO something to a[i]) >;

} // end for

Page 11: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

Applying the sweep pattern

UCN/IT:Advanced Computer Studies

11FEN 18/09/2007

Increasing all elements by one:

DO_INIT: no concretising is needed.

DO: a[i]++; < DO_INIT >;

for (int i= 0 ; i < a.Length ; i++ ) {

< DO something to a[i]) >;

} // end for

for (int i= 0 ; i < a.Length; i++) {

a[i]++;

} // end for

Page 12: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

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

12FEN 18/09/2007

Page 13: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

The Search Pattern - StructureNotation:• CC: Candidate Collection• c: Element to be examined• t: The target element

UCN/IT:Advanced Computer Studies

13FEN 18/09/2007

< Initialise CC >;

bool found= false;

while ( ! found && <CC ¹ Ø > ) {

< Select c from CC >;

if ( < c==t > )

found = true;

else {

< Split CC >

}

Only the abstract operations (in red) are

problem specific

The structure is general and

reusable

Page 14: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

Applying the pattern to an int[] a

UCN/IT:Advanced Computer Studies

14FEN 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

Page 15: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

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

15FEN 18/09/2007

Page 16: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

Binary Search:

Applied to a sorted array of integers

UCN/IT:Advanced Computer Studies

16FEN 18/09/2007

CC

a:

low high

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 <= highSPLIT: 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 <= highSPLIT: if (c<t) low= middle + 1;

else high= middle – 1;

Page 17: Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN 2013-03-021AK IT: Softwarekonstruktion

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

17FEN 18/09/2007