Iterating over hashset and removing elements from it - java

I have a while(whiteSet.size()>0) check in my code and inside the while block I call dfs method. The method modifies the HashSet. Why does the while loop never gives an error?
public class CycleInDG {
Set<Vertex> whiteSet = new HashSet<>();
Set<Vertex> greySet = new HashSet<>();
Set<Vertex> blackSet = new HashSet<>();
List<Integer> cyclePath = new ArrayList<>();
Map<Vertex, Vertex> parentMap = new HashMap<>();
Graph g = new Graph();
public boolean hasCycle() {
Map<Integer, Vertex> map = g.getGraphMap();
Collection<Vertex> vertices = map.values();
for (Vertex vertex : vertices) {
whiteSet.add(vertex);
}
while(whiteSet.size()>0) {
parentMap.clear();
if(dfs(whiteSet.iterator().next(), null)) {
return true;
}
}
return false;
}
public boolean dfs(Vertex current, Vertex parent) {
moveVertex(current, whiteSet, greySet);
for (Vertex vertex : current.getNeighbors()) {
if(blackSet.contains(vertex)) {
continue;
}
if(greySet.contains(vertex)) {
parentMap.put(vertex, current);
return true;
}
parentMap.put(vertex, current);
boolean result = dfs(vertex, current);
if(result) {
return result;
}
}
moveVertex(current, greySet, blackSet);
return false;
}
public void moveVertex(Vertex v, Set<Vertex> src, Set<Vertex> dest) {
src.remove(v);
dest.add(v);
}
}

Related

Java BinaryTree balance a tree

I am trying to get a binary search tree to balance.
I balance the tree after the insert method.
I hope someone here can guide me through this.
The method is balanceTheTree(...).
I did a recursive method, I don't know if it's the best solution
public class BinaryTreeTable<E extends Comparable<E>, T> implements Table<E, T> {
private Node root;
public BinaryTreeTable() {
this.root = new Node(null, null, null);
}
#Override
public boolean insert(E key, T data) {
boolean ret;
if (this.root.key == null) {
Node toInsert = new Node(null, key, data);
this.root = toInsert;
ret = true;
} else {
Node father = this.seekFather(key);
if (father == null) {
ret = false;
} else {
Node toInsert = new Node(father, key, data);
if (key.compareTo(father.key) > 0) {
father.rSon = toInsert;
ret = true;
balanceTheTree(toInsert);
} else if (key.compareTo(father.key) < 0) {
father.lSon = toInsert;
ret = true;
balanceTheTree(toInsert);
} else {
ret = false;
}
}
}
return ret;
}
private void rightRotation(Node theN) {
Node k2 = theN.rSon;
theN.rSon = k2.lSon;
k2.lSon = theN;
}
private void leftRotation(Node theN) {
Node k1 = theN.lSon;
theN.lSon = k1.rSon;
k1.rSon = theN;
}
private void leftRightRotation(Node theN) {
leftRotation(theN.rSon);
rightRotation(theN);
}
private void rightLeftRotation(Node theN) {
rightRotation(theN.lSon);
leftRotation(theN);
}
private void balanceTheTree(Node theN) {
if (theN == null) {
} else {
if (Math.abs(Math.subtractExact(computeH(theN.rSon), computeH(theN.lSon))) > 1) {
System.out.println("A droite " + theN.getLabel());
if (computeH(theN.lSon) > computeH(theN.rSon)) {
leftRotation(theN);
} else {
leftRightRotation(theN);
}
} else if (Math.abs(Math.subtractExact(computeH(theN.lSon), computeH(theN.rSon))) > 1) {
System.out.println("A gauche");
} else {
balanceTheTree(theN.rSon);
balanceTheTree(theN.lSon);
}
}
}
private int computeH(Node theN) {
int ret = 1;
if (theN == null) {
ret = 0;
} else if ((theN.lSon != null) && (theN.rSon == null)) {
ret = computeH(theN.lSon) + 1;
} else if ((theN.lSon == null) && (theN.rSon != null)) {
ret = computeH(theN.rSon) + 1;
} else if ((theN.lSon != null) && (theN.rSon != null)) {
ret = Math.max(computeH(theN.lSon), computeH(theN.rSon)) + 1;
}
return ret;
}
public class Node {
// Attributs
private Node lSon ;
private Node rSon ;
private Node father ;
private T theValue ;
private E key ;
// Constructeur
public Node (Node father, E key, T theValue) {
this.father = father;
this.key = key;
this.theValue = theValue;
}
public String getLabel() {
return String.valueOf(key);
}
public Node getLeft() {
return lSon;
}
public Node getRight() {
return rSon;
}
public Node clone() {
return new Node(this.father, this.key, this.theValue);
}
}
}
The computeH() method allows me to know the size of a node
The insertion method works correctly.
The tree i want to balance
public static void main(String[] args) {
BinaryTreeTable binaryTreeTable = new BinaryTreeTable();
binaryTreeTable.insert(10, "Test");
binaryTreeTable.insert(5, "Test");
binaryTreeTable.insert(7, "Test");
binaryTreeTable.insert(3, "Test");
binaryTreeTable.insert(4, "Test");
binaryTreeTable.insert(15, "Test");
binaryTreeTable.insert(19, "Test");
binaryTreeTable.insert(16, "Test");
binaryTreeTable.insert(20, "Test");
binaryTreeTable.showTree();
}

