Data Structures Algorithms and Applications in C++ 1st Edition by Adam Drozdek ISBN 1133608426 9781133608424 PDF Download
Data Structures Algorithms and Applications in C++ 1st Edition by Adam Drozdek ISBN 1133608426 9781133608424 PDF Download
https://ebookball.com/product/data-structures-algorithms-and-
applications-in-c-1st-edition-by-adam-drozdek-
isbn-1133608426-9781133608424-17250/
https://ebookball.com/product/data-structures-and-algorithms-in-c-1st-
edition-by-adam-drozdek-asin-b002wlxmby-25076/
https://ebookball.com/product/problem-solving-in-data-structures-and-
algorithms-using-c-1st-edition-by-hemant-jain-
isbn-9352655915-9789352655915-15768/
https://ebookball.com/product/problem-solving-in-data-structures-and-
algorithms-using-c-1st-edition-by-hemant-jain-
isbn-1540407306-9781540407306-15866/
https://ebookball.com/product/data-structures-and-algorithms-in-
java-1st-edition-by-peter-drake-isbn-0131469142-9780131469143-12422/
https://ebookball.com/product/data-structures-and-algorithms-1st-
edition-by-alfred-aho-isbn-0201000237-9780201000238-25072/
Data Structures
and Algorithms
in C++
Fourth Edition
Adam Drozdek
Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
This is an electronic version of the print textbook. Due to electronic rights restrictions,
some third party content may be suppressed. Editorial review has deemed that any suppressed
content does not materially affect the overall learning experience. The publisher reserves the right
to remove content from this title at any time if subsequent rights restrictions require it. For
valuable information on pricing, previous editions, changes to current editions, and alternate
formats, please visit www.cengage.com/highered to search by ISBN#, author, title, or keyword for
materials in your areas of interest.
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Data Structures and © 2013 Cengage Learning
Algorithms in C++,
ALL RIGHTS RESERVED. No part of this work covered by the copyright herein
Fourth Edition
may be reproduced, transmitted, stored or used in any form or by any means
by Adam Drozdek graphic, electronic, or mechanical, including but not limited to photocopying,
Executive Editor: Marie Lee recording, scanning, digitizing, taping, Web distribution, information networks,
or information storage and retrieval systems, except as permitted under
Senior Product Manager:
Section 107 or 108 of the 1976 United States Copyright Act, without the prior
Alyssa Pratt
written permission of the publisher.
Associate Product Manager:
Stephanie Lorenz
For product information and technology assistance, contact us at
Content Project Manager: Cengage Learning Customer & Sales Support, www.cengage.com/support
Matthew Hutchinson For permission to use material from this text or product, submit all
Art Director: Cheryl Pearl requests online at www.cengage.com/permissions
Further permissions questions can be emailed to
Print Buyer: Julio Esperas
[email protected]
Compositor: PreMediaGlobal
Proofreader: Andrea Schein Library of Congress Control Number: 2012942244
Indexer: Sharon Hilgenberg ISBN-13: 978-1-133-60842-4
ISBN-10: 1-133-60842-6
Cengage Learning
20 Channel Center Street
Boston, MA 02210
USA
Cengage Learning is a leading provider of customized learning solutions with
office locations around the globe, including Singapore, the United Kingdom,
Australia, Mexico, Brazil and Japan. Locate your local office at:
www.cengage.com/global
Cengage Learning products are represented in Canada by Nelson Education, Ltd.
To learn more about Cengage Learning, visit www.cengage.com
Purchase any of our products at your local college store or at our preferred
online store www.cengagebrain.com
Some of the product names and company names used in this book have been used for identification purposes only
and may be trademarks or registered trademarks of their respective manufacturers and sellers.
Any fictional data related to persons or companies or URLs used throughout this book is intended for instructional
purposes only. At the time this book was printed, any such data was fictional and not belonging to any real persons or
companies.
Cengage Learning reserves the right to revise this publication and make changes from time to time in its content
without notice.
The programs in this book are for instructional purposes only. They have been tested with care, but are not guaranteed
for any particular intent beyond educational purposes. The author and the publisher do not offer any warranties or repre-
sentations, nor do they accept any liabilities with respect to the programs.
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Contents
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
vi ■ Contents
2 Complexity Analysis 51
2.1 Computational and Asymptotic Complexity 51
2.2 Big-O Notation 52
2.3 Properties of Big-O Notation 54
2.4 Ω and Θ Notations 56
2.5 Possible Problems 57
2.6 Examples of Complexities 57
2.7 Finding Asymptotic Complexity: Examples 59
2.8 The Best, Average, and Worst Cases 61
2.9 Amortized Complexity 64
2.10 NP-Completeness 68
2.11 Exercises 71
Bibliography 74
3 Linked Lists 75
3.1 Singly Linked Lists 75
3.1.1 Insertion 81
3.1.2 Deletion 83
3.1.3 Search 89
3.2 Doubly Linked Lists 90
3.3 Circular Lists 94
3.4 Skip Lists 96
3.5 Self-Organizing Lists 101
3.6 Sparse Tables 106
3.7 Lists in the Standard Template Library 109
3.8 Concluding Remarks 113
3.9 Case Study: A Library 114
3.10 Exercises 125
3.11 Programming Assignments 127
Bibliography 130
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Contents ■ vii
5 Recursion 169
5.1 Recursive Definitions 169
5.2 Function Calls and Recursion Implementation 172
5.3 Anatomy of a Recursive Call 174
5.4 Tail Recursion 177
5.5 Nontail Recursion 178
5.6 Indirect Recursion 184
5.7 Nested Recursion 186
5.8 Excessive Recursion 186
5.9 Backtracking 190
5.10 Concluding Remarks 197
5.11 Case Study: A Recursive Descent Interpreter 198
5.12 Exercises 207
5.13 Programming Assignments 210
Bibliography 213
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Contents ■ ix
8 Graphs 391
8.1 Graph Representation 393
8.2 Graph Traversals 395
8.3 Shortest Paths 398
8.3.1 All-to-All Shortest Path Problem 405
8.4 Cycle Detection 408
8.4.1 Union-Find Problem 409
8.5 Spanning Trees 411
8.6 Connectivity 415
8.6.1 Connectivity in Undirected Graphs 415
8.6.2 Connectivity in Directed Graphs 418
8.7 Topological Sort 421
8.8 Networks 423
8.8.1 Maximum Flows 423
8.8.2 Maximum Flows of Minimum Cost 433
8.9 Matching 438
8.9.1 Stable Matching Problem 442
8.9.2 Assignment Problem 445
8.9.3 Matching in Nonbipartite Graphs 447
8.10 Eulerian and Hamiltonian Graphs 449
8.10.1 Eulerian Graphs 449
8.10.2 Hamiltonian Graphs 453
8.11 Graph Coloring 459
8.12 NP-Complete Problems in Graph Theory 462
8.12.1 The Clique Problem 462
8.12.2 The 3-Colorability Problem 463
8.12.3 The Vertex Cover Problem 465
8.12.4 The Hamiltonian Cycle Problem 466
8.13 Case Study: Distinct Representatives 467
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
x ■ Contents
8.14 Exercises 480
8.15 Programming Assignments 486
Bibliography 487
9 Sorting 491
9.1 Elementary Sorting Algorithms 492
9.1.1 Insertion Sort 492
9.1.2 Selection Sort 495
9.1.3 Bubble Sort 497
9.1.4 Comb Sort 500
9.2 Decision Trees 501
9.3 Efficient Sorting Algorithms 505
9.3.1 Shell Sort 505
9.3.2 Heap Sort 508
9.3.3 Quicksort 512
9.3.4 Mergesort 518
9.3.5 Radix Sort 521
9.3.6 Counting Sort 527
9.4 Sorting in the Standard Template Library 528
9.5 Concluding Remarks 532
9.6 Case Study: Adding Polynomials 534
9.7 Exercises 542
9.8 Programming Assignments 543
Bibliography 545
10 Hashing 548
10.1 Hash Functions 549
10.1.1 Division 549
10.1.2 Folding 549
10.1.3 Mid-Square Function 550
10.1.4 Extraction 550
10.1.5 Radix Transformation 551
10.1.6 Universal Hash Functions 551
10.2 Collision Resolution 551
10.2.1 Open Addressing 552
10.2.2 Chaining 558
10.2.3 Bucket Addressing 560
10.3 Deletion 561
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Contents ■ xi
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xii ■ Contents
12.6 Exercises 667
12.7 Programming Assignments 668
Bibliography 671
Appendixes
A Computing Big-O 743
A.1 Harmonic Series 743
A.2 Approximation of the Function lg(n!) 743
A.3 Big-O for Average Case of Quicksort 745
A.4 Average Path Length in a Random Binary Tree 747
A.5 The Number of Nodes in an AVL Tree 748
B Algorithms in the Standard Template Library 749
B.1 Standard Algorithms 749
C NP-Completeness 758
C.1 Cook’s Theorem 758
Index 771
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Preface
xiii
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Random documents with unrelated
content Scribd suggests to you:
stumbled back, his hand crashing against a rheostat. A thunderous
surge of high voltage crackled suddenly, kniving along the glassite,
and the motors from the lower decks sang a mounting, thunderous
song.
At the same time, everything shifted. Something had dealt the mud-
submarine a tremendous blow from above. They were sent
careening against the hull and then to the floor, which began to tilt.
Link Raeburn had been thrown to his hands and knees. Now his eyes
goggled up at the instrument panel. Lonny Higgens sat sprawled out
with the girl a tumbled heap in his arms.
"Good going, Lonny!" cried Raeburn incredulously. "We're sinking
again. How did you manage to do it?"
Lonny blinked through a cascade of tumbling russet curls and looked
up wonderingly.
"I suppose the electrolysis worked after all," he answered weakly.
"Under the pressure, the high voltage must have produced liquid
oxygen, and then ignited it. And if the propellers are working we
ought to be able to wriggle out into the clear some way."
"That puts a different light on the entire matter," said Raeburn,
getting to his feet and drawing a ray pistol from his pocket. "I told
you I would do you in for good the next time I made a try. Get up,
Lonny, and start saying your prayers."
"My goodness," gasped Baron Munchy, crawling up over the edge of
the control chair and looking on with glittering, faceted eyes.
VI
Lonny Higgens got up slowly, then glanced lazily toward the control
instruments, where the depth indicator had dipped down noticeably.
"That's all very well in due time," he said, "but we're still under the
ledge, and not out of danger by any means. If we don't shove from
under it we'll land back in the same shape."
"Get over to those controls then," ordered Raeburn.
Lonny grinned and went to the familiar seat. The craft was making
larger circles as it descended, indicating that his guess must have
been correct, that they were in a pocket, and that the pocket was
broadening. Somewhere at the bottom of that pocket was a tunnel
opening upon the outer ocean and it was up to him to find that
opening—blindfold. If they could only keep descending until the
vessel entered the channel their main problem would be solved. But
if the pressure generated from the explosion was dissipated too
suddenly, his mud-ship would ascend into the trap and stay forever
there on the muddy ocean floor.
He felt a lurch. The ship had paused and was sinking no longer. This
then, was the limit. It would not go low enough. He saw the horror
in Raeburn's eyes.
They would die from starvation here. The gun in Raeburn's hands
would be merciful, if it relieved them from the more hideous death
that was certain to come.
The hull shuddered, slipped against a rocky outer substance that
seemed to give way suddenly. He felt the relaxation of the outer
barrier through the controls, knew that the propellers were driving it
out and into the true bed of the Uranusian ocean. The needle
indicator paused uncertainly, started to rise. By the expression on
Raeburn's face he knew that the other had not guessed that their
trap was behind them.
It was his chance. Lonny's hands moved swiftly on the controls. A
surge of power sent the rear-drive mud-propellers spinning. Too
much power. The ship tilted swiftly and Raeburn lost his balance.
The man at the controls left them in a flash.
Lonny seized the wrist that held the gun, wrenched it away. It went
skidding across the floor. Then he stuck out fiercely at the sardonic
features so close to him. Raeburn rocked backward, flailing out with
both hands, as Lonny came in again, both fists landing solidly. His
antagonist spun backward, then fell helplessly to the decking. Baron
Munchy was jumping up and down in ecstacy.
"Hit'm, Boss! Sock 'im again!" he piped, but Lonny picked up the
gun, slipped it into his pocket, and shook his head in the negative.
"Hit'm, Boss. Sock 'im again!" Baron Munchy piped, jumping up and
down in ecstacy. "Him all bad. Say Boss no good."
"There's to be no more fighting, Raeburn," he said. "I'll pick you off
with the gun if you start anything. When we break the surface you
can get your mud-shoes and go."
Four thousand feet. Three thousand. The mud-submarine was rising
rapidly now, had passed the two thousand mark.
"You've really hurt the little fellow's feelings," said Lana Hilton,
evading his eyes and gesturing toward Baron Munchy, who was
beating his fists against the wall in sheer frustration. "He must have
been praying for blood and thunder."
"I'll plaster 'im!" Munchy was squeaking. "I'll do him in!"
One thousand.
"He's a misfit here," said Lonny slowly. "He comes from Umbriel, or
one of the other moons. On his own world he was used to great
activity. Uranus hasn't affected him—acting upon his nerves—as it
has the rest of us. But he's a misfit here. He expects the normal
activity of his own satellite upon Uranus. That just isn't possible. I
think he'd like it on earth."
"You mean—" began Lana, just as the mud-submarine broke the
surface and began bobbing to a rest. Lonny followed Raeburn up the
hatchway, watched him open it. The upper mists broke in damply,
sending heavy white furlers about their faces. Link Raeburn looked
glum and defeated as he donned the heavy mud-shoes and slogged
away into the mist.
Lonny Higgens closed the hatchway and yawned. He was beginning
to feel dog-tired again—a normal sensation on Uranus—but a grim
decision had taken shape in his mind.
"Sure," he said, in answer to the question in her gleaming eyes. "I'm
going to get out of here. I'm going to send an S. O. S. If that doesn't
work I'll get a straight call through to earth, charter a space yacht,
and have it sent to pick us up."
"Lonny, you mean, that—" began Lana, moving toward him with her
lips invitingly close.
But Lonny Higgens evaded her. He turned his back and sat down in a
chair, then yawned again. Uranus had him! Old rocking chair had
him! Something had him, as long as he was on this blasted planet.
Lovely as Lana was, it would take more energy than he could
assimilate to make love to her on this muddy world.
"I guess you'll have to save it," he sighed regretfully. "But you'd not
be safe to try those tactics again—once we get back on earth."
*** END OF THE PROJECT GUTENBERG EBOOK DERELICTS OF
URANUS ***
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
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.
ebookball.com