Question: For Java, how do I add an output showing the tree edges in an adjacency matrix like this 0 1 0 0 0 0 0
For Java, how do I add an output showing the tree edges in an adjacency matrix like this
0 1 0 0 0 0 0 0
0 0 0 0 0 1 1 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0
The input adjacency Matrix is
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.util.*; import java.io.*; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Scanner; import java.util.Set; import java.util.Stack;
public class DFS { private Stack stack=new Stack(); private HashMap
{
int number_of_nodes = adjacency_matrix[source].length - 1;
int visited[] = new int[number_of_nodes + 1];
int element = source;
int i = source;
System.out.print(element + "\t");
visited[source] = 1;
stack.push(source);
while (!stack.isEmpty())
{
element = (Integer) stack.peek();
i = element;
while (i <= number_of_nodes)
{
if (adjacency_matrix[element][i] == 1 && visited[i] == 0)
{
stack.push(i);
visited[i] = 1;
element = i;
i = 1;
System.out.print(element + "\t");
continue;
}
i++;
}
stack.pop();
}
} public void connected(int adj[][], int source) { int number_of_nodes = adj[source].length - 1; int visited[] = new int[number_of_nodes + 1]; visited[0]=1; int element = source; int i = source; int cc=1; for (int k = 1; k <= number_of_nodes; k++){ if (visited[k] == 0) { visited[source] = 1; stack.push(source); while (!stack.isEmpty()) { element = (Integer) stack.peek(); i = element; while (i <= number_of_nodes) { if (adj[element][i] == 1 && visited[i] == 0) { stack.push(i); visited[i] = 1; element = i; i = 1; continue; } i++; } stack.pop(); } boolean connected = false;
for (int vertex = 1; vertex <= number_of_nodes; vertex++) { if (visited[vertex] == 1) { connected = true; } else { connected = false; } }
} } { System.out.print("There are "); System.out.print(cc); System.out.println(" connected compoments"); } }
public void back_edge(int adj[][], int source) { stack = new Stack
backEdges = new HashMap
adjacencyMatrix = new int[number_of_nodes + 1][number_of_nodes + 1];
for (int sourcevertex = 1; sourcevertex <= number_of_nodes; sourcevertex++)
{
for (int destinationvertex = 1; destinationvertex <= number_of_nodes; destinationvertex++)
{
adjacencyMatrix[sourcevertex][destinationvertex] =
adj[sourcevertex][destinationvertex];
}
}
int visited[] = new int[number_of_nodes + 1];
int element = source;
int destination = source;
visited[source] = 1;
stack.push(source);
while (!stack.isEmpty())
{
element = (Integer) stack.peek();
destination = element;
while (destination <= number_of_nodes)
{
if (adjacencyMatrix[element][destination] == 1 && visited[destination] == 1)
{
if (stack.contains(destination))
{
backEdges.put(element, destination);
adjacencyMatrix[element][destination]= 0;
}
}
if (adjacencyMatrix[element][destination] == 1 && visited[destination] == 0 )
{
stack.push(destination);
visited[destination] = 1;
adjacencyMatrix[element][destination] = 0;
element = destination;
destination = 1;
continue;
}
destination++;
}
stack.pop();
}
System.out.println("Back Edges :(1 = back edge 0=not back edge) using adjacency matrix"); number_of_nodes = adjacencyMatrix[1].length - 1; System.out.print(" "); for(int i=1;i<=number_of_nodes;i++) System.out.print(" "+i); System.out.println(); for(int j=1;j<=number_of_nodes;j++) { System.out.print(j+"-->"); for(int i=1;i<=number_of_nodes;i++) System.out.print(adjacencyMatrix[i][j]+" "); System.out.println(); } } public static void main(String...arg) { int number_of_nodes, source; Scanner scanner = null; try { System.out.println("Enter the number of nodes in the graph"); scanner = new Scanner(System.in); number_of_nodes = scanner.nextInt(); int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1]; System.out.println("Enter the adjacency matrix"); for (int i = 1; i <= number_of_nodes; i++) for (int j = 1; j <= number_of_nodes; j++) adjacency_matrix[i][j] = scanner.nextInt(); System.out.println("Enter the source for the graph"); source = scanner.nextInt(); System.out.println("The DFS Traversal for the graph is given by "); DFS dfs = new DFS(); dfs.dfs(adjacency_matrix, source); dfs.connected(adjacency_matrix, source); dfs.back_edge(adjacency_matrix, source); }catch(InputMismatchException inputMismatch) { System.out.println("Wrong Input format"); } scanner.close(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
