ada chap 1

51
06CSE43— Analysis and Design of Algorithms ISE 4 TH SEM

Upload: ayesha-ahmad

Post on 15-Oct-2014

271 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: ADA Chap 1

06CSE43— Analysis and Design of Algorithms

ISE 4TH SEM

Page 2: ADA Chap 1

VANDANA M. LADWANI 2

Why Study Algorithms? Al Khawarizmi

“A great Iranian mathematician, geographer and astronomer. He introduced the zero, negative numbers, algebra, and the decimal system to the West. He also invented mathematical programming using a set of instructions to perform complex calculations. The term algorithm is named after a variation of his name, Algorithmi. “

What is it? Briefly speaking, algorithms are procedure solution to problems. Algorithms are not answers but rather precisely defined

procedures for getting answers. (Example of sorting 3 numbers.) Cornerstone of computer science. Programs will not exist

without algorithms. Algorithm design techniques, or problem-solving strategies,

are useful in fields beyond computer science.

Page 3: ADA Chap 1

VANDANA M. LADWANI 3

Algorithms An algorithm is a sequence of unambiguous instructions for

solving a computational problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time.

“computer”

problem

algorithm

input output

Page 4: ADA Chap 1

VANDANA M. LADWANI 4

Example of Computational Problem: Sorting

Statement of problem: Input: A sequence of n numbers <a1, a2, …, an>

Output: A reordering of the input sequence <a´1, a´

2, …, a´n> so

that a´i ≤ a´

j whenever i < j

Instance: The sequence <5, 3, 2, 8, 3>

Algorithms: Selection sort Insertion sort Merge sort (many others)

Page 5: ADA Chap 1

VANDANA M. LADWANI 5

Properties of Algorithms What distinguish an algorithm from a recipe, process,

method, technique, procedure, routine…? Finiteness

terminates after a finite number of steps Definiteness

Each step must be rigorously and unambiguously specified.-e.g., ”stir until lumpy”

Input Valid inputs must be clearly specified.

OutputThe data that result upon completion of the algorithm must be

specified. Effectiveness

Steps must be sufficiently simple and basic.

Page 6: ADA Chap 1

VANDANA M. LADWANI 6

Examples Is the following a legitimate

algorithm?

i 1

While (i <= 10) do

a i + 1

Print the value of a

End of loop

Stop

Page 7: ADA Chap 1

VANDANA M. LADWANI 7

Why Study Algorithms? Theoretical importance

The core of computer science Practical importance

A practitioner’s toolkit of known algorithms

Framework for designing and analyzing algorithms for new problems

Page 8: ADA Chap 1

VANDANA M. LADWANI 8

Examples of Algorithms – Computing the Greatest Common Divisor of Two Integers

gcd(m, n): the largest integer that divides both m and n.

First try -- Euclid’s algorithm: gcd(m, n) = gcd(n, m mod n) Step1: If n = 0, return the value of m as the

answer and stop; otherwise, proceed to Step 2. Step2: Divide m by n and assign the value of

the remainder to r. Step 3: Assign the value of n to m and the value

of r to n. Go to Step 1.

Page 9: ADA Chap 1

VANDANA M. LADWANI 9

Methods of Specifying an Algorithm

Natural language Ambiguous

“Mike ate his sandwich at 9:00 AM.” Pseudocode

A mixture of a natural language and programming language-like structures

Precise and succinct. Pseudocode in this course

omits declarations of variables use indentation to show the scope of such statements as

for, if, and while. use for assignment

Page 10: ADA Chap 1

VANDANA M. LADWANI 10

Pseudocode of Euclid’s Algorithm

Algorithm Euclid(m, n)//Computes gcd(m, n) by Euclid’s algorithm//Input: Two nonnegative, not-both-zero integers m and n //Output: Greatest common divisor of m and n while n ‡ 0 do

r m mod nm nn r

return m Questions:

Finiteness: how do we know that Euclid’s algorithm actually comes to a stop?

Definiteness: nonambiguity Effectiveness: effectively computable.

Page 11: ADA Chap 1

VANDANA M. LADWANI 11

Second Try for gcd (m, n) Consecutive Integer Algorithm

Step1: Assign the value of min{m, n} to t.

Step2: Divide m by t. If the remainder of this division is 0, go to Step3;otherwise, go to Step 4.

