Answered step by step
Verified Expert Solution
Question
1 Approved Answer
package edu.desu.graphs; import java.util.Arrays; import java.util.LinkedList; public class DSUGraph { / / No . of vertices private final int vertices; private final int [ ]
package edu.desu.graphs;
import java.util.Arrays;
import java.util.LinkedList;
public class DSUGraph
No of vertices
private final int vertices;
private final int adjacencyMatrix;
adjacencyList
private final LinkedList adjacencyList;
public boolean isConnected
return isConnected;
public boolean isDirected
return isDirected;
private final boolean isConnected;
private final boolean isDirected;
Constructor, for every node in the graph, create a list to represent adjacent nodes
@SuppressWarningsunchecked
DSUGraphint numOfVertices, boolean connected, boolean directed
isDirected directed;
isConnected connected;
vertices numOfVertices ;
adjacencyList new LinkedListvertices;
for int i ; i vertices; i
adjacencyListi new LinkedList;
adjacencyMatrix new intverticesvertices;
for int i ; i vertices; i
for int j ; j vertices; j
adjacencyMatrixij;
Function to add an edge into the graph
public void addEdgeint source, int destination
Add edge for Adjacency list
adjacencyListsourceadddestination;
Add Edge for Matrix
adjacencyMatrixsourcedestination;
ifisDirected
adjacencyListdestinationaddsource;
adjacencyMatrixdestinationsource;
Hack method because the indexes are not aligned with the actual value in the array
public int rowSumint arr, int n
base or terminating condition
if n
return ;
Calling method recursively
return rowSumarr n arrn ;
public void print
Loop through Matrix and print all rows
System.out.println MATRIX ;
for int row : adjacencyMatrix
final int value rowSumrow row.length;
ifvalue
converting each row as string and then printing in a separate line
System.out.printlnArraystoStringrow;
System.out.println
;
System.out.println LIST ;
Loop through List and Print values
for int i ; i vertices; i
ifadjacencyListiisEmpty
System.out.printi;
for int x : adjacencyListi
System.out.printx;
System.out.println;
public int getVertices
return vertices;
public int getAdjacencyMatrix
return adjacencyMatrix;
public LinkedList getAdjacencyList
return adjacencyList;
public int inDegreeint i
YOU MUST IMPLEMENT THIS METHOD
return ;
public int outDegreeint i
YOU MUST IMPLEMENT THIS METHOD
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started