Book Image

C++ Data Structures and Algorithm Design Principles

By : John Carey, Anil Achary, Shreyans Doshi, Payas Rajan
Book Image

C++ Data Structures and Algorithm Design Principles

By: John Carey, Anil Achary, Shreyans Doshi, Payas Rajan

Overview of this book

C++ is a mature multi-paradigm programming language that enables you to write high-level code with a high degree of control over the hardware. Today, significant parts of software infrastructure, including databases, browsers, multimedia frameworks, and GUI toolkits, are written in C++. This book starts by introducing C++ data structures and how to store data using linked lists, arrays, stacks, and queues. In later chapters, the book explains the basic algorithm design paradigms, such as the greedy approach and the divide-and-conquer approach, which are used to solve a large variety of computational problems. Finally, you will learn the advanced technique of dynamic programming to develop optimized implementations of several algorithms discussed in the book. By the end of this book, you will have learned how to implement standard data structures and algorithms in efficient and scalable C++ 14 code.
Table of Contents (11 chapters)

The Knapsack Problem(s)

In this section, we will discuss the standard knapsack problem, also known as the 0-1 knapsack problem, which is known to be NP-complete, and thereby does not allow us to have any polynomial-time solution. Then, we will turn our discussion toward a version of the knapsack problem called the fractional knapsack problem, which can be solved using a greedy approach. Our focus in this section is to demonstrate how even subtle differences between how a problem is defined can lead to large changes in the solution strategies.

The Knapsack Problem

Suppose you are given a set of objects, O = {O1, O2, …, On}, with each having a certain weight, Wi, and a value of Vi. You are also given a bag (or a knapsack) that can carry only a total weight of T units. Now, say you are tasked with finding out about a set of objects to keep in your bag so that the total weight is less than or equal to T, and the total value of the objects is the maximum it can possibly be.

A real-world example...