Data Structures and Algorithms (DSA) Course 13 Algorithms

Similar documents
Data Structures and Algorithms (DSA) Course 12 Trees & sort. Iulian Năstac

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

DESIGN AND ANALYSIS OF ALGORITHMS

Algorithm classification

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

O(n): printing a list of n items to the screen, looking at each item once.

Algorithms and Data Structures

For 100% Result Oriented IGNOU Coaching and Project Training Call CPD: ,

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

Algorithms Interview Questions

INTRODUCTION TO ALGORITHMS

Introduction to Algorithms

Overview of Data. 1. Array 2. Linked List 3. Stack 4. Queue

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

END-TERM EXAMINATION

Comparison Sorts. Chapter 9.4, 12.1, 12.2

CAD Algorithms. Categorizing Algorithms

Algorithmic Analysis. Go go Big O(h)!

The divide and conquer strategy has three basic parts. For a given problem of size n,

Introduction to Data Structure

Chapter 2: Complexity Analysis

Computer Algorithms. Introduction to Algorithm

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Measuring algorithm efficiency

Algorithms. Lecture Notes 5

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red.

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

CS-6402 DESIGN AND ANALYSIS OF ALGORITHMS

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

PRAM Divide and Conquer Algorithms

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Introduction to Computer Science

Subset sum problem and dynamic programming

Keywords: Binary Sort, Sorting, Efficient Algorithm, Sorting Algorithm, Sort Data.

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Data Structures and Algorithms

CS6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK UNIT I

Lecture 2: Algorithm Analysis

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

CS301 - Data Structures Glossary By

Lecture 3, Review of Algorithms. What is Algorithm?

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Classic Data Structures Introduction UNIT I

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: CS6402- Design & Analysis of Algorithm Year/Sem : II/IV UNIT-I INTRODUCTION

CS:3330 (22c:31) Algorithms

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Analysis of Algorithms Part I: Analyzing a pseudo-code

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

Lecture 5: Sorting Part A

COMP Data Structures

Sorting L7.2 Recall linear search for an element in an array, which is O(n). The divide-and-conquer technique of binary search divides the array in ha

6. Algorithm Design Techniques

Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Algorithms and Data Structures

CSE 373: Data Structures and Algorithms

Sorting Pearson Education, Inc. All rights reserved.

DDS Dynamic Search Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

CSE 373: Data Structures and Algorithms

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

Outline and Reading. Analysis of Algorithms 1

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Binary heaps (chapters ) Leftist heaps

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

Lecture 1. Introduction

Data Structures and Algorithms

CS 61B Asymptotic Analysis Fall 2018

Lecture 3: Sorting 1

Algorithm Analysis and Design

UNIT 1 BASICS OF AN ALGORITHM

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

Advanced Algorithms and Data Structures

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

L.J. Institute of Engineering & Technology Semester: VIII (2016)

Sorting and Searching

Data Structures and Algorithms (DSA) Course 9 Lists / Graphs / Trees. Iulian Năstac

CSCE 321/3201 Analysis and Design of Algorithms. Prof. Amr Goneid. Fall 2016

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms

Data Structures Lecture 8

Goal of the course: The goal is to learn to design and analyze an algorithm. More specifically, you will learn:

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Computational biology course IST 2015/2016

Advanced Algorithms and Data Structures

Introduction to Computer Science

Recitation 9. Prelim Review

Transcription:

Data Structures and Algorithms (DSA) Course 13 Algorithms Iulian Năstac

Special binary trees (Recapitulation) P: A binary tree with the height i could have a maximum number of 2 i+1-1 nodes. Notes: A level with the depth k could have maximum 2 k nodes 2

Complete binary tree (Recapitulation) A binary tree with n nodes that has a height of i is called as being a complete binary tree if it is obtained from a full binary tree with a height of i, in which there are eliminated the last consecutive nodes, numbered with n+1, n+2,... up to 2 i+1-1. 3

