Chapter 8 Loops while loop syntax while ;... end;.

Slides:



Advertisements
Similar presentations
Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
Advertisements

Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Bar Ilan University And Georgia Tech Artistic Consultant: Aviya Amir.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
Lecture 12 Oct 15 Recursion – more examples Chapter 8 problems and solutions Cell arrays, Chapter 10.
Chapter 8 Loops while loop syntax while ;... end;.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Lec 5 Feb 10 Goals: analysis of algorithms (continued) O notation summation formulas maximum subsequence sum problem (Chapter 2) three algorithms image.
Algorithm Efficiency and Sorting
Concept of Basic Time Complexity Problem size (Input size) Time complexity analysis.
Lecture 2: Basic Information Theory Thinh Nguyen Oregon State University.
Fundamentals of Multimedia Chapter 7 Lossless Compression Algorithms Ze-Nian Li and Mark S. Drew 건국대학교 인터넷미디어공학부 임 창 훈.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Lecture 18 Oct 24 Cell arrays (Ch 4), structures recursion (Ch 12)
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
Chapter 8 Loops while loop syntax while ;... end;.
1 Lossless Compression Multimedia Systems (Module 2) r Lesson 1: m Minimum Redundancy Coding based on Information Theory: Shannon-Fano Coding Huffman Coding.
Management Information Systems Lection 06 Archiving information CLARK UNIVERSITY College of Professional and Continuing Education (COPACE)
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
Analysis of Algorithms
Lecture 4 on Data Structure Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Searching : Linear search Searching refers to the operation of finding.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Lossless Compression CIS 465 Multimedia. Compression Compression: the process of coding that will effectively reduce the total number of bits needed to.
CSC 211 Data Structures Lecture 13
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 8. Greedy Algorithms.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Data Structure Introduction.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Additive White Gaussian Noise
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
Lecture 13 Oct 15, 2012 cell array (review) Ch 4 recursion (Ch 12)
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Chapter Topics Chapter 16 discusses the following main topics:
Sort & Search Algorithms
Chapter 15 Recursion.
Decrease-and-Conquer Approach
Chapter 15 Recursion.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Chapter 7 Sorting Spring 14
Algorithm Analysis CSE 2011 Winter September 2018.
13 Text Processing Hongfei Yan June 1, 2016.
Topic: Divide and Conquer
CS 201 Fundamental Structures of Computer Science
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Divide and Conquer Algorithms Part I
And now for something completely different . . .
Presentation transcript:

Chapter 8 Loops while loop syntax while ;... end;

Example 1: We wrote a function insert that inserts A(k) in the array A(1:k – 1). Now we will write the function insertionsort to call insert iteratively. function insert(A, k) % assume k > 1 and A(1:k-1)is sorted temp = A(k); j = k-1; while (j > 0) if A(j) > temp A(j+1) = A(j); j = j – 1; else break; end; A(j+1) = temp;

Insertion sorting function B = insertionSort(A) j = 2; while (j <= length(A)) B = insert(A, j); j = j + 1; end; Comparison between insertion sorting and selection sorting In the best case, insertion sorting is much faster than selection sorting. On average, insertion sorting performs half as many comparisons as selection sorting. Selection sorting is much better than insertion sorting in the number of data movements.

for loop syntax for j = 1:k end

An example involving image processing Median filter: Suppose an image is corrupted by a random noise that randomly changes a small number (say 10%) of pixels to arbitrary value. How can we remove this noise? Sample input and output:

Median filter design We should avoid trying to determine which pixels are corrupted. (too difficult.) Instead, replace the content of each pixel by some combined value of the neighbors. Good pixel: likely surrounded by nearly identical values, so replacement does not hurt. Bad pixel: most (or all) neighbors are good so replacement removes the noise! How to combine the neighbor values? mean filter median filter

Median filter implementation function out = medianFilter(img) [row, col, num]= size(img); for i = 2:row - 1 for j = 2:col-1 lst1 = sort([img(i-1,j-1,1), img(i-1,j,1), img(i-1,j+1,1), img(i, j-1,1), img(i,j,1), img(i, j+1,1), img(i+1, j-1,1), img(i+1,j,1), img(i+1, j+1,1)]); out(i,j,1) = lst1(5); lst1 = sort([img(i-1,j-1,2), img(i-1,j,2), img(i-1,j+1,2), img(i, j-1,2), img(i,j,2), img(i, j+1,2),img(i+1, j-1,2), img(i+1,j,2), img(i+1, j+1,2)]); out(i,j,2) = lst1(5); lst1 = sort([img(i-1,j-1,3), img(i-1,j,3), img(i-1,j+1,3), img(i, j-1,3), img(i,j,3), img(i, j+1,3), img(i+1, j-1,3), img(i+1,j,3), img(i+1, j+1,3)]); out(i,j,3) = lst1(5); end;

Simulating the outcome of a probability game Players A and B play the following game. A fair coin is tossed until one of the two patterns HTH or THT occurs. If the first pattern occurs, A wins else B wins. What is the probability that A wins the game? There are two ways to solve this problem: Use Markov chain (can find the answer exactly. Will be studied later.) Simulation (simulate the experiment a large number of times and record the number of wins by each player)

Simulation based solution Outer loop: repeat the game a large number of times, say Inner loop: each iteration represents one game. Outline of solution: countA = 0; countB = 0; for j = 1: while the game is not over toss coin once; update the last three bits; end; update the winner count; end;

