100% found this document useful (5 votes)
38 views

Download Full (Ebook) A Textbook of Data Structures and Algorithms, Volume 3 : Mastering Advanced Data Structures and Algorithm Design Strategies by G. A. Vijayalakshmi Pai ISBN 9781786308924, 1786308924 PDF All Chapters

The document provides information about various eBooks on data structures and algorithms available for download at ebooknice.com. It includes titles such as 'A Textbook of Data Structures and Algorithms, Volume 3' by G. A. Vijayalakshmi Pai and other related works, along with their ISBNs and links for access. The document emphasizes the availability of instant digital products in multiple formats for immediate reading on any device.

Uploaded by

lesarrogozqf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (5 votes)
38 views

Download Full (Ebook) A Textbook of Data Structures and Algorithms, Volume 3 : Mastering Advanced Data Structures and Algorithm Design Strategies by G. A. Vijayalakshmi Pai ISBN 9781786308924, 1786308924 PDF All Chapters

The document provides information about various eBooks on data structures and algorithms available for download at ebooknice.com. It includes titles such as 'A Textbook of Data Structures and Algorithms, Volume 3' by G. A. Vijayalakshmi Pai and other related works, along with their ISBNs and links for access. The document emphasizes the availability of instant digital products in multiple formats for immediate reading on any device.

Uploaded by

lesarrogozqf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

Download the Full Ebook and Access More Features - ebooknice.

com

(Ebook) A Textbook of Data Structures and


Algorithms, Volume 3 : Mastering Advanced Data
Structures and Algorithm Design Strategies by G.
A. Vijayalakshmi Pai ISBN 9781786308924,
1786308924
https://ebooknice.com/product/a-textbook-of-data-structures-
and-algorithms-volume-3-mastering-advanced-data-structures-
and-algorithm-design-strategies-47399684

OR CLICK HERE

DOWLOAD EBOOK

Download more ebook instantly today at https://ebooknice.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

Start reading on any device today!

(Ebook) A Textbook of Data Structures and Algorithms,


Volume 2 by G. A. Vijayalakshmi Pai ISBN 9781786308917,
1786308916
https://ebooknice.com/product/a-textbook-of-data-structures-and-
algorithms-volume-2-47696298

ebooknice.com

(Ebook) JavaScript Data Structures and Algorithms: An


Introduction to Understanding and Implementing Core Data
Structure and Algorithm Fundamentals by Sammie Bae ISBN
9781484239889, 1484239881
https://ebooknice.com/product/javascript-data-structures-and-
algorithms-an-introduction-to-understanding-and-implementing-core-
data-structure-and-algorithm-fundamentals-52953836
ebooknice.com

(Ebook) Data Structures and Algorithm Analysis in Java by


Mark A. Weiss ISBN 9780132576277, 0132576279

https://ebooknice.com/product/data-structures-and-algorithm-analysis-
in-java-2612714

ebooknice.com

(Ebook) Introduction to Algorithms & Data Structures 3:


Learn Linear Data Structures with Videos & Interview
Questions. by Aremu Bolakale, Jr. Charles Johnson. ISBN
9791222443188, 122244318X
https://ebooknice.com/product/introduction-to-algorithms-data-
structures-3-learn-linear-data-structures-with-videos-interview-
questions-52327550
ebooknice.com
(Ebook) Data Structures and Algorithm Analysis in Java,
3rd Edition by Dr. Clifford A. Shaffer ISBN 0486485811

https://ebooknice.com/product/data-structures-and-algorithm-analysis-
in-java-3rd-edition-2391666

ebooknice.com

(Ebook) Data Structures and Algorithm Analysis in C++, 3rd


Edition by Dr. Clifford A. Shaffer ISBN 048648582X

https://ebooknice.com/product/data-structures-and-algorithm-analysis-
in-c-3rd-edition-2384264

ebooknice.com

(Ebook) A Common-Sense Guide to Data Structures and


Algorithms in Python, Volume 1 by Jay Wengrow ISBN
9798888650356, 8888650350
https://ebooknice.com/product/a-common-sense-guide-to-data-structures-
and-algorithms-in-python-volume-1-57759564

ebooknice.com

(Ebook) A Common-Sense Guide to Data Structures and


Algorithms by Jay Wengrow ISBN 9781680502442, 1680502441

https://ebooknice.com/product/a-common-sense-guide-to-data-structures-
and-algorithms-7118874

ebooknice.com

(Ebook) Probabilistic data structures and algorithms for


big data applications by Gakhov, Andrii ISBN
9783748190486, 3748190484
https://ebooknice.com/product/probabilistic-data-structures-and-
algorithms-for-big-data-applications-9979270

ebooknice.com
A Textbook of Data Structures and Algorithms 3
One of the greatest lessons I have learnt in my life is
to pay as much attention to the means of work as to its end…
I have been always learning great lessons from that one principle,
and it appears to me that all the secret of success is there;
to pay as much attention to the means as to the end….
Let us perfect the means; the end will take care of itself.

– Swami Vivekananda
(Lecture Delivered at Los Angeles, California, January 4, 1900)
A Textbook of Data
Structures and Algorithms 3

Mastering Advanced Data Structures


and Algorithm Design Strategies

G A Vijayalakshmi Pai
First published 2022 in Great Britain and the United States by ISTE Ltd and John Wiley & Sons, Inc.

Previous edition published in 2008 as “Data Structures and Algorithms: Concepts, Techniques and
Applications” by McGraw Hill Education (India) Pvt Ltd. © McGraw Hill Education (India) Pvt Ltd.
2008

Apart from any fair dealing for the purposes of research or private study, or criticism or review, as
permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced,
stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers,
or in the case of reprographic reproduction in accordance with the terms and licenses issued by the
CLA. Enquiries concerning reproduction outside these terms should be sent to the publishers at the
undermentioned address:

ISTE Ltd John Wiley & Sons, Inc.


27-37 St George’s Road 111 River Street
London SW19 4EU Hoboken, NJ 07030
UK USA

www.iste.co.uk www.wiley.com

© ISTE Ltd 2022


The rights of G A Vijayalakshmi Pai to be identified as the author of this work have been asserted by her
in accordance with the Copyright, Designs and Patents Act 1988.

Any opinions, findings, and conclusions or recommendations expressed in this material are those of the
author(s), contributor(s) or editor(s) and do not necessarily reflect the views of ISTE Group.

Library of Congress Control Number: 2022947642

British Library Cataloguing-in-Publication Data


A CIP record for this book is available from the British Library
ISBN 978-1-78630-892-4
Contents

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii

Chapter 13. Hash Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1


13.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
13.1.1. Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
13.2. Hash table structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
13.3. Hash functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
13.3.1. Building hash functions . . . . . . . . . . . . . . . . . . . . . . . . . 4
13.4. Linear open addressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
13.4.1. Operations on linear open addressed hash tables. . . . . . . . . . . 8
13.4.2. Performance analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
13.4.3. Other collision resolution techniques with open addressing . . . . 11
13.5. Chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
13.5.1. Operations on chained hash tables . . . . . . . . . . . . . . . . . . . 15
13.5.2. Performance analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
13.6. Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
13.6.1. Representation of a keyword table in a compiler. . . . . . . . . . . 18
13.6.2. Hash tables in the evaluation of a join operation on
relational databases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
13.6.3. Hash tables in a direct file organization . . . . . . . . . . . . .. . . 22
13.7. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
vi A Textbook of Data Structures and Algorithms 3

Chapter 14. File Organizations . . . . . . . . . . . . . . . . . . . . . . . . . . 33


14.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
14.2. Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
14.3. Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
14.4. Basic file operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
14.5. Heap or pile organization . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
14.5.1. Insert, delete and update operations . . . . . . . . . . . . . . . . . . 39
14.6. Sequential file organization . . . . . . . . . . . . . . . . . . . . . . . . . 39
14.6.1. Insert, delete and update operations . . . . . . . . . . . . . . . . . . 39
14.6.2. Making use of overflow blocks . . . . . . . . . . . . . . . . . . . . . 40
14.7. Indexed sequential file organization . . . . . . . . . . . . . . . . . . . . 41
14.7.1. Structure of the ISAM files . . . . . . . . . . . . . . . . . . . . . . . 41
14.7.2. Insert, delete and update operations for a naïve ISAM file . . . . . 42
14.7.3. Types of indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
14.8. Direct file organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
14.9. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

Chapter 15. k-d Trees and Treaps . . . . . . . . . . . . . . . . . . . . . . . . 61


15.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
15.2. k-d trees: structure and operations . . . . . . . . . . . . . . . . . . . . . . 61
15.2.1. Construction of a k-d tree . . . . . . . . . . . . . . . . . . . . . . . . 65
15.2.2. Insert operation on k-d trees . . . . . . . . . . . . . . . . . . . . . . . 69
15.2.3. Find minimum operation on k-d trees . . . . . . . . . . . . . . . . . 70
15.2.4. Delete operation on k-d trees . . . . . . . . . . . . . . . . . . . . . . 72
15.2.5. Complexity analysis and applications of k-d trees . . . . . . . . . . 74
15.3. Treaps: structure and operations . . . . . . . . . . . . . . . . . . . . . . . 76
15.3.1. Treap structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
15.3.2. Operations on treaps . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
15.3.3. Complexity analysis and applications of treaps . . . . . . . . . . . 82
15.4. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

Chapter 16. Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93


16.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
16.2. Linear search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
16.2.1. Ordered linear search . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
16.2.2. Unordered linear search . . . . . . . . . . . . . . . . . . . . . . . . . 94
16.3. Transpose sequential search . . . . . . . . . . . . . . . . . . . . . . . . . 96
16.4. Interpolation search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
16.5. Binary search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
16.5.1. Decision tree for binary search . . . . . . . . . . . . . . . . . . . . . 101
Contents vii

16.6. Fibonacci search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104


16.6.1. Decision tree for Fibonacci search . . . . . . . . . . . . . . . . . . . 105
16.7. Skip list search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
16.7.1. Implementing skip lists . . . . . . . . . . . . . . . . . . . . . . . . . . 112
16.7.2. Insert operation in a skip list . . . . . . . . . . . . . . . . . . . . . . 113
16.7.3. Delete operation in a skip list . . . . . . . . . . . . . . . . . . . . . . 114
16.8. Other search techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
16.8.1. Tree search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
16.8.2. Graph search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
16.8.3. Indexed sequential search . . . . . . . . . . . . . . . . . . . . . . . . 116
16.9. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

Chapter 17. Internal Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131


17.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
17.2. Bubble sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
17.2.1. Stability and performance analysis . . . . . . . . . . . . . . . . . . . 134
17.3. Insertion sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
17.3.1. Stability and performance analysis . . . . . . . . . . . . . . . . . . . 136
17.4. Selection sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
17.4.1. Stability and performance analysis . . . . . . . . . . . . . . . . . . . 140
17.5. Merge sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
17.5.1. Two-way merging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
17.5.2. k-way merging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
17.5.3. Non-recursive merge sort procedure . . . . . . . . . . . . . . . . . . 144
17.5.4. Recursive merge sort procedure . . . . . . . . . . . . . . . . . . . . 145
17.6. Shell sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
17.6.1. Analysis of shell sort . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
17.7. Quick sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
17.7.1. Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
17.7.2. Quick sort procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
17.7.3. Stability and performance analysis . . . . . . . . . . . . . . . . . . . 158
17.8. Heap sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
17.8.1. Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
17.8.2. Construction of heap . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
17.8.3. Heap sort procedure. . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
17.8.4. Stability and performance analysis . . . . . . . . . . . . . . . . . . . 167
17.9. Radix sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
17.9.1. Radix sort method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
17.9.2. Most significant digit first sort . . . . . . . . . . . . . . . . . . . . . 171
17.9.3. Performance analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
17.10. Counting sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
viii A Textbook of Data Structures and Algorithms 3

17.10.1. Performance analysis . . . . . . . . . . . . . . . . . . . . . . . . . . 175


17.11. Bucket sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
17.11.1. Performance analysis . . . . . . . . . . . . . . . . . . . . . . . . . . 178
17.12. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179

Chapter 18. External Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . 197


18.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
18.1.1. The principle behind external sorting . . . . . . . . . . . . . . . . . 197
18.2. External storage devices . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
18.2.1. Magnetic tapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
18.2.2. Magnetic disks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
18.3. Sorting with tapes: balanced merge . . . . . . . . . . . . . . . . . . . . . 202
18.3.1. Buffer handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
18.3.2. Balanced P-way merging on tapes . . . . . . . . . . . . . . . . . . . 205
18.4. Sorting with disks: balanced merge . . . . . . . . . . . . . . . . . . . . . 206
18.4.1. Balanced k-way merging on disks . . . . . . . . . . . . . . . . . . . 207
18.4.2. Selection tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
18.5. Polyphase merge sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
18.6. Cascade merge sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
18.7. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

Chapter 19. Divide and Conquer . . . . . . . . . . . . . . . . . . . . . . . . . 229


