Upcoming SlideShare
×

# Algorithm

1,425 views

Published on

algorithm notes for computer science. its very importnant.

Published in: Education
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,425
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
63
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Algorithm

1. 1. SYLLABUSAlgorithms, Abstract Data Type, The Running Times Of a Program, Good Programming Practice,The data type, Implementation of lists, Array implementation of list, Pointer implementation oflist, Doubly-link lists, Stack, Queues, Mapping.Sets, An ADT with union, intersection and difference, Bit vector, implementation of sets, Link-list implementation of sets, The data dictionary.Efficiency of algorithms, Analysis of recursive programs, Solving recurrence equation, Divide and conquer algorithms, Dynamic programming, Greedy algorithm, Prim’s algorithm, Kruskal’s algorithm, Dijkstra’s method, Backtracking.Basic terminology, Implementation of tree, An Array Representation of Trees, Representation of Trees by List of Children, Binary trees, The internal sorting model, Bubble sort, Insertion sort, Selection sort, Heap sort, Divide and conquer, Merge sort, Quick sort, Binary search.A Model of External Computation, External sorting, Characteristics of External Sorting, Criteria for Developing an External Sorting Algorithm, Important Uses of External Sorting, Merge Sort--A Digression, Top-Down Strategy, Bottom-Up Strategy, Storing Information in Files, Hashed Files, Indexed FilesThe Issues in Memory, Garbage Collection Algorithms For Equal-Sized Block, Collection in Place, Buddy system, Distribution of blocks, Allocation blocks, Returning blocks to available storage, Storage compaction and Compaction problemIntroduction to NP Problem, Polynomial-time, Abstract Problems, Encoding, NP-Completeness andReducibility, NP-Completeness, Circuit Satisfiability, NP-Complete Problems, The Vertex-coverProblem, The Subset-sum Problem, The Hamiltonian-cycle Problem, The Traveling-salesmanProblem PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 1
2. 2. TABLE OF CONTENTSUNIT 1Basic of Algorithms1.1 Algorithm1.2 Abstract Data Type1.3 The Running Times Of a Program1.4 Good Programming PracticeUNIT 2Basic Data Type2.1 The data type “list”2.2 Static and Dynamic Memory Allocation2.3 Pointers2.4 Implementation of lists2.5 Linear Linked List2.6 Array implementation of list2.7 Pointer implementation of list2.8 Doubly-link lists2.9 Stack2.10 Queues2.11 MappingUNIT 3Basic Operations and Sets3.1 Sets3.2 An ADT with union, intersection and difference3.3 Bit vector implementation of sets3.4 Link-list implementation of sets3.5 The data dictionaryUNIT 4Algorithms Analysis Techniques4.1 Efficiency of algorithms4.2 Analysis of recursive programs4.3 Solving recurrence equationUNIT 5Algorithms Design Technique5.1 Divide and conquer algorithms5.2 Dynamic programming5.3 Greedy algorithm5.4 Minimum-cost spanning trees5.5 Minimum Spanning Tree5.6 Prim’s Algorithm5.7 Kruskal’s Algorithm5.8 Shortest Paths5.9 Dijkastra’s Algorithm5.10 Backtracking PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 2
3. 3. UNIT 6Trees and Sorting6.1 Basic terminology6.2 Implementation of tree6.3 An Array Representation of Trees6.4 Representation of Trees by List of Children6.5 Binary trees6.6 The internal sorting model6.7 Bubble sort6.8 Insertion sort6.9 Selection sort6.10 Heap sort6.11 Divide and conquer 6.11.1 Merge sort 6.11.2 Quick sort 6.11.3 Binary searchUNIT 7Algorithms for External Storage7.1 A Model of External Computation7.2 External sorting7.3 Characteristics of External Sorting7.4 Criteria for Developing an External Sorting Algorithm7.5 Important Uses of External Sorting7.6 Merge Sort--A Digression7.7 Top-Down Strategy7.8 Bottom-Up Strategy7.9 Storing Information in Files7.10 Hashed Files7.11 Indexed FilesUNIT 8Memory Management8.1 The Issues in Memory8.2 Garbage Collection Algorithms For Equal-Sized Block8.3 Collection in Place8.4 Buddy system8.5 Distribution of blocks8.6 Allocation blocks8.7 Returning blocks to available storage8.8 Storage compaction and Compaction problemUNIT 9NP Complete Problem9.1 Introduction9.2 Polynomial-time9.3 Abstract Problems9.4 Encoding9.5 NP-Completeness and Reducibility9.6 NP-Completeness9.7 Circuit Satisfiability9.8 NP-Complete Problems PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 3
4. 4. 9.9 The Vertex-cover Problem9.10 The Subset-sum Problem9.11 The Hamiltonian-cycle Problem9.12 The Traveling-salesman Problem PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 4
5. 5. UNIT 1 Basic of Algorithms1.1 Algorithm1.2 Abstract Data Type1.3 The Running Times Of a Program1.4 Good Programming Practice1.1 Introduction to AlgorithmsComputer Science can be defined as the study of algorithms. This study encompasses four distinctareas: • Machines for executing algorithms. • Languages for describing algorithms. • Foundations of algorithms. • Analysis of algorithms.It can be easily seen that "algorithm" is a fundamental notion in computer science. So it deservesa precise definition. The dictionarys definition, "any mechanical or recursive computationalprocedure," is not entirely satisfying since these terms are not basic enough.Definition: An algorithm is a finite set of instructions, which, if followed, accomplish a particulartask. In addition every algorithm must satisfy the following criteria: 1. Input: there are zero or more quantities which are externally supplied; 2. Output: at least one quantity is produced; 3. Definiteness: each instruction must be clear and unambiguous; 4. Finiteness: if we trace out the instructions of an algorithm, then for all cases the algorithm will terminate after a finite number of steps; 5. Effectiveness: every instruction must be sufficiently basic that a person using only pencil and paper can in principle carry it out. It is not enough that each operation is definite, but it must also be feasible. In formal computer science, one distinguishes between an algorithm, and a program. Aprogram does not necessarily satisfy the fourth condition. One important example of such aprogram for a computer is its operating system, which never terminates (except for systemcrashes) but continues in a wait loop until more jobs are entered. An algorithm can be described in several ways. One way is to give it a graphical form ofnotation such as. This form places each processing step in a "box" and uses arrows to indicate thenext step. Different shaped boxes stand for different kinds of operations. If a computer is merely a means to an end, then the means may be an algorithm but theend is the transformation of data. That is why we often hear a computer referred to as a dataprocessing machine. Raw data is input and algorithms are used to transform it into refined data. PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 5
6. 6. So, instead of saying that computer science is the study of algorithms, alternatively, we might saythat computer science is the study of data.1.2 Abstract Data Type: ADT is kind of data abstraction where a types internal form is hidden behind a set ofaccess functions. Values of the type are created and inspected only by calls to the accessfunctions. This allows the implementation of the type to be changed without requiring anychanges outside the module in which it is defined. Objects and ADT are both forms of dataabstraction, but objects are not ADT. Objects use procedural abstraction (methods), not typeabstraction. A classic example of an ADT is a stack data type for which functions might be provided tocreate an empty stack, to push values onto a stack and to pop values from a stack. ADT A type whose internal form is hidden behind a set of access functions. Objects of thetype are created and inspected only by calls to the access functions. This allows theimplementation of the type to be changed without requiring any changes outside the module inwhich it is defined. Abstract data types are central to object-oriented programming where every class is anADT.1.3 The Running Time of a Program When solving a problem we are faced frequently with a choice among algorithms. On whatbasis should choose? There are two often-contradictory goals. 1. We would like an algorithm that is easy to understand code and debug. 2. We would like an algorithm that makes efficient use of the computers resources, especially, one that runs as fast as possible. Measuring the running time of a program: The running time of a program depends on factors such as: 1. The input of a program. 2. The quality of a code generated by the compiler used to create the object program. 3. The nature and speed of the instructions on the machine used to execute the program. 4. The time complexity of the algorithm underlying the program.Calculating the Running Time of a Program Some Important Formulas for finding the Running Time of a program are the following: When finding the Running Time of a Program with that has Scary nested loops its a goodthing to know how Summations work and why you use Summations. We use summations because of what a Summation stands for is very similar to how a loopis run. PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 6
7. 7. Some important properties of Summations are good to review in order to make it easier towork with them. The following property shows one way of manipulating summations to work toyour advantage in certain questions.Another important aspect of summations is how to manipulate the equation properly to fit thefirst helpful equations given above.Some rules for finding the Running Time that come are following: PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 7
8. 8. A good way to understand how Big Oh works is to look at the following diagram. Expression Name O(1) constant PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 8
9. 9. logarithmic log squared O(n) linear N log n Quadratic cubic exponential Table: The Names of Common Big Oh Expressions1.4 Good Programming Practice: There are a substantial number of ideas we should bear in mind when designing an algorithmand implementing it as a program. These ideas often appear platitudinous, because by and largethey can be appreciated only through their successful use in real problems, rather than bydevelopment of theory. 1. Plan the design of a program. This strategy of sketch-then-detail tends to produce a more organized final program that is easier to debug and maintain. 2. Encapsulate. Use procedures and ADT’s to place the code for each principal operation and type of data in one place in the program listing. Then, if changes become necessary, the section of code requiring change will be localized. 3. Use or modify an existing program. One of the chief inefficiencies in the programming process is that usually a project is tackled as if it were the first program ever written. One should first look for an existing program that does all or a part of the task. 4. Program at the command level. A well-designed operating system will allow us to connect a network of available programs together without writing any programs at all, except for one list of operating system commands. To make command compos able, it is generally necessary that each behave as a filter, a program with one input file and one output file. PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 9
10. 10. UNIT 2 Basic Data Type2.1 The data type “list”2.2 Static and Dynamic Memory Allocation2.3 Pointers2.4 Linear Linked List 2.4.1 Array implementation of list 2.4.2 Pointer implementation of list 2.4.3 Doubly-link lists2.5 Stack2.6 Queues2.7 Mapping2.1 The Data Type “List” Using ADT’s allows the data in a specific piece of code to be hidden from other pieces ofcode that dont need and shouldnt have access to it. This is often called modular programming orencapsulation. The idea behind the ADT is to hide the actual implementation of the code, leavingonly its abstraction visible. In order to do this, one needs to find a clear division between thelinked list code and the surrounding code. When the linked list code is removed, what remains isits logical abstraction. This separation makes the code less dependent on any one platform. Thus,programming using the ADT method is usually considered a necessity for cross-platformdevelopment as it makes maintenance and management of the application code much easier. The ADT concept is developed here via the example of how the doubly linked listApplication Programming Interface (API) was created. The doubly linked list (DLL) can also be anindependent C module and compiled into a larger program. An abstract data type (ADT).2.2 Static and Dynamic Memory Allocation: - In static memory allocation memory is allocated at compile time. If we declare a stackthrough an array of 100 elements (all integers) then the statement will be: int stk[100]; This declaration would typically be used if 100 records are to be stored in memory. Themoment we make this declaration 200 bytes are reserved in memory for storing 100 integers in it.However it may so happen that when we actually run the program we might be interested in PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 10
12. 12. Link can be provided through a pointer that indicates the location of the node containing thesuccessor of this list element. The NULL in the last node indicates that this is the last node in thelist.2.4 Linked List2.4.1 Array Implementation of List: - This type of implementation of a linked list is sometimes useful. It suffers from the usualarray problem of having to estimate the needed size of the array in advance. There are some waysaround this problem (such as using a vector instead of an array, allocating space for the arraydynamically, or even allocating space for additional arrays as needed), but they have their owncomplications. We will assume here that the maximum size needed can be foretold. The array willhold a fixed number of records (structures), each of which represents a node. In this implementation, a linked list object contains an array of nodes, perhaps calledNodeArray, and four integers fields labelled Avail, Front, Rear, and Count. Each record inNodeArray contains an Info field and an integer Next field. Avail, Front, Rear, and each Nextfield all contain fake pointers. What we use as a fake pointer to a node is the array index of thatnode. To represent the NULL pointer we can use an illegal array index, such as -1. We will notstart our list with a dummy node, though it is possible to do so by making a few small changes.The picture of an empty list is as follows: The idea is that Front points to the first node of the list. Since it is -1, our imitation NULLpointer, this means that we have an empty list. The Count of zero also indicates the same thing.Rear is -1, indicating that there is no node at the rear of the list (as the list is empty). The Availfield is intended to point to a list of free nodes. The programmer must arrange to manage freespace within the array with this method! In out picture above, all of the nodes are free and arelinked together in a list with Avail containing zero and thus pointing at the first free node,NodeArray[0]. Then this node has a Next value of 1, showing that it points to NodeArray[1], etc.Finally, NodeArray[4].Next contains our imitation NULL pointer to mark the end of the list. After items have been dynamically inserted into the list and perhaps items have beendeleted as well, we might end up with a picture such as the following. PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 12
13. 13. Can you read what is on this list? All you do is follow the (fake) pointers. Since Head contains 1,the first node contains the string "Ann". This node contains a pointer to node 0, so the secondnode contains "Sam". This node contains a pointer to node 4, so the third node contains "Mary".Finally, this node has -1 in the Next field, marking the end of the list. So the three data items inthe list are "Ann", "Sam", and "Mary". If you need a quick way to get to the last node on the list,you use the 4 found in the Rear field. The list of available (free) nodes contains node 2 and node3. Note that such a list object always contains two linked lists: the list of data nodes and the listof available (free) nodes. It is left as an exercise to the reader to implement a linked list class usingthis array-based scheme.2.4.2 Pointer Implementation of List: - It is very common to implement a linked list using pointers and either structures orobjects for the nodes. We will choose objects for the nodes and set up the class ListNodeClass asshown below.class ListNodeClass { private: ItemType Info; ListNodeClass * Next; public: // First, the constructor: ListNodeClass(const ItemType & Item, ListNodeClass * NextPtr = NULL): Info(Item), Next(NextPtr) { }; void GetInfo(ItemType & TheInfo) const; friend class ListClass; // very convenient to allow this };typedef ListNodeClass * ListNodePtr; Note that the two data fields are private, as usual. The type ItemType would have to be setup to handle whatever data we want to hold in the list. The Next field is a pointer to anotherListNodeClass object, since the * refers to whatever Next points to. The most important function is the constructor. It takes a data item and a pointer as itsparameters and uses them to fill in the fields of the object being constructed. Note that the pointerparameter has a default value of NULL. Recall that the Info(Item), Next(NextPtr) means to copy PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 13
14. 14. Item into Info and NextPtr into Next. The GetInfo function simply sends back a code of the datafrom the object. The ListNodeClass is also told that it has a friend class named ListClass. Functions of thefriend class have direct access to the private data fields (Info and Next) of ListNodeClass objects. Itis not necessary to allow this direct access, but it is easier than the alternative: using get and setclass functions every time a ListClass function wants to look at or change the private fields of aListNodeClass object. Some people refuse to use friend classes, however, because they give moreaccess than is really necessary, thus partially defeating the information hiding that class provide. Finally, note that the above code sets up ListNodePtr as a convenient type name for apointer to a node. This means that we wont have to use ListNodeClass * from here on in the restof the code, but can instead use ListNodePtr. Next, lets set up a class declaration for list objects. Each list will have three private datafields: a pointer to the first node on the list, a pointer to the last node on the list, and a count ofhow many data nodes are in the list.The Front pointer will point to what is called a dummy node. This is a node containing no data. Itis easier to code the insertion and deletion operations if the first node on the list contains no data.Otherwise, the code to insert a new first node or to delete the current first node must be differentfrom the code to handle the normal cases. The following is the class declaration for ListClass. Helping functions, which are only usedby other ListClass functions are made private, so that they cannot be accessed otherwise. (This isanother instance of information hiding.) There is a comment that the three data fields aresometimes made into protected fields. The purpose of this is that if we ever created a subclass(using inheritance), functions of the subclass would be able to directly access these fields. Withprivate fields, the fields would be inherited, but not directly accessible by the subclass. Since wedo not plan to inherit a new class from ListClass, this is not a concern.class ListClass { private: ListNodePtr GetNode(const ItemType & Item, ListNodePtr NextPtr = NULL); void FreeNode(ListNodePtr NodePtr); void ClearList(void); // Next 3 are sometimes made into protected fields: ListNodePtr Front, Rear; int Count; public: // constructor: ListClass(void); // destructor: ~ListClass(void); int NumItems(void) const; bool Empty(void) const; void InsertFront(const ItemType & Item); void InsertRear(const ItemType & Item); void InsertInOrder(const ItemType & Item); ItemType RemoveFront(void); ListNodePtr Find(const ItemType & Item) const; }; The public functions above provide the usual linked list operations. The above classdeclaration thus shows that, from an abstract data type point of view, the list operations are listcreation, list destruction, getting the number of items in the list, checking if the list is empty,inserting an item at the front of the list, inserting an item at the rear of a list, inserting an iteminto an ordered list, removing an item from the front of a list, and finding an item in the list.Certainly other operations are possible. For example, why not have an operation to remove theitem from the rear of a list? (One practical reason not to do so is that it is not easy to adjust theRear pointer after deleting the last node and to get a NULL into the Next field of the new last node. PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 14
15. 15. The only way to do so appears to be traverse the list from the first to the last node, beforeremoving the final node, in order to get a pointer to the next to the last node!) A ListClass object, named List is pictured below. Functions are not shown in the objects inorder to keep the diagram from becoming cluttered, but if they were, the private functions shouldbe shown as completely boxed in, whereas the public function should be shown in open boxes toindicate that they are available for public use. Note that a ListClass object doesnt really containthe data in the list. Rather, the data is contained in node objects that are outside of the List objectitself. The List object simply contains pointers to the first and last nodes of the list. The complete code files for this example are given below. The last file contains a simpletest program to try out several linked list operations. This test program is straightforward and willnot be commented upon here. Read through these files to get the general idea, and then go on tothe notes that follow. In the next to the last file above, you will note that the constructor initializes a ListClassobject to contain a Count of zero, and to have Front and Rear pointers that point to a dummynode. Of course, this means that it has to create a dummy node. This is handled by the helpingfunction GetNode. In turn, GetNode uses new with the ListNodeClass constructor to dynamicallyallocate a new node. Since a ListClass object points to other objects outside of it, it is important to have adestructor to clean up the space used by these ListNodeClass objects. The destructor uses theClearList function to reclaim the space used by the data nodes and then calls FreeNode(Front) toget rid of the dummy node. The ListClass object itself is automatically removed. The FreeNode function is trivial in that it just uses delete to remove the node pointed to.The ClearList function, however, is more interesting. Lets look at it in more detail since it is agood example of a list processing function./* Given: Nothing. Task: Clear out all nodes on the list except the dummy. The list is thus set to an empty list. Return: Nothing directly, but the implicit ListClass object is modified.*/void ListClass::ClearList(void) { ListNodePtr Current, Temp; Current = Front->Next; while (Current != NULL) { Temp = Current; Current = Current->Next; FreeNode(Temp); } Front->Next = NULL; Rear = Front; Count = 0; } In essence the idea is to traverse the list, freeing up each data node as it is encountered.The Current pointer is used to point to the successive nodes on the list. Note that it is initializedto point where the dummy nodes Next field points, that is, to point to the first data node. Then wehave a while loop. Inside of it, the Current pointer is copied into Temp, Current is advanced byplacing in it a copy of the Next field of the node we have been pointing at, and FreeNode is used to PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 15
16. 16. free up the node that Temp points to. Notice that we cant simply free the node that Currentpoints to each time around the loop. If we did that we would lose our pointer into the linked listand have no way to advance to the next node. The loop stops when Current is advanced to pickup the NULL value marking the end of the list. After the loop ends, the ListClass object is adjustedso that the Front and Rear pointers are NULL and the Count is zero. In other words, the list objectis changed into the typical picture of an empty list.Lets look at a few more of the list manipulation functions, starting with InsertFront./* Given: Item A data item. Task: To insert a new node containing Item at the front of the implicit ListClass object. Return: Nothing directly, but the implicit object is modified.*/void ListClass::InsertFront(const ItemType & Item) { ListNodePtr NodePtr; NodePtr = GetNode(Item, Front->Next); Front->Next = NodePtr; if (Count == 0) Rear = NodePtr; Count++; }It is clear that the GetNode function is being used to create a node to hold the new data item. Thesecond parameter to this function is used to initialize the Next field of this new node so that itpoints to the first data node. At this point we have the following picture: To finish things off, the function adjusts the Next field of the dummy node so that it pointsto the new node. The Count field for the list also needs to be incremented. That finishes things forthe example in our picture.Unfortunately, it is not always quite that simple. There is a special case lurking in thebackground. What happens if we insert at the front of an empty list? Remember that an empty listlooks like this: PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 16
17. 17. The steps we previously did to insert a new node at the front of the list are still fine, butthey leave one thing out: they do not adjust the Rear pointer, currently pointing at the dummynode, to point to the newly inserted node. That is handled by the if in the code above.You may not have noticed it, but the ListClass functions are directly accessing the private datafields of the ListNodeClass objects. That would not normally be allowed, but it is legal herebecause we made ListClass to be a friend of ListNodeClass. Next, lets look at the InsertInOrder function. Note that it assumes that the list is alreadyin order./* Given: Item A data item. Task: Insert Item into a new node added to the implicit ListClass object (assumed to already be in ascending order based on the Info field) so as to maintain the ascending order. Return: Nothing directly, but the implicit object is modified.*/void ListClass::InsertInOrder(const ItemType & Item) { ListNodePtr Current, Last, Temp; bool Proceed; Temp = GetNode(Item); Current = Front->Next; Last = Front; Proceed = true; while (Proceed && (Current != NULL)) if (Item > Current->Info) { Last = Current; Current = Current->Next; } else Proceed = false; Last->Next = Temp; Temp->Next = Current; if (Current == NULL) // item was inserted at rear of list Rear = Temp; Count++; } Suppose that we have an ordered list containing 4, 6, 9, and that we want to insert 8using the above function. Note that it moves a pair of pointers, Current and Last, through the list, PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 17
18. 18. with Last always pointing to the node before the one that Current points to. Current starts out bybeing initialized to point to the first data node, while Last points to the dummy node. Each timearound the while loop a check is made to see if the 8 is greater than the item contained in thenode that Current points to. If so, the pointers are advanced so that each points to the next node.If not, then the place to insert has been found, so the loop is stopped by setting the Proceed flag tofalse. The picture at the point where the insertion location has been found is shown below:All that remains to be done is to link in the new node, up the Count by 1, and check for anyspecial cases. It turns out that the only unique case is when the new item ends up going at thevery end of the list. In such a case the Rear pointer must be adjusted (as shown in the codeabove). The Remove Front function is simpler. However, it exits with an error message if there isno data item to remove. If not, it sets up NodePtr to point to the first data node and extracts thevalue from this nodes Info field. It then adjusts the Next field of the dummy node so that it pointsto the second data node, skipping around the first one. The Count is decremented and the firstdata node is freed up. Not surprisingly, there is a special case. This occurs when the list only hasone data node. After removing it, the Rear field must be adjusted to point to the dummy node.2.4.3 Doubly-Link lists: - A doubly linked list has two pointer fields in each node, often named Left and Right. Ifthe linked list is pictured as a horizontal sequence of nodes, the Right pointers are used totraverse the lists from left to right (that is, from beginning to end). However, the Left pointers canbe used to back up to the left whenever that is desired. The following is one possible picture of adoubly linked list. Sometimes dummy nodes are added at the front and rear as well. PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 18
19. 19. One can thus traverse a doubly linked list in both the forward and backward directions.This can make coding simpler, for example, when writing a sequential search function to find agiven node in the list, with the plan that we will use this to find the node after which to insert anew node. With a singly linked list we needed to return both a pointer to the node found and apointer to the previous node. With a doubly linked list it suffices to return a pointer to thematching node, since we can always follow its Left field to get at the previous node.Layout of Doubly Linked List in memory The arrows indicate to what the Prior and Next pointers point. The Current pointer canpoint to any Node Struct, so it is open-ended. In order to fulfill the first requirement, I decided to strictly adhere to the ANSI C standard,and, with the possible exception of how one sets up ones data and uses the DLLs input/outputfunctions, there should be no endian (byte order) problems. The second requirement was met withthe creation of a top-level structure. There is only one of these structures per linked list. It keepstrack of the node pointers, the size of the applications data in bytes, how many nodes are in thelist, whether or not the list has been modified since it was created or loaded into memory, wheresearching starts from, and what direction a search proceeds in. Figure 1 illustrates how the top-level structure is integrated into the DLL.typedef struct list { Node *head; Node *tail; Node *current; Node *saved; size_t infosize; unsigned long listsize; DLL_Boolean modified; DLL_SrchOrigin search_origin; DLL_SrchDir search_dir; } List; This and the next typedef structure remain hidden from the application program. Thenode pointers mentioned above are defined in the next structure, which includes the pointers tothe applications data and the pointers to the next and prior nodes. One of these structures iscreated for each node in the list.typedef struct node { Info *info; struct node *next; struct node *prior; PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 19
20. 20. } Node; The last definition is a dummy typedef of the users data. It is defined as type void so thatthe DLLs functions will be able to handle any of Cs or an applications data types.typedef void Info; As you can see, if the two structures mentioned above are hidden from the application, allof the ugly innards of how the linked list operates will by default be hidden from the application.Thus, we have an abstract data type.2.5 STACK: - A stack is an abstract data structure that consists of an ordered collection of items withtwo basic operations called push and pop. The push operation appends a single new item into thestack collection. The pop operation removes a single item off the stack collection, but in thereverse order that the item was added using the push operation. We can model a stack data structure in terms of a pipe that has one end open and theother closed. The push operation places items into the pipe from the open end. The popoperation removes an item only from the open end. Assuming the items do not juggle around inthe pipe, the items placed into the pipe can only be removed in the order that they are placed intothe pipe. Although, in theory the stack is like an infinite length pipe that can hold an endlessnumber of items, in practice, due to the finite memory available on a computer system, the stackhas a maximum size. This is called the maximum stack size. In theory, one never needs totrack the number of elements currently in the stack collection, but in practice this is called thecurrent stack size. The operation of performing a pop on an empty stack is given a special errorcalled a stack underflow. Since in practice, a stack can only contain a finite number of elements,an error called a stack overflow occurs when the stack is full and a push operation is performed.Stack will work on LIFO application.Adding into stackprocedure add(item : items){add item to the global stack stack;top is the current top of stack and n is its maximum size}begin if top = n then stackfull; top := top+1; stack(top) := item;end: {of add}Deletion in stackprocedure delete(var item : items){remove top element from the stack stack and put it in the item}begin if top = 0 then stackempty; item := stack(top); top := top-1;end; {of delete} These two procedures are so simple that they perhaps need no more explanation. Procedure delete actuallycombines the functions TOP and DELETE, stackfull and stackempty are procedures which are left unspecified sincethey will depend upon the particular application. Often a stackfull condition will signal that more storage needs to beallocated and the program re-run. Stackempty is often a meaningful condition. PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 20
21. 21. 2.6 QUEUE: - A Queue is an ordered list of items that have two basic operations: adding new items to thequeue, and removing items of the queue. The operation of adding new items on the queue occursonly at one end of the queue called the rear(or back). The operation of removing items of thequeue occurs at the other end called the front. The following consists of a queue that has fiveintegers: front: 23 33 -23 90 2 : rear The operations of adding 2 new items,5 and 4, to the queue occurs in the back and thequeue looks like so. front: 23 33 -23 90 2 5 4 :rear The operations of removing three items from the queue would yield the queue front: 90 2 5 4 : rear The first item on the queue, the item that will be removed with the next remove operation,is called the front of the queue. The last item is calledthe rear of the queue. The number of items in the queue is called the queue size. If the number ofitems in the queue is zero, an attempt at a removeoperation produces a queue underflow error. In theory, there does not exist a queue overflow.But in practice, implementations of a queue have a maximum queue size.Addition into a queueprocedure addq (item : items){add item to the queue q}begin if rear=n then queuefull else begin rear :=rear+1; q[rear]:=item; end;end;{of addq}Deletion in a queueprocedure deleteq (var item : items){delete from the front of q and put into item} PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 21
22. 22. begin if front = rear then queueempty else begin front := front+1 item := q[front]; end;end; {of deleteq}2.7 Mapping A mapping or associative store is a function from elements of one type, called the domaintype to elements of another type, called the range type. We express the fact that the mapping Massociates element r of range type range type with element d of domain type domain type by M(d)=r. Certain mapping such as square(i)= i2 can be implemented easily by giving an arithmeticexpression or other simple means for calculating M(d) from d . However for many mapping there isno apparent way to describe M(d) other than to store for each d the value of M(d). PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 22
23. 23. UNIT 3 Basic Operations and Sets3.1 Sets3.2 An ADT with union, intersection and difference3.3 Bit vector implementation of sets3.4 Link-list implementation of sets3.5 The data dictionary3.1 Sets A set is a collection of member (or elements) each member of a set either is itself a set orprimitive element called an atom. All member of a set are different, which means no set cancontain two copies of the same elements. When used as tool in algorithm and data structure design, atoms usually are integers,characters or strings and all elements in any one set are usually of the same type. We shall oftenassume that a relation, usually denoted and read “less than” or “process”, linearly orders atoms. Alinear order < on a set S satisfies two properties:1. For any a and b in S, exactly one a< b, a=b or b< a is true.2. For all a, b and c in S, if a<b and b<c then a<c (transitivity).3.2 An ADT with union, intersection and differenceA set is an unordered collection of particular objects. These objects are referred to as elements.An example is shown as: A = {1, 2, 3, 4, 5, 9, 10}Where. A is the set and the elements are identified within the braces. Generally a set is denotedby a capital letter while lower case letters represents the elements. For a large series with apattern, the set could be designated as: N = {1, 2, 3, 4, 5, … }The use of the symbol ∈ in set theory means that object is an element of a set. For example, a ∈ Smeans that a is an element within the set S. More than one element can be identified such as a, b∈ S which indicates that both a and b are elements of the set S. Continuing in this vein, ∉ meansthat an element is not in a set. For example, a ∉ S states that a is not an element within the set S.A subset is a set where every element belongs to another set. The symbols ⊂ and ⊃ are used todesignate subsets, depending upon the syntax. If all of the elements of V are also elements of Tthen V is a subset of T and this can be written as V ⊂ T , which can be interpreted as saying V iscontained in T, or T ⊃ V, which can be used to imply that T contains V. For example, T = {all letters in the English alphabet} V = {all vowels} then V⊂T If there exists one element that exists in V that is not in T, then R is not a subset and thisis designated as V ⊄ T. PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 23
24. 24. If two sets, S and T, contain exactly the same identical elements, then they are equal andS = T. It is also true that S ⊂ T and T ⊂ S. If on the other hand, S ⊂ T and S ≠ T then S is called aproper subset of T. This means that there is at least one element of T, which is not an element ofS. It is important to recognize the syntax utilized in set theory. For example, if the set S = {a,b, c}, then a ∈ S is the correct form that means a is an element of the set S. But, using {a} ∈ S isincorrect because the braces mean a set. S can consist of subsets that contain only one element.For example, {a} can be used to designate a subset of S containing only a. Then, it is proper tostate that {a} ⊂ S which means that the set containing {a} is contained within S. It is also possible to represent a set of elements in one set from another set, which hascertain properties. This is written as: S = {x ∈ T | x has the property p}or, if the set T is clear to the user, this can be shortened to the form: S = {x | x has the property p}The following example shows the set S contains all of those elements from the set of naturalnumbers N, where x + 2 = 7. S = {x ∈ N | x + 2 = 7}Since S contains only one element, 5, this is called a unit set. Another example shows that thedefinition of a particular set can be done in different fashions. In this first definition, the set E willcontain all even natural numbers: E = {x ∈ N | x is even}hence, E = {2, 4, 6, 8, … }Another way of defining this set is as follows: E = {2n | n ∈ N } By convention, uses of certain letters usually define the element of a set. For example, thenull set, ∅, is an empty set, which contains no elements. ∅ is a subset of all sets. The universalset, U, is the set consisting of all the elements that are of interest for a problem. N is generally used for natural values (N = {1, 2, 3, 4, ...}, sometimes 0 is considered anatural number), and Z is used for integers (Z = {..., -3, -2, -1, 0, 1, 2, 3, ...}), and R represents allreal numbers. The union of sets defines another set, which consists of elements from two or more sets. Itcan be shown mathematically as A ∪ B whose elements are {x | x ∈ A or x ∈ B}. This operation isoften called a Boolean OR operation since the element must be in either set A or set B to beincluded in the newly formed set. For example, if A = {0, 2, 3, 5, 7, 9, 11}and B = {1, 2, 4, 6, 7, 9, 11}then C = A ∪ B = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11} PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 24
25. 25. Venn Diagram showing the union of two sets. A figure called a Venn diagram is often used to depict the different relationships in set theory. If the two sets have at least one common element then they are called conjoint. If, on the other hand, there are no common elements in the two sets then they are said to be disjoint. For example, E = {all male students at Ferris} and F = {all female students at Ferris}, the sets are disjoint because a student cannot be a member of both groups. In the example above, the sets A and B are conjoint since there is at least one common element. The intersection of two sets is denoted as C = A ∩ B where the elements of this new set are defined by {x | x ∈ A and x ∈ B}. This is sometimes called a Boolean AND operation since an element must be in both sets to be included in the new set. If the sets are disjoint then A ∩ B = 0. In the example above, where: A = {0, 2, 3, 5, 7, 8, 9, 11} and B = {1, 2, 4, 6, 7, 9, 11} then C = A ∩ B = {2, 7, 9, 11}Venn Diagram showing the intersection oftwo sets. The complement of a set is defined by the elements that are a part of the universal set but not in the subset. If A is a subset then the complement is shown as Ac or comp (A). As an example, if U = {all male and female students at FSU} then the subset A = {all male students at FSU} then PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 25
26. 26. comp (A) = {all female students at FSU}Venn Diagram showing the A minus B setoperation.Another useful set of operations is AB which means A minus B. The new set will contain all ofthose elements of A that are not in B or {x | x ∈ A and x ∉ B}. Using the examples of A and B fromabove, C = AB = {0, 3, 5, 8}Sets also follow the distributive law. Thus, A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)and A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C)While not a proof, this property is depicted in using Venn diagrams. PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 26
27. 27. A B A B A B C C C a A B A B A B C C C b Graphical "proof" of the distributive law.3.3 A Bit –Vector Implementation of Sets The best implementation of a SET ADT depends on the operation to be performed and onthe size of the set. When all sets in our domain of discourse are subsets of a small “universal set”whose elements are the integers 1,2,3…………….N for some fixed N, then we can use a bit-vector(boolean array) implementation. A set is represented by a bit vector in which the I th bit is true if Iis an element of the set. The major advantage of this representation is that MEMBER, INSERT andDELETE operation can be performed in constant time by directly addressing the appropriate bit.UNION, INTERSECTION and DIFFERENCE can be performed in time proportional to the size ofthe universal set.3.4 Link-list implementation of sets: -It should also be evident that linked lists can represent sets, where the items in the list are theelements of the set. Unlike the bit-vector representation, the list representation uses spaceproportional to the size of the set represented, not the size of the universal set.When we have operations like INTERSECTION on sets represented by linked lists, we have severaloptions. If the universal set is linearly ordered, then we can represent a set by a stored list. Thatis, we assume all set members are comparable by a relation “<” and the members of a set appearon a list in the order e1, e2, e3………………..en, where e1<e2<e3<………………en. The advantage of a PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 27
28. 28. stored list is that we do not need to search the entire list to determine whether an element is onthe list.3.5 The data dictionary: - “A data dictionary is used to describe all aspects of a computer based information system: -in particular its data and its processes, i.e. data about data.” “To DOCUMENT for all posterity the data elements, structures, flows, stores, processes, andexternal entities in the system to be designed.” Specific arrangements of data attributed (elements) that define the organization of a singleinstance of a data flow. Data structures belong to a particular data store. Think about this. Forexample, you may desire to collect information about an instance of an "employee." Employeespave payroll information; perhaps work address information, and human resources information.Because the purpose of this information is to serve several systems or data flows, you might wantto store the whole of the employee information in several data structures.Data Elements: -The descriptive property or characteristic of an entity. In database terms, this is a "attribute" or a"field." In Microsoft and other vendor terms, this is a "property." Therefore, the data element ispart of a data structure, which is part of a data store. It has a source, and there may be dataentry rules you wish to enforce on this element.Data Flows:Represent an input of data to a process or the output of data (or information) from a process. Adata flow is also used to represent the creation, deletion, or updating of data in a file or database(called a "data sore" in the Data Flow Diagram (DFD.) Note that a data flow is not a process, butthe passing of data only. These are represented in the DFD. Each data flow is a part of a DFDExplosion of a particular process.When we use a set in the design of an algorithm, we may not need powerful operations like unionand intersection. Often, we only need to keep a set of “current” objects, with periodic insertionsand deletions from the set. Also, from time to time we may need to know whether a particularelement is in the set. A set ADT with the operations INSERT, DELETE and MEMBER has beengiven the name dictionary. We shall also include MAKENULL as a dictionary operation to initializewhether data structure is used in the implementation. PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 28
29. 29. UNIT 4 Algorithms Analysis Techniques4.1 Efficiency of algorithms4.2 Analysis of recursive programs4.3 Solving recurrence equation4.1 Efficiency of algorithms 1. Algorithms (Algorithm is a well-defined sequence of steps) 2. Computational resources: time and space (which leads to solving a certain problem. Steps should be: a. Unambiguous b. There should be finitely many of them) 3. Best, worst and average case performance 4. How to compare algorithms: machine-independent measure of efficiency 5. Growth rate. 6. Complexity measure O ( ).Best, worst and average case• Best performance: the item we search for is in the first position; examines one position.• Worst performance: item not in the array or in the last position; examines all positions.• Average performance (given that the item is in the array): examines half of the array.Rate of GrowthWe dont know how long the steps actually take; we only know it is some constant time. We canjust lump all constants together and forget about them.What we are left with is the fact that the time in sequential search grows linearly with the input,while in binary search it grows logarithmically much slower.Big Oh complexity measureBig O notation gives an asymptotic upper bound on the actual function, which describestime/memory usage of the algorithm.The complexity of an algorithm is O(f(N)) if there exists a constant factor K and an input size N0such that the actual usage of time/memory by the algorithm on inputs greater than N0 is alwaysless than K f(N).Big O "Big O" refers to a way of rating the efficiency of an algorithm. It is only a rough estimate of theactual running time of the algorithm, but it will give you an idea of the performance relative to thesize of the input data. PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 29
30. 30. An algorithm is said to be of order O(expression), or simply of order expression (whereexpression is some function of n, like n2 and n is the size of the data) if there exist numbers p, qand r so that the running time always lies below between p.expression+q for n > r. Generallyexpression is made as simple and as small as possible.For example, the following piece of code executes the innermost code(n2 - n) / 2, and the wholeexpression for the running time will be a quadratic. Since this lies below n2 for all n > 100, thealgorithm is said to have order O(n2).for i := n downto 2 do begin h := 1; for j := 2 to i do if list[j] > list[h] then h := j; temp := list[h]; list[h] := list[i]; list[i] := list[temp]; end;(this is the algorithm for Selection Sort)Choosing an algorithm Choosing an algorithm isnt a case of simply picking the fastest one and implementing it.Especially in a competition, you will also have to balance this against the time it will take you tocode the algorithm. In fact you should use the simplest algorithm that will run in the timeprovided, since you generally get no more points for a faster but more complex algorithm. In fact itis sometimes worth implementing an algorithm that you know will not be fast enough to get100%, since 90% for a slow, correct algorithm is better than 0% for a fast but broken one. When choosing an algorithm you should also plan what type of data structures you intendto use. The speed of some algorithms depends on what types of data structures they are combinedwith.Analysis of recursive programs The techniques for solving different equation are sometimes subtle and bear considerableresemblance to the methods for solving differential equation. Consider the sorting program, therethe procedure merge sort takes a list of length n as input and returns a sorted list as its output.The procedure merge(L1, L2) takes as input two sorted list L1 and L2, scans them each, elementby element, from the front. At each step, the larger of the two front elements is deleted from its listand emitted as output. The result is a single sorted list containing the elements of L1 and L2. Thetime taken by merge on lists of length n/2 is O(n). function mergesort(L : LIST, n : integer) : LIST; { L is a list of length n. A sorted version of is returned. We assume n is a power of 2. } var L1,L2: LIST begin if n=1 then return(L); else begin Break L into two halves L1 and L2, each of length n/2; return(merge(mergesort(L1,n/2), mergesort(L2,n/2))); PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 30
31. 31. end end; { mergesort } Let T(n) be the worst case running time of procedure mergesort, we can write a recurrence( or difference ) equation the upper bounds T(n), as follows T(n) <= c1 if n=1 Or T(n) < = 2T(n/2) + c2 n if n>1 C1 and C2 are constant.Solving recurrence equation There are three different approaches we might take to solving a recurrence equation. 1. Guess a solution f(n) and use the recurrence to show that T(n) <= f(n). Some times we guess only the form of f(n), leaving some parameters unspecified (e.g guess f(n)=an 2 for some a) and deduce suitable values for the parameters as we try to prove T(n) <= f(n) for all n. 2. Use the recurrence itself to substitute for any T(m), m < n, on the right until all terms T(m) for m > 1 have been replaced by formulas involving only T(1) . Since T(1) is always a constant, we have a formula for T(n) in terms of n and constants. This formula is what we have referred to as a “closed form” for T(n). PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 31
32. 32. UNIT 5 Algorithms Design Technique5.1 Divide and conquer algorithms5.2 Dynamic programming5.3 Greedy algorithm5.4 Minimum-cost spanning trees5.5 Minimum Spanning Tree5.6 Prim’s Algorithm5.7 Kruskal’s Algorithm5.8 Shortest Paths5.9 Dijkastra’s Algorithm5.10 Backtracking5.1 Divide-and-Conquer algorithmsThis is a method of designing algorithms that (informally) proceeds as follows:Given an instance of the problem to be solved, split this into several, smaller, sub-instances (ofthe same problem) independently solve each of the sub-instances and then combine the sub-instance solutions so as to yield a solution for the original instance. This description raises thequestion:By what methods are the sub-instances to be independently solved? The answer to this question is central to the concept of Divide-&-Conquer algorithm and isa key factor in gauging their efficiency. Consider the following: We have an algorithm, alpha say, which is known to solve allproblem instances of size n in at most c n^2 steps (where c is some constant). We then discoveran algorithm, beta say, which solves the same problem by: • Dividing an instance into 3 sub-instances of size n/2. • Solves these 3 sub-instances. • Combines the three sub-solutions taking d n steps to do this.Suppose our original algorithm, alpha, is used to carry out the `solves these sub-instances step 2.LetT(alpha)( n ) = Running time of alphaT(beta)( n ) = Running time of betaThen,T(alpha)( n ) = c n^2 (by definition of alpha)ButT(beta)( n ) = 3 T(alpha)( n/2 ) + d n = (3/4)(cn^2) + dnSo if dn < (cn^2)/4 (i.e. d < cn/4) then beta is faster than alphaIn particular for all large enough n, (n > 4d/c = Constant), beta is faster than alpha. This realisation of beta improves upon alpha by just a constant factor. But if the problemsize, n, is large enough then n > 4d/c n/2 > 4d/c ... n/2^i > 4d/c PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 32
33. 33. Which suggests that using beta instead of alpha for the `solves these stage repeatedly until thesub-sub-sub..sub-instances are of size n0 < = (4d/c) will yield a still faster algorithm.So consider the following new algorithm for instances of size nProcedure gamma (n : problem size ) is Begin if n <= n^-0 then Solve problem using Algorithm alpha; else Split into 3 sub-instances of size n/2; Use gamma to solve each sub-instance; Combine the 3 sub-solutions; end if; end gamma;Let T(gamma)(n) denote the running time of this algorithm. cn^2 if n < = n0T(gamma)(n) = 3T(gamma)( n/2 )+dn otherwiseWe shall show how relations of this form can be estimated later in the course. With these methodsit can be shown thatT(gamma)( n ) = O( n^{log3} ) (=O(n^{1.59..})This is an asymptotic improvement upon algorithms alpha and beta. The improvement that results from applying algorithm gamma is due to the fact that itmaximise the savings achieved beta. The (relatively) inefficient method alpha is applied only to "small" problem sizes.The precise form of a divide-and-conquer algorithm is characterised by: • The threshold input size, n0, below which the problem size is not sub-divided. • The size of sub-instances into which an instance is split. • The number of such sub-instances. • The algorithm used to combine sub-solutions.It is more usual to consider the ratio of initial problem size to sub-instance size. The threshold in(I) is sometimes called the (recursive) base value. In summary, the generic form of a divide-and-conquer algorithm is:Procedure D-and-C (n : input size) is Begin if n < = n0 then Solve problem without further sub-division; else Split into r sub-instances each of size n/k; for each of the r sub-instances do D-and-C (n/k); Combine the r resulting sub-solutions to produce the solution to the original problem; end if; end D-and-C; PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 33
34. 34. 5.2 Dynamic Programming The term ``Dynamic Programming" (DP) refers to a collection of algorithms that can beused to compute optimal policies given a perfect model of the environment as a Markov decisionprocess (MDP). Classical DP algorithms are of limited utility in reinforcement learning bothbecause of their assumption of a perfect model and because of their great computational expense,but they are still very important theoretically. DP provides an essential foundation for theunderstanding of the methods presented in the rest of this book. In fact, all of these methods canbe viewed as attempts to achieve much the same effect as DP, only with less computation andwithout assuming a perfect model of the environment.Starting with this chapter, we usually assume that the environment is a finite MDP. That is, weassume that its state and action sets, and , for , are finite, and that its dynamicsare given by a set of transition probabilities, , andexpected immediate rewards, , for all , , and ( is plus a terminal state if the problem is episodic). Although DPideas can be applied to problems with continuous state and action spaces, exact solutions arepossible only in special cases. A common way of obtaining approximate solutions for continuousstate and action tasks is to quantize the state and action spaces and then apply finite-state DPmethods. The key idea of DP, and of reinforcement learning generally, is the use of value functionsto organize and structure the search for good policies. As discussed there, we can easily obtainoptimal policies once we have found the optimal value functions, or , which satisfy theBellman optimality equations:orfor all , , and . As we shall see, DP algorithms are obtained by turningBellman equations such as these into assignment statements, that is, into update rules forimproving approximations of the desired value functions. PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 34
35. 35. 5.3 Greedy Algorithm An algorithm is a step-by-step recipe for solving a problem. A greedy algorithm might alsobe called a "single-minded" algorithm or an algorithm that gobbles up all of its favorites first. Theidea behind a greedy algorithm is to perform a single procedure in the recipe over and over againuntil it cant be done any more and see what kind of results it will produce. It may not completelysolve the problem, or, if it produces a solution, it may not be the very best one, but it is one way ofapproaching the problem and sometimes yields very good (or even the best possible) results.5.4 Minimum-Cost spanning trees Let G=(V,E) be an undirected connected graph. A sub-graph t = (V,E1) of G is a spanningtree of G if and only if t is a tree. Above figure shows the complete graph on four nodes together with three of its spanningtree. Spanning trees have many applications. For example, they can be used to obtain anindependent set of circuit equations for an electric network. First, a spanning tree for the electricnetwork is obtained. Let B be the set of network edges not in the spanning tree. Adding an edgefrom B to the spanning tree creates a cycle. Kirchoff’s second law is used on each cycle to obtain acircuit equation. Another application of spanning trees arises from the property that a spanning tree is aminimal sub-graph G’ of G such that V(G’) = V(G) and G’ is connected. A minimal sub-graph withn vertices must have at least n-1 edges and all connected graphs with n-1 edges are trees. If thenodes of G represent cities and the edges represent possible communication links connecting twocities, then the minimum number of links needed to connect the n cities is n-1. The spanningtrees of G represent all feasible choice. In practical situations, the edges have weights assigned to them. These weights mayrepresent the cost of construction, the length of the link, and so on. Given such a weighted graph,one would then wish to select cities to have minimum total cost or minimum total length. In eithercase the links selected have to form a tree. If this is not so, then the selection of links contains acycle. Removal of any one of the links on this cycle results in a link selection of less constconnecting all cities. We are therefore interested in finding a spanning tree of G. with minimumcost since the identification of a minimum-cost spanning tree involves the selection of a subset ofthe edges; this problem fits the subset paradigm.5.5 Minimum Spanning Tree: -Let G = (V,E) be an undirected connected graph. A subgraph t = (V, E 1) of g is a spanning tree of gif t is a tree.Example: shows the complete graph on four nodes together with three of its spanning trees. PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 35
36. 36. Spanning trees have many applications. For example, they can be used to obtain an independent set of circuit equationsfor an electric network. In practical situations, the edges have weights assigned to them. These weights may representthe cost of construction, the length of the link, and so on. Given such a weighted graph, one would then wish to selectcities to have minimum total cost or minimum total length. In either case the links selected have to form a tree. We aretherefore interested in finding a spanning tree g with minimum cost. A graph and one of its minimum spanning treeinvolves the selection of a subset of the edges, this problem fits the subset paradigm. 1 1 10 28 10 6 2 6 2 14 16 25 14 16 25 3 7 3 7 5 24 18 12 12 5 4 22 4 22 Figure (a): Graph Fig. (b): Minimum spanning treeWe present two algorithms for finding a minimum spanning tree of a weighted graph: Prim’salgorithm and Kruskal’s algorithm.5.6 Prim’s AlgorithmA greedy method to obtain a minimum spanning tree is the to build the tree edge by edge. Thenext edge to include is chosen according to some optimization criterion. The simplest suchcriterion is to choose an edge that results in a minimum increase in the sum of the costs of theedges so far included. There are two possible ways to interpret this criterion. In the first, the set ofedges so far selected from a tree. Thus if A is the set of edges selected so far, then A forms a tree.The next edge (u,v) to be included in A is a minimum cost edge not in A with property that A ∪{(u,v)} is also a tree. The following example shows this selection criterion results in a minimumspanning tree. The corresponding algorithm is known as Prim’s algorithm.Example shows the working of Prim’s method on the graph . The spanning tree obtained is shownin figure (b) and has a cost of 99. PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 36
37. 37. 1 1 10 10 6 2 6 2 7 25 7 3 3 5 5 5 4 Fig. (a) Fig. (b) 1 1 10 10 6 2 6 2 7 25 7 25 3 3 5 5 4 4 12 22 22 Fig. (c) Fig. (d) 1 1 10 10 6 2 16 6 2 16 7 25 7 14 25 3 3 5 12 5 4 4 12 22 22 Fig. (e) Fig. (f)Having seen how Prim’s method works, let us obtain a n algorithm to find a minimum costspanning tree using this method. The algorithm will start with a tree that include only a minimumcost edge of g. Then, edges are added to this tree one by one. The next edge (i, j) to be added issuch that I is a vertex already included in the tree, j is a vertex not yet included, and the cost of (i,j), cost [i, j], is minimum among all edges (k, l) such that vertex k is in the tree and vertex e is notin the tree. To determine this edge (i, j) efficiently, we associate with each vertex j not yet included PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 37
38. 38. in the tree a value near [j]. The value near [j] [near (j)] is minimum among all choices for near [j].We define near [j] = 0 for all vertices j that are already in the tree. The next edge to include isdefined by the vertex j such that near [j] # 0 (j not already in the tree) and cost [j] [near (j)] isminimum. Prim (E, cost, n, t) //E is the set of edges in g. cost [n] [n] is // the cost adjacmcy matrix of an n vertex //graph such that cost [i,j] is //either a positive real number or is it //no edge (i, j) exists. //A minimum spanning tree is computed //and stored as a set of edges in //The array t[n–1] [2]. (The final cost is //returned. { Let (k, l) be an edge of minimum cost in E; Min cost = cost [k] [l]; t [1] [1] = k; k[1] [2] = l; for (i = 1; i < = n : i + l) if (cost [i] [l] < cost [i] [k]) near [i] = l; else near [i] = k; near [k] = near [l] = 0; for (i = 2; i < = n–1; i + l) { //Find n–2 additional edges for t. Let j be an index such that near [j] ! = 0 and Cost [j] near [(j)] is minimum; T [i] [1] = j; t [i] [2] = near [j]; min cost = mincost + cost [j] [near [j]]; near [j] = 0; for (k = 1; k < = n; k + l) //update near if (near [k] ! = 0 && cost [k] [near [k] > cost [k] [j]) near [k] = j; } return (min cost); PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 38
39. 39. }The time required by algorithm prim is 0 (n2), where n is the number of vertices in thegraph g.5.7 Kruskal’s Algorithm There is a second possible interpretation of the optimization criteria mentioned earlier inwhich the edges of the graph are considered in non-decreasing order of cost. This interpretation isthat the set t of edges so far selected for the spanning tree be such that it is possible to complete tinto a tree. Thus t may not be a tree at all stages in the algorithm. In fact it will generally only bea forest since the set of edges t can be completed into a tree if there are no cycles in t. Thismethod is due to kruskal. Example: Consider the graph of figure (a). We begin with no edges selected figure(a) showsthe current graph with no edges selected Edge (1,6) is the first edge considered. It is included inthe spanning tree being built. This yields the graph of figure (b). Next the edge (3,4) is selected andincluded in the tree (fig. (c)). The next edge to be considered is (2,7). Its inclusion in the tree beingbuilt does not create a cycle, so we get the graph of figure. Edge (2,3) is considered next andincluded in the tree figure (e). Of the edges not yet considered (7,4) has the least cost. It isconsidered next. Its inclusion in the tree results in a cycle, so this edge is discarded. Edge (5,4) isthe next edge to be added in the tree being built. This result in the configuration of figure (f). Thenext edge to be considered is the edge (7,5). It is discarded as its inclusion creates a cycle. Finallyedge (6,5) is considered an included in the tree built. This completes the spanning tree. Theresulting tree has cost 99. 1 1 10 6 2 6 2 7 7 3 3 5 5 4 4 Fig. (a) Fig. (b) 1 1 10 10 6 2 6 2 PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 39
40. 40. 7 14 3 7 3 5 12 5 4 4 12 Fig. (c) Fig. (d) 1 1 10 10 2 6 16 6 2 16 7 14 14 3 7 3 5 12 12 4 4 5 22 Fig. (e) Fig. (f)For clarity, kruskal’s method is written out more formally in following algorithm.1. t = 0;2. while [(it has less than n–1 edges) R& (E! = 0)]3. {4. Choose an edge (u,v) from E of lowest cost;5. Delete (q, w) from E;6. If (u, w) does not create a cycle in it) add (v,w) to t;7. else Discard (v, w);8. }Initially E is the set of all edges in g. The only functions we wish to perform on this set are(1) determine an edge with minimum cost (line 4) and (2) delete this edge (line 5). Both thesefunctions can be performed efficiently if the edges in E are maintained as a sorted sequential list.It is not essential to sort all the edges so long as the next edge for line 4 can be determined easily.If the edges are maintained as a min heap, then the next edge to consider can be obtained in 0(long |E|) line. The construction of heap it self take O (|E|) time. To be able to perform step 6efficiently, the vertices in g should be grouped together in such a way that one can easilydetermine whether the vertices v and w are already connected by the earlier selection of edges. If PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 40
41. 41. they are, then the edge (v,w) is to be added to t. One possible grouping is to place all vertices inthe same connected component by t into a set. For example, when the edge (2,6) is to beconsidered, the sets are {1,2}, {2,4,6}, and {5}. Vertices 2 and 6 are in different sets so these setsare combined to give {1,2,3,4,6} and {5}. The next edge to be considered is (1,4). Since vertices 1and 4 are in the same set, the edge is rejected. The edge (3,5) connects vertices in different setsand results in the final spanning tree.5.8 Shortest Paths Graphs can be used to represent the highway structure of a state on country with verticesrepresenting cities and edges representing sections of highway. The edge can them be assignedweights which may be either the distance along that section of highway. A motoriot wishing todrive from city A to B would be interested in answers to the following question:• Is there a path from A to B?• If there is more than one path from A to B, which is the shortest path? The problems defined by these questions are special cases of the path problems we study inthis section. The length of a path is now defined to be the sum of the weights of the edges or thatpath. The starting vertex of the path is referred to as the source, and the last vertex thedestination. The graphs are digraphs to allow for one-way structs. In the problem we consider weare given a directed graph g = (V,E), a weighting function cost for the edges of g, and a sourcevertex V0. The problem is to determine the shortest path from V0 to all the remaining vertices ofg. It is assumed that all the weight are positive.5.9 Dijkstra’s AlgorithmThis algorithm determines the lengths of the shortest paths from v0 to all other vertices in g. Dijkstra’s (v, cost, dist, n) //dist [j], 1< = j < = n, is set to the legnth //of the hortest path from vertex v to //vertex j in a diagraph g with n //vertices dist [v] set to zero. G is //represented by its cost adjacency matrix //cost [n] [n]. { for (i = 1; i < = n; i &&) { //intializes S [i] = false; dist [i] = cost [v][i] } S [v] = true; dist [v] = 0.0; ||put v in S. { //Determines n–1 paths from v. PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 41
42. 42. Choose u from among these vertices not in S such that digit [u] is minimum; S[u] = true; ||put u in S For (each is adjacent to u with S[w] = false) //update distances dist [w] = dist [u] + cost [u] [w] } }Example: Consider the eight vertex diagraph with cost adjacency matrix as in figure. The values ofdist and the vertices selected at each iteration of for loop of line 12 is previous algorithm, forfinding all the shortest paths from Boston are shown in figure. To begin with, S contains onlyBoston. In the first iteration of for loop (that is num = 2), the city u that is not in S and whose dist[4] is minimum is identified to be New York. In the next iteration of the for loop, the city thatenters S is Miami since it has the smallest dist [ ] value from among all the nodes not in S. Noneof the dist [ ] values are altered. The algorithm when only seven of the eight vertices are in S. Bythe definition of dist, the distance of the last vertex, in this case Los Angeles, is correct as theshortest path from Boston to Los Angeles can go through only the remaining six vertices. Boston 1500 5 Chicago 4 250 1200 1000San Francisco 6 New York 800 2 3300 Denver 1400 900 1000 1 8 1000Los Angeles New Orleans 1700 7 Miami Figure (a) 1 2 3 4 5 6 7 81 02 300 03 100 800 04 1200 05 1500 0 2506 1000 0 900 14007 0 10008 1700 0 PC TRAINING INSTITUTE LTD. PCTI HOUSE, UU-11, PITAM PURA, DELHI-110088. TEL: 47510411/422 Email : info@pctiltd.com, Web-site : www.pctiltd.com 42