Question: Can you help with this question please? (Part 3 Java) Codes: DiGraphADT: import java.util.*; /** * An interface for a simple unweighted digraph, for use
Can you help with this question please?
(Part 3 Java)



Codes:
DiGraphADT:
import java.util.*;
/**
* An interface for a simple unweighted digraph, for use in CS207.
* No node data is maintained. Nodes are numbered from 0.
*
*/
public interface DiGraphADT
{
/** the number of nodes in the graph
@return returns the number of nodes in the graph
*/
int nNodes();
/** the number of edges in the graph
@return returns the number of edges in the graph
*/
int nEdges();
/** Adds an edge from node1 to node2
If the edge is already present in the graph it should not be duplicated
and the method should return false. Otherwise the edge should be added
and the method should return true.
@param node1 source node
@param node2 destination node
@return returns true if the edge was not already present. Otherwise
returns false.
*/
boolean addEdge(int node1, int node2);
/** true if there is an edge from node1 to node2
@param node1 source node
@param node2 destination node
@return returns true iff there is an edge from node1 to node2
*/
boolean isEdge(int node1, int node2);
/** The successors of a given node in the graph
@param node a node in the graph
@return returns an ArrayList of Integers which represent the successor
nodes of the given node
*/
ArrayList
/** The predecessors of a given node in the graph
@param node a node in the graph
@return returns an ArrayList of Integers which represent the predecessor nodes of the given node
*/
ArrayList
/** The outDegree of a node in the graph
@param node a node in the graph
@return returns the outDegree of the given node
*/
int outDegree(int node);
/** The inDegree of a node in the graph
@param node a node in the graph
@return returns the inDegree of the given node
*/
int inDegree(int node);
}
DiGraphEdgeList:
import java.util.*;
/**
* DiGraphEdgeList provides an implementation of a digraph using
* an edge list
*
*/
public class DiGraphEdgeList implements DiGraphADT
{
// instance variable
private ArrayList
private int numNodes; // number of nodes
//internal class
class Edge {
int node1, node2;
Edge(int node1, int node2){
this.node1 = node1;
this.node2 = node2;
}
}
/**
* Constructor for objects of class GraphEdgeList
* @param n number of nodes in the graph
*/
public DiGraphEdgeList(int n)
{
// initialise instance variables
edges = new ArrayList
numNodes = n;
}
/** the number of nodes in the graph
@return returns the number of nodes in the graph
*/
public int nNodes()
{
return numNodes;
}
/** the number of edges in the graph
@return returns the number of edges in the graph
*/
public int nEdges()
{
return edges.size();
}
/** Adds an edge from node1 to node2
If the edge is already present in the graph it should not be duplicated
and the method should return false. Otherwise the edge should be added
and the method should return true.
@param node1 source node
@param node2 destination node
@return returns true if the edge was not previously present. Otherwise returns false.
*/
public boolean addEdge(int node1, int node2)
{
if (isEdge(node1,node2))
return false;
Edge edge = new Edge(node1,node2);
edges.add(edge);
return true;
}
/** true if there is an edge from node1 to node2
@param node1 source node
@param node2 destination node
@return returns true iff there is an edge from node1 to node2
*/
public boolean isEdge(int node1, int node2)
{
int i=0;
boolean found = false;
while (!found && i { if ((edges.get(i).node1 == node1) && (edges.get(i).node2 == node2)) found = true; else i++; } return found; } /** The successors of a given node in the graph @param node a node in the graph @return returns an ArrayList of Integers which represent the successor nodes of the given node */ public ArrayList { ArrayList for (int i=0; i if (edges.get(i).node1 == node) successorNodes.add(edges.get(i).node2); return successorNodes; }; /** The predecssors of a given node in the graph @param node a node in the graph @return returns an ArrayList of Integers which represent the predecessor nodes of the given node */ public ArrayList { ArrayList for (int i=0; i if (edges.get(i).node2 == node) predecessorNodes.add(edges.get(i).node1); return predecessorNodes; }; /** The outDegree of a node in the graph @param node a node in the graph @return returns the outDegree of the given node */ public int outDegree(int node) { return successors(node).size(); } /** The inDegree of a node in the graph @param node a node in the graph @return returns the inDegree of the given node */ public int inDegree(int node) { return predecessors(node).size(); } } Edges: 1 5 4 3 2 1 7 6 8 5 9 5 0 1 6 0 7 4 9 1 1 8 3 9 3 0 4 2 6 3 - Topological: import java.util.*; import java.io.*; /** * Class Topological * The main method creates and populates a graph from edges in file * Edges.txt. It then calls method topologicalOrder() which you are asked to * code using the final algorithm for this presented in lectures (slide 13 * of MyPlace item 10.5.) * */ public class Topological { // instance variables - do not alter private DiGraphADT g; private int n; /** * Constructor for objects of class Topological - do not alter except * that you may wish to replace the instance of DiGraphEdgeList with an * instance of a different implementation of DiGraphADT */ public Topological() { // initialise instance variables n = 10; // example graph used here has 10 nodes g = new DiGraphEdgeList(n); try { readGraph(); } catch (Exception e){ System.out.println("Invalid file content"); } } /** * populates the graph taking data from file Edges.txt - do not alter */ private void readGraph() throws Exception { int firstNode, secNode; Reader r = new BufferedReader(new FileReader("Edges.txt")); StreamTokenizer st = new StreamTokenizer(r); st.parseNumbers(); st.nextToken(); while (st.ttype != StreamTokenizer.TT_EOF) { firstNode = (int) st.nval; st.nextToken(); secNode = (int) st.nval; System.out.println(firstNode + " " + secNode); g.addEdge(firstNode,secNode); st.nextToken(); } } /** prints a topological ordering of the nodes in the graph, using * the improved algorithm presented in lectures. This algorithm makes use * of a queue holding nodes of degree 0 */ public void topologicalOrder() { // declare local variables // create an empty queue // populate an array with initially holding the indegrees of nodes // and place nodes with indegree 0 on the queue // repeatedly remove a node from the queue, print it (i.e. display // the node number), decrement the array entry for all its successor // nodes and add to the queue any nodes whose indegree hence falls to 0 } /** creates and populates a graph and then prints a topological * ordering of the nodes - do not alter */ public static void main(String[] args) { Topological gr = new Topological(); gr.topologicalOrder(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
