1 / 22

Data Structures ساختمان داده ها

Data Structures ساختمان داده ها. مظفر بگ محمدی دانشکده فنی دانشگاه ایلام. Grading. کتاب: ساختمان داده ها به زبان C++ نوشته: هورویتز- ساهنی – مهتا ترجمه: امیر علیخان زاده انتشارات خراسان نمره: تمرین: 15% برنامه نويسي: 15% میان ترم: 35% پایان ترم: 35%. Cautions.

ddimaggio
Download Presentation

Data Structures ساختمان داده ها

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structuresساختمان داده ها مظفر بگ محمدی دانشکده فنی دانشگاه ایلام

  2. Grading • کتاب: • ساختمان داده ها به زبان C++ • نوشته: هورویتز- ساهنی – مهتا • ترجمه: امیر علیخان زاده • انتشارات خراسان • نمره: • تمرین: 15% • برنامه نويسي: 15% • میان ترم: 35% • پایان ترم: 35%

  3. Cautions • کپی کردن از روی تمرین دیگران با نمره صفر (هم برای کپی کننده و هم برای کسی که حل تمرین را در اختیار دیگران قرار دهد) مواجه خواهد شد. • گروههای بیش از سه نفر مجاز نیستند. • خطای گروههای سه نفره در 1.5 ضرب خواهد شد. • مثال: اگر درصد پاسخ صحیح 80 درصد باشد، نمره گروه سه نفره فرضی برابر 70 خواهد بود.

  4. Outline • Recursion • Algorithm complexity • Arrays vs. Linked List • Stacks • Queues • Trees • Binary trees, heaps, search trees, • Graphs • Spanning tree, minimum spanning tree, shortest path tree, • Sorting • Quick sort, Insertion sort, heap sort, merge sort, radix sort • Hashing

  5. Recursion

  6. Recursion: Definition • Function that solves a problem by relying on itself to compute the correct solution for a smaller version of the problem • Requires terminating condition: Case for which recursion is no longer needed

  7. Factorial Recursion • Factorial: n! = n * (n-1)! • Base Case => 0! = 1 • Smaller problem => Solving (n-1)! • Implementation: long factorial(long inputValue) { if (inputValue == 0) return 1; else return inputValue * factorial(inputValue - 1); }

  8. Searching • We want to find whether or not an input value is in a sorted list: 8 in [1, 2, 8, 10, 15, 32, 63, 64]? 33 in [1, 2, 8, 10, 15, 32, 63, 64]? ------------------------------------------------------ int index = 0; while (index < listSize) { if (list[index] == input) return index; index++; } return –1;

  9. Searching • Better method? • Use fact that we know the list is sorted • Cut what we have to search in half each time • Compare input to middle • If input greater than middle, our value has be in the elements on the right side of the middle element • If input less than middle, our value has to be in the elements on the left side of the middle element • If input equals middle, we found the element.

  10. Searching: Binary Search for (int left = 0, right = n –1; left <= right;) { middle =(left + right) / 2; if (input == list[middle]) return middle; else if (input < list[middle]) right = middle – 1; else left = middle + 1; } return – 1;

  11. Searching: Binary Search 8 in [1, 2, 8, 10, 15, 32, 63, 64]? 1st iteration: Left = 0, Right = 7, Middle = 3, List[Middle] = 10 Check 8 == 10 => No, 8 < 10 2nd iteration: Left = 0, Right = 2, Middle = 1, List[Middle] = 2 Check 8 == 2 => No, 8 > 2 3rd iteration: Left = 2, Right = 2, Middle = 2 Check 8 == 8 => Yes, Found It!

  12. Searching: Binary Search • Binary Search Method: • Number of operations to find input if in the list: • Dependent on position in list • 1 operation if middle • Log2 n operations maximum • Number of operations to find input if not in the list: • Log2 n operations maximum

  13. Recursive Binary Search • Two requirements for recursion: • Same algorithm, smaller problem • Termination condition • Binary search? • Search in half of previous array • Stop when down to one element

  14. Recursive Binary Search int BinarySearch(int *list, const int input, const int left, const int right) { if (left < right) { middle =(left + right) / 2; if (input == list[middle]) return middle; else if (input < list[middle]) return BinarySearch (list, input, left, middle-1); else return BinarySearch (list, input, middle+1, right); } return – 1; }

  15. Fibonacci Computation • Fibonacci Sequence: • 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, … • Simple definition: • Fib[0] = 1 • Fib[1] = 1 • Fib[N] = Fib(N-1) + Fib(N-2)

  16. Recursive Fibonacci int fibonacci(int input) { if ((input == 0) || (input == 1)) return 1; else return (fibonacci(input-1) + fibonacci(input-2)); }

  17. Iterative Fibonacci int fibonacci(int input) { int first = 1; int second = 1; int temp; for (int k = 0; k < input; k++) { temp = first; first = second; second = temp + second; } return first; }

  18. Types of Recursion • Linear Recursion: • 1 recursive call per function • Factorial, Binary Search examples • Tree Recursion: • 2 or more recursive calls per function • Fibonacci Example

  19. Efficiency of Recursion • Recursion can sometimes be slower than iterative code • Two main reasons: • Program stack usage • When using recursive functions, every recursive call is added to the stack and it grows fast. • Result generation

  20. Efficiency of Recursion • Stack Usage: • When a function is called by a program, that function is placed on the program call stack: • Every stack entry maintains information about the function: • Where to return to when the function completes • Storage for local variables • Pointers or copies of arguments passed in readFile() Returns file data to be used in getData() getData() main() Returns formatted data to be printed in main()

  21. Efficiency of Recursion • Another Reason for Slowdowns [Tree Recursion] • Traditional Recursion doesn’t save answers as it executes • Fib(5) = Fib(4) + Fib(3) = Fib(3) + Fib(2) + Fib(3) = Fib(2) + Fib(1) + Fib(2) + Fib(3) = Fib(1) + Fib(0) + Fib(1) + Fib(2) + Fib(3) = Fib(1) + Fib(0) + Fib(1) + Fib(1) + Fib(0) + Fib(3) = Fib(1) + Fib(0) + Fib(1) + Fib(1) + Fib(0) + Fib(2) + Fib(1) = Fib(1) + Fib(0) + Fib(1) + Fib(1) + Fib(0) + Fib(1) + Fib(0) + Fib(1) • Solution: Dynamic programming – saving answers as you go and reusing them

  22. Dynamic Programming • Fibonacci Problem • We know the upper bound we are solving for • I.e. Fibonacci (60) = 60 different answers • Generate an array 60 long and initialize to –1 • Every time we find a solution, fill it in the array • Next time we look for a solution, if the value in the array for the factorial we need is not –1, use the value present.

More Related