Anyone know programming in java? Need help ASAP
Hi guys, I was wondering if anyone knows how to program in java. I posted it in ramblings figuring there must be someone in this field.
Specifically, I need help using Dijkstra's algorithm.
The task is to determine the shortest path from a given node to each other node. I only need to track the cost, and NOT route of each path. The graph is represented by an adjacent matrix with all nodes connected.
Specifically, I need help using Dijkstra's algorithm.
The task is to determine the shortest path from a given node to each other node. I only need to track the cost, and NOT route of each path. The graph is represented by an adjacent matrix with all nodes connected.
http://renaud.waldura.com/doc/java/dijkstra/

BTW, if that's what you want then you suck at interwebz and need to pick a new field

BTW, if that's what you want then you suck at interwebz and need to pick a new field
well i knew that lol. i can google on my own.
i have written some code but don't know if it seems right. can you take a look.
public class Final_Start {
final int NODES = 6;
final int INFINITY = 65535;
final int X = INFINITY;
int[][] weights = new int[][] {
// A B C D E F
{ X, 5, 15, 3, 9, 2 },
{ 5, X, 6, 14, 11, 1 },
{15, 6, X, 8, 4, 12 },
{ 3, 14, 8, X, 13, 10 },
{ 9, 11, 4, 13, X, 7 },
{ 2, 1, 12, 10, 7, X } };
// Finds the shortest path from passed start node to all others
void shortestPath(int start)
{
for (int k=0; k<6; k++){
for (int i=0; i<6; i++){
for (int j=0; j<6; j++){
if(weights[i][k] != X && weights[k][j] != X && weights[i][k]+weights[k][j] < weights[i][j]){
weights[i][j] = weights[i][k]+weights[k][j];
}
}
}
}
} // end shortestPath
public void showSolution(int start) {
System.out.println("The shortest path from " + (char)('A' + start) + " to each node is:\n");
} // end showSolution
public static void main(String args[]) {
Final_Start fs = new Final_Start();
fs.shortestPath(0);
fs.showSolution(0);
}
i have written some code but don't know if it seems right. can you take a look.
public class Final_Start {
final int NODES = 6;
final int INFINITY = 65535;
final int X = INFINITY;
int[][] weights = new int[][] {
// A B C D E F
{ X, 5, 15, 3, 9, 2 },
{ 5, X, 6, 14, 11, 1 },
{15, 6, X, 8, 4, 12 },
{ 3, 14, 8, X, 13, 10 },
{ 9, 11, 4, 13, X, 7 },
{ 2, 1, 12, 10, 7, X } };
// Finds the shortest path from passed start node to all others
void shortestPath(int start)
{
for (int k=0; k<6; k++){
for (int i=0; i<6; i++){
for (int j=0; j<6; j++){
if(weights[i][k] != X && weights[k][j] != X && weights[i][k]+weights[k][j] < weights[i][j]){
weights[i][j] = weights[i][k]+weights[k][j];
}
}
}
}
} // end shortestPath
public void showSolution(int start) {
System.out.println("The shortest path from " + (char)('A' + start) + " to each node is:\n");
} // end showSolution
public static void main(String args[]) {
Final_Start fs = new Final_Start();
fs.shortestPath(0);
fs.showSolution(0);
}
Trending Topics
eh i learned a bit of java my first year but ended up declaring something else. Nothing really stuck, sorry. I know basic javascript? I know that's not even close to being the same. I can kind of read what you're doing with but i cant help
Going through the array with a for loop while incrementing the array position and also nested arrays or something. Fun stuff.
Android apps are more or less java are they not? They run in a java VM.
Going through the array with a for loop while incrementing the array position and also nested arrays or something. Fun stuff.Android apps are more or less java are they not? They run in a java VM.
Last edited by ez12a; Apr 24, 2012 at 06:39 PM.
objective programming, so much fun, I don't miss it one bit.
Your objective is to recreate the existing Dijkstra class? Did you try compiling it? I'm not very excited about the nested loops, do they traverse properly? plot it out on paper (backwards).
If I'll get bored, I'll trace it (after 9 hours of staring at 3 screens, I'm not bored yet, sorry)
Your objective is to recreate the existing Dijkstra class? Did you try compiling it? I'm not very excited about the nested loops, do they traverse properly? plot it out on paper (backwards).
If I'll get bored, I'll trace it (after 9 hours of staring at 3 screens, I'm not bored yet, sorry)
objective programming, so much fun, I don't miss it one bit.
Your objective is to recreate the existing Dijkstra class? Did you try compiling it? I'm not very excited about the nested loops, do they traverse properly? plot it out on paper (backwards).
If I'll get bored, I'll trace it (after 9 hours of staring at 3 screens, I'm not bored yet, sorry)
Your objective is to recreate the existing Dijkstra class? Did you try compiling it? I'm not very excited about the nested loops, do they traverse properly? plot it out on paper (backwards).
If I'll get bored, I'll trace it (after 9 hours of staring at 3 screens, I'm not bored yet, sorry)

Actually, I don't even have to use the Dijkstra class. The task is to determine the shortest path from a given node to each other node. I only need to track the cost, and NOT route of each path. I already know what they are, but just want the program to figure it out and print them.
It compiles properly, but when I run the program it only prints this:
"The shortest path from A to each node is:"
I want this output:
Node: B - Cost: 3
Node: C - Cost: 9
Node: D - Cost: 3
Node: E - Cost: 9
Node: F - Cost: 2
P.S. If anyone can figure it out I'll send them a gift. lol
Well, I got it to work. I was missing a print statement
Only problem is that it prints out extra stuff. The stuff in red is all I want.
4 3 9 3 9 2
3 2 6 6 8 1
9 6 8 8 4 7
3 6 8 6 12 5
9 8 4 12 8 7
2 1 7 5 7 2
The shortest path from A to each node is:
public class Final_Start {
final int NODES = 6;
final int INFINITY = 65535;
final int X = INFINITY;
int[][] weights = new int[][] {
// A B C D E F
{ X, 5, 15, 3, 9, 2 },
{ 5, X, 6, 14, 11, 1 },
{15, 6, X, 8, 4, 12 },
{ 3, 14, 8, X, 13, 10 },
{ 9, 11, 4, 13, X, 7 },
{ 2, 1, 12, 10, 7, X } };
// Finds the shortest path from passed start node to all others
void shortestPath(int start) {
for (int k=0; k<6; k++){
for (int i=0; i<6; i++){
for (int j=0; j<6; j++){
if(weights[i][k] != X && weights[k][j] != X && weights[i][k]+weights[k][j] < weights[i][j]){
weights[i][j] = weights[i][k]+weights[k][j];
}
}
}
}
for (int r=0; r<6; r++) {
for (int c=0; c<6; c++) {
if(weights[r][c] == X){
System.out.print("n/a");
}else{
System.out.print(" " + weights[r][c]);
}
}
System.out.println(" ");
}
} // end shortestPath
public void showSolution(int start) {
System.out.println("The shortest path from " + (char)('A' + start) + " to each node is:\n");
} // end showSolution
public static void main(String args[]) {
Final_Start fs = new Final_Start();
fs.shortestPath(0);
fs.showSolution(0);
}
}

Only problem is that it prints out extra stuff. The stuff in red is all I want.
4 3 9 3 9 2
3 2 6 6 8 1
9 6 8 8 4 7
3 6 8 6 12 5
9 8 4 12 8 7
2 1 7 5 7 2
The shortest path from A to each node is:
public class Final_Start {
final int NODES = 6;
final int INFINITY = 65535;
final int X = INFINITY;
int[][] weights = new int[][] {
// A B C D E F
{ X, 5, 15, 3, 9, 2 },
{ 5, X, 6, 14, 11, 1 },
{15, 6, X, 8, 4, 12 },
{ 3, 14, 8, X, 13, 10 },
{ 9, 11, 4, 13, X, 7 },
{ 2, 1, 12, 10, 7, X } };
// Finds the shortest path from passed start node to all others
void shortestPath(int start) {
for (int k=0; k<6; k++){
for (int i=0; i<6; i++){
for (int j=0; j<6; j++){
if(weights[i][k] != X && weights[k][j] != X && weights[i][k]+weights[k][j] < weights[i][j]){
weights[i][j] = weights[i][k]+weights[k][j];
}
}
}
}
for (int r=0; r<6; r++) {
for (int c=0; c<6; c++) {
if(weights[r][c] == X){
System.out.print("n/a");
}else{
System.out.print(" " + weights[r][c]);
}
}
System.out.println(" ");
}
} // end shortestPath
public void showSolution(int start) {
System.out.println("The shortest path from " + (char)('A' + start) + " to each node is:\n");
} // end showSolution
public static void main(String args[]) {
Final_Start fs = new Final_Start();
fs.shortestPath(0);
fs.showSolution(0);
}
}
Thread
Thread Starter
Forum
Replies
Last Post
joflewbyu2
5G TLX (2015-2020)
139
Oct 8, 2015 11:16 AM
Mugen TSX
Eastern Canada
0
Sep 16, 2015 09:52 PM
Skirmich
2G TL (1999-2003)
37
Sep 15, 2015 06:41 PM
Billu99tl
3G TL Problems & Fixes
1
Sep 13, 2015 11:30 AM











