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

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
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...
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 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...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
In C++, Design and implement an ADT that represents a triangle. The data for the ADT...
In C++, Design and implement an ADT that represents a triangle. The data for the ADT should include the three sides of the triangle but could also include the triangle’s three angles. This data should be in the private section of the class that implements the ADT. Include at least two initialization operations: one that provides default values for the ADT’s data, and another that sets this data to client-supplied values. These operations are the class’s constructors. The ADT also...
3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...
3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. import java . io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int...
26.5 Project 3: List & Queue ADT Overview For this project, you are to implement two...
26.5 Project 3: List & Queue ADT Overview For this project, you are to implement two abstract data types (ADTs). You will write a doubly linked list (Dll) and a queue class. The queue will use the dll internally. The class interfaces are downloadable below. You must follow the interface exactly. While you can define other public and private methods and fields, the class names and methods given must appear as provided, or you will not pass the unit tests....
implement the Queue ADT using the linked list approach #include "QueueLinked.h" template QueueLinked::QueueNode::QueueNode(const DataType& nodeData, QueueNode*...
implement the Queue ADT using the linked list approach #include "QueueLinked.h" template QueueLinked::QueueNode::QueueNode(const DataType& nodeData, QueueNode* nextPtr) { } template QueueLinked::QueueLinked(int maxNumber = Queue::MAX_QUEUE_SIZE) { } template QueueLinked::QueueLinked(const QueueLinked& other) { } template QueueLinked& QueueLinked::operator=(const QueueLinked& other) { } template QueueLinked::~QueueLinked() { } template void QueueLinked::enqueue(const DataType& newDataItem) throw (logic_error) { } template DataType QueueLinked::dequeue() throw (logic_error) {    DataType temp;    return temp; } template void QueueLinked::clear() { } template bool QueueLinked::isEmpty() const {    return false; } template...
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of...
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of the method push, pop, top, isEmpty, and size. You should not use any extra data structure. Related codes: public interface Stack<E> { int size( ); boolean isEmpty( ); void push(E e); E top( ); E pop( ); } public class LinkedStack<E> implements Stack<E> { private SinglyLinkedList<E> list = new SinglyLinkedList<>( );    public LinkedStack( ) { }    public int size( ) { return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT