SlideShare ist ein Scribd-Unternehmen logo
1 von 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 + * *

Weitere ähnliche Inhalte

Was ist angesagt?

Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplicationMegha V
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAdelina Ahadova
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsMohamed Loey
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysisjayavignesh86
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.Tariq Khan
 
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
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithmsDr Geetha Mohan
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsEhtisham Ali
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencySaranya Natarajan
 
02 order of growth
02 order of growth02 order of growth
02 order of growthHira Gul
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsSwapnil Agrawal
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 

Was ist angesagt? (20)

Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysis
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
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
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithms
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Big o notation
Big o notationBig o notation
Big o notation
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
 
Unit 1 chapter 1 Design and Analysis of Algorithms
Unit 1   chapter 1 Design and Analysis of AlgorithmsUnit 1   chapter 1 Design and Analysis of Algorithms
Unit 1 chapter 1 Design and Analysis of Algorithms
 
02 order of growth
02 order of growth02 order of growth
02 order of growth
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 

Andere mochten auch

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 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
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languagesVarun Garg
 
Programming languages
Programming languagesProgramming languages
Programming languagesAsmasum
 

Andere mochten auch (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
 
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
 
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
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languages
 
Programming languages
Programming languagesProgramming languages
Programming languages
 

Ähnlich wie 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 listsYoung Alista
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues listsTony Nguyen
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues listsHarry Potter
 
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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 

Kürzlich hochgeladen (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 

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 + * *