session 05 c patterns
Download
Skip this Video
Download Presentation
Session 05: C# Patterns

Loading in 2 Seconds...

play fullscreen
1 / 17

Session 05: C# Patterns - PowerPoint PPT Presentation


  • 181 Views
  • Uploaded on

Session 05: C# Patterns. Algorithm Patterns: Sweep Search. Patterns. The concept of patterns originates from architecture (Christopher Alexander, 1977):

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Session 05: C# Patterns' - erek


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
session 05 c patterns

Session 05:C# Patterns

Algorithm Patterns:

Sweep

Search

AK IT: Softwarekonstruktion

patterns
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
(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
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
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
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
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 pattern1
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

sweep algorithms on sequences of integers
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
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 pattern1
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
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
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 && ) {

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

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

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

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

else high= middle-1;

} // end while

CC

a:

low high

UCN/IT:

Advanced Computer Studies

binary search
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

ad