Introduction to Algorithms. Theerayod Wiangtong EE Dept., MUT. Contents. Introduction Divide and conquer, recurrences Sorting Trees: binary tree Dynamic programming Graph algorithms. General. Algorithms are first solved on paper and later keyed in on the computer.

Download Presentation

Introduction to Algorithms

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.

Contents • Introduction • Divide and conquer, recurrences • Sorting • Trees: binary tree • Dynamic programming • Graph algorithms

General • Algorithms are first solved on paper and later keyed in on the computer. • To solve problems we have procedures, recipes, process descriptions – in one word algorithms. • The most important thing is to be • Simple • Precise • Efficient

History • First algorithm: Euclidean Algorithm to find greatest common divisor, 400-300 B.C. • 19th century – Charles Babbage, Ada Lovelace. • 20th century – John von Neumann, Alan Turing.

Data Structures and Algorithms • Data structure • Organization of data to solve the problem at hand. • Algorithm • Outline, the essence of a computational procedure, step-by-step instructions. • Program • implementation of an algorithm in some programming language.

Robustness Adaptability Correctness Efficiency Reusability Overall Picture Data Structure and Algorithm Design Goals • Using a computer to help solve problems. • Precisely specify the problem. • Designing programs • architecture • algorithms • Writing programs • Verifying (testing) programs Implementation Goals

Algorithmic Solution • Algorithm describes actions on the input instance. • There may be many correct algorithms for the same algorithmic problem. Input instance, adhering to the specification Output related to the input as required Algorithm

An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time. Properties: Precision Determinism Finiteness Efficiency Correctness Generality Definition of an Algorithm

How to Develop an Algorithm • Precisely define the problem. Precisely specify the input and output. Consider all cases. • Come up with a simple plan to solve the problem at hand. • The plan is language independent. • The precise problem specification influences the plan. • Turn the plan into an implementation • The problem representation (data structure) influences the implementation.

Example: Searching Solutions • Metaphor: shopping behavior when buying a beer: • search1: scan products; stop as soon as a beer is found and go to the exit. • search2: scan products until you get to the exit; if during the process you find a beer put it into the basket. • search3: scan products; stop as soon as a beer is found and jump out of the window.

Example 1: Searching • OUTPUT • index of number q in sequence A or NIL • INPUT • A - sorted sequence (increasing order) of n (n >0) numbers • q - a single number j a1, a2, a3,….,an; q 2 2 5 4 10 11; 5 2 5 4 10 11; 9 NIL OUTPUT: precise specifications of what the algorithm produces as an output, and how this relates to the input. The handling of special cases of the input should be described. INPUT: precise specifications of what the algorithm gets as an input.

Searching#1 search1 INPUT: A[1..n] – sorted array of integers, q – an integer. OUTPUT: index j such that A[j] = q. NIL if "j (1£j£n): A[j] ¹ q j := 1 while j £nand A[j] ¹ q doj++ if j £nthenreturn j else return NIL • The code is written in an unambiguous pseudo code and INPUT and OUTPUT of the algorithm are specified. • The algorithm uses a brute-force technique, i.e., scans the input sequentially.

Searching, C solution #define n 5 int j, q; int a[n] = { 11, 1, 4, -3, 22 }; int main() { j = 0; q = -2; while (j < n && a[j] != q) { j++; } if (j < n) { printf("%d\n", j); } else { printf("NIL\n"); } } // compilation: gcc -o search search.c // execution: ./search

Searching#2 • Run through the array and set a pointer if the value is found. search2 INPUT: A[1..n] – sorted array of integers, q – an integer. OUTPUT: index j such that A[j] = q. NIL if "j (1£j£n): A[j] ¹ q j := 1; ptr := NIL; for j := 1 to ndo if a[j] = qthen ptr := j return ptr;

Searching#3 • Run through the array and return the index of the value in the array. search3 INPUT: A[1..n] – sorted array of integers, q – an integer. OUTPUT: index j such that A[j] = q. NIL if "j (1£j£n): A[j] ¹ q j := 1; for j := 1 to ndo if a[j] = qthenreturn j return NIL

Efficiency Comparisons • search1 and search3 return the same result (index of the first occurrence of the search value). • search2 returns the index of the last occurrence of the search value. • search3 is most efficient, it does not finish the loop (as a general rule it is good to avoid this).

Sorting • Sorting is a classical and important algorithmic problem. • We look at sorting arrays (in contrast to files, which restrict random access). • A key constraint is the efficient management of the space • In-place sorting algorithms • The efficiency comparison is based on the number of comparisons (C) and the number of movements (M).

