Data Structure and Algorithms Using C++ A Practical Implementation (Sachi Nandan Mohanty, Pabitra Kumar Tripathy) (Z-Library)
Author: Sachi Nandan Mohanty, Pabitra Kumar Tripathy
科学
No Description
📄 File Format:
PDF
💾 File Size:
8.9 MB
64
Views
0
Downloads
0.00
Total Donations
📄 Text Preview (First 20 pages)
ℹ️
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
📄 Page
1
(This page has no text content)
📄 Page
2
Table of Contents Cover Title Page Copyright Preface 1 Introduction to Data Structure 1.1 Definition and Use of Data Structure 1.2 Types of Data Structure 1.3 Algorithm 1.4 Complexity of an Algorithm 1.5 Efficiency of an Algorithm 1.6 Asymptotic Notations 1.7 How to Determine Complexities 1.8 Questions 2 Review of Concepts of ‘C++’ 2.1 Array 2.2 Function 2.3 Pointer 2.4 Structure 2.5 Questions 3 Sparse Matrix 3.1 What is Sparse Matrix 3.2 Sparse Matrix Representations 3.3 Algorithm to Represent the Sparse Matrix 3.4 Programs Related to Sparse Matrix 3.5 Why to Use Sparse Matrix Instead of Simple Matrix? 3.6 Drawbacks of Sparse Matrix 3.7 Sparse Matrix and Machine Learning
📄 Page
3
3.8 Questions 4 Concepts of Class 4.1 Introduction to CLASS 4.2 Access Specifiers in C++ 4.3 Declaration of Class 4.4 Some Manipulator Used In C++ 4.5 Defining the Member Functions Outside of the Class 4.6 Array of Objects 4.7 Pointer to Object 4.8 Inline Member Function 4.9 Friend Function 4.10 Static Data Member and Member Functions 4.11 Constructor and Destructor 4.12 Dynamic Memory Allocation 4.13 This Pointer 4.14 Class Within Class 4.15 Questions 5 Stack 5.1 STACK 5.2 Operations Performed With STACK 5.3 ALGORITHMS 5.4 Applications of STACK 5.5 Programming Implementations of STACK 5.6 Questions 6 Queue 6.1 Queue 6.2 Types of Queue 6.3 Linear Queue 6.4 Circular Queue 6.5 Double Ended Queue
📄 Page
4
6.6 Priority Queue 6.7 Programs 6.8 Questions 7 Linked List 7.1 Why Use Linked List? 7.2 Types of Link List 7.3 Single Link List 7.4 Programs Related to Single Linked List 7.5 Double Link List 7.6 Programs on Double Linked List 7.7 Header Linked List 7.8 Circular Linked List 7.9 Application of Linked List 7.10 Garbage Collection and Compaction 7.11 Questions 8 TREE 8.1 Tree Terminologies 8.2 Binary Tree 8.3 Representation of Binary Tree 8.4 Operations Performed With the Binary Tree 8.5 Traversing With Tree 8.6 Conversion of a Tree From Inorder and Preorder 8.7 Types of Binary Tree 8.8 Expression Tree 8.9 Binary Search Tree 8.10 Height Balanced Tree (AVL Tree) 8.11 Threaded Binary Tree 8.12 Heap Tree 8.13 Huffman Tree 8.14 Decision Tree
📄 Page
5
8.15 B-Tree 8.16 B + Tree 8.17 General Tree 8.18 Red–Black Tree 8.19 Questions 9 Graph 9.1 Graph Terminologies 9.2 Representation of Graph 9.3 Traversal of Graph 9.4 Spanning Tree 9.5 Single Source Shortest Path 9.6 All Pair Shortest Path 9.7 Topological Sorting 9.8 Questions 10 Searching and Sorting 10.1 Linear Search 10.2 Binary Search 10.3 Bubble Sort 10.4 Selection Sort 10.5 Insertion Sort 10.6 Merge Sort 10.7 Quick Sort 10.8 Radix Sort 10.9 Heap Sort 10.10 Questions 11 Hashing 11.1 Hash Functions 11.2 Collisions 11.3 Collision Resolution Methods 11.4 Clustering
📄 Page
6
11.5 Questions Index End User License Agreement
📄 Page
7
Scrivener Publishing 100 Cummings Center, Suite 541J Beverly, MA 01915-6106 Publishers at Scrivener Martin Scrivener (martin@scrivenerpublishing.com) Phillip Carmical (pcarmical@scrivenerpublishing.com)
📄 Page
8
Data Structure and Algorithms Using C++ A Practical Implementation Edited by Sachi Nandan Mohanty ICFAI Foundation For Higher Education, Hyderabad, India and Pabitra Kumar Tripathy Kalam Institute of Technology, Berhampur, India
📄 Page
9
This edition first published 2021 by John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, USA and Scrivener Publishing LLC, 100 Cummings Center, Suite 541J, Beverly, MA 01915, USA © 2021 Scrivener Publishing LLC For more information about Scrivener publications please visit www.scrivenerpublishing.com. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, except as permitted by law. Advice on how to obtain permission to reuse material from this title is available at http://www.wiley.com/go/permissions. Wiley Global Headquarters 111 River Street, Hoboken, NJ 07030, USA For details of our global editorial offices, customer services, and more information about Wiley products visit us at www.wiley.com. Limit of Liability/Disclaimer of Warranty While the publisher and authors have used their best efforts in preparing this work, they make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation any implied warranties of merchantability or fitness for a particular purpose. No warranty may be created or extended by sales representatives, written sales materials, or promotional statements for this work. The fact that an organization, website, or product is referred to in this work as a citation and/or potential source of further information does not mean that the publisher and authors endorse the information or services the organization, website, or product may provide or recommendations it may make. This work is sold with the understanding that the publisher is not engaged in rendering professional services. The advice and strategies contained herein may not be suitable for your situation. You should consult with a specialist where appropriate. Neither the publisher nor authors shall be liable for any loss of profit or any other commercial damages, including but not limited to special, incidental, consequential, or other damages. Further, readers should be aware that websites listed in this work may have changed or disappeared between when this work was written and when it is read. Library of Congress Cataloging-in-Publication Data ISBN 978-1-119-75054-3 Cover image: Pixabay.Com Cover design by Russell Richardson Set in size of 11pt and Minion Pro by Manila Typesetting Company, Makati, Philippines
📄 Page
10
Printed in the USA 10 9 8 7 6 5 4 3 2 1
📄 Page
11
Preface Welcome to the first edition of Data Structures Using and Algorithms C++. A data structure is the logical or mathematical arrangement of data in memory. To be effective, data has to be organized in a manner that adds to the efficiency of an algorithm and also describe the relationships between these data items and the operations that can be performed on these items. The choice of appropriate data structures and algorithms forms the fundamental step in the design of an efficient program. Thus, a deep understanding of data structure concepts is essential for students who wish to work on the design and implementation of system software written in C++, an object-oriented programming language that has gained popularity in both academia and industry. Therefore, this book was developed to provide comprehensive and logical coverage of data structures like stacks, queues, linked lists, trees and graphs, which makes it an excellent choice for learning data structures. The objective of the book is to introduce the concepts of data structures and apply these concepts in real-life problem solving. Most of the examples presented resulted from student interaction in the classroom. This book utilizes a systematic approach wherein the design of each of the data structures is followed by algorithms of different operations that can be performed on them and the analysis of these algorithms in terms of their running times. This book was designed to serve as a textbook for undergraduate engineering students across all disciplines and postgraduate level courses in computer applications. Young researchers working on efficient data storage and related applications will also find it to be a helpful reference source to guide them in the newly established techniques of this rapidly growing research field. Dr. Sachi Nandan Mohanty and Prof. Pabitra Kumar Tripathy December 2020
📄 Page
12
1 Introduction to Data Structure 1.1 Definition and Use of Data Structure Data structure is the representation of the logical relationship existing between individual elements of data. In other words the data structure is a way of organizing all data items that considers not only the elements stored but also their relationship to each other. Data structure specifies Organization of data Accessing methods Degree of associativity Processing alternatives for information The data structures are the building blocks of a program and hence the selection of a particular data structure stresses on The data structures must be rich enough in structure to reflect the relationship existing between the data, and The structure should be simple so that we can process data effectively whenever required. In mathematically Algorithm + Data Structure = Program Finally we can also define the data structure as the “Logical and mathematical model of a particular organization of data” 1.2 Types of Data Structure Data structure can be broadly classified into two categories as Linear and Non-Linear
📄 Page
13
Linear Data Structures In linear data structures, values are arranged in linear fashion. Arrays, linked lists, stacks, and queues are the examples of linear data structures in which values are stored in a sequence. Non-Linear Data Structure This type is opposite to linear. The data values in this structure are not arranged in order. Tree, graph, table, and sets are the examples of nonlinear data structure. Operations Performed in Data Structure In data structure we can perform the operations like Traversing Insertion Deletion Merging Sorting Searching 1.3 Algorithm The step by step procedure to solve a problem is known as the ALGORITHM. An algorithm is a well-organized, pre-arranged, and defined computational module that receives some values or set of values as input and provides a single or set of values as out put.
📄 Page
14
These well-defined computational steps are arranged in sequence, which processes the given input into output. An algorithm is said to be accurate and truthful only when it provides the exact wanted output. The efficiency of an algorithm depends on the time and space complexities. The complexity of an algorithm is the function which gives the running time and/or space in terms of the input size. Steps Required to Develop an Algorithm Finding a method for solving a problem. Every step of an algorithm should be defined in a precise and in a clear manner. Pseudo code is also used to describe an algorithm. The next step is to validate the algorithm. This step includes all the steps in our algorithm and should be done manually by giving the required input, perform the required steps including in our algorithm and should get the required amount of output in a finite amount of time. Finally implement the algorithm in terms of programming language. Mathematical Notations and Functions ❖ Floor and Ceiling Functions Floor function returns the greatest integer that does not exceed the number. Ceiling function returns the least integer that is not less than the number. ❖ Remainder Function
📄 Page
15
To find the remainder “mod” function is being used as ❖ To find the Integer and Absolute value of a number INT(5.34) = 5 This statement returns the integer part of the number INT(- 6.45) = 6 This statement returns the absolute as well as the integer portion of the number ❖ Summation Symbol To add a series of number as a1+ a2 + a3 +............+ an the symbol Σ is used ❖ Factorial of a Number The product of the positive integers from 1 to n is known as the factorial of n and it is denoted as n!. Algorithemic Notations While writing the algorithm the comments are provided with in [ ]. The assignment should use the symbol “: =” instead of “=” For Input use Read : variable name For output use write : message/variable name The control structures can also be allowed to use inside an algorithm but their way of approaching will be some what different as Simple If If condition, then: Statements [end of if structure] If...else
📄 Page
16
If condition, then: Statements Else : Statements [end of if structure] If...else ladder If condition1, then: Statements Else If condition2, then: Statements Else If condition3, then: Statements ………………………………………… ………………………………………… ………………………………………… Else If conditionN, then: Statements Else: Statements [end of if structure] LOOPING CONSTRUCT Repeat for var = start_value to end_value by step_value Statements [end of loop] Repeat while condition: Statements [end of loop] Ex : repeat for I = 1 to 10 by 2 Write: i [end of loop] OUTPUT 1 3 5 7 9 1.4 Complexity of an Algorithm The complexity of programs can be judged by criteria such as whether it satisfies the original specification task, whether the code is
📄 Page
17
readable. These factors affect the computing time and storage requirement of the program. Space Complexity The space complexity of a program is the amount of memory it needs to run to completion. The space needed by a program is the sum of the following components: A fixed part that includes space for the code, space for simple variables and fixed size component variables, space for constants, etc. A variable part that consists of the space needed by component variables whose size is dependent on the particular problem instance being solved, and the stack space used by recursive procedures. Time Complexity The time complexity of a program is the amount of computer time it needs to run to completion. The time complexity is of two types such as Compilation time Runtime The amount of time taken by the compiler to compile an algorithm is known as compilation time. During compilation time it does not calculate for the executable statements, it calculates only the declaration statements and checks for any syntax and semantic errors. The run time depends on the size of an algorithm. If the number of instructions in an algorithm is large, then the run time is also large, and if the number of instructions in an algorithm is small, then the time for executing the program is also small. The runtime is calculated for executable statements and not for declaration statements. Suppose space is fixed for one algorithm then only run time will be considered for obtaining the complexity of algorithm, these are
📄 Page
18
Best case Worst case Average case Best Case Generally, most of the algorithms behave sometimes in best case. In this case, algorithm searches the element for the first time by itself. For example: In linear search, if it finds the element for the first time by itself, then it behaves as the best case. Best case takes shortest time to execute, as it causes the algorithms to do the least amount of work. Worst Case In worst case, we find the element at the end or when searching of elements fails. This could involve comparing the key to each list value for a total of N comparisons. For example in linear search suppose the element for which algorithm is searching is the last element of array or it is not available in array then algorithm behaves as worst case. Average Case Analyzing the average case behavior algorithm is a little bit complex than the best case and worst case. Here, we take the probability with a list of data. Average case of algorithm should be the average number of steps but since data can be at any place, so finding exact behavior of algorithm is difficult. As the volume of data increases, the average case of algorithm behaves like the worst case of algorithm. 1.5 Efficiency of an Algorithm Efficiency of an algorithm can be determined by measuring the time, space, and amount of resources it uses for executing the program. The amount of time taken by an algorithm can be calculated by finding the number of steps the algorithm executes, while the space refers to the number of units it requires for memory storage.
📄 Page
19
1.6 Asymptotic Notations The asymptotic notations are the symbols which are used to solve the different algorithms and the notations are Big Oh Notation (O) Little Oh Notation (o) Omega Notation (Ω) Theta Notation (θ) Big Oh (O) Notation This Notation gives the upper bound for a function to within a constant factor. We write f(n) = O(g(n)) if there are +ve constants n0 and C such that to the right of n0, the value of f(n) always lies on or below Cg(n) Omega Notation (Ω) This notation gives a lower bound for a function to with in a constant factor. We write f(n) = Ωg(n) if there are positive constants n0 and C such that to the right of n0 the value of f(n) always lies on or above Cg(n) Theta Notation (θ) This notation bounds the function to within constant factors. We say f(n) = θg(n) if there exists +ve constants n0, C1 and C2 such that to the right of n0 the value of f(n) always lies between c1g(n) and c2(g(n)) inclusive. Little Oh Notation (o) Introduction An important question is: How efficient is an algorithm or piece of code? Efficiency covers lots of resources, including: CPU (time) usage Memory usage
📄 Page
20
Disk usage Network usage All are important but we will mostly talk about CPU time Be careful to differentiate between: Performance: how much time/memory/disk/... is actually used when a program is running. This depends on the machine, compiler, etc., as well as the code. Complexity: how do the resource requirements of a program or algorithm scale, i.e., what happens as the size of the problem being solved gets larger. Complexity affects performance but not the other way around. The time required by a method is proportional to the number of “basic operations” that it performs. Here are some examples of basic operations: one arithmetic operation (e.g., +, *). one assignment one test (e.g., x == 0) one read one write (of a primitive type) Note: As an example, O(1) refers to constant time. O(n) indicates linear time; O(nk) (k fixed) refers to polynomial time; O(log n) is called logarithmic time; O(2n) refers to exponential time, etc. n2 + 3n + 4 is O(n2), since n2 + 3n + 4 < 2n2 for all n > 10. Strictly speaking, 3n + 4 is O(n2), too, but big-O notation is often misused to mean equal to rather than less than. 1.7 How to Determine Complexities In general, how can you determine the running time of a piece of code? The answer is that it depends on what kinds of statements are used.
The above is a preview of the first 20 pages. Register to read the complete e-book.