Answer this question Explain how the constructor in your solution represents the…
- User
Answer this question Explain how the constructor in your solution represents the socialNetwork. Describe how you have adapted the DiGraph class so that it contains the same information as in the diagram.
Here are the classes
import java.util.ArrayList;
public class DiGraph { protected int numberOfNodes; protected ArrayList[] adjacencyList;
code/** * Constructor for a directed graphs with a number of nodes but no edges. * @param nodes the number of nodes in the graph */ public DiGraph(int nodes) { //initialise the number of nodes numberOfNodes = nodes; //make the array of ArrayLists adjacencyList = new ArrayList [nodes]; //go through each node and make a new ArrayList for that node for (int i = 0; i < nodes; i++) { adjacencyList[i] = new ArrayList<Integer>(); } } /** * Add an edge between the two nodes given * @param fromNode the node the edge starts from * @param toNode the node that the edge goes to */ public void addEdge(int fromNode, int toNode){ adjacencyList[fromNode].add(toNode); } /** * Return the number of nodes in the graph * @return the number of nodes in the graph */ public int getNumberOfNodes() {return numberOfNodes; } /** * Determine whether there is an edge between the two nodes given * @param fromNode the node the edge starts from * @param toNode the node that the edge goes to * @return true if there is an edge between fromNode and toNode, false otherwise */ public boolean hasEdge(int fromNode, int toNode) {return adjacencyList[fromNode].contains(toNode); } /** * Print the adjacency list representation of the graph */ public void printAdjacencyList(){ System.out.println("Number of nodes = "+ numberOfNodes); for (int i = 0; i< adjacencyList.length; i++){ System.out.print("Neighbours of " + i + " : "); for (Integer neighbour: adjacencyList[i]) { System.out.print(neighbour + " "); } System.out.println(); } } - Assistant
In the given solution, the
SocialNetworkclass extends theDiGraphclass, which represents a directed graph of a social network.The constructor of the
SocialNetworkclass invokes the constructor of theDiGraphclass with the number of network members. This initializes the graph with the number of nodes as the number of members in the social network.javapublic SocialNetwork() { super(Names.networkMembers.length); }The
DiGraphclass is adapted in theSocialNetworkclass by adding methods for finding followers, checking reachability, and finding receivers for a person.The
broadcastsTomethod in theSocialNetworkclass returns a list of followers (direct connections) of a given person. ThecanReachmethod checks if there is a path from the source person to the target person in the social network. ThereceiversOfmethod returns a list of receivers (all reachable nodes) of a given person.