Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Basics in algorithms and data structure

342 views

Published on

  • Login to see the comments

Basics in algorithms and data structure

  1. 1. Data Structures and Algorithms “I Will, In Fact, Claim That The Difference Between A Bad Programmer And A Good One Is Whether He Considers His Code Or His Data Structures More Important. Bad Programmers Worry About The Code. Good Programmers Worry About Data Structures And Their Relationships.” Linus Torvalds  The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days.
  2. 2. Algorithm:  a method or a process followed to solve a problem.  An algorithm takes the input to a problem and transforms it to the output. Algorithm Efficiency There are often many approaches (algorithms)to solve a problem. How do we choose between them?!! At the heart of computer program design are two goals: 1. To design an algorithm that is easy to understand, code and debug.(software ) 2. To design an algorithm that makes efficient use of the computer’s resources.( algorithm analysis) Critical resources: • Time • Space (disk, RAM) • Programmer’s effort • Ease of use
  3. 3. Searching:  When we maintain a collection of data, one of the operations we need is a search routine to locate desired data quickly.  To gauge the performance of searching algorithm : • We will count the number of comparisons the algorithms make to analyze their performance. (time it take to finish every time we search using algorithm) Best, Worst and Average Cases Not all inputs of a given size take the same time. Example: Sequential search for K in an array of n integers: • Begin at first element in array and look at each element in turn until K is found. Best Case: [Find at first position: 1 compare] Worst Case: [Find at last position: n compares] Average Case: [(n + 1)/2 compares]
  4. 4. Computation Complexity Definition: Measure the efficiency of an algorithm in terms of time or space required. Time tends to be more important. The efficiency of an algorithm is always stated as a function of the input size The “Big-O” notation: O(f(n)) Worst case scenario The maximum number of computation steps taken on any input size n.
  5. 5. In an increasing order of complexity: Constant time: O(1) Logarithmic time: O(logn) Linear time: O(n) Polynomial time: O(n2) Exponential time: O(2n) Suppose each step takes 1 microseconds (10-6): n O(1) O(logn) O(n) O(n^2) O(2^n) 10 1 1 10 100 1024 100 1 2 100 10000 40196936841331500 years 1000 1 3 1000 1 sec … 10000 1 4 10000 1.67 min …
  6. 6. Example of search results: Unsuccessful Search: search(45) Successful Search: search(12) NOT_FOUND 4 number
  7. 7. sequential Search Performance: • We count how many times the search value is compared against the array elements. • Successful Search • We analyze the successful and unsuccessful searches separately. – Best Case – 1 comparison – Worst Case – N comparisons (N – array size) • Unsuccessful Search – Best Case = Worst Case – N comparisons Notes: The order of this algorithm is O(N) as N is the number of elements in array.
  8. 8. Searching “self organized search” • If we want to make sequential more fast we can make some priorities to some entries that we can need to get to them 80% of time more than others we can use to method to make it : • First by use algorithm of “move to front”:  we put our need entry at the top of the array so we can find it mush easier when need it. • Second by use algorithm of “ transpose”:  every time we access an entry we make only one shift to the position of the element so we can reach it next time mush easier. Note : both of to algorithms don’t need any sorting for the array to use with it.
  9. 9. • /* C program to input N numbers and store them in an array. • * Do a linear search for a given key and report success • * or failure. • */ • #include <stdio.h> • void main() • { • int array[10]; • int i, num, keynum, found = 0; • printf("Enter the value of num n"); • scanf("%d", &num); • printf("Enter the elements one by one n"); • for (i = 0; i < num; i++) • {scanf("%d", &array[i]); • } • printf("Input array is n"); • for (i = 0; i < num; i++) • { • printf("%dn", array[i]); • } • printf("Enter the element to be searched n"); • scanf("%d", &keynum); • /* Linear search begins */ • for (i = 0; i < num ; i++) • { • if (keynum == array[i] ) • { • found = 1; • break; • } • } • if (found == 1) • printf("Element is present in the arrayn"); • else • printf("Element is not present in the arrayn"); • }
  10. 10. Binary Search • If the array is sorted, then we can apply the binary search technique. number • The basic idea is straightforward. First search the value in the middle position. If X is less than this value, then search the middle of the left half next. If X is greater than this value, then search the middle of the right half next. Continue in this manner. 55 12 17 23 38 44 77 0 1 2 3 4 5 6 7 8 84 90
  11. 11. Sequence of Successful Search - 1 38 < 44 search( 44 ) Low=mid+1=5 #1
  12. 12. Sequence of Successful Search - 2 high = mid-1=5 44<77 #2 #3 Low=mid +1 so low > high Finished
  13. 13. Binary Search Performance: Successful Search: Best Case – 1 comparison Worst Case – log2N comparisons Unsuccessful Search: Best Case =Worst Case – log2N comparisons Since the portion of an array to search is cut into half after every comparison, we compute how many times the array can be divided into halves. So we can note in the example we discussed we can see that we make 3 search in an array of 8 elements . So binary search can make number of search equal to log N for base=2. After K comparisons, there will be N/2K elements in the list. We solve for K when N/2K = 1, deriving K = log2N.
  14. 14. This * Otherwise, a message "UNSUCCESSFUL SEARCH" is displayed. 5. */6.# include <stdio.h> 7. 8.void main() 9.{ 10.int array[20]; 11.int i, low, mid, high, key, size; 12. 13.printf("Enter the size of an arrayn"); 14.scanf("%d", &size); 15.printf("Enter the array elementsn"); 16.for (i = 0; i < size; i++) 17.{ 18.scanf("%d", &array[i]); 19.} 20.printf("Enter the keyn"); 21.scanf("%d", &key); 22./* search begins */ 23.low = 0; 24.high = (size - 1); 25.while (low <= high) 26.{ 27.mid = (low + high) / 2; 28.if (key == array[mid]) 29.{ 30.printf("SUCCESSFUL SEARCHn"); 31.return; 32.} 33.if (key < array[mid]) 34.high = mid - 1; 35.else 36.low = mid + 1; 37.} 38.printf("UNSUCCESSFUL SEARCHn"); 39.}
  15. 15. Data Structure  Data structures organize data⇒ more efficient programs.  The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days. Efficiency • A solution is said to be efficient if it solves the problem within its resource constraints. • space [These are typical constraints for programs] • time.
  16. 16. trees
  17. 17. Where I can find tree data structure?! . PDF is a tree based format Some very successful physics/games engines build trees to accurately simulate human movement. file system on a computer We can insert/delete keys in moderate time (quicker than Arrays )
  18. 18. Trees & binary trees
  19. 19. Binary search tree (BST) Algorithm is :
  20. 20. For BST we put values which is smaller than node value in the left and bigger values in the right As 30 have to be in the left
  21. 21. Difference between BT & BST A binary tree is simply a tree in which each node can have at most two children. A binary search tree is a binary tree in which the nodes are assigned values, with the following restrictions : 1. The left sub tree of a node can only have values less than the node 2. The right sub tree of a node can only have values greater than the node 3. The left sub tree of a node is a binary search tree. 4. The right sub tree of a node is a binary search tree.
  22. 22. BST Operations
  23. 23. BST Traversal  Traversal mean how can BST rotate to visit every node in the tree That can be use when we want to print every element in a tree or want to get the max or min of tree elements .
  24. 24. Pre-order In this way we first visit the root then left hand side then right hand side.
  25. 25. In-order Visiting left sub tree then root then right sub tree
  26. 26. Post order Visiting left sub tree then right then root at the end
  27. 27. Binary Tree Insertion Insertion mean that you want to put new node to your BT
  28. 28. 1 10 8 4 6 3 2 5 Binary Tree Insertion
  29. 29. 1 1 10 8 4 6 3 2 5
  30. 30. 1 1 10 8 4 6 3 2 5
  31. 31. 1 1 10 8 4 6 3 2 5
  32. 32. 1 1 10 8 4 6 3 2 5
  33. 33. 1 10 1 10 8 4 6 3 2 5
  34. 34. 1 10 1 10 8 4 6 3 2 5
  35. 35. 1 10 1 10 8 4 6 3 2 5
  36. 36. 1 10 1 10 8 4 6 3 2 5
  37. 37. 1 10 1 10 8 4 6 3 2 5
  38. 38. 1 10 1 10 8 4 6 3 2 5 8
  39. 39. 1 10 1 10 8 4 6 3 2 5 8
  40. 40. 1 10 1 10 8 4 6 3 2 5 8
  41. 41. 1 10 1 10 8 4 6 3 2 5 8
  42. 42. 1 10 1 10 8 4 6 3 2 5 8
  43. 43. 1 10 1 10 8 4 6 3 2 5 8
  44. 44. 1 10 1 10 8 4 6 3 2 5 8 4
  45. 45. 1 10 1 10 8 4 6 3 2 5 8 4
  46. 46. 1 10 1 10 8 4 6 3 2 5 8 4
  47. 47. 1 10 1 10 8 4 6 3 2 5 8 4
  48. 48. 1 10 1 10 8 4 6 3 2 5 8 4
  49. 49. 1 10 1 10 8 4 6 3 2 5 8 4
  50. 50. 1 10 1 10 8 4 6 3 2 5 8 4
  51. 51. 1 10 1 10 8 4 6 3 2 5 8 4
  52. 52. 1 10 1 10 8 4 6 3 2 5 8 4 6
  53. 53. 1 10 1 10 8 4 6 3 2 5 8 4 6
  54. 54. 1 10 1 10 8 4 6 3 2 5 8 4 6
  55. 55. 1 10 1 10 8 4 6 3 2 5 8 4 6
  56. 56. 1 10 1 10 8 4 6 3 2 5 8 4 6
  57. 57. 1 10 1 10 8 4 6 3 2 5 8 4 6
  58. 58. 1 10 1 10 8 4 6 3 2 5 8 4 6
  59. 59. 1 10 1 10 8 4 6 3 2 5 8 4 63
  60. 60. 1 10 1 10 8 4 6 3 2 5 8 4 63
  61. 61. 1 10 1 10 8 4 6 3 2 5 8 4 63
  62. 62. 10 1 10 8 4 6 3 2 5 8 4 63
  63. 63. 1 10 1 10 8 4 6 3 2 5 8 4 63
  64. 64. 1 10 1 10 8 4 6 3 2 5 8 4 63
  65. 65. 1 10 1 10 8 4 6 3 2 5 8 4 63
  66. 66. 1 10 1 10 8 4 6 3 2 5 8 4 63
  67. 67. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  68. 68. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  69. 69. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  70. 70. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  71. 71. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  72. 72. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  73. 73. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  74. 74. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  75. 75. 1 10 1 10 8 4 6 3 2 5 8 4 63 2 5
  76. 76. 1 10 1 10 8 4 6 3 2 5 8 4 63 2 5
  77. 77. Binary Tree Deletion
  78. 78. 1 10 1 10 8 4 6 3 2 5 8 4 63 2 5
  79. 79. 1 10 1 10 8 4 6 3 2 5 8 4 63 2 5
  80. 80. 1 10 1 10 8 4 6 3 2 5 8 4 63 5
  81. 81. 1 10 1 10 8 4 6 3 2 5 8 4 63 5 We will delete the node that haven’t any children and make parent pointe to NULL
  82. 82. 1 10 1 10 8 4 6 3 5 8 4 63 5 Node 8 has only on child
  83. 83. 1 10 1 10 8 4 6 3 5 8 4 63 5
  84. 84. 1 10 1 10 8 4 6 3 5 4 63 5
  85. 85. 1 10 1 10 8 4 6 3 5 4 63 5
  86. 86. 1 10 1 10 8 4 6 3 5 4 63 5 We will delete the node and make upper parent point to the child
  87. 87. 1 10 1 10 4 6 3 5 4 63 5
  88. 88. 1 10 1 10 4 6 3 5 4 63 5 Node 4 contain two children
  89. 89. 1 10 1 10 4 6 3 5 4 63 5
  90. 90. 1 10 1 10 4 6 3 5 4 63 5
  91. 91. 1 10 1 10 4 6 3 5 4 63 5
  92. 92. 1 10 1 10 4 6 3 5 5 63 5
  93. 93. 1 10 1 10 4 6 3 5 5 63 5
  94. 94. 1 10 1 10 4 6 3 5 5 63
  95. 95. 1 10 1 10 4 6 3 5 5 63
  96. 96. 1 10 1 10 6 3 5 5 63 Here we make delete for a root that contain 2 children to solve it we will choose from our tree the largest value in the right or the smallest value in the left.
  97. 97. BST Search Performance: Successful Search: Best Case – 1 comparison Worst Case – log2N comparisons Unsuccessful Search: Best Case =Worst Case – log2N comparisons
  98. 98. Applications of BST • allows you to do range searches efficiently. • Used in internet routers. • Finding square root of given number • In The language rules and are often used to build dictionaries. • One of the best ways can be modified to make search of data but sometimes having problem in insertion & deletion that can be complex when achieving best searching using types of BSTs.
  99. 99. AVL (Adelson-Velskii and Landis) tree. An AVL tree is identical to a BST except height of the left and right subtrees can differ by at most 1. height of an empty tree is defined to be (–1). AVL Tree 5 8 2 4 3 1 7 0 1 2 3 Height Height is the longest distance from the root to the most far leaf
  100. 100. 6 81 4 3 1 5 height 0 1 2 3 We have more than one node as difference between the left and right sub trees that can make order of search and insert and delete become more than log2n So we need to make re-balance to the tree to make it AVL Not an AVL tree
  101. 101. Note : Balanced factor for every node :that is the difference between the right and left sub trees to make a decision if the difference more than 1 or less than -1 it will need re-balance.
  102. 102. -1 01 0 0 0 0 -1 0 1 00 0 0 00 0
  103. 103. -1 01 0 0 0 0 -1 0 1 00 0 0 00 0U1 U2 U3 U4 U5 U6 U7 U8 U9 U10 U11 U12 B BB B B B Insertions and effect on balance
  104. 104. Tree becomes unbalanced only if the newly inserted node Example: Its balance now 1 0 A B T1 T3 T2 1
  105. 105. 2 1 A B T1 T3 T2 1 2 When inset new node in the left hand side it become not balance and need to re-balance it
  106. 106. 2 1 A B T1 T3 T2 1 2 0 0 A B T1 T3T2 new new We can choose node B and make self-balance but it will make more efforts in insertion and deletion but make search then order of log2n
  107. 107. AVL Tree Building Example  Let us work through an example that inserts numbers in a balanced search tree. We will check the balance after each insert and rebalance if necessary using rotations Insert(1) 1
  108. 108. Insert(2) 1 2
  109. 109. 1 2 3 -2 Insert(3) single left rotation
  110. 110. 1 2 3 -2 Insert(3) single left rotation
  111. 111. 1 2 3
  112. 112. More examples : 100 120 Balance factor =-1 0 So parent balance factor still -1its balance
  113. 113. 140 120 -1 -2 0 insert 140 Go back to every parent to see it’s balance factor It’s over weight in the right . It’s first case right- right unbalance
  114. 114. 140 120 Null To replace we make change for nodes pointers make 120 to link for 100 &100 to link for what 120 link for in the left hand side of it 0 0 0
  115. 115. Insert 80: 140 120 0 1 0 80 2-1=1 balance
  116. 116. Insert 60: 140 120 0 2 80 60 It’s note balance and this time we can let it second case as this time it’s left-left weight
  117. 117. Re-balance: 140 120 0 0 80 60 0 1 We will change two links 100 link to what 80 link to in the right (null)and 80 will link to what 100 was link to so 80 link to 100
  118. 118. Insert 90: 140 120 3 80 60 0 2 90 unbalance 1 0 0 Case 3 : when we have left- right weight
  119. 119. 140 120 0 80 60 0 90 We will choose 100 as it between 80 and 120 and make it the root 0 1 Null We make the right side of 100 to be left side of 120 and left side of 100 right side of 80 as 80<100<120 0
  120. 120. 140 120 0 80 60 0 90 0 2 Null 1 130 Insert 130: Case 4: When find right- left weight
  121. 121. To re-balance: 140120 0 80 60 0 90 0 2 1 130 Null We make the right side of 130 to be left side of 140 and left side of 130 right side of 120 as 120<130<140
  122. 122. • When we maintain a collection of data, many applications call for rearranging the data in certain order, e.g. arranging Person information in ascending order of age. • Here’s the problem statement: Given an array of N integer values, arrange the values into ascending order. • We will count the number of comparisons the algorithms make to analyze their performance. – The ideal sorting algorithm will make the least possible number of comparisons to arrange data in a designated order. • We will compare different sorting algorithms by analyzing their worst-case performance. Sorting
  123. 123. 1. Find the smallest element in the list.(by putting element by element as min and compare with all of the array) 2. Exchange the element in the first position and the smallest element. Now the smallest element is in the first position. 3. Repeat Step 1 and 2 with the list having one less element till make it sort. exchange first First min 0 1 2 3 4 5 6 7 8 5 17 23 90 12 44 38 84 77 0 1 2 3 4 5 6 7 8 5 17 23 90 12 44 38 84 77 sorted unsorted Selection Sort
  124. 124. 0 1 2 3 4 5 6 7 8 5 17 23 90 12 44 38 84 77 sorted Pass # 1 2 3 7 5 12 23 90 17 44 38 84 77 5 12 17 90 23 44 38 84 77 5 12 17 23 38 44 77 84 90 5 12 17 23 38 44 77 84 90 8 We can note that this method make a lot of rotation we don’t need it as we can see the no difference between as 7&8
  125. 125. 1./* 2. * C program to accept N numbers and arrange them in an ascending order 3. */ 4.#include <stdio.h> 5. 6.void main() 7.{ 8.int i, j, a, n, number[30]; 9. 10.printf("Enter the value of N n"); 11.scanf("%d", &n); 12.printf("Enter the numbers n"); 13.for (i = 0; i < n; ++i) 14.scanf("%d", &number[i]); 15.for (i = 0; i < n; ++i) 16.{ 17.for (j = i + 1; j < n; ++j) 18.{ 19.if (number[i] > number[j]) 20.{ 21.a = number[i]; 22.number[i] = number[j]; 23.number[j] = a; 24.} 25.} 26.} 27.printf("The numbers arranged in ascending order are given below n"); 28.for (i = 0; i < n; ++i) 29.printf("%dn", number[i]); 30.} Selection sort C-code
  126. 126. Selection Sort Performance • We derive the total number of comparisons by counting the number of times the inner loop is executed. • For each execution of the outer loop, the inner loop is executed length – start times • Complexity O(n^2) as: • We loop n+n-1+n-2+n-3……..+1 nearly we have order of n*n. • We can use this method if I need to get first one or two or three elements for getting first element in its right place this method will be convenient but if need a very large array to be sort it will take a lot of time !! • If n=1000 we need to loop 1000000 time its to mush !! • Adv:But we can see its very simple to implement(for software). • Adv. :Very good for storage as it needn’t to waste space in memory it sort in place. • Disadv:It’s Blind sort as it will loop O(N^2) even having a sorted array.
  127. 127. Bubble Sort • With the selection sort, we make one exchange at the end of one pass. • The bubble sort improves the performance by making more than one exchange during its pass. • By making multiple exchanges, we will be able to move more elements toward their correct positions using the same number of comparisons as the selection sort makes. • The key idea of the bubble sort is to make pairwise comparisons and exchange the positions of the pair if they are out of order.
  128. 128. 0 1 2 3 4 5 6 7 8 23 17 5 90 12 44 38 84 77 One Pass of Bubble Sort exchange 17 23 5 90 12 44 38 84 77 exchange 17 5 23 90 12 44 38 84 77 exchange Ok 17 5 23 12 90 44 38 84 77 exchange 17 5 23 12 44 90 38 84 77 17 5 23 12 44 38 90 84 77 exchange 17 5 23 12 44 38 84 90 77 17 5 23 12 44 38 84 77 90 The largest value 90 is at the end of the list. 1) 2) 3) 4) 5) 6) 7) 8)
  129. 129. 1./* Bubble sorting begins */ 2.for (i = 0; i < num; i++) 3.{ 4.for (j = 0; j < (num - i - 1); j++) 5.{ 6.if (array[j] > array[j + 1]) 7.{ 8.temp = array[j]; 9.array[j] = array[j + 1]; 10.array[j + 1] = temp; 11.} 12.} 13.} 14.printf("Sorted array is...n"); 15.for (i = 0; i < num; i++) 16.{ 17.printf("%dn", array[i]); 18.} 19.}
  130. 130. Bubble Sort Performance • In the worst case, the outer while loop is executed N-1 times for carrying out N-1 passes. • For each execution of the outer loop, the inner loop is executed bottom+1 times. The number of comparisons in each successive pass is N-1, N-2, … , 1. Summing these will result in the total number of comparisons. • So the performances of the bubble sort and the selection sort are approximately equivalent. However, on the average, the bubble sort performs much better than the selection sort because it can finish the sorting without doing all N-1 passes .  Example : if we have sorted array it will loop only one time not as selection sort o(n^2)  Its not Blind sort.
  131. 131. Thank you 

×