data structures ساختمان داده ها مظفر بگ محمدی دانشکده فنی...

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

Upload: claude-haynes

Post on 28-Dec-2015

241 views

Category:

Documents


6 download

TRANSCRIPT

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

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

مظفر بگ محمدیمظفر بگ محمدی

دانشکده فنیدانشکده فنی

دانشگاه ایالمدانشگاه ایالم

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

GradingGrading :کتاب: کتاب

ساختمان داده ها به زبان ساختمان داده ها به زبانCC++++ مهتا مهتا––نوشته: هورویتز- ساهنی نوشته: هورویتز- ساهنی ترجمه: امیر علیخان زادهترجمه: امیر علیخان زادهانتشارات خراسانانتشارات خراسان

:نمره:نمره :1515تمرین: تمرین%% :1515برنامه نويسي: برنامه نويسي%% :3535میان ترم: میان ترم%% :3535پایان ترم: پایان ترم%%

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

CautionsCautions

کپی کردن از روی تمرین دیگران با نمره صفر کپی کردن از روی تمرین دیگران با نمره صفر)هم برای کپی کننده و هم برای کسی که حل )هم برای کپی کننده و هم برای کسی که حل تمرین را در اختیار دیگران قرار دهد( مواجه تمرین را در اختیار دیگران قرار دهد( مواجه

خواهد شد. خواهد شد. .گروههای بیش از سه نفر مجاز نیستند.گروههای بیش از سه نفر مجاز نیستند ضرب خواهد ضرب خواهد 1.51.5خطای گروههای سه نفره در خطای گروههای سه نفره در

شد.شد. درصد باشد، درصد باشد، 8080مثال: اگر درصد پاسخ صحیح مثال: اگر درصد پاسخ صحیح

خواهد بود. خواهد بود.7070نمره گروه سه نفره فرضی برابر نمره گروه سه نفره فرضی برابر

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

OutlineOutline Recursion Recursion Algorithm complexityAlgorithm complexity Arrays vs. Linked ListArrays vs. Linked List StacksStacks Queues Queues TreesTrees

Binary trees, heaps, search trees, Binary trees, heaps, search trees, GraphsGraphs

Spanning tree, minimum spanning tree, shortest path Spanning tree, minimum spanning tree, shortest path tree, tree,

SortingSorting Quick sort, Insertion sort, heap sort, merge sort, radix Quick sort, Insertion sort, heap sort, merge sort, radix

sortsort HashingHashing

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

RecursionRecursion

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

Recursion: DefinitionRecursion: Definition

Function that solves a problem by Function that solves a problem by relying on itself to compute the relying on itself to compute the correct solution for a correct solution for a smallersmaller version version of the problemof the problem

Requires Requires terminating conditionterminating condition: : Case for which recursion is no Case for which recursion is no longer neededlonger needed

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

Factorial RecursionFactorial Recursion

Factorial:Factorial: n! = n * (n-1)!n! = n * (n-1)! Base Case => 0! = 1Base Case => 0! = 1 Smaller problem => Solving (n-1)!Smaller problem => Solving (n-1)!

Implementation:Implementation:

long factorial(long inputValue)long factorial(long inputValue)

{{

if (inputValue == 0) return 1;if (inputValue == 0) return 1;

else return inputValue * else return inputValue * factorial(inputValue - 1);factorial(inputValue - 1);

}}

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

SearchingSearching We want to find whether or not an input value is We want to find whether or not an input value is

in a sorted list:in a sorted list:

8 in [1, 2, 8, 10, 15, 32, 63, 64]?8 in [1, 2, 8, 10, 15, 32, 63, 64]?33 in [1, 2, 8, 10, 15, 32, 63, 64]?33 in [1, 2, 8, 10, 15, 32, 63, 64]?------------------------------------------------------------------------------------------------------------

int index = 0;int index = 0;while (index < listSize)while (index < listSize){{

if (list[index] == input) return index;if (list[index] == input) return index;index++;index++;

}}return –1;return –1;

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

SearchingSearching

Better method? Better method? Use fact that we know the list is sortedUse fact that we know the list is sorted Cut what we have to search in half each timeCut what we have to search in half each time

Compare input to middleCompare input to middle If input greater than middle, our value has be in If input greater than middle, our value has be in

the elements on the right side of the middle the elements on the right side of the middle elementelement

If input less than middle, our value has to be in the If input less than middle, our value has to be in the elements on the left side of the middle elementelements on the left side of the middle element

If input equals middle, we found the element.If input equals middle, we found the element.

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

Searching: Binary SearchSearching: Binary Search

for (int left = 0, right = n –1; left <= right;)for (int left = 0, right = n –1; left <= right;)

{{

middle =(left + right) / 2;middle =(left + right) / 2;

if (input == list[middle]) return middle;if (input == list[middle]) return middle;

else if (input < list[middle]) right = middle else if (input < list[middle]) right = middle – 1;– 1;

else left = middle + 1;else left = middle + 1;

}}

return – 1;return – 1;

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

Searching: Binary SearchSearching: Binary Search

8 in [1, 2, 8, 10, 15, 32, 63, 64]?8 in [1, 2, 8, 10, 15, 32, 63, 64]?11stst iteration: iteration:

Left = 0, Right = 7, Middle = 3, List[Middle] = Left = 0, Right = 7, Middle = 3, List[Middle] = 1010

Check 8 == 10 => No, 8 < 10Check 8 == 10 => No, 8 < 10

22ndnd iteration: iteration:Left = 0, Right = 2, Middle = 1, List[Middle] = 2Left = 0, Right = 2, Middle = 1, List[Middle] = 2Check 8 == 2 => No, 8 > 2Check 8 == 2 => No, 8 > 2

33rdrd iteration: iteration:Left = 2, Right = 2, Middle = 2Left = 2, Right = 2, Middle = 2Check 8 == 8 => Yes, Found It!Check 8 == 8 => Yes, Found It!

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

Searching: Binary SearchSearching: Binary Search

Binary Search Method:Binary Search Method: Number of operations to find input if in Number of operations to find input if in

the list:the list: Dependent on position in listDependent on position in list 1 operation if middle1 operation if middle LogLog22 n operations maximum n operations maximum

Number of operations to find input if Number of operations to find input if not in the list:not in the list: LogLog22 n operations maximum n operations maximum

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

Recursive Binary SearchRecursive Binary Search

Two requirements for recursion:Two requirements for recursion: Same algorithm, smaller problemSame algorithm, smaller problem Termination conditionTermination condition

Binary search?Binary search? Search in half of previous arraySearch in half of previous array Stop when down to one elementStop when down to one element

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

Recursive Binary SearchRecursive Binary Searchint BinarySearch(int *list, const int input, const int left, int BinarySearch(int *list, const int input, const int left,

const int right)const int right){{

if (left < right)if (left < right){{

middle =(left + right) / 2;middle =(left + right) / 2;if (input == list[middle]) return middle;if (input == list[middle]) return middle;else if (input < list[middle]) return else if (input < list[middle]) return

BinarySearch (list, input, left, middle-1);BinarySearch (list, input, left, middle-1);else return BinarySearch (list, input, middle+1, else return BinarySearch (list, input, middle+1,

right);right);}}

return – 1;return – 1;}}

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

Fibonacci ComputationFibonacci 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] = 1Fib[0] = 1 Fib[1] = 1Fib[1] = 1 Fib[N] = Fib(N-1) + Fib(N-2)Fib[N] = Fib(N-1) + Fib(N-2)

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

Recursive FibonacciRecursive Fibonacci

int fibonacci(int input)int fibonacci(int input)

{{

if ((input == 0) || (input == 1)) if ((input == 0) || (input == 1)) return 1;return 1;

else return (fibonacci(input-1) + else return (fibonacci(input-1) + fibonacci(input-2));fibonacci(input-2));

}}

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

Iterative FibonacciIterative Fibonacciint fibonacci(int input)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;}}

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

Types of RecursionTypes of Recursion

Linear Recursion:Linear Recursion: 1 recursive call per function1 recursive call per function Factorial, Binary Search examplesFactorial, Binary Search examples

Tree Recursion:Tree Recursion: 2 or more recursive calls per function2 or more recursive calls per function Fibonacci ExampleFibonacci Example

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

Efficiency of RecursionEfficiency of Recursion

Recursion can sometimes be slower Recursion can sometimes be slower than iterative codethan iterative code

Two main reasons:Two main reasons: Program stack usageProgram stack usage

When using recursive functions, every When using recursive functions, every recursive call is added to the stack and it recursive call is added to the stack and it grows fast.grows fast.

Result generationResult generation

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

Efficiency of RecursionEfficiency of Recursion Stack Usage:Stack Usage:

When a function is called by a program, that function is When a function is called by a program, that function is placed on the program call stack:placed on the program call stack:

Every stack entry maintains information about the Every stack entry maintains information about the function:function: Where to return to when the function completesWhere to return to when the function completes Storage for local variablesStorage for local variables Pointers or copies of arguments passed inPointers 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()

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

Efficiency of RecursionEfficiency of Recursion Another Reason for Slowdowns [Tree Recursion]Another Reason for Slowdowns [Tree Recursion]

Traditional Recursion doesn’t save answers as it executesTraditional Recursion doesn’t save answers as it executes Fib(5) Fib(5) = Fib(4) + Fib(3)= Fib(4) + Fib(3)= Fib(3) + Fib(2) + Fib(3)= Fib(3) + Fib(2) + Fib(3)= Fib(2) + Fib(1) + 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(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(3)= Fib(1) + Fib(0) + Fib(1) + Fib(1) + Fib(0) + Fib(2) + = Fib(1) + Fib(0) + Fib(1) + Fib(1) + Fib(0) + Fib(2) +

Fib(1)Fib(1)= Fib(1) + Fib(0) + Fib(1) + Fib(1) + Fib(0) + Fib(1) + = Fib(1) + Fib(0) + Fib(1) + Fib(1) + Fib(0) + Fib(1) +

Fib(0) + Fib(1)Fib(0) + Fib(1) Solution: Dynamic programming – saving answers Solution: Dynamic programming – saving answers

as you go and reusing themas you go and reusing them

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

Dynamic ProgrammingDynamic Programming

Fibonacci ProblemFibonacci Problem We know the upper bound we are solving forWe know the upper bound we are solving for I.e. Fibonacci (60) = 60 different answersI.e. Fibonacci (60) = 60 different answers Generate an array 60 long and initialize to –Generate an array 60 long and initialize to –

11 Every time we find a solution, fill it in the Every time we find a solution, fill it in the

arrayarray Next time we look for a solution, if the value Next time we look for a solution, if the value

in the array for the factorial we need is not –in the array for the factorial we need is not –1, use the value present.1, use the value present.