forked from xtaci/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_demo.cpp
More file actions
64 lines (53 loc) · 1.09 KB
/
Copy pathgraph_demo.cpp
File metadata and controls
64 lines (53 loc) · 1.09 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
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "undirected_graph.h"
/**
* randomly generate a graph, for test purpose
*/
struct Graph * undirected_graph_rand(int nvertex)
{
struct Graph * g = undirected_graph_create();
int i;
for(i=0;i<nvertex;i++) {
undirected_graph_add_vertex(g, i);
}
// random connect
for(i=0;i<nvertex;i++) {
int j;
for(j=i+1;j<nvertex;j++) {
int dice = rand()%5;
if (dice == 0) { // chance 20%
int w = rand()%100;
undirected_graph_add_edge(g, i, j, w);
}
}
}
return g;
}
int main()
{
srand(time(NULL));
int NVERTEX = 100;
struct Graph * g = undirected_graph_rand(NVERTEX);
undirected_graph_print(g);
printf("Random Delete Vertex:\n");
// random delete vertex
int i;
for(i=0;i<NVERTEX;i++) {
int n = rand()%NVERTEX;
printf("delete: %d\n", n);
undirected_graph_del_vertex(g,n);
}
undirected_graph_print(g);
printf("Delete All Edges: \n");
for(i=0;i<NVERTEX;i++) {
int j;
for(j=i+1;j<NVERTEX;j++) {
undirected_graph_del_edge(g, i, j);
}
}
undirected_graph_print(g);
graph_free(g);
return 0;
}