We will write three functions: toss()  produces 1 or 2 with probability 0.5 tossgame()  produces 1 (2) if A (B) is the winner of one randomly simulated game repeatTossGame(n)  repeat the tossgame n times, find a = the number of times A wins and output the ratio a/n

Recursive functions Before we conclude this chapter, we will discuss recursive functions, those that can call themselves. We have examples of functions that call other functions, e.g. insertionsort calling insert etc. If f(n) is defined in terms of f(n – 1), as for example, in the case of factorial, why not let f call itself? n! = n x (n – 1)! Or in matlab: fact(n) = n.* fact(n – 1)

Rules for recursive functions 1. there should be exit from recursion. (i.e., there should be some conditional branch path in which there is no recursive call). Such cases are called the base cases. 2. recursive calls should make towards base case (usually by calling itself with smaller input values). 3. Recursion may be more time or memory consuming so should be careful with their use.

Example 1: Write a recursive function to compute n! function out = fact(n) if n <= 1 out = 1; else out = n.* fact(n-1); end;

Example 2: Write a recursive function in Matlab to perform binary search. Assume array A is sorted in ascending order. Search(A, low, high, x) will return the largest t such that A(t) <= x. Pre-condition: A(low) <= x <= A(high) Thus low <= t <= high. Initially, low = 1, high = size(A)

Recursive binary search program function out = search(A, low, high, x) % returns the largest t s.t. A(t) <= x where low <= t <= high % A is assumed to be sorted if high - low == 1 if A(high) == x out = high; else out = low; end; else mid = floor((low + high)/2); if A(mid) == x out = mid; elseif A(mid) < x out = search(A, mid + 1, high, x); else out = search(A, low, mid - 1, x); end;

A recursive image tiling problem Given an input image as in the left-side, we want to produce the image on the right-side.

Solution to the recursive image tiling problem Observation 1: 3 rd quadrant has the input image, but shrunk into quarter. We need to write a function shrink that shrinks the image to quarter size. Quadrant 1 of the output is the shrunk version of the whole output. Equivalently, if F is the function that outputs the image on the right when given as input on the left, then F applied to the image on the 3 rd quadrant of the output is what goes into the 1 st quadrant of the output. Quadrants 2 and 4 are now easy to determine. They can be obtained by copying a part of the image in quadrant 1.

Some exercises in Chapter 8 and solutions Exercise 8.1. The input is a two-column matrix. The first column gives the number and the second column gives the number of repetitions. You are to create an output vector that matches these numbers. For example: >> repeat([2 3; 3 1; 4 2] ans = [ ]

Exercise 8.1. >> repeat([2 3; 3 1; 4 2] ans = [ ] function out = repeat1(m) out = []; [r, c] = size(m); for i = 1 : r %Go through each row for j = 1 : m(i, 2) %Repeat the number of times specified in the second column out(end + 1) = m(i, 1); end

Exercise 8.4. Write a function myfind, that mimics the behavior of the built-in function find. That is, it takes as input a boolean vector of 1’s and 0’s.

>> A = [9, 11, 8, 6, 11, 12]; >> myfind(A, 11) ans = [ ]

Exercise 8.4. Write a function myfind, that mimics the behavior of the built-in function find. That is, it takes as input a boolean vector of 1’s and 0’s. function out = myfind(A, x) out = []; for j = 1:length(A) if A(j) == x out = [out, j]; end;

Exercise 8.8 Write a function intoBits to take an integer number as input and output a string of 0’s and 1’s representing the number in base 2. >> intoBits(12) ans = 1100

Exercise 8.11 Write functions minAll, minCol and minRow that find the overall minimum, column minima and row minima of a matrix A.

Exercise 8.11 Write functions minAll, minCol and minRow that find the overall minimum, column minima and row minima of a matrix A. function res = minAll(m) [r, c] = size(m); res = m(1, 1); %Set up the first temporary minimum for i = 1 : r %Going through each row for j = 1 : c %Going throuch each column if res > m(i, j) %Updating if necessary res = m(i, j) end

Exercise 8.11 Write functions minAll, minCol and minRow that find the overall minimum, column minima and row minima of a matrix A. function res = minRow(m) [r, c] = size(m); res = zeros(1, r); %Set up res for i = 1 : r %Going through each row res(i) = m(i, 1); %Set up the first temporary minimum for row i for j = 2 : c %Going throuch each column if res(i) > m(i, j) %Updating if necessary res(i) = m(i, j); end

Project – entropy computation The concept of entropy is fundamental to information and coding theory – which deals with efficient ways to add redundancy to the data so that the receiver can recover the message even if some of data is corrupted by the channel. Information theory was developed by Claude Shannon. Watch movie about Shannon in UCTV.

probability distribution of individual letters The basic idea of information theory is that if the input has less randomness, it can be more easily predicted and hence it can be compressed to greater degree. Suppose there are k distinct symbols in a text. Let P(j-th symbol) = p j. Entropy is defined as: E = –  j  p j log p j Goal is to compute the entropy of a given text.

Example: Given a three-letter alphabet containing E, Z and _ with frequencies as follows: E Z _ Entropy is given by: E = -0.5 log – 0.25 log – 0.25 log Using matlab, we can compute E as: >> p = [ ] p = >> -sum(p.*log2(p)) ans = >>

Project: Write a program in Matlab that computes the entropy of English. Step 1: collect a corpus of text. count the frequency of letters for the corpus. text will be stored in multiple files program should calculate the frequency of letters across all the files. Step 2: compute entropy from the frequency table.