19.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
19.2. Principle and abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
19.3. Finding maximum and minimum . . . . . . . . . . . . . . . . . . . . . . 231
19.3.1. Time complexity analysis . . . . . . . . . . . . . . . . . . . . . . . . 232
19.4. Merge sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
19.4.1. Time complexity analysis . . . . . . . . . . . . . . . . . . . . . . . . 233
19.5. Matrix multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
19.5.1. Divide and Conquer-based approach to “high school”
method of matrix multiplication . . . . . . . . . . . . . . . . . . . . . . . . . 234
19.5.2. Strassen’s matrix multiplication algorithm . . . . . . . . . . . . . . 236
19.6. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

Chapter 20. Greedy Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245


20.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
20.2. Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
20.3. Knapsack problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
20.3.1. Greedy solution to the knapsack problem . . . . . . . . . . . . . . . 247
Contents ix

20.4. Minimum cost spanning tree algorithms . . . . . . . . . . . . . . . . . . 249


20.4.1. Prim’s algorithm as a greedy method . . . . . . . . . . . . . . . . . 250
20.4.2. Kruskal’s algorithm as a greedy method . . . . . . . . . . . . . . . 250
20.5. Dijkstra’s algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
20.6. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

Chapter 21. Dynamic Programming . . . . . . . . . . . . . . . . . . . . . . 261


21.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
21.2. 0/1 knapsack problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
21.2.1. Dynamic programming-based solution . . . . . . . . . . . . . . . . 264
21.3. Traveling salesperson problem. . . . . . . . . . . . . . . . . . . . . . . . 266
21.3.1. Dynamic programming-based solution . . . . . . . . . . . . . . . . 267
21.3.2. Time complexity analysis and applications of traveling
salesperson problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
21.4. All-pairs shortest path problem . . . . . . . . . . . . . . . . . . . . . . . 269
21.4.1. Dynamic programming-based solution . . . . . . . . . . . . . . . . 270
21.4.2. Time complexity analysis . . . . . . . . . . . . . . . . . . . . . . . . 272
21.5. Optimal binary search trees. . . . . . . . . . . . . . . . . . . . . . . . . . 272
21.5.1. Dynamic programming-based solution . . . . . . . . . . . . . . . . 274
21.5.2. Construction of the optimal binary search tree . . . . . . . . . . . . 276
21.5.3. Time complexity analysis . . . . . . . . . . . . . . . . . . . . . . . . 279
21.6. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280

Chapter 22. P and NP Class of Problems . . . . . . . . . . . . . . . . . . . 287


22.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
22.2. Deterministic and nondeterministic algorithms . . . . . . . . . . . . . . 289
22.3. Satisfiability problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
22.3.1. Conjunctive normal form and Disjunctive normal form . . . . . . 294
22.3.2. Definition of the satisfiability problem . . . . . . . . . . . . . . . . 294
22.3.3. Construction of CNF and DNF from a logical formula . . . . . . . 295
22.3.4. Transformation of a CNF into a 3-CNF . . . . . . . . . . . . . . . . 296
22.3.5. Deterministic algorithm for the satisfiability problem . . . . . . . 297
22.3.6. Nondeterministic algorithm for the satisfiability problem . . . . . 297
22.4. NP-complete and NP-hard problems . . . . . . . . . . . . . . . . . . . . 297
22.4.1. Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
22.5. Examples of NP-hard and NP-complete problems . . . . . . . . . . . . 300
22.6. Cook’s theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
22.7. The unsolved problem 𝑃 ? 𝑁𝑃 . . . . . . . . . . . . . . . . . . . . . . . 303
22.8. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
x A Textbook of Data Structures and Algorithms 3

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313

Summaries of other volumes . . . . . . . . . . . . . . . . . . . . . . . . . . . 317


Preface

Efficient problem solving using computers, irrespective of the discipline or


application, calls for the design of efficient algorithms. The inclusion of appropriate
data structures is of critical importance to the design of efficient algorithms. In other
words, good algorithm design must go hand in hand with appropriate data
structures for an efficient program design to solve a problem.

Data structures and algorithms is a fundamental course in computer science,


which most undergraduate and graduate programs in computer science and other
allied disciplines in science and engineering offer during the early stages of the
respective programs, either as a core or as an elective course. The course enables
students to have a much-needed foundation for efficient programming, leading to
better problem solving in their respective disciplines.

Most of the well-known text books/monographs on this subject have discussed the
concepts in relation to a programming language – beginning with Pascal and spanning
a spectrum of them such as C, C++, C#, Java, Python and so on, essentially calling for
ample knowledge of the language, before one proceeds to try and understand the
data structure. There does remain a justification in this. The implementation of data
structures in the specific programming language need to be demonstrated or the
algorithms pertaining to the data structures concerned need a convenient medium of
presentation and when this is the case, why not a programming language?

Again, while some authors have insisted on using their books for an advanced
level course, there are some who insist on a working knowledge of the specific
programming language as a prerequisite to using the book. However, in the case of a
core course, as it is in most academic programs, it is not uncommon for a novice or a
sophomore to be bewildered by the “miles of code” that demonstrate or explain a
data structure, rendering the subject difficult to comprehend. In fact, the efforts that
one needs to put in to comprehend the data structure and its applications are
xii A Textbook of Data Structures and Algorithms 3

distracted by the necessity to garner sufficient programming knowledge to follow


the code. It is indeed ironic that while a novice is taught data structures to appreciate
programming, in reality it turns out that one learns programming to appreciate data
structures!

In my decades-old experience of offering the course to graduate programs, which


admits students from diverse undergraduate disciplines, with little to no strong
knowledge of programming, I had several occasions to observe this malady. In fact,
it is not uncommon in some academic programs, especially graduate programs
which, due to their shorter duration, have a course in programming and data
structures running in parallel in the same semester, much to the chagrin of the
novice learner! That a novice is forced to learn data structures through their
implementation (in a specific programming language), when in reality it ought to be
learning augmented with the implementation of the data structures, has been the
reason behind the fallout.

A solution to this problem would be to


i) Frame the course such that the theory deals with the concepts, techniques and
applications of data structures and algorithms, not taking recourse to any specific
programming language, but instead settling for a pseudo-language, which clearly
expounds the data structure. Additionally, supplementing the course material with
illustrative problems, review questions and exercises to reinforce the students’ grasp
of the concepts would help them gain useful insights while learning.
ii) Augment the theory with laboratory sessions to enable the student to
implement the data structure in itself or as embedded in an application, in the
language of his/her own choice or as insisted upon in the curriculum. This would
enable the student who has acquired sufficient knowledge and insight into the data
structures to appreciate the beauty and merits of employing the data structure by
programming it themself, rather than “look” for the data structure in a prewritten
code.

This means that text books catering to the fundamental understanding of the data
structure concepts for use as course material in the classroom are as much needed as
the books that cater to the implementation of data structures in a programming
language for use in the laboratory sessions. While most books in the market conform
to the latter, bringing out a book to be classroom course material and used by
instructors handling a course on data structures and algorithms, comprehensive
enough for the novice students to benefit from, has been the main motivation in
writing this book.

As such, the book details concepts, techniques and applications pertaining to data
structures and algorithms, independent of any programming language, discusses
Preface xiii

several examples and illustrative problems, poses review questions to reinforce the
understanding of the theory, and presents a suggestive list of programming
assignments to aid implementation of the data structures and algorithms learned.

In fact, the book may either be independently used as a textbook since it is self-
contained or serve as a companion for books discussing data structures and
algorithms implemented in specific programming languages such as C, C++, Java,
Python, and so on.

At this juncture, it needs to be pointed out that a plethora of programming


resources and freely downloadable implementations of the majority of the data
structures in almost all popular languages are available on the Internet, which can
undoubtedly serve as good guides for the learner. However, it has to be emphasized
that an earnest student of data structures and algorithms must invest a lot of time and
self-effort in trying to implement the data structures and algorithms learned, in a
language of one’s choice, all by oneself, in order to attain a thorough grasp of the
concepts.

About this edition

This edition is a largely revised and enlarged version of its predecessor,


published by McGraw Hill, USA. The earlier edition published in 2008 saw 15
reprints in its life span of 13 years (ending January 2022) and was recommended as
a text book for the course in several universities and colleges. It comprised 17
chapters categorized into five parts and reinforced learning through 133 illustrative
problems, 215 review questions and 74 programming assignments.

The features of this new edition are as follows:


– There are 22 chapters spread across three volumes that detail sequential linear
data structures, linked linear data structures, nonlinear data structures, advanced data
structures, searching and sorting algorithms, algorithm design techniques and NP-
completeness.
– The data structures of k-d trees and treaps have been elaborated in a newly
included chapter (Chapter 15) in Volume 3.
– The data structures of strings, bit rays, unrolled linked lists, self-organizing
linked lists, segment trees and k-ary trees have been introduced in the appropriate
sections of the existing chapters in Volumes 1 and 2.
– The concepts of counting binary search trees and Kruskal’s algorithm have
been detailed in the appropriate sections of the existing chapters in Volume 2.
xiv A Textbook of Data Structures and Algorithms 3

– Skip list search, counting sort and bucket sort have been included in the
chapters on searching and sorting algorithms in Volume 3.
– The algorithm design techniques of divide and conquer, the greedy method
and dynamic programming have been elaborately discussed in Chapters 19–21 in
Volume 3.
– The concept of NP-completeness has been detailed in a newly included
chapter, Chapter 22 in Volume 3.
– Several illustrative problems, review questions and programming assignments
have been added to enrich the content and aid in understanding the concepts. The
new edition thus includes 181 illustrative problems, 276 review questions and 108
programming assignments.

Organization of the book

The book comprises three volumes, namely, Volume 1: Chapters 1–7, Volume 2:
Chapters 8–12 and Volume 3: Chapters 13–22.

Volume 1 opens with an introduction to data structures and concepts pertaining


to the analysis of algorithms, detailed in Chapters 1 and 2, which is essential to
appreciate the theories and algorithms related to data structures and their
applications.

Chapters 3–5 detail sequential linear data structures, namely, arrays, strings, bit
arrays, stacks, queues, priority queues and dequeues, and their applications.
Chapters 6 and 7 elucidate linked linear data structures, namely linked lists, linked
stacks and linked queues, and their applications.

Volume 2 details nonlinear data structures. Chapters 8 and 9 elaborate on the


nonlinear data structures of trees, binary trees and graphs, and their applications.
Chapters 10–12 highlight the advanced data structures of binary search trees, AVL
trees, B trees, tries, red-black trees and splay trees, and their applications.

Volume 3 details an assortment of data structures, algorithm design strategies


and their applications.

Chapters 13–15 discuss hash tables, files, k-d trees and treaps. Chapter 16
discusses the search algorithms of linear search, transpose sequential search,
interpolation search, binary search, Fibonacci search, skip list search and other
search techniques.
Preface xv

Chapter 17 elaborates on the internal sorting algorithms of bubble sort, insertion


sort, selection sort, merge sort, shell sort, quick sort, heap sort, radix sort, counting
sort and bucket sort, and Chapter 18 discusses the external sorting techniques of
sorting with tapes, sorting with disks, polyphase merge sort and cascade merge sort.

Chapters 19–21 detail the algorithm design strategies of divide and conquer, the
greedy method and dynamic programming and their applications.

Chapter 22 introduces the theories and concepts of NP-completeness.

For a full list of the contents of Volumes 1 and 2, see the summary at the end of
this book.

Salient features of the book

The features of the book are as follows:


– all-around emphasis on theory, problems, applications and programming
assignments;
– simple and lucid explanation of the theory;
– inclusion of several applications to illustrate the use of data structures and
algorithms;
– several worked-out examples as illustrative problems in each chapter;
– list of programming assignments at the end of each chapter;
– review questions to strengthen understanding;
– self-contained text for use as a text book for either an introductory or advanced
level course.

Target audience

The book could be used both as an introductory or an advanced-level textbook


for undergraduate, graduate and research programs, which offer data structures and
algorithms as a core course or an elective course. While the book is primarily meant
to serve as a course material for use in the classroom, it could be used as a
companion guide during the laboratory sessions to nurture better understanding of
the theoretical concepts.

An introductory level course for a duration of one semester or 60 lecture hours,


targeting an undergraduate program or first-year graduate program or a diploma
xvi A Textbook of Data Structures and Algorithms 3

program or a certificate program, could include Chapters 1–7 of Volume 1, Chapter 8


of Volume 2, Chapters 13, 16 (sections 16.1, 16.2, 16.5) and 17 (sections 17.1–17.3,
17.5, 17.7) of Volume 3 in its curriculum.

A middle-level course for a duration of one semester or 60 lecture hours


targeting senior graduate-level programs and research programs such as MS/PhD
could include Chapters 1–7 of Volume 1, Chapters 8–11 of Volume 2, Chapter 13
and selective sections of Chapters 16–17 of Volume 3.

An advanced level course that focuses on advanced data structures and algorithm
design could begin with a review of Chapter 8 and include Chapters 9–12 of Volume 2,
Chapters 14 and 15 and selective sections of Chapters 16–18, and Chapters 19–22 of
Volume 3 in its curriculum based on the level of prerequisite courses satisfied.

Chapters 8–10 and Chapter 11 (sections 11.1–11.3) of Volume 2 and Chapters 13,
14 and 18 of Volume 3 could be useful to include in a curriculum that serves as a
prerequisite for a course on database management systems.

To re-emphasize, all theory sessions must be supplemented with laboratory


sessions to encourage learners to implement the concepts learned in an appropriate
language that adheres to the curricular requirements of the programs concerned.
Acknowledgments