Step3: Divide n by t. If the remainder of this division is 0, return the value of t as the answer and stop; otherwise, proceed to Step4.

Step4: Decrease the value of t by 1. Go to Step2. Questions

Finiteness Definiteness Effectiveness Which algorithm is faster, the Euclid’s or this one?

Page 12: ADA Chap 1

VANDANA M. LADWANI 12

Third try for gcd(m, n) Middle-school procedure

Step1: Find the prime factors of m.

Step2: Find the prime factors of n.

Step3: Identify all the common factors in the two prime expansions found in Step1 and Step2. (If p is a common factor occurring Pm and Pn times in m and n, respectively, it should be repeated in min{Pm, Pn} times.)

Step4: Compute the product of all the common factors and return it as the gcd of the numbers given.

Question Is this a legitimate algorithm?

Page 13: ADA Chap 1

VANDANA M. LADWANI 13

What can we learn from the previous 3 examples?

Each step of an algorithm must be unambiguous.

The same algorithm can be represented in several different ways. (different pseudo codes)

There might exists more than one algorithm for a certain problem.

Algorithms for the same problem can be based on very different ideas and can solve the problem with dramatically different speeds.

Page 14: ADA Chap 1

VANDANA M. LADWANI 14

Algorithm design and analysis process

Understand the problem

Computational means, exact vs. Approximate solving, data structure,Algorithm design technique

Design an algorithm

Prove correctness

Analyze the algorithm

Code the algorithm

Page 15: ADA Chap 1

VANDANA M. LADWANI 15

Fundamentals of Algorithmic Problem Solving

Understanding the problem Asking questions, do a few examples by hand, think about

special cases, etc. Deciding on

Exact vs. approximate problem solving Appropriate data structure

Design an algorithm Proving correctness Analyzing an algorithm

Time efficiency : how fast the algorithm runs Space efficiency: how much extra memory the algorithm

needs. Simplicity and generality

Coding an algorithm

Page 16: ADA Chap 1

VANDANA M. LADWANI 16

Algorithm design strategies

Brute force

Divide and conquer

Decrease and conquer

Transform and conquer

Greedy approach

Dynamic programming

Backtracking and Branch and bound

Space and time tradeoffs

Page 17: ADA Chap 1

VANDANA M. LADWANI 17

Algorithm Design Strategies Brute force (exhaustive search)

A straightforward way to solving a problem

Just do it! Doing a thing well is often a

waste of time. by Robert Byrne Exponential problem Bubble sort

Page 18: ADA Chap 1

VANDANA M. LADWANI 18

Algorithm Design Strategies Divide and conquer

A general algorithm design technique that solves a problem’s instance by dividing it into several smaller instances.

Solving each of them recursively. Then combining their solutions to get

a solution to the original instance of the problem.

Page 19: ADA Chap 1

VANDANA M. LADWANI 19

Algorithm Design Strategies Decrease and conquer

A general algorithm design technique based on exploiting a relationship between a solution to a given instance of a problem and a solution to a smaller instance of the same problem.

Page 20: ADA Chap 1

VANDANA M. LADWANI 20

Algorithm Design Strategies Transform and conquer

A set of techniques based on the transformation to a problem that is easier to solve.

That’s the secret to life … replace one worry with another. (Charles Schulz)

Page 21: ADA Chap 1

VANDANA M. LADWANI 21

Algorithm Design Strategies Greedy approach

Suggests constructing a solution to an optimization problem through a sequence of steps until a complete solution is reached.

Examples: Prim’s algorithm (MST) Shortest Path Problem, etc

Page 22: ADA Chap 1

VANDANA M. LADWANI 22

Algorithm Design Strategies Dynamic programming

A technique solving problems with overlapping subproblems. Typically, these subproblems arise from a recurrence relating a solution to a given problem with smaller subproblems.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34 F(n)=f(n-1)+f(n-2)

Page 23: ADA Chap 1

VANDANA M. LADWANI 23

Algorithm Design Strategies Backtracking and Branch and

Bound For problems in which the number of

choices grows at least exponentially with their instance size.

Construct a solution one at a time, trying to terminate the process as soon as one can ascertain that no solution can be obtained as a result of the choice already made.

Page 24: ADA Chap 1

VANDANA M. LADWANI 24

Algorithm Design Strategies Backtracking

