You are on page 1of 136
MCA 12 - DESIGN AND ANALYSIS OF ALGORITHMS Page No. — = Block 1 | Introduction to Algorithms and its Development | 4 _ _ | | | Block 2 | Basic Tools and Algorithms 24 — Block 3 | Algorithm Design Methods 75 | Block 4 | Simulation and Computer Science Algorithms | 110 | &¢) TAMILNADU OPEN UNIVERSITY Ss Course Design and Preparation Committee Chairperson: Dr. M. BHASKARAN Vice-Chancellor Tamil Nadu Open University Course Design: Dr. P. Thiyagarajan Reader & Head School of Continuing Education Tamil Nadu Open University Course Writers: Mr. M. Sagayaraj Senior Lecturer Department of Computer Science and Engineering SRM Institute of Science and Technology Kattankulathur — 603203 Co-ordination: Er. N. Sivashanmugam Lecturer School of Computer Science Tamil Nadu Open University Mrs. Shobarani Junior Consultant (Academic) School of Computer Science Tamil "y" Open University Composed by: Mr.K.S, Murralitharan Tamil Nadu Open University Reprint: Mar 2017 © Tamitnady Open University AU Rights reserved. No part of this work may be reproduced in any form, by mimeograph or any other means, without permission in writing from Tamil Nadu Open University. Further information on Tamil Nadu Open University Programmes may be obtained from the Universtiy Office at Saidapet, Chennai - 600 015 and Website: www.tnou.ac.in COURSE INTRODUCTION Asset of steps or instructions which precisely describe a limited procedure of task, Computer programs are written in programming languages designed around the requirements for precision and coordination of sets of algorithms. The mathematical content in the design and analysis of algorithms comprises introductory parts of the subjects of networks, combinatory, probability and statistics. These are all discussed because of their use in algorithmic applications. Simple implementation and testing of programs are an important part of the development of algorithms; computer code has to be exhibited Sorting algorithm is an algorithm that puts elements of a list in a certain order. The most used orders are numerical order and lexicographical order. Efficient sorting is important to optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly; it is also often useful for canonical zing data and for producing human-readable output, Block one deal with the Introduction to Algorithms and its Development Block two explains more about the Basic tools of Algorithm. Block three will make you familiarize about Algorithm design methods and Block four deals with Simulation and Computer Science Algorithms. BLOCK-1: INTRODUCTION In this block, we will learn about the basics of Algorithms, Basic Steps in Development of Algorithm, ‘Statement of the Problem, Development of a Model, Design of an Algorithm, Correctness of the Algorithm, Program, Testing and Documentation. Design and Analysis of Algorithms is divided into four blocks. Block one consists of one Unit as follows: Unit-1: deals with the above contents. UNIT -1 Structure INTRODUCTION Overview Learning Objectives 1.1 Introduction 1.2 Algorithms 1.3 Basic Steps in Development 1.4 Design of an Algorithm 1.5 Correctness of the Algorithm Let Us Sum Up Answer To Leaning Activities References OVERVIEW A basic course in computing focuses on such topics as the working of a computer, keypunching and data preparation, the syntax of programming language, coding, input/output, the elementary aspects and uses of data structures, subroutines and function concepts, the art of debugging, the design of relatively simple programs, some machine language concepts, and a few application programs. The above course is not covering some topics like i. The complete development, from start to finish, of a reasonably complicated problem for computer solution. ji, Algorithm design techniques, such as sub goals, hill climbing, working backward, backtracking, branch and bound, recursion and heuristics ili, Efficient and correct implementation of stated algorithms iv. Algorithm and program correctness v. Measures of algorithm efficiency, complexity, and overall effectiveness vi. Program testing, including tests for correctness, complexity, and general program behavior vii More sophisticated mathematical thinking This course is concentrating on the above topics, It also involves a significant amount of programming and it is intended to serve as a bridge between the more practical, programming- oriented courses and the more theoretical, mathematically oriented courses in computer science. The mathematical content in the design and analysis of algorithms comprises introductory parts of the subjects of networks, combinatory, probability and statistics. These are all discussed because of their use in algorithmic applications Simple implementation and testing of programs are an important part of the development of algorithms; computer code has to be exhibited. LEARNING OBJECTIVES After compieting this unit, you should be able to «List out the notions which are used in an algorithm «Explain the different steps involved in developing an algorithm * Design an algorithm for any kind of problem | INTRODUCTION The development of an algorithm is helping a human being to solve any type of problem by using the knowledge of mathematics and computer science. This text helps to learn how to Get started on a problem ii Design an algorithm that works. iii, Implementation of algorithm as a computer program iv. Judge the effectiveness of an algorithm Generally, a computer is a relatively new and compicx tool and its takes a lot of time to learn how to use it well. The skilis are necessary to use a computer as a pawerful instrument for solving problems, particularly mathematical problems, are Not easy to acquire. The main objective of this unit is to make a first pass at the concept of the complete development of an algorithm. The basic steps of which are: 1. Statement of the problem Development of a model . Design of the algorithm Correctness af the algorithm Analysis and complexity of the algorithm 2 3 4 5. Implementation 6. 7. Program testing 8 Documentation 4.2 ALGORITHMS _ An algorithm can be loosely defined as an unambiguous procedure for solving a problem. A procedure is a finite sequence of well-defined steps or operations, each of which requires only a finite amount of memory or working storage and takes a finite amount of time to complete. And also an algorithm must terminate in finite time for any input In the above definition, the term unambiguous is very ambiguous. “Unambiguous” is to whom or to what? Since nothing is universally clear or vague, the executor of the algorithm must be at least implicitly, specified. An algorithm to compute the derivative of a cube polynomial might be clear to someone who is familiar with calculus, but it may be totally incomprehensible to someone who is not. Thus executor's computational capabilities must also be specified The following example of an algorithm illustrates a level of detail that is consistent with our definition. The problem is to find the maximum number in a list of N real numbers R(1),R(2),....R(N). The basic idea is to scan the number one by one and remember the largest number that has been seen so far. This process will find inspect all the numbers and the largest number will be retained. The notation A€ B denotes an assignment statement; that is the value of the variable B is set to the variable A. Algorithm MAX Given N real numbers in a one dimensional array R(1),R(2),....R(N), find M and j such that M=RQ@)= max Rik) token In the case where two or more elements of R have the largest value, the value of j retained will be the smallest possible, Step 0. {initialize] Set M CR); and je 1. Step 1. {N=4] If N=1 then Stop fi Step 2. [Inspect each number] For k€ 2 to N do step 3 od; and stop. Step 3. [Compare] If M < R(k) then set MER(k); and jek. (M is now the largest number we have inspected, and itis in the ki, position array.) [The construct fi and od are used to denote the end of the if and do constructs, respectively.] The above algorithm can be converted to coded form very easily whereas some algorithms are too complicated to make transition from the verbal form to computer code in one step. For that type of algorithms at least one intermediate stage of development has to be introduced. 1.3 BASIC STEPS IN DEVELOPMENT In this section, each of the basic steps, which are involved in developing an algorithm, is discussed. The primary goal is to establish the function of each step and to gain some perspective on how these steps combine to form a coherent whole Statement of the Problem Before understanding the problem, we must be able to give it a precise statement. Developing a precise problem statement is only by asking the right questions. Some good questions to ask upon encountering a crudely formulated problem are: = Do | understand the vocabulary used in the raw formulation? * What information has been given? = What do | want te find out? * How would | recognize a solution? * What information is missing, and will any of this information be of use? * Is any of the given information worthless? * What assumptions have been made? * Depends upon the problem, other questions are also possible. These questions have to be asked only after some of them have been given answers or partial answers. Example: Jack is a computer representative whose territory covers 20 cities scattered throughout Texas. He works for large commissions, but his company will reimburse him for only 50 percent of the actual cost of automobile travel for his business trips. Jack has taken the trouble to figure out how much it would cost him to travel by car between every pair of cities in his territory. He would clearly like to keep his travel costs down. What is given is the list of cities in Jack's territory and the associated cost matrix that is a square array with entry cij equal to the cost of going from city i to j, For the above problem the size of the cost matrix is 20 rows and 20 columns. What do we want to find out? We want to help Jack to keep his travel costs down. It is not that much clear. It is not ‘enough to solve the problem. That is whether Jack is visiting all the cities only once or more than once and also whether Jack is having any priorities and schedule preferences. For the above information, Jack has to be asked some more questions. He tells us that he would like an itinerary that would start at his base city, take him to each of the other cities in his territory exactly once, and return him to his base. Consequently, we would like a list of cities, which contains each city only once, except for the base city, which is listed first and last. The order of the cities on sum of the costs between every consecutive pair of cities on the list is the total cost of the tour represented by the list. We could solve Jack's problem if we could give him the list with the smallest possible total cost. 10 This is the good basic statement of the problem. We know what we have and what we want to find. Development of a model Once the problem has been stated clearly, the next step is to formulate it as a mathematical model, It is a very important step in the development process. The choice of mode! selection is influencing the remainder of the solution process. It is impossible to provide a set of rules to automate the modeling stage. Each problem is requiring an individual attention, Some guidelines are explained now: The best way to become proficient is by acquiring the experience that comes from the study of successful models. At least two questions have to be asked to set up a model 1, Which mathematical model structures seem best-suited for the problem? 2. Are there other problems that have been solved which resemble this one? Most of the problems that are solved in mathematics tend to be the perturbations of previously solved problems. We need a considerable amount of guidance from existing work in order to make progress. Some mathematical structures have to be selected to represent both what we know and what we want to find, This choice of structures will be influenced by such factors as 1. the fact that our knowledge is limited to relatively few structures 2. convenience of representation computational simplicity 4, _, The usefulness of the various operations associated with the structure or structures under consideration Once the tentative structure has been made, then the problem has to be restated in terms of these mathematical objects. It may be a candidate model if we can give affirmative answers to such questions as: + Is all the important information in the problem clearly labeled by mathematical objects? + Is there a mathematical quantity associated with the result sought? * Have we recognized some useful relations between the the model? * Can we work with the model? Is it reasonably object manipulability? Example: Consider the computer salesman problem presented in the earlier section. Start with the problem statement given at the end of the example. Have we solved a problem similar to this before? In a mathematical sense, it is not. But we have been confronted with routing problems using road maps or mazes. Now, Is it possible to represent our problem that is similar to a map? To represent the problem as a map, a city can be placed as a dot or point in a paper using a pencil. The distance between any pair of points corresponding to cities i and j is proportional to the travel cost ¢;. So; the points can be dfawn in any way and draw a line between i and j, and label the line with the “weight “cy, The object, which was created, is mathematically called as network or graph. In general, a network is a set of points together with weighted or un weighted lines drawn between ‘some or all pairs of these points. 12 Assume that Jack has only five cities with the cost matrix as given below: The cost of the trip for both i to j and j to i is same, but this is not necessary city 39 2 4 - 1 2 Cost Matrix A five-city salesman problem What are we looking for in the problem? In terms of network, the list of cities defines a closed cycle starting from the base city and returning there after passing through each city exactly only once. This kind of traversal is known as tour. The cost of the tour is defined as the sum of the weights of all the traversed edges. Finding a tour with the smallest cost can solve the problem. in the above figure, the traversal 1-5:3-4-2 is a tour with cost 5+2+144+1=13. 1.4 DESIGN OF AN ALGORITHM Once the problem has been stated clearly and a mathematical model is created, then the algorithm can be designed to solve the problem. The choice of design technique is highly depends on the mathematical model and that will influence the effectiveness of a solution algorithm. For one problem, two algorithms may be correct, but they may differ in their effectiveness. Measures of effectiveness will be discussed in the latter sections. Example: The same salesman problem is taken as example. From the problem statement and model discussed earlier suggest the following algorithm. The n cities are numbered with the integers from 1 to n, each city is given a unique integer number. The base city is given the number n. Every tour corresponds uniquely to a permutation of the integers 1,2,..., 1.In fact each tour corresponds to unique permutation and each permutation corresponds to a unique tour. Such correspondence is called one-to-one, Thus for a given permutation, the corresponding tour can be traced out from the network and cost of the tour can be found. Generating all the permutations of the first n-1 positive integers can solve the problem. For each permutation construct the cost of the tour. Proceed through the list of all permutations, saving the tour that, so far, has the least cost. If we find a tour 14 with a lower cost, we make this new four the standard for the future comparison, Algorithm ETS (Exhaustive Traveling Salesman) To solve an N-city traveling salesman problem by systematically considering all the permutations on the first N- 1 positive integers. In this way we will consider every possible tour and choose a TOUR with the least cost MIN. ETS requires as input the number of cities N and the cost matrix C Step 0. {Initialize] Set TOUR €0; and MIN <= Step 1. [Generate ai permutations] For | € 1 to (N-1) do through step 4 od and STOP. Step 2{Get new permutation] Set P€ the In permutation of the integers 1,2,..., N-1 Step 3.{Construct a new tour] Construct the new tour T(P) that corresponds to the permutation p; and compute the cost COST(T(P)). Step 4.[Compare] If Cosi(T(P)) < MIN then set TOUR €T(P); and MIN €COST(T(P)) fi CORRECTNESS OF THE ALGORITI The more difficult and sometimes tedious, steps in the development of an algorithm is proving or asserting that the algorithm is correct. The most common method is by executing the program on a variety of test cases. The answers produced by the program can be verified by the known values or results obtained by hand calculations. Sometimes this technique will fal. The following guideline can be used to proof ‘ne correctness of the algorithm. Any algorithm is given in terms of @ series of steps, say step 0 through step m. Give some justification for each step. In particular, this might involve a lemma about conditions that exists before and after the step is executed. Then try to offer some proof that the algorithm will terminate, and, in doing so, will have examined the entire appropriate input data and produced all the appropriate output data. Example: Algorithm ETS is very easy to prove the correctness The different tour is examined; the tour with the minimum cost must be examined. After seeing that, it will be retained by the algorithm. It will never be discarded since this can only happen if there exists a cheaper tour to replace it. The algorithms must terminate because there are only a finite number of tours to examine. The above method is known as proof by exhaustion and it is the crudest of all proof techniques. Implementation Once the algorithm has been stated and it is convinced that it is correct, then the next step, which is implementation, starts. In which the algorithm has to be represented in the form of coding into a computer program. An algorithm is not directly translatable into code. Some single step of an algorithm may need a subroutine for its implementation. And also we must design an entire system of computer data structures to represent important aspects of the model being used. For the above process, we must answer the following que: + What are the variables? = What are their types? How many arrays, and of what size, are needed? = Would it be worthwhile to use linked lists? + What subroutines sr needed? * What programming language should be used? The implementation of an algorithm is directly affecting the iemory requirements and the speed of the algorithm. Another structured aspect of program implementation concems top-down programming, In this method the initial algorithm is successively modified into a sequence of increasingly refined algorithms in such a way that the final version is the correct computer program The other important concept is, one must be very careful that the process of converting @ proven algorithm fo @ computer program is a faithful one Programm Testing Once a program has been coded, then it is the fime to run it. Debugging precedes running, After a variety of syntactic, keypunching, and logical errors have been corrected, and then the program can ve executed. Program testing might be described as an experimental verification that the program is doing what it should. it is also an experimental attempt to ascertain the usage limits of the algorithm/program It is not enough to prove that an algorithm is correct. The finai coded product must be extensively tested and verified the progie: “ust be verified for a broad spectrum of allowable inpu This process can be time-consuming, ramplex Usually the set of ai! possible inputs is astronomical and a complete testing 1s impractical. We must choose a variety of inputs, which test every segment of code in the program. A representative sampling of the cases that we are likely to encounter in practice is a must. It is rarely possible to guarantee that @ program is correct, but we can and must de enough testing to be reasonably confident that it i. The performance analysis is also to be carried. Experience indicates that average case analysis is more valuable and more difficult to obtain the best and worst case analysis. For an algorithm, if the worst-case analysis is available and average case analysis is not then it is of considerable importance to ascertain experimentally if the algorithm performs significantly better on the average than it does in the worst case. Both analytical and experimental evidence should be offered, whenever possible to support performance claims. Programs also should be tested to determine their computational limitations, Many programs may work wl! fo: some inputs whereas poor for some other inputs. Alvorithm EYS is good if n<= and very bad for n>=15. Unfortunately, the transition is not gradual. The algorithm tends to perform poorly for the intermediate cases. It is desirable to use both analytical and experimental methods to characterize those input data that are either good or bad Documentation The documentation is nut a last step, but it should be interwoven with the entire development of the algorithm and particularly in the design and implementation steps. The program, which is written by one person, is not understandable by others. In this situation, documentation helps anyone to understand the program. Documentation includes every piece of information that you produce which helps to explain what is going on, that is such things as flowcharts, records of the stages in your top-down development, supporting proofs of correctness, test results, detailed descriptions of wy ot and output requirements and format, and so forth. Document you programs the way you would want others to document the programs, which you read ‘LET US SUM UP +22 discussed about the basic notions used for an algarithm and the basic steps involved in the development of an algorithms KEYWORDS _ Algorithm: A set of steps or instructions which precisely describe a limited procedure or task. Computer programs are written in programming languages designed around the requirements for precision afid coordination of sets of algorithms. Documentation: Information which accompanies a digital object to give background, context and history. Testing: The piocess of theasuring, analyzing, or evaluating the characteristics, capacities, attributes, or aptitudes of a substance, group, of invjivical Implementation: Th cution of the adoption decision, that is, the innovation or the research is put into practice. LEARNING ACTIVITIES A. Fill in the blanks: 1 Analgorithm can be loosely defined as an unambiguous procedure for ......... a problem. 2. Program testing might be described as an experimental that the program is doing what it should. &. State True or False: 1 An algorithm can be directly translatable into code. 2. The implementation of an algorithm ts directly affecting the memory requirements and the speed of the algorithm ANSWER TO LEARNING ACTIVITIES A. Fill in the blanks: 1. solving 2. Verification 8B. State True or Faise: 4. True 2. True CHECK YOU 1 Define an algorithm. 2. Discuss the different step involved in the development of an algorithm. REFERENCES The Design and Analysis of Algorithms ~ Dexter © Kozen, Publisher Springer Algorithms - MH Alsuwaiyel, Publisher World Scientific Fundamentals of Computer Algorithms - Ellis Horowitz and Sartaj Sahni, Publisher Galgotia Algorithms and Data Structures - Niklaus Wirth, Publisher PHI : INTRODUCTION - In this block, we will learn about the Top-down strictured Programming, Program Correctness with example - The Knight's Tour, Networks (Graphs) with Fundamentals and Representation of Trees, Isomorphism, Linked Lists, Adjaceney Lists, Push-down Lists, Queues, Probabilistic Model with example - Performance Analysis of Straight Insertion Sort Unit 2: This Unit deals with the above contents. 2 UNIT -2 SOME BASIC TOOLS AND ALGORITHMS Structure Overview Leaming Objectives 2.1 Basic Tools 2.1.1 Top-Down Structured Programming 2.1.2 Program Correctness 2.1.3 The Knight's Tour 2.2 Networks 2.2.1 Fundamentals 2.2.2 Representation 2.2.3 Trees 2.2.4 |somorphism 2.3 Some Data Structures 2.3.1.Linked Lists 2.3.2 Adjacency Lists 2.3.3 Push-Down Lists 2.4 Queues 2.5 Probabili 2.6 An Example - Straight insertion Sort Let Us Sum Up Answer To Leaming Activities ic Model References OVERVIEW This Unit explains about different programming methods. It deais the networks and also the different methods to represent the network are also explained. The other data structures like linked lists, pushdown lists and queues are also explained with algorithms for creation, insertion ard deletion of the above data structure. LEARNING OBJ! ECTIVES After completing this unit, you should be able to * Explain the various basic tools which are used for programming * Program using top-down structured programming Explain the networking concept Program for linked list © Program for data structure * Explain the straight insertion sort ~ BASIC TOOLS _ There are number of basic tools that are used to design and analysis of algorithms. They are al discussed in the following sections. 2.1.1 Top-Down Structured Programming Structured programming is explained using a flow chart, A flow chart is a directed network having three kinds of vertices. + Function vertex is used to represent a function EXPY. ¢ Predicate vertex is used to represent a function p:X> {TF}, that is, a Boolean function, which passes control along one of two branches. 23 + Collecting vertex represents the passage of control from one of two incoming brariches to one outgoing branch Types of vertices in a flow chart Function vertex \ oo T Yn == Vv F = Predicate vertex | Collecting vertex A structured flowchart is a flow chart can be expressed as a composition of the four primitive flowcharts. These four flowcharts are sufficient to design any elgorithm. oT aa | 4 <> If a program is represented using structured flowchart, then B is interpreted as a Boolean expression, and $1, $2 are interpreted as program statements or procedures. The flowcharts from a to d are called as program control structures. Figure a is called as composition and is written as do $1;S2 od (or simply as $1;82). Figure 2.1.2b is called selection ( or alternation) and is written as if B then S1 else S2 fi ( the fi is optional and serves to complete the if). The flowcharts 2.1.2¢ and d are iteration (or repetition) and are written as while B do S1 od and do $1 while B odithe ods also optional). The following flowchart is used for the simpler statement, if B then S1. Itis a special case of the if-then-else statement. _e Cs » Flowchart for the statement if B then St 26 All the above four peagrar’ control ate have a single enity anc single exit braiich. The flowchart for the statement is yiven below if B then do $1;52 od else if C then S3 else $4 ‘The flowchart for the statement is given below if B then if C then do $1;S2 od else while D do 3 od fi else do $4;85;86 od fi. Structured programming refers to the process of designing algorithms in terms of stuctured flowcharis. The foliawing flowchart explains the slalement casei of $4;82;...;Sm fo itis equivalent to iFi= 4 thon $1 else ifi=2 then S2clse Flow chart for a case statement The next example corresponds to the situation where one reeds to abnormally terminate an iteration, or DG loop, by providing an exit branch. The linguistic statement is while B do S1; if C then S2 else goto OUT fi od F Abnormal a . ¢ > $2 Flow chart for an abnormal termination of iteration Another example of a simple, unstructured fovrshart which ‘occurs naturally in programming is in the following diegram. This also can be represented using structured tiowchart also. The linguistic statements for the unstructured flowchart is k: S1; if B then $2; goto k fi The linguistic statement for the following structure uiowchart is do $1; while & do S2; $1 od od Program correctness Top-down programming refers to the process of stepwise fefinement of an algorithm 0 successively smaller peaces until a point is reaches irom: which code can easily be written. The following flowchart explains this process. A top down refinement of the flow chart Top down structured programming refers to the top- down process restricted to the use of structured flow charts The idea is simpte Suppose one is trying to design an algorithm for a particular function f. suppose further that it can be proved that f equals the composition of two other functions g and h that is, {(x) = h(g(X)) is illustrated in the following figure. Then the problem of designing an algorithm for f reduces to the problems of designing algorithms for g and h The expression of a function f as a composition of function g and h L* J | Furthermore assume that it can be proved that the function g equals some function i if a given parameter x is nonnegative, or equals some other function j if x is negative. Then an algorithm for computing g can be expressed as an if- then-else. It is explained in the following diagram. Te i j _—! r= ' —o— The expression of the function g using an if-then-else predicate Therefore, algorithms for the functions i and j can be designed, a correct algorithm for function g can be designed automatically. 2.1.2 The Knight's Tour In the game of chess, a knight can move from a given square to one of at most eight other positions as like the following diagram. Since a knight cannot move off the chessboard, it cannot move to eight new positions from every Position. For example, from position X = (1,2) it can only reach in one move the positions X!,X2,X3 indicated in the diagram. New position from position (I,J) (2,s+1) (1-4,+#2) (#4,5#2) (2,41) (142,J-1) (#4,u-2) (I-41.-2) (-2,J-1) CP NOAKRYN OS If knight starts on some square of the chessboard and make a sequence of 63 moves in such a way that it visits every square exactly once, then the traversal is called as knight's tour. Consider an arbitrary walk by the knight from an arbitrary square, (5,6) is shown below. an integer N indicates the Nth square visited in the walk. In this example, after the 45" move to the position (8,2) is not possible. The Kinght's cannot move without either going off the board or visiting 4 | 46 42] 2 An arbitrary walk by a knight from position (5,6) The following simple strategy is used for knight's tour. Start an arbitrary square. From a given square, attempt to move first to position 1 as like in the diagram “possible moves of a knight” If we cannot do this try to move position 2. failing this move position 3, etc, in a clockwise fashion. If we ever reach a position from which it is not possible to move to any of these eight positions without either moving off the board or revisiting a square, then the walk will stop and we will record the results. The algorithm may be Construct wate Create a possible knight's Construct walk from position(, J) Construct walk from a ae position(, J) Gear este apis a Pins Top-Down development of a possible knight's tour algorithm 7 99 [99 [99 [99 [99 99 [99 [99 wo | oo [o [99 [99 0 0 0 0 aes ae 5 [99 199 7 99 (99 [99 | 99 99 {|| eae [99 [99 [9a [99 [99 78 99 [99 |99 [99 [oo Initial board configuration To construct a walk from the position (I, J) can be started from any position of the BOARD (I, J). All the positions ase stored in a array with to integers (I,J) between 1 & 8. Initially all the elements of the array are initialized with the value 0 as like the above diagram. A move in the square to a position (I,J) is possible only if BOARD (I,J) is = 0 A move off the board can be detected if | or J lie outside the range of integers between 1 & 8. Moving of the board also can be detected by another method in which additional two rows are created around the array BOARD and store an initial value 99 in each new square. 38 In this case the move of the board can be detected by the nonzero value. Consider again the problem of constructing a tour from an arbitrary position (IJ). This step can be broken into sequence of smallest step, an if — then - else, a do-while, or a while-do. One can sense that there is a basic iteration involved here is an index say k, runs through ail the 8 positions which can be reached from position (I,J). The walk stops whenever k > 8. This suggests the breakdown of the following diagram. In attempting to move to a new position from (I, J), one of two things can happen. If it is possible change the value of (), J) to the new position and reset k to 1. or if it is not possible increment the vaiue k by 4 (k = k#1). Another problem of determining which square is in position k from square (I, J). In this regard the following table is created which indicates the changes in value | and J require to reach position k |_K | Move | JMOVE a 3 41 ee 2 a - eS eee Tables of changes in | and J required reaching position k 39 ‘Atenpto move he kh poston fom Jj getanew value b) Can we move to Peston k Get new value for (.)) Final Top-down development of knight's tour algorithm At this point we can complete our final structured flowchart is shown below Start, ee ore Initial position (i) — Initialize Board t Initialize variables, including Final Flowchart for Knight's tour algorithm 4l 2.2 NETWORKS _ Networks are the most useful of mathematical tools is indicated by the many diverse areas in which they have been applied. Furthermore, networks and in particular trees are probably the most commonly used mathematical structures in ‘computer science. Networks and trees are used in the study of data structures, Compiling, Programming Languages, operating systems, computation theory, Sorting, Searching, and Artificial Intelligence. Some example networks are given below. pred +o ____¢ s+ «Th | | | | | LI 4 _ Spanning trees and electrical networks 42 NH2 ° CH eee OH — oe» CH2 CH2 s CHB Representation of organic compounds 2.2.1 Fundamentals A network G = ( V,E) consists of a finite set V = V(G) of M > = 1 vertices together with a finite set E = E(G) of N>=0 unordered pairs (u,v) of distinct vertices which are called the edges of G. The following networks G; = (Vi, E;) consists of seven vertices V; = {t,u.v,w.x,y,z} and seven edges E; = {(u,¥), (vw), (WX), (W.2), (ZV), (V2X), (Ww.y)} is Show in the following diagram. U "T Vv G1 Zz “| Y An undirected network 4B Two vertices u and v are adjacent if there is edge (u, v) in G otherwise u and v are independent. Edge (u,v) is also said to be incident two vertices u and v. If the edges (u,v) and (v,u) are considered to be the same then they are undirected edges. Edges of the form (u,u) which are called loops, and they are not allowed. The multiedges are also not allowed in a network that is the occurrence of two or more copies of a given edge. The network, which is considering the multiedge, is called as multinetwork. The example diagram is given below. A network which consists of a finite set of vertices together with a set of ordered pairs [ u,v] of distinct vertices is called as directed network | A multi network with a loop at 7 44, G3: x An directed network In directed networks G = (V,E) the elements of E are referred as arcs and the elements of V are referred as nodes. If [ u.v| is an arc in a directed network then u is adjacent to v and v is adjacent from u. A weighted network is one whose edges or ares have been assigned a real numbers, which are mode! quantities such as time distance or cost. The neighborhood of a vertex v, N (v) consists of a set of vertices adjacent to v that is N (v) = {u | (uv)< E } In the following diagram the neighborhood of a vertex v consists of vertices u, x, wand z Gt Xx, Z 45 The degree of a vertex v denoted deg(v) equals the number of vertices in N(v) that is deg(v) =| N (v) |. A vertex v is an end vertex if deg(v) = 1 and if deg(v) = 0 v is set to be isolated. A network G' = (V', E') is a subnetwork of a network G = (V, £) if Vc Vand Ec E. if V' = V then Gis a spanning subnetwork of G. It v © V then G ~ v denotes the subnetwork whose vertices are V — {v} and whose edges consists of all edges in V that are not incident to v. That is G —¥ is obtained by deleting v and all its incident edges Similarly for an edge € = (uy) < E, G- e denotes the subnetwork obtained by removing e but not u and v from G; G +e denotes the network obtained by adding e to G. A walk in a network G = (V,E) from a vertex u, to a vertex u, is a (finite) sequence of vertices W = Us, Uay.... Un with the property that (ui, uss) ¢ E(G) for every 1 | 1-- Lo CG GQ & Cos G, Row t Row2 Rows ADAMS: —o] BUCHANAN] cae 2 ‘ 5 JACKSON serrerson] | xennay | Ly, n ‘ 7 4 J — Ree Rows Lol uncon TRUMAN wastinaTdn [> 5 10 3 | Row7 Row9 Row 10 Diagrams of linear linked lists = E--E PREV PTR JT] aE =3fm me ee se Deleting a cell from a linked list To delete an element from the linked list, the pointers have to be rearranged. To delete an element, the pointer, which is pointing that element, will be redirected to the next element of deleting element. The algor Algorithm DELETE 1! To delete a cell c, with INFO(i) = VALUE from a linked list, the first cell of which is given by FIRST. Step 1.|Get FIRST cell] Set PTR CFIRST;PREV©O; Step 2[Is cell empty] while PTR # 0 do step 3 od Step 3,{Is this it] If INFO(PTR) = VALUE then [Is it the first cel?) If PREV = 0 then [delete from front] Set FIRST two +f rour ] 9 PREV PTR —— TREE one | ++I wo, | Lo our s PREV, Pre Tike One FOUR i PREV PTR THREE ] aap ee orale a ; PREV: TR : THREE Flow chart for algorithm Insert rest insert attront? — only ell on ist Insert nn stat [ owen Inset new oe 8 | | GD 2.3.2 Adjacency Lists A network G = (V,E) can be represented as a linked list of adjacencies which is resembles of adjacency vectors but it uses less memory. The following diagram shows the linked list Tepresentation of a network. 60 OVaAanrRONn = "1 12 13 14 15 16 7 18 19 20 21 7) 15 19 13 a 25 24 45 42 61 In a linked list representation, the adjacent vertices of a given vertex are found from the column marked as NEXT. This representation requires 2(M+2N) Words of memory where m is the number of vertices and N is the number of edges in G. 2.3.3 Push-Down Lists In a linked list, insertion and deletion can take place in middle of the arrays. If the modifications are take place only at the front or the end; then the linked lists are not necessary. But the simple linear array is enough. To design an algorithm to decide an arbitrary sequence of parentheses, brackets, and braces is well formed. The definition of well forming is The sequences ( ), |], and {} are well formed es If sequence x is well-formed, so are (x),[x], and (x). ° Mf sequences x and y are well formed, s0 is xy. ES Nothing is well formed unless its being so follows from a finite sequence of applications of 1,2, and 3. This definition defines well-formed sequences by a constructive process. For example the following are well formed: Iter ‘Sequence Formed by rule 2 a) 1 - (oo 2anda © «oD 2andb q ne) 1.3 and¢ e ca one) anda ' Cla oe» 1.B.ande 9 C1 ae 2andé To decide whether the expressions are well formed « ‘not, a standard technique pushdown store is used.'It is a0 way infinite sequence of words of memory. — Top Diagram of a Push down store The pointer TOP is used to push and pop the elements from’ the stack. Initially, the value of the pointer TOP is 0.Before inserting an element, the pointer TOP is incremented. Now the element is inserted to insert to the location which is pointed by the pointer TOP. During deletion, the element which is pointed by the pointer TOP is retrieved and the pointer TOP is decremented by one. Now, using pushdown store how a sequence of expressions will be checked whether it is well formed or not. The expression isg={{ ] ( [I< })). The elements of g will denoted x; x2. _xn where each x is one of {,,(.).,0r ]. The symbols {( and [ ate called as LEFT symbols. We say that x; is aleft mate of x, ifx; = [ and x, =Jor x, = ( and x; =)or x = { andx =}. an Ge) 1234 5678 9101112 13 14 63 { { 1 cf t ¢ | i 2 3 5 6 7 | Top | froma) [ace ea) Ie | [ | fe) Foy ee i 1 0 | | Lt 0 ) 1 nf } t 3 8 9 10 11 12 13 14 | | ee ee ( ( { { t ‘ ‘a { 0 t | T 0 { 0 be - Lo | _| 0 4 L Pushdown configurations produced by Algorithm WELL-FORMED Algorithm WELL-FORMED 11 To determine if an arbitrary sequence of symbols x x2, XN uy Where each x, is one of {,},4.){, or), is well formed. Step 0 [initialize] set TOP <0; 1€1 Step 1.[Read the sequence from left to right] While | <= N do through step 3 od. Step 2.[Push a left symbol down] If x, is a LEFT symbol ut x; on top of the push down [pop a symbol off] f TOP is a left mate of x; then pop the element TOP off the store else PRINT “NOT-WELL FORMED"; and STOP fifi Step 3. [Read next symbol Set (© 1+ 4 Step 4. {Is store empty] If TOP = 0 then PRINT “WELL-FORMED" else PRINT * NOT WELL-FORMED’ fi; and STOP. 24 QUEUES — In a push down store elements can be inserted deleted only at one end. In a queue the elements are inse: at one end and deleted in the other end. The insertion en? called as REAR and the deletion end is called as FRONT A queue is a single dimensional array and t variables FRONT and REAR are used to point the front and rear of the queue. Initially if REAR >=FRONT then the queue is full insertion is not possible. Otherwise increment the REAR pointer and insert the element into the location pointed by REAR. To delete an element check the pointers REAR and FRONT whether both are equal or not. If both are equal then queue is empty and deletion is not possible. Otherwise deletion will be carried out by incrementing the pointer FRONT. oe eee 6 7 8 8 a cee F=0 1) | [al jal [al [] de] fe R=0 a j 2 8] 48 L| LI 3 _ icldc eel acl RD/ xD) RD} iD} |B) DP sie} lel 2.5 PROBABILISTIC MODEL _ A large number of important problems invoived some degree of uncertainty. Quantities of interest will not be predictable in advance but will exhibit an inherent randomness that must be incorporated into any useful model. This type of model is called as Probabilistic. ‘The applications of having knowledge on probabilistic to the design and analysis of algorithm are: 1. Analysis of the average performance of an algorithm 2. Simulation. 3. Computational statistics, 4. Decision making algorithms. 5. Random number generation 2.6 AN EXAMPLE-PERFORMANCE ANALYSIS __OF STRAIGHT INSERTION SORT _ The expected or average performance analysis of the aight insertion sort is discussed here. It takes a list of unordered positive integers and sorts them into ascending order, Forexample,27 412 71 81 59 14 273 87 A sorted list is created which is initially empty. In each iteration the first element is removed from, the unsorted list and placed into the proper place of the sorted list. This is by scanning the sorted fist from the smatiest to the largest number until a correct place for the new number is located. The sorting process is explained as follows. Iteration 0 Unsorted 412 71 81 59 14 273 87 Sorted 27 Mteration 1 Unsorted 412, 71 81 59 14 «273 87 Sorted 27 aie tteration 8 Unsorted 71 81 «59 14273. «87 Sorted a m1 at fteration 3 Unsorted 81 59 «14 «273 87 Sorted a7 74 Bt 412 Iteration 4 Unsorted 59 14 «(273 87 Sorted 27 59 71 B14 Iteration 5 Unsorted 14 273 «87 Sorted 14° 27° 5907181412. Iteration 6 Unsorted 273 87 Sorted 14 27 «69° «71 81-273 412 Iteration 7 Unsorted 87 Sorted vs fe GC tA Gh Ee A) CHP Algorithm SIS (Straight insertion sort) IITo sort, in place a sequence of integers I(1),I(2),.....-- {(N) into ascending /order. Step 1:[Basic iteration] For j€2 to n do through step 4 e od; and STOP. Step 2: [Take next integer] set €I(J); and LEJ@1 Step 3: [compare with sorted integers] While k=1 do set I(L+1) Step4: [Insert] The average number of comparisons needed to sort N keys is N*/4. The average complexity is O(N?). Flow chart for algorithm SIS -€T US SUM UP _ Algorithms are essential to the way computers process information, because a computer program is essentially an algorithm that tells the computer what specific steps to perform (in what specific order) in order to carry out a specified task, such as calculating employees’ paychecks or printing students’ teport cards. Thus, an algorithm can be considered to be any sequence of operations which can be performed by a Turing- complete system. Typically, when an algorithm is associated with processing information, data is read from an input source or device, written to an output sink or device, and/or stored for further processing. Stored data is regarded as part of the internal state of the entity performing the algorithm. In practice, the state is stored in a data structure, but an algorithm requires the intemal data only for specific operation sets called abstract data types. For any such computational process, the algorithm must be rigorously defined: specified in the way it applies in all possible circumstances that could arise. That is, any conditional steps must be systematically dealt with, case-by-case; the criteria for each case must be clear (and computable). Because an algorithm is a precise list of precise steps, the order of computation will almost always be critical to the functianing of the algorithm. Instructions are usually assumed to be listed explicitly, and are described as starting ‘from the top’ and going ‘down to the bottom’, an idea that is described more formally by flow of control. 19 is a programming stye characterized by the general flow of the project. The project starts with an often complex description of the goals of the project, and then starts breaking it down into smaller groups. Top-down programming is easier carried out using object oriented programming, because using OOP, many people can work on the same project at once. Vertex: The common endpoint from which two rays begin (ie, the comer of an angle) or the point where two lines intersect; the point on a triangle or pyramid opposite to and farthest from the base. Flowchart is a representation, primarily through the use of symbols, of the sequence of activities in a system (process, operation, function, or activity). Networks are the most useful of mathematical tools is indicated by the many diverse areas in which they have been applied In mathematics, the incidence matrix of an uincliracted graph G is ap * q matrix where p and q are the number of vertices and edges respectively, such that if the vertex and edge are incident and 0 otherwise. ‘The adjacency matrix for a finite graph G on n vertices is an n x n matrix where the non diagonal entry aij is te number of edges joining vertex i and vertex j, and the diaeonal entry ail is either twice the number of ‘oops at vertex i or just the number of loops (usages differ; this article follows the former, directed graphs always follow the latter) There exists a unique adjacency matrix tor each graph, and it is not the adjacency matrix of any other graph. In the special case of a finite, simple graph, the adjacency matrix is a (0,1)-matrix with zeros on its diagonal. If the graph is undirected, the adjacency matrix is symmetric, n A tree is a widely-used computer dala structure that emulates 2 tee structure with a set of linked nodes. Each node has zero or more child nodes, which are below it in the tree (in computer science, unlike in nature, trees grow down, not up). A node that has a child is called the child's parent node. A child has at most one parent; a node without a parent is called the root node (or root). Nodes with no children are called leaf nodes, lsomorphism A mapping between mathematical structures of the same type that preserves {fis structure and is both injective (1-to-1) and surjective: ( 219). ison mrphic objects are essentially the same with respect to the prescived structure. LEARNING ACTIVITIES A. Fill in the blanks: 1, Linked lists permit ............and .of nodes at any point in the list in constant time, but do not allow random access. 2. The network, which is considering the... , is called as musitinetwork, 3. A tes 1 undirected. . Network that has no cycies, 8. State True or False: An acyclic directed network has directed cycles. 2. To delete an element from the linked list, the pointers have te be rearranged. 3. A directed network is a tree if and only if it is a tree with its arcs taken as undirected edges. ANSWER TO LEARNING ACTIVITIES A. Fill in the blanks: 1. insertion, deletion 2, multiedge 3. connected B, State True or False: 1. False 2. True 3. True CHECK YOUR PROGRESS 4, Define the following i. Function vertex ii, Predicate vertex ili, Collecting vertex Discuss the various program control structures. Discuss top-down structured programming, What is knight's tour? Discuss knight's tour problem with an examnple. Oaron Define the following i, Network = ili, Adjacent vertex iv. Independent vertex v. Undirected edges vi. Loop vii. Multi edges viii, Directed network ix. Weighted network x. End vertex 3 7. What is adjacency matrix? 8. What is incidence matrix, adjacency vectors, Tree, Spanning tree? 9. What is isomorphism? 40. Explain the advantages of array data structure. 141. What is linked list? 12.Discuss the insertion, deletion algorithms in a linked list? 13. What is adjacency list? 14,Discuss stack data structure. 15.Using pushdown stack, how an expression can he verified whether it is well formed or not? 16. Discuss the Queue data structure 17.Explain the straight insertion sort with an example? 18. Discuss the algorithm foe straight insertion sorting? The Design and Analysis of Algorithms - Dexter C Kozen, Publisher Springer Algorithms -MH Alsuwaiyel, Publisher World Scientific Fundarnentals of Computer Algorithms ~ Ellis Horowitz and Sartaj Sahni, Publisher Galgotia Algorithms and Dala Structures - Niklaus Wirth, Publisher PHI 4 INTRODUCTION In this block, we will learn about the Basic Problem Solving Methods like Sub goal, Hill-Climbing and Working Backward with Example - jeep Problem. We also leam Backtrack Programming with Example - Bicycle Lock Problem, Branch and Bound with Example - Traveling Salesman Problem for five-city, Network-Recursion with Example - Factorials and Fibonacci Series and Ackermann’s Function. Unit 3: This unit deals with the above contents UNIT- 3 ALGORITHM DESIGN METHODS Structure Overview Learning Objectives 3.1 Basic Problem Solving Methods 3.1.1 Sub Goals, Hill Climbing and Working Backward 3.1.2 Jeep Problem 3.2 Backtrack Programming 3.2.1 Bicycle Lock Problem 3.3 Branch and Bound Problem 3.4 Recursion 3.4.1 Factorials and Fibonacci Numbers 3.4.2 Ackermann's Function Let Us Sum Up Answer To Leaining Aciivities References OVERVIEW _ __ We will learn more about the Basic problem solving techniques like Sub goal, Hill-Climbing and Working Backward, Backtrack Programming, Branch and Bound, network- Recursion and Ackermann’s Function with examples. The example will illustrate the general principle for the statements. 76 LEARNING OB. After completing this unit one can explain * The different fundamental algorithm design and problem solving techniques © About the backtracking and branch and bound techniques and can be implemented into the large combinatorial problems * About recursion techniques 34 BASIC PROBLEM SOLVING METHODS _ In this section, the basic algorithm des ign techniques like sub goals, hill climbing and working backward are discussed with example. 3.1.4 Sub goals, Hill climbing and working backward ‘The sub goal method is a method, which involves the reduction of a difficult problem into a sequence of simpier problems. The simpler problems are very simple to solve and also solutions of the simpler problems can be organized inio a solution for the original problem. There is no general rule to identify the method to solve a problem. Only asking questions can identify the method. Sub goals can often is identified from answers to the following. 1. Can we solve part of the problem? Is it possible to ignore some conditions and solve the rest of the problem? 2 Can we solve the problem for special cases? Is it possible to design an algorithm, which yields a solution that satisfies all the conditions of the problem, but whose inputs are restricted to a subset of all inputs? 3. Is there remaining about the problem which we do not fully understand? if we strive for a deeper understanding of some feature of the problem, can we leain something that may help us to start moving toward a solution? 4. Have we seen a similar problem whose sdlution is known? Could this solution be modified to solve the problem? Is it possible that the problem is equivalent to a known unsolved problem? The second method io design an algorithm is hill climbing, This method starts with a initial guess or computed value, then proceeds to move “uphill” toward better solutions as quickly as possible. One cannot always guarantee that the final solution produced by hill climbing algorithm will be optimal. This ‘defect’ often limits the applicability of the hill climbing method. After reaching a point, it is no longer possible to move uphill, then the algorithm terminates. Sometimes, the result produced by this method will not be optimal. This is the disadvantage of this method. Generally, hill-climbing method is greedy. Because this method move only towards a predefined goal, This tends to make them a bit shortsighted. This method is applicable for the problems, which needs fast and approximate solution. The Backward method starts from a goal or solution and work backward towards the initial statement of the problem. Then, if these steps are reversible, work forward from the problem statement to the solution. 3.1.2 Jeep problem The objective of the problem is to move a jeep across 1000-mile desert using a minimum of fuel. The fuel capacity of the jeep is 500 gallons and it consumes 4 gallon per mile. The initial position has an unlimited reservoir of fuel, Since there are no fuel depots in the desert, we must use the vehicle to set up and stock its own storage points from its tank. Where do we set. up the fuel storage and how much fuel should be left at each? Approach this problem by working backward. How far back can we start with exactly k full loads of fuel and be able to make it to the end of the desert? This question can be asked for k=1,2,.... until we find an integer n such that n full loads will enable us to cross the entire 1000-mile desert. For k=1, the answer is 500 miles. The vehicle can be filled at point 8 and cross the remaining 500-miles of desert. Here the jeep can be started with 500 gallons of fuel. Now set some sub goal for the problem by asking some questions. The question may be how much fuel does the vehicle need to travel a given distance? How far can it travel on a given amount of fuel? The candidate answer for the first question is available and the answer to the second question is at least 1000 miles. that is two loads (1000 gallons) are Suppose available, This case will be handled by building on the result for k=1. What is the maximum value of x; such that if we start with 1000 gallons at 500- x; , it will be possible to move enough fuel to B to enable the trip to be completed as in the k=1 case? a) C+- 1000 miles x1 500 miles XQ +4 A ~ Omiles The jeep problem: cross the desert from A to C. The value of x1 can be found as follows. Load up at position 500- x1, travel x; miles to B, and drop off every thing in the tank except x; gallons, which we use to return to position 500- x;. At this point we are empty. Now pick up the second full load, trave! x; miles to B, fill up at B with the fuel that was dropped off there, leave B with a full tank, and drive to C. The total number of miles traveled consists of three segments of x; miles and one segment BC of 500 miles. To make x; as large as possible, we use every drop of fuel Therefore, x; is found from 3x; +500 = 1000 (gallons) with the solution x; = 500/3. Thus, two loads (1000 gallons) enable us to cover Dz =500+x, = 500(1+ 1/3) miles 80 When k loads are available, try to move back as far as possible from the position we found for k-1 loads. Consider k=3, Where can we start with 1500 gallons so that the vehicle can move 1000 gations to position 500- x;? From the above figure, we seek the largest value of x2 such that starting with 1500 gallons at 500- x; - x2, we can move 1000 gallons to 500 - x; . We start at 500- x; x2 , move up to 500- x; , drop off all but x2 gallons, and return to position 500- x; - x» empty. If we repeat this, we will have used 4x. gallons in transport and deposited 1000-4x. gallons at 500- x1. Now there are exactly 500 gallons left at 500- x: - x2. Load up the last 500 gallons and drive to 500- x), usingx» gallons in the process, We are now at, 500- x; having used 5x2 _gallans in transporting fuel. The amount of fuel stored here is 1500- x2 ~ gallons. This must equal 1000; that is \ x2 = 500/5, We then conclude that 1500 gallons can get us Dy=500+ x14 Xp = 500(1+1/3+1/5) miles Continuing to work backward in an inductive fashion, we find that n loads enable our vehicle to travel D, miles, where Dn=800(1/1+4/3+1/5+. . 1/2n-1) The smallest value of n such that Dn >=1000. The simplé computation shows that for n=7, 07=977.5 miles. That is seven loads, or 3500 gallons, will enable us to travel 977.5 miles. An eighth load would be more than we need to move 3500 gallons from position A to a point 22.5 miles (1000 — 977.5) past A. you should be able to show that 337.5 gallons an be used transport 3500 gallons to the 22.5 mile point. This gives total fuel expenditure of 3337.5 gallons to move the Vehicle across the desert from A to C. . The algorithm can be given as follows: Start at A with 3837.5 gallons. b. There is enough fuel here to shuttle 3500 gallons to the 22.5 mile point, where we find ourselves with an empty tank and seven loads of fuel. c. There is enough fuel here to shuttle 3000 gallons - "to a position 22.5 + 500/13 miles fro A, where we fir ourselves with an empty tank d. The next shuttle gets us to 4 position 22.5 +500/13 “500/11 miles from A with an tank and 2500 gallons : Continuing this way, we work forward over our backward analysis. Ve soon find ourselves at the 00(1-1/3)=1000/3 mile point with 1000 gallons. We then shuttle 500 gallons to B, where we fil up and travel to C without stopping BACK) PROGRAMMING This algorithm design techniques can be described as an organized exhaustive search, which often avoids searching all possibilities. ‘his method is suitable for solving problems where a potentially large, but finite, number of solutions have to be inspected. 3.2.4 Bicycle iock Problem A bicycle consists of a combinational lock that consists of N switches, each of which can be “on” or “off”. By setting all the switches or |.N/2! switches in on condition the lock will open. Suppose the combination is forgotten and the lock must be open. Sometimes one may wants to know tiie combinations. In. this situation we need an algorithm to generate the combinations, The condition LN/2| ignored, and then there are 2" possible combinations for the lock. Each combination is presented as N-tuple of 0's and 1's. The ith digit is 1 if switch i is on and 0 if switch i is off. All the N-tuple is modeled using a binary tree, Each vertex on the kth level of this tree will correspond to a distinct setting of the first k digits of the N-tuple. The two branches downward from a vertex at this level correspond to the two possible settings for the (k+1)st digit in the N-tuple. Root Level 0 Level 11 110 101, Level 3 . é 1111 1130 104 1100 1811 1010 1001 4000/0111 0110 0101 Levela ‘oat1 bat0 6001 0000, A binary tree representing N- tuples of 0s and 1s "The condition that the umber of switches must be at least LN/2] enables us to avoid generating parts of the tree, which cannot possibly provide a correct combination For example, suppose we find ourselves at the -vertex marked 00 in the above diagram. Since the right branch (to 000) could not leads to an acceptable combination, there is no need to generate. If any of the successors of a particular vertex do not satisfy a restriction on the problem, then these successors need not be considered. In this particular problem, none of the vertices within: the dotted circles need to be investigated or even generated. Using this binary tree as a model, we can now state a backtrack procedure for generating only those combinations with at least. LN/2) switches on. This algorithm amounts to a traversal of a tree. Move down the tree as far as possible to the left until we can move no further. Upon reaching an end-vertex, try the corresponding combination if it fails, backtrack up one level and see if we can move down again along an unused branch. If it is possible, take the leftmost unuseid branch. If not, backtrack up one more level and try to move down from this vertex. Before moving down check to see if it is possible to satisfy the |N/2] restriction at any successor vertex. In the above diagram, the algorithm will visit the following sequence of vertices. . 1 1" 4 1111. check this combination 114 1110 411 1 110 1101 110 1100 110 1, 10 101 1011 101 1040 101 10 100 1001 100 10 root Backtrack since 1111 is an end- vertex Take the only unused branch down; check this combination Backtrack again Backtrack further since 111 has no unused branches Take the only unused branch check combination backtrack from 1101 check combination backtrack from 1400 backtrack since no unused branches: backtrack since no unused branches take only unused branch take leftmost unused branch check combination backtrack check combination backtrack backtrack take only unused branch check combination backtrack backtrack; note that we do not move _down to to 1000 since this left successor violates the constraints that we have at least two 18, backtrack backtrack | i 0 take only unused branch ete. This algorithm terminates when we return to the root, and there are no unused branches remaining, 3.3 BRANCH AND BOUND PROBLEM The branch and bound method is as like the backtracking method. The backtracking method searches a tree model of the solution space and is applicable to a wide variety of discrete combinational problems. Backtracking algorithms finding one ar all configurations which satisfy certain properties, Branch-and-Bound algorithms are oriented more toward optimization. The problem being solved specifies a real-valued cost function for each of the vertices that appear in the search tree. The goal is to find a configuration for which the cost function is maximized or minimized Traveling salesman problem is the example for the branch and bound method. The problem is to find a least cost tour of the 1 cities in his sales region. The tour is to visit each city exactly once. In network terminology, a least cost spanning cycle of the network of cities has to be found. To find the optimum tour, the salesman has a cost matrix C, where element c; equals the cost of direct travel between city i and city j. The problem is cated symmetric if ¢; = ¢) for all i and j that is if the cost of traveling between every pair of cities is the same regardless of direction. Assume oj = a for all i. 86 A traveling salesman problem: a) cost matrix (b) five-city network Let us first consider branching. The cost matrix and the network are in the above diagram. The root of the search tree will correspond to set of all possible tour's that is this vertex represents the set of all 4! Possible tours in our five-city problem. For any asymmetric N-city problem, the root will represent the entire set R of (N-1)! Possible tours. The branches from the root are determined by the selection of a 87 ~ingle edge, say (ij ). The objective is to split the set of all tours: into two steps: One which is very likely to contain an optimum tour and one which is not. To do this we choose an edge (i) which we hope will be in an optimum tour, and partition R into two sets {ij} and {ij} : The paths to any tree vertex from the rule specify certain edges which must or must not be included in the sets represented by the tree vertex. Each vertex in the tree is associated with a lower bound cost of any tour in the set represented by the vertex. The computation of this lower bound is the major labor saving device in any branch and bound algorithm, Therefore much thought should be given to obtaining tight bounds. The reason is: assume that we have construct a specific complete tour with cost, m. If the lower bound associated with the set of tours represented by vertex vy equals M>=m, we need never consider vertex v, or any of its descendants in the reminder of our search for an optimum The basic in the computation of lower bounds is known as reduction. It is based on the following two abservations 1 In terms of the cost matrix C, every two contains exactly one element from each row and each column. A set of elements choose and search that there is exactly one elemenl in each column and row of C does not necessarily represent tour. For example, in the above diagram the set {(1,5), (6,1), (2,3), @.4), (4,2)}satisfies this condition but does not form a tour. 2. Aconstant h is subtracted form each entry of any row or column of the cost matrix C, the cost of any tour under the new matrix C’ is exactly h less than the cost of the same tour under C. Since any tour must contain an edge from this row or column, we have 8 reduced the cost of all tours by h. This subtraction is catled a row or column reduction. Let t be an optimum tour under the cost matrix C. Then the cost of tour t is z(t) = ¥ oj (ij are the elements of t) If C’ is a row reduced or column reduced version of C then t must still be an optimum tour under C’ and z()=h+z() where z'(t) is the cost of tour t under C'. By a reduction of the entire cost matrix C we mean the following. Sequentially go down the rows of C and subtract the value of each rows smallest element hj from every element in the row, Then do the same for each column. If hy = 0 for any row or column just go on to the next row or column since this one is already reduced. + Let h = hi (All rows and columns) The resulting cost matrix will be called the reduction of C. The following diagram is the reduction of the cost matrix of the initia! cost matrix. The values hj or at the end of each row and column. The total reduction is h = 47 units. Therefore a lower bound and the cast of any tour in R is also 47, That is z()=h+z() >=h=47 since z' (t) > = 0 for the any tour t under the reduce matrix C’, 89 ho=0 Wr=0 aD h=2+541+6+74+3=47 , y . The reduction of the cost matrix for the matrix which is in the above diagram Consider lower bounds for the vertices at level 1, that is, for the sets {3,5} and {3,5). We shall work with the reduced matrix. While keeping in mind that cost of 47 must be added to the cost of any optimum tour f under C’ in order to obtained the true cost of t under C. The edge (3,5) is, by definition, in every tour in set {3,5}. This fact prohibits our choice of edge (5,3) since edges (3,5) and (5,3) form a cycle, and this is not permitted in any tour. Edge (5,3) is avoided by setting 0:3 = « Row 3 and column 5 can also be deleted from further consideration with respect to set {3,5}. This is because we already have an edge from 3 to 5. The part of the reduced cost matrix in which will be of any use in further searching of the tour set {3,5} is shown in the following diagram. It can then be reduced to the cost matrix shown in cost matrix, with b = 15. A lower bound for any tour in the set {3,5} is now 47 + 15 = 62 9% A lower bound for the set {3,5} is obtained in @ somewhat different fashion. Edge (3,5) cannot be in this set, so set cag = oc in the matrix. Any tour in (3,5} will use some edge from city 3 and some edge into city 5. The cheapest edge from city 3, excluding the old value of (3,5), has cost 2; the cheapest edge to city 5 has value 0. Therefore, a lower bound on any tour in set {3,5} is given by 47 +2 +0 At this stage we have managed to reduce the size of the cost matrix to be considered at vertex {3,5}. Also, if we can find an explicit tour in the set {3,5) with cost less than or equal to 62, then there is no reason for further branching and bounding on vertex {3,5} In this case we say vertex {3,5} is fathomed in the tree, The next objective might then be @ branching on vertex {3,5} in the hope of finding an explicit tour with cost c in the range 49 < ¢ < 62. A rough flowchart for this branch-and-bound algorithm is shown in the following diagram. We shall fill in a number of important details shortly. Some of the notation used is as follows. The letter X will denote the current vertex in the search tree, and w(X) will be the corresponding lower bound, The successors to X will be called Y and Y, which are chosen by branching on some edge (k, |). The symbol 2p is the value of the cheapest currently known tour. Initially 2 = vc Spend some time studying this flowchart. n 1 ‘ile wrables Read in data Redice Cactvp 2 root, X=R b 3) Choa age. For nex ranching Branching proces Setupyerex y 4. | compute wis) i Branching press Setup vertex y 5 | compute wis) 11 | UpditeC tor cuent a Ts the current cost matin small ? Fatbom vertex ¥ by exhaustion 9 [| seloet next x fom ‘which to rane IF) <0, set Z0™ wis) fave tour ae Yves ‘A rough flowchart for a branch-and-bound algorithm Now let us develop the content of some of the boxes in our flowchart in more detail. Box 1: The initialization is straightforward, and the details are left as an exercise, 92

You might also like