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


ADVERTISEMENT

Reading In A File With BufferedReader / Using Tokenizer For Adding Into Adjacency Matrix

Mar 6, 2015

I am having trouble adding numbers read from a file with BufferedReader, using Tokenizer to split the numbers up by " " - space and adding them to an adjacency matrix.Below is the text file and my code, that I have at the moment.

0 1 0 0 1 1 0 0
1 0 0 0 0 1 1 0
0 0 0 1 0 0 1 0
0 0 1 0 0 0 0 1
1 0 0 0 0 1 0 0
1 1 0 0 1 0 0 0
0 1 1 0 0 0 0 1
0 0 0 1 0 0 1 0

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
public class Foo
{
@SuppressWarnings("null")
public static void main(String[] args) throws Exception
{
String line, token = null, delimiter = " ";

[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

Logic To Find Adjacency Of Numbers 0 And 1 In Array

Oct 3, 2013

I should mention here that number of elements can be from 1....10'000. And it has to be done as O(N)

I attempted at the problem like so :

public static int adjacencies(int[] A) {
int count = 0;
boolean found = false;
for(int i = 0; i < A.length-1; i++) {
if (A[i] == A[i+1]) {

[Code] .....

To me it looks right and it seems to work.

View Replies View Related

Checking If Matrix Is Upper Or Lower Triangular Matrix Based On User Input

Dec 5, 2014

java program that will determine if the matrix is a lower or upper triangular matrix. I only need to use java.util.Scanner;.

View Replies View Related

Method Transpose2D - Array (Matrix)

Apr 29, 2014

Write the Java code of the method Transpose2D that takes one 2-dimentional array (matrix) A and returns one 2-dimentional array B that is transpose of matrix A. (assume that all the rows are of the same length)

public int[][] transpose (int[][] array) {
if (array == null || array.length == 0)//empty or unset array, nothing do to here
return array; 
int width = array.length;
int height = array[0].length;

[Code] ....

View Replies View Related

2D Array Computation - Performing Certain Matrix Calculations

Apr 22, 2015

I've created several methods in another class to perform certain matrix calculations. Do I need to have mutator method in this calculation class to define the column length of the matrix object? I understand why the following code is not working, but I was hoping to get a quick and easy workaround. I'm not sure how to initialize the size of the column for newMatrix within the method.

public int [][] sum(int matrixOne[][], int matrixTwo[][]) {
int sumMatrix [][] = new int[matrixOne.length]["WHAT TO PUT HERE"];
for (int i = 0; i < matrixOne.length; i++) {
for (int j = 0; j < matrixOne[i].length; j++) {
sumMatrix[i][j] = matrixOne[i][j] + matrixTwo[i][j];
System.out.println("matrixSum" + sumMatrix[i][j]);
}
}
return sumMatrix;
}

Then I will use this method to print the new matrix:

public void printMatrix(int newMatrix [][]) {
for (int i = 0; i < newMatrix.length; i++) {
for (int j = 0; j < newMatrix[i].length; j++) {
System.out.print(newMatrix[i][j] + " ");
}
System.out.println("");
}
}

Is all of this overkill and should I just define all the methods within the same class to avoid these issues?

View Replies View Related

IndexOutOfBoundException - Inserting Values In 2D Array For Matrix

Jun 14, 2014

public class matrix_mul

  static int a[][],b[][],k;
  public static void main(String o[]) {
       k=0;
       a=new int[3][3];
       b=new int[3][3];

[Code] ....

Well, this is my code for inserting values in 2D array for matrix & then printing it on Command line but it's show ArrayIndexOutOfBoundExcepton 3 or sometimes 1 (on cmd) ....

View Replies View Related

Indexing Each Subclass Of Certain Class

Jul 3, 2014

I have a problem where I want to give each subclass of a certain class an index number (I don't care what index numbers are given, as long as there is a one-to-one relationship between subclasses and index numbers and the index numbers don't skip). This number will be used to sort the subclasses as an intermediate step to what I want to achieve. I know I could do this:

interface Superclass {
int index();
}
class Subclass implements Superclass {
int index() {
return 0; //or 1, or 2, ...
}
}

But this quickly gets tedious when I'm looking at lots of subclasses. Plus, there's the off chance that I could mess up and assign an index twice to two different subclasses by accident. Is there a better way to do this? I read about Annotations.

View Replies View Related

Can't Transfer Floating Number Matrix From MATLAB Bin File To Java Array

Mar 5, 2014

The problem is, the values are completely different than they are saved in MATLAB, and probably I need to shift the values after transforming them into byte arrays.My Java code which reads values of floating numbers from a MATLAB bin file as follows:

import java.io.*;
import java.io.File;
import javax.imageio.ImageIO;
import java.nio.ByteBuffer; // may be useful?
public class floatingNumberMatrixReader {

[code]....

View Replies View Related

Quad Tree For Indexing Line Segments

Jul 27, 2014

I need programming Quad-Tree for indexing line segments. what is difference between standard Quad-Tree (for indexing points) and PMR Quad-Tree (for indexing line segments) ?

View Replies View Related

JSF :: Export To Excel With Indexing Feature With Icefaces 1.8.2

Feb 24, 2014

As of now am using icefaces 1.8.2 provided tags for downloading data into excel sheet where i can download all the data from a data table. But now the requirement is like need to download the data into excel sheet based on values passed from UI as parameters to the action class method.

Attaching the code snippet along with this post.

CustomerList.xhtml :-
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<ice:outputLabel value="Export:" />

[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

Adjacency List Graph

May 4, 2014

I'm trying to read a text file of a graph and print information about the graph including the order and size of the graph, rather it is a directed or undirected graph, if it is directed the in and out degree, and the and a list of all vertices for which it is adjacent. The problem is the adjacency list is just printing out the list of vertices instead of the adjacency list. Is there something in my code that is problematic? Also I'm unsure of how to go about the in and out degree

"graphs.txt"
6,1
0,2 0,4 1,4 1,5 2,1 2,3 2,5 3,2 3,4 4,1 4,5 5,1 5,3 5,2
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner; 
public class AdjList {
private Node first; // beginning of list

[Code

View Replies View Related

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'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 View Related

How To Get Each Row Of A Matrix From Input

Apr 24, 2014

Im trying to get the user to input the rows one by one but my input stops after the first. the examples i looked at in my book had this way i used as well. i are my rows and j are my columns. What is the correct way to do it?

import java.util.Scanner;
public class Exercise08_01 {
public static void main(String[] args) {

[Code]....

View Replies View Related

Matrix LL Representation

Aug 1, 2014

Ok I am trying to at this point read in a matrix from a txt file in the format with order of the matrix leading the matrix. Like this :

1
5
2
2 3
5 9
3
3 -2 4
-1 5 2
-3 6 4
4
2 4 5 6
0 3 6 9
0 0 9 8
0 0 0 5
4
2 4 5 6
0 0 0 0
0 0 9 8
0 0 0 5

My code then is supposed to read it in and store it as an Array of singly linked lists. I am having trouble with my code, I am only getting outputs where it is only storing the first line of the txt matrixs like so:

5.0

2.0 3.0
2.0 3.0

3.0 -2.0 4.0
3.0 -2.0 4.0
3.0 -2.0 4.0

2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0

2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0

Is there something obvious in my code that is making it only save the first lines and how can i fix it.

//import java tools
import java.io.*;
import java.util.Scanner;
//Test.java
//See readMe.txt file for implementation instructions
//input: "input.txt"

[code]...

View Replies View Related

Why Code Is Not Printing Matrix And Sum

Feb 28, 2014

why my code is not printing the matrix and sum?

The Java-program Matrix below first asks the user for the number of rows and columns in a matrix. After this the program asks for the values of the elements. Finally, program prints the elements of the matrix and their sum on screen. Your task is to create the missing methods. Check the example print to see how to modify the print. When printing the matrix, values on the same row are separated using tabulator.

Program to complete:

import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
int rows, columns;
Scanner reader = new Scanner(System.in);
System.out.print("Type in the number of rows: ");

[code]...

Write the missing methods here / Methods are written in the text box below.

Example output

Type in the number of rows: 3
Type in the number of columns: 4
Type in the element 1 of the row 1: 1
Type in the element 2 of the row 1: 2
Type in the element 3 of the row 1: 3
Type in the element 4 of the row 1: 4
Type in the element 1 of the row 2: 5

[code]...

Matrix:

1 2 3 4
5 6 7 8
9 10 11 12

Sum of the elements of the matrix: 78

my code

import java.util.Scanner;
public class apples {
public static void main(String[] args) {
int rows, columns;
Scanner reader = new Scanner(System.in);
System.out.print("Type in the number of rows: ");

[code]...

View Replies View Related

Create A Matrix Of JTextFields?

Nov 16, 2014

I am making a Sudoku game and creating a matrix of JTextFields. However I am getting the following errors

Exception in thread "main" java.lang.NullPointerException
at SudokuView.board(SudokuView.java:30)
at SudokuView.<init>(SudokuView.java:18)
at SudokuMain.main(SudokuMain.java:5)

I know the problem is with this code

box[i][j] = new JTextField();
panel.add(box[i][j]);

I know this because when I do this:

panel.add(new JTextField());

It works. However it puzzles me why it is not working.

Whole Code:

import java.awt.GridLayout;
import javax.swing.*;
 public class SudokuView {
 JFrame frame;
JPanel panelBoard;
JTextField[][] box;
int row=10; int col=10;
 SudokuView(){
frame = new JFrame("Play Sudoku GOOD LUCK");

[code]....

View Replies View Related

Getting The Index Of A Matrix Of Buttons

Dec 7, 2014

I am attempting to get the x and y coordinate of a matrix of buttons. I have googled my way to failure and read the docs and I think I am traveling of track.

Below is where I create a matrix of buttons via Swing

public class View extends Mybuttons{
 private static final long serialVersionUID = 1L;
JFrame frame;
JPanel matrixPanel, optionsPanel;
Mybuttons[][] matrixBtn;

Later in this class:

JPanel matrixPan(){
matrixBtn = new Mybuttons[25][25];
JPanel panel = new JPanel();
panel.setSize(550,550);
panel.setLayout(new GridLayout(25,25));
//creating a 25x25 matrix of buttons

[Code]...

In the controller class I am trying to get the index of each button in the getUnvisitedChildNode method. I need to do this so I can search the buttons around the button passed to it and check if they are been visited yet. Below getUnvisitedChildNode you will be bfs (breadth first search).

private Mybuttons getUnvisitedChildNode(Mybuttons b){
//example of some of the things I have tried
int x= Mybuttons.getComponentAt(b);
int y= Mybuttons.indexOf(b);
int j=0;
return b;
}

[Code]...

View Replies View Related

Checking Correctness In A Matrix

Nov 21, 2014

I have a programming assignment in which I have to make a program which loads a crossword from a Properties file and then, when the user press a button, the program checks if the letters the user has typed in the interface are the same as in the correct matrix. If all the letters from a word are correct, the program must paint every letter of the word green.

I'm using two matrix, one that is called "mundo" (I 'm from Latin America) , which has the correct letter for each box. The other one is a JtextField matrix which ='ve created using a Gridllayout. I called this one crucigrama (crossword in Spanish)

This is the code I wrote to validate: So you can understand, here is a translation:

numero = number
fila = row
column = columna
bien - a boolean I used to check if the letter I've checked before are the same as the correct ones
.darLetra() - returns a string with the correct letter

[Code] .....

View Replies View Related

Convert String To Matrix?

Mar 8, 2014

I have string abcdef.I need to convert that string into 2 dimensional matrix.For each 2 characters in the string will be a matrix.For instance:

String: abcdef
Matrix will be: [a b],[c d],[e f]

How to do that in java?

View Replies View Related

How To Swap Matrix From Index

Jan 2, 2015

so I have this matrix

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

If the user for example enter the number 2 for index I need to swap the matrix from this index two lines with two line so this is the result what I need

1 2 5 6
3 4 7 8
9 10 13 14
11 12 15 16

View Replies View Related

Create Square Matrix That Has Min And Max Value

May 10, 2015

I have to create a square matrix that has a min and max value as well as a size value which is given a integer value in the main method. The matrix has to be filled with random values. Also I have to add that matrix to another one in an addMatrix method and I have to subtract both in a subMatrix method. These are the requirements:

Methods:
Constructor() - receives the row and col size for myMatrix and a max and min values for range of random fill values for the matrix.
RandFill() - fills matrices with random numbers
addMatrix() - receives a matrix object. adds its myMatrix with the received object's myMatrix. The result is placed in this object's myResultMatrix.
subMatrix() - subtracts both matrices

I typed up this code but I'm not sure about some parts of it and I would creating min and max values in the Random method and in the main printing the separate 2 matrices and adding and subtracting them:

import java.util.*;
public class SquareMatrix2 {
public int size;
public int myMatrix [][];
public int myResultMatrix;
public int [][] ResultStatus;

[Code] ....

View Replies View Related







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