#include
#define VERTEXNUM 4
#define HUGE 32000
const char *vertices [] ={
"selandar",
"chinchin",
"jasin",
"merlimau"
};
const int edges [VERTEXNUM][VERTEXNUM] ={
{0,22,16,32},
{22,0,15,26},
{16,15,0,21},
{32,26,21,0}
};
int visited [VERTEXNUM] ={0};
int curMinCost = HUGE;
int curMinHamNum = 0;
int currentRoute [VERTEXNUM];
void findRoute (int start, int current, int costSoFar, int numVisited);
void foundHamiltonian (int start, int cost);
int main (void) {
int start = 0;
int j;
for (j=0; j
printf("lowest cost hamiltonian cycle: %d w/ cost %d\n", curMinHamNum,
curMinCost);
getchar();
return (0);
}
void findRoute (int start, int current, int costSoFar, int numVisited) {
int j;
currentRoute[numVisited]=current;
if (current == start) {
if (numVisited == VERTEXNUM -1)
foundHamiltonian (start, costSoFar+edges[current][start]);
} else {
visited[current] = 1;
for (j=0; j
findRoute(start,j,costSoFar+edges[current] [j], numVisited+1);
}
visited[current] = 0;
}
}
void foundHamiltonian (int start, int cost) {
int j;
static int hamNum=0;
hamNum++;
printf("#%d: %s", hamNum, vertices[start]);
for (j=0; j
printf ("costs %d\n", cost);
if (cost < curMinCost){
curMinCost =cost;
curMinHamNum = hamNum;
}
}
AlanPosted Oct 9, 2007, 2:52 PM
ana alyPosted Oct 8, 2007, 9:46 PM
Yes your result same as mine. Supposedly the start location is the KL Tower since I set the start value to 1.8, not the Chow Kit. The problem lies here and I am not sure what goes wrong.
AlanPosted Oct 8, 2007, 3:03 PM
I've been through the code and the only lines where I differed from yours were these:
Public
visited(VERTEXNUM - 1) As IntegerPublic
currentRoute(VERTEXNUM - 1) As IntegerYou had an upper bound of VERTEXNUM for both of these arrays, but it doesn't really matter as it won't make any difference to the results.
I see that you've declared 'start' as a double but then assigned it to integer parameters which will force it to be rounded off. However, if you don't do this, then foundHamiltonian() is never called at all.
These are the results I got when I ran it:
#:1 Chow Kit->Bukit Bintang->China Town->Istana Negara->KL Tower->Chow Kitcosts 10.6
#:2 Chow Kit->Bukit Bintang->China Town->KL Tower->Istana Negara->Chow Kitcosts 12.9
#:3 Chow Kit->Bukit Bintang->Istana Negara->China Town->KL Tower->Chow Kitcosts 10.1
#:4 Chow Kit->Bukit Bintang->Istana Negara->KL Tower->China Town->Chow Kitcosts 13.9
#:5 Chow Kit->Bukit Bintang->KL Tower->China Town->Istana Negara->Chow Kitcosts 10.6
#:6 Chow Kit->Bukit Bintang->KL Tower->Istana Negara->China Town->Chow Kitcosts 12.1
#:7 Chow Kit->China Town->Bukit Bintang->Istana Negara->KL Tower->Chow Kitcosts 12.4
#:8 Chow Kit->China Town->Bukit Bintang->KL Tower->Istana Negara->Chow Kitcosts 12.9
#:9 Chow Kit->China Town->Istana Negara->Bukit Bintang->KL Tower->Chow Kitcosts 10.1
#:10 Chow Kit->China Town->Istana Negara->KL Tower->Bukit Bintang->Chow Kitcosts 12.1
#:11 Chow Kit->China Town->KL Tower->Bukit Bintang->Istana Negara->Chow Kitcosts 12.4
#:12 Chow Kit->China Town->KL Tower->Istana Negara->Bukit Bintang->Chow Kitcosts 13.9
#:13 Chow Kit->Istana Negara->Bukit Bintang->China Town->KL Tower->Chow Kitcosts 10.9
#:14 Chow Kit->Istana Negara->Bukit Bintang->KL Tower->China Town->Chow Kitcosts 12.4
#:15 Chow Kit->Istana Negara->China Town->Bukit Bintang->KL Tower->Chow Kitcosts 9.1
#:16 Chow Kit->Istana Negara->China Town->KL Tower->Bukit Bintang->Chow Kitcosts 10.6
#:17 Chow Kit->Istana Negara->KL Tower->Bukit Bintang->China Town->Chow Kitcosts 12.9
#:18 Chow Kit->Istana Negara->KL Tower->China Town->Bukit Bintang->Chow Kitcosts 12.9
#:19 Chow Kit->KL Tower->Bukit Bintang->China Town->Istana Negara->Chow Kitcosts 9.1
#:20 Chow Kit->KL Tower->Bukit Bintang->Istana Negara->China Town->Chow Kitcosts 10.1
#:21 Chow Kit->KL Tower->China Town->Bukit Bintang->Istana Negara->Chow Kitcosts 10.9
#:22 Chow Kit->KL Tower->China Town->Istana Negara->Bukit Bintang->Chow Kitcosts 10.1
#:23 Chow Kit->KL Tower->Istana Negara->Bukit Bintang->China Town->Chow Kitcosts 12.4
#:24 Chow Kit->KL Tower->Istana Negara->China Town->Bukit Bintang->Chow Kitcosts 10.6
lowest cost hamiltonian cycle: 15 w/ cost 9.1
It sounds like this may differ from what you got?
ana alyPosted Oct 8, 2007, 9:38 AM
Thank you for your reply. Actually I had changed the places and distances. The distance now involve decimal number, so I use double as the data type. Therefore few changes had to be done in order to get the result in decimal also. No problem arise when number of places is 4, but once I add one more place, and set the start value to the new location, the output goes wrong. I plan to set until 15 locations. Here I attached the code.
Public Class WebForm1
Inherits System.Web.UI.Page
Public Const VERTEXNUM As Integer = 5
Public Const HUGE As Integer = 32000
Public Shared vertices() As String = {"Bukit Bintang", "China Town", "Chow Kit", "Istana Negara", "KL Tower"}
'distance in kilometre(s)
Public Shared edges(,) As Double = {{0, 0.9, 2.1, 3.1, 1.8}, {0.9, 0, 2.2, 1.4, 1.9}, {2.1, 2.2, 0, 3.4, 1.6}, {3.1, 1.4, 3.4, 0, 4.6}, {1.8, 1.9, 1.6, 4.6, 0}}
Public visited(VERTEXNUM) As Integer
Public curMinCost As Double = HUGE
Public curMinHamNum As Integer = 0
Public currentRoute(VERTEXNUM) As Integer
Public Function main() As Double
Dim start As Double = 1.8
Dim j As Integer
For j = 0 To VERTEXNUM - 1
If (j <> start) Then
findRoute(start, j, edges(start, j), 0)
End If
Next j
TextBox1.Text &= "lowest cost hamiltonian cycle: " & curMinHamNum & " w/ cost " & curMinCost & Environment.NewLine
Return 0
End Function
Public Sub findRoute(ByVal start As Integer, ByVal current As Integer, ByVal costSoFar As Double, ByVal numVisited As Integer)
Dim j As Integer
currentRoute(numVisited) = current
If (current = start) Then
If (numVisited = VERTEXNUM - 1) Then
foundHamiltonian(start, costSoFar + edges(current, start))
End If
Else
visited(current) = 1
For j = 0 To VERTEXNUM - 1
If (visited(j) = 0) Then
findRoute(start, j, costSoFar + edges(current, j), numVisited + 1)
End If
Next j
visited(current) = 0
End If
End Sub
Public Sub foundHamiltonian(ByVal start As Integer, ByVal cost As Double)
Dim j As Integer
Static hamNum As Integer = 0
hamNum = hamNum + 1
TextBox1.Text &= "#:" & hamNum & " " & vertices(start)
For j = 0 To VERTEXNUM - 1
TextBox1.Text &= "->" & vertices(currentRoute(j))
Next j
TextBox1.Text &= "costs " & cost & Environment.NewLine
If (cost < curMinCost) Then
curMinCost = cost
curMinHamNum = hamNum
End If
End Sub
End Class
As u can see, the fist value in each curly bracket, represent each location involve. When I set the start value to 1.8 the result display that the start location is China Town . I got confuse here, and try to make changes here and there, but it wont work. Maybe the data type that I used effect the way value passes along the functions.
AlanPosted Oct 8, 2007, 6:58 AM
As far as I can see, you don't have anything hard-coded in there which would prevent the code from being generalized to cope with more than 4 vertices, provided you change VERTEXNUM and the vertices and edges arrays appropriately.
Also, whilst I don't understand why you would want to change 'start' to a Double rather than an Integer, I don't see that this would actually make any difference to the output because of VB.Net's numeric type coercion rules.
Can you let me know what values you're using and what the output should be (as per the C program) and I'll take a look at it.
ana alyPosted Oct 7, 2007, 2:12 AM
i assume this is because the data type i used...could you please help me on how to increase the number of location i can use
ana alyPosted Oct 6, 2007, 11:05 PM
ana alyPosted Sep 23, 2007, 10:59 PM
Oh you are right Alan. I missed look the concatenate symbol at the function main. Thank you so much. It works.By the way there are so much more to learn :D
AlanPosted Sep 23, 2007, 7:28 AM
That's strange, it should work OK :-/
Did you remember to concatenate each line to what's already in the TextBox's Text property (using the &= operator) as I did ? If you don't do that, then it will overwrite it each time a line is written.
The alternative would be to use the ListBox control.
ana alyPosted Sep 23, 2007, 3:47 AM
I had set the textmode to multiline but still it could not display all 7 lines of output. Is there any other way that could display the output?
AlanPosted Sep 22, 2007, 1:40 PM
Sorry, I'm talking nonsense about the ASP.Net TextBox not being able to display multiple lines. You just need to set the TextMode property to TextBoxMode.MultiLine.
I actually tested the revised code in a WinForms application where I needed to set the TextBox's Multiline property, and change its dimensions, to display all 7 lines. I then put the code back into a web form class as you had it but couldn't then find the Multiline property, forgetting that things are done a little differently in ASP.Net :)
ana alyPosted Sep 22, 2007, 12:51 PM
So do you mean that even the textline is set to multiline, but still it could not display all 7 lines? There might be a way at least could display the final result including the roads involved with the final result. Anyway thank you because finally it works
AlanPosted Sep 21, 2007, 9:58 AM
I've been through it all again and found a number of suble errors in the VB translation which were preventing it from working properly.
The only problem with having a Web app rather than a Windows Forms app is that the former doesn't have a multiline TextBox and so can only show the final rather than all seven lines of output.
Anyway, here's the working version. I've stuck as closely as possible to the original C code and so I've set VERTEXNUM = 4 and got rid of the temporary variables:
Public Class WebForm1
Inherits System.Web.UI.Page
Private
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clickmain()
End Sub Public Function main() As Integer Dim start As Integer = 0 Dim j As Integer For j = 0 To VERTEXNUM - 1 If (j <> start) ThenfindRoute(start, j, edges(start, j), 0)
End If Next jTextBox1.Text =
"lowest cost hamiltonian cycle: " & curMinHamNum & " w/ cost " & curMinCost & Environment.NewLine 'changing line as follows will allow all 7 lines to be printed in WinForms application 'TextBox1.Text &= "lowest cost hamiltonian cycle: " & curMinHamNum & " w/ cost " & curMinCost & Environment.NewLine Return 0 End Function Public Sub findRoute(ByVal start As Integer, ByVal current As Integer, ByVal costSoFar As Integer, ByVal numVisited As Integer) Dim j As IntegercurrentRoute(numVisited) = current
If (current = start) Then If (numVisited = VERTEXNUM - 1) ThenfoundHamiltonian(start, costSoFar + edges(current, start))
End If Elsevisited(current) = 1
For j = 0 To VERTEXNUM - 1 If (visited(j) = 0) ThenfindRoute(start, j, costSoFar + edges(current, j), numVisited + 1)
End If Next jvisited(current) = 0
End If End SubPublic Sub foundHamiltonian(ByVal start As Integer, ByVal cost As Integer)
Dim j As Integer
Static hamNum As Integer = 0hamNum = hamNum + 1
TextBox1.Text &=
"#:" & hamNum & " " & vertices(start) For j = 0 To VERTEXNUM - 1TextBox1.Text &=
"->" & vertices(currentRoute(j)) Next jTextBox1.Text &=
"costs " & cost & Environment.NewLine If (cost < curMinCost) ThencurMinCost = cost
curMinHamNum = hamNum
End If End SubEnd
Classana alyPosted Sep 21, 2007, 12:32 AM
Dear alan, I had changed the code as your suggestion but still the output is not like what the C codes display
Ø Is this because I make the project as web application but not windows application. Does this make any sense that cause the code doesn’t work?
Ø the output that I try on the C code shows this
#1: selandar->chinchin->jasin->merlimau->selandar costs 90
#2:…
…
Lowest cost Hamiltonian cycle: 2 w/ cost 85
AlanPosted Sep 20, 2007, 5:33 PM
I've compared the two and the main things I noticed are:
1. The vertices array should be of String rather than Char type.
2. Having changed the VERTEXNUM constant from 4 to 3, you've then overlooked this in some For statements and iterated up to 2 rather than 3.
3. You've tried to simplify some code within loops by assigning certain expressions to variables but have overlooked in some cases that the expressions depended on the loop variable itself.
Here's what I think the corrected VB code should be:
Public Class WebForm1
Inherits System.Web.UI.Page
Public Const VERTEXNUM As Integer = 3
Public Const HUGE As Integer = 32000
Public Shared vertices() As String = {"Selandar", "ChinChin", "Jasin", "Merlimau"} ' changed Char to String
Public Shared edges(,) As Integer = {{0, 22, 16, 32}, {22, 0, 15, 26}, {16, 15, 0, 21}, {32, 26, 21, 0}}
Public visited(VERTEXNUM) As Integer
Public curMinCost As Integer = HUGE
Public curMinHamNum As Integer = 0
Public currentRoute(VERTEXNUM) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
main()
End Sub
Public Function main() As Integer
Dim start As Integer = 0
'Dim j As Integer ' not needed
'Dim x As Integer = edges(start, j) 'not needed
For j = 0 To 3 ' changed 2 to 3
If (j <> start) Then
findRoute(start, j, edges(start,j), 0) 'replaced x
TextBox1.Text = "lowest cost hamiltonian cycle: " & curMinHamNum & " w/ cost " & curMinCost 'several minor changes here
Return 0
End If
Next j
End Function
Public Sub findRoute(ByVal start As Integer, ByVal current As Integer, ByVal costSoFar As Integer, ByVal numVisited As Integer)
Dim j As Integer
Dim w As Integer = numVisited + 1
Dim y As Integer = costSoFar + edges(current, start)
'Dim z As Integer = costSoFar + edges(current, j) 'not needed
currentRoute(numVisited) = current
If (current = start) Then
If (numVisited = VERTEXNUM) Then ' removed -1
foundHamiltonian(start, y)
Else
visited(current) = 1
For j = 0 To 3 ' changed 2 to 3
If (Not visited(j)) Then
findRoute(start, j, costSoFar + edges(current, j), w) 'replaced z
End If
Next j
visited(current) = 0
End If
End If
End Sub
Public Sub foundHamiltonian(ByVal start As Integer, ByVal cost As Integer)
Dim j As Integer
Static hamNum As Integer = 0
hamNum = hamNum + 1
TextBox1.Text = "#:" & hamNum & " " & vertices(start) ' changed "" to " "
For j = 0 To 3 ' changed 2 to 3
TextBox1.Text = "->" & vertices(currentRoute(j))
Next j
'TextBox1.Text = "costs " & cost 'inserted space after costs
If (cost < curMinCost) Then
curMinCost = cost
curMinHamNum = hamNum
End If
End Sub
End Class
ana alyPosted Sep 20, 2007, 11:17 AM
Public Class WebForm1
Inherits System.Web.UI.Page
Public Const VERTEXNUM As Integer = 3
Public Const HUGE As Integer = 32000
Public Shared vertices() As Char = {"Selandar", "ChinChin", "Jasin", "Merlimau"}
Public Shared edges(,) As Integer = {{0, 22, 16, 32}, {22, 0, 15, 26}, {16, 15, 0, 21}, {32, 26, 21, 0}}
Public visited(VERTEXNUM) As Integer
Public curMinCost As Integer = HUGE
Public curMinHamNum As Integer = 0
Public currentRoute(VERTEXNUM) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
main()
End Sub
Public Function main() As Integer
Dim start As Integer = 0
Dim j As Integer
Dim x As Integer = edges(start, j)
For j = 0 To 2
If (j <> start) Then
findRoute(start, j, x, 0)
TextBox1.Text = "lowest cost hamiltonian cycle:" & "" & curMinHamNum & "cost" & "" & curMinCost
Return 0
End If
Next j
End Function
Public Sub findRoute(ByVal start As Integer, ByVal current As Integer, ByVal costSoFar As Integer, ByVal numVisited As Integer)
Dim j As Integer
Dim w As Integer = numVisited + 1
Dim y As Integer = costSoFar + edges(current, start)
Dim z As Integer = costSoFar + edges(current, j)
currentRoute(numVisited) = current
If (current = start) Then
If (numVisited = VERTEXNUM - 1) Then
foundHamiltonian(start, y)
Else
visited(current) = 1
For j = 0 To 2
If (Not visited(j)) Then
findRoute(start, j, z, w)
End If
Next j
visited(current) = 0
End If
End If
End Sub
Public Sub foundHamiltonian(ByVal start As Integer, ByVal cost As Integer)
Dim j As Integer
Static hamNum As Integer = 0
hamNum = hamNum + 1
TextBox1.Text = "#:" & hamNum & "" & vertices(start)
For j = 0 To 2
TextBox1.Text = "->" & vertices(currentRoute(j))
Next j
'TextBox1.Text = "costs" & cost
If (cost < curMinCost) Then
curMinCost = cost
curMinHamNum = hamNum
End If
End Sub
End Class
Jan MontanoPosted Sep 20, 2007, 6:58 AM
Could you also please post your vb.net transalation? I guess from there, we could figure out what's causing the unexpected behavior you're getting.
We could check line by line if it really is the right translation.
Cheers,
Jan