Notes: A complete binary tree can be sequentially represented using a vector (noted T), in which the nodes of depth k, from left to right, are inserted in the following positions: : T[2 k ], T[2 k +1],, T[2 k+1-1], excepting the final level, which may be incomplete. 4

Warning: This is a generic vector, which it begins with T [1] (not with T [0], as usual in the C programing). We can make the necessary changes when we will write the 5 code in C.

Notes: The parent of a node from T[i], i>1, can be found in T[i div 2]. The sons of a node from T[i], can be found (if exist) in T[2i] and T[2i + 1]. 6

The heap tree (Recapitulation) A heap is a specialized tree-based data structure that satisfies the heap property: If A is a parent node of B then the key of node A is ordered with respect to the key of node B with the same ordering applying across the heap. 7

Example: This heap can be represented by the following vector: 10 7 9 4 7 5 2 2 1 6 T[1] T[2] T[3] T[4] T[5] T[6] T[7] T[8] T[9] T[10] 8

Notes: In a heap tree we can make modifications at the node level (changing the value of the current node). Thus the value of a node can be increased or decreased, resulting a canceling of the specific order inside the heap tree. The order of the heap can be simply restored through two operations called sift-down and sift-up. 9

How can we create a heap from an unordered vector T[1..n]? A less effective solution is to start from a heap of one element and add items one by one. slow_make_heap(t[1..n]) { for (i=2; i n; i++) sift_up (T[1..i], i); } 10

But there is another linear algorithm that works better (in terms of order / efficiency): make_heap(t[1..n]) { for (i = n div 2; i 1; i - -) sift_down (T[ ], i); } 11

Example: Starting from the following vector (which is not a heap): 1 6 9 2 7 5 2 7 4 10 T[1] T[2] T[3] T[4] T[5] T[6] T[7] T[8] T[9] T[10] Through a sequence of few steps we can obtain: 10 7 9 4 7 5 2 2 1 6 T[1] T[2] T[3] T[4] T[5] T[6] T[7] T[8] T[9] T[10] 12

The main application of the heap concept: A new sorting technique (heapsort) Theheapsort algorithm was invented by J. W. J. Williams in 1964. In the same year, R. W. Floyd published an improved version that could sort an array in-place, continuing his earlier research into the treesort algorithm. 13

heapsort (T[1..n]) { make_heap(t); for( i = n; i 2; i - -) { T[1] T[i]; sift_down (T[1 i-1], 1) } } 14

Other ordering (or sorting) algorithms: Insertion Selection Merge sort Quicksort etc. 15

Ordering methods Ordering = arranging items of the same kind, class or nature, in an ordered sequence. For this purpose we consider that the data are a collection of items of a certain type and each item comprises one or even more values (variables), which are decisive in the ordering that is performed. Such a value is called key. 16

For the C programming language, a sorting algorithm can be achieved by one of the following methods: 1. Arranging data (which are sorted) so that their keys will finally correspond to the desired order. 2. By ordering an array of pointers (to the data that must be sorted) in order to form an ordered set. 17

Note: In the following we will discuss only about sorting unidimensional arrays (vectors) with numerical data. 18

Insertion sorting algorithm (Recapitulation) Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. The general idea of sorting by insertion is to consider (at a time) each element of the array and insert it into the substring previously ordered. The operation involves a growing sequence, which is moved to the right. 19

20

A brief description of the algorithm in pseudocode is as follows: procedure insert(t[1..n]) { local variables i, j, x for i 2 to n do { x T[i] j i -1 while (j>0 and x<t[j]) do { T[j+1] T[j] j j 1 } T[j+1] x } } Note: In implementing the above algorithm in C code, we have to keep in mind that a vector is starting with an zero index. 21

The selection sorting algorithm (Recapitulation) Selection sort is a sorting algorithm, specifically an in-place comparison sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited. 22

23

