I need help implementing this with an ArrayList import java.util.*; public class
ID: 3772012 • Letter: I
Question
I need help implementing this with an ArrayList
import java.util.*;
public class ListsGraph implements Graph{
private List<Integer>[] edges;
public ListsGraph(int n){
//This constructor is complete. Do not change it.
edges = new List[n];
for(int i = 0; i < edges.length; i++)
edges[i] = new ArrayList<Integer>(); //Every index of the array is an object of type Java ArrayList.
}
/*In the following methods:
* The parameter head is an index of the array edges and the parameter tail is a member of
* the list (of type java ArrayList) of edges[head].
*/
// Set the edge (head, tail).
public void setEdge(int head, int tail){
{
if (head > ListsGraph. || destination > Adjacency_List.size())
{
System.out.println("the vertex entered in not present ");
return;
}
List<Integer> slist = Adjacency_List.get(source);
slist.add(destination);
List<Integer> dlist = Adjacency_List.get(destination);
dlist.add(source);
}
}
// Disconnect the edge (head, tail)
public void removeEdge(int head, int tail){
//Complete this method.
check(head, tail);
}
// Returns true if there is an edge between (head, tail)
public boolean isEdge(int head, int tail){
check(head, tail);
//Complete this method.
return false; //This line is temporary. Remove it when you complete this method.
}
// Returns number of vertices of the graph
public int numberOfVertices(){
//Complete this method.
return 0; //This line is temporary. Remove it when you complete this method.
}
private void check(int head, int tail){
//This method is complete. Do not change it.
check(head);
check(tail);
}
private void check(int vertex){
//This method is complete. Do not change it.
try{
if(vertex < 0 || vertex >= edges.length)
throw new GraphException("Wrong vertex number " + vertex);
}catch (GraphException e){
System.out.println(e.getMessage());
System.exit(0);
}
}
}
Explanation / Answer
I made bold for modified functions....
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication3;
/**
*
* @author prmsh
*/
import java.util.*;
public class ListsGraph implements Graph{
private List<Integer>[] edges;
public ListsGraph(int n){
//This constructor is complete. Do not change it.
edges = new List[n];
for(int i = 0; i < edges.length; i++)
edges[i] = new ArrayList<Integer>(); //Every index of the array is an object of type Java ArrayList.
}
/*In the following methods:
* The parameter head is an index of the array edges and the parameter tail is a member of
* the list (of type java ArrayList) of edges[head].
*/
// Set the edge (head, tail).
public void setEdge(int head, int tail){
{
if (head > ListsGraph || destination > Adjacency_List.size())
{
System.out.println("the vertex entered in not present ");
return;
}
List<Integer> slist = Adjacency_List.get(source);
slist.add(destination);
List<Integer> dlist = Adjacency_List.get(destination);
dlist.add(source);
}
}
// Disconnect the edge (head, tail)
public void removeEdge(int head, int tail){
//Complete this method.
if (head > ListsGraph. || destination > Adjacency_List.size())
{
System.out.println("the vertex entered in not present ");
return;
}
//removing edge
List<Integer> slist = Adjacency_List.get(source);
slist.remove(destination)
List<Integer> dlist = Adjacency_List.get(destination);
dlist.remove(source);
check(head, tail);
}
// Returns true if there is an edge between (head, tail)
public boolean isEdge(int head, int tail){
check(head, tail);
//Complete this method.
//getting lists by source and destination
List<Integer> slist = Adjacency_List.get(source);
List<Integer> dlist = Adjacency_List.get(destination);
//checking edges present or not
if(slist.contains(destination) && dlist.contains(source)){
return true;
}
return false; //This line is temporary. Remove it when you complete this method.
}
// Returns number of vertices of the graph
public int numberOfVertices(){
//Complete this method.
List<String> edgeList = Adjacency_List;
Set<String> uniqueList = new HashSet<String>(edgeList);
return uniqueList.size();
}
private void check(int head, int tail){
//This method is complete. Do not change it.
check(head);
check(tail);
}
private void check(int vertex){
//This method is complete. Do not change it.
try{
if(vertex < 0 || vertex >= edges.length)
throw new GraphException("Wrong vertex number " + vertex);
}catch (GraphException e){
System.out.println(e.getMessage());
System.exit(0);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.