Analysis of Algorithms O Hm

Analysis of Algorithms O Hm

DESIGN AND ANALYSIS OOF ALGORITHMSHMPress Associatesociateciate versProfessorProfP Department of Informationformatio Science and Technology Collegee off EngineeEngiEngineering, Guindy Campus Anna UniversityUnUniversity, Chennai Oxford ΞKdžĨŽƌĚhŶŝǀĞƌƐŝƚLJWƌĞƐƐ͘ůůƌŝŐŚƚƐƌĞƐĞƌǀĞĚ͘ Oxford University Press is a department of the University of Oxford. It furthers the University’s objective of excellence in research, scholarship, and education by publishing worldwide. Oxford is a registered trade mark of Oxford University Press in the UK and in certain other countries. Published in India by Oxford University Press YMCA Library Building, 1 Jai Singh Road, New Delhi 110001, India © Oxford University Press 2014 The moral rights of the author/s have been asserted. First published in 2014 All rights reserved. No part of this publication may be reproduced,ed, storestored in a retrieval system, or transmitted, in any form or by any means,ns, withoutwithou the prior permission in writing of Oxford University Press, or as expresslypressly permittedpe by law, by licence, or under terms agreed with the appropriatepriateiate reproreprographics rights organization. Enquiries concerning reproduction outside the scope of the above should be sent to the Rights Department, Oxfordo d UniversityUniversi Press, at the address above. Press You must not circulate this workk in any otheroth form and you must impose this same conditionndition on anany acquirer. ISBN-13: 978-0-19-809369-5-0-19-809369-519-80936 ISBN-10:: 0-19-809369-1-19-80936 Typesetet in Times New Roman by Idealall PublishingPublishin SSolutions, Delhi Printed in Indiandia by RadhaRad Press, New Delhi 110031 Third-party websiteebsiteite aaddressesdressesdres mentioned in this book are provided by Oxford Universityversity PPressUniversityss in good faith and for information only. Oxford University Press disclaimdisclaims any responsibility for the material contained therein. Oxford ΞKdžĨŽƌĚhŶŝǀĞƌƐŝƚLJWƌĞƐƐ͘ůůƌŝŐŚƚƐƌĞƐĞƌǀĞĚ͘ Features of the Book CHAPTER Introducon to 1 Topical Coverage Algorithms CHAPTER The book provides extensive coverage of Basics of ComputaƟonal ƚŽƉŝĐƐƐƚĂƌƟŶŐĨƌŽŵ 18 CHAPTER Complexity ĂůŐŽƌŝƚŚŵďĂƐŝĐƐĂŶĚ ĚĂƚĂƐƚƌƵĐƚƵƌĞƐ͕ŵŽǀŝŶŐ Data Structures—I 5 ŽŶƚŽĚŝīĞƌĞŶƚĂůŐŽƌŝƚŚŵ CHAPTER design techniques, “Bad programmers worry about the code. Good programmers worry about data structures followed by discussions Greedy Algorithms ŽŶĐŽŵƉƵƚĂƟŽŶĂů 11 ĐŽŵƉůĞdžŝƟĞƐĂŶĚWZD “Greed is all right,CHAPTER by the way...TER I think greed is healthy. You can be greedy ĂůŐŽƌŝƚŚŵƐ͘ d till f lss d b t lf ” Dynamic Programming 13 Press y 11.8.1 MŝŶŝŵƵŵ SƉĂŶŶŝŶŐ TƌĞĞƐ Treatment of Concepts The history of an MST is as interesting as its concept. In 1926, Otakerer Boruvka formulatedformulaformers ^ŝŵƉůĞĂŶĚůŽŐŝĐĂů the MST problem. A Polish mathematician, Vojtech Jarnik, describedbedd the problemprobl in 1929 ĞdžƉůĂŶĂƟŽŶƐŽĨĐŽŶĐĞƉƚƐ in a letter to Otaker Boruvka. The same problem was conceiveded independentlyndependen by Kruskal in 1956. +ence, Kruskal rediscovered the problem. /ater it wasas de¿ned independently indepi by are provided using Robert Prim in 1957 and by Edsger Dijkstra in 1958 Example 11.1211.12 Consider the graph G shownsh wn in Fig.Fig 11.11. Construct an MST for the ĞƐƐĞŶƟĂůŵĂƚŚĞŵĂƟĐĂů given graph G using Kruskal’s algorithm. UniversityUn ĞdžƉƌĞƐƐŝŽŶƐ͕ŝůůƵƐƚƌĂƟŽŶƐ͕ 6ROXWLRQ The ¿rst step in Kruskal’s algorithmlgorithm is to sort all the edges and form an edge list, ĂŶĚŶƵŵĞƌŽƵƐƐŽůǀĞĚ say E. The edges of graph G shown in Fig. 11.2111.2 are sorted and shown in Table 11.13. ĞdžĂŵƉůĞƐ͘ŝīĞƌĞŶƚ ŽŵƉůĞdžŝƚLJŶĂůLJƐŝƐŽĨ<ƌƵƐŬĂů͛ƐůŐŽƌŝƚŚŵŚŵ ƚLJƉĞƐŽĨƉƌŽďůĞŵƐĂůŽŶŐ Since the disjoint set data structure is used,ed, initialiinitializationordor takes at most (|V|) time. The time complexity of the algorithm dependsends on the numnumber of edges. As there are |E| edges, O(| E ǁŝƚŚƚŚĞŝƌĂůŐŽƌŝƚŚŵƐ͕ |log| E |) time is required to sortt these edgeedges. The disjoint set takes at most 2| E | ¿nd opera- ĞdžĂŵƉůĞƐ͕ĂŶĚĐŽŵƉůĞdžŝƚLJ tions and |V |í1 operations. Therefore,refore, theth total complexity of Kruskal’s algorithm is at most O(| E |log| E |) time. OxforO ĂŶĂůLJƐĞƐĂƌĞŐŝǀĞŶ͘ AlgoritŚm PresentaƟon ůŐŽƌŝƚŚŵƐĂƌĞƉƌĞƐĞŶƚĞĚ Step 1: Create a node x by allocating memory for it. in two ways, that is, step- Step 2: Assign the required value to the item part of node x. wise approach;ŝŶĨŽƌŵĂů item(x) = value Step 3: Set in the pointer to null. ƌĞƉƌĞƐĞŶƚĂƟŽŶͿĂŶĚ next(x) = null pseudocode approach Step 4: ReturnAlgorithm the node x . create(L, x, value) ;ĨŽƌŵĂůƌĞƉƌĞƐĞŶƚĂƟŽŶͿ %% Input: List L and element x with 'value' ĨŽƌƉƌŽǀŝĚŝŶŐĂďĞƩĞƌ %% Output: Node x Begin understanding of the allot(x) %% Allot memory for node x with two ºeldsŹitem and next item(x) = value logic behind solving a next(x) = null ŐŝǀĞŶƉƌŽďůĞŵǁŝƚŚŽƵƚ ƵƐŝŶŐĂŶLJƐƉĞĐŝĮĐ ƉƌŽŐƌĂŵŵŝŶŐůĂŶŐƵĂŐĞ͘ ΞKdžĨŽƌĚhŶŝǀĞƌƐŝƚLJWƌĞƐƐ͘ůůƌŝŐŚƚƐƌĞƐĞƌǀĞĚ͘ Historical Notes Box 1.1 Origin of the word ‘algorithm’ The word algorithm is derived from the name of a Persian Europe. He also introduced the simple step-b Historical notes are mathematician, Abu Ja’fer Mohammed Ibn Musa al for addition, subtraction, multiplication, and provided throughout the Khowarizmi, who lived sometime around 780–850 AD. He his book. The word algebra has also been d was from the town of Khowarazm, now in Uzbekistan. He the title of this book. When his book was t ŬĂƐƐĞƉĂƌĂƚĞďŽdžĞƐ͘ was a teacher of mathematicsBox 17.2 inGeorge Baghdad. Bernard He wrote Dantzig a book Latin, his name was quoted as Algorismus dŚĞLJƉƌŽǀŝĚĞĂĚĚŝƟŽŶĂů *eorge Bernard Dantzig was born in 1914 at Portland, duality theory. He worked with Fulkerson an Oregon, United States. His father became a professor of in formulating the travelling salesperson pro ŝŶĨŽƌŵĂƟŽŶƌĞůĂƚĞĚƚŽ mathematics at University of Maryland after World War linear programming and solved the TSP proble II. Dantzig’s biggest contribution is that he designed the 49 cities at that time. In 197, he was award ƚŚĞĐŽŶĐĞƉƚƐĚŝƐĐƵƐƐĞĚ͘ simplex method for solving LPPs. Apart from the simplex National Medal of Science, the highest honou Glossary and Summary Q GLOSSARY Q ŐůŽƐƐĂƌLJŽĨŬĞLJƚĞƌŵƐ Agent A performer of an algorithm $OJRULWKPYHUL¿FDWLRQ The process of provid ĂůŽĂůŽŶŐǁŝƚŚĚĞĮŶŝƟŽŶƐĂŶĚ Algorist A person who is skilled in algorithm development cal proof that the algorithm works correctly f Algorithm A step-by-step procedure for solving a given Algorithm validation The process of chec ĂƉŽŝĂƉŽŝŶƚͲǁŝƐĞƐƵŵŵĂƌLJŝƐ problem ness of an algorithm, this is done by givin givgiven at the end of each Algorithm gap The difference between lower and upper it and checking its results with expected Q SUMMARY Q esesschapter to help readers An algorithm is a step-by-step procedure for solving a Algorithm veri¿cation is a processes of proprovv ƌĞĐĂƉŝƚƵůĂƚĞƚŚĞŝŵƉŽƌƚĂŶƚ given problem. ematical proof that the givenn algorithmalgorithorit wwoo ĐŽŶĐĞƉƚƐĞdžƉůĂŝŶĞĚ͘ A computational problem is characterized by two for all instances of data. PresP factors²speci¿cation of valid input and output param- A proof of an algorithmm is said to existexis if t ity Revieǁ YuesƟons͕ iversityiv Q REVIEW QUESTIONS Q Edžercises͕ and 1.1 De¿ne ann algorithm. 1.8 What is the difference between algo 1.2 Whatat are the characteristicschar of an algorithm" and algorithm validation" AddiƟonal Proďlems 1.3 SurveyS y the InternetInte and list out at least ¿ve algorithms 1.9 How is an algorithm validated and EƵŵĞƌŽƵƐƌĞǀŝĞǁ that have Univhugeh impact on our daily lives. with an example. 1.44 What area the stages of problem solving" 1.10 State algorithm classi¿cations. Wha ƋƵĞƐƟŽŶƐ͕ĞdžĞƌĐŝƐĞƐ͕ĂŶĚ Q EXERCISES Q 1.1 Assume that there are two algorithms A and B for a complexities of A, B, and C are 3n ĂĚĚŝƟŽŶĂůĐŽŶĐĞƉƚͲďĂƐĞĚ rd given problem P. The time complexity functions of algo- respectively. Assume that the input i ƉƌŽďůĞŵƐĂƌĞŐŝǀĞŶĂƚ rithms A and B are, respectively, 5n and log2n. Which Assume that the machine executes algorithm should be selected assuming that all other per second. How much time will algo the end of every chapterpter f conditions remain the same for both the problems" C take" Which algorithm will be the Q ADDITIONAL PROBLEM Q to test the readers’er 12 L h f l h di h 1.1 John MacCormick had written a book titled Nine recognition, data compression, datab conceptual knowledgegeOxfordOx Algorithms That Changed the Future: The Ingenious signatures, that changed the world. Ideas that drive Today’s Computers, Princeton University (a) What are these algorithms" Se and also enhance their Press, Princeton, that had listed the nine wonderful and ¿nd what these algorithms ĂůŐŽƌŝƚŚŵǁƌŝƟŶŐƐŬŝůůƐ͘ algorithms, namely, search engine indexing, page rank, (b) Identify one more algorithm that Q CROSSWORD Q 1 Crossword Puzzles 2 Crossword puzzles, 3 45 ŐŝǀĞŶĂƐĂŶĞdžĐŝƟŶŐĂŶĚ ŝŶƚĞƌĂĐƟǀĞůĞĂƌŶŝŶŐ exercise at the end of each 7 8 ĐŚĂƉƚĞƌ͕ŵŽƟǀĂƚĞĂŶĚĂŝĚ 910 readers to self-check their ƵŶĚĞƌƐƚĂŶĚŝŶŐ͘ 11 ΞKdžĨŽƌĚhŶŝǀĞƌƐŝƚLJWƌĞƐƐ͘ůůƌŝŐŚƚƐƌĞƐĞƌǀĞĚ͘ WƌĞĨĂĐĞ Computers have become an integral part of our daily life in recent times. They have enormously impacted our personal, professional, as well as social lives. Computers help us in tasks such as document editing, Internet browsing, sending emails, making presentations, performing complex scienti¿c computations, social networking, and playing games. Industries and government of¿ces use computers effectively in production, e-governance, and e-commerce. Considering the increasing demand of computers in society, schools, colleges, and universities have included computer education in their curriculum, to help students become

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    36 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us