First I have a txt file that contains thousands pairs of nodes and it looks like this: 1 3
1 5
1 8
1 7
2 3
2 4
2 5
2 6
3 2
3 4....
Every pair of node can be one edge, so I was using scanner to read them and and put every pair of nodes into a linked-list. Now I want to find the nodes(like top 10 or top 20 of them) with high degree and at the same time I want to output their followers as well. So I tried code as following:
import java.io.*;
import java.util.*;
import java.util.Arrays;
class Main {
public static class Edge{
int from;
int to;
public Edge(int from, int to){
this.from = from;
this.to = to;
}
public String toString(){
return from+"->"+to;
}
}
public static class Scanfile{
public Scanner x;
public void openFile(){
try{
x = new Scanner(new File("face.txt"));
}
catch(Exception e){
System.out.println("could not open file");
}
}
public void readFile(){
LinkedList<Edge> adj = new LinkedList<Edge>();
while(x.hasNext()){
String a = x.next();
int resa = Integer.parseInt(a);
String b = x.next();
int resb = Integer.parseInt(b);
Edge edge = new Main.Edge(resa, resb);
adj.add(edge);
// System.out.println(edge);
}
//System.out.println(adj);
}
public void closeFile(){
x.close();
}
}
public static class Graph extends Scanfile{
private int V;
private LinkedList<Integer> degree[];
Scanfile u = new Scanfile();
Graph(int resa){
V=resa;
degree = new LinkedList(resa);
for(int i=0;i<resa;i++)
degree[i] = new LinkedList();
}
public void DFSUtil(int resa,boolean visited[]) {
// Mark the current node as visited and print it
visited[resa] = true;
System.out.print(resa+" ");
// Recur for all the vertices adjacent to this vertex
Iterator<Integer> i = degree[resa].listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
DFSUtil(n, visited);
}
}
public void DFS(){
boolean visited[]=new boolean[V];
for(int i=0;i<V;i++)
if(visited[i]==false)
DFSUtil(i, visited);
}
}
public static void main(String[] args) {
Scanfile s = new Scanfile();
s.openFile();
s.readFile();
s.closeFile();
Graph g=new Graph();
g.DFS();
}
}
The code is never gonna work and the error messages are: 1.Can't find symbol resa 2.Incompatible types: int cannot be converted to Collection:degree = new LinkedList(resa) 3.Constructor Graph in class Graph cannot be applied to given types;Graph g=new Graph()
I think the way that I passing the variables were wrong(Is there a way to pass the whole linked-list as varible?). I'm a real fresh man in java field. I would appreciate for any suggestions. Hope someone can help me!