Randomly Generated Network

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2007
Posts: 62
Reputation: minigweek is on a distinguished road 
Solved Threads: 4
minigweek's Avatar
minigweek minigweek is offline Offline
Junior Poster in Training

Randomly Generated Network

 
0
  #1
May 24th, 2007
Hi Everyone, Good day to you all.
I am solving a problem : to Generate a Random Network. I hv divided the problem into two parts. First generate a Graph, then from the graph from a network.
Here the code generates a random graph.
User Gives n ->no. of nodes , and the prog. generates the graph with n nodes.
Constraints:
1. No Self Loop
2. Each node must have atleast 3 edges
3. Each node can have at max 7 edges.
-------------------------------------------------
Here is what the problem is. I have used int in all, as a result the program runs fine for an input ( n ) < 32768 . But I would like the program to run for input of an even larger number. For that i used the Replace all function of my IDE ( code::blocks) and replaced all int with long . Except the MAIN of course.
No Luck. Still the program goes into infinte loop for n>32768.
I Know why that happens, int resets to n when input is 32768+n .
So changing it to Long should have solved the problem. I thought so.
---------------------------------------------------
Now what the program does :
Struct adj -> The Adjacency List. Adj shall store the node number, and the number of edges node n has, and the address of next nodes.
struct nodes ->Stores the node number and the edge length (from n) and the address of next nodes.
edc : Global , tracks how many nodes has atleast 3 edges.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Function :
randomed : Generates Random no. based on lower bound a, upper bound b ( for the edge length );
checknd : checks whether a node exists in the list, if not adds it, else discards it.
disp : outputs the adjacency list to a file.
-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-

  1. #include<iostream>
  2. #include<ctime>
  3. #include<fstream>
  4. using namespace std;
  5. struct nodes;
  6. struct adj
  7. {
  8. int ndnm;
  9. int nmed;
  10. nodes *next;
  11. };
  12. struct nodes
  13. {
  14. int ndnm;
  15. int edln;
  16. nodes *next;
  17. };
  18. int edc=0; // counts no of nodes hving atleast 3 edges
  19. int randomed(int a,int b);
  20. void checknd(nodes *t,adj *list,int u, int v, int edlength);
  21. void disp(adj *list,int n);
  22. int main()
  23. {
  24.  
  25. t=time(0);
  26. srand((unsigned int) t);
  27. cout<<"\nEnter the number of nodes ... :";
  28. int n;
  29. cin>>n;
  30. adj *list=new adj[n+1];
  31. int i=0;
  32. for(i=0;i<=n;i++)
  33. {
  34. list[i].ndnm=i;
  35. list[i].nmed=0;
  36. list[i].next=NULL;
  37. }
  38. cout<<"Adjacency List Initialization Formation finished.\n";
  39. /*
  40.   for(i=0;i<=n;i++)
  41.   {
  42.   cout<<"Node Num :"<<list[i].ndnm<<"\n";
  43.   cout<<"Number of edges :"<<list[i].nmed<<"\n";
  44.   cout<<"Address Stored in *next :"<<list[i].next<<"\n";
  45.   }
  46. */
  47. int n1,n2;
  48. cout<<"\nEnter Edge Length Limit :\n";
  49. cout<<"\nEnter lower bound :";
  50. cin>>n1;
  51. cout<<"\nEnter upper bound :";
  52. cin>>n2;
  53.  
  54. int count=0,edlength; // Counts the no. of nodes having atleast 3 edges.
  55. int u=0,v=0; //Stores the Node number .... Randomy generated
  56. nodes *temp,*temp2;
  57. while(edc!=n)
  58. {
  59. u=rand()%n+1;
  60. v=rand()%n+1;
  61. if(u!=v)
  62. {
  63. if(list[u].nmed<7 && list[v].nmed<7)
  64. {
  65. edlength=randomed(n1,n2);
  66. if(list[u].nmed==0)
  67. {
  68. list[u].next=new nodes;
  69. temp=list[u].next;
  70. temp->ndnm=v;
  71. temp->edln=edlength;
  72. temp->next=NULL;
  73. list[u].nmed++;
  74. }
  75. else
  76. {
  77. checknd(list[u].next,list,u,v,edlength);
  78. }
  79. if(list[v].nmed==0)
  80. {
  81. list[v].next=new nodes;
  82. temp2=list[v].next;
  83. temp2->ndnm=u;
  84. temp2->edln=edlength;
  85. temp2->next=NULL;
  86. list[v].nmed++;
  87. }
  88. else
  89. {
  90. checknd(list[v].next,list,v,u,edlength);
  91. }
  92.  
  93. }
  94. }
  95. }
  96. disp(list,n);
  97. cin.ignore();
  98.  
  99. return 0;
  100. }
  101.  
  102. //Now the code of the function which shall generate the length of the edges. randomly
  103. int randomed(int a, int b)
  104. {
  105. return (a+rand()%(b-a)+1);
  106. }
  107.  
  108. //Function which checks the adjacency list of each node and finds whether
  109. //an edge exist to the node , if not then adds the edge. :)
  110. void checknd(nodes *t,adj *list,int u,int v,int edlength)
  111. {
  112. int flag=0;
  113. nodes *t2; // Stores the Node which Has next as NULL;
  114. do
  115. {
  116. if(t->ndnm==v)
  117. {
  118. flag=1;
  119. }
  120. if(t->next==NULL)
  121. t2=t;
  122. t=t->next;
  123. }
  124. while(t!=NULL);
  125. t=t2;
  126. if(flag==0)
  127. {
  128. t->next=new nodes;
  129. t=t->next;
  130. t->next=NULL;
  131. t->ndnm=v;
  132. list[u].nmed++;
  133. if(list[u].nmed==3)
  134. {
  135. edc++; // Here the no. of nodes having 3 edges atleast are incremented. Global edc.
  136. }
  137. t->edln=edlength;
  138. }
  139. }
  140.  
  141. void disp(adj *list, int n) // Function which displays the output to a File.
  142. {
  143. ofstream file;
  144. file.open("d:\\illusionist\\node.txt");
  145. int i=0;
  146. nodes *temp;
  147. for(i=1;i<=n;i++)
  148. {
  149. temp=list[i].next;
  150. file<<"\n"<<list[i].ndnm<<"\t---->";
  151. do
  152. {
  153. file<<" "<<temp->ndnm<<"-";
  154. temp=temp->next;
  155. }
  156. while(temp);
  157. }
  158. }
