33#include <stdbool.h>
44#include <string.h>
55
6+
67struct graph_node {
78 int req_num ;
89 int reqs [15 ];
10+ bool touched ;
11+ bool taken ;
912};
1013
11- static bool dfs (struct graph_node * courses , int id , bool * takens , bool * touched , int * order , int * count )
14+ static bool dfs (struct graph_node * courses , int id , int * order , int * count )
1215{
1316 int i ;
14- if (touched [id ]) {
17+ if (courses [id ]. touched ) {
1518 return true;
16- } else if (takens [id ]) {
19+ } else if (courses [id ]. taken ) {
1720 return false;
1821 } else {
19- takens [id ] = true;
22+ courses [id ]. taken = true;
2023 for (i = 0 ; i < courses [id ].req_num ; i ++ ) {
21- if (!dfs (courses , courses [id ].reqs [i ], takens , touched , order , count )) {
24+ if (!dfs (courses , courses [id ].reqs [i ], order , count )) {
2225 return false;
2326 }
2427 }
25- /* marked as available and no need to traverse next time */
28+ /* Record path in backtrace before DFS return */
2629 order [(* count )++ ] = id ;
27- touched [id ] = true;
28- takens [id ] = false;
30+ /* If paths overlapped, mark in backtracing for no need to traverse next time */
31+ courses [id ].touched = true;
32+ courses [id ].taken = false;
2933 return true;
3034 }
3135}
@@ -34,27 +38,22 @@ static bool dfs(struct graph_node *courses, int id, bool *takens, bool *touched,
3438 * Return an array of size *returnSize.
3539 * Note: The returned array must be malloced, assume caller calls free().
3640 */
37- static int * findOrder (int numCourses , int * * prerequisites , int prerequisitesRowSize , int prerequisitesColSize , int * returnSize )
41+ static int * findOrder (int numCourses , int * * prerequisites , int prerequisitesSize , int * prerequisitesColSize , int * returnSize )
3842{
3943 int i ;
4044 int * order = malloc (numCourses * sizeof (int ));
41- bool * takens = malloc (numCourses );
42- bool * touched = malloc (numCourses );
4345 struct graph_node * courses = malloc (numCourses * sizeof (* courses ));
4446
4547 memset (courses , 0 , numCourses * sizeof (* courses ));
46- memset (takens , false, numCourses * sizeof (bool ));
47- memset (touched , false, numCourses * sizeof (bool ));
48-
49- for (i = 0 ; i < prerequisitesRowSize ; i ++ ) {
48+ for (i = 0 ; i < prerequisitesSize ; i ++ ) {
5049 int id = prerequisites [i ][0 ];
5150 int req = prerequisites [i ][1 ];
5251 courses [id ].reqs [courses [id ].req_num ++ ] = req ;
5352 }
5453
5554 * returnSize = 0 ;
5655 for (i = 0 ; i < numCourses ; i ++ ) {
57- if (!dfs (courses , i , takens , touched , order , returnSize )) {
56+ if (!dfs (courses , i , order , returnSize )) {
5857 * returnSize = 0 ;
5958 return order ;
6059 }
@@ -67,6 +66,7 @@ int main(void)
6766{
6867 int i , course_num = 3 , pair_num = 1 ;
6968 int * * pairs = malloc (pair_num * sizeof (int * ));
69+ int * col_sizes = malloc (pair_num * sizeof (int ));
7070 pairs [0 ] = malloc (2 * sizeof (int ));
7171 pairs [0 ][0 ] = 1 ;
7272 pairs [0 ][1 ] = 0 ;
@@ -84,7 +84,7 @@ int main(void)
8484 //pairs[4][1] = 5;
8585
8686 int count = 0 ;
87- int * ids = findOrder (course_num , pairs , pair_num , 2 , & count );
87+ int * ids = findOrder (course_num , pairs , pair_num , col_sizes , & count );
8888 for (i = 0 ; i < count ; i ++ ) {
8989 printf ("%d " , ids [i ]);
9090 }
0 commit comments