forked from SkienaBooks/Algorithm-Design-Manual-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbipartite.c
117 lines (82 loc) · 2.34 KB
/
bipartite.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
/* bipartite.c
Two color a bipartite graph
by: Steven Skiena
begun: March 27, 2002
*/
/*
Copyright 2003 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
This program appears in my book:
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information.
This book can be ordered from Amazon.com at
http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include <stdio.h>
#include "bool.h"
#include "bfs-dfs.h"
#include "queue.h"
extern bool processed[]; /* which vertices have been processed */
extern bool discovered[]; /* which vertices have been found */
extern int parent[]; /* discovery relation */
extern int entry_time[MAXV+1]; /* time of vertex entry */
extern int exit_time[MAXV+1]; /* time of vertex exit */
#define UNCOLORED 0 /* vertex not yet colored */
#define WHITE 1 /* white vertex */
#define BLACK 2 /* black vertex */
int color[MAXV+1]; /* assigned color of v */
bool bipartite; /* is the graph bipartite? */
void process_vertex_early(int v) {
}
void process_vertex_late(int v) {
}
/* [[[ complement_cut */
int complement(int color) {
if (color == WHITE) {
return(BLACK);
}
if (color == BLACK) {
return(WHITE);
}
return(UNCOLORED);
}
/* ]]] */
/* [[[ pecolor_cut */
void process_edge(int x, int y) {
if (color[x] == color[y]) {
bipartite = FALSE;
printf("Warning: graph not bipartite, due to (%d,%d)\n", x, y);
}
color[y] = complement(color[x]);
}
/* ]]] */
/* [[[ twocolor_cut */
void twocolor(graph *g) {
int i; /* counter */
for (i = 1; i <= (g->nvertices); i++) {
color[i] = UNCOLORED;
}
bipartite = TRUE;
initialize_search(g);
for (i = 1; i <= (g->nvertices); i++) {
if (discovered[i] == FALSE) {
color[i] = WHITE;
bfs(g, i);
}
}
}
/* ]]] */
int main(void) {
graph g;
int i;
read_graph(&g, FALSE);
print_graph(&g);
twocolor(&g);
for (i = 1; i <= (g.nvertices); i++) {
printf(" %d", color[i]);
}
printf("\n");
return 0;
}