3

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!

c0der
  • 18,467
  • 6
  • 33
  • 65
TIGUZI
  • 231
  • 1
  • 3
  • 12
  • Do you want to print the outgoing arcs of the node with highest degree or the in and outgoing arcs ? – b.GHILAS Nov 29 '19 at 08:54
  • @billalGHILAS Top 10 highest in-degree – TIGUZI Nov 29 '19 at 19:37
  • 1
    1. You need to post [mre]. Are you able to read the file and print the data ? If so make your question mre by hard coding some test data instead of reading a file. If you can't read the file, focus you question on reading the file only removing the rest of the code. 2. There is no reason for `Graph` to extend `Scanfile`. 3. There is no reason to make `Graph` and `Edge` static. – c0der Nov 30 '19 at 07:15
  • Reading the file into a HashMap with integer key for the node number and list values for the edge endpoints. That results in an adjacency list representation for the graph and sets you up in a better position for the task at hand. –  Nov 30 '19 at 09:13

0 Answers0