| | |
Randomly Generated Network
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-
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
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.
-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-
c++ Syntax (Toggle Plain Text)
#include<iostream> #include<ctime> #include<fstream> using namespace std; struct nodes; struct adj { int ndnm; int nmed; nodes *next; }; struct nodes { int ndnm; int edln; nodes *next; }; int edc=0; // counts no of nodes hving atleast 3 edges int randomed(int a,int b); void checknd(nodes *t,adj *list,int u, int v, int edlength); void disp(adj *list,int n); int main() { t=time(0); srand((unsigned int) t); cout<<"\nEnter the number of nodes ... :"; int n; cin>>n; adj *list=new adj[n+1]; int i=0; for(i=0;i<=n;i++) { list[i].ndnm=i; list[i].nmed=0; list[i].next=NULL; } cout<<"Adjacency List Initialization Formation finished.\n"; /* for(i=0;i<=n;i++) { cout<<"Node Num :"<<list[i].ndnm<<"\n"; cout<<"Number of edges :"<<list[i].nmed<<"\n"; cout<<"Address Stored in *next :"<<list[i].next<<"\n"; } */ int n1,n2; cout<<"\nEnter Edge Length Limit :\n"; cout<<"\nEnter lower bound :"; cin>>n1; cout<<"\nEnter upper bound :"; cin>>n2; int count=0,edlength; // Counts the no. of nodes having atleast 3 edges. int u=0,v=0; //Stores the Node number .... Randomy generated nodes *temp,*temp2; while(edc!=n) { u=rand()%n+1; v=rand()%n+1; if(u!=v) { if(list[u].nmed<7 && list[v].nmed<7) { edlength=randomed(n1,n2); if(list[u].nmed==0) { list[u].next=new nodes; temp=list[u].next; temp->ndnm=v; temp->edln=edlength; temp->next=NULL; list[u].nmed++; } else { checknd(list[u].next,list,u,v,edlength); } if(list[v].nmed==0) { list[v].next=new nodes; temp2=list[v].next; temp2->ndnm=u; temp2->edln=edlength; temp2->next=NULL; list[v].nmed++; } else { checknd(list[v].next,list,v,u,edlength); } } } } disp(list,n); cin.ignore(); return 0; } //Now the code of the function which shall generate the length of the edges. randomly int randomed(int a, int b) { return (a+rand()%(b-a)+1); } //Function which checks the adjacency list of each node and finds whether //an edge exist to the node , if not then adds the edge. :) void checknd(nodes *t,adj *list,int u,int v,int edlength) { int flag=0; nodes *t2; // Stores the Node which Has next as NULL; do { if(t->ndnm==v) { flag=1; } if(t->next==NULL) t2=t; t=t->next; } while(t!=NULL); t=t2; if(flag==0) { t->next=new nodes; t=t->next; t->next=NULL; t->ndnm=v; list[u].nmed++; if(list[u].nmed==3) { edc++; // Here the no. of nodes having 3 edges atleast are incremented. Global edc. } t->edln=edlength; } } void disp(adj *list, int n) // Function which displays the output to a File. { ofstream file; file.open("d:\\illusionist\\node.txt"); int i=0; nodes *temp; for(i=1;i<=n;i++) { temp=list[i].next; file<<"\n"<<list[i].ndnm<<"\t---->"; do { file<<" "<<temp->ndnm<<"-"; temp=temp->next; } while(temp); } }

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.
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
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.
![]() |
Similar Threads
- Hidden program installs .dlls with randomly generated names in random "notify" reg. (Viruses, Spyware and other Nasties)
- Backdoor Hijack Virus (Viruses, Spyware and other Nasties)
- New social networking features (DaniWeb Community Feedback)
- Rundll32 Error (Viruses, Spyware and other Nasties)
- help with virus infection... (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: help in Makefile pls...
- Next Thread: I need your help with functions
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






