This eBook includes the following formats, accessible from your Account page after purchase:
EPUB
The open industry format known for its reflowable content and usability on supported mobile devices.
PDF
The popular standard, used most often with the free Acrobat® Reader® software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
Also available in other formats.
Algorithms power modern systems--from flight control and robotics to recommendation engines and AI applications. Algorithms and Programs: An AI-Assisted Approach shows how to clearly define an algorithm's purpose, design a structured outline, translate it into reliable code, and reason about its efficiency and complexity.
Rather than relying on traditional pseudocode, this book uses precise algorithm outlines that are easier to understand and directly support correct implementation. This approach bridges the gap between design and code, reducing ambiguity and minimizing errors.
Each algorithm is expressed as a sequence of clear, intended outcomes, which naturally define its correctness conditions and support reasoning about invariants and program behavior. This structured method encourages better programming practice, improves reliability, and aligns well with modern AI-assisted development tools.
Chapter 1. Specifying Problems, Functions, and Classes 1
1.1. Declarative Language.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2. Specifying Functions.. . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . 1
1.3. Generic Problem Formulation.. . . . . . . . . . . . . . . . . . . . .. . . . . . . .. . . . . . . . 4
1.4. Specifying Classes.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Chapter 2. Expressing Algorithms and Their Complexity 7
2.1. Algorithm Decomposition.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2. Using AI for Algorithm Development.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.3. Complexity.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Chapter 3. Algorithm Creation and Analysis 23
3.1. Decomposition of Algorithms.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.2. Outlining Algorithms.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.3. Pragmatic Outcomes.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . 27
3.4. Creating Outcomes.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.5. Implementing an Invariant Outcome with Loops.. . . . . . . . . . . . . . . . . . . . 33
3.6. Other Approaches to Creating Invariant Outcomes.. . . . . . . . . . . . . . . . . . . 35
3.7. Outcome Implementation.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . 37
Chapter 4. Divide-and-Conquer 41
4.1. Divide-and-Conquer Definition.. . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . 42
4.2. Example Application 1: Most Valuable Subsequence.. . . . . . . . . . . . . . . . . . . 47
4.3. Example Application 2: Binary Search. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
4.4. Time Complexity of Binary Search.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.5. False Divide-and-Conquer.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.6. Maximum Sublist Example: An Alternative Algorithm. . . . . . . . . . . . . . . . . . 54
4.7. Time Complexity of the Maximum Sublist Algorithm.. . . . . . . . . . . . . . . . . 56
Chapter 5. Sorting 59
5.1. An Application of Weakening Postconditions: Weakening Is Ordered.. .. .. . 59
5.2. A Sorting Algorithm Outline Based on Weakened Order.. . . . . . . . . . . . . . . 60
5.3. Insert Sort.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 63
5.4. Mergesort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.5. Quicksort.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
5.6. Heapsort.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 68
5.7. Radix Sort.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 70
5.8. Example of a Custom Algorithm.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . 73
Chapter 6. Dynamic Programming 75
6.1. Definition of Dynamic Programming and the Rod-Cutting Example.. . . . . . . . . 75
6.2. Time Complexity of Dynamic Programming.. . . . . . . . . . . . . . . . . . . . . . . . . . . 80
6.3. Longest Common Subsequence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Chapter 7. Greedy Algorithms 85
7.1. The Greedy Algorithm.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
7.2. Room Scheduling Example Completed.. . . . . . . . . . . . . . . .. . . . . . . . . . . . 90
7.3. Huffman Codes Example.. . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . 92
7.4. Time Complexity of Greedy Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Chapter 8. Minimum Spanning Trees 99
8.1. Graphs.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
8.2. Definition of Minimum Spanning Tree.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
8.3. Prims MST Algorithm.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
8.4. Kruskals MST Algorithm.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Chapter 9. Shortest Paths 109
9.1. The Shortest Path Problem.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
9.2. The Bellman-Ford Algorithm.. . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . 112
9.3. Dijkstras Algorithm.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Index 121
