-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain.c
141 lines (118 loc) · 3.06 KB
/
chain.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "chain.h"
int find_chains_from(Graph* graph, int start)
{
Node *temp = graph->adj_list[start].list, *cur;
EdgeList* edgelist = NULL;
int chains = 0;
//simple chains cannot begin from a degree two vertex
if(graph->adj_list[start].edges == 2)
return 0;
//All simple chains from this vertex have been explored
graph->vertex_list[start].visited = TRUE;
//increment over the adjacent (nodes)vertices of the vertex with id "start"
while(temp)
{
//if the adjacent vertex has not already been visited
if(temp->vertex->visited == FALSE)
{
print_vertex(&graph->vertex_list[start], " -> ");
//travel along the simple chain
cur = temp;
edgelist = &graph->adj_list[cur->vertex->id];
while(1)
{
if(edgelist->edges == 2)
{
cur->vertex->visited = TRUE;
print_vertex(cur->vertex, " -> ");
//Out of the two adjaxcent vertices choose the unvisited one.
//If both are visited then you are stuck in a cycle. Break out of it.
if(edgelist->list->vertex->visited)
if(edgelist->list->next->vertex->visited)
{
printf("<Detected a cycle!>\n");
break;
}
else
cur = edgelist->list->next;
else
cur = edgelist->list;
edgelist = &graph->adj_list[cur->vertex->id];
}
else
{
print_vertex(cur->vertex, "");
chains++;
break;
}
}
printf("\n");
}
temp = temp->next;
}
return chains;
}
int silent_find_chains_from(Graph* graph, int start)
{
Node *temp = graph->adj_list[start].list, *cur;
EdgeList* edgelist = NULL;
int chains = 0;
//simple chains cannot begin from a degree two vertex
if(graph->adj_list[start].edges == 2)
return 0;
//All simple chains from this vertex have been explored
graph->vertex_list[start].visited = TRUE;
//increment over the adjacent (nodes)vertices of the vertex with id "start"
while(temp)
{
//if the adjacent vertex has not already been visited
if(temp->vertex->visited == FALSE)
{
//travel along the simple chain
cur = temp;
edgelist = &graph->adj_list[cur->vertex->id];
while(1)
{
if(edgelist->edges == 2)
{
cur->vertex->visited = TRUE;
//Out of the two adjaxcent vertices choose the unvisited one.
//If both are visited then you are stuck in a cycle. Break out of it.
if(edgelist->list->vertex->visited)
if(edgelist->list->next->vertex->visited)
break;
else
cur = edgelist->list->next;
else
cur = edgelist->list;
edgelist = &graph->adj_list[cur->vertex->id];
}
else
{
chains++;
break;
}
}
}
temp = temp->next;
}
return chains;
}
int list_all_chains(Graph* graph)
{
int total = 0, i;
Graph* copy = make_graph_copy(graph);
for(i = 0; i < graph->verts; i++)
total += find_chains_from(copy, copy->vertex_list[i].id);
free_graph(©);
return total;
}
int silent_list_all_chains(Graph* graph)
{
int total = 0, i;
Graph* copy = make_graph_copy(graph);
for(i = 0; i < graph->verts; i++)
total += silent_find_chains_from(copy, copy->vertex_list[i].id);
free_graph(©);
return total;
}