├── MagicMatrix.java ├── MainA.java └── solution.java /MagicMatrix.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MagicMatrix { 4 | public static void main(String[] args) { 5 | Scanner sc=new Scanner(System.in); 6 | int n=sc.nextInt(); 7 | int[][] mat=new int[n][n]; 8 | 9 | magicMatrix(mat,n); 10 | } 11 | static void magicMatrix(int[][] mat,int n){ 12 | int pi=0; 13 | int pj=n/2; 14 | int pl=0; 15 | while(pl queue = new LinkedList<>(); 23 | boolean[] visited = new boolean[G.vertices]; 24 | queue.add(0); 25 | for (; !queue.isEmpty();) { 26 | int row = queue.poll(); 27 | System.out.print(row + " "); 28 | visited[row] = true; 29 | for (int col = 0; col < G.vertices; col++) { 30 | if (G.adjancencyMatrix[row][col] == 1 && !visited[col]) { 31 | visited[col] = true; 32 | queue.add(col); 33 | } 34 | 35 | } 36 | } 37 | 38 | } 39 | 40 | } 41 | 42 | public class MainA { 43 | 44 | public static void main(String[] args) { 45 | Graph g = new Graph(6); 46 | int[][] edges = {{0, 1}, {0, 2}, {1, 3}, {1, 4}, {2, 4}, {3, 5}, {4, 5}}; 47 | for (int[] e : edges) { 48 | g.addEdge(e[0], e[1]); 49 | } 50 | System.out.println(Arrays.deepToString(g.adjancencyMatrix)); 51 | g.BreadthFirstSearch(g); 52 | 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class GraphUsingList { 3 | public static void main(String[] args) { 4 | Scanner sc = new Scanner(System.in); 5 | 6 | // creating a edge (list of list) ex: [[],[],[]] 7 | List> edges = new ArrayList<>(); 8 | 9 | // creating sublist ex: [] 10 | List subList1 = new ArrayList<>(); 11 | subList1.add(0); 12 | subList1.add(1); 13 | List subList2 = new ArrayList<>(); 14 | subList2.add(0); 15 | subList2.add(2); 16 | List subList3 = new ArrayList<>(); 17 | subList3.add(1); 18 | subList3.add(2); 19 | List subList4 = new ArrayList<>(); 20 | subList4.add(2); 21 | subList4.add(3); 22 | 23 | // adding all sublist to the edges list, to create [[],[],[]] 24 | edges.add(subList1); 25 | edges.add(subList2); 26 | edges.add(subList3); 27 | edges.add(subList4); 28 | 29 | // edges[][] = [ [0,1], [0,2], [1,2], [2,3] ] 30 | 31 | // creating a empty graph such as list of list 32 | List> graph = new ArrayList<>(); 33 | 34 | // creating empty sublist 35 | List s1 = new ArrayList<>(); 36 | List s2 = new ArrayList<>(); 37 | List s3 = new ArrayList<>(); 38 | List s4 = new ArrayList<>(); 39 | 40 | // adding empty sublist to the graph list, to create list of list [[],[],[]] 41 | graph.add(s1); 42 | graph.add(s2); 43 | graph.add(s3); 44 | graph.add(s4); 45 | 46 | // assigning values to graph list 47 | for(List edge : edges) { // accessing sublist from edges list [0,1] [0,2] 48 | int u = edge.get(0); // Each sublist has two elements, so accessing it and assigning first element to u 49 | int v = edge.get(1); // second element to v 50 | 51 | graph.get(u).add(v); // adding the adjacency vertices, such as if u = 0, its nearest is 1 which v. so adding it in the 0th index of sublist of graph 52 | graph.get(v).add(u); // adding the adjacency vertices, such as if v = 1, its nearest is 0 which u. so adding it in the 0th index of sublist of graph 53 | } 54 | 55 | // graph representation 56 | // 0 [1, 2] 57 | // 1 [0, 2] 58 | // 2 [0, 1, 3] 59 | // 3 [2] 60 | 61 | // printing the graph 62 | for(List sg: graph) { // accessing the sublist of the graph using for-each loop 63 | for(int element : sg) { // accessing each element from the sublist of graph 64 | System.out.print(element+" "); // printing the element 65 | } 66 | System.out.println(); 67 | } 68 | 69 | sc.close(); 70 | } 71 | } 72 | --------------------------------------------------------------------------------