Page 25: ADA Chap 1

VANDANA M. LADWANI 25

Page 26: ADA Chap 1

VANDANA M. LADWANI 26

Algorithm Design Strategies Branch and Bound

Provide a bound on the best value of the objective function

The value of the best solution

Page 27: ADA Chap 1

VANDANA M. LADWANI 27

ASSIGNMENT Problem

Page 28: ADA Chap 1

VANDANA M. LADWANI 28

Page 29: ADA Chap 1

VANDANA M. LADWANI 29

Algorithm Design Strategies

Space and time tradeoffs Type I: input enhancement

Preprocess the problem’s input, in whole or in part, and store the additional information obtained to accelerate solving the problem.

Type II: pre-structuring Simply uses extra space to facilitate faster

and/or more flexible access to the data.

Page 30: ADA Chap 1

VANDANA M. LADWANI 30

Important Problem Types Sorting Searching String processing Graph problems

Page 31: ADA Chap 1

VANDANA M. LADWANI 31

Sorting (I) Rearrange the items of a given list in

ascending order. Input: A sequence of n numbers <a1, a2, …, an> Output: A reordering <a´

1, a´2, …, a´

n> of the input sequence such that a´

1≤ a´2 ≤ … ≤ a´

n.

Why sorting? Help searching Algorithms often use sorting as a key subroutine.

Sorting key A specially chosen piece of information used to

guide sorting. I.e., sort student records by names.

Page 32: ADA Chap 1

VANDANA M. LADWANI 32

Sorting (II) Examples of sorting algorithms

Selection sort Bubble sort Insertion sort Merge sort Heap sort …

Evaluate sorting algorithm complexity: the number of key comparisons.

Two properties Stability: A sorting algorithm is called stable if it preserves

the relative order of any two equal elements in its input. In place : A sorting algorithm is in place if it does not require

extra memory, except, possibly for a few memory units.

Page 33: ADA Chap 1

VANDANA M. LADWANI 33

Selection SortAlgorithm SelectionSort(A[0..n-1])//The algorithm sorts a given array by selection sort//Input: An array A[0..n-1] of orderable elements//Output: Array A[0..n-1] sorted in ascending orderfor i 0 to n – 2 do

min ifor j i + 1 to n – 1 do

if A[j] < A[min] min j

swap A[i] and A[min]

Page 34: ADA Chap 1

VANDANA M. LADWANI 34

Searching Find a given value, called a search key,

in a given set. Examples of searching algorithms

Sequential searching Binary searching…

Page 35: ADA Chap 1

VANDANA M. LADWANI 35

String Processing A string is a sequence of

characters from an alphabet. Text strings: letters, numbers, and

special characters. String matching: searching for a

given word/pattern in a text.

Page 36: ADA Chap 1

VANDANA M. LADWANI 36

Graph Problems Informal definition

A graph is a collection of points called vertices, some of which are connected by line segments called edges.

Modeling real-life problems Modeling WWW communication networks Project scheduling …

Examples of graph algorithms Graph traversal algorithms Shortest-path algorithms Topological sorting

Page 37: ADA Chap 1

VANDANA M. LADWANI 37

Computational Problems Shortest paths in a graph

To find the distances from each vertex to all other vertices.

Page 38: ADA Chap 1

VANDANA M. LADWANI 38

Computational Problems Minimum spanning tree

A spanning tree of a connected graph is its connected acyclic sub graph (i.e. a tree).

Page 39: ADA Chap 1

VANDANA M. LADWANI 39

Fundamental Data Structures Linear data structures Stacks, queues, and heaps Graphs Trees

Page 40: ADA Chap 1

VANDANA M. LADWANI 40

Linear Data Structures Arrays

A sequence of n items of the same data type that are stored contiguously in computer memory and made accessible by specifying a value of the array’s index.

Linked List A sequence of zero or more

nodes each containing two kinds of information: some data and one or more links called pointers to other nodes of the linked list.

Singly linked list (next pointer) Doubly linked list (next +

previous pointers)

Arrays fixed length (need

preliminary reservation of memory)

contiguous memory locations direct access Insert/delete

Linked Lists dynamic length arbitrary memory locations access by following links Insert/delete

Page 41: ADA Chap 1

VANDANA M. LADWANI 41

Stacks, Queues, and Heaps (1)

Stacks A stack of plates

insertion/deletion can be done only at the top. LIFO

Two operations (push and pop) Queues

A queue of customers waiting for services Insertion/enqueue from the rear and

deletion/dequeue from the front. FIFO

Two operations (enqueue and dequeue)

Page 42: ADA Chap 1

VANDANA M. LADWANI 42

Stacks, Queues, and Heaps (2)

Priority queues (implemented using heaps)

A data structure for maintaining a set of elements, each associated with a key/priority, with the following operations

Finding the element with the highest priority Deleting the element with the highest priority Inserting a new element

Scheduling jobs on a shared computer.

Page 43: ADA Chap 1

VANDANA M. LADWANI 43

Graphs Formal definition

A graph G = <V, E> is defined by a pair of two sets: a finite set V of items called vertices and a set E of vertex pairs called edges.

Undirected and directed graphs (digraph). What’s the maximum number of edges in

an undirected graph with |V| vertices? Complete, dense, and sparse graph

A graph with every pair of its vertices connected by an edge is called complete. K|V|

Page 44: ADA Chap 1

VANDANA M. LADWANI 44

Graph Representation Adjacency matrix

n x n boolean matrix if |V| is n. The element on the ith row and jth column is 1 if

there’s an edge from ith vertex to the jth vertex; otherwise 0.

The adjacency matrix of an undirected graph is symmetric.

Adjacency linked lists A collection of linked lists, one for each vertex, that

contain all the vertices adjacent to the list’s vertex. Which data structure would you use if the graph

is a 100-node star shape?

Page 45: ADA Chap 1

VANDANA M. LADWANI 45

Weighted Graphs Weighted graphs

Graphs or digraphs with numbers assigned to the edges.

Page 46: ADA Chap 1

VANDANA M. LADWANI 46

Graph Properties -- Paths and Connectivity

Paths A path from vertex u to v of a graph G is defined as a

sequence of adjacent (connected by an edge) vertices that starts with u and ends with v.

Simple paths: All edges of a path are distinct. Path lengths: the number of edges, or the number of

vertices – 1. Connected graphs

A undirected graph is said to be connected if for every pair of its vertices u and v, there is a path from u to v.

Connected component The maximum connected sub graph of a given graph.

Page 47: ADA Chap 1

VANDANA M. LADWANI 47

Graph Properties -- Acyclicity Cycle

A simple path of a positive length that starts and ends a the same vertex.

Acyclic graph A graph without cycles DAG (Directed Acyclic Graph)

Page 48: ADA Chap 1

VANDANA M. LADWANI 48

Trees (I) Trees

A tree (or free tree) is a connected acyclic graph. Forests: a graph that has no cycles but is not

necessarily connected. Properties of trees

For every two vertices in a tree there always exists exactly one simple path from one of these vertices to the other. Why?

Rooted trees: The above property makes it possible to select an arbitrary vertex in a free tree and consider it as the root of the so-called rooted tree.

Levels of rooted tree.

|E| = |V| - 1

Page 49: ADA Chap 1

VANDANA M. LADWANI 49

Trees (II) ancestors

For any vertex v in a tree T, all the vertices on the simple path from the root to that vertex are called ancestors.

descendants All the vertices for which a vertex v is an ancestor are said to be

descendants of v. parent, child and siblings

If (u, v) is the last edge of the simple path from the root to vertex v (and u ‡ v), u is said to be the parent of v and v is called a child of u.

Vertices that have the same parent are called siblings. Leaves

A vertex without children is called a leaf. Subtree

A vertex v with all its descendants is called the subtree of T rooted at v.

Page 50: ADA Chap 1

VANDANA M. LADWANI 50

Trees (III) Depth of a vertex

The length of the simple path from the root to the vertex.

Height of a tree The length of the longest simple path from

the root to a leaf.

Page 51: ADA Chap 1

VANDANA M. LADWANI 51

Ordered Trees Ordered trees

An ordered tree is a rooted tree in which all the children of each vertex are ordered.

Binary trees A binary tree is an ordered tree in which every vertex has no

more than two children and each children is designated s either a left child or a right child of its parent.

Binary search trees Each vertex is assigned a number. A number assigned to each parental vertex is larger than all the

numbers in its left subtree and smaller than all the numbers in its right subtree.

log2n h n – 1, where h is the height of a binary tree.