SlideShare une entreprise Scribd logo
1  sur  214
Algorithm Analysis & Data Structures Jaideep Srivastava
Schedule of topics ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Introduction ,[object Object],[object Object],[object Object],[object Object],[object Object]
Lecture 1 – Algorithm Analysis & Complexity
Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object]
What is Algorithm Analysis For ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example Algorithms ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Examples of famous algorithms ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Role of Algorithms in Modern World ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
A real-world Problem ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
IP Prefixes and Routing ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Data Structures ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Algorithms to Process these Data ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Max Subsequence Problem ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Algorithm 1 for Max Subsequence Sum ,[object Object],[object Object],int maxSum = 0; for( int i = 0; i < a.size( ); i++ ) for( int j = i; j < a.size( ); j++ ) { int thisSum = 0; for( int k = i; k <= j; k++ ) thisSum += a[ k ]; if( thisSum > maxSum ) maxSum  = thisSum; } return maxSum; ,[object Object]
Algorithm 2 ,[object Object],[object Object],into maxSum = 0; for( int i = 0; i < a.size( ); i++ ) int thisSum = 0; for( int j = i; j < a.size( ); j++ ) {   thisSum += a[ j ];   if( thisSum > maxSum )   maxSum  = thisSum; } return maxSum;
Algorithm 3 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Algorithm 3 (cont.) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Algorithm 3:  Analysis ,[object Object],[object Object],[object Object],[object Object]
Algorithm 4 ,[object Object],[object Object],2, 3, -2, 1, -5, 4, 1, -3, 4, -1, 2 int maxSum = 0, thisSum = 0; for( int j = 0; j < a.size( ); j++ ) { thisSum += a[ j ]; if ( thisSum > maxSum ) maxSum = thisSum; else if ( thisSum < 0 ) thisSum = 0; } return maxSum; }
Proof of Correctness ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Algorithm 4 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Why Efficient Algorithms Matter ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
How to Measure Algorithm Performance ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Abstraction ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Average, Best, and Worst-Case ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Examples ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Examples ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Simplifying the Bound ,[object Object],[object Object],[object Object],[object Object],[object Object]
Simplifications ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Simplification ,[object Object],[object Object]
Simplification ,[object Object],[object Object]
Complexity and Tractability  Assume the computer does 1 billion ops per sec.
2 n n 2 n  log  n n log  n log  n n n  log  n n 2 n 3 n 3 2 n
Another View ,[object Object],1.3 13 10 2 n 2.2 22 10 N 3 3.2 45 14 5n 2 10 10 1 1000n 10 100 10 100n Increase in Problem size Problem size solved in 10 4  sec Problem size solved in 10 3  sec T(n)
Asymptotics ,[object Object]
Caveats ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Asymptotic Notations ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
By Pictures ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example
Examples
Summary (Why O(n)?) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Runtime Analysis ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Runtime Analysis (cont.) ,[object Object],[object Object],[object Object],[object Object],[object Object]
Runtime Analysis (cont.) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Runtime Analysis (cont.) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example ,[object Object],[object Object],[object Object],[object Object],[object Object]
Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Run Time for Recursive Programs ,[object Object],[object Object],[object Object],[object Object],[object Object]
Example: Factorial  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],T(n) T(n-1) T(n-2) T(1)
Example: Factorial (cont.) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example: Hanoi Towers ,[object Object],[object Object]
// Early-terminating version of selection sort bool sorted = false; !sorted && sorted = true; else sorted = false;   // out of order ,[object Object],[object Object],template<class T> void SelectionSort(T a[], int n) { for (int size=n;  (size>1); size--) { int pos = 0; // find largest for (int i = 1; i < size; i++) if (a[pos] <= a[i]) pos = i; Swap(a[pos], a[size - 1]); } } Worst Case, Best Case, and Average Case
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],T(N) f(N) c f(N) n 0 T(N)=O(f(N)) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
An Analogy: Cooking Recipes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Lecture 2 - Recursion
What is Recursion? ,[object Object],[object Object],[object Object]
How does it work? ,[object Object],[object Object],[object Object]
How does it work? (cont.) ,[object Object],[object Object],[object Object]
Example ,[object Object],N! = N * (N-1)!  for N>0 0!  = 1 ,[object Object],[object Object]
Recursive Function Call ,[object Object],[object Object]
Finding a Recursive Solution ,[object Object],[object Object]
General format for many recursive functions ,[object Object],[object Object],[object Object],[object Object],[object Object]
Tail Recursion ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],void iterativetail(int i) { for (  ; i > 0; i--) System.out.print( i+ “ ”);   }
NonTail Recursion ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Indirect recursion ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Nested recursion ,[object Object],[object Object],[object Object]
Writing a recursive function to find  n factorial ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Writing a recursive function to find  Factorial(n) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Recursive Function Example: Factorial ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Factorial Function ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],returns  6   to  main()
Exponentiation ,[object Object],[object Object],[object Object],[object Object]
Can we write it recursively? ,[object Object],[object Object],[object Object],[object Object]
Another Recursive Function ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Power  x N  = x * x N -1   for  N >0  x 0  = 1 main(...) {  ...  20  cout << (Power(2,3)); ...
[object Object],The Puzzle
A Tree Diagram for Fibonacci’s Puzzle
Observations ,[object Object],[object Object],[object Object],[object Object]
Fibonacci sequence ,[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],A More Complex Recursive Function
Fibonacci Sequence Function ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5)
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns  Fib(3)  + Fib(4)
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns  Fib(3)  + Fib(4) Fib(3):  Fib returns  Fib(1)  + Fib(2)
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns  Fib(3)  + Fib(4) Fib(3):  Fib returns  Fib(1)  + Fib(2) Fib(1):  Fib returns 1
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns  Fib(3)  + Fib(4) Fib(3):  Fib returns 1 +  Fib(2)
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns  Fib(3)  + Fib(4) Fib(3):  Fib returns 1 +  Fib(2) Fib(2):  Fib returns 1
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns  Fib(3)  + Fib(4) Fib(3):  Fib returns 1 + 1
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns 2 +  Fib(4)
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns 2 +  Fib(4) Fib(4):  Fib returns  Fib(2)  + Fib(3)
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns 2 +  Fib(4) Fib(4):  Fib returns  Fib(2)  + Fib(3) Fib(2):  Fib returns 1
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns 2 +  Fib(4) Fib(4):  Fib returns 1 +  Fib(3)
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns 2 +  Fib(4) Fib(4):  Fib returns 1 +  Fib(3) Fib(3):  Fib returns  Fib(1)  + Fib(2)
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns 2 +  Fib(4) Fib(4):  Fib returns 1 +  Fib(3) Fib(3):  Fib returns  Fib(1)  + Fib(2) Fib(1):  Fib returns 1
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns 2 +  Fib(4) Fib(4):  Fib returns 1 +  Fib(3) Fib(3):  Fib returns 1 +  Fib(2)
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns 2 +  Fib(4) Fib(4):  Fib returns 1 +  Fib(3) Fib(3):  Fib returns 1 +  Fib(2) Fib(2):  Fib returns 1
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns 2 +  Fib(4) Fib(4):  Fib returns 1 +  Fib(3) Fib(3):  Fib returns 1 + 1
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns 2 +  Fib(4) Fib(4):  Fib returns 1 + 2
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- Fib(5) Fib(5):  Fib returns 2 + 3
Tracing with Multiple Recursive Calls Main Algorithm:  answer <- 5
Fib(5) 15 calls to Fib to find the 5th Fibonacci number!!! Fib(3) Fib(4) Fib(3) Fib(2) Fib(2) Fib(1) Fib(1) Fib(0) Fib(1) Fib(0) Fib(2) Fib(1) Fib(1) Fib(0)
Excessive Recursion-Fibonacci recursion ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
 
 
Rules of Recursion ,[object Object],[object Object],[object Object]
Rules of Recursion ,[object Object],[object Object]
Rules of Recursion ,[object Object],[object Object]
Towers of Hanoi ,[object Object]
Towers of Hanoi B C A
Towers of Hanoi B C A
Towers of Hanoi B C A
Towers of Hanoi B C A
Towers of Hanoi B C A
Towers of Hanoi B C A
Towers of Hanoi B C A
Towers of Hanoi B C A
Towers of Hanoi A pseudocode description of the solution is: Towers(Count, Source, Dest, Spare) if (Count is 1) Move the disk directly from Source to Dest else { Solve Towers(Count-1, Source, Spare, Dest) Solve Towers(1, Source, Dest, Spare) Solve Towers(Count-1, Spare, Dest, Source) }
Towers of Hanoi void solveTowers( int count, char source, char dest, char spare){ if (count == 1) cout<<“Move disk from pole “ << source  << &quot; to pole &quot; << destination <<endl; else { towers(count-1, source, spare, destination); towers(1, source, destination, spare); towers(count-1, spare, destination, source); }//end if }//end solveTowers
Recall that . . .  ,[object Object],[object Object],[object Object]
Pascal Triangle (Is this recursive?)
Pascal Triangle ,[object Object],[object Object],[object Object],[object Object]
Pascal Triangle ,[object Object],n      0                             1              1                        1       1              2                     1     2      1              3                 1     3      3     1              4             1     4      6      4    1              5         1     5      10     10    5    1 This can also be written:              r     0    1    2    3     4    5          n  0     1              1     1     1              2     1     2    1              3     1     3    3    1              4     1     4    6    4    1              5     1     5   10  10    5    1
Pascal Triangle ,[object Object],[object Object]
Pascal Triangle ,[object Object],[object Object],[object Object]
What is the value of  rose(25) ? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Finding the value of  rose(25) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Writing recursive functions ,[object Object],[object Object],[object Object]
Three-Question Method of verifying  recursive functions ,[object Object],[object Object],[object Object]
Guidelines for writing recursive  functions ,[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],struct ListType
Recursive function to determine if value is in list ,[object Object],[object Object],[object Object],[object Object],index  of current element to  examine  74  36  . . .  95 list[0]  [1]  [startIndex] 75  29  47  . . . [length -1]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
“ Why use recursion?” Many solutions could have been written without recursion, by using iteration instead.  The iterative solution uses a loop, and the recursive solution uses an if statement. However, for certain problems the recursive solution is the most natural solution.  This often occurs when  pointer  variables are used.
When to Use Recursion ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],int factorial[8] = {1, 1, 2, 6, 24, 120, 720, 5040};
Use a recursive solution when: ,[object Object],[object Object],[object Object],SHALLOW DEPTH  EFFICIENCY  CLARITY
Lecture 3 – Binary Trees
Why Trees? ,[object Object],[object Object],[object Object],[object Object],[object Object]
Trees: Recursive Definition ,[object Object],[object Object],[object Object]
Trees: Recursive Definition (cont.) ROOT OF TREE T T1 T2 T3 T4 T5 SUBTREES
Trees: An Example A B C D E F G H I
Trees: More Definitions ,[object Object],[object Object],[object Object],[object Object]
Trees: More Definitions (cont.) ,[object Object],[object Object],[object Object],[object Object]
Binary Trees – A Informal Definition ,[object Object],[object Object],[object Object],struct TreeNode { Object  element; TreeNode *left_child; TreeNode *right_child; };
Binary Trees – Formal Definition ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Binary Trees: Recursive Definition ROOT OF TREE T T1 T2 SUBTREES *left_child *right_child
Differences Between A Tree & A Binary Tree ,[object Object],[object Object]
Differences Between A Tree & A Binary Tree (cont.) ,[object Object],[object Object],[object Object],a b a b
Internal and External Nodes ,[object Object],[object Object],[object Object],internal node external node
Recursive definition of a Binary Tree ,[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],What is a binary tree? (cont.)
Mathematical Properties of Binary Trees ,[object Object],[object Object],[object Object]
Minimum Number Of Nodes ,[object Object],[object Object],minimum number of nodes is   h + 1
Maximum Number Of Nodes ,[object Object],[object Object],[object Object]
Number of Nodes & Height ,[object Object],[object Object],[object Object],[object Object],[object Object]
Relationship Between Number of Nodes (Internal - External) ,[object Object],[object Object]
Number of edges ,[object Object],Let's try to prove this using induction...
Number of edges ,[object Object],Let's try to prove this using induction...
Binary Tree Representation ,[object Object],[object Object]
Binary Trees ,[object Object],[object Object],[object Object],[object Object],[object Object]
Node Number Properties  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Complete  binary tree 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Full Binary Tree With n Nodes ,[object Object],[object Object],[object Object],Full binary tree with 11 nodes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Array Representation ,[object Object],[object Object],a b c d e f g h i j 1 2 3 4 6 7 8 9 b a c d e f g h i j 1 2 3 4 5 6 7 8 9 10 tree[] 0 5 10
Right-Skewed Binary Tree ,[object Object],[object Object],a b 1 3 c 7 d 15 tree[] 0 5 10 a - b - - - c - - - - - - - 15 d
Array Representation (cont.) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Linked Representation ,[object Object],[object Object]
Trees: Linked representation  Implementation 1 struct TreeNode { Object  element; TreeNode *child1; TreeNode *child2; . . . TreeNode *childn; }; ,[object Object],[object Object]
struct TreeNode { Object  element; TreeNode *child1; TreeNode *sibling; }; Each node contain a link to its first child and a link to its next sibling. This is a better idea. Trees: Linked representation  Implementation 2
Implementation 2: Example / The downward links are to the first child; the horizontal links are to the next sibling. / / / / A / B F / C D E / G H / I /
Binary Trees ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Linked Representation Example a c b d f e g h leftChild element rightChild root
Some Binary Tree Operations ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
CS122 Algorithms and Data Structures Week 7:  Binary Search Trees Binary Expression Trees
Uses for Binary Trees…  --  Binary Search Trees ,[object Object],[object Object],[object Object]
A Property of Binary Search Trees ,[object Object],[object Object],[object Object]
A Property of  Binary Search Tree ROOT OF TREE T T1 T2 SUBTREES *left_child *right_child X All nodes in T1 have keys < X. All nodes in T2 have keys > X.
Binary Search Trees in C++ ,[object Object],[object Object],[object Object]
Search Operation BinaryNode *search (const int &x, BinaryNode *t) { if ( t == NULL ) return NULL; if (x == t->key)  return t; // Match if ( x < t->key ) return search( x, t->left ); else  //  t ->key < x return search( x, t->right );  }
FindMin Operation BinaryNode* findMin (BinaryNode *t) { if ( t == NULL ) return NULL; if ( t -> left == NULL ) return t; return findMin (t -> left); } This method returns a pointer to the node containing the smallest element in the tree.
FindMax Operation BinaryNode* findMax (BinaryNode *t) { if ( t == NULL ) return NULL; if ( t -> right == NULL ) return t; return findMax (t -> right); } This function returns a pointer to the node containing the largest element in the tree.
Insert Operation ,[object Object]
Insert Operation (cont.) void BinarySearchTree insert (const int &x, BinaryNode *&t) const { if (t == NULL) t = new BinaryNode (x, NULL, NULL); else if (x < t->key) insert(x, t->left); else if( t->key < x) insert(x, t->right); else ;  // Duplicate entry; do nothing } Note the pointer t is passed using call by reference.
Removal Operation ,[object Object],[object Object]
[object Object],[object Object],Removal Operation (cont.)
Removal Operation (cont.) void  remove (const int &x, BinaryNode* &t) const { if ( t == NULL ) return;  // key is not found; do nothing if ( t->key == x) { if( t->left != NULL && t->right != NULL ) {  // Two children t->key = findMin( t->right )->key; remove( t->key, t->right );  } else  {  // One child BinaryNode *oldNode = t; t = ( t->left != NULL ) ? t->left : t->right; delete oldNode;  } }  else {  // Two recursive calls if ( x < t->key )  remove( x, t->left ); else if( t->key < x ) remove( x, t->right ); } }
Deleting by merging
Deleting by merging
Deleting by copying
Balancing a binary tree ,[object Object],[object Object]
Balancing a binary tree
Analysis ,[object Object],[object Object]
Average Level of Nodes 10 5 20 1 8 13 34 Consider this very well-balanced binary search tree. What is the level of its leaf nodes? N=7 Data Order: 10, 5, 1, 8, 20, 13, 34
A Better Analysis ,[object Object],[object Object]
Effect of Data Order Obtained if data is 4, 3, 2 1 Obtained if data is 1, 2, 3, 4 Note in these cases the average depth of nodes is about  N/2 , not  log(N)!
Depth of Nodes ,[object Object],[object Object]
Effects of Data Order… ,[object Object],[object Object]
Summary ,[object Object],[object Object]
Uses for Binary Trees… --  Binary Expression Trees ,[object Object],[object Object],[object Object]
Binary Expression Trees: Examples ,[object Object],- a (a + b) * (c – d) / (e + f) + a b - a / + a b - c d + e f * /
Merits of Binary Tree Form ,[object Object],[object Object],[object Object],+ a b - c d + e f * /
Levels Indicate Precedence ,[object Object],[object Object],[object Object]
A Binary Expression Tree What value does it have? ( 4 + 2 )  *  3  =  18 ‘ *’ ‘ +’ ‘ 4’ ‘ 3’ ‘ 2’
Inorder Traversal:  (A + H) / (M - Y)   ‘ +’ ‘ A’ ‘ H’ ‘ -’ ‘ M’ ‘ Y’ tree Print left subtree first Print right subtree last Print second ‘ /’
Inorder Traversal (cont.) a + * b c + * + g * d e f Inorder traversal yields: (a + (b * c)) + (((d * e) + f) * g)
Preorder Traversal:  / + A H - M Y ‘ +’ ‘ A’ ‘ H’ ‘ -’ ‘ M’ ‘ Y’ tree Print left subtree second Print right subtree last Print first ‘ /’
Preorder Traversal (cont.) a + * b c + * + g * d e f Preorder traversal yields: (+ (+ a (* b c)) (* (+ (* d e) f) g))
‘ +’ ‘ A’ ‘ H’ ‘ -’ ‘ M’ ‘ Y’ tree Print left subtree first Print right subtree second Print last Postorder Traversal:  A H + M Y - /   ‘ /’
Postorder Traversal (cont.) a + * b c + * + g * d e f Postorder traversal yields: a b c * + d e * f + g * +
Traversals and Expressions ,[object Object],[object Object],[object Object]
Constructing an Expression Tree ,[object Object],[object Object]
Expression Tree Algorithm ,[object Object],[object Object],[object Object],[object Object]
Example a b +  : Note: These stacks are depicted horizontally. a b + b a
Example a b + c d e +  : + b a c d e + b a c d e +
Example a b + c d e + *  : + b a c d e + *
Example a b + c d e + * * : + b a c d e + * *

Contenu connexe

Tendances

Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First SearchKevin Jadiya
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureDharita Chokshi
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using BacktrackingAbhishek Singh
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemMadhu Bala
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithmsDr Geetha Mohan
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadlineArafat Hossan
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplicationMegha V
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree methodRajendran
 

Tendances (20)

Brute force method
Brute force methodBrute force method
Brute force method
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 
Merge sort
Merge sortMerge sort
Merge sort
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithms
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadline
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
 
Fragmentaton
Fragmentaton Fragmentaton
Fragmentaton
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 

En vedette

Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and FlowchartsDeva Singh
 
Our presentation on algorithm design
Our presentation on algorithm designOur presentation on algorithm design
Our presentation on algorithm designNahid Hasan
 
Lecture 2 role of algorithms in computing
Lecture 2   role of algorithms in computingLecture 2   role of algorithms in computing
Lecture 2 role of algorithms in computingjayavignesh86
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and AlgorithmDhaval Kaneria
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsSwapnil Agrawal
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsArvind Krishnaa
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)jehan1987
 
03 algorithm properties
03 algorithm properties03 algorithm properties
03 algorithm propertiesLincoln School
 
Algorithm Design Presentation
Algorithm Design PresentationAlgorithm Design Presentation
Algorithm Design PresentationKawsar Ahmed
 
Algorithm and flowchart2010
Algorithm and flowchart2010Algorithm and flowchart2010
Algorithm and flowchart2010Jordan Delacruz
 
Data Structures and Algorithms
Data Structures and AlgorithmsData Structures and Algorithms
Data Structures and AlgorithmsPierre Vigneras
 
Introduction to Pseudocode
Introduction to PseudocodeIntroduction to Pseudocode
Introduction to PseudocodeDamian T. Gordon
 

En vedette (20)

Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and Flowcharts
 
Our presentation on algorithm design
Our presentation on algorithm designOur presentation on algorithm design
Our presentation on algorithm design
 
Writing algorithms
Writing algorithmsWriting algorithms
Writing algorithms
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
Lecture 2 role of algorithms in computing
Lecture 2   role of algorithms in computingLecture 2   role of algorithms in computing
Lecture 2 role of algorithms in computing
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and Algorithm
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Flowchart and algorithm
Flowchart and algorithmFlowchart and algorithm
Flowchart and algorithm
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
03 algorithm properties
03 algorithm properties03 algorithm properties
03 algorithm properties
 
Algorithm Design Presentation
Algorithm Design PresentationAlgorithm Design Presentation
Algorithm Design Presentation
 
Algorithm and flowchart2010
Algorithm and flowchart2010Algorithm and flowchart2010
Algorithm and flowchart2010
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
 
Data Structures and Algorithms
Data Structures and AlgorithmsData Structures and Algorithms
Data Structures and Algorithms
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Introduction to Pseudocode
Introduction to PseudocodeIntroduction to Pseudocode
Introduction to Pseudocode
 

Similaire à Introduction to Algorithms

VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxskilljiolms
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMSTanya Makkar
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues listsJames Wong
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
StacksqueueslistsFraboni Ec
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues listsTony Nguyen
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues listsHarry Potter
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues listsYoung Alista
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...TechVision8
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 DsQundeel
 
Data Structure
Data StructureData Structure
Data Structuresheraz1
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 DsQundeel
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdfMemMem25
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm AnalysisMary Margarat
 
analysis of algorithms
analysis of algorithmsanalysis of algorithms
analysis of algorithmsMyMovies15
 

Similaire à Introduction to Algorithms (20)

VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues lists
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
Stacksqueueslists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
 
Data Structure
Data StructureData Structure
Data Structure
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
Lec7
Lec7Lec7
Lec7
 
Lec7.ppt
Lec7.pptLec7.ppt
Lec7.ppt
 
Lec7.ppt
Lec7.pptLec7.ppt
Lec7.ppt
 
analysis of algorithms
analysis of algorithmsanalysis of algorithms
analysis of algorithms
 
Analysis of Algorithum
Analysis of AlgorithumAnalysis of Algorithum
Analysis of Algorithum
 

Dernier

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 

Dernier (20)

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 

Introduction to Algorithms

  • 1. Algorithm Analysis & Data Structures Jaideep Srivastava
  • 2.
  • 3.
  • 4. Lecture 1 – Algorithm Analysis & Complexity
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33. Complexity and Tractability Assume the computer does 1 billion ops per sec.
  • 34. 2 n n 2 n log n n log n log n n n log n n 2 n 3 n 3 2 n
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56. Lecture 2 - Recursion
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77. A Tree Diagram for Fibonacci’s Puzzle
  • 78.
  • 79.
  • 80.
  • 81.
  • 82. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5)
  • 83. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns Fib(3) + Fib(4)
  • 84. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns Fib(3) + Fib(4) Fib(3): Fib returns Fib(1) + Fib(2)
  • 85. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns Fib(3) + Fib(4) Fib(3): Fib returns Fib(1) + Fib(2) Fib(1): Fib returns 1
  • 86. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns Fib(3) + Fib(4) Fib(3): Fib returns 1 + Fib(2)
  • 87. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns Fib(3) + Fib(4) Fib(3): Fib returns 1 + Fib(2) Fib(2): Fib returns 1
  • 88. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns Fib(3) + Fib(4) Fib(3): Fib returns 1 + 1
  • 89. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4)
  • 90. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns Fib(2) + Fib(3)
  • 91. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns Fib(2) + Fib(3) Fib(2): Fib returns 1
  • 92. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns 1 + Fib(3)
  • 93. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns 1 + Fib(3) Fib(3): Fib returns Fib(1) + Fib(2)
  • 94. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns 1 + Fib(3) Fib(3): Fib returns Fib(1) + Fib(2) Fib(1): Fib returns 1
  • 95. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns 1 + Fib(3) Fib(3): Fib returns 1 + Fib(2)
  • 96. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns 1 + Fib(3) Fib(3): Fib returns 1 + Fib(2) Fib(2): Fib returns 1
  • 97. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns 1 + Fib(3) Fib(3): Fib returns 1 + 1
  • 98. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns 1 + 2
  • 99. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + 3
  • 100. Tracing with Multiple Recursive Calls Main Algorithm: answer <- 5
  • 101. Fib(5) 15 calls to Fib to find the 5th Fibonacci number!!! Fib(3) Fib(4) Fib(3) Fib(2) Fib(2) Fib(1) Fib(1) Fib(0) Fib(1) Fib(0) Fib(2) Fib(1) Fib(1) Fib(0)
  • 102.
  • 103.  
  • 104.  
  • 105.
  • 106.
  • 107.
  • 108.
  • 109. Towers of Hanoi B C A
  • 110. Towers of Hanoi B C A
  • 111. Towers of Hanoi B C A
  • 112. Towers of Hanoi B C A
  • 113. Towers of Hanoi B C A
  • 114. Towers of Hanoi B C A
  • 115. Towers of Hanoi B C A
  • 116. Towers of Hanoi B C A
  • 117. Towers of Hanoi A pseudocode description of the solution is: Towers(Count, Source, Dest, Spare) if (Count is 1) Move the disk directly from Source to Dest else { Solve Towers(Count-1, Source, Spare, Dest) Solve Towers(1, Source, Dest, Spare) Solve Towers(Count-1, Spare, Dest, Source) }
  • 118. Towers of Hanoi void solveTowers( int count, char source, char dest, char spare){ if (count == 1) cout<<“Move disk from pole “ << source << &quot; to pole &quot; << destination <<endl; else { towers(count-1, source, spare, destination); towers(1, source, destination, spare); towers(count-1, spare, destination, source); }//end if }//end solveTowers
  • 119.
  • 120. Pascal Triangle (Is this recursive?)
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133. “ Why use recursion?” Many solutions could have been written without recursion, by using iteration instead. The iterative solution uses a loop, and the recursive solution uses an if statement. However, for certain problems the recursive solution is the most natural solution. This often occurs when pointer variables are used.
  • 134.
  • 135.
  • 136. Lecture 3 – Binary Trees
  • 137.
  • 138.
  • 139. Trees: Recursive Definition (cont.) ROOT OF TREE T T1 T2 T3 T4 T5 SUBTREES
  • 140. Trees: An Example A B C D E F G H I
  • 141.
  • 142.
  • 143.
  • 144.
  • 145. Binary Trees: Recursive Definition ROOT OF TREE T T1 T2 SUBTREES *left_child *right_child
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167. struct TreeNode { Object element; TreeNode *child1; TreeNode *sibling; }; Each node contain a link to its first child and a link to its next sibling. This is a better idea. Trees: Linked representation Implementation 2
  • 168. Implementation 2: Example / The downward links are to the first child; the horizontal links are to the next sibling. / / / / A / B F / C D E / G H / I /
  • 169.
  • 170. Linked Representation Example a c b d f e g h leftChild element rightChild root
  • 171.
  • 172. CS122 Algorithms and Data Structures Week 7: Binary Search Trees Binary Expression Trees
  • 173.
  • 174.
  • 175. A Property of Binary Search Tree ROOT OF TREE T T1 T2 SUBTREES *left_child *right_child X All nodes in T1 have keys < X. All nodes in T2 have keys > X.
  • 176.
  • 177. Search Operation BinaryNode *search (const int &x, BinaryNode *t) { if ( t == NULL ) return NULL; if (x == t->key) return t; // Match if ( x < t->key ) return search( x, t->left ); else // t ->key < x return search( x, t->right ); }
  • 178. FindMin Operation BinaryNode* findMin (BinaryNode *t) { if ( t == NULL ) return NULL; if ( t -> left == NULL ) return t; return findMin (t -> left); } This method returns a pointer to the node containing the smallest element in the tree.
  • 179. FindMax Operation BinaryNode* findMax (BinaryNode *t) { if ( t == NULL ) return NULL; if ( t -> right == NULL ) return t; return findMax (t -> right); } This function returns a pointer to the node containing the largest element in the tree.
  • 180.
  • 181. Insert Operation (cont.) void BinarySearchTree insert (const int &x, BinaryNode *&t) const { if (t == NULL) t = new BinaryNode (x, NULL, NULL); else if (x < t->key) insert(x, t->left); else if( t->key < x) insert(x, t->right); else ; // Duplicate entry; do nothing } Note the pointer t is passed using call by reference.
  • 182.
  • 183.
  • 184. Removal Operation (cont.) void remove (const int &x, BinaryNode* &t) const { if ( t == NULL ) return; // key is not found; do nothing if ( t->key == x) { if( t->left != NULL && t->right != NULL ) { // Two children t->key = findMin( t->right )->key; remove( t->key, t->right ); } else { // One child BinaryNode *oldNode = t; t = ( t->left != NULL ) ? t->left : t->right; delete oldNode; } } else { // Two recursive calls if ( x < t->key ) remove( x, t->left ); else if( t->key < x ) remove( x, t->right ); } }
  • 188.
  • 190.
  • 191. Average Level of Nodes 10 5 20 1 8 13 34 Consider this very well-balanced binary search tree. What is the level of its leaf nodes? N=7 Data Order: 10, 5, 1, 8, 20, 13, 34
  • 192.
  • 193. Effect of Data Order Obtained if data is 4, 3, 2 1 Obtained if data is 1, 2, 3, 4 Note in these cases the average depth of nodes is about N/2 , not log(N)!
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201. A Binary Expression Tree What value does it have? ( 4 + 2 ) * 3 = 18 ‘ *’ ‘ +’ ‘ 4’ ‘ 3’ ‘ 2’
  • 202. Inorder Traversal: (A + H) / (M - Y) ‘ +’ ‘ A’ ‘ H’ ‘ -’ ‘ M’ ‘ Y’ tree Print left subtree first Print right subtree last Print second ‘ /’
  • 203. Inorder Traversal (cont.) a + * b c + * + g * d e f Inorder traversal yields: (a + (b * c)) + (((d * e) + f) * g)
  • 204. Preorder Traversal: / + A H - M Y ‘ +’ ‘ A’ ‘ H’ ‘ -’ ‘ M’ ‘ Y’ tree Print left subtree second Print right subtree last Print first ‘ /’
  • 205. Preorder Traversal (cont.) a + * b c + * + g * d e f Preorder traversal yields: (+ (+ a (* b c)) (* (+ (* d e) f) g))
  • 206. ‘ +’ ‘ A’ ‘ H’ ‘ -’ ‘ M’ ‘ Y’ tree Print left subtree first Print right subtree second Print last Postorder Traversal: A H + M Y - / ‘ /’
  • 207. Postorder Traversal (cont.) a + * b c + * + g * d e f Postorder traversal yields: a b c * + d e * f + g * +
  • 208.
  • 209.
  • 210.
  • 211. Example a b + : Note: These stacks are depicted horizontally. a b + b a
  • 212. Example a b + c d e + : + b a c d e + b a c d e +
  • 213. Example a b + c d e + * : + b a c d e + *
  • 214. Example a b + c d e + * * : + b a c d e + * *