I am fairly new to the Java lang and this is my first post so if I do it incorrectly please forgive me. Anyways, I am basically trying to find

  • MaxInDegree
  • MaxOutDegree
  • MaxDegree
  • MinInDegree
  • MinOutDegree
  • MinDegree

of a 2d array with various nodes. Here is what I have so far (all the methods are being called from a different program. I am so stuck. Please help.

import java.io.*; 
import java.util.*; 
public class Graph
{
	private final int NO_EDGE = -1; 
	private int g[][]; 
	
	private int outDegree;
	private int inDegree;
	private int degree;
	
	private int numEdges;
	public Graph( String "graphdata.txt" ) throws Exception
	{
		readGraphFile( graphdata.txt );
	}


	private void readGraphFile( String graphdata.txt ) throws Exception
	{

		Scanner infile = new Scanner( new File( graphdata.txt ) );

		int n;                     
		int row, col, weight;

		n = graphdata.txt.nextInt();
		g = new int[n][n]; // 2D array of zeros
		numEdges=0;

		for(int i=0; i<g.length; i++)
			{	
				g[i][i] = 0;
				if(!g[i][i])
					g=NO_EDGE;
			}

	
		while ( graphdata.txt.hasNext() )
		{
			row = graphdata.txt.nextInt();
			col = graphdata.txt.nextInt();
			weight = graphdata.txt.nextInt();
			addEdge( row, col, weight ); 
		}
		System.out.println("Graph File processed: " + numEdges + " edges stored.");
	}
	private void addEdge( int r, int c, int w )
	{
		if(r>g.length||c>g[r].length)
			return;
		g[r][c] = w;
		++numEdges;
	}
	private boolean hasEdge( int src, int dest )
	{
		val=g[src][dest];
		if (val==0||val==-1)
			return false;
		else 
			return true;
	}
	public int maxInDegree ()
	{
		for (i=0; i<g.length; i++)
		{
			for(j=0; j<g[i].length; j++)
			{
				if (g[i][j]>0)
					inDegree++;
			}
		}
		return inDegree;
	}

	public void print()
	{
		for (int r=0 ; r < g.length ;++r )
		{
			for ( int c=0 ; c<g[r].length ; ++c )
				System.out.printf( "%3d ", g[r][c] );
			System.out.println();
		}
	} 

} // End Graph Class
public Graph( String "graphdata.txt" ) throws Exception
	{
		readGraphFile( graphdata.txt );
	}

This makes no sense to me.
Try:

public Graph() throws Exception
	{
String name = "graphdata.txt";
		readGraphFile( name);
	}

Next method:
- Do the same thing in the first method. You must make a name for the parameter variable, and you can use this later on.

g[i][i] = 0;
				if(!g[i][i])
					g=NO_EDGE;

-

if(!g[i][i])

does not exist, because

g[i][i]

is an int. Use

g[i][i] == 0

or something similar.
-

g=NO_EDGE;

makes no sense. When you

g=NO_EDGE;

you assign an int to a variable with the int [][] type. That cannot happen.

Try something with these tips, and ask me further if you need more help. I am not gong to do all your homework...

Thank you. I changed both of those things. I really just need to understand how to scan each row of the array and if a number greater than 0 is there, count it (the actual value does not matter). Then after I have scanned each row, look at the row with the most non zeroes and determine how many numbers there are. Then do the same with the min (if that makes any sense).

OK, as I understand it:
- loop through the array
- check wether g[j] != 0
- if so: counter++;

Keep four variables: one with the minimum number of non-zero boxes, and another with the maximum number of non-zero boxes. The other two numbers will be the row numbers, of the current minimum and maximum number of non-zero items.
When you encounter a row which has more or less non-zero items then the row in the variable, set the variable as the current row, and update the maximum or minimum non-zero items variable.

Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.