Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Introduction to
Algorithms
What is an Algorithm? An algorithm is a finite set of instructions which, if followed, accomplish a particular task.
What is an Algorithm? Finiteness: If we trace out the instructions of an algorithm, then for all cases the algorithm will terminate after a finite number of steps; Effectiveness: Every instruction must be sufficiently basic that it can in principle be carried out by a person using only pencil and paper. It is not enough that each operation be definite, but it must also be feasible.
num[10] big =0,i=0 if num[i] > big big = num[i] repeat step 3 for i=1 to 10 print big end
num[10] big =0,i=0 if num[i] > big big = num[i] repeat step 3 for i=1 to 10 print big end
TIME COMPLEXITY
Sequence of statements
statement 1; statement 2; ... statement k;
Number of Statements
=k
The total time is found by adding the times for all statements:
total time = time(statement 1) + time(statement 2) + ... + time(statement k) If each statement is "simple" (only involves basic operations) then the time for each statement is constant and the total time is also constant:
O(1).
if-then-else statements
if (condition) { sequence of statements 1 } else { sequence of statements 2 } Here, either sequence 1 will execute, or sequence 2 will execute. Therefore, the worst-case time is the slowest of the two possibilities:
O(N).
Mahindra Satyam 2010 9
for loops
for (i = 0; i < N; i++) { sequence of statements }
The loop executes N times, so the sequence of statements also executes N times.
Since we assume the statements are the total time for the for loop is
which is O(N) overall.
O(1), N * O(1),
Nested loops -
for (i = 0; i < N; i++) { for (j = 0; j < M; j++) { sequence of statements } } The outer loop executes N Every time the outer loop executes, the inner loop executes M As a result, the statements in the inner loop execute a total of N * M Thus, the complexity is O(N * M). In a common special case where the stopping condition of the inner loop is j < N instead of j < M (i.e., the inner loop also executes N times), the total complexity for the two loops is O(N2). times. times. times.
Nested loops -
Here we can't just multiply the number of iterations of the outer loop times the number of iterations of the inner loop, because the inner loop has a different number of iterations each time.
Nested loops -
The following table gives you, how many iterations that inner loop has
Value of i Number of iterations of inner loop
So the total number of times the sequence of statements executes is: N + N-1 + N-2 + ... + 3 + 2 + 1. Hence the total is O(N2).
int linearSearch(int a[],int { int i; for(i=0;i<n;i++) { if( a[i]==key) return i; } return -1; }
n,int key) 1 n n 1 1
- ignore - ignore - ignore
Unit of Time
Total
: 2n + 3
2n + 3 ~ 2n
15
Average case : 1st Input 2nd Input 3rd Input nth Input
1+2+3+n --------------------n
Complexities of some of
Technique / Case
Best Case
Average Case
Worst Case
Bubble Sort
Selection Sort Insertion Sort Merge Sort Quick Sort
Mahindra Satyam 2010
O(1)
O(1) O(1) O(n log n) O(nlog n)
O(n2)
O(n2) O(n2) O(n log n) O(n log n)
O(n2)
O(n2) O(n2) O(n log n) O(n2)
18
Technique / Case
Best Case
Average Case
Worst Case
Binary Search
O(1)
O( log n)
O( log n)
Linear Search
O(1)
O(n)
O(n)
Priority of Complexities
O(1) O(log n)
Best
Linear Time
Quadratic Time Exponential Time Factorial Time
O(n)
O(n2) O(2n) O(n!)
O(n log n)
O(n3)
Worst
Input:
Structure of data
Some Definitions
Internal Sort The data to be sorted is all stored in the computers main memory. External Sort Some of the data to be sorted might be stored in some external, slower, device. In Place Sort The amount of extra space required to sort the data is constant with the input size.
Mahindra Satyam 2010 24
Stability
Sort file on second key: Records with key value 3 are not in order on first key!!
Insertion Sort
Idea: like sorting a hand of playing cards Start with an empty left hand and the cards facing down on the table. Remove one card at a time from the table, and insert it into the correct position in the left hand compare it with each of the cards already in the hand, from right to left The cards held in the left hand are sorted these cards were originally the top cards of the pile on the table
Insertion Sort
To insert 12, we need to make room for it by moving first 36 and then 24.
Insertion Sort
Insertion Sort
Insertion Sort
input array
right sub-array
sorted
unsorted
Insertion Sort
Bubble Sort
0 PASS - 1 5
1 7
2 6
3 4
If the element at (j+1)th position is larger than jth element, interchange occurs
temp
0 PASS - 1 5
1 6
2 4
3 3
4 7 In second pass, the adjacent elements are compared excluding the last sorted element.
PASS - 2
temp
0 PASS - 1 5
1 6
2 4
3 3
4 7 In third pass, the adjacent elements are compared excluding the last sorted element.
PASS - 2
PASS - 3
temp
0 PASS - 1 5
1 6
2 4
3 3
4 7 In fourth pass, the adjacent elements are compared excluding the last sorted element.
PASS - 2
PASS - 3
PASS - 4
temp
36
Analysis
0 5 1 7 2 6 3 4 4 3 (n-1) No. of comparisons
PASS - 1
PASS - 2
(n-2)
PASS - 3
7 (1)
PASS - 4
Thus, the total no. of comparisons for the elements to be arranged in order are
Mahindra Satyam 2010
Merge SORT
Problem: Input: A[n] unsorted array of n 1 integers. Output: A[n] sorted in non-decreasing order
Recursively
sort FirstPart sort SecondPart
A:
FirstPart
SecondPart
FirstPart
SecondPart
Merge
A is sorted!
Merge-Sort (A, n) if n=1 return else Space: n n1 n2 n/2 create array L[n1], R[n2] for i 0 to n1-1 do L[i] A[i] for j 0 to n2-1 do R[j] A[n1+j] Merge-Sort(L, n1) Recursive Call Merge-Sort(R, n2) Merge(A, L, n1, R, n2 )
Mahindra Satyam 2010 43
Time: n
Merge-Sort: Merge
Sorted
A: merge
Sorted
Sorted
L:
Mahindra Satyam 2010
R:
44
A:
L:
R: 3
A:
3 1
k=0
15 28
10 14
L:
1 3
i=0
Mahindra Satyam 2010
R:
15 28 30 2 6 8 3 6
j=0
46
10 14 22 4 5 7
A:
1 2 5
k=1
15 28 30
10 14
L:
1 3 2 5
i=1
Mahindra Satyam 2010
R:
15 28 6 8 3 6
j=0
47
10 14 22 4 5 7
A:
1 2 3 15 28 30
k=2
10 14
L:
1 2 6
i=2
Mahindra Satyam 2010
R:
8 3 6
j=0
48
10 14 22 4 5 7
A:
1 2 3 4
k=3
10 14
L:
1 2 6
i=2
Mahindra Satyam 2010
R:
8 3 6 10 14 22 4 5 7
j=1
49
A:
1 2 3 4 5
k=4
10 14
L:
1 2 6
i=2
Mahindra Satyam 2010
R:
8 3 6 10 14 22 4 5 7
j=2
50
A:
1 2 3 4 5 6
k=5
10 14
L:
1 2 6
i=2
Mahindra Satyam 2010
R:
8 3 6 10 14 22 4 5 7
j=3
51
A:
1 2 3 4 5 6 7
k=6
14
L:
1 2 6 8
i=3
Mahindra Satyam 2010
R:
3 6 10 14 22 4 5 7
j=3
52
A:
1 2 3 4 5 5 7 8 14
k=7
L:
1 3 2 5 15 28 6 8
i=3
Mahindra Satyam 2010
R:
3 6 10 14 22 4 5 7
j=4
53
A:
1 2 3 4 5 6 7 8
k=8
L:
1 3 2 5 15 28 6 8
i=4
Mahindra Satyam 2010
R:
3 6 10 14 22 4 5 7
j=4
54
merge(A,L,n1,R,n2)
Number of iterations: (n1+n2) i j 0 for k 0 to n1+n2-1 time: c(n1+n2) for some c Total if i < n1 if j = n2 or L[i] R[j] A[k] L[i] i i + 1 else if j < n2 A[k] R[j] j j + 1
88
44
Merge-Sort Analysis
Time, divide
n n
n/2
n/2
n/4
n/4
n/4
n/4
4 n/4 = n
Merge-Sort Analysis
Time, merging
n cn
n/2
n/2
n/4
n/4
n/4
n/4
4 cn/4 = n
Quick sort
Quick Sort
It is based on the divide and conquer paradigm. Divide: The array A[p..r] is partitioned (rearranged) into nonempty sub arrays A[p..q] and A[q+1..r] such that each element of A[p..q] is less than or equal to each element of A[q+1..r]. The index q is computed as a part of this partitioning.
Conquer: Two sub arrays A[p..q] and A[q+1..r] are sorted by recursive calls to quick sort.
Combine: Since the sub arrays are sorted in place, no work is needed to combine them, the entire array A[p..r] is now sorted.
QUICKSORT (A, p, r) 1. 2. 3. 4. if p < r then q PARTITION (A, p, r) QUICKSORT (A, p, q) QUICKSORT (A, q+1, r)
lb 0 1 2 3 4 5 6
ub 7 13
11
12
14
11
pivot
7 13
11
14
12
11
pivot
Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7
11
12
14
13
11
pivot
down
up
Set pivot as the first element in the list, pivot = x[lb] Set ^down to the first element (^lb), and ^up to the last element (^ub), which hypothetically considered is set to infinity
Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7
11
12
14
13
11
pivot
down
up
up--;
Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7
11
12
14
13
11
pivot
down
up
up--;
Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7
11
12
14
13
11
pivot
down
up
up--;
Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7
11
14
12
13
11
pivot
down
up
up--;
Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7
11
14
12
13
11
pivot
down
up
while(down < up) { while(x[down] <= pivot) down++; while(x[up] > pivot) up--;
Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7
11
14
12
13
11
pivot
down
up
up--;
Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7
11
14
12
13
11
pivot
down
up
up--;
Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7
11
14
12
13
11
pivot
down
up
up--;
Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7
11
14
12
13
11
pivot
down
up
up--;
Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7
11
14
12
13
11
pivot
down up
up--;
Partition Algorithm
lb 0 1 2 3 4 5 6 ub 7
11
14
12
13
11
pivot
up
down
up--;
Partition Algorithm lb 0 1 2 3 4 5 6 ub 7
11
14
12
13
11
pivot
up
down
up--;
Partition Algorithm lb 0 1 2 3 4 5 6 ub 7
11
14
12
13
11
pivot
up
down
up--;
Quick Sort
7 13
pivot
11
12
14
11
11
14
12
13
Quick Sort
11
12
14
13
11
14
12
13
Quick Sort
0 1 2 3 4 5 6 7 13
11
12
14
11
14
12
13
Quick Sort
0 1 2 3 4 5 6 7 13
11
12
14
11
14
12
13
1
Mahindra Satyam 2010 101
Datastructures
Data structure
A data structure is an aggregation of atomic and composite data into a set with defined relationships. Structure means a set of rules that holds data together.
Data structure
A combination of elements in which each is either a data type or another data structure. A set of associations or relationships (structure) involving the combined elements.
Data Structure
Abstraction
The concept of abstraction means We know what a data type can do How it is done is hidden
operations
Data structure
1. 2. 3. 4.
1. One data structure that meets these requirements is the hash table (chapter 9). 2. Records are accessible by account number (called an exact-match query) 3. Hash tables allow for extremely fast exact-match search. 4. Hash tables also support efficient insertion of new records. 5. Deletions can also be supported efficiently (but too many deletions lead to some degradation in performance requiring the hash table to be reorganized).
1. Database system for cities and towns. 2. Users find information about a particular place by name (exact-match query) 3. Users also find all places that match a particular value (or range of values), such as location or population size (called a range query).
The database must answer queries quickly enough to satisfy the patience of a typical user. For an exact-match query, a few seconds is satisfactory For a range queries, the entire operation may be allowed to take longer, perhaps on the order of a minute.
The hash table is inappropriate for implementing the city database because: It cannot perform efficient range queries
The B+ tree (section 10) supports large databases: Insertion Deletion Range queries If the database is created once and then never changed, a simple linear index would be more appropriate.
Select a data structure as follows: 1. Analyze the problem to determine the resource constraints a solution must meet. 2. Determine the basic operations that must be supported. Quantify the resource constraints for each operation. 3. Select the data structure that best meets these requirements.
1. Are all data inserted into the data structure at the beginning, or are insertions intersparsed with other operations? 2. Can data be deleted? 3. Are all data processed in some well-defined order, or is random access allowed?
Each data structure has costs and benefits. Rarely is one data structure better than another in all situations. A data structure requires: space for each data item it stores, time to perform each basic operation, programming effort.
Each problem has constraints on available space and time. Only after a careful analysis of problem characteristics can we know the best data structure for the task.
Bank example: Start account: a few minutes Transactions: a few seconds Close account: overnight
Linked List
Objectives
1. 2.
Understanding efficient memory utilization with Linked representation Time complexities with respect to insertion, deletion and search
10
200
20
300
30
NUL L
100
200
300
Consider a linked list of 3 nodes. Let first be the pointer to first node. Let us consider inserting a new node ( with data as 5 ) at the front.
curr
10
200
20
300
30
NULL
100
5
NU LL
200
300
150
3. first = curr
125
10
200
20
300
30
NUL L
100
200
300
10
200
20
300
30
NUL L
100
200
300
10
200
20
300
30
NUL L
100
200
300
Take another pointer temp (t) which is also pointing to first node.
10
200
20
300
30
NUL L
100
200
300
10
200
20
300
30
NUL L
100
200
300
10
200
20
300
30
NUL L
100
200
300
10
200
20
300
30
NULL
100
200
300
40
NULL
400
r->data = 40
r->next = NULL
Mahindra Satyam 2010 132
10
200
20
300
30
NULL
100
200
300
40
NUL L
400
10
200
20
300
30
NUL L
100
200
300
Consider a linked list of 3 nodes. Let first be the pointer to first node.
Let us consider the insertion of a new node (data = 25) at position 3 (i.e. before node 30)
10
200
20
300
30
NULL
100
200
300
Take two pointers temp (t) and old (o) pointing to first node. Navigate the pointers temp and old such that temp points to the correct position (sorted position ) for the new element and old points to its previous node.
10
200
20
300
30
NULL
25
100
200
300
10
200
20
300
30
NULL
25
100
200
300
10
200
20
300
30
NULL
25
100
200
300
10
200
20
300
X
curr 25
30
NULL
100
200
300
NULL
400
curr = getNode(val); curr -> next = t;
Mahindra Satyam 2010
o->next = curr;
139
STACKS
Stacks
Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); void *pop( Stack s ); Like a plate stacker Other methods
int IsEmpty( Stack s ); /* Return TRUE if empty */ void *Top( Stack s ); /* Return the item at the top, without deleting it */
Mahindra Satyam 2010 141
Stacks - Implementation
Arrays Provide a stack capacity to the constructor Flexibility limited but matches many real uses Capacity limited by some constraint Memory in your computer Size of the plate stacker, etc push, pop methods Linked list also possible Stack: basically a Collection with special semantics!
Stacks - Relevance
Stacks appear in computer programs Key to call / return in functions & procedures Stack frame allows recursive calls Call: push stack frame Return: pop stack frame Stack frame Function arguments Return address Local variables
in HLL
function f( int x, int y) { int a; if ( term_cond ) return ; a = .; return g( a ); } function g( int z ) { int p, q; p = . ; q = . ; return f(p,q); }
Context for execution of f
Mahindra Satyam 2010 144
Recursion
Very useful technique Definition of mathematical functions Definition of data structures Recursive structures are naturally processed by recursive functions!
Recursion
Recursively defined functions factorial Fibonacci GCD by Euclids algorithm Fourier Transform Games
Towers of Hanoi Chess
Recursion - Example
Fibonacci Numbers
Pseudo-code
Recursion - Example
Fibonacci Numbers
However, many recursive functions, eg binary search, are simple, elegant and efficient!
QUEUES
Queue Overview
Queue ADT Basic operations of queue Enqueuing, dequeuing etc. Implementation of queue Array Linked list
Queue ADT
Like a stack, a queue is also a list. However, with a queue, insertion is done at one end, while deletion is performed at the other end. Accessing the elements of queues follows a First In, First Out (FIFO) order. Like customers standing in a check-out line in a store, the first customer in is the first customer served.
Another form of restricted list Insertion is done at one end, whereas deletion is performed at the other end Basic operations: enqueue: insert an element at the rear of the list dequeue: delete the element at the front of the list
Primary queue operations: Enqueue and Dequeue Like check-out lines in a store, a queue has a front and a rear. Enqueue Insert an element at the rear of the queue Dequeue Remove an element from the front of the queue
Remove (Dequeue)
Mahindra Satyam 2010
front
rear
Insert (Enqueue)
153
Implementation of Queue
Just as stacks can be implemented as arrays or linked lists, so with queues. Dynamic queues have the same advantages over static queues as dynamic stacks have over static stacks
There are several different algorithms to implement Enqueue and Dequeue Nave way When enqueuing, the front index is always fixed and the rear index moves forward in the array.
rear
rear
rear
front Enqueue(3)
Mahindra Satyam 2010
front Enqueue(6)
front Enqueue(9)
155
Nave way When enqueuing, the front index is always fixed and the rear index moves forward in the array. When dequeuing, the element at the front the queue is removed. Move all the elements after it by one position. (Inefficient!!!)
rear
rear
rear = -1
front Dequeue()
Mahindra Satyam 2010
front
front Dequeue()
156
Dequeue()
TREES
Church of
England Cabinet
House of Commons
House of Lords
Supreme Court
Ministers County Council Metropolitan police Rural District Council County Borough Council
Definition of Tree
A tree is a finite set of one or more nodes such that: There is a specially designated node called the root. The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree. We call T1, ..., Tn the subtrees of the root.
node (13) degree of a node 1 leaf (terminal) A 1 3 nonterminal 2 parent B 2 1 C 2 3 D 2 children 3 2 sibling degree of a tree (3) 4 2 E 3 0 F 3 0 G 31 H 3 0 I 3 0 J 3 ancestor level of a node height of a tree (4) 0 K4 0 L 4 0 M 4
Terminology
The degree of a node is the number of subtrees of the node
The node with degree 0 is a leaf or terminal node. A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the children of the node. Children of the same parent are siblings. The ancestors of a node are all the nodes along the path from the root to the node.
Tree Properties
Property Number of nodes Height Root Node Leaves Interior nodes Number of levels Ancestors of H Descendants of B Siblings of E Right subtree Value
Representation of Trees
List Representation
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) ) The root comes first, followed by a list of sub-trees
data
link 1
link 2
...
link n
A Tree Node
Every tree node: object useful information children pointers to its children nodes
O
Mahindra Satyam 2010 165
A left child B
data
right sibling
Tree ADT
Objects: any type of objects can be stored in a tree Methods: accessor methods root() return the root of the tree parent(p) return the parent of a node children(p) returns the children of a node query methods size() returns the number of nodes in the tree isEmpty() - returns true if the tree is empty elements() returns all elements isRoot(p), isInternal(p), isExternal(p)
Tree Implementation
typedef struct tnode { int key; struct tnode* lchild; struct tnode* sibling; } *ptnode; Create a tree with three nodes (one root & two children) Insert a new node (in tree with root R, as a new child at level L) Delete a node (in tree with root R, the first child at level L)
Tree Traversal
Two main methods: Preorder Postorder Recursive definition PREorder: visit the root traverse in preorder the children (subtrees) POSTorder traverse in postorder the children (subtrees) visit the root
Thank you
mahindrasatyam.com
Safe Harbor This document contains forward-looking statements within the meaning of section 27A of Securities Act of 1933, as amended, and section 21E of the Securities Exchange Act of 1934, as amended. The forward-looking statements contained herein are subject to certain risks and uncertainties that could cause actual results to differ materially from those reflected in the forward-looking statements. We undertake no duty to update any forward-looking statements. For a discussion of the risks associated with our business, please see the discussions under the heading Risk Factors in our report on Form 6-K concerning the quarter ended September 30, 2008, furnished to the Securities and Exchange Commission on 07 November, 2008, and the other reports filed with the Securities and Exchange Commission from time to time. These filings are available at http://www.sec.gov