-
Notifications
You must be signed in to change notification settings - Fork 4
/
dg.h
62 lines (48 loc) · 1.2 KB
/
dg.h
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
#ifndef __DG_H__
#define __DG_H__
/*
* directed graph's simple implementation, used for
* type-checking
*/
#define MAX_VERTEX_NUM 20
typedef struct DG_edge_ *DG_edge;
typedef struct DG_ DG
struct DG_edge_ {
int adjvex;
DG_edge next;
void *info; // info of the edge
};
struct DG_vertex {
void *info; // info of the vertex
DG_edge firstEdge;
};
struct DG_ {
DG_vertex vertice[MAX_VERTEX_NUM];
int verticeCnt;
// cycles of the DG if any
int *cycles;
// topological sort result if no cycles
int *tpg;
// topological sort reverse index into tpg
int tpg_sort_rev_index;
/*
* auxiliary data
*/
// marked whether a vertex is visited
int visited[MAX_VERTEX_NUM];
// marked whether a vertex is on the visiting path
int on_stack[MAX_VERTEX_NUM];
// edge_to[v] = previous vertex on path to v
int edge_to[MAX_VERTEX_NUM];
};
DG_edge DG_Edge(int adjvex, DG_edge next, void *info);
DG_vertex DG_Vertex(void *info, DG_edge firstEdge);
DG DG_makeEmptyDG();
int indexOfVertex(DG dg, void *info);
// enter edge: v1 -> v2
DG DG_enterEdge(DG dg, void *info1, void *info2);
void DG_dfs_from(DG dg, int v);
// only called once for a single DG
void DG_dfs(DG dg);
int DG_cycle(DG dg);
#endif