Why isn't my iterator working? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
For a class assignment I have to implement my own iterator from scratch. The iterator is iterating through a linked list of nodes. All of my test cases that use the iterator are failing and I can't tell what is wrong with it.
import java.util.Iterator;
import java.util.NoSuchElementException;
class LinkedNodeIterator<E> implements Iterator<E> {
LinkedNode<E> headNode;
LinkedNode<E> curr;
// Constructors
public LinkedNodeIterator(LinkedNode<E> head) {
headNode = head;
curr = headNode;
}
#Override
public boolean hasNext() {
if(headNode == null)
return false;
if(curr.getNext() == null)
return false;
return true;
}
#Override
public E next() {
if(curr.getNext() == null || curr == null)
throw new NoSuchElementException();
LinkedNode<E> save = curr;
curr = curr.getNext();
return save.getData();
}
#Override
public void remove() {
throw new UnsupportedOperationException();
}
}
Some test cases that fail (they all return count = 0):
public class PublicLinkedSetTest {
Set<String> set0;
Set<String> set1;
Set<String> set2;
Set<String> set3;
Set<String> set4;
#Before
public void before() {
set0 = new LinkedSet<String>();
set1 = new LinkedSet<String>();
for (String e : new String[]{"c", "a", "d", "b", "e"}) {
set1 = set1.adjoin(e);
}
set2 = new LinkedSet<String>();
for (String e : new String[]{"b", "d", "a", "e", "c"}) {
set2 = set2.adjoin(e);
}
set3 = new LinkedSet<String>();
for (String e : new String[]{"a", "d", "b"}) {
set3 = set3.adjoin(e);
}
set4 = new LinkedSet<String>();
for (String e : new String[]{"x", "y", "z", "a", "b", "d"}) {
set4 = set4.adjoin(e);
}
}
public void testIterator1() {
int count = 0;
for (String e : set1) {
count += 1;
}
assertEquals(5, count);
}
#Test
public void testIterator2() {
int count = 0;
for (String e : set2) {
count += 1;
}
assertEquals(5, count);
}
#Test
public void testIterator3() {
int count = 0;
for (String e : set3) {
count++;
}
assertEquals(3, count);
}
Here is the code for my LinkedSet
import java.util.Iterator;
public class LinkedSet<E> implements Set<E> {
private LinkedNode<E> head = null;
private LinkedNode<E> link;
// Constructors
public LinkedSet() {
}
public LinkedSet(E e) {
this.head = new LinkedNode<E>(e, null);
}
private LinkedSet(LinkedNode<E> header) {
header = head;
}
#Override
public int size() {
int count = 0;
for(E e : this){
count++;}
return count;
}
#Override
public boolean isEmpty() {
for(E e : this){
if(e != null)
return false;
}
return true;
}
#Override
public LinkedNodeIterator<E> iterator() {
return new LinkedNodeIterator<E>(this.head);
}
#Override
public boolean contains(Object o) {
for(E e : this){
if(e == o)
return true;
}
return false;
}
#Override
public boolean isSubset(Set<E> that) {
that = new LinkedSet<E>();
if(this.size()>that.size())
return false;
for(E e : this){
if(that.contains(e) == false)
return false;
}
return true;
}
#Override
public boolean isSuperset(Set<E> that) {
that = new LinkedSet<E>();
if(this.isSubset(that))
return true;
else
return false;
}
#Override
public Set<E> adjoin(E e) {
boolean alwaysEqual = true;
if(this.head == null)
return this;
for(E t : this){
if(t != e)
alwaysEqual = false;}
if(alwaysEqual == true)
return this;
LinkedNode<E> temp = this.head;
LinkedNode<E> newNode = new LinkedNode<E>(e, temp);
LinkedSet<E> newSet = new LinkedSet<E>(newNode);
Set<E> otherSet = newSet;
return otherSet;
}
#Override
public Set<E> union(Set<E> that) {
Set<E> thisSet = this;
for(E e : that){
if(!this.contains(e))
thisSet = thisSet.adjoin(e);
}
return thisSet;
}
#Override
public Set<E> intersect(Set<E> that) {
LinkedSet<E> newSet = null;
Set<E> otherNewSet = newSet;
for(E e : that){
if(this.contains(e)){
if(otherNewSet == null){
LinkedNode<E> newNode = new LinkedNode<E>(e, null);
otherNewSet = new LinkedSet<E>(newNode);
}
else{
otherNewSet = otherNewSet.adjoin(e);
}
}
}
return otherNewSet;
}
#Override
public Set<E> subtract(Set<E> that) {
LinkedSet<E> newSet = null;
Set<E> otherNewSet = newSet;
for(E e : that){
if(!this.contains(e)){
if(otherNewSet == null){
LinkedNode<E> newNode = new LinkedNode<E>(e, null);
otherNewSet = new LinkedSet<E>(newNode);
}
else{
otherNewSet = otherNewSet.adjoin(e);
}
}
}
return otherNewSet;
}
#Override
public Set<E> remove(E e) {
LinkedSet<E> newSet = null;
Set<E> otherNewSet = newSet;
if(!this.contains(e))
return this;
else{
for(E t : this){
if(t != e){
if(otherNewSet == null){
LinkedNode<E> newNode = new LinkedNode<E>(e, null);
otherNewSet = new LinkedSet<E>(newNode);
}
}
else{
otherNewSet = otherNewSet.adjoin(e);
}
}
}
return otherNewSet;
}
#Override
#SuppressWarnings("unchecked")
public boolean equals(Object o) {
if (! (o instanceof Set)) {
return false;
}
Set<E> that = (Set<E>)o;
return this.isSubset(that) && that.isSubset(this);
}
#Override
public int hashCode() {
int result = 0;
for (E e : this) {
result += e.hashCode();
}
return result;
}
}
The problem isn't your iterator, it's your adjoin method.
When you construct a new LinkedSet like this:
set1 = new LinkedSet<String>();
It means that you call this constructor:
// Constructors
public LinkedSet() {
}
But that constructor doesn't assign anything to head, so it has its default initial value:
private LinkedNode<E> head = null;
Since head starts off as null, and isn't ever assigned to, this method never does anything but return this:
#Override
public Set<E> adjoin(E e) {
boolean alwaysEqual = true;
if (this.head == null)
return this;
for (E t : this) {
if (t != e)
alwaysEqual = false;
}
if (alwaysEqual == true)
return this;
LinkedNode<E> temp = this.head;
LinkedNode<E> newNode = new LinkedNode<E>(e, temp);
LinkedSet<E> newSet = new LinkedSet<E>(newNode);
Set<E> otherSet = newSet;
return otherSet;
}
So your iterator isn't iterating over anything, because your set has nothing in it.
The code guarantees a NullPointerException if curr is null.
if(curr.getNext() == null || curr == null)
Execution is left to right. Test the reference for null before you attempt to dereference it.

Use a specific object-property to index, using a Map structure

I'd like to use the integer-id-value of my vertex-class as an index, in my Map-structure, without using a parallel data structure.
class Vertex {
private int v;
private int label;
//...
}
I store vertex objects, in a Map<Vertex,ArrayList<Edge>> adjMap.
Is it possible use the v-property of my Vertex class, as an index-key in my Map?
It depends on the Map implementation you are using. For example, for a HashMap you can override equals and hashCode of your Vertex class so that two vertices will be considered equal if their v properties are equal.
class Vertex {
private int v;
private int label;
public Vertex (int v)
{
this.v = v;
}
#Override
public boolean equals (Object o)
{
if (!(o instanceof Vertex))
return false;
Vertex ov = (Vertex)o;
return this.v == ov.v;
}
#Override
public int hashCode ()
{
return v;
}
}
Now, to locate the value for a given v value in your Map :
adjMap.containKey(new Vertex(v));
Ok. Then your equals and hashcode should only use property v. Code below:
public class Vertex {
private int v;
private int label;
public static void main(String[] args) {
Map<Vertex, String> map = new HashMap<Vertex, String>();
Vertex vertex = new Vertex();
vertex.v = 5;
vertex.label = 10;
map.put(vertex, "vertex");
Vertex vertex2 = new Vertex();
vertex2.v = 5;
vertex2.label = 100;
System.out.println("Value:: "+ map.get(vertex2));
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + v;
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Vertex other = (Vertex) obj;
if (v != other.v)
return false;
return true;
}
}
Output: Value:: vertex

Find the maximum ratio between the number of nodes in a binary tree

I wrote a class that defines some operations on binary trees.
Now I have to add a method that returns a reference to the node (or to one of the nodes, if there are more than one) U such that the ratio between the number of nodes in the subtree of the root U (thus including the node itself) and height (+1) of the same is maximized.
So I have to add to the sum the value 1 for the leaves otherwise the ratio would be 1/0 = infinity: that is, the result would be any leaf (wrong).
The algorithm must be linear in the number of nodes, visiting the tree once.
Can I define a private class auxiliary in case I need it.
I do not know how to do, someone help me?
My code is this.
public class BinaryTree {
protected class Node {
protected Integer element;
protected Node left;
protected Node right;
Node(int element) {
this.element = element;
left = right = null;
}
Node(int element, Node left, Node right) {
this.element = element;
this.left = left;
this.right = right;
}
boolean isLeaf() {
return left == null && right == null;
}
} //end Node class
public class NodeReference {
private Node node;
private NodeReference(Node node) {
this.node = node;
}
public int getElement() {
return node.element;
}
public void setElement(int e) {
node.element = e;
}
} //end class NodeReference
protected Node root;
public BinaryTree() {
root = null;
}
public boolean isEmpty() {
return root == null;
}
public void add(int element, String path) {
//working properly
}
protected Node add(int elem, String path, Node node) {
//working properly
}
public void printPreOrder() {
//working properly
}
protected void printPreOrder(Node node) {
//working properly
}
public int height() {
//working properly
}
protected int height(Node node) {
//working properly
}
public int sum() {
//working properly
}
private int sum(Node node) {
//working properly
}
public int size() {
//working properly
}
private int size(Node node) {
//working properly
}
public boolean search(int x) {
//working properly
}
protected boolean search(int x, Node node) {
//working properly
}
public boolean equalTo(BinaryTree t) {
//working properly
}
public boolean equals(Object ob) {
//working properly
}
protected boolean areEqual(Node node1, Node node2) {
//working properly
}
public BinaryTree copy() {
//working properly
}
protected Node copy(Node node) {
//working properly
}
public NodeReference find(int x) {
//working properly
}
private Node find(int x, Node nd) {
//working properly
}
public boolean isCompletelyBalanced() {
//working properly
}
private int isCompletelyBalanced(Node node) {
//working properly
}
public boolean is1Balanced() {
//working properly
}
private int is1Balanced(Node node) {
//working properly
}
private class BoolNode {
boolean found;
Node node;
BoolNode(boolean found, Node node) {
this.found = found;
this.node = node;
}
}
public boolean removeSubtree(int x) {
//working properly
}
protected BoolNode removeSubtree(int x, Node node) {
//working properly
}
public int maxElem() throws IllegalStateException {
if(root == null)
throw new IllegalStateException("Empty tree.");
return maxElem(root);
}
private static int max3(int x, int y, int z) {
return max(x, max(y, z));
}
private int maxElem(Node node) {
int max = node.element;
if(node.left != null)
max = Math.max(max, maxElem(node.left));
if(node.right != null)
max = Math.max(max, maxElem(node.right));
return max;
}
public NodeReference maxDescendantsHeightRatio() {
//As I write this method?
}
}
I started doing it this way:
public NodeReference maxDescendantsHeightRatio() {
ArrayList<Node> list = iteratorPreOrder();
ArrayList<NodeWithRatio> listRatio = new ArrayList<NodeWithRatio>();
for(int i = 0; i < list.size(); i++) {
int s = size();
int h = height() + 1;
int r = ratioScore(s, h);
listRatio.add(new NodeWithRatio(this, r));
}
//sort the array list
Collections.sort(listRatio, new Comparator<Point>() {
public int compare(NodeWithRatio o1, NodeWithRatio o2) {
return Integer.compare(o1.ratio, o2.ratio);
}
});
//find max value in the list of node with ratio
NodeWithRatio result = listRatio.get(listRatio.size() - 1); //gets the last item, largest for an ascending sort
return result.node;
//return null;
}
private int ratioScore(int size, int height) {
return size / height;
}
private class NodeWithRatio {
Node node;
int ratio;
public NodeWithRatio(Node n, int r) {
node = n;
ratio = r;
}
} //end NodeWithRatio class
public ArrayList<Node> iteratorPreOrder() {
ArrayList<Node> templist = new ArrayList<Node>();
preorder(root, templist);
for(int i = 0; i < templist.size(); i++)
System.out.println(templist.get(i).element);
return templist;
}
private void preorder(Node node, ArrayList<Node> templist) {
if(node != null) {
templist.add(node); // adds to end of list.
preorder(node.left, templist);
preorder(node.right, templist);
}
}
public int height() {
if(isEmpty())
return -1;
return height(root);
}
protected int height(Node node) {
return (node == null)? -1: 1 + Math.max(height(node.left), height(node.right));
}
public int size() {
if(isEmpty())
return 0;
return size(root);
}
private int size(Node node) {
if(node == null)
return 0;
return size(node.left) + size(node.right) + 1;
}
I think it's the wrong piece of code:
for(int i = 0; i < list.size(); i++) {
int s = size();
int h = height() + 1;
int r = ratioScore(s, h);
listRatio.add(new NodeWithRatio(this, r));
}
Since it is not recursive, but I do not know how to fix it ...
Does anyone have any advice?
Thanks!
As you figured out, the calculation of the ratio is a combination of the calculation of the size and height of the tree.
In order to find the max ratio in one pass over the nodes of the tree, we can use a recursive method that would calculate both the size and height of the tree. In order to calculate the ratio for a node, it's not enough to know the ratios of its two children - we need to know the size and height of the children's sub trees. For this purpose I'll return an array of two ints - the first is the size and the second the height.
public int[] ratio(Node node)
{
int[] result = new int[2];
int[0] = 0;
int[1] = 0;
if (node = null)
return result;
int[] leftRatio = ratio(node.left);
int[] rightRatio = ratio(node.right);
result[0] = leftRatio[0] + rightRatio[0] + 1; // current sub tree size
result[1] = Math.max(leftRatio[1] + rightRatio[1]) + 1; // current sub tree height
return result;
}
Now, if you want to find all the nodes with the highest ratio, you can add a static variable holding the current max ratio and another static variable holding the nodes having that max ratio (I know, it's ugly, but it works).
...
static float maxRatio = 0;
static Set<Node> maxRatioNodes = new Set<Node>();
...
public int[] ratio(Node node)
{
int[] result = new int[2];
int[0] = 0;
int[1] = 0;
if (node = null)
return result;
int[] leftRatio = ratio(node.left);
int[] rightRatio = ratio(node.right);
result[0] = leftRatio[0] + rightRatio[0] + 1; // current sub tree size
result[1] = Math.max(leftRatio[1] + rightRatio[1]) + 1; // current sub tree height
float currRatio = result[0]/result[1];
if (currRatio > maxRatio) { // found a new max ratio
maxRatio = currRatio;
maxRatioNodes.clear();
maxRatioNodes.add (node);
} else if (currRatio == maxRatio) { // node's ratio equals the current max ratio
maxRatioNodes.add (node);
}
return result;
}

How To Create A Java Graph File from .txt file

I am trying to create a graph file .I have to read in values from a .gra file(which I think is a .txt file).We were told to tokenise lines based on a space in the format <vertex> <name> <x-coord> <y-coord>,same for edge
I had a look at a couple of related questions,but still cant find the answer.
Here's the code I was given:
public EdgeListVertex(V element) {
this.element = element;
}
#Override
public V element() {
return element;
}
public String toString() {
return element.toString();
}
}
private class EdgeListEdge implements Edge<E> {
Position<EdgeListEdge> position;
E element;
EdgeListVertex start, end;
public EdgeListEdge(EdgeListVertex start, EdgeListVertex end, E element) {
this.start = start;
this.end = end;
this.element = element;
}
#Override
public E element() {
return element;
}
public String toString() {
return element.toString();
}
}
private List<EdgeListVertex> vertices;
private List<EdgeListEdge> edges;
public EdgeListGraph() {
vertices = new LinkedList<EdgeListVertex>();
edges = new LinkedList<EdgeListEdge>();
}
#SuppressWarnings("unchecked")
#Override
public Vertex<V>[] endVertices(Edge<E> e) {
EdgeListEdge edge = (EdgeListEdge) e;
Vertex<V>[] endpoints = (Vertex<V>[]) new Vertex[2];
endpoints[0] = edge.start;
endpoints[1] = edge.end;
return endpoints;
}
#Override
public Vertex<V> opposite(Vertex<V> v, Edge<E> e) {
Vertex<V>[] endpoints = endVertices(e);
if (endpoints[0].equals(v)) {
return endpoints[1];
} else if (endpoints[1].equals(v)) {
return endpoints[0];
}
throw new InvalidVertexException();
}
#Override
public boolean areAdjacent(Vertex<V> v, Vertex<V> w) {
for (EdgeListEdge edge: edges) {
if ((edge.start.equals(v)) && (edge.end.equals(w))) return true;
if ((edge.end.equals(v)) && (edge.start.equals(w))) return true;
}
return false;
}
#Override
public V replace(Vertex<V> v, V x) {
EdgeListVertex vertex = (EdgeListVertex) v;
V temp = vertex.element;
vertex.element = x;
return temp;
}
#Override
public E replace(Edge<E> e, E x) {
EdgeListEdge edge = (EdgeListEdge) e;
E temp = edge.element;
edge.element = x;
return temp;
}
#Override
public Vertex<V> insertVertex(V v) {
EdgeListVertex vertex = new EdgeListVertex(v);
Position<EdgeListVertex> position = vertices.insertLast(vertex);
vertex.position = position;
return vertex;
}
#Override
public Edge<E> insertEdge(Vertex<V> v, Vertex<V> w, E o) {
EdgeListEdge edge = new EdgeListEdge((EdgeListVertex) v, (EdgeListVertex) w, o);
Position<EdgeListEdge> position = edges.insertLast(edge);
edge.position = position;
return edge;
}
#Override
public V removeVertex(Vertex<V> v) {
Iterator<Edge<E>> it = incidentEdges(v).iterator();
while (it.hasNext()) it.remove();
EdgeListVertex vertex = (EdgeListVertex) v;
vertices.remove(vertex.position);
return vertex.element;
}
#Override
public E removeEdge(Edge<E> e) {
EdgeListEdge edge = (EdgeListEdge) e;
edges.remove(edge.position);
return edge.element;
}
#Override
public List<Edge<E>> incidentEdges(Vertex<V> v) {
LinkedList<Edge<E>> list = new LinkedList<Edge<E>>();
for (EdgeListEdge edge : edges) {
if (edge.start.equals(v)) list.insertLast(edge);
if (edge.end.equals(v)) list.insertLast(edge);
}
return list;
}
#Override
public List<Vertex<V>> vertices() {
LinkedList<Vertex<V>> list = new LinkedList<Vertex<V>>();
for (EdgeListVertex vertex : vertices) {
list.insertLast(vertex);
}
return list;
}
#Override
public List<Edge<E>> edges() {
LinkedList<Edge<E>> list = new LinkedList<Edge<E>>();
for (EdgeListEdge edge : edges) {
list.insertLast(edge);
}
return list;
}
}
Any tips?
A good approach might be to create a new class with fields for vertex, name and coordinates. Then read in the data from a scanner into an ArrayList of the class you just created. After this, you could just pass the ArrayList into whatever you are using for graphing (which might require a few tweaks).
Example scanner code (untested):
File graphFile = new File(filepath);
Scanner graphScanner = new Scanner(graphFile);
Then, to read from the scanner (untested):
ArrayList<graphClass> dataPoints = new ArrayList<graphClass>(); //you will have to make graphClass
while(graphScanner.hasNextLine()) {
dataPoints.add(new graphClass(graphScanner.nextDouble(), graphScanner.next(), graphScanner.nextDouble(), graphScanner.nextDouble()); //assuming constructor of graphClass takes vertex, name, x, y in that order
I hope this helps.

Categories