Hey, I'm trying to create a graph which will later be used to make some sort of pagerank program, but at the moment I'm stuck on the basics.
"write a class called Graph that encapsulates
a suitable graph data structure and methods for operating on it (given that the number of nodes
and edges will potentially be very large). Include methods in your class which load and storegraphs to and from an ASCII text file."
I have decided to create an array list with all the nodes, then add a link by adding an arraylist to that particular node with the names of the other nodes it links to,i.e 1:2,3
2:1
3:4,1
4:2,3,1
But i dont know how to add an array list to the node?
this is my code so far:
import java.io.*;
import java.util.*;
public class pagerank {
public ArrayList<String> biggraph = new ArrayList<String>();
//public LinkedList<String>[] links;
public static void main(String[] args) {
new pagerank().read("pagerank.txt");
}
public void read(String filename) {
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(filename));
String nextline = new String("");
String currentline = null;
while ((currentline = bufferedReader.readLine()) != null) {
nextline = currentline;
String[] part = new String[1];
part = nextline.split(" ");
graph(part[0], part[1]);
//System.out.println(part[0]);
//System.out.println(part[1]);
}
bufferedReader.close();
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}
public void graph(String from, String into){
addVertex(from);
addVertex(into);
addLink(from, into);
//checks to see if from exists, if not add
//checks to see if into exists, if not add
//checks to see if edge exists within from's linked list, if not add
}
public void addVertex(String name){
//if biggraph does not contain name, add to big graph, create linked list for it
//Boolean found = false;
Iterator<String> iter = this.biggraph.iterator();
String currItem = "";
while ( iter.hasNext() == true ) {
currItem = iter.next();
if (currItem == name) {
return;
}
}
biggraph.add(name);
return;
}
public void addLink(String from, String into){
//search for from in biggraph, add element to its linked list
Iterator<String> iter = this.biggraph.iterator();
String currItem = "";
int i = 0;
while ( iter.hasNext() == true ) {
currItem = iter.next();
i++;
if (currItem == from) {
/*PROBLEM HERE! LINE BELOW!*/
biggraph[i] = new ArrayList<String>();
}