Download - BSIT 41 Content
INFORMATION TECHNOLOGY PROGRAMMESBachelor of Science in Information Technology - B.Sc.(IT)Master of Science in Information Technology - M.Sc. (IT)
Incollaboration
with
KUVEMPU UNIVERSITY
B.Sc.(IT) - 4th Semester
BSIT - 41 AlgorithmsBSIT - 42 Java Programming
Directorate of Distance EducationKuvempu University
Shankaraghatta, Shimoga District, Karnataka
Universal Education TrustBangalore
II
Titles in this Volume :BSIT - 41 AlgorithmsBSIT - 42 Java Programming
Prepared by UNIVERSAL EDUCATION TRUST (UET)Bangalore
First Edition : May 2005Second Edition : May 2012
Copyright © by UNIVERSAL EDUCATION TRUST, BangaloreAll rights reserved
No Part of this Book may be reproducedin any form or by any means without the writtenpermission from Universal Education Trust, Bangalore.
All Product names and company names mentionedherein are the property of their respective owners.
NOT FOR SALEFor personal use of Kuvempu UniversityIT - Programme Students only.
Corrections & Suggestionsfor Improvement of Study materialare invited by Universal Education Trust, Bangalore.
E-mail : [email protected]
Printed at :Pragathi Print CommunicationsBangalore - 20Ph : 080-23340100
III
ALGORITHMS(BSIT - 41)
: Contributing Authors :
Dr. D.S. GuruProfessor
Department of Studies in Computer ScienceUniversity of Mysore
Mysore
T.N. VikramDepartment of Studies in Computer Science
University of MysoreMysore
&
P.B. MallikarjunaDepartment of Studies in Computer Science
University of MysoreMysore
IV
Blank Page
V
a
Preface
In this course material, we address the issues related to problem solving and algorithm design. Thismaterial provides you an insight into the field of designing algorithms.
Algorithms are a specific way by which we formulate the problems before we code them in to aprogram. i.e. we are converting the problem from the way human beings look at to the manner in whicha computer can look at it.
However, there is no unique way of designing the algorithms. Given a problem to be executed on acomputer, different people can convert it into algorithms in different ways. Then, what is it that thealgorithms course is about? Even though it may not be possible to exactly teach the “art” of algorithms, itis possible to teach the “science” portion of the same. There are some basic concepts, which can holdgood irrespective of the problem domain and these can be used as “off the shelf” pieces of informationwhile devising the algorithm. In this course, you are being introduced to some of the basic algorithms wefollow.
Once the algorithm has been designed, there should also be a mechanism to decide on the “goodness”of the algorithm. This becomes more important because often more than one method of solving the givenproblem is available so, we should be able to compare one solution with another.
An algorithm not only depends on its own nature, but depends on the supporting data structures too.Thus, in this material we also cover some of the preliminary aspects of related data structures. In some ofthe parts of the material, we have assumed that the reader has a background of at least one conventionalprogramming language such as Pascal, C, C++ etc. and as well a little knowledge of data structures.
The material has been organized as a collection of 6 chapters. Chapter 1 presents in detail the necessityof taking up a course on algorithms through exploration of significance of algorithms in the field of computer
VI Preface
science. The essential knowledge on the prerequisite data structures has been, to some extent, covered inChapter 2. The chapter 3, deals with some simple problems and associated algorithms. The concept ofsearching and sorting is been introduced in chapter 4 while the concept of recursion is presented inchapter 5. In chapter 6 we have covered the concept of binary tree representation techniques and binarytree traversal schemes.
VII
a
Cont en t s
Chapter 1
REVIEW 1
1.0 Objectives........................................................................................ 11.1 Concept of algorithm......................................................................... 11.2 Characteristics of Algorithm............................................................... 21.3 Problem - Solving Aspect................................................................... 31.4 How to devise the algorithms ......................................................... 41.5 How to validate the algorithms........................................................... 51.6 How to Test the algorithms................................................................ 51.7 Algorithmic Notations........................................................................ 5
Summary.......................................................................................... 7Exercise........................................................................................... 7
Chapter 2
ELEMENTARY DATA STRUCTURES 8
2.0 Objectives........................................................................................ 82.1 Fundamentals................................................................................... 82.2 Linear Data Structures..................................................................... 9
2.2.1 Array and its representation................................................ 92.2.2 Stacks................................................................................ 112.2.3 Queues.............................................................................. 132.2.4 Circular Queue................................................................... 17
2.3 Linked Lists...................................................................................... 192.4 Non-Linear data structures................................................................ 21
VIII
2.4.1 Introduction to Graph theory................................................ 212.4.1.1 Finite and infinite graphs....................................... 222.4.1.2 Incidence and degree............................................ 222.4.1.3 Isolated vertex, pendent vertex and Null Graph....... 232.4.1.4 Walk, Path and Connected Graph........................... 23
2.4.2 Matrix representation of Graphs.......................................... 242.4.2.1 Adjacency Matrix................................................. 242.4.2.2 Incidence Matrix.................................................. 25
2.4.3 Trees................................................................................. 262.4.3.1 Some properties of trees....................................... 26
Summary.......................................................................................... 27Exercise........................................................................................... 27
Chapter 3
SOME SIMPLE ALGORITHMS 28
3.0 Objectives........................................................................................ 283.1 Addition of two numbers.................................................................... 293.2 Exchanging the values of two variables............................................... 293.3 Input three numbers and output them in ascending order...................... 293.4 To find the Quadrant of a given Co-ordinate position........................... 323.5 To Find the Roots of a Quadratic Equation......................................... 333.6 Checking for Prime........................................................................... 353.7 Factorial of a Number....................................................................... 373.8 To Generate Fibonacci Series............................................................ 383.9 Sum of ‘N’ Numbers and Average..................................................... 393.10 To Add Two Matrices....................................................................... 40
Summary.......................................................................................... 41Exercise........................................................................................... 42
Chapter 4
SEARCHING AND SORTING 434.0 Objectives........................................................................................ 434.1 Searching......................................................................................... 43
4.1.1 Sequential Search............................................................... 434.1.2 Binary Search.................................................................... 44
4.2 Sorting............................................................................................. 464.2.1 Insertion Sorting................................................................. 464.2.2 Selection Sorting................................................................. 484.2.3 Bubble Sort......................................................................... 50Summary.......................................................................................... 52Exercise........................................................................................... 52
Contents
IXContents
Chapter 5
RECURSION 54
5.0 Objectives........................................................................................ 545.1 What is Recursion?........................................................................... 545.2 Why do we need Recursion?............................................................. 555.3 When to use Recursion?................................................................... 555.4 Factorial of a Positive Number........................................................... 565.5 Finding the nth Fibonacci Number........................................................ 575.6 Sum of First N Integers..................................................................... 585.7 Binary Search .................................................................................. 585.8 Maximum and Minimum in the given list of N Elements....................... 595.9 Merge Sort....................................................................................... 615.10 Quick Sort ....................................................................................... 635.11 The Towers of Hanoi Problem........................................................... 66
5.11.1 The Recursive Algorithm..................................................... 665.12 Demerits of Recursion...................................................................... 69
Summary.......................................................................................... 70Exercise........................................................................................... 70
Chapter 6
REPRESENTATION AND TRAVERSAL OF A BINARY TREE 72
6.0 Objectives........................................................................................ 726.1 Binary Tree...................................................................................... 726.2 Representation of a Binary Tree........................................................ 74
6.2.1 Adjacency Matrix Represntation......................................... 746.2.2 Single Dimensional Array Representation............................. 756.2.3 Linked Representation of Binary Trees................................. 766.2.4 Binary Tree as a Data Structure........................................... 79
6.3 Traversal of a Binary Tree................................................................. 806.3.1 Traversal of a Binary Tree Represented in an Adjacency
Matrix .............................................................................. 816.3.2 Binary Tree Traversal of a Binary Tree From One
Dimensional Array Representation...................................... 836.3.3 Binary Tree Traversal in Linked Representation................... 85
6.4 Operations on Binary Tree................................................................. 86Summary.......................................................................................... 93Exercise........................................................................................... 93
References.............................................................................. 94