cubeit tech - algorithms

26
Introduction to Algorithms

Upload: kirill-suslov

Post on 11-Feb-2017

256 views

Category:

Software


0 download

TRANSCRIPT

Page 1: CubeIT Tech - Algorithms

Introduction to Algorithms

Page 2: CubeIT Tech - Algorithms

Why do you need algorithms?Good algorithmic training is VERY important for the programmer, but you don’t need to memorize all the algorithmsUnderstanding algorithms gives you:1) Ability to solve incomprehensible problems2) Habit to analyze the effectiveness of each of your solutions3) Ability to skillfully use the existing tools

One more reason: jobs

Page 3: CubeIT Tech - Algorithms

Outline•Definition of algorithm• Formal properties of algorithms• Some classification of algorithms•Complexity of algorithms•ALGORITHMS

Page 4: CubeIT Tech - Algorithms

Disclaimer

Some of the terms and words Google-translated.Sorry.

Source code is availigle on GitHub

Page 5: CubeIT Tech - Algorithms

Definition of algorithm

Simple: Algorithm is a set of rules for solving a problem in a finite number of steps

Less simple:Algorithm is a finite set of unambiguous instructions that, given some set of initial conditions, can be performed in a prescribed sequence to achieve a certain goal and that has a recognizable set of end conditions.

Page 6: CubeIT Tech - Algorithms

Formal properties of algorithms1) Finiteness2) Discreteness3) Clarity4) Accuracy5) Mass character6) Definitiveness

Page 7: CubeIT Tech - Algorithms

FinitenessFiniteness (or effectiviness)

means that algorithm has a finite number of steps to received result.

Page 8: CubeIT Tech - Algorithms

DiscretenessDiscreteness

means that algorithm has to be broken down into a sequence of steps performed

Page 9: CubeIT Tech - Algorithms

ClarityClarity

means that algorithm must contain only the commands that are part of a set of commands that can perform a particular executor

Page 10: CubeIT Tech - Algorithms

AccuracyAccuracy

means that any step of an algorithm should be definite and unambiguous

Page 11: CubeIT Tech - Algorithms

Mass characterMass character

means that once algorithm is made up it must solve similar problems with different input data

Page 12: CubeIT Tech - Algorithms

DefinitivenessDefinitiveness

means that the same set of input data will produce the same result, i.e. the result is uniquely determined by the initial data.

Page 13: CubeIT Tech - Algorithms

Test yourselfI want to cut an orange. What’s the plan?

Page 14: CubeIT Tech - Algorithms

Example of algorithm:How to cut an orange

Page 15: CubeIT Tech - Algorithms

ClassificationCouple of categories of algorithms (there are a plenty of them)

•Deterministic / Randomized• Exact / Approximation• Sequential / Parallel• etc.

Page 16: CubeIT Tech - Algorithms

Complexity of algorithms1) Confusing explanation 2) Clear explanation (must read)

The idea is Big-O notation

Big-O notation is a relative representation of the complexity of an algorithm.

Page 17: CubeIT Tech - Algorithms
Page 18: CubeIT Tech - Algorithms

Algorithms: Strings/arraysAlgorithm #1: Remove Element

Given an array and a value, remove all instances of that value in place and return the new length. (Note: The order of elements can be changed. It doesn't matter

what you leave beyond the new length.)

Page 19: CubeIT Tech - Algorithms

Algorithms: Strings/arraysAlgorithm #2: Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array.

[1,3,5,6], 5 -> 2 [1,3,5,6], 2 -> 1 [1,3,5,6], 7 -> 4[1,3,5,6], 0 -> 0

Page 20: CubeIT Tech - Algorithms

Algorithms: Strings/arraysAlgorithm #3: Anagrams

Given an array of strings, return all groups of strings that are anagrams.

An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; for example Torchwood can be rearranged into Doctor Who.If two strings are anagram to each other, their sorted sequence is the same. Therefore, this problem can be seen as a problem of

finding duplicate elements.

Page 21: CubeIT Tech - Algorithms

Algorithms: MatrixAlgorithm: Rotate Image

You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).

Page 22: CubeIT Tech - Algorithms

Algorithms: Linked ListLinked list is a linear collection of data elements, called nodes pointing to the next node by means of pointer

Algorithm: Reverse Linked List

Reverse a singly linked list.

Page 23: CubeIT Tech - Algorithms

Algorithms: Binary TreeBinary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child

Algorithm: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree.

Page 24: CubeIT Tech - Algorithms

Algorithms: SortingСhances that you will need to write a sorting algorithm are close to zero.

http://www.sorting-algorithms.com/https://www.youtube.com/watch?v=kPRA0W1kECg

Page 25: CubeIT Tech - Algorithms

Today:• Learn some theory (definition properties, complexity)•Overview several algorithms

Page 26: CubeIT Tech - Algorithms

Extra resources1) http://www.programcreek.com/2012/11/top-10-algorithms-for-cod

ing-interview/