The author is grateful to ISTE Ltd., London, UK, for accepting to publish the
book, in collaboration with John Wiley & Sons Inc., USA. She expresses her
appreciation to the publishing team, for their professionalism and excellent
production practices, while bringing out this book in three volumes.

The author expresses her sincere thanks to the Management and Principal, PSG
College of Technology, Coimbatore, India for the support extended while writing
the book.

The author would like to place on record her immense admiration and affection
for her father, Late Professor G. A. Krishna Pai and her mother Rohini Krishna Pai
for their unbounded encouragement and support to help her follow her life lessons
and her sisters Dr. Rekha Pai and Udaya Pai, for their unstinted, anywhere-anytime-
anything kind of help and support, all of which were instrumental and inspirational
in helping this author create this work.

G. A. Vijayalakshmi Pai
August 2022
13

Hash Tables

13.1. Introduction

The data structures of binary search trees, AVL trees, B trees, tries, red-black
trees and splay trees discussed so far in the book (Volume 2) are tree-based data
structures. These are nonlinear data structures and serve to capture the hierarchical
relationship that exists between the elements forming the data structure. However,
there are applications that deal with linear or tabular forms of data, devoid of any
superior-subordinate relationship. In such cases, employing these data structures
would be superfluous. Hash tables are one among such data structures which favor
efficient storage and retrieval of data elements that are linear in nature.

13.1.1. Dictionaries

Dictionary is a collection of data elements uniquely identified by a field called a


key. A dictionary supports the operations of search, insert and delete. The ADT of a
dictionary is defined as a set of elements with distinct keys supporting the
operations of search, insert, delete and create (which creates an empty dictionary).
While most dictionaries deal with distinct keyed elements, it is not uncommon to
find applications calling for dictionaries with duplicate or repeated keys. In this case,
it is essential that the dictionary evolves rules to resolve the ambiguity that may arise
while searching for or deleting data elements with duplicate keys.

A dictionary supports both sequential and random access. Sequential access is


one in which the data elements of the dictionary are ordered and accessed according
to the order of the keys (ascending or descending, for example). Random access is
one in which the data elements of the dictionary are accessed according to no
particular order.
2 A Textbook of Data Structures and Algorithms 3

Hash tables are ideal data structures for dictionaries. In this chapter, we
introduce the concept of hashing and hash functions. The structure and operations of
the hash tables are also discussed. The various methods of collision resolution, for
example, linear open addressing and chaining and their performance analyses are
detailed. Finally, the application of hash tables in the fields of compiler design,
relational database query processing and file organization are discussed.

13.2. Hash table structure

A hash function H(X) is a mathematical function which, when given a key X of


the dictionary D maps it to a position P in a storage table termed hash table. The
process of mapping the keys to their respective positions in the hash table is called
hashing. Figure 13.1 illustrates a hash function.

Figure 13.1. Hashing a key

When the data elements of the dictionary are to be stored in the hash table, each
key Xi is mapped to a position Pi in the hash table as determined by the value of
H(Xi), that is, Pi = H(Xi). To search for a key X in the hash table all that one does is
determine the position P by computing P = H(X) and accessing the appropriate data
element. In the case of insertion of a key X or its deletion, the position P in the hash
table where the data element needs to be inserted or from where it is to be deleted
respectively, is determined by computing P = H(X).

If the hash table is implemented using a sequential data structure, for example,
arrays, then the hash function H(X) may be so chosen to yield a value that
corresponds to the index of the array. In such a case the hash function is a mere
mapping of the keys to the array indices.
Hash Tables 3

EXAMPLE 13.1.–

Consider a set of distinct keys { AB12, VP99, RK32, CG45, KL78,


OW31, ST65, EX44 } to be represented as a hash table. Let us suppose the hash
function H is defined as below:

H(XYmn) = ord(X)

where X, Y are the alphabetical characters, m, n are the numerical characters of the
key, and ord(X) is the ordinal number of the alphabet X.

The computations of the positions of the keys in the hash table are shown below:

Position of the key


Key XYmn H(XYmn)
in the hash table
AB12 ord(A) 1
VP99 ord(V) 22
RK32 ord(R) 18
CG45 ord(C ) 3
KL78 ord(K) 11
OW31 ord(O) 15
ST65 ord(S) 19
EX44 ord(E) 5

The hash table accommodating the data elements appears as shown below:

1 AB12 ……….
2 ….
3 CG45
4 ….
5 EX44 ………
…. …..
11 KL78

15 OW31 ………….

18 RK32
19 ST65 ……………
….
22 VP99 ….
… … …
4 A Textbook of Data Structures and Algorithms 3

In Example 13.1, it was assumed that the hash function yields distinct values for
the individual keys. If this were to be followed as a criterion, then the situation may
turn out of control since, in the case of dictionaries with a very large set of data
elements, the hash table size can be too huge to be handled efficiently. Therefore, it
is convenient to choose hash functions that yield values lying within a limited range
so as to restrict the length of the table. This would consequently imply that the hash
functions may yield identical values for a set of keys. In other words, a set of keys
could be mapped to the same position in the hash table.

Let X1, X2, ….Xn be the n keys that are mapped to the same position P in the hash
table. Then, H(X1) = H(X2) = …H(Xn) = P. In such a case, X1, X2, ….Xn are called
synonyms. The act of two or more synonyms vying for the same position in the hash
table is known as a collision. Naturally, this entails a modification in the structure of
the hash table to accommodate the synonyms. The two important methods of linear
open addressing and chaining to handle synonyms are presented in sections 13.4 and
13.5, respectively.

13.3. Hash functions

The choice of the hash function plays a significant role in the structure and
performance of the hash table. It is therefore essential that a hash function satisfies
the following characteristics:
i) easy and quick to compute;
ii) even distribution of keys across the hash table. In other words, a hash function
must minimize collisions.

13.3.1. Building hash functions

The following are some of the methods of obtaining hash functions:


i) Folding: The key is first partitioned into two or three or more parts. Each of
the individual parts is combined using any of the basic arithmetic operations such as
addition or multiplication. The resultant number could be conveniently manipulated,
for example, truncated, to finally arrive at the index where the key is to be stored.
Folding assures a better spread of keys across the hash table.

Example: Consider a six-digit numerical key: 719532. We choose to partition


the key into three parts of two digits each, that is, 71 | 95 | 32, and merely add the
numerical equivalent of each of the parts, that is, 71 + 95 + 32 = 198. Truncating the
Hash Tables 5

result yields 98 which is chosen as the index of the hash table where the key 719532
is to be accommodated.

ii) Truncation: In this method, the selective digits of the key are extracted to
determine the index of the hash table where the key needs to be accommodated. In
the case of alphabetical keys, their numerical equivalents may be considered.
Truncation though quick to compute does not ensure even distribution of keys.

Example: Consider a group of six-digit numerical keys that need to be


accommodated in a hash table with 100 locations. We choose to select digits in
positions 3 and 6 to determine the index where the key is to be stored. Thus, key
719532 would be stored in location 92 of the hash table.

iii) Modular arithmetic: This is a popular method and the size of the hash table
L is involved in the computation of the hash function. The function makes use of
modulo arithmetic. Let k be the numerical key or the numerical equivalent if it is an
alphabetical key. The hash function is given by

H(k) = k mod L

The hash function evidently returns a value that lies between 0 and L-1.
Choosing L to be a prime number has a proven better performance by way of even
distribution of keys.

Example: Consider a group of six-digit numerical keys that need to be stored in


a hash table of size 111. For a key 145682, H(k) = 145682 mod 111 = 50. Hence, the
key is stored in location 50 of the hash table.

13.4. Linear open addressing

Let us suppose a group of keys is to be inserted into a hash table HT of size L,


making use of the modulo arithmetic function H(k) = k mod L. Since the range
of the hash table index is limited to lie between 0 and L-1, for a population of N
(N > L) keys, collisions are bound to occur. Hence, a provision needs to be made in
the hash table to accommodate the data elements that are synonyms.

We choose to adopt a sequential data structure to accommodate the hash table.


Let HT[0: L-1] be the hash table. Here, the L locations of the hash table are termed
buckets. Every bucket provides accommodation for the data elements. However, to
accommodate synonyms, that is, keys that map to the same bucket, it is essential that
a provision be made in the buckets. We, therefore, partition buckets into what are
6 A Textbook of Data Structures and Algorithms 3

called slots to accommodate synonyms. Thus, if bucket b has s slots, then s


synonyms can be accommodated in bucket b. In the case of an array implementation
of a hash table, the rows of the array indicate buckets and the columns the slots. In
such a case, the hash table is represented as HT[0:L-1, 0:s-1]. The number of slots in
a bucket needs to be decided based on the application. Figure 13.2 illustrates a
general hash table implemented using a sequential data structure.

Figure 13.2. Hash table implemented using a sequential data structure

EXAMPLE 13.2.–

Let us consider a set of keys {45, 98, 12, 55, 46, 89, 65, 88, 36, 21} to be
represented as a hash table as shown in Figure 13.2. Let us suppose the hash
function H is defined as H(X) = X mod 11. The hash table, therefore, has 11 buckets.
We propose three slots per bucket. Table 13.1 shows the hash function values of the
keys and Figure 13.3 shows the structure of the hash table.

Key X 45 98 12 55 46 89 65 88 36 21
H(X) 1 10 1 0 2 1 10 0 3 10

Table 13.1. Hash function values of the keys (Example 13.2)

Observe how keys {45, 12, 89}, {98, 65, 21} and {55, 88} are synonyms
mapping to the same bucket 1, 10 and 0 respectively.The provision of three slots per
bucket makes it possible to accommodate synonyms.
Hash Tables 7

Figure 13.3. Hash table (Example 13.2)

Now, what happens if a synonym is unable to find a slot in the bucket? In other
words, if the bucket is full, then where do we find a place for the synonyms? In such
a case an overflow is said to have occurred. All collisions need not result in
overflows. But in the case of a hash table with single slot buckets, collisions mean
overflows.

The bucket to which the key is mapped by the hash function is known as the
home bucket. To tackle overflows we move further down, beginning from the home
bucket and look for the closest slot that is empty and place the key in it. Such a
method of handling overflows is known as Linear probing or Linear open
addressing or closed hashing.

EXAMPLE 13.3.–

Let us proceed to insert the keys {77, 34, 43} in the hash table discussed in
Example 13.2. The hash function values of the keys are {0, 1, 10}. When we
proceed to insert 77 in its home bucket 0, we find a slot is available and hence the
insertion is done. In the case of 34, its home bucket 1 is full and hence there is an
overflow. By linear probing, we look for the closest slot that is vacant and find one
in the second slot of bucket 2. While inserting 43, we find bucket 10 to be full.
8 A Textbook of Data Structures and Algorithms 3

The search for the closest empty slot proceeds by moving downward in a circular
fashion until it finds a vacant place in slot 3 of bucket 2. Note the circular movement
of searching the hash table while looking for an empty slot. Figure 13.3 illustrates
the linear probing method undertaken for the listed keys. The keys which have been
accommodated in places other than their home buckets are shown over grey
background.

Figure 13.4. Linear open addressing (Example 13.3)

13.4.1. Operations on linear open addressed hash tables

Search: Searching for a key in a linear open addressed hash table proceeds on
lines similar to that of insertion. However, if the searched key is available in the
home bucket then the search is done. The time complexity in such a case is O(1).
However, if there had been overflows while inserting the key, then a sequential
search has to be called which searches through each slot of the buckets following the
home bucket until either (i) the key is found or (ii) an empty slot is encountered in
which case the search terminates or (iii) the search path has curled back to the home
bucket. In the case of (i), the search is said to be successful. In the cases of (ii) and
(iii), it is said to be unsuccessful.

EXAMPLE 13.4.–

Consider the snapshot of the hash table shown in Figure 13.5, which represents
keys whose first character lies between ‘A’ and ‘I’, both inclusive. The hash
Hash Tables 9

function used is H(X) = ord(C) mod 10 where C is the first character of the
alphabetical key X.

Figure 13.5. Illustration of search in a hash table

The search for keys F18 and G64 is straightforward since they are present in
their home buckets, which are 6 and 7, respectively. The search for keys A91 and
F78 for example, are slightly more involved, in the sense that though they are
available in their respective home buckets they are accessed only after a sequential
search for them is done in the slots corresponding to their buckets. On the other
hand, the search for I99 fails to find it in its home bucket, which is 9. This, therefore,
triggers a sequential search of every slot following the home bucket until the key is
found, in which case the search is successful or until an empty slot is encountered, in
which case the search is a failure. I99 is indeed found in slot 2 of bucket 2! Observe
how the search path curls back to the top of the hash table from the home bucket of
key I99. Let us now search for the key G93. The search proceeds to look into its
home bucket (7) before a sequential search for the same is undertaken in the slots
following the home bucket. The search stops due to its encountering an empty slot
and therefore the search is deemed unsuccessful.

Algorithm 13.1 illustrates the search algorithm for a linear open addressed hash
table.
10 A Textbook of Data Structures and Algorithms 3

Insert: the insertion of data elements in a linear open addressed hash table is
executed as explained in the previous section. The hash function, which is quite
often modulo arithmetic based, determines the bucket b and thereafter slot s in
which the data element is to be inserted. In the case of overflow, we search for the
closest empty slot beginning from the home bucket and accommodate the key in the
slot. Algorithm 13.1 could be modified to execute the insert operation. The line

