Represent the
control-flow graph as a set of nodes and a map between nodes and
their neighbors. The following code snippet declares two
classes—ControlFlowGraph and its static inner class Node—to enable
the desired representation:
a. Implement the addNode method and the two addEdge
methods.
b. Implement the reachable method.
Note: "..." is where code is to be inserted below the post condition
import java.util.*;
public class ControlFlowGraph {
// invariant : set " nodes " contains all nodes in the graph , i .
e . , if
// there is an edge "m - > n " , both " m " and " n " are in "
nodes "
Set nodes; // set of nodes
Map> edges; // adjacency set
static class Node {
String clazz; // containing
class
String method; // containing
method
int id; // unique identifier
public Node(String clazz, String
method, int id) {
this.clazz =
clazz;
this.method =
method;
this.id =
id;
}
public String toString() {
return clazz +
"." + method + ": " + id;
}
@Override
public boolean equals(Object o)
{
// postcondition : returns true iff
all attributes match ( up to " equals ")
if(this == o)
return true;
if(o == null)
return false;
Node temp =
(Node) o;
if((!Objects.equals(this.clazz, temp.clazz)) ||
(!Objects.equals(this.method,
temp.method)) ||
(this.id
!= temp.id))
return false;
else
return true;
}
@Override
public int hashCode() {
// postcondition : satisfies the
hash - code contract
int hashKey =
31;
int hash =
1;
hash = hashKey *
hash + ((this.clazz == null) ? 0 : clazz.hashCode());
hash = hashKey *
hash + ((this.method == null) ? 0 : method.hashCode());
hash = hashKey *
hash + this.id;
return
hash;
}
}
public String toString() { // for
ControlFlowGraph
return "# nodes = " + nodes.size()
+ "\n nodes = " + nodes + "\n edges = " + edges;
}
public ControlFlowGraph() {
nodes = new HashSet();
edges = new HashMap>();
}
public void addNode(String clazz, String method,
int id) {
// postcondition : adds Node ( clazz, method, id ) to
the set of nodes ,
// and if the node is not previously in the set,
// updates the adjacency - set representation to map
the given node to an empty set
Node toAddNode = new Node(clazz,
method, id);
if(nodes.contains(toAddNode))
return;
nodes.add(toAddNode);
Set toAddSet = new HashSet();
edges.put(toAddNode,
toAddSet);
}
public void addEdge(String clazz1, String method1,
int id1,
String clazz2,
String method2, int id2) {
// postcondition : adds edge Node ( clazz1 , method1 ,
id1 ) -> Node ( clazz2 , method2 , id2 )
// if any of the two nodes is not already in the set
of nodes ,
// the set is updated to include them
//... QUESTION A
}
public void addEdge(String clazz, String method,
int id1, int id2) {
// postcondition : adds edge between nodes identified
by " id1 " and " id2 " ,
// which are in the same method " method " in class "
clazz "
//... QUESTION A
}
public boolean reachable(String c1, String m1, int
id1, String c2, String m2, int id2) {
return false;
// postcondition : returns true iff there is a
directed path ( of length >= 0)
// from Node ( c1 , m1 , id1 ) to Node ( c2 , m2 , id2
) in the graph
// ... QUESTION B
}
1 A
if(Node(clazz1) && Node(clazz2))
{
nodes.add(clazz1(edges),clazz2(edges))
}
else
{
Nodes.put(Nodes(clazz1),Nodes(clazz2))
}
2A
if(clazz1(method) ==clazz2(method)
{
edges.add(Node(id1),Node(id2))
}
1B
for(int i=0;i<c1;i++)
{
for(int j=0;j<c2;j++)
{
Edge.length = Node(i).id1+Node(j).id2;
}
}
if(Edge.length>0)
return true;
else
return false;
Represent the control-flow graph as a set of nodes and a map between nodes and their...
ProgramIntro.pngProgramIntro2.pngProgramIntro1.pngProgram1.pngProgram2.pngGraph Class:import java.util.ArrayList;//Graph is a class whose objects represent graphs. public class Graph { ArrayList<Node> nodeList; ArrayList<Edge> edgeList; public Graph() { nodeList = new ArrayList<Node>(); edgeList = new ArrayList<Edge>(); } public ArrayList<Node> getNodeList() { return nodeList; } public ArrayList<Edge> getEdgeList() { return edgeList; } public void addNode(Node n) { nodeList.add(n); } public void addEdge(Edge e) { edgeList.add(e); } public String...
//Graph Class:
import java.util.ArrayList;
//Graph is a class whose objects represent graphs.
public class Graph {
ArrayList<Node> nodeList;
ArrayList<Edge> edgeList;
public Graph() {
nodeList = new ArrayList<Node>();
edgeList = new ArrayList<Edge>();
}
public ArrayList<Node> getNodeList() {
return nodeList;
}
public ArrayList<Edge> getEdgeList() {
return edgeList;
}
public void addNode(Node n) {
nodeList.add(n);
}
public void addEdge(Edge e) {
edgeList.add(e);
}
public String toString() {
String s = "Graph g.\n";
if (nodeList.size() > 0) {
for (Node n : nodeList) {
// Print node info
String t = "\nNode " + n.getName() + ", abbrev " + n.getAbbrev() + ", value " + n.getVal() + "\n";
s = s.concat(t);
}
s = s.concat("\n");
}
return s;
}
}
// Node Class:
import java.util.ArrayList;
// Node is a class whose objects represent nodes (a.k.a., vertices) in the graph.
public class Node {
String name;
String val; // The value of the Node
String abbrev; // The abbreviation for the Node
ArrayList<Edge> outgoingEdges;
ArrayList<Edge> incomingEdges;
String color; //Create the color of the TYPE Node List
int start; //Create the Starting Time
int end; //Create the Ending Time
public Node( String theAbbrev ) {
setAbbrev( theAbbrev );
val = null;
name = null;
outgoingEdges = new ArrayList<Edge>();
incomingEdges = new ArrayList<Edge>();
}
public String getAbbrev() {
return abbrev;
}
public String getName() {
return name;
}
public String getVal() {
return val;
}
public ArrayList<Edge> getOutgoingEdges() {
return outgoingEdges;
}
public ArrayList<Edge> getIncomingEdges() {
return incomingEdges;...
Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the following classes Node.java: This class represents a vertex in the graph. It has only a single instance variable of type int which is set in the constructor. Implement hashCode() and equals(..) methods which are both based on the number instance variable Node - int number +Node(int number); +int getNumberO; +int hashCode() +boolean equals(Object o) +String toString0) Edge.java: This class represents a...
Java
Do 72a, 72b, 72c, 72d. Code & output required.
public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...
Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...
JAVA
Submit: HashSet.java
with the following methods added:
HashSet.java
// Implements a set of objects using a hash table.
// The hash table uses separate chaining to resolve
collisions.
// Original from buildingjavaprograms.com supplements
public class HashSet<E> {
private static final double MAX_LOAD_FACTOR = 0.75;
private HashEntry<E>[] elementData;
private int size;
// Constructs an empty set.
@SuppressWarnings("unchecked")
public HashSet() {
elementData = new HashEntry[10];
size = 0;
}
// ADD METHODS HERE for exercise solutions:
// Adds the...
Java: Return an array of booleans in a directed graph. Please complete the TODO section in the mark(int s) function import algs13.Bag; import java.util.HashSet; // See instructions below public class MyDigraph { static class Node { private String key; private Bag<Node> adj; public Node (String key) { this.key = key; this.adj = new Bag<> (); } public String toString () { return key; } public void addEdgeTo (Node n) { adj.add (n); } public Bag<Node> adj () { return adj;...
Given a graph and a starting vertex, count the number of nodes a
specified distance away.
Requirements:
Create a graph of int's. Given a starting node with value key
and a distance dist , return the number of nodes that have a path
from id with exactly dist edges. If there are multiple paths, only
consider the shortest one.
int countNodesWithDist(int id, int dist); // example
declaration
Examples:
For the graph below, countNodesWithDist(2, 1) should return 2
since there are...
Use the example Graph.javaPreview the document class and
Edge.javaPreview the document class as a starting point for this
assignment, you'll need to make your own main class to create an
instance of the Graph class.
For this assignment we are going to create a map of a city
subway system using a Graph data structure. The Metro Station Map
that you'll use for this assignment is here: Assignment 9 - Metro
Station Map.PNG
Enter all the information from the Metro...
A binary tree is a complete binary tree if all the internal nodes (including the root node) have exactly two child nodes and all the leaf nodes are at level 'h' corresponding to the height of the tree. Consider the code for the binary tree given to you for this question. Add code in the blank space provided for the member function checkCompleteBinaryTree( ) in the BinaryTree class. This member function should check whether the binary tree input by the...