-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ba1657
commit 2d4d1c4
Showing
19 changed files
with
1,409 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/bin/bash | ||
# CS 302, assignment #1 test script. | ||
|
||
# --------------------------------------------------------- | ||
# Initial checks. | ||
# Ensure executable file exists. | ||
|
||
if [ -z $1 ]; then | ||
echo "Error, must provide executable file name." | ||
exit | ||
fi | ||
|
||
if [ ! -e $1 ] || [ ! -x $1 ]; then | ||
echo "Error, unable to find $1 executable file." | ||
exit | ||
fi | ||
|
||
# --------------------------------------------------------- | ||
# Define script variables and function | ||
|
||
bolded=`tput bold` | ||
normal=`tput sgr0` | ||
|
||
pause() { | ||
if [ "$1" = "skip" ]; then | ||
echo | ||
else | ||
echo | ||
echo -n "Press return to continue" | ||
read | ||
clear | ||
fi | ||
} | ||
|
||
# --------------------------------------------------------- | ||
# Display initial page | ||
|
||
clear | ||
echo | ||
echo | ||
echo ${bolded}Assignment 1 - Execution Times Utility${normal} | ||
echo | ||
echo | ||
echo "The total execution times may exceed 3 hours" | ||
echo "depending on the machine..." | ||
echo | ||
echo | ||
echo "The times are recorded in 'a1times.txt' " | ||
echo | ||
echo | ||
echo | ||
pause $2 | ||
|
||
echo -e "Executions Times for the maximal matrix sum path problem.\n\n" > a1times.txt | ||
|
||
# --------------------------------------------------------- | ||
# Begin executions -> Integer 0/1 Knapsack Problem | ||
|
||
echo "Executions for the maximal matrix sum path problem." | ||
echo -e "Executions for the maximal matrix sum path problem." >> a1times.txt | ||
echo | ||
|
||
for cnt in {10..35..5} | ||
do | ||
echo -e "\n\n\n============================================================\n" >> a1times.txt | ||
echo "Execution Tests, $cnt item count." | ||
echo | ||
(time ./$1 -rc $cnt) 2>> a1times.txt 1>>a1times.txt | ||
echo -e "\n" >> a1times.txt | ||
(time ./$1 -dy $cnt) 2>> a1times.txt 1>>a1times.txt | ||
echo -e "\n" >> a1times.txt | ||
done | ||
|
||
echo -e "\n-----------------------------------------\n" >> a1times.txt | ||
|
Binary file added
BIN
+54.9 KB
Assign1/asst1Files/bestPath (prabhaskumra-VirtualBox's conflicted copy 2018-09-04).o
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
#include "bestPath.h" | ||
#include <iostream> | ||
#include <iomanip> | ||
|
||
using namespace std; | ||
|
||
|
||
// this will initilize class variables | ||
bestPath::bestPath() | ||
{ | ||
|
||
} | ||
|
||
// displays matrix in formatted manner | ||
void bestPath::displayMatrix() | ||
{ | ||
cout << "Matrix:" << endl << "Order: " << matrixOrder << endl << endl; | ||
|
||
for(int j = 0; j<matrixOrder; j++){ | ||
for(int k = 0; k < matrixOrder; k++){ | ||
cout <<fixed <<right << setw(4) << matrix[j][k] <<" "; | ||
} | ||
cout << endl; | ||
} | ||
//cout << matrix[][] << endl; | ||
} | ||
|
||
// create matrix | ||
bool bestPath::createMatrix(const int order) | ||
{ | ||
matrixOrder = order; | ||
|
||
if(order < MIN_ORDER || order > MAX_ORDER) | ||
{ | ||
cout << "Error, invalid matrix order" << endl; | ||
return false; | ||
} | ||
|
||
matrix = new int* [matrixOrder]; | ||
for(int i = 0; i<matrixOrder; i++) | ||
matrix[i] = new int[matrixOrder]; | ||
|
||
// populating the array | ||
for(int j = 0; j<matrixOrder; j++) | ||
for(int k = 0; k < matrixOrder; k++) | ||
matrix[j][k] = rand()%LIMIT+1; | ||
|
||
return true; | ||
|
||
} | ||
// solve the matrix path using dynamic programming apptoach | ||
int bestPath::bestPthDY() | ||
{ | ||
|
||
int ** matrix2 = new int* [matrixOrder]; | ||
for(int i = 0; i<matrixOrder; i++) | ||
matrix2[i] = new int[matrixOrder]; | ||
|
||
for(int j = 0; j<matrixOrder; j++) | ||
for(int k = 0; k < matrixOrder; k++) | ||
matrix2[j][k] = matrix[j][k]; | ||
|
||
|
||
for(int i = 1; i < matrixOrder; i++) | ||
{ | ||
for (int j = 0; j < matrixOrder; j++) | ||
{ | ||
|
||
if(j > 0 && j < matrixOrder -1) | ||
matrix2[i][j] += max(matrix2[i-1][j-1], matrix2[i-1][j+1]); | ||
|
||
else if(j==0) | ||
matrix2[i][j] += matrix2[i-1][j+1]; | ||
|
||
else if(j==matrixOrder-1) | ||
matrix2[i][j] += matrix2[i-1][j-1]; | ||
|
||
} | ||
} | ||
|
||
int sum = 0; | ||
for(int i = 0; i < matrixOrder; i++) | ||
sum = max(matrix2[matrixOrder-1][i], sum); | ||
|
||
|
||
for(int i = 0; i<matrixOrder; i++) | ||
{ | ||
delete [] matrix2[i]; | ||
} | ||
delete[] matrix2; | ||
|
||
return sum; | ||
} | ||
// solve matrix using maximal sum path problem by calling the private recursive function | ||
int bestPath::bestPthREC() | ||
{ | ||
int arr[matrixOrder]; | ||
|
||
|
||
for(int c = 0; c < matrixOrder; c++) | ||
arr[c] = bestPthREC1(0, c); | ||
|
||
int temp = 0; | ||
|
||
for(int i=0 ;i<matrixOrder; i++) | ||
{ | ||
if(arr[i] > temp) | ||
temp = arr[i]; | ||
} | ||
|
||
return temp; | ||
} | ||
|
||
|
||
// solve the path problem recursively | ||
int bestPath::bestPthREC1(int r, int c) | ||
{ | ||
if(c < 0 || c >= matrixOrder) | ||
return 0; | ||
|
||
else if(r == matrixOrder - 1) | ||
return matrix[r][c]; | ||
|
||
else | ||
{ | ||
int value = 0; | ||
|
||
value = (max ( (matrix[r][c] + bestPthREC1(r+1, c-1)) , (matrix[r][c] + bestPthREC1(r+1, c+1))) ); | ||
|
||
return value; | ||
} | ||
|
||
} | ||
|
||
//destructer | ||
bestPath::~bestPath() | ||
{ | ||
for(int i = 0; i<matrixOrder; i++) | ||
{ | ||
delete [] matrix[i]; | ||
} | ||
delete[] matrix; | ||
|
||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <string> | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class bestPath | ||
{ | ||
public: | ||
bestPath(); | ||
~bestPath(); | ||
void displayMatrix(); | ||
bool createMatrix(const int); | ||
int bestPthDY(); | ||
int bestPthREC(); | ||
|
||
private: | ||
int matrixOrder; | ||
int ** matrix; | ||
static const int LIMIT = 999; | ||
static const int MIN_ORDER = 5; | ||
static const int MAX_ORDER = 100; | ||
|
||
int bestPthREC1(int, int); | ||
|
||
|
||
}; | ||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+89.3 KB
Assign1/asst1Files/main (prabhaskumra-VirtualBox's conflicted copy 2018-09-04)
Binary file not shown.
Binary file added
BIN
+92.3 KB
Assign1/asst1Files/main (prabhaskumra-VirtualBox's conflicted copy 2018-09-04).o
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Prabhas Kumra | ||
// CS 302 Assignment #1 | ||
// Provided Main Program | ||
|
||
#include <iostream> | ||
#include <cstdlib> | ||
#include <string> | ||
#include <sstream> | ||
#include "bestPath.h" | ||
|
||
using namespace std; | ||
|
||
// ********************************************************************* | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
|
||
// --------------- | ||
// Initial declarations. | ||
|
||
enum algorithmOption {RECURSIVE, DYNAMIC, NONE}; | ||
string stars; | ||
stars.append(60, '*'); | ||
|
||
algorithmOption algoOpt = NONE; | ||
int order = 0; | ||
int bestPathValue = 0; | ||
bestPath myPathObj; | ||
stringstream ss; | ||
|
||
// --------------- | ||
// Validate command line argument. | ||
// <-rc|-dy> <order> | ||
|
||
if (argc == 1) { | ||
cout << "Usage: ./main <-rc|-dy> <order>" << endl; | ||
exit(1); | ||
} | ||
|
||
if (argc != 3) { | ||
cout << "Error, invalid command line options." << endl; | ||
exit(1); | ||
} | ||
|
||
if (string(argv[1]) == "-rc") | ||
algoOpt = RECURSIVE; | ||
|
||
if (string(argv[1]) == "-dy") | ||
algoOpt = DYNAMIC; | ||
|
||
if (algoOpt == NONE) { | ||
cout << "Error, invalid algorithm option." << endl; | ||
exit(1); | ||
} | ||
|
||
if (string(argv[2]) != "") { | ||
ss << argv[2]; | ||
ss >> order; | ||
} | ||
|
||
// --------------- | ||
// Solve (as per selected algorithm). | ||
|
||
if (!myPathObj.createMatrix(order)) { | ||
cout << "Program terminated." << endl; | ||
exit(1); | ||
} | ||
|
||
if (algoOpt == RECURSIVE) | ||
bestPathValue = myPathObj.bestPthREC(); | ||
|
||
if (algoOpt == DYNAMIC) | ||
bestPathValue = myPathObj.bestPthDY(); | ||
|
||
// --------------- | ||
// Display headers, list, and result. | ||
|
||
cout << stars << endl; | ||
cout << "CS 302 - Assignment #1" << endl; | ||
cout << "Best Matrix Path Finder." << endl; | ||
cout << endl; | ||
|
||
cout << "Algorithm: "; | ||
switch (algoOpt) { | ||
case RECURSIVE: | ||
cout << "Recursive" << endl; | ||
break; | ||
case DYNAMIC: | ||
cout << "Dynamic Programming" << endl; | ||
break; | ||
default: | ||
cout << "Error, invalid selection." << endl; | ||
break; | ||
} | ||
cout << endl; | ||
myPathObj.displayMatrix(); | ||
cout << endl; | ||
cout << "Best Possible = " << bestPathValue << endl; | ||
|
||
// --------------- | ||
// Done, end program. | ||
|
||
return 0; | ||
} | ||
|
||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Make file for assignment #1. | ||
|
||
OBJS = main.o bestPath.o | ||
CC = g++ -g -Wall -pedantic -std=c++11 | ||
|
||
all: main | ||
|
||
main: main.o bestPath.o bestPath.h | ||
$(CC) -o main $(OBJS) | ||
|
||
main.o: main.cpp bestPath.h | ||
$(CC) -c main.cpp | ||
|
||
bestPath.o: bestPath.cpp bestPath.h | ||
$(CC) -c bestPath.cpp | ||
|
||
# ----- | ||
# clean by removing object files. | ||
|
||
clean: | ||
rm $(OBJS) | ||
|
Binary file not shown.