if ( HT[i, j] = 0) then print (“ KEY not found”);

in the algorithm is replaced by

if ( HT[i, j] = 0) then HT[i, j] = X;

/* insert X in the empty slot*/

Delete: the delete operation on a hash table can be clumsy. When a key is
deleted it cannot be merely wiped off from its bucket (slot). A deletion leaves the
slot vacant and if an empty slot is chosen as a signal to terminate a search then many
of the elements following the empty slot and displaced from their home buckets may
go unnoticed. To tackle this it is essential that the keys following the empty slot be
moved up. This can make the whole operation clumsy.

An alternative could be to write a special element in the slot every time a delete
operation is done. This special element not only serves to camouflage the empty
space ‘available’ in the deleted slot when a search is in progress but also serves to
accommodate an insertion when an appropriate element assigned to the slot turns up.

However, it is generally recommended that deletions in a hash table be avoided


as much as possible due to their clumsy implementation.

13.4.2. Performance analysis

The complexity of the linear open addressed hash table is dependent on the
number of buckets. In the case of hash functions that follow modular arithmetic, the
number of buckets is given by the divisor L.

The best case time complexity of searching for a key in a hash table is given by
O(1) and the worst case time complexity is given by O(n), where n is the number of
data elements stored in the hash table. A worst case occurs when all the n data
elements map to the same bucket.
Hash Tables 11

The time complexities when compared to those of their linear list counterparts
are not in any way less. The best and worst case complexity of searching for an
element in a linear list of n elements is respectively, O(1) and O(n). However, on
average, the performance of the hash table is much more efficient than that of the
linear lists. It has been shown that the average case performance of a linear open
addressed hash table for an unsuccessful and successful search is given by

𝑈 ~ 1+
( )

𝑆 ~ 1+
( )

where 𝑈 and 𝑆 are the number of buckets examined on an average during an


unsuccessful and successful search respectively. The average is considered over all
possible sequences of the n keys X1, X2, ….Xn.. α is the loading factor of the hash
table and is given by α = where b is the number of buckets. The smaller the
loading factor better is the average case performance of the hash table in comparison
to that of linear lists.

13.4.3. Other collision resolution techniques with open addressing

The drawbacks of linear probing or linear open addressing could be overcome to


an extent by employing one or more of the following strategies:
i) Rehashing

A major drawback of linear probing is clustering or primary clustering wherein


the hash table gives rise to long sequences of records with gaps in between the
sequences. This leads to longer sequential searches especially when an empty slot
needs to be found. The problem could be resolved to an extent by resorting to what
is known as rehashing. In this, a second hash function is used to determine the slot
where the key is to be accommodated. If the slot is not empty, then another function
is called for, and so on.

Thus, rehashing makes use of at least two functions H, H’ where H(X), H’(X)
map keys X to any one of the b buckets. To insert a key, H(X) is computed and the
key X is accommodated in the bucket if it is empty. In the case of a collision, the
second hash function H’(X) is computed and the search sequence for the empty slot
proceeds by computing,

hi = ( H(X)+ i. H’(X)) mod b, i = 1, 2, ….


12 A Textbook of Data Structures and Algorithms 3

procedure LOP_HASH_SEARCH(HT, b, s X)
/* HT[0:b-1, 0:s-1] is the hash table implemented as a
two dimensional array. Here b is the number of buckets
and s is the number of slots. X is the key to be
searched in the hash table. In case of unsuccessful
search, the procedure prints the message “KEY not
found” otherwise prints “KEY found”*/

h = H(X); /* H(X) is the hash function computed


on X */
i = h; j =0; /* i, j are the indexes for the
bucket and slot respectively*/
while ( HT[i, j] ≠ 0 and HT[i, j] ≠ X ) do
j = j + 1; /* search for X in the slots*/
if (j > (s-1)) then j = 0;
/* reset slot index to 0 to continue
searching in the next bucket*/

if (j == 0) then { i = (i+1) mod b;


/* continue searching in the
next bucket in a circular manner*/
if (i == h) then {print ( “Key not found”);
exit();}
endwhile
if ( HT[i, j]== X) then print (“ KEY found”);
if ( HT[i, j] = 0) then print (“ KEY not found”);

end LOP_HASH_SEARCH

Algorithm 13.1. Procedure to search for a


key X in a linear open addressed hash table

Here, h1, h2, ... is the search sequence before an empty slot is found to
accommodate the key. It needs to be ensured that H’(X) does not evaluate to 0, since
there is no way this would be of help. A good choice for H’(X) is given by
M – (X mod M) where M is chosen to be a prime smaller than the hash table size
(see illustrative problem 13.6)

ii) Quadratic probing

This is another method that can substantially reduce clustering. In this method
when a collision occurs at address h, unlike linear probing which probes buckets in
Hash Tables 13

locations h+1, h+2 …. and so forth, the technique probes buckets at locations h+1,
h+4, h+9,…. and so forth. In other words, the method probes buckets at locations
(ℎ + 𝑖 ) 𝑚𝑜𝑑 𝑏, 𝑖 = 1, 2, . . . ., where h is the home bucket and b is the number
of buckets. However, there is no guarantee that the method gives a fair chance to
probe all locations in the hash table. Though quadratic probing reduces primary
clustering, it may result in probing the same set of alternate cells. Such a case known
as secondary clustering occurs especially when the hash table size is not prime.

If b is a prime number then quadratic probing probes exactly half the number of
locations in the hash table. In this case, the method is guaranteed to find an empty
slot if the hash table is at least half empty (see illustrative problems 13.4 and 13.5).

iii) Random probing

Unlike quadratic probing where the increment during probing was definite,
random probing makes use of a random number generator to obtain the increment
and hence the next bucket to be probed. However, it is essential that the random
number generator function generates the same sequence. Though this method
reduces clustering, it can be a little slow when compared to others.

13.5. Chaining

In the case of linear open addressing, the solution of accommodating synonyms


in the closest empty slot may contribute to a deterioration in performance. For
example, the search for a synonym key may involve sequentially going through
every slot occurring after its home bucket before it is either found or unfound. Also,
the implementation of the hash table using a sequential data structure such as arrays
limits its capacity (b × s slots). While increasing the number of slots to minimize
overflows may lead to wastage of memory, containing the number of slots to the
bare minimum may lead to severe overflows hampering the performance of the hash
table.

An alternative to overcome this malady is to keep all synonyms that are mapped
to the same bucket chained to it. In other words, every bucket is maintained as a
singly linked list with synonyms represented as nodes. The buckets continue to be
represented as a sequential data structure as before, to favor the hash function
computation. Such a method of handling overflows is called chaining or open
hashing or separate chaining. Figure 13.6 illustrates a chained hash table.
14 A Textbook of Data Structures and Algorithms 3

Figure 13.6. A chained hash table

In the figure, observe how the buckets have been represented sequentially and
each of the buckets is linked to a chain of nodes which are synonyms mapping to the
same bucket.

Chained hash tables only acknowledge collisions. There are no overflows per se
since any number of collisions can be handled provided there is enough memory to
handle them!

EXAMPLE 13.5.–

Let us consider the set of keys {45, 98, 12, 55, 46, 89, 65, 88, 36, 21} listed in
Example 13.2, to be represented as a chained hash table. The hash function H used is
H(X) = X mod 11. The hash function values for the keys are shown in Table 13.1.
The structure of the chained hash table is shown in Figure 13.7.

Observe how each of the groups of synonyms for example, {45, 12, 89}, {98, 65,
21} and {55, 88} are represented as singly linked lists corresponding to the buckets
1, 10 and 0 respectively. In accordance with the norms pertaining to singly linked
lists, the link field of the last synonym in each chain is a null pointer. Those buckets
which are yet to accommodate keys are also marked null.
Hash Tables 15

Figure 13.7. Hash table (Example 13.5)

13.5.1. Operations on chained hash tables

Search: The search for a key X in a chained hash table proceeds by computing
the hash function value H(X). The bucket corresponding to the value H(X) is
accessed and a sequential search along the chain of nodes is undertaken. If the key is
found then the search is termed successful otherwise unsuccessful. If the chain is too
long maintaining the chain in order (ascending or descending) helps in rendering the
search efficient.

Algorithm 13.2 illustrates the procedure to undertake a search in a chained


hash table.
16 A Textbook of Data Structures and Algorithms 3

Insert: To insert a key X into a hash table, we compute the hash function H(X) to
determine the bucket. If the key is the first node to be linked to the bucket then all
that it calls for is a mere execution of a function to insert a node in an empty singly
linked list. In the case of keys that are synonyms, the new key could be inserted
either at the beginning or the end of the chain leaving the list unordered. However, it
would be prudent and less expensive too, to maintain each of the chains in the
ascending or descending order of the keys. This would also render the search for a
specific key among its synonyms to be efficiently carried out.

EXAMPLE 13.6.–

Let us insert keys {76, 48} into the chained hash table shown in Figure 13.7.
Since 76 already has three synonyms in its chain corresponding to bucket 10, we
choose to insert it in order in the list. On the other hand, 48 is the first key in its
bucket, which is 4. Figure 13.8 illustrates the insertion.

Figure 13.8. Inserting keys into a chained hash table


Hash Tables 17

procedure CHAIN_HASH_SEARCH(HT, b, X)
/* HT[0:b-1] is the hash table implemented as a one
dimensional array of pointers to buckets. Here b is the number
of buckets. X is the key to be searched in the hash table. In
case of unsuccessful search, the procedure prints the message
“KEY not found” otherwise prints “KEY found”*/

h = H(X); /* H(X) is the hash function computed on X */

TEMP = HT[h]; /* TEMP is the pointer to the first


node in the chain*/
while (DATA(TEMP) ≠ X and TEMP ≠ NIL ) do
/* search for the key down the chain*/
TEMP = LINK(TEMP);
endwhile

if ( DATA(TEMP)== X) then print (“ KEY found”);


if ( TEMP == NIL) then print (“ KEY not found”);
end CHAIN_HASH_SEARCH.

Algorithm 13.2. Procedure to search


for a key X in a chained hash table

Algorithm 13.2 could be modified to insert a key. It merely calls for the insertion
of a node in a singly linked list that is unordered or ordered.

Delete: Unlike that of linear open addressed hash tables, the deletion of a key X
in a chained hash table is elegantly done. All that it calls for is a search for X in the
corresponding chain and deletion of the respective node.

13.5.2. Performance analysis

The complexity of the chained hash table is dependent on the length of the chain
of nodes corresponding to the buckets. The best case complexity of a search is O(1).
A worst case occurs when all the n elements map to the same bucket and the length
of the chain corresponding to that bucket is n, with the searched key turning out
to be the last in the chain. The worst-case complexity of the search in such a case
is O(n).

On an average, the complexity of the search operation on a chained hash table is


given by
18 A Textbook of Data Structures and Algorithms 3

( )
𝑈 ~ ,𝛼 ≥ 1

𝑆 ~1 +

where 𝑈 and 𝑆 are the number of nodes examined on an average during an


unsuccessful and successful search respectively. α is the loading factor of the hash
table and is given by α = where b is the number of buckets.

The average case performance of the chained hash table is superior to that of the
linear open addressed hash table.

13.6. Applications

In this section, we discuss the application of hash tables in the fields of compiler
design, relational database query processing and file organization.

13.6.1. Representation of a keyword table in a compiler

In section 10.4.1, (Volume 2) the application of binary search trees and AVL
trees for the representation of symbol tables in a compiler was discussed. Hash
tables find applications in the same problem as well.

A keyword table which is a static symbol table is best represented by means of a


hash table. Each time a compiler checks out a string to be a keyword or a user-id, the
string is searched against the keyword table. An appropriate hash function could be
designed to minimize collisions among the keywords and yield the bucket where the
keyword could be found. A successful search indicates that the string encountered is
a keyword and an unsuccessful search indicates that it is a user-id. Considering the
significant fact that but for retrievals, no insertions or deletions are permissible on a
keyword table, hash tables turn out to be one of the best propositions for the
representation of symbol tables.

EXAMPLE 13.7.–

Consider a subset of a keyword set commonly used in programming languages,


for example, { while, repeat, and, or, not, if, else, begin,
end, function, procedure, int, float, Boolean}. For simplicity,
we make use of the hash function H(X) = ord(C) -1 where C is the first character of
the keyword X. Figure 13.9 illustrates a linear open addressed hash table with two
slots per bucket (HT[0..25, 0..1]) and a chained hash table representation for
Hash Tables 19

the keyword set. Considering the efficient retrievals promoted by the hash table, the
choice of the data structure for the symbol table representation contributes to the
efficient performance of a compiler as well.

Figure 13.9. Hash table representations for a keyword set

13.6.2. Hash tables in the evaluation of a join operation on relational


databases

Relational databases support a selective set of operations, for example,


selection, projection, join (natural join, equi-join) and so on, which aid query
processing. Of these, the natural join operation is most commonly used in relational
database management systems. As indicated by the notation ⋈, the operation
works on two relations (databases) to combine them into a single relation. Given
two relations R and S, a natural join operation of the two databases is indicated as
20 A Textbook of Data Structures and Algorithms 3

