Question

In: Computer Science

1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using...

1. Implement the graph ADT using the adjacency list structure.

2. Implement the graph ADT using the adjacency matrix structure.

LANGUAGE IS IN JAVA

Comment for any questions

Data structures and algorithms

Solutions

Expert Solution

Save this file as Adjacency.java since class Adjacency is public

CODE FOR ADJACENCY LIST IN JAVA:

//implementation of Adjacency list
import java.util.LinkedList;

public class Adjacency
{
   // class to represent a graph.
   // A graph is an array of adjacency lists.
   static class Graph
   {
       int nVertices;
       LinkedList<Integer> adjListArray[];
      
       // constructor
       //Size of array and = number of vertices in graph
       Graph(int nVertices)
       {
           this.nVertices = nVertices;
           adjListArray = new LinkedList[nVertices];
          
           // Creating a new list for each vertex tp store adjacent nodes
           for(int i = 0; i < nVertices ; i++){
               adjListArray[i] = new LinkedList<>();
           }
       }
   }
  
   // To add edge
   static void addEdge(Graph graph, int src, int dest)
   {
       // To add edge from source to destination
       graph.adjListArray[src].add(dest);
      
   // To add an edge from destination to source since the graph is undirected
       graph.adjListArray[dest].add(src);
   }
  
   //function to print the adjacency list for a given graph
   static void print(Graph graph)
   {     
       for(int v = 0; v < graph.nVertices; v++)
       {
           System.out.println("Adjacency list of vertex "+ v);
           System.out.print("head");
           for(Integer pCrawl: graph.adjListArray[v]){
               System.out.print(" -> "+pCrawl);
           }
           System.out.println("\n");
       }
   }
  
   // Driver function
   public static void main(String args[])
   {
       // creating a graph
       int V = 5;
       Graph graph = new Graph(V);
       addEdge(graph, 0, 1);
       addEdge(graph, 0, 4);
       addEdge(graph, 1, 2);
       addEdge(graph, 1, 3);
       addEdge(graph, 1, 4);
       addEdge(graph, 2, 3);
       addEdge(graph, 3, 4);
  
       // printing the adjacency list
       print(graph);
   }
}
SCREENSHOT AND OUTPUT OF THE PROGRAM:

OUTPUT:

CODE FOR ADJACENCY MATRIX IN JAVA:

Save this class in a file named Graph.java

//Adjacency Matrix in Java
public class Graph {
// Two-dimensional matrix that will store only boolean values
private boolean adjMatrix[][];
  
//store the value of the number of vertices in a graph
private int nVertices;

public Graph(int nVertices) {
this.nVertices = nVertices;
adjMatrix = new boolean[nVertices][nVertices];
}
//To add Edge
public void addEdge(int i, int j) {
adjMatrix[i][j] = true;
adjMatrix[j][i] = true;
}
//To remove Edge
public void removeEdge(int i, int j) {
adjMatrix[i][j] = false;
adjMatrix[j][i] = false;
}
//To return edge
public boolean isEdge(int i, int j) {
return adjMatrix[i][j];
}
public String toString() {
StringBuilder s = new StringBuilder();
for (int i = 0; i < nVertices; i++) {
s.append(i + ": ");
for (boolean j : adjMatrix[i]) {
s.append((j?1:0) + " ");
}
s.append("\n");
}
return s.toString();
}
//Driver function
public static void main(String args[])
{
Graph graph = new Graph(4);

graph.addEdge(1, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 2);
graph.addEdge(2, 0);
graph.addEdge(2, 3);

//Printing the adjacency matrix
System.out.print(graph.toString());
}
}

SCREENSHOTS OF CODE AND SAMPLE OUTPUT:

OUTPUT:

HAPPY LEARNING


Related Solutions

Write C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
Please provide Python 3.7 implementation of Graph ADT using in-built structure List only (not using dict)....
Please provide Python 3.7 implementation of Graph ADT using in-built structure List only (not using dict). Basic structure of graph will be: Node = [] Edges = [ [ ] , [ ] ] Also provide method to print the graph along with all path
Answer True or False 1. For graph representation, adjacency Matrix is more efficiency than adjacency list...
Answer True or False 1. For graph representation, adjacency Matrix is more efficiency than adjacency list in term of searching for edge. 2. Topological sort runs in O(|V| + |E|) where |V| is the number of vertices, and |E| is the number of edges in the input graph. 3. If vertex u can reach vertex v, and vertex v can reach vertex u, then vertices u and v are in the same Strongly-connected component (SCC). 4. The Bellman-Ford algorithm will...
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS:...
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS: Not allowed to use ANY built-in Python data structures and their methods. You must solve by importing the DynamicArray class and using class methods to write solution. Also not allowed to directly access any variables of the DynamicArray class (like self.size, self.capacity and self.data in part 1). All work must be done by only using class methods. Below is the Bag ADT starter code...
Overview: implement the ADT List in Java. This program is meant to the ADT List from...
Overview: implement the ADT List in Java. This program is meant to the ADT List from the ground up In the lecture, we learned how to implement an ADT like the ArrayList you have used in Project 1. With this project, you have the chance to implement an ADT called MyList, which is a simplified replacement for the full-blown ArrayList. Requirements You will implement the MyList ADT according to the following: 1. MyList must implement the List interface. It will...
Implement the ADT character string as the class LinkedString by using a linked list of characters....
Implement the ADT character string as the class LinkedString by using a linked list of characters. Include the following LinkedString constructors and methods: LinkedString(char[] value) Allocates a new character linked list so that it represents the sequence of characters currently contained in the character array argument. LinkedString(String original) Initializes a new character linked list so that it represents the same sequence of characters as the argument. char charAt(int index) Returns the char value at the specified index. The first character...
You will implement the MyList ADT according to the following: 1. MyList must implement the List...
You will implement the MyList ADT according to the following: 1. MyList must implement the List interface. It will look something like below: public class MyList<E> implements List<E> { ... } Note: As said in the class, when implementing an interface, you have to implement each method in it. There are a lot of methods in the List interface. We are going to address this issue below. 2. The initial capacity of MyList must be 2. This is not an...
how do I create a graph class to store a adjacency list representation of a graph?...
how do I create a graph class to store a adjacency list representation of a graph? I need some help and dont conpletely understand how to start. if you could provide a code with some steps id appreciate it.
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
C++ program that converts a directed graph data from a user into a corresponding adjacency list...
C++ program that converts a directed graph data from a user into a corresponding adjacency list format. First it will read an input graph data from a user first, after it will convert it to the adjacency matrix format. Assume that the maximum number of vertices in the input graph is less than or equal to 50. The Sample Input 1 3 6 0 1 1 0 1 2 2 1 2 0 0 2 Output 1 0->1->2 1->0->2 2->0->1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT