Dijkstra's Shortest Path Algorithm - Determine Connection Between Individuals Within Given Array

May 11, 2014

I am using the shortest path algorithm to determine the connection between individuals within a given array. The array is written into the code and not read from external files.

When I am having problem is .. i am having problems how to prompt the user for the starting point or vertex and read that prompt to determine the starting point in the array. I know that this code :

computePaths(v0);

determines the starting point. i want to read "v0" from the user prompt.

total code being used

import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.io.IOException;
import java.util.Scanner;
class Vertex implements Comparable<Vertex>

[Code] .....

View Replies


ADVERTISEMENT

Dijkstra's Algorithm - Array Full Of Null Data

May 6, 2014

I'm trying write a Dijkstra's implementation in java. First off, here is the algorithm:

package lab3;
import java.util.HashMap;
import java.util.Stack;
 
/**
* Compute shortest paths in a graph.
*
* Your constructor should compute the actual shortest paths and maintain all the information needed to reconstruct them. The returnPath() function should use this information to return the appropriate path of edge ID's from the start to the given end.
*
* Note that the start and end ID's should be mapped to vertices using the graph's get() function.
*/
class ShortestPaths {
Multigraph graph;
final int INF = Integer.MAX_VALUE;
PriorityQueue<Integer> Q;

[Code] ....

I followed someone else psuedocode very closely but for whatever reason, my edge[] array is just full of null data, which means I can't actually return the shortest path. Why that's happening/how to fix it? Maybe I'm not understanding dijstra's correctly.

View Replies View Related

Algorithm To Find Shortest Route Between Two Points In 2D Grid Array

Mar 24, 2014

What would be a good and simple algorithm to find the shortest route between two points in a 2D array[grid] ? There can be certain obstacles in the grid i.e. some of the cells may be inaccessible. I tried googling for it and found that A* is the best for this but I am just a beginner and would like to start with something much simpler.

View Replies View Related

Dijkstra Algorithm Implementation Does Not Work Properly

Jan 5, 2015

I have to implement Dijkstra algorithm in Java.

Input file looks like this:

3
5
1 5 1
4 5 3
3 4 5
4 3 7
1 6 9
2 5 12
2 6 3
5 4 4

First and second numbers are in order source and destination points.

First and second numbers in another each line represent edges. Third one is wage of each edge.

My code looks as follows:

import java.util.*;
import java.io.*;
class Vertex implements Comparable<Vertex>
{
public int name = 0;
public ArrayList<Edge> adjacencies = new ArrayList<Edge>();
public double minDistance = Double.POSITIVE_INFINITY;
public Vertex previous;

[Code] .....

View Replies View Related

How To Get Shortest Path Between Two Nodes In Adjacency Matrix Using Undirected Weighted Graph

Apr 26, 2014

how to get shortest path between two nodes in adjacency matrix using with undirected weighted graph using BFS algoritham java program??

View Replies View Related

Dijkstra's Algorithm With Priority Queue - Method To Find Minimum Distance Is Nonfunctional

May 14, 2014

I was given some code by a professor to add some features to as part of an assignment. However, the code itself doesn't seem to work.

import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;
public class DijkstraPriorityQueue

[Code] ....

The method to find minimum distance is nonfunctional...I receive an error that the types are incompatible. I can't do the assignment if the base code doesn't work to begin with...

View Replies View Related

Algorithm That Determine Ancestors And Descendants

Dec 16, 2014

I have a problem with this example. I don't understand what should I do. Data on family relationships are contained in two tables.

Table 1 has a column Child and Mother,
Table 2 - Child and Father.

Formulate algorithm that determines: all the descendants of a person X, all the ancestors of a person X. Write a program implementing the algorithm.

View Replies View Related

Determine If There Is Path From One Vertex To Another - Why Is This Returning False

Dec 7, 2014

This method pretty much determines if there is a path from one vertex to another. When I check to see if there is a path from 5 to 2 (which there is) it return false. However, when I create a driver method, it return true (which it should). Why? It should never reach outside of the else because the condition is met, right???

Here is the graph:

0 1 2 3 4 5
------------
0| 0 1 1 0 0 0
1| 0 0 0 0 0 0
2| 0 0 0 1 0 0
3| 1 0 0 0 0 0
4| 0 0 1 0 0 1
5| 0 1 0 1 0 0

Path: 5 points to 1 and 3 (1 does not have a path to anything). 3 points to only 0. 0 points to 1 and 2. Thus there is a path from 5 to 2.

Here is the code without the driver (the one that is returning false, even tho it should be true)

public boolean existsPath(int x, int y){//x = 5, y = 2
stack.push(x);//mark x as visited
if(x == y){//path found
stack.removeAllElements();
return true;

[Code] ....

View Replies View Related

Create 2D Array Out Of CSV File And Find Number Of Elements To Determine Array Size

Mar 24, 2015

I am taking the Class Algorithms and Datastructures and got an assignment for Lab that really throws me off. The goal is to create an Array out of a given CSV file, implement several Methods that get the size of array, etc.

I am still stuck in the first part where the CSV has to be imported into the Array. My problem is that I need a mechanism that figures out the needed size for the Array, creates the array, and only then transfers the data from the CSV.

The list consists of the following wifi related values:

MAC-Adress, SSID, Timestamp, Signalstrength.

These are on the list, separated by comma. The Columns are each of these, and the rows are the four types of values making up the information on a certain wifi network.

The catch is, we are not allowed to use any of the following:

java.util.ArrayList
java.util.Arrays
and any class out of java.util.Collection.

So far I used the BufferedReader to read in the file and tried to implement the array, but I get an arrayindexoutofboundsexception.

Below is my Code (Its still an active construction zone):

public class WhatsThere {
public WhatsThere(String wifiscan) throws IOException {
}
public static void main(String[] args) throws IOException {
// WhatsThere Liste = new WhatsThere(String wifiscan);
String[][] arrayListe = new String[0][0];

[Code] ....

View Replies View Related

Establish Connection Using Connection Pool And Retrieve Username

Sep 1, 2014

I have created a JSP page, on click of a particular button, the control moves to the servlet "TestServlet".

This is the code under TestServlet:

import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

[Code]...

Now, I have got a comment saying I should get the database connection from connection pool. And one more issue is that I have used the function System.getProperty("user.name") to get the username which i have logged in. But this code will be run on Unix which will not support this function. Any function where I can get the windows username. There is a function getUserPrincipal(), but this function returns a NULL. How to resolve this.

View Replies View Related

Servlets :: Closing DB Connection Retrieved From Connection Pool

Jan 11, 2015

I configured a connection pool in tomcat 7. For every database activity I get a connection from the pool. Do I have to close the connection like other regular connection after the database operation is done? If I close the connection will it have any effect on the connection pool?

View Replies View Related

Input 10 Integer Numbers Into Array And Determine Maximum Value Entered

Apr 22, 2015

a. Write a Java program to input 10 integer numbers into an array named fmax and determine the maximum value entered. Your program should contain only one loop, and the maximum should be determined as array element values are being input. (Hint: Set the maximum equal to the first array element, which should be input before the loop used to input the remaining array values.)

b. Repeat 1a, keeping track of both the maximum element in the array and the index number for the maximum. After displaying the numbers, display these two messages:

The maximum value is: _________
This is element number __________ in the list of numbers

Have your program display the correct values in place of the underlines in the messages.

c. Repeat 1b, but have your program locate the minimum value of the data entered.

I did parts a and b but for part see i just want to know if i did it correctly or not

import java.util.Scanner;
public class MinimumValueArray {
public static void main(String[] args) {
//Variable Declaration
Scanner keyboard = new Scanner(System.in);
int size = 10;

[Code] ,.....

When I run it i get this The minimum value is 0.0

The element that holds the value is 0 right away. is this right for the minimum or am i supposed to enter values and it will display the minimum value like in parts a and b wit the maximum? will the minimum just always be 0 or ?

View Replies View Related

Finding Path Through 2D Array From Top To Bottom?

Apr 20, 2015

I have a 2D array that is of type int[][]. it is populated with 1's and 0's. I need to create a method that allows me to search from the top row finding a possible path to the bottom--the path is a path of connecting cells containing 1. I need to create an array that stores the cells that are needed to be searched. When the search carries out, for each cell stored it needs to check the cells to the left, right, below and above the current cell.

I also need to create a variable to store the current cell. I thought initially it would be an int but it can't be because it needs to hold the the index of the current cell. and any of the cells searched that are an immediate neighbour of the current cell are added to the storage array.

I have these instructions but I am having trouble converting into code

Finding a path through vegetation from the top row to the bottom row: The idea is we will start from a cell in the top row and advance below for as long as we can. Since we are still not done exploring a cell until we have explored ALL it’s vegetation neighbors, we will add the cell to an array of cellsToExplore and only come back to it if the current cell we are examining is fully explored. Here is pseudo-code of the algorithm !

• Create array cellsToExplore (what size should this array be?)!

• Create an integer count that stores the number of cells in cellsToExplore!

• Go through the top row and add ALL vegetation cells to cellsToExplore!

• Create a variable to store currentCell we are exploring and set it to null.!

• while count > 0 do!

- set currentCell to the last cell in cellsToExplore (last cell added to the array).!

- label currentCell as burning vegetation!

- If the currentCell is on the bottom row then we return true (we found the path).!

- if the cell below currentCell is vegetation then add the cell below to the cellsToExplore array.!

- else if cell to the right of currentCell is vegetation then add the cell to the right to cellsToExplore!

- else if cell to the left of currentCell is vegetation then add the cell to the left to cellsToExplore!

- else if cell above the currentCell is vegetation then add the cell above to cellsToExplore.!

- else remove the currentCell from the cellsToExplore (we are done with this cell). !

• Return false!

View Replies View Related

How To Find A Path Through Array Maze

Mar 4, 2015

So what I'm trying to do is write a code in java which finds a path through a maze. I want it to go through the maze and determine if there's a * symbol at that location or not. If there is a * symbol at the specified location then the program would search for another position until it finds one without the * symbol and if it can't then I'll have the program return null. I also want it to implement backtracking which I'm not sure how to do. Here's my code so far

private boolean findPath(int fromRow, int fromCol, int toRow, int toCol){
boolean solved = true;
for(int i=fromRow; i<toRow; i++){
for(int j=fromCol; j<toCol; j++){
if (x[i][j] == ('*')){
//do something
}
}
}
return false;
}

the code isn't finished yet however what I'm not sure is what do I do with the if statement and how do I implement backtracking?

View Replies View Related

Sorting Runners Into Time Order Shortest First

May 5, 2014

i need to sort these runners into time order shortest first, the classes both work i just cant get the method sortRunnerList() to work

Java Code:

public class Runner implements Comparable <Runner>
{
/* static variables */
private static int nextNumber;
// To be added by students in Question 3, part (i)(a) and part(iv)(a)
/* instance variables */

private int number; // runner's number
private String name; // runner's name

[Code] .....

View Replies View Related

MaxSum Algorithm With Negative Values In Array

Apr 2, 2014

How would I go about inputting the negative values in the array in case 1 the array comes from the user, case 2 from the text file? The sum prints perfectly fine with positive values but if I input negative values it just completely ignores them.

case 1:
int sum;
System.out.print("Enter list of comma-delimeted integers: ");
Scanner scan = new Scanner(System.in);
String input2=scan.next();
String[] num = input2.split(",");
int[] a= new int[num.length];

[Code] ....

View Replies View Related

Array Is A Set Of Numbers In Triangle - Find Maximum Path

Aug 12, 2014

If you aren't familiar with the euler problem 67, you are given an array that is a set of numbers in a triangle. Like this

3
7 4
2 4 6
8 5 9 3

and you have to find the maximum path, which for this one is

(3)
(7) 4
2 (4) 6
8 5 (9) 3

I have solved this problem iteratively with the code below

depth = depth-2;
while (depth >=0) {
for (int j = 0; j <= depth; j++) {
values[depth][j] += Math.max(values[depth+1][j], values[depth+1][j+1]);
}
depth -= 1;
}

depth is a variable for the row in the triangle. My problem is that i need the solution to be recursive and i am having trouble doing this. So far i have

public static int findMax(int[][] array,int depth) {
if (depth==0)
return array[0][0];
else if
}

View Replies View Related

Prim's Algorithm Using Adjacency Matrix Array Indexing

Apr 27, 2014

I am working with an Adjacency Matrix to try to find the MST of a graph. Along the way I have hit a snag that I am not sure how to get around. When running the program I will parse through each row of the matrix and find the smallest weight. However when trying to reset the row at the end of the lowest sort I cannot move to the next row.

The graph looks like this:

My Matrix was created from the graph and I have determined by starting at Vertex W my path should looks like this:
W->R->D->H->G->S->C->B->A

View Replies View Related

Use Relative Path In Place Of Absolute Path

Nov 7, 2014

I am copying the xml files from one folder to other folder, in the source folder, i have some files which have some content like "backing File="$IDP_ ROOT/metadata/iPAU-SP-metadata.xml" but while writing to the destination folder.i am replacing the "$IDP_ROOT" with my current working directory. The entire copying of files is for deploying into tomcat server. The copying is done only when server starts for the first time.Problem: If i change the folder name from my root path in my machine after i run the server,the entire process will be stopped because the destination folder files already contains the content which is with existed files names or folder names.

So i want to change it to relative path instead absolute path. What is the best way to do it? Please look at code below:

[ // Getting the current working directory
String currentdir = new File(".").getAbsoluteFile().getParent() + File.separator;

if(currentdir.indexOf("ControlPanel")!=-1){
rootPath=currentdir.substring(0, currentdir.indexOf("ControlPanel"));
}else{
rootPath=currentdir;

[code]....

View Replies View Related

How To Determine Size Of Table

Jul 6, 2014

Can be :

results = null;
results = new double[2];
results = new double[50];
results = new double[100];

How determine size of this table : System.out.println("table size=",???);

View Replies View Related

How To Determine Value After Nested For Loop

Oct 12, 2014

int count = 0;
for (int x = 0; x < 3; x++)
for (int y = x; y < 3; y++)
for (int z = y; z < 3; z++)count++;

What is the value of count after the code segment is executed?

The answer is 9, but I don't really understand how this answer was derived.

View Replies View Related

How To Determine Value After Nested For Loop

Oct 12, 2014

int count = 0;
for (int x = 0; x < 3; x++)
for (int y = x; y < 3; y++)
for (int z = y; z < 3; z++)
count++;

What is the value of count after the code segment is executed?

The answer is 9, but I don't really understand how this answer was derived.

View Replies View Related

Determine If Rectangles Cover Each Other

Oct 22, 2014

[URL] .... This is a link to the credentials that need to be met.

/* Constructors, getters and setters that will determine if the rectangles cover each other.*/

import java.util.Random;
import java.lang.Math.*;
class Rectangle{
   int width = 50;
   int length = 50;
   int x = 90;
   int y = 90;
  
[Code] ....

View Replies View Related

How To Give URL Connection To Code

May 11, 2014

how to give URL Connection to this?

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
public class WebBrowser

[code]....

View Replies View Related

Connection To A Database In Its Own Class

Jan 15, 2015

I was wondering if I could take the following code and somehow put the connection to database in it's own class without the "public static void main(String[] args)" method. That way I could call it anytime i want to open a connection to the database.

Java Code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Sample {
public static void main(String[] args) throws ClassNotFoundException

[Code] ....

View Replies View Related

EJB / EE :: GenericJDBCException - Could Not Open Connection

Oct 3, 2014

I am new to EJB; I am writing EJB (3.1) program in JBoss 7.1 - I am using MySQL DB with JPA. While executing the program, I am facing the Generic JDBCException: Could not open connection. It is happening only for certain tables in the database; but NOT for all tables. My code is able to access few of the tables in the same database. Here is my full stack trace: -

Exception in thread "main" javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection
at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:166)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:230)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:304)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:190)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)

[code]....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved