ANALYSIS AND DESIGN OF ALGORITHMS

Slides:



Advertisements
Similar presentations
Review Binary Search Trees Operations on Binary Search Tree
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Introduction to Graphs
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
The Design and Analysis of Algorithms
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Design and Analysis of Algorithms - Chapter 11 Algorithm An algorithm is a.
TK3043 Analysis and Design of Algorithms Introduction to Algorithms.
Important Problem Types and Fundamental Data Structures
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
GRAPH Learning Outcomes Students should be able to:
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org Some of the sides are exported from different sources to.
Introduction to Algorithms
Chapter 2 Graph Algorithms.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Data Structure Introduction.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. Chapter 1 Introduction.
Design and Analysis of Algorithms - Chapter 11 Algorithm b An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining.
UNIT-I INTRODUCTION ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 1:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3 rd ed., Ch. 1 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
LIMITATIONS OF ALGORITHM POWER
Introduction to design and analysis algorithm
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
1 Introduction to design and analysis algorithm. 2.
TK3043 Analysis and Design of Algorithms
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
The Design and Analysis of Algorithms
Chapter 5 : Trees.
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Introduction to the Design and Analysis of Algorithms
Introduction to The Design & Analysis of Algorithms
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
Program based on pointers in C.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Chapter 1.
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Objective of This Course
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Introduction to Data Structures
Lectures on Graph Algorithms: searching, testing and sorting
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Design and Analysis of Algorithms
Chapter 11 Limitations of Algorithm Power
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
3. Brute Force Selection sort Brute-Force string matching
ITEC 2620M Introduction to Data Structures
Design and Analysis of Algorithms
3. Brute Force Selection sort Brute-Force string matching
CSC 380: Design and Analysis of Algorithms
Important Problem Types and Fundamental Data Structures
Introduction to Algorithms
Analysis and design of algorithm
Design and Analysis of Algorithms
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
3. Brute Force Selection sort Brute-Force string matching
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Presentation transcript:

ANALYSIS AND DESIGN OF ALGORITHMS UNIT-I CHAPTER 1: INTRODUCTION

WHAT IS AN 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.

WHY TO STUDY ALGORITHMS ? Theoretical importance The core of computer science Practical importance A practitioner’s toolkit of known algorithms Framework for designing and analyzing algorithms for new problems

NOTION OF ALGORITHM Problem Algorithm “Computer” Input Output

PROPERTIES OF AN ALGORITHM: Finiteness Effectiveness Algorithm Definiteness Output Input Recipe, process, method, technique, procedure, routine,… with following requirements: 1. Finiteness : terminates after a finite number of steps. 2. Definiteness: unambiguously specified. 3. Input: valid inputs are clearly specified. 4. Output: can be proved to produce the correct output given a valid input. 5. Effectiveness: steps are sufficiently simple and basic.

EUCLID’S ALGORITHM Problem: Find gcd(m,n), the greatest common divisor of two nonnegative, not both zero integers m and n Examples: gcd(60,24) = 12 gcd(60,0) = 60

EUCLID’S ALGORITHM Euclid’s algorithm is based on repeated application of equality gcd(m,n) = gcd(n, m mod n) until the second number becomes 0, which makes the problem trivial. Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12

ALGORITHM Euclid(m, n) Step 1 If n = 0, return m and stop; otherwise proceed to Step 2 Step 2 Divide m by n and assign the value of the remainder to r Step 3 Assign the value of n to m and the value of r to n. Go to Step 1. PSEUDOCODE: // computes gcd(m, n) by Euclid’s algorithm // Input: Two nonnegative, not-both-zero intgers m and n // Output: Greatest common divisor of m & n while n ≠ 0 do r ← m mod n m← n n ← r return m

OTHER METHODS FOR COMPUTING gcd(m,n) Consecutive Integer Checking Algorithm Step 1 Assign the value of min{m,n} to t Step 2 Divide m by t. If the remainder of this division is 0, goto Step 3; otherwise, go to Step 4 Step 3 Divide n by t. If the remainder of this division is 0, return the value of t as answer and stop; otherwise, proceed to Step 4 Step 4 Decrease the value of t by 1 and go to Step 2

