Can someone help me convert one of these iterative methods to a recursive method
ID: 3755977 • Letter: C
Question
Can someone help me convert one of these iterative methods to a recursive method?
CODE BELOW:
private void inputGraph() {
int row, col;
String line, current;
StringTokenizer st;
try
{
// get number of vertices from first line
line = graphStream.readLine();
st = new StringTokenizer(line);
current = st.nextToken();
numVerts = Integer.parseInt (current);
adjMatrix = new int[numVerts][numVerts];
row = 0;
while ((line = graphStream.readLine()) != null) {
st = new StringTokenizer (line);
col = 0;
while (col < numVerts && st.hasMoreTokens()) {
current = st.nextToken();
if (current.compareTo (INF) == 0)
adjMatrix[row][col++] = INFINITY;
else
adjMatrix[row][col++] = Integer.parseInt (current);
}
row++;
}
}
catch (IOException io)
{
System.out.println ("File has I/O problems. ");
}
catch (NumberFormatException nfe)
{
System .out.println (" File must contain only integers. ");
}
finally
{
try
{
if (graphStream != null)
graphStream.close();
}
catch (IOException io2)
{
System.out.println ("Unable to close file. ");
}
}
}
OR
public void insertVertex(int a, int x, int y) // insert a vertex
{
if(x == y) // if adjMatrix[i][i]
{
if(a != 0) // if value if not zero, display error and exit
{
System.out.println("Cannot initialize graph, adjMatrix[i][i] must be zero! So No Path Found... Exiting...");
System.exit(0);
}// end if
}// end outer if
adjMatrix[x][y] = a; // insert vertex into matrix
}// end method insertVertex()
OR
public void display() throws FileNotFoundException {
for (int row = 0; row < numVerts; row++) {
for (int col = 0; col < numVerts; col++)
// prints symbol instead of 999999999
if (adjMatrix[row][col] > INFINITY - 0.01 * INFINITY)
System.out.print ("& ");
else
System.out.print ("" + adjMatrix[row][col] + " ");
System.out.println();
}
System.out.println(" ");
Explanation / Answer
private void inputGraph() {
int row, col;
String line, current;
StringTokenizer st;
try
{
// get number of vertices from first line
line = graphStream.readLine();
st = new StringTokenizer(line);
current = st.nextToken();
numVerts = Integer.parseInt (current);
adjMatrix = new int[numVerts][numVerts];
row = 0;
while ((line = graphStream.readLine()) != null) {
st = new StringTokenizer (line);
col = 0;
while (col < numVerts && st.hasMoreTokens()) {
current = st.nextToken();
if (current.compareTo (INF) == 0)
adjMatrix[row][col++] = INFINITY;
else
adjMatrix[row][col++] = Integer.parseInt (current);
}
row++;
}
}
catch (IOException io)
{
System.out.println ("File has I/O problems. ");
}
catch (NumberFormatException nfe)
{
System .out.println (" File must contain only integers. ");
}
finally
{
try
{
if (graphStream != null)
graphStream.close();
}
catch (IOException io2)
{
System.out.println ("Unable to close file. ");
}
}
}
public void insertVertex(int a, int x, int y) // insert a vertex
{
if(x == y) // if adjMatrix[i][i]
{
if(a != 0) // if value if not zero, display error and exit
{
System.out.println("Cannot initialize graph, adjMatrix[i][i] must be zero! So No Path Found... Exiting...");
System.exit(0);
}// end if
}// end outer if
adjMatrix[x][y] = a; // insert vertex into matrix
}// end method insertVertex()
public void display() throws FileNotFoundException {
for (int row = 0; row < numVerts; row++) {
for (int col = 0; col < numVerts; col++)
// prints symbol instead of 999999999
if (adjMatrix[row][col] > INFINITY - 0.01 * INFINITY)
System.out.print ("& ");
else
System.out.print ("" + adjMatrix[row][col] + " ");
System.out.println();
}
System.out.println(" ");
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.