int main()
{
	int graph[MAXSIZE][MAXSIZE];
	struct node * hash_table[MAXSIZE] = {NULL};
	int i;
	int j;
	int k;
	int T;
	int V;
	int flag;

	freopen("detect_cycle_graph_inp.txt", "r", stdin);

	scanf("%d", &T);

	for (i = 0; i < T; i++) {
			scanf("%d", &V);

			for (j = 1; j <= V; j++) {
				make_set(hash_table, j);

				for (k = 1; k <= V; k++) {
					scanf("%d", &graph[j][k]);
				}
			}

			flag = detect_cycle(graph, V, hash_table);

			if (flag == 1) {
				printf("Yes\n");
			} else {
				printf("No\n");
			}
	}

	fclose(stdin);

	return 0;
}
Example #2
0
/** recursive function to depth first search for cycles.
 * @param visit: the lock visited at this step.
 *	its dfs_next pointer gives the visited lock up in recursion.
 * 	same as lookfor at level 0.
 * @param level: depth of recursion. 0 is start.
 * @param from: search for matches from unvisited node upwards.
 */
static void search_cycle(struct lock_ref* visit, int level, 
	struct lock_ref* from)
{
	struct lock_ref* ref;
	/* check for cycle */
	if(detect_cycle(visit, from) && level != 0) {
		found_cycle(visit, level);
		fatal_exit("found lock order cycle");
	}
	/* recurse */
	if(!visit->lock->visited)
		from = visit;
	if(verb > 1) fprintf(stderr, "[%d] visit lock %u %u %s %d\n", level,
			(unsigned)visit->lock->id.thr, 
			(unsigned)visit->lock->id.instance,
			visit->lock->create_file, visit->lock->create_line);
	RBTREE_FOR(ref, struct lock_ref*, visit->lock->smaller) {
		ref->lock->dfs_next = visit;
		search_cycle(ref, level+1, from);
	}
	visit->lock->visited = 1;
}