473,586 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to find the minimum cost path in a matrix

3 New Member
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.westmins tercollege.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=10 0000;
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(shortes t(i,j+1,Jmax),s hortest(i+1,j+1 ,Jmax),shortest (i-1,j+1,Jmax))+Ar ray[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
2 4853
JosAH
11,448 Recognized Expert MVP
Have a look at the Floyd Warshall algorithm.

kind regards,

Jos
Apr 30 '07 #2
elissa
3 New Member
thanks for your replay
but i stell dont get it

how can i modify my code to find the correct min path
should i use link list or tree ?
i dont know
May 1 '07 #3

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

Similar topics

3
3575
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 mil). I have reorganized my tablespaces so that the two tables are on different tablespaces, different disks. They both have their indexes stored...
8
12565
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
5878
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 parse then it takes about O(N^2) where N is the # of vertices, too much for large graphs. Furthermore, I don't need to know the all the path from a...
20
17020
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. The others are free to go over ... (I already found that part) -> http://users.pandora.be/hebbrecht/jochen/c++/test.cpp
2
2680
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 two nodes s and t. If we increaset the cost of each edge of G by some amount x, would P still be the shortest path between s and t, because all...
6
17302
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) { int width = 0;
1
3037
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: 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...
9
28930
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 determinant of this matrix: |A-xI|=0, and in C++ I do this by checking every float value with one digit after the decimal between -10000.0 and 10000.0, and...
7
3131
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 i tried /program to find shortest path #include<stdio.h> #include<cstdlib> #include<cstring> #include<fstream> #include<iostream.h> #define...
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.