We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Social Networks Library
Page Index Toggle Pages: 1
Social Networks Library (Read 393 times)
Social Networks Library
Jun 25th, 2008, 4:07pm
 
I modified Todd Holloway's Social Networks example(http://ella.slis.indiana.edu/~tohollow/SocialNetworksLibrary/) to read a 2-column, tab delimited txt file into a 2D array (connections[][]).  In one column is a player's name, the second column is the team name.  Each player can have more than one team, each team has multiple players.  When I draw these as nodes, I only want one instance of a player or team node.

I loop through the conections[][] (player and team), adding uniques to a set (which later becomes my unique node source).  I couldn't figure out how to create a node and then refer back to it later when I wanted to connect it to another node, so I created another 2D array of labels (the values from my unique node set) and the actual node itself (nodeLabels[][]).

Finally, I loop through connections[player][team], and match it to nodeLabels[player or team][node], returning the corresponding node.  I have to find the starting node and finishing node using this method before adding the connecting line.

The problem is that this method is too slow when using text files larger than 20 lines.  I need a way to create unique nodes, identify them later, and deal with much larger text files.  

Ultimately, I want the thickness of the line connecting the nodes to correlate to a third column in the original text file (batting average).  I have not even bothered implementing this yet, but thought I should mention in case it changes the direction I need to go.  I could probably identify additional columns/attributes to add as well.
Re: Social Networks Library
Reply #1 - Jun 25th, 2008, 4:08pm
 
void setup() {
 size(horSS,vertSS);
 String[] lines;
 lines = loadStrings("Generic File.txt");
 Node[][] aa = new Node[lines.length][];
 String[][] zz = new String[lines.length][];
 String[][] nodeLabel = new String[lines.length][];
 int i = 0, j = 0;
 String[][] connections = new String[lines.length][2];
 
 // read each line from the text file
 for (i = 0; i < lines.length; i++) {
   connections[i] = split(lines[i], '\t');
     zz[i] = new String[lines.length];    
 }


//Create an Array of unique Labels and their associated nodes
for (int q = 0; q < lines.length; q++ ) {
  nodeLabel[q] = new String[lines.length];
  for (int qq = 0; qq < 2; qq++ ) {
    set.add(connections[q][qq]);  
  }
}
 
 // create a 2d array of labels and nodes
 Iterator it = set.iterator();
   i = 0;
   while (it.hasNext()) {
     aa[i] = new Node[lines.length];
     Object element = it.next();
     nodeLabel[i][0] = (element.toString());
     aa[i][0] = new Node(this, nodeLabel[i][0]);
     net.addNode(aa[i][0]);
     i++;
   }
 
 
 //loop through my connections, look for the matching
 //node label and return the corresponding node for that label
 for (i = 1; i < lines.length; i++) {        
   for (j = 0; j < lines.length; j++) {
       //FIND FIRST NODE
       if (connections[i][0].equals (nodeLabel[j][0])) {
         node1 = j;
       }
        //FIND SECOND NODE
       if (connections[i][1].equals (nodeLabel[j][0])) {
         node2 = j;
       }
   //connect the two nodes based on the looping logic above
   net.addEdge(new Edge(this, aa[node1][0], aa[node2][0]));    
   }
 }
}
Page Index Toggle Pages: 1