-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom-tree-generator.cpp
More file actions
51 lines (48 loc) · 1.04 KB
/
random-tree-generator.cpp
File metadata and controls
51 lines (48 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
*Author : Kalyan Chirla
*Date :- 03-07-2018
*/
#include <iostream>
#include <cstdlib>
#include<time.h>
using namespace std;
int main()
{
long long int n; //Number of nodes in the tree.
cin >> n;
long long int src[n]; // source array.
long long int dst[n]; //destination array.
long long int arr[n][n]; //adjacency matrix.
for(long long int i=0;i<n;i++)
{
dst[i]=i;
}
for(long long int i=0;i<n;i++)
{
for(long long int j=0;j<n;j++)
{
arr[i][j]=0;
}
}
long long int top=0;
src[0]=dst[0];
top++;
long long int i=0;
while(top!=(n))
{ srand(time(NULL));
long long int temp = rand()%(i+1); // random integer generator between 0 and i.
arr[src[temp]][dst[top]]=1;
arr[dst[top]][src[temp]]=1;
src[i+1]=dst[top];
top++;
i++;
}
for(long long int i=0;i<n;i++)
{
cout << "\n";
for(long long int j=0;j<n;j++)
{
cout << arr[i][j] << " ";
}
}
}