473,379 Members | 1,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

how to find the minimum cost path in a matrix

3
hi guys i really wish if some one could help me with this problem , well i solved it but the output is always wrong so can some one help me:

the Question is in this link:
http://people.westminstercollege.edu/faculty/ggagne/fall2006/306/handouts/hw2/index.html

can some one please help me with the dynamic programing part of the question
this is what i did so far:
---------------------------------
#include <iostream>
using namespace::std;

const int q1=4;
const int q2=4;
double Array[q1][q2]={16,5,4,6,2,5,1,9,8,7,6,5,4,3,2,1};//global variable
double Array2[q1][q2]={16,5,4,6,2,5,1,9,8,7,6,5,4,3,2,1};
//-------Class Object----------
class Object
{
public:
double cost;
int i,j;
};
//-------------------------------

//-----------fuction smallest , return the value of smallest element-------
double smalest(Object A,Object B,Object C)
{

double min;

min=A.cost;
Array2[A.i][A.j]=-1;


if(min>B.cost)
{ Array2[A.i][A.j]=Array[A.i][A.j];
min=B.cost;
Array2[B.i][B.j]=-1;

}

if(min>C.cost)
{ cout<<"in C loop "<<min<<"\n";
Array2[B.i][B.j]=Array[B.i][B.j];
Array2[A.i][A.j]=Array[A.i][A.j];
min=C.cost;
Array2[C.i][C.j]=-1;

}

return min;
}
//---------------------------------------------------------------------------

//------------------function shortest find the shortest path ---------------

Object shortest(int i,int j,int Jmax)
{

if(j>Jmax || i<0 ||i>=q1 )
{Object noValue;
noValue.cost=100000;
return noValue;
}

if(j==Jmax)
{Object temp;
temp.cost=Array[i][j];
temp.i=i;
temp.j=j;
return temp;
}

else
{
Object value;
value.cost= smalest(shortest(i,j+1,Jmax),shortest(i+1,j+1,Jmax ),shortest(i-1,j+1,Jmax))+Array[i][j];
value.i=i;
value.j=j;
return value;}

}

//--------------------------------------------------------------------------------

int main()
{
Object cost;
cost =shortest(1,0,3);

cout<<cost.cost<<"\n";

for (int i=0;i<q1;i++)
{
for (int j=0;j<q2;j++)
cout<<Array2[i][j]<<" ";
cout<<"\n";
}

cout<<"\n";
for ( i=0;i<q1;i++)
{
for (int j=0;j<q2;j++)
cout<<Array[i][j]<<" ";
cout<<"\n";
}

return 0;}
-----------------------------

i hope some one could help me or just post the algorithem
thank all , i will appreciates any attempt
Apr 30 '07 #1
1 3031
JosAH
11,448 Expert 8TB
I just answered your question in the C/C++ group. As it seems that you want
C/C++ code (this is a Java group), do you want me to remove this thread here?

kind regards,

Jos
Apr 30 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Pierre Fortin | last post by:
This quest for understanding started very innocently... A simple error on my part, passing on args as "args" instead of "*args" to os.path.join() led me to wonder why an error wasn't raised... ...
3
by: Dan | last post by:
I am a relatively new user on Oracle 9.2.0.1 and I am having trouble performance tuning this production database. I am running a large query that joins two tables, document(3 mil) and entity(9...
8
by: Agoston Bejo | last post by:
Take a look at the following example: table T(i INTEGER, j INTEGER) I want to get the value of i where j is minimal and some conditions apply. (1) SELECT i FROM T WHERE AND j
6
by: ThanhVu Nguyen | last post by:
Hi all, I need recommendation for a very fast shortest path algorithm. The edges are all directed, positive weights. Dijkstra shortest path will solve it just fine but the if the graph is not...
20
by: Webdad | last post by:
Hi! I running my first year as industrial engineer (informatics) We have an assignment to do : .... create a playfield (matrix). Some places in that field are blocked, so you can't pass them....
2
by: tonokio | last post by:
I wasn't sure if this was the best place to post since there isn't really an algorithm section for programming. I was curious that if you're given a MST tree of G and P is the shortest path between...
6
by: finerrecliner | last post by:
hey everyone i'm trying to make a function that will return the length and width of a dynamically allocated 2D array. here's what i came up with to find width: int findWidth(int** matrix)...
2
by: elissa | last post by:
hi guys i really wish if some one could help me with this problem , well i solved it but the output is always wrong so can some one help me: the Question is in this link:...
9
by: curiously enough | last post by:
I need a practical method to find the eigenvalues of a matrix in C++ because the one I know(the only one I know) is to subtract the elements of the diagonal by the eigenvalue and then find the...
7
by: latalui | last post by:
i am a beginner, guys plizzzzzzzz help me. i am given a assignment to find short paths either using dijkstra algorithm or using binary search algorithm in linux OS. check out what is wrong the code...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.