Download presentation
Presentation is loading. Please wait.
1
ANALYSIS AND DESIGN OF ALGORITHMS
UNIT-I CHAPTER 1: INTRODUCTION
2
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.
3
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
4
NOTION OF ALGORITHM Problem Algorithm “Computer” Input Output
5
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.
6
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
7
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
8
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
9
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
10
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.
11
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 = 24 = gcd(60,24) = =12 Is this an algorithm?
12
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
13
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
14
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
15
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
16
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
17
// 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 }
18
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
19
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.
20
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).
21
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
22
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
23
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.
24
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
25
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.
26
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.
27
Important Problem Types
Sorting Searching String processing Graph problems Combinatorial problems Geometric problems Numerical problems
28
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.
29
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
30
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.
31
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.
32
Geometric Problems Problems deal with geometric objects such as points,curves,lines,polygons etc. Closest pair Convex hull Numerical problems
33
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?
35
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
36
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.
37
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.
38
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.
39
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.
40
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.
41
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.
42
Applications of graph Transportation and communication networks
Project scheduling and games Road maps etc
43
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.
44
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.
45
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.
46
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
47
END OF CHAPTER 1
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.