Swift Data Structure and Algorithms

Book description

Master the most common algorithms and data structures, and learn how to implement them efficiently using the most up-to-date features of Swift 3

About This Book

  • Develop a deep understanding of the collections in the Swift Standard Library with this step-by-step guide
  • Develop native Swift data structures and algorithms for use in mobile, desktop, and server-based applications
  • Learn about performance efficiency between different data structures and algorithms

Who This Book Is For

This book is for developers who want to learn how to implement and use common data structures and algorithms natively in Swift. Whether you are a self-taught developer without a formal technical background or you have a degree in Computer Science, this book will provide with the knowledge you need to develop advanced data structures and algorithms in Swift using the latest language features.

What You Will Learn

  • Get to know about the basic data structures and how to use the Swift REPL
  • Use the Swift Standard Library collections bridging to Objective-C collections, and find out about protocol-oriented programming
  • Find out about Swift generators and sequences, and see how to use them to implement advanced data structures such as Stack, StackList, Queue, and LinkedList
  • Implement sorting algorithms such as Insertion Sort, Merge Sort, and Quick Sort and understand the performance trade-offs between them
  • See how to implement various binary trees, B-Tree, and Splay Trees
  • Perform advanced searching methods using Red-Black trees, AVL trees, and Trie trees, and take a look at several substring search algorithms
  • Get to know about the data structures used in graphs and how to implement graphs such as depth-first search, breadth-first search, directed graphs, spanning tree, and shortest path
  • Explore algorithm efficiency and see how to measure it

In Detail

Apple's Swift language has expressive features that are familiar to those working with modern functional languages, but also provides backward support for Objective-C and Apple's legacy frameworks. These features are attracting many new developers to start creating applications for OS X and iOS using Swift.

Designing an application to scale while processing large amounts of data or provide fast and efficient searching can be complex, especially running on mobile devices with limited memory and bandwidth. Learning about best practices and knowing how to select the best data structure and algorithm in Swift is crucial to the success of your application and will help ensure your application is a success. That's what this book will teach you.

Starting at the beginning, this book will cover the basic data structures and Swift types, and introduce asymptotic analysis. You'll learn about the standard library collections and bridging between Swift and Objective-C collections. You will see how to implement advanced data structures, sort algorithms, work with trees, advanced searching methods, use graphs, and performance and algorithm efficiency. You'll also see how to choose the perfect algorithm for your problem.

Style and approach

This easy-to-follow yet comprehensive guide can either be read from beginning to end, or depending on your current knowledge level, you can jump to the specific chapter that interests you. Each chapter topic starts with an introduction to the topic and algorithm before moving on to the hands-on implementation and analysis.

Table of contents

  1. Swift Data Structure and Algorithms
    1. Swift Data Structure and Algorithms
    2. Credits
    3. About the Authors
    4. About the Reviewers
    5. www.PacktPub.com
      1. Why subscribe?
    6. Preface
      1. What this book covers 
      2. What you need for this book 
      3. Who this book is for 
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code 
        2. Downloading the color images of this book 
        3. Errata
        4. Piracy
        5. Questions
    7. 1. Walking Across the Playground
      1. What is the importance of data structures?
        1. Data structures + algorithms = programs
        2. Interactive Playgrounds
        3. The Swift REPL
      2. Fundamental data structures
        1. Contiguous data structures
          1. Arrays
          2. Declaring an array
          3. Retrieving elements
          4. Adding elements
          5. Removing elements
        2. Linked data structures
          1. Singly linked list
      3. Overview of data structures
        1. Overview of algorithms
      4. Data types in Swift
        1. Value types and reference types
        2. Named and compound types
        3. Type aliases
        4. Collection types in the Swift standard library
      5. Asymptotic analysis
        1. Order of growth
      6. Summary
    8. 2. Working with Commonly Used Data Structures
      1. Using the Swift standard library
        1. Why structures?
        2. Declaring arrays in Swift
          1. Initializing array
          2. Adding and updating elements in an array
          3. Retrieving and removing elements from an array
        3. Retrieving and initializing dictionaries
          1. Initializing a dictionary
          2. Adding/modifying/removing a key-value pair
          3. Retrieving values from a dictionary
        4. Declaring sets
          1. Initializing a set
          2. Modifying and retrieving elements of a set
          3. Set operations
            1. Comparison operations
            2. Membership and equality operations
        5. Characteristics of tuples
          1. Unnamed tuples
          2. Named tuples
      2. Implementing subscripting
        1. Subscript syntax
        2. Subscript options
      3. Understanding mutability and immutability
        1. Mutability of collections
      4. Interoperability between Swift and Objective-C
        1. Initialization
        2. Swift type compatibility
        3. Bridging collection classes
          1. NSArray to Array
          2. NSSet to set
          3. NSDictionary to dictionary
      5. Swift protocol-oriented programming
        1. Dispatching
        2. Protocol syntax
        3. Protocols as types
        4. Protocol extensions
        5. Examining protocols for use in collections
          1. Array literal syntax
          2. Making an array enumerable
            1. Sequence/IteratorProtocol
      6. Summary
    9. 3. Standing on the Shoulders of Giants
      1. Iterators, sequences, and collections
        1. Iterators
          1. Sequences
        2. Collections
      2. Stack
        1. Applications
        2. Implementation
        3. Protocols
      3. Queue
        1. Applications
        2. Implementation
        3. Protocols
      4. Circular buffer
        1. Applications
        2. Implementation
        3. Protocols
      5. Priority queue
        1. Applications
        2. Implementation
        3. Protocols
      6. StackList
        1. Applications
        2. Implementation
        3. Protocols
      7. Summary
    10. 4. Sorting Algorithms
      1. The insertion sort
        1. The algorithm
        2. Analysis of the insertion sort
        3. Use cases of the insertion sort
        4. Optimizations
      2. Merge sort
        1. The algorithm for array-based merge sort
        2. Analysis of merge sort
        3. The algorithm and analysis for linked list-based merge sort
        4. Performance comparison
      3. Quick sort
        1. The algorithm – Lomuto's implementation
        2. Analysis of Lomuto's partitioning scheme
        3. The algorithm – Hoare's implementation
        4. Analysis of Hoare's partitioning scheme
          1. Choice of pivot
            1. The wrong way – first or last element
            2. The wrong way – select random element
            3. The right way
        5. Improved pivot selection for quick sort algorithm
        6. Optimizations
      4. Summary
    11. 5. Seeing the Forest through the Tree
      1. Tree – definition and properties
      2. Overview of different types of tree
        1. Binary tree
        2. Binary search tree
        3. B-tree
        4. Splay tree
        5. Red-black tree
      3. Binary trees
        1. Types and variations
        2. Code
      4. Binary search trees
        1. Inserting a node
        2. Tree walks (traversals)
          1. Inorder tree walk
          2. Preorder tree walk
          3. Postorder tree walk
        3. Searching
        4. Deletion
      5. B-trees,
      6. Splay trees
        1. Splay operation
          1. Simple rotation or zig
          2. Zig-Zig or Zag-Zag
          3. Zig-Zag
      7. Summary
    12. 6. Advanced Searching Methods
      1. Red-black trees
      2. Red-black tree node implementation
      3. Rotations
        1. Right rotation
        2. Left rotation
      4. Insertion
      5. AVL trees
        1. AVL tree node implementation
        2. AVL tree rotations
          1. Simple rotation left
          2. Simple rotation right
          3. Double rotation – right-left
          4. Double rotation – left-right
        3. Search
        4. Insertion
      6. Trie tree
      7. Radix tree
      8. A look at several substring search algorithms
        1. Substring search algorithm examples
          1. Naive (brute force) algorithm
          2. The Rabin-Karp algorithm
      9. Summary
    13. 7. Graph Algorithms
      1. Graph theory
        1. Types of graphs
          1. Undirected graph
          2. Directed graph
          3. Weighted graph
        2. Graph representations
          1. Object-oriented approach – structs/classes
          2. Adjacency list
          3. Adjacency matrix
          4. Incidence matrix
      2. Data structures
        1. Vertex
        2. Edge
        3. Adjacency list
      3. Depth first search
      4. Breadth first search
      5. Spanning tree
        1. Minimum spanning tree
      6. Prim algorithm
      7. Shortest path
      8. Dijkstra algorithm
        1. SwiftGraph
      9. Summary
    14. 8. Performance and Algorithm Efficiency
      1. Algorithm efficiency
        1. Best, worst, and average cases
      2. Measuring efficiency and the Big-O notation
        1. Asymptotic analysis
          1. How to calculate complexities
      3. Orders of common functions
        1. O(1)
        2. O(log(n))
        3. O(n)
        4. O(nlog(n))
        5. O(n^2)
        6. O(2^n)
          1. Graphic comparison
      4. Evaluating runtime complexity
      5. Summary
    15. 9. Choosing the Perfect Algorithm
      1. URL shortener
        1. Problems with long URL
        2. URL shortener solution approach
        3. URL shortener Swift implementation
          1. Method 1 - searching for the correct tuple
          2. Method 2 – accessing the correct array position by index
      2. Searching in a huge amount of data
        1. The huge blacklist problem
        2. The huge blacklist solution approach
        3. The huge blacklist Swift implementation
          1. Method 2 – the Bloom filter solution
      3. Summary
      4. Epilogue

Product information

  • Title: Swift Data Structure and Algorithms
  • Author(s): Erik Azar, Mario Eguiluz Alebicto
  • Release date: November 2016
  • Publisher(s): Packt Publishing
  • ISBN: 9781785884504