R ⋈ S. The resulting relation is a combination of the two relations based on


attributes common to the two relations.

EXAMPLE 13.8.–

Consider the two relations ITEM_DESCRIPTION and VENDOR shown in


Figure 13.10(a). The ITEM_DESCRIPTION relation describes the items and the
VENDOR relation contains details about the vendors supplying the items. The
relation ITEM_DESCRIPTION contains the attributes ITEM_CODE and
ITEM_NAME. The VENDOR relation contains the attributes ITEM_CODE,
VENDOR _NAME and ADDRESS (city).

A query pertaining to who the vendors are for a given item code calls for joining
the two relations. The join of the two relations yields the relation shown in Figure
13.10(b). Observe how the natural join operation combines the two relations on the
basis of their common attribute ITEM_CODE. Those tuples (rows) of the two
relations having a common attribute value in the ITEM_CODE field are “joined”
together to form the output relation.

Figure 13.10. Natural join of two relations

One method of evaluating a join is to use the hash method. Let H(X) be the hash
function where X is the attribute value of the relations. Here H(X) is the address of
the bucket which contains the attribute value and a pointer to the appropriate tuple
corresponding to the attribute value. The pointer to the tuple is known as Tuple
Identifier (TID). TIDs in general, besides containing the physical address of the
Hash Tables 21

tuple of the relation also hold identifiers unique to the relation. The hash tables are
referred to as hash indexes in relational database terminology.

A natural join of the two relations R and S over a common attribute ATTRIB,
results in each bucket of the hash indexes recording the attribute values of ATTRIB
along with the TIDs of the tuples in relations R and S whose R.ATTRIB =
S.ATTRIB.

When a query associated with the natural join is to be answered all that it calls
for is to access the hash indexes to retrieve the appropriate TIDs associated with the
query. Retrieving the tuples using the TIDs satisfies the query.

EXAMPLE 13.9.–

Figure 13.11(a) shows a physical view of the two relations


ITEM_DESCRIPTION and VENDOR. Figure 13.11(b) shows the hash function
values based on which the hash table (Figure 13.11(c)) has been constructed. The
hash function used is not discussed. Each bucket of the hash index records the TIDs
of the attribute values mapped to the bucket. Thus, TIDs corresponding to
ITEM_CODE = P402 of both the relations, are mapped to bucket 16 and so on.

Assume that a query “List the vendor(s) supplying the item P402” is to be
processed. To process this request, we first compute H(“P402”) which as shown in
Figure 13.11(b) yields the bucket address 16. Accessing bucket 16 we find the TID
corresponding to the relation VENDOR is 7001. To answer the query all that needs
to be done is to retrieve the tuple whose TID is 7001.

A general query such as “List the vendors supplying each of the items” may call
for sequentially searching each of the hash indexes corresponding to each attribute
value of ITEM_CODE.
22 A Textbook of Data Structures and Algorithms 3

Figure 13.11. Evaluation of natural join operation using hash indexes

13.6.3. Hash tables in a direct file organization

File organization deals with methods and techniques to structure data in


external or auxiliary storage devices such as tapes, disks, drums and so forth. A file
is a collection of related data termed records. Each record is uniquely identified by
what is known as a key, which is a datum or a portion of data in the record. The
major concern in all these methods is regarding the access time when records
pertaining to the keys (primary or secondary) are to be retrieved from the storage
devices to be updated or inserted or deleted. Some of the commonly used file
organization schemes are sequential file organization, serial file organization,
indexed sequential access file organization and direct file organization. Chapter 14
elaborately details files and their methods of organization.

The direct file organization (see section 14.8) which is a kind of file organization
method, employs hash tables for the efficient storage and retrieval of records from
the storage devices. Given a file of records, { f1, f2, f3,……fN} with keys { k1, k2,
k3,…kN} a hash function H(k) where k is the record key, determines the storage
address of each of the records in the storage device. Thus, direct files undertake
direct mapping of the keys to the storage locations of the records with the records of
the file organized as a hash table.
Hash Tables 23

Summary

– Hash tables are ideal data structures for dictionaries. They favor efficient storage and
retrieval of data lists which are linear in nature.

– A hash function is a mathematical function which maps keys to positions in the hash
tables known as buckets. The process of mapping is called hashing. Keys which map to
the same bucket are called as synonyms. In such a case a collision is said to have occurred.
A bucket may be divided into slots to accommodate synonyms. When a bucket is full and
a synonym is unable to find space in the bucket then an overflow is said to have occurred.

– The characteristics of a hash function are that it must be easy to compute and at the
same time minimize collisions. Folding, truncation and modular arithmetic are some of the
commonly used hash functions.

– A hash table could be implemented using a sequential data structure such as arrays.
In such a case, the method of handling overflows where the closest slot that is vacant is
utilized to accommodate the synonym key is called linear open addressing or linear
probing. However, over the course of time, linear probing can lead to the problem of
clustering thereby deteriorating the performance of the hash table to a mere sequential
search!

– The other alternative methods of handling overflows are rehashing, quadratic probing
and random probing.

– A linked implementation of a hash table is known as chaining. In this all the


synonyms are chained to their respective buckets as a singly linked list. On an average, a
chained hash table is superior in performance when compared to that of a linear probed
hash table.

– Hash tables have found applications in the design of symbol tables in compiler
design, query processing in relational database management systems and direct file
organizations.

13.7. Illustrative problems

PROBLEM 13.1.–

Insert the following data into a hash table implemented using linear open
addressing. Assume that the buckets have three slots each. Make use of the hash
function h(X) = X mod 9.

{17, 09, 34, 56, 11, 71, 86, 55, 22, 10, 4, 39, 49, 52, 82, 13, 40, 31, 35, 28, 44}
24 A Textbook of Data Structures and Algorithms 3

Solution:

The linear open addressed hash table is shown in Figure P13.1 Those keys not
accommodated in their home buckets are shown over a grey shaded background.

Figure P13.1. The hash table implemented using linear


open addressing, for the data listed in illustrative problem 13.1

PROBLEM 13.2.–

For the set of keys listed in illustrative problem 13.1, trace a chained hash table
making use of the same hash function.

Solution:

The chained hash table is shown in Figure P13.2. The nodes in the chain are
inserted in ascending order of the key values.

Figure P13.2. The hash table implemented using


chaining for the data listed in illustrative problem 13.1
Hash Tables 25

PROBLEM 13.3.–

Comment on the statement: “To minimize collisions in a linear open addressed


hash table it is recommended that the ratio of the number of buckets in a hash table
to the number of keys to be stored in the hash table is made bigger”.

Solution:

No, this is illogical since increasing the number of buckets will only lead to the
wastage of space.

PROBLEM 13.4.–

For the set of keys {17, 9, 34, 56, 11, 4, 71, 86, 55, 10, 39, 49, 52, 82, 31, 13, 22,
35, 44, 20, 60, 28} obtain a hash table HT[0..8, 0..2] following quadratic probing.
Make use of the hash function H(X) = X mod 9. What are your observations?

Solution:

Quadratic probing employs the function (ℎ + 𝑖 ) 𝑚𝑜𝑑 𝑛 𝑖 = 1, 2, . .., where


n = 9, to determine the empty slot during collisions. Here h is the address of the
home bucket given by the hash function H(X), where X is the key. The quadratic
probed hash table is shown in Figure P13.4.

Figure P13.4. The hash table following quadratic probing and using the hash
function h(X) = X mod 9, for the keys listed in illustrative problem 13.4

Note how during the insertion of keys 13 and 22, their home buckets, which is 4 is
full. To handle this collision, quadratic probing begins searching buckets 4+1 mod 9,
Other documents randomly have
different content
church; yet when they became Christians, and of their own
accord underwent the laws of the gospel, presently as sheep to
a shepherd, and as members to the head, they became subject
to the prelate of the ecclesiastical hierarchy. Which, whether it
be true or not, is to be considered by that light which we have
from the Holy Scripture, concerning the power of our Saviour
and his apostles, over such as they had converted. But our
Saviour, as he imitated the commonwealth of the Jews in his
magistrates, the twelve and the seventy; so did he also in the
censure of the church, which was excommunication; but
amongst the Jews, the church did put the excommunicated
persons from the congregation, which they might do by their
power temporal; but our Saviour and his apostles, who took
upon them no such power, could not forbid the excommunicated
person to enter into any place and congregation, into which he
was permitted to enter, by the prince, or sovereign of the place.
For that had been to deprive the sovereign of his authority. And
therefore the excommunication of a person subject to an earthly
power, was but a declaration of the church, which did
excommunicate, that the person so excommunicated was to be
reputed still as an infidel, but not to be driven by their authority,
out of any company, he might otherwise lawfully come into. And
this is it our Saviour saith (Matth. xviii. 17): If he refuse to hear
the church, let him be unto thee as an heathen man and a
publican. So that the whole effect of excommunicating a
Christian prince, is no more than he or they that so
excommunicate him depart and banish themselves out of his
dominion. Nor can they thereupon discharge any of his subjects
of their obedience to him; for that were to deprive him of his
dominion; which they may not do, for being out of the church. It
is confessed by them that make this objection, and proved in the
former section, that our Saviour gave no authority to his apostles
to be judges over them. And therefore in no case can the
sovereign power of a commonwealth be subject to any authority
ecclesiastical, besides that of Christ himself. And though he be
informed concerning the kingdom of heaven, and subject himself
thereto at the persuasions of persons ecclesiastical, yet is he not
thereby subject to their government and rule. For if it were by
their authority he took that yoke upon him, and not by their
persuasion, then by the same authority he might cast it off. But
this is unlawful. For if all the churches in the world should
renounce the Christian faith, yet is not this sufficient authority
for any of the members to do the same. It is manifest therefore,
that they who have sovereign power, are immediate rulers of the
church under Christ, and all other but subordinate to them. If
that were not, but kings should command one thing upon pain of
death, and priests another, upon pain of damnation, it would be
impossible that peace and religion should stand together.
That no man 11. And therefore there is no just cause for
hath any just any man to withdraw his obedience from the
pretence of sovereign state, upon pretence that Christ hath
religion
against ordained any state ecclesiastical above it. And
obedience to though kings take not upon them the ministerial
commonweal priesthood, yet are they not so merely laic, as
th. God not to have sacerdotal jurisdiction. To conclude
speaketh to this chapter, since God speaketh not in these
man by his
vicegerents. days to any man by his private interpretation of
the Scriptures, nor by the interpretation of any
power above, or not depending on the sovereign power of every
commonwealth, it remaineth, that he speaketh by his vice-gods,
or lieutenants here on earth, that is to say, by sovereign kings,
or such as have sovereign authority as well as they.

CHAPTER VIII.

1. The things that dispose to rebellion, discontent, pretence, and hope of


success. 2. Discontent that disposeth to sedition, consisteth partly in fear
of want, or punishment: 3. Partly in ambition. 4. Six heads of pretences
to rebellion. 5. The first of them, that men ought to do nothing against
conscience, confuted. 6. The second, that sovereigns are subject to their
own laws, confuted. 7. The third, that the sovereignty is divisible,
confuted. 8. The fourth, that subjects have a propriety distinct from the
dominion of the sovereign, confuted. 9. The fifth, that the people is a
person distinct from the sovereign, confuted. 10. The sixth, that
tyrannicide is lawful, confuted. 11. Four heads of hope of success in
rebellion. 12. Two things necessary to an author of rebellion, much
eloquence, and little wisdom. 13. That the authors of rebellion
necessarily are to be men of little wisdom. 14. That the same are
necessarily eloquent. 15. In what manner they concur to their common
effects.

The things 1. Hitherto of the causes why, and the


