algorithm - Creating node with weight in java graph -


i creating program create graph bibliography dataset. graph directed, has author node , paper node, , has 2 types of edges (author paper edge, paper paper edge).

i want input whether or not creating making sense. right now, produces right result when want outedge , inedge , node. im not sure if implementation correct in terms of methods, designs, , algorithm.

also, have problem assigning weight node. want ask how can well. right now, have tried follows:

for (string item : candidateauthortype1unique) {     double weight = collections.frequency(candidateauthortype1, item);     n.setweight(item,weight);;     system.out.println(n.getname() + " : " + n.getweight()); } 

however, after using setweight, getname() method returns null. means weight assigned not assigned item. wonder how update weight item.

if use

for (string item : candidateauthortype1unique) {     double weight = collections.frequency(candidateauthortype1, item);     n = new node(item,weight);     system.out.println(n.getname() + " : " + n.getweight()); }    

does mean each time new node n created, old n node not stored? how can checked every node ever created , weight?

i ask input program. input helpful me. thank you.

main class: ranker.java

import java.util.*; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.lang.reflect.field; import java.sql.*;  public class ranker {      static graph g;     static node n;     static edge e;     static hashmap nodemap;   // maps integer nodeids node object     string id;     double weight;      ranker() {             g = new graph();             nodemap = new hashmap();             n = new node(id,weight);     }      public static void main (string[] args) throws classnotfoundexception, sqlexception, ioexception, illegalargumentexception, illegalaccessexception{          ranker ranker = new ranker();         connection connect = null;         preparedstatement preparedstatement = null;         resultset resultset = null;         hashmap nodemap =  new hashmap();   // maps integer nodeids node objects          class.forname("com.mysql.jdbc.driver");         connect = drivermanager.getconnection("jdbc:mysql://localhost/arnetminer?"+"user=root&password=1234");         preparedstatement = connect.preparestatement("select fr,t,ty subedge");          resultset = preparedstatement.executequery();         int i=0;          while(resultset.next()) {            g.addedgeforindexing(resultset.getint(1),resultset.getint(2),resultset.getint(3));            i++;                         system.out.println( "edges added g = "+i);         }                 system.out.println("loaded " + g.nodecount() + " nodes.");          buildnodes();          system.out.println("enter first author key:");         bufferedreader br = new bufferedreader(new inputstreamreader(system.in));         string authorkey1 = br.readline();          int authorid1 = integer.parseint(authorkey1);         string authorid1 = "a"+authorid1;         arraylist<string> p1 = g.getoutedgestop(authorid1);         system.out.println("p1 = " + p1);          arraylist<string> p2 = new arraylist<string>();         (int j = 0; j<p1.size();j++){             arraylist<string> temp = g.getoutedgestop(p1.get(j));             if (temp!=null)             p2.addall(temp);         }         system.out.println("p2 = " +p2);          arraylist<string> candidateauthortype1 = new arraylist<string>();         (int k = 0; k<p2.size(); k++){             arraylist<string> temp = g.getinedgesfromptoa(p2.get(k));             if(temp!=null)                 candidateauthortype1.addall(temp);         }         system.out.println("candidate author type 1= " + candidateauthortype1);          arraylist<string> candidateauthortype1unique = removeduplicates(candidateauthortype1);         system.out.println("-----------------------------------------------");         system.out.println("candidate author type 1 , author node weight:");         (string item : candidateauthortype1unique) {             double weight = collections.frequency(candidateauthortype1, item);             n.setweight(item,weight);;             system.out.println(n.getname() + " : " + n.getweight());         }                 arraylist<string> candidatepapertype1 = new arraylist<string>();         (int l = 0; l<candidateauthortype1.size(); l++){             arraylist<string> temp = g.getoutedgestop(candidateauthortype1.get(l));             if(temp!=null)                 candidatepapertype1.addall(temp);         }         system.out.println("candidate paper type 1= " + candidatepapertype1);     }          private static arraylist<string> removeduplicates(arraylist<string> element){         arraylist<string> result = new arraylist<>();         hashset<string> set = new hashset<>();         (string item : element) {             if (!set.contains(item)) {             result.add(item);             set.add(item);             }         }         return result;       }      private static void buildnodes()      {         string nodeid;         double weight = 0;         node n;         iterator = g.nodeiteratorinitial();         while (it.hasnext()) {           nodeid = (string) it.next();            if (!nodemap.containskey(nodeid)){               n = new node(nodeid,weight);               nodemap.put(nodeid, 0);           }         }     } } 

graph.java

import java.lang.reflect.field; import java.util.*;  public class graph {      private hashset<string>  nodeids;        public hashmap<integer, string> nodeidswithtn;     public hashmap<integer, string> tnmap;       private hashmap<string, arraylist<string>>  edges;     private hashmap<string, arraylist<string>>  reverse;     private int numnodes;     private int numedges;     private int numreverse;      public graph() {         edges = new hashmap<string, arraylist<string>>();         reverse = new hashmap<string, arraylist<string>>();         nodeids = new hashset<string>();         nodeidswithtn = new hashmap<integer, string>();         tnmap = new hashmap<integer, string>();         new hashset();     }      public void addedgeforindexing(int from, int to, int t) throws illegalargumentexception, illegalaccessexception {         string = ""+from;         string = ""+to;         int type = t;          if(t==1)         {             from="a"+from;             to="p"+to;         }         else if(t==2)         {             from="p"+from;             to="p"+to;         }                    else             system.out.println("t ="+t+" value undefined");              edge e = new edge(from,to,type);              nodeids.add(e.from);             nodeids.add(e.to);              arraylist<string> tmp = null;             if (edges.containskey(e.from))               tmp = (arraylist<string>) edges.get(e.from);             else {               tmp = new arraylist<string>();               edges.put(e.from,tmp);             }             tmp.add(e.to);              arraylist<string> tmp2 = null;             if (reverse.containskey(e.to))               tmp2 = (arraylist<string>) reverse.get(e.to);             else {               tmp2 = new arraylist<string>();               reverse.put(e.to,tmp2);             }             tmp2.add(e.from);     }      public int nodecount() {         if(nodeids.size() > 0)              return nodeids.size();             // else return numnodes;         return numedges;     }      public int countinedges(integer key) {         if (!reverse.containskey(key)) return 0;             return ((arraylist<?>) reverse.get(key)).size();     }      public int countoutedges(integer key) {         if (!edges.containskey(key)) return 0;             return ((arraylist<?>) edges.get(key)).size();     }      public arraylist<string> getinedgesfromptoa(string id) {         if (!reverse.containskey(id)) return null;               arraylist<string> = reverse.get(id);             arraylist<string> result = new arraylist<string>();                 for(int j=0;j<a.size();j++){                       if(a.get(j).startswith("a")){                           result.add(a.get(j));                                                  }                  }         return result;     }      public arraylist<string> getoutedgestop(string id) {         if (!edges.containskey(id)) return null;                 arraylist<string> = edges.get(id);             arraylist<string> result = new arraylist<string>();             for(int j=0;j<a.size();j++){               if(a.get(j).startswith("p")){               result.add(a.get(j));                                          }             }         return result;     }      public iterator<string> nodeiteratorinitial() {         return nodeids.iterator();     } } 

edge.java

public class edge {       string from;     string to;     int type;     private static int counter = 0;     public edge(string from, string to, int type) {         this.from = new string(from);         this.to = new string(to);         this.type = type;     //  system.out.println("edges added " + + " " +  + " type "+ type);     }      public string getfrom(){         return from;     }      public string getto(){         return to;     }      public int gettype(){         return type;     }      public void setfrom(string from){         this.from = from;     }      public void setto(string to){         this.to = to;     }      public void settype(int type){         this.type = type;     } } 

node.java

public class node {        string id;       double weight;       private static int counter = 0;        public node(string id,double weight) {           this.id = id;           this.weight = weight;;        }        public double getweight(){             return weight;       }        public string getname() {            return id;       }        public void setweight(string id, double weight){          if (this.id==id){           this.weight=weight;}           system.out.println("the node " + id + " has weight " + weight);       }         public void setname(string id){           this.id=id;       }  } 

as initialising n in ranker() constructor, when constructor called, string id has not been assigned , therefore contains value null. therefore node n gets id null. reason why weight isn't updated in setweight(string id, double weight) function, new id compared null returns false therefore weight doesn't updated.

you make following changes in code

1) remove n = new node(id,weight) initialisation in ranker() constructor.

2) add following lines instead of n.setweight(item,weight) in main method in ranker class.

if (n == null)     n = new node(item, weight); n.setweight(item, weight); 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -