Download presentation
Presentation is loading. Please wait.
Published byClaude Haynes Modified over 9 years ago
1
Data Structures ساختمان داده ها مظفر بگ محمدی دانشکده فنی دانشگاه ایلام
2
Grading کتاب : کتاب : ساختمان داده ها به زبان C++ ساختمان داده ها به زبان C++ نوشته : هورویتز - ساهنی – مهتا نوشته : هورویتز - ساهنی – مهتا ترجمه : امیر علیخان زاده ترجمه : امیر علیخان زاده انتشارات خراسان انتشارات خراسان نمره : نمره : تمرین : 15% تمرین : 15% برنامه نويسي : 15% برنامه نويسي : 15% میان ترم : 35% میان ترم : 35% پایان ترم : 35% پایان ترم : 35%
3
Cautions کپی کردن از روی تمرین دیگران با نمره صفر ( هم برای کپی کننده و هم برای کسی که حل تمرین را در اختیار دیگران قرار دهد ) مواجه خواهد شد. کپی کردن از روی تمرین دیگران با نمره صفر ( هم برای کپی کننده و هم برای کسی که حل تمرین را در اختیار دیگران قرار دهد ) مواجه خواهد شد. گروههای بیش از سه نفر مجاز نیستند. گروههای بیش از سه نفر مجاز نیستند. خطای گروههای سه نفره در 1.5 ضرب خواهد شد. خطای گروههای سه نفره در 1.5 ضرب خواهد شد. مثال : اگر درصد پاسخ صحیح 80 درصد باشد، نمره گروه سه نفره فرضی برابر 70 خواهد بود. مثال : اگر درصد پاسخ صحیح 80 درصد باشد، نمره گروه سه نفره فرضی برابر 70 خواهد بود.
4
Outline Recursion Recursion Algorithm complexity Algorithm complexity Arrays vs. Linked List Arrays vs. Linked List Stacks Stacks Queues Queues Trees Trees Binary trees, heaps, search trees, Binary trees, heaps, search trees, Graphs Graphs Spanning tree, minimum spanning tree, shortest path tree, Spanning tree, minimum spanning tree, shortest path tree, Sorting Sorting Quick sort, Insertion sort, heap sort, merge sort, radix sort Quick sort, Insertion sort, heap sort, merge sort, radix sort Hashing 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 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 Requires terminating condition: Case for which recursion is no longer needed
7
Factorial Recursion Factorial:n! = n * (n-1)! Factorial:n! = n * (n-1)! Base Case => 0! = 1 Base Case => 0! = 1 Smaller problem => Solving (n-1)! Smaller problem => Solving (n-1)! Implementation: Implementation: long factorial(long inputValue) { if (inputValue == 0) return 1; if (inputValue == 0) return 1; else return inputValue * factorial(inputValue - 1); else return inputValue * factorial(inputValue - 1);}
8
Searching We want to find whether or not an input value is in a sorted list: 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? Better method? Use fact that we know the list is sorted Use fact that we know the list is sorted Cut what we have to search in half each time Cut what we have to search in half each time Compare input to middle 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 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 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. 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]? 1 st iteration: Left = 0, Right = 7, Middle = 3, List[Middle] = 10 Check 8 == 10 => No, 8 No, 8 < 10 2 nd iteration: Left = 0, Right = 2, Middle = 1, List[Middle] = 2 Check 8 == 2 => No, 8 > 2 3 rd iteration: Left = 2, Right = 2, Middle = 2 Check 8 == 8 => Yes, Found It!
12
Searching: Binary Search Binary Search Method: Binary Search Method: Number of operations to find input if in the list: Number of operations to find input if in the list: Dependent on position in list Dependent on position in list 1 operation if middle 1 operation if middle Log 2 n operations maximum Log 2 n operations maximum Number of operations to find input if not in the list: Number of operations to find input if not in the list: Log 2 n operations maximum Log 2 n operations maximum
13
Recursive Binary Search Two requirements for recursion: Two requirements for recursion: Same algorithm, smaller problem Same algorithm, smaller problem Termination condition Termination condition Binary search? Binary search? Search in half of previous array Search in half of previous array Stop when down to one element 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: Fibonacci Sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, … 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, … Simple definition: Simple definition: Fib[0] = 1 Fib[0] = 1 Fib[1] = 1 Fib[1] = 1 Fib[N] = Fib(N-1) + Fib(N-2) Fib[N] = Fib(N-1) + Fib(N-2)
16
Recursive Fibonacci int fibonacci(int input) { if ((input == 0) || (input == 1)) return 1; if ((input == 0) || (input == 1)) return 1; else return (fibonacci(input-1) + fibonacci(input-2)); else return (fibonacci(input-1) + fibonacci(input-2));}
17
Iterative Fibonacci int fibonacci(int input) { int first = 1; int first = 1; int second = 1; int second = 1; int temp; int temp; for (int k = 0; k < input; k++) for (int k = 0; k < input; k++) { temp = first; temp = first; first = second; first = second; second = temp + second; second = temp + second; } return first; return first;}
18
Types of Recursion Linear Recursion: Linear Recursion: 1 recursive call per function 1 recursive call per function Factorial, Binary Search examples Factorial, Binary Search examples Tree Recursion: Tree Recursion: 2 or more recursive calls per function 2 or more recursive calls per function Fibonacci Example Fibonacci Example
19
Efficiency of Recursion Recursion can sometimes be slower than iterative code Recursion can sometimes be slower than iterative code Two main reasons: Two main reasons: Program stack usage Program stack usage When using recursive functions, every recursive call is added to the stack and it grows fast. When using recursive functions, every recursive call is added to the stack and it grows fast. Result generation Result generation
20
Efficiency of Recursion Stack Usage: Stack Usage: When a function is called by a program, that function is placed on the program call stack: 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: Every stack entry maintains information about the function: Where to return to when the function completes Where to return to when the function completes Storage for local variables Storage for local variables Pointers or copies of arguments passed in Pointers or copies of arguments passed in main() getData() readFile() Returns file data to be used in getData() Returns formatted data to be printed in main()
21
Efficiency of Recursion Another Reason for Slowdowns [Tree Recursion] Another Reason for Slowdowns [Tree Recursion] Traditional Recursion doesn’t save answers as it executes Traditional Recursion doesn’t save answers as it executes Fib(5) 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 Solution: Dynamic programming – saving answers as you go and reusing them
22
Dynamic Programming Fibonacci Problem Fibonacci Problem We know the upper bound we are solving for We know the upper bound we are solving for I.e. Fibonacci (60) = 60 different answers I.e. Fibonacci (60) = 60 different answers Generate an array 60 long and initialize to –1 Generate an array 60 long and initialize to –1 Every time we find a solution, fill it in the array 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. 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.