Java Programming Write a program to perform a topological sort on a graph. Creat
ID: 3577303 • Letter: J
Question
Java Programming
Write a program to perform a topological sort on a graph.
Create a representation of a directed graph.
Create a Vertex class that has the value of the vertex, the indegree and a list of adjacent nodes.
Create a DirectedGraph class that is a list of Vertex objects. This is your graph.
Write a TopologicalSort class that has methods that take in a Directed graph and does a topological sort.
Your test case should use the graph representation of the following
Explanation / Answer
import java.io.*;
import java.util.*;
public category ID3
the category to represent a knowledge purpose consisting of numAttributes values
of attributes */
class DataPoint
};
/* the category to represent a node within the decomposition tree.
*/
class TreeNode
};
TreeNode root = new TreeNode();
public int getSymbolValue(int attribute, String symbol)
return index;
}
public int []getAllValues(Vector information, int attribute) {
Vector values = new Vector();
int num = information.size();
for (int i=0; i< num; i++) {
DataPoint purpose = (DataPoint)data.elementAt(i);
String image =
(String)domains[attribute].elementAt(point.attributes[attribute] );
int index = values.indexOf(symbol);
if (index < 0)
}
int []array = new int[values.size()];
for (int i=0; i< array.length; i++) {
String image = (String)values.elementAt(i);
array = domains[attribute].indexOf(symbol);
}
values = null;
return array;
}
public Vector getSubset(Vector information, int attribute, int value) set = new Vector();
int num = information.size();
for (int i=0; i< num; i++) {
DataPoint purpose = (DataPoint)data.elementAt(i);
if (point.attributes[attribute] == value) set.addElement(point);
}
return subset;
}
public double calculateEntropy(Vector data) {
int numdata = information.size();
if (numdata == 0) come 0;
int attribute = numAttributes-1;
int numvalues = domains[attribute].size();
double total = 0;
for (int i=0; i< numvalues; i++) {
int count=0;
for (int j=0; j< numdata; j++) {
DataPoint purpose = (DataPoint)data.elementAt(j);
if (point.attributes[attribute] == i) count++;
}
double chance = one.*count/numdata;
if (count > 0) total += -probability*Math.log(probability);
}
return sum;
}
public mathematician alreadyUsedToDecompose(TreeNode node, int attribute)
if (node.parent == null) come false;
return alreadyUsedToDecompose(node.parent, attribute);
}
public void decomposeNode(TreeNode node) {
double bestEntropy=0;
boolean selected=false;
int selectedAttribute=0;
int numdata = node.data.size();
int numinputattributes = numAttributes-1;
node.entropy = calculateEntropy(node.data);
if (node.entropy == 0) return;
for (int i=0; i< numinputattributes; i++) {
int numvalues = domains.size();
if ( alreadyUsedToDecompose(node, i) ) continue;
double averageentropy = 0;
for (int j=0; j< numvalues; j++) set = getSubset(node.data, i, j);
if (subset.size() == 0) continue;
double subentropy = calculateEntropy(subset);
averageentropy += subentropy *
subset.size();
}
averageentropy = averageentropy / numdata; //
Taking the weighted average
if (selected == false) else {
if (averageentropy < bestEntropy)
}
}
if (selected == false) return;
int numvalues = domains[selectedAttribute].size();
node.decompositionAttribute = selectedAttribute;
node.children = new TreeNode [numvalues];
for (int j=0; j< numvalues; j++)
for (int j=0; j< numvalues; j++)
node.data = null;
}
public int readData(String filename) throws Exception catch ( Exception e) {
System.err.println( "Unable to open information file: " + computer file name + " " + e);
return 0;
}
BufferedReader bin = new BufferedReader(new InputStreamReader(in) );
String input;
while(true) {
input = bin.readLine();
if (input == null) {
System.err.println( "No information found within the information file: " + computer file name +
" ");
return 0;
}
if (input.startsWith("//")) continue;
if (input.equals("")) continue;
break;
}
StringTokenizer tokenizer = new StringTokenizer(input);
numAttributes = tokenizer.countTokens();
if (numAttributes <= 1) {
System.err.println( "Read line: " + input);
System.err.println( "Could not obtain the names of attributes within the
line");
System.err.println( "Expecting a minimum of one input attribute and one
output attribute");
return 0;
}
domains = new Vector[numAttributes];
for (int i=0; i < numAttributes; i++) domains = new Vector();
attributeNames = new String[numAttributes];
for (int i=0; i < numAttributes; i++)
while(true)
root.data.addElement(point);
}
bin.close();
return 1;
}
public void printTree(TreeNode node, String tab) ME = new ID3();
int standing = Pine Tree State.readData("c:\in.txt");
if (status <= 0) return;
me.createDecisionTree();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.