I would really like to know how can i make this program accept and run with bigger input.
Your time reading my post is most appreciated. Thanks a lot for your time.
Regards
MiniGWeek
Last edited by minigweek; May 24th, 2007 at 1:38 am. Reason: Added Code=Language , Salutation.
I was born Genius, but some loser Leeched it.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Randomly Generated Network

 
0
  #2
May 24th, 2007
you are using a 16 bit compiler. which one?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 62
Reputation: minigweek is on a distinguished road 
Solved Threads: 4
minigweek's Avatar
minigweek minigweek is offline Offline
Junior Poster in Training

Re: Randomly Generated Network

 
0
  #3
May 24th, 2007
I am using Code::Blocks editor, and the minigw compiler that comes along with it. ALso Just tested it with Visual CPP Express Edition 2005 , same problem.
Last edited by minigweek; May 24th, 2007 at 1:57 am. Reason: Added Compiler, on which tested.
I was born Genius, but some loser Leeched it.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 62
Reputation: minigweek is on a distinguished road 
Solved Threads: 4
minigweek's Avatar
minigweek minigweek is offline Offline
Junior Poster in Training

Re: Randomly Generated Network

 
1
  #4
May 24th, 2007
lol
My dilemma is solved. here is why the above was not working for an input greater than 32768 .. because rand() is a pseudorandom number generator between the seed [ set by srand() or the default if u do nt use srand() ] and RAND_MAX . RAND_MAX is an integer constant defined in cstdib.
Its value is 32767. As a result all the nodes randomly generated , lied between 32678. it seems i need to define my own function usinf rand() such that for an input greater than 32768 , it caters to that.
Thank u all for dropping by
I was born Genius, but some loser Leeched it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC