/Graph Using Adjacency Matrix

Graph Using Adjacency Matrix

Adjacency Matrix

import java.util.Scanner;

public class AdjacencyMatrix {

    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int vertex[] = new int[scanner.nextInt()];
        int edges[][] = new int[vertex.length][vertex.length];

        for (int i = 0; i < vertex.length; i++) {
            vertex[i] = scanner.nextInt();
        }

        int nofedges = scanner.nextInt();
        for (int i = 0; i < nofedges; i++) {
            int pos1 = scanner.nextInt();
            int pos2 = scanner.nextInt();
            edges[pos1][pos2] = 1;
            edges[pos2][pos1] = 1;
        }
        printArray(edges);
    }

    private static void printArray(int a[][]) {
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[0].length; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
    }
}