Consecutive Integer Checking Algorithm Example: gcd(10,6) = 2 t m % t n % t 6 10 % 6 = 4 5 10 % 5 = 0 6 % 5 = 1 4 10 % 4 = 2 3 10 % 3 = 1 2 10 % 2 = 0 6 % 2 = 0 2 is the GCD, since m % t and n % t are zero.

OTHER METHODS FOR COMPUTING gcd(m, n) (CONT…) Middle - school procedure Step 1 Find the prime factors of m. Step 2 Find the prime factors of n. Step 3 Identify all the common factors in the two prime expansions found in step1 and step2 (If P is a common factor occuring Pm and Pn times in m and n respectively, it should be repeated min{Pm,Pn} times). Step 4 Compute the product of all the common factors and return it as gcd(m,n) Example: If m = 60 and n = 24 then 60 = 2 . 2 . 3 . 5 24 = 2 . 2 . 2 . 3 gcd(60,24) = 2 . 2 . 3 =12 Is this an algorithm?

Eratosthenes (ehr-uh-TAHS-thuh-neez) Eratosthenes was the librarian at Alexandria, Egypt in 200 B.C. Note every book was a scroll. Copyright © 2000 by Monica Yuskaitis

Eratosthenes (ehr-uh-TAHS-thuh-neez) Eratosthenes was a Greek mathematician, astronomer, and geographer. He invented a method for finding prime numbers that is still used today. This method is called Eratosthenes’ Sieve. Copyright © 2000 by Monica Yuskaitis

Eratosthenes’ Sieve A sieve has holes in it and is used to filter out the juice. Eratosthenes’s sieve filters out numbers to find the prime numbers. Copyright © 2000 by Monica Yuskaitis

Generating prime numbers using Sieve of Eratosthenes: Example: n = 25 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 3 5 7 9 11 13 15 17 19 21 23 25 2 3 5 7 11 13 17 19 23 25 2 3 5 7 11 13 17 19 23 Note : If P is a number whose multiples are being eliminated, then P.P should not be greater than n, and therefore P cannot exceed √n .

Generating prime numbers using Sieve of Eratosthenes: Example: n = 25 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 3 5 7 9 11 13 15 17 19 21 23 25 2 3 5 7 11 13 17 19 23 25 2 3 5 7 11 13 17 19 23 2, 3, 5, 7, 11, 13, 17, 19, 23 are the generated prime numbers. Note : If P is a number whose multiples are being eliminated, then P.P should not be greater than n, and therefore P cannot exceed √n .

// Implements the Sieve of Eratosthenes // Input: An integer n ≥ 2 ALGORITHM Sieve(n) // Implements the Sieve of Eratosthenes // Input: An integer n ≥ 2 // Output: for p ← 2 to n do A[p] ← p for p ← 2 to √n do { if A[p] ≠ 0 // p hasn’t been eliminated on previous passes { j ← p * p while j ≤ n do {A[j] ← 0 // mark element as eliminated j ← j + p }

algorithm continued… // Copy the remaining elements of A to array L of primes i ← 0 for p ← 2 to n do if A[p] ≠ 0 L[i] ← A[p] i ← i+ 1 return L

FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING Understand the problem Decide on: Computational means, exact Vs approximate solving, Data structure(s), algorithm design technique Design an Algorithm Prove correctness Analyze the Algorithm Code the Algorithm Fig: Algorithm Design and Analysis Process.

Understanding the Problem Thoroughly understand the problem. We need to identify the problem requirements. Description of the problem statement should be clear. We should know what input should be given and what output is obtained with sample input and output data. Very essential to specify the exact range of inputs given to algorithm.If it is not specified, it may work correctly for a majority of inputs but may crash on some other input. Correct algorithm is not the one that works most of the time but one that works correctly for all legitimate values(input).

Ascertain the capabilities of a Computational Device. Architecture of the device For the device based on von Neumann architecture s in which instructions and data are stored in memory and programs are executed sequentially(Design sequential algo). If a device is capable of executing the instructions in parallel.(Design parallel algo) Speed of the device Select exact/approximate algorithms Selection of appropriate data structures

Design an algorithm Various algorithms design strategies as shown below. Brute force Divide and conquer Decrease and conquer Transform and conquer Space and time tradeoff Dynamic programming Greedy technique Back tracking Branch and bound Algorithm can be specified in different ways Natural lang Pseudocode Flow chart

Proof of algorithms correctness It is our responsibility to prove that the algorithm produces the required output for every legitimate input. Proof of correctness of an algorithm is normally done using mathematical induction.

Analysis of algorithms Solution to the problem can be obtained using different algorithms. Methods using which efficiency of an algorithm can be measured are …. - Time efficiency -Space efficiency

Select an algorithm which is more efficient in terms of time and space. It also helps to find the bottlenecks in a given program. Very important to write the algorithms so as to maintain the simplicity. Generality.

Coding an Algorithm: programming language. Algorithm is coded using suitable data structure in programming language. can be tested to know the actual statistics about the algorithm’s consumption of time and space requirements. - If less efficient then you can fine tune the code to improve the speed or you can go for better algorithm.

Important Problem Types Sorting Searching String processing Graph problems Combinatorial problems Geometric problems Numerical problems

Sorting Bubble Selection Insertion Quick Merge Heap (Based on which the records are sorted is called a key) Two properties of sorting techniques are Stable -If the sorting algorithm preserves the relative order of any two equal elements Ex:-if input has two elements a[i] and a[j] at positions i and j repectively where i<j, after sorting if the elements a[i] and a[j] are moved from i and j to the positions i’ and j ‘ and if i’<j’ then the algorithm is called stable. In place-If an algorithm does not require an extra memory space except for few memory units.

String processing String processing deals with manipulation of characters or strings, string matching, search and replace, deleting a string in text, inserting a string at any position in the text. String matching What it is? Various algorithms are available… Brute force Boyer More Horspool’s

Graph problems A graph is defined as G=(V,E).It can be represented using arrays or linked lists. Graph traversal algorithms Topological Shortest path Ex:The traveling salesman problem (TSP) is the problem of finding the shortest tour through n cities that visits every city exactly once. In addition to obvious applications involving route planning, it arises in such modern applications as circuit board and VLSI chip fabrication, X-ray crystallography, and genetic engineering. The graph-coloring problem seeks to assign the smallest number of colors to the vertices of a graph so that no two adjacent vertices are the same color. This problem arises in several applications, such as event scheduling.

Combinatorial problems Problems that involve permutations,combinations ar subset constructions are called combinatorial problems. These problems satisfy certain conditions by either maximizing the profit / minimizing the cost Problems faced due to it If size of the problem increases slightly ,no of combinatorial objects grows extremely fast and reaches unimaginable magnitudes. There are no known algorithms to solve these problems in an acceptable amount of time.

Geometric Problems Problems deal with geometric objects such as points,curves,lines,polygons etc. Closest pair Convex hull Numerical problems

1. Consider the algorithm for the sorting problem that sorts an array by counting, for each of its elements, the number of smaller elements and then uses this information to put the element in its appropriate position in the sorted array: ALGORITHM ComparisonCountingSort(A[0..n − 1]) //Sorts an array by comparison counting //Input: Array A[0..n − 1] of orderable values //Output: Array S[0..n − 1] of A’s elements sorted // in nondecreasing order for i ←0 to n − 1 do Count[i]←0 for i ←0 to n − 2 do for j ←i + 1 to n − 1 do if A[i]<A[j ] Count[j ]←Count[j ]+ 1 else Count[i]←Count[i]+ 1 S[Count[i]]←A[i] return S a. Apply this algorithm to sorting the list 60, 35, 81, 98, 14, 47. b. Is this algorithm stable? c. Is it in-place?

Review of data structure A data structure can be defined as a particular scheme of organizing related data items. Linear Data Structures-The two most important elementary data structures are the array and the linked list. Two special types of lists, stacks and queues

