public class Graph { /* this function will convert graph that it get fom GraphToSet.java to set */ public static void convert(int[][] a) { int rows = a.length; int cols = a[0].length; System.out.println("Graph in matrix form["+rows+"]["+cols+"] = "); //Displaying the graph in matrix form
for (int i=0; i<rows; i++) { System.out.print("{"); for (int j=0; j<cols; j++) System.out.print(" " + a[j] + ","); System.out.println("},"); } System.out.println("\nG=(V,E)"); System.out.print("G=({"); for (int k=0; k<rows; k++)
System.out.print(" "+ a[k][0] + ", "); //retrieving all the vertices from GraphToSet.java System.out.print("}"); //and print it out System.out.print(", {");
for (int i=0; i<rows; i++) { System.out.print("("); for (int j=0; j<cols; j++) //retrieving all the edges from GraphToSet.java System.out.print(" " + a[j] + ","); //and print it out System.out.print(")"); } System.out.println("})"); //final output for graph to set
}
} ------------------------------------------------------------ public class GraphToSet { /* run GraphToSet to get the output from Graph.java and GraphToJava.java */ public static void main(String[] argv) { int x[][] = { { 1, 2 }, //graph are repsented in matrix form { 2, 3 }, { 3, 4 }, { 4, 1 }, { 1, 3 },
};
Graph.convert(x); //convert the graph into matrix by calling the function //convert() from Graph.java
} } --------------------------------------------------------- the output of this program will have at least 1 item that is repeated. for example {1, 2, 3, 4, 1} how to make the program only print out one "1" {1, 2, 3, 4}? The program output is ({1, 2, 3, 4, 1}, {1,2},{2,3},{3,4},{4,1}{1,3}). what i want is ({1, 2, 3, 4}, {1,2},{2,3},{3,4},{4,1}{1,3}).