Sorting • Simple sorting methods use roughly n * n comparisons • Insertion sort • Selection sort • Bubble sort • Fast sorting methods use roughly n * log n comparisons. • Merge sort • Heap sort • Quicksort

Example 2: Sorting OUTPUT a permutation of the input sequence of numbers INPUT sequence of n numbers Sort b1,b2,b3,….,bn a1, a2, a3,….,an 2 45 7 10 2 5 4 10 7 • Correctness (requirements for the output) • For any given input the algorithm halts with the output: • b1 < b2 < b3 < …. < bn • b1, b2, b3, …., bnis a permutation ofa1, a2, a3,….,an

3 4 6 8 9 Insertion Sort A 7 2 5 1 1 j n i A 44 55 12 42 94 18 06 67 44 55 12 42 94 18 06 67 12 44 55 42 94 18 06 67 12 42 44 55 94 18 06 67 12 42 44 55 94 18 06 67 12 18 42 44 55 94 06 67 06 12 18 42 44 55 94 67 06 12 18 42 44 55 67 94 • Strategy • Start with one sorted card. • Insert an unsorted card at the correct position in the sorted part. • Continue until all unsorted cards are inserted/sorted.

Insertion Sort/2 INPUT: A[1..n] – an array of integers OUTPUT: a permutation of A such that A[1]£ A[2]£ …£A[n] forj := 2 to ndo key := A[j] i := j-1 while i > 0 and A[i] > key do A[i+1] := A[i]; i-- A[j+1] := key

1 2 3 4 5 Selection Sort A 7 8 9 6 1 j n i • Strategy • Start empty handed. • Enlarge the sorted part by switching the first element of the unsorted part with the smallest element of the unsorted part. • Continue until the unsorted part consists of one element only. A 44 55 12 42 94 18 06 67 06 55 12 42 94 18 44 67 06 12 55 42 94 18 44 67 06 12 18 42 94 55 44 67 06 12 18 42 94 55 44 67 06 12 18 42 44 55 94 67 06 12 18 42 44 55 94 67 06 12 18 42 44 55 67 94

Selection Sort/2 INPUT: A[1..n] – an array of integers OUTPUT: a permutation of A such that A[1]£ A[2]£ …£A[n] forj := 1 to n-1do key := A[j]; ptr := j for i := j+1to n do if A[i] < key then ptr := i; key := A[i]; A[ptr] := A[j]; A[j] := key

1 2 3 4 5 Bubble Sort A 7 9 8 6 1 j n i • Strategy • Start from the back and compare pairs of adjacent elements. • Switch the elements if the larger comes before the smaller. • In each step the smallest element of the unsorted part is moved to the beginning of the unsorted part and the sorted part grows by one. A 44 55 12 42 94 18 06 67 06 44 55 12 42 94 18 67 06 12 44 55 18 42 94 67 06 12 18 44 55 42 67 94 06 12 18 42 44 55 67 94 06 12 18 42 44 55 67 94 06 12 18 42 44 55 67 94 06 12 18 42 44 55 67 94

Bubble Sort/2 INPUT: A[1..n] – an array of integers OUTPUT: a permutation of A such that A[1]£ A[2]£ …£A[n] forj := 2 to ndo for i := n to j do if A[i-1] < A[i] then key := A[i-1]; A[i-1] := A[i]; A[i]:=key

Divide and Conquer • Principle:If the problem size is small enough to solve it trivially, solve it. Else: • Divide: Decompose the problem into two or more disjoint subproblems. • Conquer: Use divide and conquer recursively to solve the subproblems. • Combine: Take the solutions to the subproblems and combine the solutions into a solution for the original problem.

Merge Sort • Sort an array by • Dividing it into two arrays. • Sorting each of the arrays. • Merging the two arrays. 17 31 96 50 17 31 50 96 85 24 63 45 17 31 96 50 17 24 31 45 50 63 85 96 24 45 63 85 85 24 63 45

Merge Sort Algorithm • Divide: If S has at least two elements put them into sequences S1 and S2. S1 contains the firstén/2ù elements and S2 contains the remaining ën/2û elements. • Conquer: Sort sequences S1 and S2 using merge sort. • Combine: Put back the elements into S by merging the sorted sequences S1 and S2 into one sorted sequence.

Merge Sort: Algorithm MergeSort(l, r) if l < r then m :=ë(l+r)/2û MergeSort(l, m) MergeSort(m+1, r) Merge(l, m, r) Merge(l, m, r) Take the smallest of the two first elements of sequences A[l..m] and A[m+1..r] and put into the resulting sequence. Repeat this, until both sequences are empty. Copy the resulting sequence into A[l..r].