# Object Oriented Data Structures - PowerPoint PPT Presentation

1 / 32

Object Oriented Data Structures. Recursion Introduction to Recursion Principles of Recursion Backtracking: Postponing the Work Tree-Structured Programs: Look Ahead in Games. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. C. C. C. A. A. A. A.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Object Oriented Data Structures

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

#### Presentation Transcript

Object Oriented Data Structures

RecursionIntroduction to RecursionPrinciples of RecursionBacktracking: Postponing the WorkTree-Structured Programs: Look Ahead in Games

Kruse/Ryba ch05

Recursion

Recursion

Recursion

Recursion

Recursion

Recursion

Recursion

Recursion

Kruse/Ryba ch05

C

C

C

A

A

A

A

M

M

M

M

M

M

M

M

M

M

M

M

M

M

M

A

A

A

D

D

D

B

D

D

D

D

D

D

D

Time

Kruse/Ryba ch05

Finish

M

A

D

B

C

D

D

### Tree of Subprogram Calls

Start

Kruse/Ryba ch05

1 if n = 0

n*(n-1)! if n > 0

n! =

1 if n = 0 and x not 0

x*(xn-1) if n > 0 and x not 0

xn =

Kruse/Ryba ch05

### Designing Recursive Algorithms

• Find the key step

• Find a stopping rule (base case)

• Check termination

• Draw a recursion tree

Kruse/Ryba ch05

### Tail Recursion

• The very last action of a function is a recursive call to itself

• Explicit use of a stack not necessary

• Reassign the calling parameters to the values specified in the recursive call and then repeat the function

Kruse/Ryba ch05

### Backtracking

An algorithm which attempts to complete a search for a solution to a problem by constructing partial solutions, always ensuring that the partial solutions remain consistent with the requirements. The algorithm then attempts to extend a partial solution toward completion, but when an inconsistency with the requirements of the problem occurs, the algorithm backs up (backtracks) by removing the most recently constructed part of the solution and trying another possibility.

Kruse/Ryba ch05

### Knight's Tour

Legal Knight Moves

Kruse/Ryba ch05

### Knight's Tour

Legal Knight Moves

Kruse/Ryba ch05

### Knight's Tour

Legal Knight Moves

Kruse/Ryba ch05

### Knight's Tour

10

64

1

31

33

53

26

62

63

34

51

12

7

28

25

30

32

2

11

27

52

54

61

9

41

8

24

35

50

29

6

13

18

40

5

49

36

55

3

60

23

14

46

57

39

21

16

42

37

56

4

48

19

59

44

17

22

58

20

15

38

45

47

43

Legal Knight Moves

Kruse/Ryba ch05

### Application: Depth- And Breadth-First Search

F

S

Kruse/Ryba ch05

• Hexadecimal – Base 16 numbering system 0-F

• Decimal - Base 10 numbering system 0-9

• Octal - Base 8 numbering system 0-7

• Decimal Digits 0,1,2,3,4,5,6,7,8,9

• Ocal Digits 0,1,2,3,4,5,6,7

• What’s . ?

Kruse/Ryba ch05

• 3210OctDecHex

• 0000000

• 0001111

• 0010222

• 0011333

• 0100444

• 0101555

• 0110666

• 0111777

• 10001088

• 10011199

• 10101210A

• 10111311B

• 11001412C

• 11011513D

• 11101614E

• 11111715F

3 2 1 0

23,22,21,20

8 4 2 1

Kruse/Ryba ch05

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

1010

1011

1100

1101

1110

1111

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Kruse/Ryba ch05

14

12

5

4

6

9

4

3

10

10

9

5

2

13

2

14

14

10

12

2

9

1

1

3

11

Kruse/Ryba ch05

### Class Cell Maintain:

• A number. This is an integer value used to identify the cell.

• Cells are numbered consecutively from left to right and top to bottom. (Order is important!)

• A list of neighboring cells. Each cell will have an entry in this list for all other neighbor cell that can be reached.

• A Boolean value, named visited, that will be used to mark a cell once it has been visited.

• Traversing a maze often results in dead ends, and the need to back up and start again.

• Marking cells avoids repeating effort and potentially walking around in circles.

Kruse/Ryba ch05

### Class Description

classcell

{public: cell(intn) : number(n), visited(false) {}

void addNeighbor(cell * n){neighbors.push_back(n);}voidvisit (deque<cell *> &);

protected:intnumber;boolvisited; list <cell *> neighbors;

};//end class cell

Kruse/Ryba ch05

### Class Maze

classmaze

{public: maze(istream&);voidsolveMaze();

protected: cell * start;boolfinished;deque<cell *> path; // used to hold the path // or paths currently // being traversed

};//end class maze

Kruse/Ryba ch05

maze::maze(istream & infile)// initialize maze by reading from file{intnumRows, numColumns;int counter = 1; cell * current = 0;

infile >> numRows >> numColumns;

vector <cell *> previousRow (numRows, 0);

Kruse/Ryba ch05

for(inti = 0; i < numRows; i++)for(int j=0; j<numColumns; j++)

{ current = new cell(counter++);int walls;infile >> walls;

if((i>0) && ((walls & 0x04)==0))

if((j>0> && ((walls & 0x08) == 0))

}//end maze()

Kruse/Ryba ch05

1

2

2

3

3

3

14

12

5

4

6

4

4

4

9

4

3

10

10

5

5

1

5

4

2

3

9

5

2

13

2

previousRow[0]

previousRow[1]

14

14

10

12

2

9

1

1

3

11

previousRow[2]

previousRow[4]

previousRow[3]

previousRow[j]

Kruse/Ryba ch05

2

2

3

3

3

14

12

5

4

6

4

4

4

9

4

3

10

10

5

5

2

3

5

4

6

9

5

2

13

2

6

6

1

previousRow[1]

14

14

10

12

2

9

1

1

3

11

previousRow[2]

previousRow[3]

previousRow[4]

1

previousRow[j]

previousRow[0]

Kruse/Ryba ch05

2

2

2

3

3

3

14

12

5

4

6

4

4

4

9

4

3

10

10

5

5

5

7

3

4

6

9

5

2

13

2

7

7

6

6

1

14

14

10

12

2

9

1

1

3

11

previousRow[2]

previousRow[3]

previousRow[4]

1

previousRow[j]

previousRow[0]

previousRow[1]

Kruse/Ryba ch05

void maze::solveMaze()// solve the maze puzzle

{ start->visit(path);while ((!finished) && (! path.empty()))

{ cell * current = path.front();path.pop_front(); finished = current->visit(path);}if ( ! finished)cout << “no solution found\n”;}//end solveMaze()

Kruse/Ryba ch05

bool cell::visit(deque<cell *> & path) { //depth firstif(visited) // already been herereturn false; visited = true; // mark as visitedcout << “visiting cell “ << number << endl;if (number == 1)

{cout << “puzzle solved\n”;return true;} list <cell *>:: iterator start, stop; start = neighbors.begin(); stop = neighbors.end();for ( ; start != stop; ++start)if (! (*start)->visited)path.push_front(*start);return false;

}

Kruse/Ryba ch05

bool cell::visit(deque<cell *> & path){// breadth firstif(visited) // already been herereturn false; visited = true; // mark as visitedcout << “visiting cell “ << number << endl;if (number == 1)

{cout << “puzzle solved\n”; return true;} list <cell *>:: iterator start, stop; start = neighbors.begin(); stop = neighbors.end();for ( ; start != stop; ++start)if (! (*start)->visited)path.push_back(*start);return false;

}

Kruse/Ryba ch05

### Depth First vs. Breadth First

• Because all paths of length one are investigated before examining paths of length two, and all paths of length two before examining paths of length three, a breadth-first search is guaranteed to always discover a path from start to goal containing the fewest steps, whenever such a path exists.

Kruse/Ryba ch05

### Depth First vs. Breadth First

• Because one path is investigated before any alternatives are examined, a depth-first search may, if it is lucky, discover a solution more quickly than the equivalent breadth-first algorithm.

Kruse/Ryba ch05

### Depth First vs. Breadth First

• In particular, suppose for a particular problem that some but not all paths are infinite, and at least one path exists from start to goal that is finite.

• A breadth-first search is guaranteed to find a shortest solution.

• A depth-first search may have the unfortunate luck to pursue a never-ending path, and can hence fail to find a solution.

Kruse/Ryba ch05

Chapter 5 Ripples Away

Kruse/Ryba ch05