Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Scanner;
- import java.util.Stack;
- public class JavaApplication14 {
- /*
- 9 11
- 0 1
- 0 8
- 1 5
- 2 3
- 2 6
- 2 7
- 4 5
- 4 8
- 5 6
- 5 8
- 6 7
- 4 4
- 0 2
- 1 2
- 1 3
- 2 3
- */
- public static void main(String[] args) {
- Scanner sc=new Scanner(System.in);
- int V=sc.nextInt();
- int E=sc.nextInt();
- //Declare a graph.
- int counter=0;
- int G[][]=new int[V][V];
- //Connect the edges.
- // G[0][2]=1; G[2][0]=1;
- // G[1][2]=1; G[2][1]=1;
- // G[1][3]=1; G[3][1]=1;
- // G[2][3]=1; G[3][2]=1;
- while(E-->0){
- int src=sc.nextInt();
- int dest=sc.nextInt();
- G[src][dest]=1;
- G[dest][src]=1;
- }
- //Print Adjacency Matrix
- // System.out.println(Arrays.deepToString(G));
- //degree(G,V);
- display(G,V);
- // System.out.println("DFS: ");
- // DFS(G,V,0);
- }
- public static void BFS(int[][] adj,int V, int begin){
- Queue<Integer> Q=new LinkedList<>();
- boolean[] visited=new boolean[V];
- int num=V;
- visited[begin]=true;
- Q.add(begin);
- while(!Q.isEmpty()){
- int element=Q.remove();
- System.out.print(element+" ");
- int temp=0;
- while(temp<num){
- if((!visited[temp])&&(adj[element][temp]==1)){
- Q.add(temp);
- visited[temp]=true;
- }
- temp++;
- }
- }
- }
- public static void DFS(int[][] adj,int V, int begin){
- Stack<Integer> Q=new Stack<>();
- boolean[] visited=new boolean[V];
- int num=V;
- visited[begin]=true;
- Q.push(begin);
- while(!Q.isEmpty()){
- int element=Q.pop();
- System.out.print(element+" ");
- int temp=0;
- while(temp<num){
- if((!visited[temp])&&(adj[element][temp]==1)){
- Q.push(temp);
- visited[temp]=true;
- }
- temp++;
- }
- }
- }
- public static void degree(int g[][],int V){
- int counter=0;
- for (int row = 0; row < V; row++) {
- for (int col = 0; col < V; col++) {
- if(g[row][col]==1){
- counter++;
- }
- }
- System.out.print(row+" "+counter);
- counter=0;
- System.out.println();
- }
- }
- public static void display(int g[][],int V){
- for (int row = 0; row < V; row++) {
- for (int col = 0; col < V; col++) {
- System.out.print(g[row][col]+" ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement