CSE 3358 NOTE SET 8 Data Structures and Algorithms.

Slides:



Advertisements
Similar presentations
Recursion CS 367 – Introduction to Data Structures.
Advertisements

Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Factorial Recursion stack Binary Search Towers of Hanoi
CMPT 225 Stacks-part2.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Stacks (Walls & Mirrors - Chapter 6). 2 Overview The ADT Stack Array Implementation of a Stack Linked-List Implementation of a Stack Application Domain:
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
Donald Knuth [F]orget about small efficiencies, say about 97% of the time.
CSC 313 – Advanced Programming Topics. Why Recurse?  Recursion is useful, powerful technique  Often yields compact, easy-to-read code  Highlights all.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Computer Science 112 Fundamentals of Programming II Introduction to Stacks.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
1 Lecture 14 Chapter 18 - Recursion. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
CSE 1342 Programming Concepts Recursion. Overview of Recursion nRecursion is present when a function is defined in terms of itself. nThe factorial of.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
1 Joe Meehean.  call themselves directly  or indirectly void f(){... f();... } void g(){... h();... } void h(){... g();... }
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
ICS220 – Data Structures and Algorithms Dr. Ken Cosh Week 5.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Stacks Prepared By : Ramesh Kumar PGT CS(KV Chamera-II(NHPC)
Tail Recursion l The case in which a function contains only a single recursive call and it is the last statement to be executed in the function. l Tail.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Programming with Recursion
Lecture 7 Macro Review Stack Frames
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
CENG 213 Data Structures Stacks 5/15/2018 CENG 213.
Stacks & Recursion.
Data Structures and Algorithms
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Introduction to C++ Recursion
Programming with Recursion
Pointers and Linked Lists
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Stacks.
Recursion Data Structures.
Stacks & Recursion.
Stacks.
Chapter 17 Recursion.
When a function is called...
Yan Shi CS/SE 2630 Lecture Notes
Stacks & Recursion.
List Iterator Implementation
Chapter 5 Recursion.
Presentation transcript:

CSE 3358 NOTE SET 8 Data Structures and Algorithms

Finding a Path  Backtracking – a method to try all possible paths in an orderly fashion.  Exhaustive search  Stop when a path is located  Not looking for the “shortest” path yet.  Intuition:  Try all paths leading from the origination city. From each of those cities, try all paths leading from each of them. Continue until a path is found

Simple Example A B C D Requested Flight: City A  City D Use a stack to hold the path you’re currently trying.

Example 2 Requested Flight: City A  City F A B C E D F

Example 3 A B C E D Requested Flight: City A  City E

General Idea Initialize Stack Loop while the stack isn’t empty and the top of the stack isn’t the destination If need to backtrack from the city on top of stack pop off the stack Else Select a destination city from city on top of stack that hasn’t already been visited Push that city on to the stack End if End Loop

Example – Adjacency List A B C D A  B, C B  ~ C  D D  ~

Recursion Review  Base Case  Recursive Case  Uses call stack to store values of local variables while new method invocation is executing  Activation record on stack Values for parameters Local variables (or pointers to them) Return address of where to resume Pointer to caller’s activation record Return value if needed/present

Tail Recursion  Only one recursive call at the very end of a function implementation void tail (int i) { if (i > 0) { cout << i << ‘ ‘; tail(i – 1); } void nonTail (int i) { if (i > 0) { tail(i – 1); cout << i << ‘ ‘; tail(i – 1); }

Tail Recursion  Tail Recursion is a “glorified loop”  Advantages:  Some languages don’t have looping constructs void tail (int i) { if (i > 0) { cout << i << ‘ ‘; tail(i – 1); } void iterative (int i) { for (; i > 0; i--) cout << i << ‘ ‘; }

Non-tail Recursion  Makes use of the fact that the activation record is on the stack  Iterative transformation typically requires handling of a stack structure void reverse() { char ch; cin.get(ch); if (ch != ‘\n’) { reverse(); cout.put(ch); }

Non-tail Recursion void reverse() { char ch; cin.get(ch); if (ch != ‘\n’) { reverse(); cout.put(ch); } void iterativeReverse() { char ch; cin.get(ch); while (ch != ‘\n’) { stack.push(ch); cin.get(ch); } while(!stack.isEmpty()) { cout << stack.top() << ‘ ‘; stack.pop(); }

Indirect Recursion  f()  g()  h()  f()  g()……  Mathematical Example When do we stop?

?