A brief description of the algorithm in pseudocode is as follows: procedure select(t[1..n]) { local variables i, j, min_j, min_x, for i 1 to n-1 do { min_j i min_x T[i] for j i+1 to n do if T[j] < min_x then { min_j j min_x T[j] } T[min_j] T[i] T[i] min_x } } Note: In implementing the above algorithm in C code, we have to keep in mind that a vector is starting with an zero index. 24

Bubblesort algorithm (Recapitulation) Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. 25

Shellsort algorithm (Recapitulation) Shellsort is an in-place comparison sort. It can be seen as either a generalization of sorting by exchange (bubble sort) or sorting by insertion (insertion sort). The method starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared. Starting with far apart elements can move some out-of-place elements into position faster than a simple nearest neighbor exchange. Donald Shell published the first version of this 26 sort in 1959.

The running time of Shellsort is heavily dependent on the gap sequence it uses. For many practical variants, determining their time complexity remains an open problem. 27

Shell sort method can be defined as in the following : 1) Start with a gap = n/2, where n is the number of the elements that will be sorted. 2) Make a crossing of the vector of items that are sorted. 3) The gap it halves gap = gap/2. 4) If gap > 0, then jump to step 2, otherwise the algorithm stops. 28

Note Each crossing through the elements involves the following substeps: 1) i = gap. 2) j = i gap + 1. 3) If j > 0 and the elements from the positions: j and j+gap are not ordered, then we will interchange their values. Otherwise jump to substep 6. 4) j = j gap. 5) Jump to substep 3. 6) i = i + 1. 7) If i > n, the crossing is stopped. Otherwise jump to substep 2. 29

Notes: In implementing the above algorithm in C code, we have to remember that a vector is starting with the zero index. Therefore, the initialization from the substep 2) becomes: j = i gap. 30

A general perspective In computer science an algorithm is a selfcontained step-by-step set of operations to be performed. Algorithms perform calculation, data processing, and/or automated reasoning tasks. Practically, there is an endless variety of algorithms (but it is quite useful to classify them - see related books, written by Donald Knuth). 31

An algorithm can be easily expressed by using a flow chart An algorithm is an effective method that can be expressed within a finite amount of space and time and in a well-defined formal language for calculating a function. Starting from an initial state and initial input (perhaps empty), the instructions describe a computation that, when executed, proceeds through a finite number of well-defined successive states, eventually producing "output" and terminating at a final ending state. The transition from one state to the next is not necessarily deterministic; some algorithms, known as randomized algorithms, incorporate random input. 32

Other classical algorithms Divide and conquer algorithms Divide and conquer (D&C) is an algorithm design paradigm based on multi-branched recursion. A divide and conquer algorithm works by recursively breaking down a problem into two or more sub-problems of the same (or related) type, until these become simple enough to be solved directly. The solutions to the sub-problems are then combined to give a solution to the original problem. Greedy algorithm A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum. In many problems, a greedy strategy does not in general produce an optimal solution, but nonetheless a greedy heuristic may yield locally optimal solutions that approximate a global optimal solution in a reasonable time. 33

Divide and conquer algorithm This technique is the basis of efficient algorithms for all kinds of problems, such as sorting (e.g. quicksort, merge sort, etc.), multiplying large numbers, syntactic analysis (e.g., top-down parsers), and computing the discrete Fourier transform (DFT). The name "divide and conquer" is sometimes applied also to algorithms that reduce each problem to only one sub-problem, such as the binary search algorithm for finding a record in a sorted list (or its analog in numerical computing, the bisection algorithm for root finding). 34

Greedy algorithm A greedy strategy for the traveling salesman problem (which is of a high computational complexity) is the following heuristic: "At each stage visit an unvisited city nearest to the current city". This heuristic need not find a best solution, but terminates in a reasonable number of steps; finding an optimal solution typically requires unreasonably many steps. In mathematical optimization, greedy algorithms solve combinatorial problems (of NP complexity). 35