Graphs Graph- is informally thought of as a collection of points in the plane called “vertices” or “nodes,” some of them connected by line segments called “edges” or “arcs.” Formally, a graph G = V,E is defined by a pair of two sets: a finite nonempty set V of items called vertices and a set E of pairs of these items called edges.

Directed graph-A graph G=(V,E) in which every edge is directed is called a directed graph or digraph. Undirected graph-A graph G=(V,E) in which every edge is undirected is called an undirected graph. Complete graph-A graph G=(V,E) is said to be a complete graph, if there exists an edge between every pair of vertices. Loop- loop is an edge which starts and ends on the same vertex.

Graphs for computer algorithms can be represented in two different methods. Adjacency matrix-adjacency matrix of a graph with n vertices is a boolean square matrix with n rows and n columns with entris 1’s and 0’s. i.If there exists an edge(i,j), the element in ith row and jth column will be 1. ii. If there is no edge from vertex I to vertex j,then the element in ith row and jth column will be 0.

Adjacency linked list-The adjacency lists of a graph or a digraph is a collection of linked lists, one for each vertex, that contain all the vertices adjacent to the list’s vertex (i.e., all the vertices connected to it by an edge). A weighted graph (or weighted digraph)-It is a graph (or digraph) with numbers assigned to its edges. These numbers are called weights or costs. Path-Apath from vertex u to vertex v of a graph G can be defined as a sequence of adjacent (connected by an edge) vertices that starts with u and ends with v. If all vertices of a path are distinct, the path is said to be simple. The length of a path is the total number of vertices in the vertex sequence defining the path minus 1, which is the same as the number of edges in the path.

Isolated node/vertex- A vertex which is not connected to any other vertex. Outdegree-no of edges leaving a node. Indegree- no of edges incident on a node. Degree- sum of out and indegree.

A cycle is a path of a positive length that starts and ends at the same vertex and does not traverse the same edge more than once. A graph with no cycles is said to be acyclic. Connected graph- A graph G, are said to be connected if and only if there exists a path between every pair of vertices. Disconnected graph- Let G(V,E) be a graph.If there exists at least one vertex in a graph that can not be reached from other vertices in the graph.

Applications of graph Transportation and communication networks Project scheduling and games Road maps etc

Trees A tree which is also called free tree is a connected acyclic graph.Collection of one or more trees is called a forest. Rooted tree- select an arbitrary vertex in a free tree and consider it as the root of the so-called rooted tree. A rooted tree is usually depicted by placing its root on the top (level 0 of the tree), the vertices adjacent to the root below it (level 1), the vertices two edges apart from the root still below (level 2), and so on. Binary tree Binary search tree Siblings Ancestors-Nodes in the path from root to the specified node x,are all ancestors of node x. Descendents- nodes in the path below the parent are called descendents. Terminal/non Height/depth Ordered tree- It is a rooted tree in which the children of each node are ordered from left to right.

Activity 4. a. Let A be the adjacency matrix of an undirected graph. Explain what property of the matrix indicates that i. the graph is complete. ii. the graph has a loop, i.e., an edge connecting a vertex to itself. iii. the graph has an isolated vertex, i.e., a vertex with no edges incident to it. b. Answer the same questions for the adjacency list representation.

A graph is complete if and only if all the elements of its adjacency matrix except those on the main diagonal are equal to 1, i.e., A[i, j] = 1 for every 1 ≤ i, j ≤ n, i = j. ii. A graph has a loop if and only if its adjacency matrix has an element equal to 1 on its main diagonal, i.e., A[i, i] = 1 for some 1 ≤ i ≤ n. iii. An (undirected, without loops) graph has an isolated vertex if and only if its adjacency matrix has an all-zero row.

For each of the following applications, indicate the most appropriate data structure: a. answering telephone calls in the order of their known priorities b. sending backlog orders to customers in the order they have been received c. implementing a calculator for computing simple arithmetical expressions

END OF CHAPTER 1