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

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Composite: Shapes Organisation State Observer Design Patterns.
MATH 224 – Discrete Mathematics
Types of Algorithms.
Design Patterns Examples of smart use of inheritance and polymorphism: Composite Pattern State Pattern FEN 2014UCN Teknologi/act2learn1.
Object-Oriented Design Patterns Composite Singleton State Observer … Autumn 2012UCN Technology: IT/Computer Science1.
Creating Computer Programs lesson 27. This lesson includes the following sections: What is a Computer Program? How Programs Solve Problems Two Approaches:
8 Algorithms Foundations of Computer Science ã Cengage Learning.
Factorial Recursion stack Binary Search Towers of Hanoi
Software Engineering and Design Principles Chapter 1.
Recursion Chapter 11 Chapter 11.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Design Patterns Daniel McClain. Background What are they?  Way of recording solutions to recurring design problems History  “A Pattern Language: Towns,
Ralph Johnson - University of Illinois1 Patterns: What They Are, and How to Write Them Ralph Johnson University of Illinois at Urbana-Champaign
Algorithm Efficiency and Sorting
Searching Arrays Linear search Binary search small arrays
Chapter 1 Program Design
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Introduction to software design patterns For CSE 3902 By: Matt Boggus.
Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns.
1 Copyright © 2014 Atego. Patterns INCOSE MBSE WG – Simon A. Perry - Atego.
TC Methodology Massimo Cossentino (Italian National Research Council) Radovan Cervenka (Whitestein Technologies)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Chapter Five An Introduction to Design Patterns Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information.
University of Toronto Department of Computer Science CSC444 Lec05- 1 Lecture 5: Decomposition and Abstraction Decomposition When to decompose Identifying.
January 12, Introduction to Design Patterns Tim Burke References: –Gamma, Erich, et. al. (AKA, The Gang of Four). Design Patterns: Elements of Reusable.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Chapter 19: Searching and Sorting Algorithms
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Fundamentals of Algorithms MCS - 2 Lecture # 7
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Design Patterns. Patterns “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Object-Oriented Design Principles and Patterns. © 2005, James R. Vallino2 How Do You Design? What principles guide you when you create a design? What.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
Data Structure Introduction.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
Software Engineering Design & UML.
Introduction to Design Patterns Part 1. © Lethbridge/Laganière 2001 Chapter 6: Using design patterns2 Patterns - Architectural Architectural Patterns:
Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.
Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
Data Structures and Algorithms Dr. Tehseen Zia Assistant Professor Dept. Computer Science and IT University of Sargodha Lecture 1.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Session 07: C# Design Patterns
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
FEN NOEA/IT: Advanced Computer Studies 1 Patterns The concept of patterns originates from architecture (Christopher Alexander, 1977): “Each pattern.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Design and Analysis of Algorithms & Computational Complexity CS490 Koji Tajii.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
Searching Arrays Linear search Binary search small arrays
GRASP – Designing Objects with Responsibilities
Algorithms and Problem Solving
Design Patterns (Chapter 6 of Text Book – Study just 8)
COP 3503 FALL 2012 Shayan Javed Lecture 15
Design Patterns Introduction
Chapter 1 Program Development
Types of Algorithms.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Algorithms Chapter 3 With Question/Answer Animations
Types of Algorithms.
Data Structures Review Session
Algorithms and Problem Solving
Lecture 6 Architecture Algorithm Defin ition. Algorithm 1stDefinition: Sequence of steps that can be taken to solve a problem 2ndDefinition: The step.
Types of Algorithms.
Presentation transcript:

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