-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomgrapho.c
104 lines (78 loc) · 2.07 KB
/
randomgrapho.c
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "basic.h"
#include "b_urn.h"
#include "ugraph.def"
UGRAPH_PROTO(G)
b_urn(U1)
/*****************************************************************************/
/*
* generates random maximal planar graph on n vertices.
* The resulting ugraph G represents a topological
* embedding of the generated random graph.
*/
void random_maximal_planar_graph(int n)
{
vertex u,v,w,x=0,y,z;
edge e,f,g,h=0,i=0;
num j,typ;
if (n<4) err_handler(88,"at leat 4 vertices");
U1_new(5*n);
G_new(n,5*n);
u = G_new_vertex();
v = G_new_vertex();
w = G_new_vertex();
U1_put(G_new_edge(u,v));
U1_put(G_new_edge(u,w));
U1_put(G_new_edge(v,w));
for(j=4; j<=n; j++)
{
do { e = U1_draw(); } while (G_source(e)==0); U1_put(e);
v = ((random()%2)==1) ? G_source(e) : G_target(e) ;
switch (G_degree(v))
{
case 2: { typ=1; break; }
case 3: { typ=(num) (1+(random()%2)); break; }
default: { typ=(num) (1+(random()%3)); break; }
}
u=G_opposite(v,e);
if (typ>1)
{
if (typ==3)
{
f=G_cyclic_incident_succ(e,v);
y=G_opposite(v,f);
h=G_cyclic_incident_pred(f,y);
G_delete_edge(f);
}
f=G_cyclic_incident_succ(e,v);
x=G_opposite(v,f);
i=G_cyclic_incident_pred(f,x);
G_delete_edge(f);
}
f=G_cyclic_incident_succ(e,v);
w=G_opposite(v,f);
g=G_cyclic_incident_pred(e,u);
z=G_new_vertex();
if (typ>1)
{
if (typ==3)
{
U1_put(G_new_edge2(z,y,G_last_incident_edge(z),h,after,after));
}
U1_put(G_new_edge2(z,x,G_last_incident_edge(z),i,after,after));
}
U1_put(G_new_edge2(z,w,G_last_incident_edge(z),f,after,after));
U1_put(G_new_edge2(z,v,G_last_incident_edge(z),e,after,after));
U1_put(G_new_edge2(z,u,G_last_incident_edge(z),g,after,after));
}
U1_delete();
G_compress(NULL,NULL);
}
void random_planar_graph(int n, int m)
{
random_maximal_planar_graph(n);
U1_new(G_number_of_edges());
Apply_forall_edges(e,G, U1_put(e); )
while (G_number_of_edges()>m)
G_delete_edge(U1_draw());
U1_delete();
}