#include #include using namespace std; /* 0 1 2 3 4 5 +-----+-----+-----+-----+-----+-----+ | 0 | 1 | 1 | 1 | 1 | 1 | 0 +-----+-----+-----+-----+-----+-----+ | 1 | 2 | 3 | 4 | 5 | 6 | 1 +-----+-----+-----+-----+-----+-----+ | 1 | 3 | 6 | 10 | 15 | 21 | 2 +-----+-----+-----+-----+-----+-----+ | 1 | 4 | 10 | 20 | 35 | 56 | 3 +-----+-----+-----+-----+-----+-----+ | 1 | 5 | 15 | 35 | 70 | 126 | 4 +-----+-----+-----+-----+-----+-----+ | 1 | 6 | 21 | 56 | 126 | 252 | 5 +-----+-----+-----+-----+-----+-----+ npath(0,0) = 0 npath(r,0) = 1 npath(0,c) = 1 npath(r,c) = npath(r,c-1) + npath(r-1,c) npath(2,2) = npath(2,1)+npath(1,2) = npath(2,0)+npath(1,1)+npath(1,1)+npath(0,2) = 1 + npath(1,0)+npath(0,1)+ npath(1,0)+npath(0,1)+1 = 1 + 1+1+1+1 + 1 = 6 npath(10,10)=npath(9,10) + npath(10,9) =npath(8,10)+npath(9,9) + npath(9,9)+npath(9,8) Print all of the possible paths. */ // recursion int npath( int r, int c ) { if ( r == 0 && c == 0 ) return 0; if ( r == 0 ) return 1; if ( c == 0 ) return 1; else return npath(r, c-1) + npath(r-1, c); } int cached[20][20] = { 0 }; int npath2( int r, int c ) { if ( r == 0 && c == 0 ) return 0; if ( r == 0 ) return 1; if ( c == 0 ) return 1; else { if ( cached[r][c] == 0 ) cached[r][c] = npath2(r, c-1) + npath2(r-1, c); return cached[r][c]; } } int npath3( int r, int c ) // bottom-up { int m[20][20] = { 0 }; m[0][0] = 0; for ( int i = 1; i <= r; i++ ) m[i][0] = 1; for ( int j = 1; j <= c; j++ ) m[0][j] = 1; for ( int i = 1; i <= r; i++ ) { for ( int j = 1; j <= c; j++ ) { m[i][j] = m[i-1][j] + m[i][j-1]; } } return m[r][c]; } int main() { clock_t start, end; int ans; start = clock(); ans = npath(15, 15); end = clock(); cout << ans << " (time: " << ((end-start) * 1. / CLOCKS_PER_SEC) << ")" << endl; start = clock(); ans = npath2(15, 15); end = clock(); cout << ans << " (time2: " << ((end-start) * 1. / CLOCKS_PER_SEC) << ")" << endl; start = clock(); ans = npath3(15, 15); end = clock(); cout << ans << " (time3: " << ((end-start) * 1. / CLOCKS_PER_SEC) << ")" << endl; return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

#include <iostream>
#include <ctime>

using namespace std;

/*

0 1 2 3 4 5
+-----+-----+-----+-----+-----+-----+
| 0 | 1 | 1 | 1 | 1 | 1 | 0
+-----+-----+-----+-----+-----+-----+
| 1 | 2 | 3 | 4 | 5 | 6 | 1
+-----+-----+-----+-----+-----+-----+
| 1 | 3 | 6 | 10 | 15 | 21 | 2
+-----+-----+-----+-----+-----+-----+
| 1 | 4 | 10 | 20 | 35 | 56 | 3
+-----+-----+-----+-----+-----+-----+
| 1 | 5 | 15 | 35 | 70 | 126 | 4
+-----+-----+-----+-----+-----+-----+
| 1 | 6 | 21 | 56 | 126 | 252 | 5
+-----+-----+-----+-----+-----+-----+

npath(0,0) = 0
npath(r,0) = 1
npath(0,c) = 1
npath(r,c) = npath(r,c-1) + npath(r-1,c)

npath(2,2) = npath(2,1)+npath(1,2)
= npath(2,0)+npath(1,1)+npath(1,1)+npath(0,2)
= 1 + npath(1,0)+npath(0,1)+ npath(1,0)+npath(0,1)+1
= 1 + 1+1+1+1 + 1 = 6

npath(10,10)=npath(9,10) + npath(10,9)
=npath(8,10)+npath(9,9) + npath(9,9)+npath(9,8)


Print all of the possible paths.

*/

// recursion
int npath( int r, int c )
{
if ( r == 0 && c == 0 ) return 0;
if ( r == 0 ) return 1;
if ( c == 0 ) return 1;
else return npath(r, c-1) + npath(r-1, c);
}

int cached[20][20] = { 0 };
int npath2( int r, int c )
{
if ( r == 0 && c == 0 ) return 0;
if ( r == 0 ) return 1;
if ( c == 0 ) return 1;
else
{
if ( cached[r][c] == 0 ) cached[r][c] = npath2(r, c-1) + npath2(r-1, c);
return cached[r][c];
}
}

int npath3( int r, int c ) // bottom-up
{
int m[20][20] = { 0 };
m[0][0] = 0;

for ( int i = 1; i <= r; i++ ) m[i][0] = 1;
for ( int j = 1; j <= c; j++ ) m[0][j] = 1;

for ( int i = 1; i <= r; i++ )
{
for ( int j = 1; j <= c; j++ )
{
m[i][j] = m[i-1][j] + m[i][j-1];
}
}

return m[r][c];

}


int main()
{
clock_t start, end;
int ans;
start = clock();
ans = npath(15, 15);
end = clock();

cout << ans << " (time: " << ((end-start) * 1. / CLOCKS_PER_SEC) << ")" << endl;

start = clock();
ans = npath2(15, 15);
end = clock();

cout << ans << " (time2: " << ((end-start) * 1. / CLOCKS_PER_SEC) << ")" << endl;

start = clock();
ans = npath3(15, 15);
end = clock();

cout << ans << " (time3: " << ((end-start) * 1. / CLOCKS_PER_SEC) << ")" << endl;

return 0;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
Types of Loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education