Notes: We can make whatever choice seems best at the moment and then solve the subproblems that arise later. The choice made by a greedy algorithm may depend on choices made so far but not on future choices or all the solutions to the subproblem. It iteratively makes one greedy choice after another, reducing each given problem into a smaller one. In other words, a greedy algorithm never reconsiders its choices. This is the main difference from dynamic programming, which is exhaustive and is guaranteed to find the solution. After every stage, dynamic programming makes decisions based on all the decisions made in the previous stage, and may reconsider the previous stage's algorithmic path to solution. 36

Warning! Greedy algorithms mostly (but not always) fail to find the globally optimal solution, because they usually do not operate exhaustively on all the data. They can make commitments to certain choices too early which prevent them from finding the best overall solution later. For example, all known greedy coloring algorithms for the graph coloring problem and all other NP-complete problems do not consistently find optimum solutions. Nevertheless, they are useful because they are quick to think up and often give good approximations to the optimum. 37

With a goal of reaching the largest-sum, at each step, the greedy algorithm will choose what appears to be the optimal immediate choice, so it will choose 12 instead of 3 at the second step, and will not reach the best solution, which contains 99. 38

Perspective Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible. They are also used in machine learning, business intelligence (BI), and artificial intelligence (AI). 39

Backtracking Backtracking is a general algorithm for finding all solutions of a problem of calculation algorithm that is based on building incremental candidate solutions, each candidate partially abandoned as soon as it becomes clear that he has no chance to be a valid solution. See the last laboratory http://www.euroqual.pub.ro/wp-content/uploads/sda_lab_06_backtracking.pdf 40

Analysis of algorithms efficiency The analysis of algorithms is the determination of the amount of resources, which are necessary to execute them. 41

Resources mean: The memory space required for storing the data, which are being processed by the algorithm. The time required for execution of all specified processes of the algorithm. 42

The study of an algorithm includes several aspects: Designing Implementation Validation Testing: Debugging Traceability refers to the ability to link all product requirements back to the designing plan (relating to code, and test cases). Efficiency evaluation 43

Testing Problem It can be particularly difficult for large software, with many lines of codes. Usually, the cases used to test can not cover all possible situations. It requires considerable time and significant computation resources. 44

Program testing can at best show the presence of errors but never their absence Nothing is as expensive as making mistakes. Edsger Wybe Dijkstra 45

The efficiency of algorithms Ideally, for a given problem, is to find several algorithms and then choose the best one among them i.e. to find the most efficient algorithm at the current time (using available resources). 46

The algorithmic efficiency is very related to the amount of resources used by that algorithm. 47

An algorithm can be analyzed in two ways: Posteriori (empirical) - analyze the behavior after the implementation of an algorithm, by running on different cases. A priori (theoretically) - before implementing and implies quantitative determination of resources (time, memory, etc.) 48

Notes: The big downside of the posteriori analysis: practically, an algorithm cannot be tested for any possible extreme cases. A priori analysis: does not depend on computer or computing language saves time (which could be spent on programming, especially when running an inefficient algorithm) it allows to study the effectiveness for the cases of any size 49

Improving the computing speed Changing an old computer (with a new one, more powerful) may lead to solving a problem 100 times faster (for the same inputs) but this increasing is only a linear one only an efficient algorithm can dramatically improve the solving of a problem with huge inputs. 50

In the following we will be focused only on the time parameter The major objective of efficiency is considered the execution time. Theoretically, other necessary resources (such as the involved memory) may be similarly estimated. 51

Definition: We say that an algorithm requires a time of the order of t if there is a positive constant c, and an implementation (program code) of the algorithm, capable of solving the problem in each case not more than ct(n) seconds, where n is the size of the case under consideration. 52

Note: The c constant actually depends of the CPU's clock frequency. 53

Asymptotic notation We note with: R + - the complete set of all real positive numbers N - the complete set of all natural numbers Considering f : N R + as an arbitrary function, we can define the O notation (or Big-O notation): * * O( f ) { t : N R ( c R ) and ( n0 N ) so for ( n n0 ) weobtain t( n) c f ( n)} 54