that dispose manner how, men have made commonwealth.
to rebellion, In this chapter I shall show briefly by what
discontent,
pretence, and causes, and in what manner, they be again
hope of destroyed; not meaning to say anything
success. concerning the dissolution of a commonwealth,
from foreign invasions, which is as it were the
violent death thereof. I shall speak only of sedition, which is also
the death of the commonwealth, but like to that which
happeneth to a man from sickness and distemper. To dispose
men to sedition, three things concur. The first is discontent; for
as long as a man thinketh himself well, and that the present
government standeth not in his way to hinder his proceeding
from well to better, it is impossible for him to desire the change
thereof. The second is pretence of right; for though a man be
discontent, yet if in his own opinion there be no just cause of
stirring against, or resisting the government established, nor any
pretence to justify his resistance, and to procure aid, he will
never show it. The third is hope of success; for it were madness
to attempt without hope, when to fail, is to die the death of a
traitor. Without these three, discontent, pretence, and hope,
there can be no rebellion: and when the same are all together,
there wanteth nothing thereto, but a man of credit to set up the
standard, and to blow the trumpet.
Discontent 2. And as for discontent, it is of two sorts: for
that it consisteth either in bodily pain present or
disposeth to expected, or else in trouble of the mind; which
sedition, is the general division of pleasure and pain,
consisteth
partly in fear
Human Nature, chap. VII. sect. 8. The presence
of want, or of bodily pain disposeth not to sedition; the fear
punishment: of it doth. As for example; when a great
multitude, or heap of people, have concurred to
a crime worthy of death, they join together, and take arms to
defend themselves for fear thereof. So also the fear of want, or
in present want, the fear of arrests and imprisonment dispose to
sedition. And therefore great exactions, though the right thereof
be acknowledged, have caused great seditions. As in the time of
Henry VII. the seditions of the Cornish men, that refused to pay
a subsidy, and, under the conduct of the Lord Audley, gave the
King battle upon Blackheath; and that of the northern people,
who in the same king’s time, for demanding a subsidy granted in
parliament, murdered the Earl of Northumberland in his house.
Partly in 3. Thirdly, the other sort of discontent, which
ambition. troubleth the mind of them who otherwise live
at ease, without fear of want, or danger of violence, ariseth only
from a sense of their want of that power, and that honour and
testimony thereof, which they think is due unto them. For all joy
and grief of mind consisting (as hath been said, Human Nature,
chap. IX. sect. 21) in a contention for precedence to them with
whom they compare themselves; such men must needs take it
ill, and be grieved with the state, as find themselves postposed
to those in honour, whom they think they excel in virtue and
ability to govern. And this is it for which they think themselves
regarded but as slaves. Now seeing freedom cannot stand
together with subjection, liberty in a commonwealth is nothing
but government and rule, which because it cannot be divided,
men must expect in common; and that can be no where but in
the popular state, or democracy. And Aristotle saith well, (lib. VI.
cap. 2 of his Politics), The ground or intention of a democracy, is
liberty. Which he confirmeth in these words: For men ordinarily
say this, that no man can partake of liberty, but only in a popular
commonwealth. Whosoever therefore in a monarchical estate,
where the sovereign power is absolutely in one man, claimeth
liberty, claimeth (if the hardest construction should be made
thereof) either to have the sovereignty in his turn, or to be
colleague with him that hath it, or to have the monarchy
changed into a democracy. But if the same be construed, with
pardon of that unskilful expression, according to the intention of
him that claimeth, then doth he thereby claim no more but this,
that the sovereign should take notice of his ability and deserving,
and put him into employment and place of subordinate
government, rather than others that deserve less. And as one
claimeth, so doth another, every man esteeming his own desert
greatest. Amongst all those that pretend to, or are ambitious of
such honour, a few only can be served, unless it be in a
democracy; the rest therefore must be discontent. And so much
of the first thing that disposeth to rebellion, namely, discontent,
consisting in fear and ambition.
Six heads of 4. The second thing that disposeth to
pretences to rebellion, is pretence of right. And that is when
rebellion. men have an opinion, or pretend to have an
opinion, that in certain cases they may lawfully resist him or
them that have the sovereign power, or deprive him or them of
the means to execute the same. Of which pretences, there be six
special cases. One is, when the command is against their
conscience, and they believe it is unlawful for a subject at the
command of the sovereign power to do any action, which he
thinketh in his own conscience not lawful for him to do, or to
omit any action, which he thinketh not lawful for him to omit.
Another is, when the command is against the laws, and they
think the sovereign power in such sort obliged to his own laws,
as the subject is; and that when he performeth not his duty, they
may resist his power. A third is, when they receive commands
from some man or men, and a supersedeas to the same from
others, and think the authority is equal, as if the sovereign
power were divided. A fourth is, when they are commanded to
contribute their persons or money to the public service, and
think they have a propriety in the same distinct from the
dominion of the sovereign power; and that therefore they are
not bound to contribute their goods and persons, no more than
every man shall of himself think fit. A fifth, when the commands
seem hurtful to the people; and they think, every one of them,
that the opinion and sense of the people, is the same with the
opinion of himself, and those that consent with him; calling by
the name of people, any multitude of his own faction. The sixth
is, when the commands are grievous; and they account him that
commandeth grievous things a tyrant; and tyrannicide, that is,
the killing of a tyrant, not only lawful, but also laudable.
The first of 5. All these opinions are maintained in the
them, that books of the dogmatics, and divers of them
men ought to taught in public chairs, and nevertheless are
do nothing
against most incompatible with peace and government,
conscience, and contradictory to the necessary and
confuted. demonstrable rules of the same. And for the
first, namely, that a man may lawfully do or omit
any thing against his conscience, and from whence arise all
seditions concerning religion and ecclesiastical government, it
hath been plainly declared in the two last chapters, that such
opinion is erroneous. For those two chapters have been wholly
spent, to prove, that Christian religion not only forbiddeth not,
but also commandeth, that in every commonwealth, every
subject should in all things to the uttermost of his power obey
the commands of him or them that is the sovereign thereof, and
that a man in so obeying, doth according to his conscience and
judgment, as having deposited his judgment in all controversies
in the hands of the sovereign power; and that this error
proceedeth from the ignorance of what and by whom God
Almighty speaketh.
The second, 6. As for the second opinion, which is this,
that that the sovereign is in such sort obliged to his
sovereigns own laws, as the subject is; the contrary thereof
are subject to
their own hath been showed, Part II. chapter I. sections 7-
12, by which it appeareth, that the sovereign
power is not to be resisted; that it carrieth the
laws, sword both of war and justice; that it hath the
confuted. right of deciding all controversies, both judicial
and deliberative; that it hath the making of all the laws civil; that
it appointeth magistrates and public ministers, and that it
implieth an universal impunity. How then can he or they be said
to be subject to the laws which they may abrogate at their
pleasure, or break without fear of punishment? And this error
seemeth to proceed from this, that men ordinarily understand
not aright, what is meant by this word law, confounding law and
covenant, as if they signify the same thing. But law implieth a
command; covenant is but a promise. And not every command is
a law, but only (Human Nature, chap. XIII. sect. 6) when the
command is the reason we have of doing the action
commanded. And then only is the reason of our actions in the
command, when the omitting is therefore hurtful, because the
action was commanded, not because it was hurtful of itself; and
doing contrary to a command, were not at all hurtful, if there
were not a right in him that commandeth to punish him that so
doth. He or they that have all punishments in their own
disposing, cannot be so commanded, as to receive hurt for
disobeying, and consequently no command can be a law unto
them. It is an error therefore to think, that the power which is
virtually the whole power of the commonwealth, and which in
whomsoever it resideth, is usually called supreme or sovereign,
can be subject to any law but that of God Almighty.
The third, 7. The third opinion, that the sovereign power
that the may be divided, is no less an error than the
sovereignty former, as hath been proved, Part II. chapter I.
is divisible,
confuted. sect. 15. And if there were a commonwealth,
wherein the rights of sovereignty were divided,
we must confess with Bodin, Lib. II. chap. I. De Republica, that
they are not rightly to be called commonwealths, but the
corruption of commonwealths. For if one part should have power
to make the laws for all, they would by their laws at their
pleasure, forbid others, to make peace or war, to levy taxes, or
to yield fealty and homage without their leave; and they that had
the right to make peace and war, and command the militia,
would forbid the making of other laws, than what themselves
liked. And though monarchies stand long, wherein the right of
sovereignty hath seemed so divided, because monarchy of itself
is a durable kind of government, yet monarchs have been
thereby divers times thrust out of their possession. But the truth
is, that the right of sovereignty is such, as he or they that have
it, cannot, though they would, give away any part thereof, and
retain the rest. As for example; if we should suppose the people
of Rome to have had the absolute sovereignty of the Roman
state, and to have chosen them a council by the name of the
senate, and that to this senate they had given the supreme
power of making laws, reserving nevertheless to themselves, in
direct and express terms, the whole right and title of the
sovereignty; which may easily happen amongst them that see
not the inseparable connexion between the sovereign power, and
the power of making laws: I say, this grant of the people to the
senate is of no effect, and the power of making laws is in the
people still. For the senate understanding it to be the will and
intention of the people, to retain the sovereignty, ought not to
take that for granted, which was contradictory thereto, and
passed by error. For (Human Nature, chap. XIII. sect. 9) in
contradictory promises, that which is directly promised, is
preferred before that which is opposite thereunto by
consequence; because the consequence of a thing is not always
obscured, as is the thing itself. The error concerning mixed
government hath proceeded from want of understanding of what
is meant by this word body politic, and how it signifieth not the
concord, but the union of many men. And though in the chapters
of subordinate corporations, a corporation being declared to be
one person in law, yet the same hath not been taken notice of in
the body of a commonwealth or city, nor have any of those
innumerable writers of politics, observed any such union.
The fourth, 8. The fourth opinion, to wit, that subjects
that subjects have their meum, tuum, and suum, in property,
have a not only by virtue of the sovereign power over
propriety them all, distinct from one another, but also
distinct from against the sovereign himself, by which they
the dominion
of the
would pretend to contribute nothing to the
sovereign, public, but what they please, hath been already
confuted. confuted, by proving the absoluteness of the
sovereignty, and more particularly, Part II.
chapter V. sect. 2; and ariseth from this, that they understand
not ordinarily that before the institution of sovereign power,
meum and tuum, implied no propriety, but a community, where
every man had right to every thing, and was in state of war with
every man.
The fifth, that 9. The fifth opinion, That the people is a
the people is distinct body from him or them that have the
a person sovereignty over them, is an error already
distinct from
the confuted, Part II. chap. II. sect 11, where it is
sovereign, showed, that when men say, the people
confuted. rebelleth, it is to be understood of those
particular persons only, and not of the whole
nation. And when the people claimeth any thing otherwise than
by the voice of the sovereign power, it is not the claim of the
people, but only of those particular men, that claim in their own
persons; and this error ariseth from the equivocation of the word
people.
The sixth, 10. Lastly, for the opinion, that tyrannicide is
that lawful, meaning by a tyrant any man in whom
tyrannicide is resideth the right of sovereignty, is no less false
lawful,
confuted. and pernicious to human society, than frequent
in the writings of those moral philosophers,
Seneca and others, so greatly esteemed amongst us. For when a
man hath the right of sovereignty, he cannot justly be punished,
as hath been often showed already, and therefore much less
deposed, or put to death. And howsoever he might deserve
punishment, yet punishment is unjust without judgment
preceding, and judgment unjust without power of judicature,
which a subject hath not over a sovereign. But this doctrine
proceedeth from the Schools of Greece, and from those that writ
in the Roman state, in which not only the name of a tyrant, but
of a king, was hateful.
Four heads of 11. Besides discontent, to the disposing of a
hope of man to rebellion, and pretence, there is
success in required, in the third place, hope of success,
rebellion.
which consisteth in four points: I. That the
discontented have mutual intelligence; II. That they have
sufficient number; III. That they have arms; IV. That they agree
upon a head. For these four must concur to the making of one
body of rebellion, in which intelligence is the life, number the
limbs, arms the strength, and a head the unity, by which they
are directed to one and the same action.
Two things 12. The authors of rebellion, that is, the men
necessary to that breed these dispositions to rebel in others,
an author of of necessity must have in them these three
rebellion,
much qualities: I. To be discontented themselves; II. To
eloquence, be men of mean judgment and capacity; and,
and little III. To be eloquent men, or good orators. And as
wisdom. for their discontent, from whence it may
proceed, hath been already declared. And for the second and
third, I am to show now, first, how they may stand together; for
it seemeth a contradiction, to place small judgment and great
eloquence, or, as they call it, powerful speaking, in the same
man: and then in what manner they concur, to dispose other
men to sedition.
That the 13. It was noted by Sallust, that in Catiline,
authors of who was author of the greatest sedition that
rebellion ever was in Rome, there was Eloquentiæ satis,
necessarily,
are to be men sapientiæ parum; eloquence sufficient, but little
of little wisdom. And perhaps this was said of Catiline,
wisdom. as he was Catiline: but it was true of him as an
author of sedition. For the conjunction of these
two qualities made him not Catiline, but seditious. And that it
may be understood, how want of wisdom, and store of
eloquence, may stand together, we are to consider, what it is we
call wisdom, and what eloquence. And therefore I shall here
again remember some things, that have been said already,
Human Nature, chap. V. VI. It is manifest that wisdom consisteth
in knowledge. Now of knowledge there are two kinds; whereof
the one is the remembrance of such things, as we have
conceived by our senses, and of the order in which they follow
one another. And this knowledge is called experience; and the
wisdom that proceedeth from it, is that ability to conjecture by
the present, of what is past, and to come, which men call
prudence. This being so, it is manifest presently, that the author
of sedition, whosoever he be, must not be prudent. For if he
consider and take his experiences aright, concerning the success
which they have had, who have been the movers and authors of
sedition, either in this or any other state, he shall find, that for
one man that hath thereby advanced himself to honour, twenty
have come to a reproachful end. The other kind of knowledge, is
the remembrance of the names or appellations of things, and
how every thing is called, which is, in matters of common
conversation, a remembrance of pacts and covenants of men
made amongst themselves, concerning how to be understood of
one another. And this kind of knowledge is generally called
science, and the conclusions thereof truth. But when men
remember not how things are named, by general agreement, but
either mistake and misname things, or name them aright by
chance, they are not said to have science, but opinion, and the
conclusions thence proceeding, are uncertain, and for the most
part erroneous. Now that science in particular, from which
proceed the true and evident conclusions of what is right and
wrong, and what is good and hurtful to the being, and well-
being of mankind, the Latins call sapientia, and we by the
general name of wisdom. For generally, not he that hath skill in
geometry, or any other science speculative, but only he that
understandeth what conduceth to the good and government of
the people, is called a wise man. Now that no author of sedition
can be wise in this acceptation of the word, is sufficiently
proved, in that it hath been already demonstrated, that no
pretence of sedition can be right or just. And therefore the
authors of sedition must be ignorant of the right of state, that is
to say, unwise. It remaineth therefore, that they be such, as
name things, not according to their true and generally agreed
upon names, but call right and wrong, good and bad, according
to their passions, or according to the authorities of such as they
admire, as Aristotle, Cicero, Seneca, and others of like authority,
who have given the names of right and wrong, as their passions
have dictated; or have followed the authority of other men, as
we do theirs. It is required therefore in an author of sedition,
that he think right, that which is wrong; and profitable, that
which is pernicious; and consequently that there be in him
sapientiæ parum, little wisdom.
That the 14. Eloquence is nothing else but the power
same are of winning belief of what we say. And to that
necessarily end we must have aid from the passions of the
eloquent.
hearer. Now to demonstration and teaching of
the truth, there are required long deductions, and great
attention, which is unpleasant to the hearer. Therefore they
which seek not truth, but belief, must take another way, and not
only derive what they would have to be believed, from
somewhat believed already, but also, by aggravations and
extenuations, make good and bad, right and wrong, appear
great or less, according as shall serve their turns. And such is the
power of eloquence, as many times a man is made to believe
thereby, that he sensibly feeleth smart and damage, when he
feeleth none, and to enter into rage and indignation, without any
other cause, than what is in the words and passion of the
speaker. This considered, together with the business that he
hath to do, who is the author of rebellion, namely, to make men
believe that their rebellion is just, their discontents grounded
upon great injuries, and their hopes great; there needeth no
more to prove, there can be no author of rebellion, that is not an
eloquent and powerful speaker, and withal, as hath been said
before, a man of little wisdom. For the faculty of speaking
powerfully, consisteth in a habit gotten of putting together
passionate words, and applying them to the present passions of
the hearer. are not able to effect.
In what 15. Seeing then eloquence and want of
manner they discretion concur to the stirring of rebellion, it
concur to may be demanded, what part each of these
their common
effects. acteth therein? The daughters of Pelias, king of
Thessaly, desiring to restore their old decrepit
father to the vigour of his youth, by the counsel of Medea,
chopped him in pieces, and set him a boiling with I know not
what herbs in a cauldron, but could not revive him again. So
when eloquence and want of judgment go together, want of
judgment, like the daughters of Pelias, consenteth, through
eloquence, which is as the witchcraft of Medea, to cut the
commonwealth in pieces, upon pretence or hope of reformation,
which when things are in combustion, they are not able to
effect.

