i would appreciate very much if someone can help me with this one. i coded four classes. one main class, one for nodes, one for connections, and one for groups first of all 20 nodes are created. for each of them, a random number of tags (letters) between 1 and 3 is created then connections between nodes are created each time one of them shares a tag. now i would like to identify groups of nodes that are not connected neither directlly or indirectly with other groups of nodes... here's the problem I create the first group with the two nodes of the first connection in the array. Then i go through all the connections generated and try to see if the nodes belongs to the first group. if not a new group is created. my problem is that all the groups created have the elements of the first group instance. some scope or reference problem, i can't figure it out :S i now the code is quite extense but here it is... thx in advance
class Main { var nodes, connections, groups; var nr_nodes; var tagsArray = new Array("A", "B", "C", "D", "E", "F", "G", "H","I","J","K","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"); var nrconnections=1; var nrgroups=1; function Main() { nr_nodes=20; this.init(); } function init() { nodes = new Array(); connections = new Array(); groups = new Array(); this.create_initial_nodes_and_connections(); } /*Create initial nodes ans connections*/ function create_initial_nodes_and_connections() { for (var j=1;j<=nr_nodes;j++) { var random_nr_tags = random(3)+1; this["node_"+j]= new Node(); this.generateTags(random_nr_tags,j); this.insertNode(this["node_"+j]); } this.generate_connections(); } function generateTags(n,no) { for (var i=1;i<=n;i++) { var random_tag = random(25)+1; this["node_"+no].setTag(tagsArray[random_tag],i); } } function generate_connections() { for (var i = 1; i <= nodes.length; ++i) { for (var j = i + 1; j <= nodes.length; j++) { this.compareTags(i,j); } } this.init_groups(); } /* Create first group with the nodes of the first connection in the connections array */ function init_groups() { this.insertGroup(this["group_1"]); this["group_"+nrgroups]= new Group(this["Connection_1"].node1, this["Connection_1"].node2); nrgroups++; this.identify_groups(); } function identify_groups() { for (var i = 2; i <= connections.length; i++) { var no1_tmp = this["Connection_"+i].node1; var no2_tmp = this["Connection_"+i].node2; trace("id" + no1_tmp.myID); for (var j=1; j<= groups.length; j++) { trace("checking group" + j) var k= this["group_"+j].checkIfBelongsToGroup(no1_tmp.myID,no2_tmp.myID); trace("result k= " + k) // 1 - Belongs, but both nodes already created // 2 - Doesn't belongs and create new group // 3 - Belongs and add one of the nodes switch(k) { case true: break; case false: this.insertGroup(this["group_"+nrgroups]); this["group_"+nrgroups]= new Group(this["Connection_"+i].node1, this["Connection_"+i].node2); trace("creating group " + nrgroups); nrgroups++; break; default: this["group_"+j].insertInGroup(k); trace ("insert in group" + j); break; } } } } /* Compare tags between two nodes and create connection */ function compareTags(node1,node2) { for (var i=1; i< this["node_"+node1].generatedTags.length;i++) { var tag_actual = this["node_"+node1].generatedTags[i]; for (var j=1; j < this["node_"+node2].generatedTags.length;j++) { if (this["node_"+node2].generatedTags[j] == tag_actual) { this["Connection_"+nrconnections] = new Connection(this["node_"+node1], this["node_"+node2]); this.insertConnection(this["Connection_"+nrconnections]); nrconnections++; } } } } function insertConnection(Connection) { connections.push(Connection); } function insertNode(no) { nodes.push(no); } function insertGroup(group) { groups.push(group); } } //////////////////////////////////////////////////////////////////////////////////////////////// class Node { var myID; var generatedTags = new Array(); static var id = 1; function Node() { myID = id = ++Node.id; generatedTags = []; } function setTag(val,position) { generatedTags[position] = val; } } //////////////////////////////////////////////////////////////////////////////////////////////// class Connection { var myID,node1, node2; static var id = 1; function Connection(n1, n2) { myID = id = ++Connection.id; node1=n1; node2=n2; } } //////////////////////////////////////////////////////////////////////////////////////////////// class Group { var groupNodes = new Array(); var myID; static var id = 1; function Group(node1, node2) { myID = id = ++Grupo.id; var n1=node1; var n2=node2; this.insertInGroup(n1.myID); this.insertInGroup(n2.myID); } function checkIfBelongsToGroup(node1,node2) { var n1=node1; var n2=node2; trace("node1=" + n1); trace("node2=" + n2); for(var i=0;i<groupNodes.length;i++) { if(n1==groupNodes[i]) { for (var j=0;j<= groupNodes.length;j++) { if(n2==groupNodes[j]) { return true; } } return n2; } else if(n2==groupNodes[i]) { for (var k=0;k< groupNodes.length;k++) { if(n1==groupNodes[k]) { return true; } } return n1; } } return false; } private function insertInGroup(node) { groupNodes.push(node); } } //////////////////////////////////////////////////// _______________________________________________ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders