anitsh / til

Today I Learn (til) - Github `Issues` used as daily learning management system for taking notes and storing resource links.
https://anitshrestha.com.np
MIT License
78 stars 11 forks source link

Algorithms Part 1 - Coursera MOOC #219

Open anitsh opened 4 years ago

anitsh commented 4 years ago

Notes from: https://www.coursera.org/learn/algorithms-part1

anitsh commented 4 years ago

Part I focuses on elementary data structures, sorting, and searching. Topics include union−find, binary search, stacks, queues, bags, insertion sort, selection sort, shellsort, quicksort, 3-way quicksort, mergesort, heapsort, binary heaps, binary search trees, red−black trees, separate-chaining and linear-probing hash tables, Graham scan, and kd-trees.

Part II focuses on graph and string-processing algorithms. Topics include depth-first search, breadth-first search, topological sort, Kosaraju−Sharir, Kruskal, Prim, Dijkistra, Bellman−Ford, Ford−Fulkerson, LSD radix sort, MSD radix sort, 3-way radix quicksort, multiway tries, ternary search tries, Knuth−Morris−Pratt, Boyer−Moore, Rabin–Karp, regular expression matching, run-length coding, Huffman coding, LZW compression and Burrows−Wheeler transform. Part II also introduces reductions and intractability, including the P = NP problem.

Steps to developing a usable algorithm. ・ Model the problem. ・ Find an algorithm to solve it. ・ Fast enough? Fits in memory? ・ If not, figure out why. ・ Find a way to address the problem. ・ Iterate until satisfied. How? The scientific method. Mathematical analysis.

UNION FIND Problem

image Used to solve dynamic connectivity problems. Quick Find( eager approach ) and Quick Union ( lazy approach ) algorithms are the two classical algorithms to solve this problem. The Weighted quick-union with path compression

Problem: Given a set of N objects, perform

Solution: Create data structure and implement client

Implementing the operations:

Union-find data type (API) Goal. Design efficient data structure for union-find. ・ Number of objects N can be huge. ・ Number of operations M can be huge. ・ Find queries and union commands may be intermixed.

Dynamic-connectivity client Goal. Connect the unconnected objects

Improvement of the algorithms is shown below

Algorithm Worst Case Time
quick-find M N
quick-union M N
weighted QU N + M log N
QU + path compression N + M log N
weighted QU + path compression N + M lg* N
anitsh commented 4 years ago

Algorithm Analysis

image image

Bottom line. Need linear or linearithmic alg to keep pace with Moore's law.

Reasons to analyze algorithms:

Scientific method. ・ Observe some feature of the natural world. ・ Hypothesize a model that is consistent with the observations. ・ Predict events using the hypothesis. ・ Verify the predictions by making further observations. ・ Validate by repeating until the hypothesis and observations agree.

Principles.

Empirical analysis. ・ Execute program to perform experiments. ・ Assume power law and formulate a hypothesis for running time. ・ Model enables us to make predictions.

Mathematical analysis. ・ Analyze algorithm to count frequency of operations. ・ Use tilde notation to simplify analysis. ・ Model enables us to explain behavior.

Scientific method. ・ Mathematical model is independent of a particular system; applies to machines not yet built. ・ Empirical analysis is necessary to validate mathematical models and to make predictions.

anitsh commented 4 years ago

Sorting

image

Elementary Sorts

Mergesort Java sort for objects, when memory is not of problem.

Quicksort Java sort for primitive types, when memory is to be considered.

Java System Sorts

Arrays.sort()

Q. Why use different algorithms for primitive and reference types? Basically depends on the space availability, and a choice that the programmer has to make.

System sort: Which algorithm to use?

Many sorting algorithms to choose from: Internal sorts. ・ Insertion sort, selection sort, bubblesort, shaker sort. ・ Quicksort, mergesort, heapsort, samplesort, shellsort. ・ Solitaire sort, red-black sort, splaysort, Yaroslavskiy sort, psort, ...

External sorts. Poly-phase mergesort, cascade-merge, oscillating sort.

String/radix sorts. Distribution, MSD, LSD, 3-way string quicksort.

Parallel sorts. ・ Bitonic sort, Batcher even-odd sort. ・ Smooth sort, cube sort, column sort. ・ GPUsort.

Reasons

Applications have diverse attributes. ・ Stable? ・ Parallel? ・ Deterministic? ・ Keys all distinct? ・ Multiple key types? ・ Linked list or arrays? ・ Large or small items? ・ Is your array randomly ordered? ・ Need guaranteed performance?

Binary Search Tree Algorithm Usage image