Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4033518
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:51:24+00:00 2026-05-20T11:51:24+00:00

I am working on a lab and need some assistance if possible. I have

  • 0

I am working on a lab and need some assistance if possible.

I have created a multidimensional array which is populated with random integers >= 0 up to 100 (inclusive) and I am attempting to apply Prim’s algorithm (via a method that I have in another class) to this multidimensional array, but it keeps giving me unwanted results (either zeroes, or the value that I put in for ‘n’).

Please be aware that I have applied Prim’s algorithm (via the method in another class) to two other arrays and it has worked perfectly well; however, now that I have created a multidimensional array populated entirely by random natural integers between 0 and 100 inclusive, it ceases to work. Here is the code from the classes (I have left the code in for the aforementioned two arrays that I have worked on in case something can be derived from there):

import java.util.Arrays;
import java.util.Random;

public class Lab6 {

static double[][] g = new double[][] {{0, 1, 2} , {1, 0, 3} , {2, 3, 0}};
static double mst[][] = MST.PrimsMST(g);

static double[][] lecExample = new double[][] {{0, 1, 2, 3, 0} , {1, 0, 6, 0, 5} , {2, 6, 0 ,4, 1} , {3, 0, 4, 0, 2} , {0, 5, 1, 2, 0}};
static double mst2[][] = MST.PrimsMST(lecExample);


public static void printArray(){

    System.out.println("Matrix (g):");
    for (int i = 0; i < g.length; i++) {
             for (int c = 0; c < g[i].length; c++) {
                 System.out.print(" " + g[i][c]);
             }
         System.out.println("");
    }

    System.out.println();

    System.out.println("MST:");
    for (int i = 0; i < mst.length; i++){
            for (int c = 0; c < mst[i].length; c++){
                System.out.print(" " + mst[i][c]);
            }
        System.out.println("");
    }

    System.out.println("Matrix (lecExample):");
    for (int i = 0; i < lecExample.length; i++) {
             for (int c = 0; c < lecExample[i].length; c++) {
                 System.out.print(" " + lecExample[i][c]);
             }
         System.out.println("");
    }

    System.out.println();

    System.out.println("MST2:");
    for (int i = 0; i < mst2.length; i++){
            for (int c = 0; c < mst2[i].length; c++){
                System.out.print(" " + mst2[i][c]);
            }
        System.out.println("");
    }

}


static Random random = new Random();
static int r = random.nextInt() & 100;

public static void randomArray(int n){

    double[][] array = new double[][] {{n, n, n, n, n}, {n, n, n, n, n}, {n, n, n, n, n}, {n, n, n, n, n}, {n, n, n, n, n}};
    double mst3[][] = MST.PrimsMST(array);

    System.out.println("Matrix (Random Number Array):");
    for(int i = 0 ; i < array.length ; i++ ) { 
       for (int c = 0 ; c < array[i].length; c++ ) { 
          array[i][c] = random.nextInt(101);
       }
    }

    for(double[] a: array) { 
        System.out.println(Arrays.toString(a));
    }

    System.out.println("MST3:");
    for (int i = 0; i < mst3.length; i++){
            for (int c = 0; c < mst3[i].length; c++){
                System.out.print(" " + mst3[i][c]);
            }
        System.out.println("");
    }

}

public static void main(String[] args){

    printArray();
    System.out.println("\n");
    randomArray(50);

}

}

MST.java:

import java.util.*;

public class MST
{
//Search for the next applicable edge
static private Edge LocateEdge(ArrayList<Integer> v,ArrayList<Edge> edges)
{
    for (Iterator<Edge> it = edges.iterator(); it.hasNext();)
    {
        Edge e = it.next();
        int x = e.i;
        int y = e.j;
        int xv = v.indexOf(x);
        int yv = v.indexOf(y);
        if (xv > -1 && yv == -1)
        {
            return(e);
        }
        if (xv == -1 && yv > -1)
        {
            return(e);
        }
    }
    //Error condition
    return(new Edge(-1,-1,0.0));
}
@SuppressWarnings("unchecked")
//d is a distance matrix, high value edges are more costly
//Assume that d is symmetric and square
public static double[][] PrimsMST(double[][] d)
{
    int i,j,n = d.length;
    double res[][] = new double[n][n];
    //Store edges as an ArrayList
    ArrayList<Edge> edges = new ArrayList<Edge>();
    for(i=0;i<n-1;++i)
    {
        for(j=i+1;j<n;++j)
        {
            //Only non zero edges
            if (d[i][j] != 0.0) edges.add(new Edge(i,j,d[i][j]));
        }
    }
    //Sort the edges by weight
    Collections.sort(edges,new CompareEdge());
    //Don't do anything more if all the edges are zero
    if (edges.size() == 0) return(res);
    //List of variables that have been allocated
    ArrayList<Integer> v = new ArrayList<Integer>();
    //Pick cheapest edge
    v.add(edges.get(0).i);
    //Loop while there are still nodes to connect
    while(v.size() != n)
    {
        Edge e = LocateEdge(v,edges);
        if (v.indexOf(e.i) == -1) v.add(e.i);
        if (v.indexOf(e.j) == -1) v.add(e.j);
        res[e.i][e.j] = e.w;
        res[e.j][e.i] = e.w;
    }
    return(res);
}

}

Whenever I run the program, this is the outcome:

Matrix (Random Number Array):
[85.0, 11.0, 79.0, 25.0, 30.0]
[62.0, 55.0, 39.0, 21.0, 92.0]
[31.0, 76.0, 3.0, 74.0, 43.0]
[59.0, 97.0, 91.0, 60.0, 7.0]
[96.0, 44.0, 26.0, 66.0, 31.0]

MST3:
0.0 50.0 50.0 50.0 50.0
50.0 0.0 0.0 0.0 0.0
50.0 0.0 0.0 0.0 0.0
50.0 0.0 0.0 0.0 0.0
50.0 0.0 0.0 0.0 0.0

There are two other classes which handle storing the edge weights (Edge.java) and also comparing the edge weights (CompareEdges.java), but they are not relevant to this particular question.

I hope that somebody is able to help as I have spent a number of hours attempting to solve this.

Many thanks.

Mick

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:51:24+00:00Added an answer on May 20, 2026 at 11:51 am

    Here is the problem:

    public static void randomArray(int n){
    
        n = 0;
    
        double[][] array = new double[][] {{n, n, n, n, n}, {n, n, n, n, n}, {n, n, n, n, n}, {n, n, n, n, n}, {n, n, n, n, n}};
        double mst3[][] = MST.PrimsMST(array);
    

    You create an array of 0’s, and you apply MST to it. And then you override your array with random numbers, but the MST method was called on the array of 0’s, not on the array of random numbers.

    Also, on a design level, I think you should take some time to restructure and factorize your code a little, otherwise you will have a lot of problems when building more complicated projects:

    • You should call the MST methods from your main method(), or from a method, not from the top-level of the class.
    • You should also initialize your random generator in a method
    • You don’t have to initialize an array with 0’s, you can just specify the size. (the = {} initialization should only be used when you want to initialize your array with specific values)
    • You wrote 5 times an array displaying code, which is exactly the same, this is the sign that you should have a method doing this.
    • Also, you use arrays of double to store int, so I think you probably want to switch to int

    So I’m thinking your class should look more like this.

    public class Lab6{
        static int[][] g= new int[][] {{0, 1, 2} , {1, 0, 3} , {2, 3, 0}};
        static int[][] lecExample = new int[][] {{0, 1, 2, 3, 0} , {1, 0, 6, 0, 5} , {2, 6, 0 ,4, 1} , {3, 0, 4, 0, 2} , {0, 5, 1, 2, 0}};
    
    
        public static void main(String[] args){
            displayArray(g);
            displayArray(MST.PrimMST(g));
            displayArray(lecExample);
            displayArray(MST.PrimMST(lecExample));
    
            int[][] randomArray = getRandomArray(50);
            displayArray(randomArray);
            displayArray(MST.PrimMST(randomArray));
        }
    
        public static int[][] getRandomArray(int n){
            int[][] a = new int[n][n];
            Random r = new Random();
    
            for(int i = 0; i < a.length; i++){
                for(int j = 0; j < a[i].length; j++){
                    a[i][j] = r.nextInt();
                }
            }
    
            return a;
        }
    
        public static void displayArray(int[] a){
            for(int i = 0; i < a.length; i++){
                for(int j = 0; j < a[i].length; j++){
                    System.out.print(" " + a[i][j]);
                }
                System.out.println("");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my robotics lab is looking for programmers to work on some projects we have
I'm writing some SQL for our lab, and I have it all done except
[Homework disclaimer] I'm working on the binary bomb lab . Basically, I have to
I'm currently working on a lab in my cpe class and we have to
Folks, I am having some trouble working with the After hook. I have organized
We are working on a lab assignment for my CS&E class and I think
In an information security lab I'm working on, I've been tasked with executing multiple
I'm currently working on an implementation of the D*Lite algorithm from Sven Koenig. http://idm-lab.org/bib/abstracts/papers/aaai02b.pdf
Working on a simple poker script in PHP and need a way to determine
Working with an api and I need to one of the first responses alongside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.