pti 2012-2-05 - algorithm - edited
DESCRIPTION
Tugas PTITRANSCRIPT
-
1
HUG1M2 - PENGANTAR TEKNIK INFORMATIKA
Semester Genap 2012-2013
05 Algoritma dan Paradigma Pemrograman
Disusun oleh: Tjokorda Agung Budi Wirayuda
1.1 Tujuan Pembelajaran:
1.2 Tujuan Umum:
1. Memaparkan konsep dasar dan keterhubungan antar basic building blocks
dari sistem terkomputerisasi: data-informasi-pengetahuan, sistem komputer,
pemrograman, Internet. (bobot = 30%)
1.3 Tujuan Khusus
Setelah mengikuti bagian kuliah ini, mahasiswa dapat:
1. Menjelaskan apa itu algoritma
2. Memahami algoritma sebagai suatu bentuk universalitas dalam problem
solving
3. Mengenal prinsip algoritma dalam efektifitas dan efisiensi
4. Mengenal dan mampu menjelaskan paradigma pemrograman serta kakas
bantu yang digunakan
2 Materi :
2.1 Definition of the algorithm
In the first semester based on 2012 curriculum, student of Informatics Engineering have attended courses KUG1C3 Basic Algorithms and Programming. Lets try to review student understanding of Algorithm by asking 5 students to explain their understanding of the definition of the algorithm.
No Nim The definition of the algorithm.
1 113120092 Langkah-langkah untuk menyelesaikan masalah
2 113120109 Langkah-langkah atau cara dalam menyelesaikan suatu masalah
3 113120101 Perintah bertahap untuk menyelesaikan suatu masalah dari awal hingga akhir
4 113120099 Urutan langkah-langkah untuk menyelesaikan suatu masalah yang berhubungan dengan matematika atau logika mulai dari awal hingga akhir
5 113120107 Urutan logis pengambilan keputusan untuk penyelesaian masalah yang disusun secara sistematis
Conclusion: Algorithm is kumpulan langkah-langkah yang dilakukan secara sistematis dan logis untuk menghasilkan suatu solusi dari sebuah permasalahan.
-
2
Several definition of Algorithm
An algorithm is a computable set of steps to achieve a desired result.
An algorithm is a set of rules that specify the order and kind of arithmetic operations that are used on specified set of data.
An algorithm is a sequence of finite number of steps arranged in a specific logical orders which, when executed, will produce a correct solution for a specific problem. (Taken from an undergraduate course at UMBC).
An algorithm is a set of instructions for solving a problem. When the instructions are followed, it must eventually stop with an answer. (Given by Prof. Carol Wolf in a course on algorithms).
An algorithm is a finite, definite, effective procedure, with some output. (Donald Knuth)
An algorithm is a set of instructions that can be followed precisely to achieve some objective. (Slide CS1013 Introduction to Algorithm and Efficiency, IT Telkom 2009)
2.2 Essentials Properties of Algorithm
What are the common essential properties of algorithm from above definitions? 1. Finite (w.r.t.: set of instructions, use of resources, time of computation) 2. Precise and computable 3. Specific logical order
a. Deterministic (every step has a well-defined successor), b. Non-Deterministic (randomized algorithms, but also parallel algorithms)
4. Produce a result When you create or read an algorithm, keep in mind The basic question for algorithm, which is (its just small sample of practice question):
1. Does it terminate? 2. Is it logically correct? 3. Is the result of the algorithm determined? 4. How much memory will it use? 5. Can it done?
The question above is a simple way to evaluate or decide its algorithm or not
2.3 Building Block of Algorithm
Using abstraction level we can view the essential components of algorithm consist of 3 part named (Input-Process-Output a.k.a I-P-O).
-
3
Remember abstraction is a concept of how we perceive an object / thing / event and describe the essential elements of an object / thing / event. Abstraction techniques can be used in any problem and it allows us to generate an appropriate domain for explaining to people in need in accordance with the level of knowledge and interests.
For better understanding lets see some example of algorithm as I-P-O.
Example 1 Case: multiplication of 2 number Input : 2 number Output : multiplication result of 2 number Process: number 1 multiply with number 2 (or vice versa)
Example 2 Case: Go to Bandung Indah Plaza Input : Starting point from IT Telkom F Building Output : Arrived at Bandung Indah Plaza Process: 1. Check wallet, 2. IF there is 100.000 and hand pone had balance THEN
call taxi to Bandung Indah Plaza;
arrived at Bandung Indah Plaza see the argo and pay the taxi, dont forget to get the change
ELSE ........ {if we dont give a initial state of limitation the process to go for Bandung Indah Plaza can be un-numerous way }
Example 3 Case: Sorting Card Input: Unsorted Card Output: Sorted Card Process: ???? -> give student 5 minute to think and create the algorithm
Keep In Mind The Basic Characteristics of Algorithm: 1. Finite (w.r.t.: set of instructions, use of resources, time of computation) 2. Precise and computable 3. Specific logical order
a. Deterministic (every step has a well-defined successor), b. Non-Deterministic (randomized algorithms, but also parallel
algorithms) 4. Produce a result
Algorithm
-
4
2.4 Specifying an algorithm
Algorithm can be seen as a way
measurable technique, it not only
that we face in daily life. Base on that condition, algorithm can be write/specify using
many rule/form/template, the common way to describe algorithm are using:
The Common Way To Describe Algorithm
Using natural language
Using flowchart
Using pseudo code
Specifying an algorithm
as a way of think to solve problem using systematic
technique, it not only applies in Informatics, it can apply in any problem
face in daily life. Base on that condition, algorithm can be write/specify using
many rule/form/template, the common way to describe algorithm are using:
Example
Algorithm E (Euclids algorithm). Given twinteger m and n, find their greatest common division, i.e., the largest positive integer which evenly divides both m and n. E1.[Find reminder.]Divide m by n and let reminder (we will have 0 r n.) E2. [Is it zero?] If r=0, the algorithm terminate; the answer. E3. [Interchange] Set m n, nr, and go back to step E1.
Algorithm LargestNumber Input: A non-empty list of numbers Output: The largest number in the list largest L0 for each item in the list L1, do if the item > largest, then largest the item return largest
systematic and
in Informatics, it can apply in any problem
face in daily life. Base on that condition, algorithm can be write/specify using
many rule/form/template, the common way to describe algorithm are using:
. Given two positive greatest common
, i.e., the largest positive integer which
and let r be the
=0, the algorithm terminate; n is
, and go back to
empty list of numbers L. number in the list L.
-
5
The Common Way To Describe Algorithm
Example
Using program source code
program multiplication var n,m:integer; begin readln(n); readln(m); writeln (n*m;) end;
Remember the purpose we write the algorithm. Its for someone else consume, so we must make sure the reader of algorithm can understand it. Base on that fact, algorithm usually writes using common agreement (consensus) depend on problem domain and reader domain.
2.5 Quality of Algorithm
Give solution to solve a problem is the key of an algorithm, in the real world there will
be more than one solution to solve one problem (example: sorting card). The basic
question: which algorithm should we choose?
Quality of algorithm usually related with term effectiveness and efficiency. Effective
and efficient had a close realtion with limited resource for computation process.
Suppose that we have a superfast computer with unlimited memory, is threre a
reason we should study algorithm? Of course, because we should prove that the
solution are terminates and give a correct answer.
These term related with the quality of algorithm:
All of those terms above are related to the technology, perception and people need
so it not absolutely can be done in every case.
Efficiency and Effectiveness
Time vs space/resource
Correnctness
100% correct
Approximation algorithm -> the error in tolerance threshold
Simplicity
easy to understand or to do, "simplicity is the most sophisticate thing"
Generality
The scope and domain of algorithm, it's can be apply on every case or specific case (environment, condiiton, etc) only
-
6
Algorithms are an important and durable part of computer science because they can
be studied in a machine/language independent way. We can use the RAM model of
computation for all our analysis. The basic assumption:
1. Each simple operation (+, -, =, if, call) takes 1 step.
2. Loops and subroutine calls are not simple operations. They depend upon the
size of the data and the contents of a subroutine. Sort is not a single step
operation.
3. Each memory access takes exactly 1 step.
We measure the run time of an algorithm by counting the number of steps of process
(complexity). Complexity is a approximate of efficiency, its a way to measure/predict
the scale of algorithm.
A. Worst-Case Complexity. The worst case complexity of an algorithm is the
function defined by the maximum number of steps taken on any instance of size
n.
B. Best-Case Complexity. The best case complexity of an algorithm is the function
defined by the minimum number of steps taken on any instance of size n.
C. Average-Case Complexity. The average-case complexity of the algorithm is the
function defined by an average number of steps taken on any instance of size n.
Each of these complexities defines a numerical function: time vs. size. Best, worst,
and average are difficult to deal with precisely because the details are very
complicated. It easier to talk about upper and lower bounds of the function.
Asymptotic notation (-> Big O; -> Big Omega ;-> Big Theta) are as well as we
can practically deal with complexity functions.
g(n) = (f(n)) means C f(n) is an upper bound on g(n).
g(n) = (f(n)) means C f (n) is a lower bound on g(n).
g(n) = (f(n)) means C 1 f(n) is an upper bound on g(n) and C 2 f(n) is a
lower bound on g(n).
C, C 1 , and C 2 are all constants independent of n.
In these material lets we focused on Big O notation to measure complexity of
algorithm. What is big O:
the number of operations required to perform a function
expression representing some growth relative to the size of the problem (N)
Formal definition: f(n) = O(g(n)) if there are positive constants n0 and c such
that to the right of n0 , the value of f(n) always lies on or below c.g(n).
Exp: O(1), O(N), O(N2), O(log N),
O(1) an algorithm takes constant time to run; performance isnt affected by the size of the problem
Exp:
-
7
addressing main memory in a computer array lookup
O(N) the number of operations required to perform a function is directly proportional to the number of items being processed
Exp: waiting in a line at a supermarket Assume: 2 mins / cust (avg) 10 cust 20 mins; 100 cust 200 mins
O(N2)
Each member of the group greets every other member 6 persons 5+4+3+2+1 = 15 7 persons 21 8 persons 28 N persons (N2-N)/2 greets
Big O disregard any constant (N2-N) as N becomes larger, subtracting N from N2
will have less and less of an overall effect
O(log N) This Big O is a task for student to search what kind
operation/process/algorithm that has O (log N) for asymtotic notation
2.6 Strategy of Algorithm
Brute-force
Greedy
Divide-and-conquer
Backtracking
Branch-and-bound
Heuristics
Pattern matching and string/text
Dynamic Programming
Numerical approximation
2.7 Programming Paradigm
A programming paradigm is a fundamental style of computer programming. There
are four main paradigms: object-oriented, imperative, functional and declarative.[1]
Their foundations are distinct models of computation: Turing machine for object-
oriented and imperative programming, lambda calculus for functional programming,
and first order logic for logic programming.
-
8
A programming model is an abstraction of a computer system. For example, the "von
Neumann model" is a model used in traditional sequential computers. For parallel
computing, there are many possible models typically reflecting different ways
processors can be interconnected. The most common are based on shared memory,
distributed memory with message passing, or a hybrid of the two.
A programming language can support multiple paradigms. For example, programs
written in C++ or Object Pascal can be purely procedural, or purely object-oriented,
or contain elements of both paradigms. Software designers and programmers decide
how to use those paradigm elements.
In object-oriented programming, programmers can think of a program as a collection
of interacting objects, while in functional programming a program can be thought of
as a sequence of stateless function evaluations. When programming computers or
systems with many processors, process-oriented programming allows programmers
to think about applications as sets of concurrent processes acting upon logically
shared data structures.
http://people.cs.aau.dk/~normark/prog3-03/html/notes/paradigms_themes-paradigm-overview-section.html
-
9
2.7.1 Overview of the imperative paradigm
First do this and next do that
The 'first do this, next do that' is a short phrase which really in a nutshell describes the spirit of the imperative paradigm. The basic idea is the command, which has a measurable effect on the program state. The phrase also reflects that the order to the commands is important. 'First do that, then do this' would be different from 'first do this, then do that'.
In the itemized list below we describe the main properties of the imperative paradigm.
Characteristics: Discipline and idea
Digital hardware technology and the ideas of Von Neumann Incremental change of the program state as a function of time. Execution of computational steps in an order governed by control
structures We call the steps for commands
Straightforward abstractions of the way a traditional Von Neumann computer works
Similar to descriptions of everyday routines, such as food recipes and car repair
Typical commands offered by imperative languages Assignment, IO, procedure calls
Language representatives Fortran, Algol, Pascal, Basic, C
The natural abstraction is the procedure Abstracts one or more actions to a procedure, which can be
called as a single command. "Procedural programming"
We use several names for the computational steps in an imperative language. The word statement is often used with the special computer science meaning 'a elementary instruction in a source language'. The word instruction is another possibility; We prefer to devote this word the computational steps performed at the machine level. We will use the word 'command' for the imperatives in a high level imperative programming language.
A procedure abstracts one or more actions to a procedure, which can be activated as a single action.
2.7.2 Overview of the functional paradigm
We here introduce the functional paradigm at the same level as imperative
programming was introduced in Section 2.7.1. Functional programming is in many
-
10
respects a simpler and more clean programming paradigm than the imperative one.
The reason is that the paradigm originates from a purely mathematical discipline: the
theory of functions. As described in Section 2.7.1, the imperative paradigm is rooted
in the key technological ideas of the digital computer, which are more complicated,
and less 'clean' than mathematical function theory.
Below we characterize the most important, overall properties of the functional programming paradigm. Needless to say, we will come back to most of them in the remaining chapters of this material.
Evaluate an expression and use the resulting value for something
Characteristics: Discipline and idea
Mathematics and the theory of functions The values produced are non-mutable
Impossible to change any constituent of a composite value As a remedy, it is possible to make a revised copy of
composite value Atemporal
Time only plays a minor role compared to the imperative paradigm
Applicative All computations are done by applying (calling) functions
The natural abstraction is the function Abstracts a single expression to a function which can be
evaluated as an expression Functions are first class values
Functions are full-fledged data just like numbers, lists, ... Fits well with computations driven by needs
Opens a new world of possibilities
2.7.3 Overview of the logic paradigm
The logic paradigm is dramatically different from the other three main programming paradigms. The logic paradigm fits extremely well when applied in problem domains that deal with the extraction of knowledge from basic facts and relations. The logical paradigm seems less natural in the more general areas of computation.
Answer a question via search for a solution
Below we briefly characterize the main properties of the logic programming paradigm.
-
11
Characteristics: Discipline and idea
Automatic proofs within artificial intelligence Based on axioms, inference rules, and queries. Program execution becomes a systematic search in a set of facts,
making use of a set of inference rules
2.7.4 Overview of the object-oriented paradigm
The object-oriented paradigm has gained great popularity in the recent decade. The primary and most direct reason is undoubtedly the strong support of encapsulation and the logical grouping of program aspects. These properties are very important when programs become larger and larger.
The underlying, and somewhat deeper reason to the success of the object-oriented paradigm is probably the conceptual anchoring of the paradigm. An object-oriented program is constructed with the outset in concepts, which are important in the problem domain of interest. In that way, all the necessary technicalities of programming come in second row.
Send messages between objects to simulate the temporal evolution of a set of real
world phenomena
As for the other main programming paradigms, we will now describe the most important properties of object-oriented programming, seen as a school of thought in the area of computer programming.
Characteristics: Discipline and idea
The theory of concepts, and models of human interaction with real world phenomena
Data as well as operations are encapsulated in objects Information hiding is used to protect internal properties of an object Objects interact by means of message passing
A metaphor for applying an operation on an object In most object-oriented languages objects are grouped in classes
Objects in classes are similar enough to allow programming of the classes, as opposed to programming of the individual objects
Classes represent concepts whereas objects represent phenomena
Classes are organized in inheritance hierarchies Provides for class extension or specialization
3 References:
-
12
[1] Knuth, Donald, Fundamental Algorithm: The Art of Computer Programming, Addison-Wesley, Computer Science and Information Processing, 1968.
[2] Tim Pengajar CS1013 Pengantar Teknik Informatika, Introduction to Algorithms and Efficiency, IT Telkom, 2009.
[3] Tim Pengajar CS1013 Pengantar Teknik Informatika Introduction to Algorithmic Strategies, IT Telkom, 2009.
[4] Fundamental Algorithm, http://www.cse.tum.de/vtc/FundAlg/ diakses Maret 2013
[5] Nrmark, Kurt. Overview of the four main programming paradigms. Aalborg University, 9 May 2011. Retrieved 22 September 2012
[6] Skiena Steven, Lecture 2: Asymtotic Notatio, Department of Computer Science State University of New York.