Notes: - The Big-O notation is the formal method of expressing the upper bound of an algorithm's running time. It's a measure of the longest amount of time it could possibly take for the algorithm to complete. - More formally, for non-negative functions, f(n) and g(n), if there exists an integer and a constant c >0 such that for all integers, f(n) cg(n), then f(n) is Big O of g(n). This is denoted as "f(n) = O(g(n))". If graphed, g(n) serves as an upper bound to the curve you are analyzing, f(n). 55

The principle of invariance Two different implementations of the same algorithm do not differ (in theirs efficiencies) by more than a multiplicative constant. Example: With two different implementations, requiring t 1 (n) seconds, and t 2 (n) seconds respectively, to solve a problem of size n, it results that: c R * so that t 1 ( n) c t ( n) n n 2 0 56

Note: t O(t) Usually we are looking for the simplest function f so that: to(f) 57

58 It is obvious that: n O(n), n 2 O(n 2 ), n 2 O(n 3 ), n 3 O(n 2 ) We can say that: ) ( ) ( : 2 2 * n O f n O f if R N f ) (2 2 ) ( : * n f O n O f if R N f

We can define relations for partial order: f g if O(f) O(g) f < g if O(f) O(g) 59

Properties: P1: Transitivity If f O(g) and g O(h) then f O(h) P2: If f O(g) then O(f) O(g) 60

P3: f and g : N R a) O(f) = O(g) f O(g) and g O(f) * we obtain : b) O(f) O(g) f O(g) and g O(f) O P4: * ( f g) O(max( f, g)) for f and g : N R where the sum and the product, also, are punctualy considered. 61

We can define the equivalence relation as: f g if O(f) = O(g) Note: In the notation O(f) we can replace f with other equivalent function. 62

Example: lg(n) ln(n) log 2 (n) or O(lg(n))=O(ln(n))=O(log 2 n) This is obvious, since it is always possible to find a constant c so that: lg(n) cln(n) Since: log a x log log b b x a it result that: c ln( n) lg( n) ln(10) ln( n) ln( n) 1 ln(10) 63

If we denote by O(1) the higher order of the functions bounded by a constant, then we can obtain the hierarchy: O(1) O(logn) O( n) O( n logn) O( n ) O( n )... O(2 2 3 n ) This hierarchy corresponds to a performance criterion. For a given problem, we always want to obtain a corresponding algorithm with its order as much as possible to the left side in the specified hierarchy. 64

Example: n 3 + 3n 2 + n + 8 O(n 3 + (3n 2 + n + 8) ) = O(max(n 3,(3n 2 + n + 8))) = O(n 3 ) Note: max(n 3,(3 n 2 + n + 8)) will be n 3 for a number, which is sufficient great (since the increase of the chart for n 3 is faster than any second-degree polynomial even if it is started from lower values). 65

Algorithms analysis techniques There isn't a general formula for analyzing the efficiency of algorithms. This is a matter of judgment, intuition and experience. The flowchart may be useful in order to find the Big-O of an algorithm. 66

Selection sort 67

- The time for a single execution of the inner loop can be upper bounded by an a constant. - For a given i, the inner loop needs a time frame of not more than b + a(n - i) units where b is a constant that denotes the loop initialization. - A single execution of the external loop needs no more than where c is other constant. c + b + a(n - i) units Finally, the total time for the algorithm is no more than: T n 1 max d ( c b a( n i)) i1 units 68

69 d n b c i a n a T n i n i 1) ( ) ( 1 1 1 1 1 max but: 2 2 2 1) ( 1) 1 ( 2 1 1 n n n n i n i and 1 1 1 1 n n i It result: b c d n a c b n n T a a ) 1 ( ) ( 2 2 2 max Therefore the insertion sorting algorithm requires a runtime of O(n 2 )