Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AdjacencyMatrix {
- public static int[][] matrix = new int[20][20];
- public static void addEdge(int src, int dst)
- {
- matrix[src][dst]=1;
- matrix[dst][src]=1;
- }
- public static void printMatrix(int vertices)
- {
- for(int i=1; i<=vertices; i++){
- for(int j=1; j<=vertices; j++){
- System.out.print(matrix[i][j]+" ");
- }
- System.out.println();
- }
- }
- public static void main(String[] args) {
- // TODO code application logic here
- Scanner sc = new Scanner(System.in);
- int vertices, edges;
- System.out.println("Enter number of vertices: ");
- vertices = sc.nextInt();
- System.out.println("Enter number of edges: ");
- edges = sc.nextInt();
- int src, dst;
- for(int i=1; i<=edges; i++){
- System.out.println("Enter the source node: ");
- src = sc.nextInt();
- System.out.println("Enter the destination node: ");
- dst = sc.nextInt();
- addEdge(src, dst);
- }
- printMatrix(vertices);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement