Menu

[Solved]N Lab Write Programs Represent Graphs Two Different Formats Adjacency Matrix Adjacency Lis Q37275222

n this lab, you will write programs to represent graphs in twodifferent formats:

  • Adjacency Matrix and
  • Adjacency List

The input to the program will be as follows:

  • The number of vertices
  • The edges between the nodes (node ID starts from 0)

You will write programs to store the representations as a matrixand a list. Then you will implement print methods to print therepresentations.

You will complete the following methods for this lab:

// Add the edges specified in the inputstatic void addEdge(Graph graph, int src, int dst){// Implement this method }// Create the adjacency matrix representationstatic void generateMatrix(Graph graph){ // Implement this method}// print the adjacency matrixstatic void printMatrix(Graph graph){ // Implement this method}// Print the graph as adjacency liststatic void printGraph(Graph graph){ // Implement this method}

Sample Input

How many vertices?: 5
Enter the edges between two nodes(counting from 0), -1 tostop
0 1
0 4
1 2
1 3
1 4
2 3
3 4
-1

Enter your choices: 1
1 print adjacency list
2 print adjacency matrix

Sample Output

Adjacency list of vertex 0
head-> 4-> 1
Adjacency list of vertex 1
head-> 4-> 3-> 2-> 0
Adjacency list of vertex 2
head-> 3-> 1
Adjacency list of vertex 3
head-> 4-> 2-> 1
Adjacency list of vertex 4
head-> 3-> 1-> 0

Explanation

We chose to print the adjacency list representation of the inputgraph.

Below is a template to work from:

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;

public class UndirectedGraph {
  
   // Class to represent a graph using adjacencylist
   // and the number of nodes
   static class Graph{
       int Nodes;
       LinkedList<Integer>adjVertices[];
       int[][] adjMatrix;
      
      
      @SuppressWarnings(“unchecked”)
       public Graph(int n){
           this.Nodes =n;
           adjVertices =new LinkedList<Integer>[Nodes];
           adjMatrix = newint[Nodes][Nodes];
          
           for(int i = 0; i< Nodes; i++){
              adjVertices[i] = newLinkedList<Integer>();
           }
       }
   }
  
   // Add an edge between two given vertices
   static void addEdge(Graph graph, int src, intdst){
      graph.adjVertices[src].add(dst);
      graph.adjVertices[dst].add(src);
   }
  
   // Create the adjacency matrix representation
   static void generateMatrix(Graph graph){
       // Implement this method
   }
  
   // print the adjacency matrix
   static void printMatrix(Graph graph){
       // Implement this method
   }
   // Print the graph as adjacency list
   static void printGraph(Graph graph){
   for(int v = 0; v < graph.Nodes; v++)
{
System.out.println(“Adjacency list of vertex “+ v);
System.out.print(“head”);
for(Integer pCrawl: graph.adjVertices[v]){
System.out.print(” -> “+pCrawl);
}
System.out.println(“n”);
}
   }
  
  
   public static void main(String args[]){
       Scanner input = newScanner(System.in);
       System.out.println(“How manyvertices?: “);
       int nodeCount =input.nextInt();
       Graph graph = null;
       if(nodeCount > 0){
           graph = newGraph(nodeCount);
       }
       System.out.println(“Enter the edgesbetween two nodes(counting from 0), -1 to stop”);
       while(true){
           int src =input.nextInt();          
           if(src == -1)break;
           int dest =input.nextInt();
           if((src >nodeCount – 1) || (dest > nodeCount – 1)){
              System.out.println(“Invalid vertex.”);
           } else{
              addEdge(graph, src, dest);
           }
       }
       System.out.println(“Enter yourchoices: n”
              + “1 print adjacency listn”
              + “2 print adjacency matrixn”);
       int choice = input.nextInt();
       switch(choice){
       case 1:
          printGraph(graph);
           break;
       case 2:
          generateMatrix(graph);
          printMatrix(graph);
           break;
       default:
       System.out.println(“Enter thecorrect choice:”);
//        case 3:
//           System.out.println(“Enter the beginning vertex:”);
//            int source =input.nextInt();
//           System.out.println(“BFS traversal of the graph starting at ” +source);
//            BFS(graph,source);
       }
      
       input.close();  
   }
}

Expert Answer


Answer to n this lab, you will write programs to represent graphs in two different formats: Adjacency Matrix and Adjacency List Th… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *