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