CHAPTER IX.

1. The law over sovereigns, salus populi. 2. That sovereigns ought to


establish the religion they hold for best. 3. That to forbid unnatural
copulation, promiscuous use of women, &c. is the law of nature. 4. That
to leave man as much liberty as may be, &c. is the duty of a sovereign by
the law of nature. 5. Meum and tuum, to be set out to the subjects,
distinct from one another, &c. a duty of sovereigns by the law of nature.
6. An extraordinary power for judging the abuses of magistrates,
necessary, &c. 7. The suppressing of popularity, &c. necessary, &c. 8. The
instruction of youth, &c. necessary, &c. 9. Avoiding of unnecessary war, a
necessary duty of the sovereign, &c.

The law over 1. Having hitherto set forth how a body politic
sovereigns is made, and how it may be destroyed, this
salus populi.
place requireth to say something concerning the
preservation of the same, not purposing to enter into the
particulars of the art of government, but to sum up the general
heads, wherein such art is to be employed, and in which
consisteth the duty of him or them that have the sovereign
power. For the duty of a sovereign consisteth in the good
government of the people. And although the acts of sovereign
power be no injuries to the subjects who have consented to the
same by their implicit wills, yet when they tend to the hurt of the
people in general, they be breaches of the law of nature, and of
the divine law; and consequently, the contrary acts are the
duties of sovereigns, and required at their hands to the utmost
of their endeavour, by God Almighty, under the pain of eternal
death. And as the art and duty of sovereigns consist in the same
acts, so also doth their profit. For the end of art, is profit; and
governing to the profit of the subjects, is governing to the profit
of the sovereign, as hath been showed Part II. chapter V. section
1. And these three: 1. The law over them that have sovereign
power: 2. Their duty: 3. Their profit: are one and the same thing
contained in this sentence, Salus populi suprema lex. By which
must be understood, not the mere preservation of their lives, but
generally their benefit and good. So that this is the general law
for sovereigns, That they procure, to the uttermost of their
endeavour, the good of the people.
That 2. And forasmuch as eternal is better than
sovereigns temporal good, it is evident, that they who are
ought to in sovereign authority, are by the law of nature
establish the
religion they obliged to further the establishing of all such
hold for best. doctrines and rule, and the commanding of all
such actions, as in their conscience they believe
to be the true way thereunto. For unless they do so, it cannot be
said truly, that they have done the uttermost of their endeavour.
That to forbid 3. For the temporal good of the people, it
unnatural consisteth in four points: 1. Multitude: 2.
copulation, Commodity of living: 3. Peace amongst
promiscuous
use of themselves: 4. Defence against foreign power.
women, &c. Concerning multitude, it is the duty of them that
are in sovereign authority, to increase the
is the law of people, in as much as they are governors of
nature. mankind under God Almighty, who having
created but one man, and one woman, declared, that it was his
will they should be multiplied and increased afterwards. And
seeing this is to be done by ordinances concerning copulation,
they are by the law of nature bound to make such ordinances
concerning the same, as may tend to the increase of mankind.
And hence it cometh, that in them who have sovereign authority,
not to forbid such copulations as are against the use of nature;
not to forbid the promiscuous use of women, not to forbid one
woman to have many husbands, not to forbid marriages within
certain degrees of kindred and affinity, are against the law of
nature. For though it be not evident, that a private man living
under the law of natural reason only, doth break the same, by
doing any of the things aforesaid; yet it is manifestly apparent,
that being so prejudicial as they are to the improvement of
mankind, that not to forbid the same, is against the law of
natural reason in him, that hath taken into his hands any portion
of mankind to improve.
That to leave 4. The commodity of living consisteth in
man as much liberty and wealth. By liberty, I mean, that there
liberty as be no prohibition without necessity of any thing
may be, &c. is
the duty of a to any man, which was lawful to him in the law
sovereign by of nature; that is to say, that there be no
the law of restraint of natural liberty, but what is necessary
nature. for the good of the commonwealth, and that
well-meaning men may not fall into the danger of laws, as into
snares, before they be aware. It appertaineth also to this liberty,
that a man may have commodious passage from place to place,
and not be imprisoned or confined with the difficulty of ways,
and want of means for transportation of things necessary. And
for the wealth of people, it consisteth in three things, the well
ordering of trade, procuring of labour, and forbidding the
superfluous consuming of food and apparel. All those therefore
that are in sovereign authority, and have taken upon them the
government of people, are bound by the law of nature to make
ordinances consisting in the points afore named, as being
contrary to the law of nature, unnecessarily, either for one’s own
fancy, to enthral, or tie men so, as they cannot move without
danger; or to suffer them whose maintenance is our benefit, to
want anything necessary for them, by our negligence.
Meum and 5. For maintaining of peace at home, there be
tuum, to be so many things necessarily to be considered,
set out to the and taken order in, as there be several causes
subjects,
distinct from concurring to sedition. And first, it is necessary
one another, to set out to every subject, his propriety, and
&c. a duty of distinct lands and goods, upon which he may
sovereigns by exercise and have the benefit of his own
the law of industry, and without which men would fall out
nature.
amongst themselves, as did the herdsmen of
Abraham and Lot, every man encroaching and usurping as much
of the common benefit as he can, which tendeth to quarrel and
sedition. Secondly, to divide the burthens and charges of the
commonwealth proportionably. Now there is a proportionably to
every man’s ability, and there is a proportionably to his benefit
by commonwealth: and this latter is it, which is according to the
law of nature. For the burdens of the commonwealth being the
price that we pay for the benefit thereof, they ought to be
measured thereby. And there is no reason, when two men
equally enjoying, by the benefit of the commonwealth, their
peace and liberty, to use their industry to get their livings,
whereof one spareth, and layeth up somewhat, the other
spendeth all he gets, why they should not equally contribute to
the common charge. That seemeth therefore to be the most
equal way of dividing the burden of public charge, when every
man shall contribute according to what he spendeth, and not
according to what he gets. And this is then done, when men pay
the commonwealth’s part in the payments they make for their
own provision. And this seemeth not only most equal, but also
least sensible, and least to trouble the mind of them that pay it.
For there is nothing so aggravateth the grief of parting with
money to the public, as to think they are over-rated, and that
their neighbours whom they envy, do thereupon insult over
them, and this disposeth them to resistance, and, after that such
resistance hath produced a mischief, to rebellion.
An 6. Another thing necessary for the maintaining
extraordinary of peace, is the due execution of justice, which
power for consisteth principally in the right performance of
judging the
abuses of their duties, which are the magistrates, ordained
magistrates, for the same by and under the authority of the
necessary, sovereign power, which being private men in
&c. respect of the sovereign, and consequently such
as may have private ends, whereby they may be corrupted with
gifts, or intercession of friends, ought to be kept in awe by an
higher power, lest people, grieved by their injustice, should take
upon them to make their own revenges, to the disturbance of
the common peace; which can by no way be avoided in the
principal and immediate magistrates, without the judicature of
the sovereign himself, or some extraordinary power delegated by
him. It is therefore necessary, that there be a power
extraordinary, as there shall be occasion from time to time, for
the syndication of judges and other magistrates, that shall abuse
their authority, to the wrong and discontent of the people; and a
free and open way for the presenting of grievances to him or
them that have the sovereign authority.
The 7. Besides these considerations, by which are
suppressing prevented the discontents that arise from
of popularity, oppression, there ought to be some means for
&c.
necessary, the keeping under of those, that are disposed to
&c. rebellion by ambition; which consist principally
in the constancy of him that hath the sovereign
power, who ought therefore constantly to grace and encourage
such, as being able to serve the commonwealth, do nevertheless
contain themselves within the bounds of modesty, without
repining at the authority of such as are employed, and without
aggravating the errors, which, as men, they may commit,
especially when they suffer not in their own particular; and
constantly to show displeasure, and dislike of the contrary. And
not only so, but also to ordain severe punishments for such, as
shall by reprehension of public actions, affect popularity and
applause amongst the multitude, by which they may be enabled
to have a faction in the commonwealth at their devotion.
The 8. Another thing necessary, is the rooting out
instruction of of the consciences of men, all those opinions
youth, &c. which seem to justify and give pretence of right
necessary,
&c. to rebellious actions; such as are the opinions,
that a man can do nothing lawfully against his
private conscience; that they who have the sovereignty, are
subject to the civil laws; that there is any authority of subjects,
whose negative may hinder the affirmative of the sovereign
power; that any subject hath a propriety distinct from the
dominion of the commonwealth; that there is a body of the
people without him or them that have the sovereign power; and
that any lawful sovereign may be resisted under the name of a
tyrant; which opinions are they, which, Part II. chap. VIII. sect. 5-
10, have been declared to dispose men to rebellion. And
because opinions which are gotten by education, and in length
of time, are made habitual, cannot be taken away by force, and
upon the sudden; they must therefore be taken away also by
time and education. And seeing the said opinions have
proceeded from private and public teaching, and those teachers
have received them from grounds and principles, which they
have learned in the Universities, from the doctrine of Aristotle,
and others, who have delivered nothing concerning morality and
policy demonstratively; but being passionately addicted to
popular government, have insinuated their opinions by eloquent
sophistry. There is no doubt, if the true doctrine concerning the
law of nature, and the properties of a body politic, and the
nature of law in general, were perspicuously set down and
taught in the Universities, but that young men, who come thither
void of prejudice, and whose minds are as white paper, capable
of any instruction, would more easily receive the same, and
afterward teach it to the people, both in books and otherwise,
than now they do the contrary.
Avoiding of 9. The last thing contained in that supreme
unnecessary law, salus populi, is their defence; and
war, a consisteth partly in the obedience and unity of
necessary the subjects, of which hath been already
duty of the
sovereign,
spoken, and in which consisteth the means of
&c. levying soldiers, and of having money, arms,
ships, and fortified places in readiness for
defence; and partly, in the avoiding of unnecessary wars. For
such commonwealths, or such monarchs, as affect war for itself,
that is to say, out of ambition, or of vain-glory, or that make
account to revenge every little injury, or disgrace done by their
neighbours, if they ruin not themselves, their fortune must be
better than they have reason to expect.

CHAPTER X.

1. All expressions, &c. concerning future actions, are either covenant,


counsel, or command. 2. The difference between a law and a covenant.
3. The command of him whose command is law in one thing, is law in
every thing. 4. The difference between law and counsel. 5. The
difference between jus and lex. 6. The division of laws, &c. 7. That the
divine moral law, and the law of nature, is the same. 8. That the civil
laws are the common measure of right and wrong, &c. 9. Martial law is
civil law. 10. Written laws, &c. Unwritten, &c. Customs and opinions, &c.

All 1. Thus far concerning the Nature of Man, and


expressions, the constitution and properties of a Body Politic.
&c.
There remaineth only for the last chapter, to
concerning
future speak of the nature and sorts of law. And first it
actions, are is manifest, that all laws are declarations of the
either mind, concerning some action future to be done,
covenant, or omitted. And all declarations and expressions
counsel, or
of the mind concerning future actions and
command.
omissions, are either promissive, as I will do, or
not do; or provisive, as for example, If this be done or not done,
this will follow; or imperative, as Do this, or do it not. In the first
sort of these expressions, consisteth the nature of a covenant; in
the second, consisteth counsel; in the third, command.
The 2. It is evident when a man doth, or
difference forbeareth to do any action, if he be moved
between a thereto by this only consideration, that the same
law and a
covenant. is good or evil in itself; and that there be no
reason why the will or pleasure of another,
should be of any weight in his deliberation, that then neither to
do nor omit the action deliberated, is any breach of law. And
consequently, whatsoever is a law to a man, respecteth the will
of another, and the declaration thereof. But a covenant is a
declaration of a man’s own will. And therefore a law and a
covenant differ: and though they be both obligatory, and a law
obligeth no otherwise than by virtue of some covenant made by
him who is subject thereunto, yet they oblige by several sorts of
promises. For a covenant obligeth by promise of an action, or
omission especially named and limited; but a law bindeth by a
promise of obedience in general, whereby the action to be done,
or left undone, is referred to the determination of him, to whom
the covenant is made. So that the difference between a
covenant and a law, standeth thus: in simple covenant, the
action to be done, or not done, is first limited and made known,
and then followeth the promise to do or not do; but in a law, the
obligation to do or not to do, precedeth, and the declaration
what is to be done, or not done, followeth after.
The 3. And from this may be deduced, that which
command of to some may seem a paradox, That the
him whose command of him, whose command is a law in
command is
law in one one thing, is a law in every thing. For seeing a
thing, is law man is obliged to obedience before what he is to
in every do be known, he is obliged to obey in general,
thing. that is to say, in every thing.
The 4. That the counsel of a man is no law to him
difference that is counselled, and that he who alloweth
another to give him counsel, doth not thereby
between law oblige himself to follow the same, is manifest
and counsel. enough. And yet men usually call counselling, by
the name of governing; not that they are not able to distinguish
between them, but because they envy many times those men
that are called to counsel, and are therefore angry with them
that they are counselled. But if to counsellors there should be
given a right to have their counsel followed, then are they no
more counsellors, but masters of them whom they counsel; and
their counsels no more counsels, but laws. For the difference
between a law and a counsel being no more but this, that in
counsel the expression is, Do, because it is best; in a law, Do,
because I have a right to compel you; or Do, because I say, do;
when counsel should give the reason of the action it adviseth to,
because the reason thereof itself is no more counsel, but a law.
The 5. The names lex and jus, that is to say, law
difference and right, are often confounded, and yet scarce
between jus are there any two words of more contrary
& lex.
signification. For right is that liberty which law
leaveth us, and laws those restraints by which we agree mutually
to abridge one another’s liberty. Law and right therefore are no
less different than restraint and liberty, which are contrary; and
whatsoever a man doth, that liveth in a commonwealth jure, he
doth it jure civili, jure naturæ, and jure divino. For whatsoever is
against any of these laws, cannot be said to be jure. For the civil
law cannot make that to be done jure, which is against the law
divine, or of nature. And therefore whatsoever any subject doth,
if it be not contrary to the civil law, and whatsoever a sovereign
doth, if it be not against the law of nature, he doth it jure divino,
by divine right. But to say, lege divinâ, by divine law, is another
thing. For the laws of God and nature allowing greater liberty
than is allowed by the law civil; for subordinate laws do still bind
more than superior laws, the essence of law being not to loose,
but to bind, a man may be commanded that by a law civil, which
is not commanded by the law of nature, nor by the law divine.
So that of things done lege, that is to say, by command of the
law, there is some place for a distinction between lege divinâ,
and lege civili. As when a man giveth an alms, or helpeth him
that is in need, he doth it not lege civili, but lege divinâ, by the
divine law, the precept whereof is charity. But for things that are
done jure, nothing can be said to be done jure divino, that is not
also jure civili, unless it be done by them that having sovereign
power, are not subject to the civil law.
The division 6. The differences of laws, are according to
of laws, &c. the differences, either of the authors and
lawmakers, or of the promulgation, or of those that are subject
to them. From the difference of the authors, or lawmakers,
cometh the division of law into divine, natural, and civil. From
the difference of promulgation, proceedeth the division of laws
into written and unwritten. And from the difference of the
persons to whom the law appertaineth, it proceedeth, that some
laws are called simply laws, and some penal. As for example,
thou shalt not steal, is simply a law; but this, he that stealeth an
ox, shall restore four-fold, is a penal, or as others call it, a
judicial law. Now in those laws, which are simply laws, the
commandment is addressed to every man; but in penal laws the
commandment is addressed to the magistrate, who is only guilty
of the breach of it, when the penalties ordained, are not
inflicted; to the rest appertained nothing, but to take notice of
their danger.
That the 7. As for the first division of law into divine,
divine moral natural, and civil, the first two branches are one
law, and the and the same law. For the law of nature, which
law of
nature, is the is also the moral law, is the law of the author of
same. nature, God Almighty; and the law of God
taught by our Saviour Christ, is the moral law.
For the sum of God’s law is, Thou shalt love God above all, and
thy neighbour as thyself; and the same is the sum of the law of
nature, as hath been showed, Part I. chap. V. And although the
doctrine of our Saviour be of three parts, moral, theological, and
ecclesiastical; the former part only, which is the moral, is of the
nature of a law universal; the latter part is a branch of the law
civil; and the theological, which containeth those articles
concerning the divinity and kingdom of our Saviour, without
which there is no salvation, is not delivered in the nature of laws,
but of counsel and direction, how to avoid the punishment,
which by the violation of the moral law, men are subject to. For
it is not infidelity that condemneth, though it be faith that
saveth, but the breach of the law and commandments of God,
written first in man’s heart, and afterwards in tables, and
delivered to the Jews by the hands of Moses.
That the civil 8. In the state of nature, where every man is
laws are the his own judge, and differeth from other
common concerning the names and appellations of
measure of
right and things, and from those differences arise quarrels
wrong, &c. and breach of peace, it was necessary there
should be a common measure of all things, that
might fall in controversy. As for example; of what is to be called
right, what good, what virtue, what much, what little, what
meum and tuum, what a pound, what a quart, &c. For in these
things private judgments may differ, and beget controversy. This
common measure, some say, is right reason: with whom I
should consent, if there were any such thing to be found or
known in rerum naturâ. But commonly they that call for right
reason to decide any controversy, do mean their own. But this is
certain, seeing right reason is not existent, the reason of some
man or men must supply the place thereof; and that man or
men, is he or they, that have the sovereign power, as hath been
already proved; and consequently the civil laws are to all
subjects the measures of their actions, whereby to determine,
whether they be right or wrong, profitable or unprofitable,
virtuous or vicious; and by them the use and definition of all
names not agreed upon, and tending to controversy, shall be
established. As for example, when upon occasion of some
strange and deformed birth, it shall not be decided by Aristotle,
or the philosophers, whether the same be a man, or no, but by
the laws; the civil law containing in it the ecclesiastical, as a part
thereof, proceeding from the power of ecclesiastical government,
given by our Saviour to all Christian sovereigns, as his immediate
vicars, as hath been said Part II. chap. VII. sect. 10.
Martial law is 9. But seeing it hath been said, that all laws
civil law. are either natural or civil, it may be demanded,
to which of these shall be referred that law, which is called
martial law, and by the Romans, disciplina militaris? And it may
seem to be the same with the law of nature; because the laws
by which a multitude of soldiers are governed in an army are not
constant, but continually changing with the occasion; and that is
still a law, which is reason for the present, and reason is the law
of nature. It is nevertheless true, that martial law is civil law,
because an army is a body politic, the whole power whereof is in
the General, and the laws thereof made by him; and though they
still follow and change as reason requireth, yet it is not, as the
reason of every private man, but as the reason of the General
requireth.
Written laws, 10. When he or they in whom is the sovereign
Unwritten, power of a commonwealth, are to ordain laws
&c. Customs, for the government and good order of the
and Opinions,
&c. people, it is not possible they should
comprehend all cases of controversy that may
fall out, or perhaps any considerable diversity of them: but as
time shall instruct them by the rising of new occasions, so are
also laws from time to time to be ordained: and in such cases
where no special law is made, the law of nature keepeth its
place, and the magistrates ought to give sentence according
thereunto, that is to say, according to natural reason. The
constitutions therefore of the sovereign power, by which the
liberty of nature is abridged, are written, because there is no
other way to take notice of them; whereas the laws of nature
are supposed to be written in men’s hearts. Written laws
therefore are the constitutions of a commonwealth expressed;
and unwritten, are the laws of natural reason. Custom of itself
maketh no laws. Nevertheless when a sentence hath been once
given, by them that judge by their natural reason, whether the
same be right or wrong, it may attain to the vigour of a law; not
because the like sentence hath of custom been given in the like
case, but because the sovereign power is supposed tacitly to
have approved such sentence for right, and thereby it cometh to
be a law, and numbered amongst the written laws of the
commonwealth. For if custom were sufficient to introduce a law,
then it would be in the power of every one that is deputed to
hear a cause, to make his errors laws. In the like manner, those
laws that go under the title of responsa prudentum, that is to
say, the opinions of lawyers, are not therefore laws, because
responsa prudentum, but because they are admitted by the
sovereign. And from this may be collected, that when there is a
case of private contract between the sovereign and the subject,
a precedent against reason shall not prejudice the cause of the
sovereign; no precedent being made a law, but upon supposition
that the same was reasonable from the beginning.
And thus much concerning the elements and general grounds
of laws natural and politic. As for the law of nations, it is the
same with the law of nature. For that which is the law of nature
between man and man, before the constitution of
commonwealth, is the law of nations between sovereign and
sovereign, after.
OF
LIBERTY AND NECESSITY:

A TREATISE,
WHEREIN ALL CONTROVERSY CONCERNING

PREDESTINATION, ELECTION, FREE-WILL, GRACE,


MERITS, REPROBATION, &c.

IS FULLY DECIDED AND CLEARED.

IN ANSWER TO A TREATISE
WRITTEN BY THE BISHOP OF LONDONDERRY,
ON THE SAME SUBJECT.
TO THE
SOBER AND DISCREET READER.

It made St. Chrysostom tremble whenever he reflected on the


proportion, which those that went the narrow way, bore to those
which marched in the broad, how many were the called, and
how few the chosen, how many they were that were created for
and in a capacity of eternal beatitude, and how few attained it.
This consideration certainly would make a man look upon the
Holy Scriptures, among Christians, as the greatest indulgence of
heaven, being all the directions it hath been pleased to afford
poor man in so difficult a journey as that of his eternal bliss or
misery. But when a man cometh to look into those transcendant
writings, he finds them to be the works of a sort of innocent
harmless men, that had little acquaintance or familiarity with the
world, and consequently not much interested in the troubles and
quarrels of several countries; that though they are all but
necessary, yet were they written occasionally, rather than out of
design; and lastly, that their main business is, to abstract man
from this world, and to persuade him to prefer the bare hope of
what he can neither see, hear, nor conceive, before all the
present enjoyments this world can afford. This begat a reverence
and esteem to them in all those who endeavour to work out
their salvation out of them. But if a man, not weighing them in
themselves, shall consider the practices of those, who pretend to
be the interpreters of them, and to make them fit meat for the
people; how that instead of renouncing the world, they
endeavour to raise themselves into the greatest promotions,
leisure, and luxury; that they make them the decoys of the
people, to carry on designs and intrigues of state, and study the
enjoyments of this world more than any other people: he will
find some grounds to conclude, the practices of such men to be
the greatest disturbance, burden, and vexation of the Christian
part of the world. The complaint is as true as sad; instead of
acquainting the credulous vulgar, with the main end of their
functions, and the great business of their embassy, what a great
measure of felicity is prepared for them, and how easily it may
be forfeited; they involve their consciences in the briars of a
thousand needless scruples, they spin out volumes out of half
sentences, nay, out of points and accents, and raise endless
controversies about things, (were men free from passion and
prejudice), in themselves clear enough: and when they have
canvassed their questions, till they are weary themselves, and
have wearied hearers and readers, and all they have to do with,
every one sits down under his own vine, and hugs his own
apprehensions; so that after all their pains, bandings, and
implacable adhesion to parties, the inconvenience remains still,
and we as far from any solid conviction, as at first setting out.
The controversies betwixt Rome and the Reformation are long
since beaten out of the pit, by other combatants of their own
brood; so that if we speak of Protestant and Catholic, they are in
a manner content to sit down with their present acquests: for as
to conviction, he certainly is a rare proselyte, at whose
conversion, interest, humour, discontent, inclination, are not
admitted to the debate.
But to come yet nearer our purpose, let us consider our own
fractions of fractions of religion here in England, where if that
saying, that it is better to live where nothing is lawful, than
where all things, be as true in religion as policy, posterity may
haply feel the sad consequence of it. What, I pray, is the effect
of so many sermons, teachings, preachings, exercises, and
exercising of gifts, meetings, disputations, conferences,
conventicles, printed books, written with so much distraction and
presumption upon God Almighty, and abuse of his Holy Word?
Marry this: it is the seminary of many vexatious, endless, and
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebooknice.com

You might also like