-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildgraph.c
More file actions
170 lines (159 loc) · 5.28 KB
/
Copy pathbuildgraph.c
File metadata and controls
170 lines (159 loc) · 5.28 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
////////////////////////////////////////////////////////////////////////////////
// Main File: main.c
// This File: buildgraph.c
// Other Files: progexe.c progexe.h Textparsing.c Textparsing.h
// linkedlist.c linkedlist.h buildgraph.h buildrepr.c
// buildrepr.h
// Semester: CS 537 Fall 2018
//
// Author: Youmin Han
// Email: youmin.han@wisc.edu
// CS Login: youmin
//
// Author: Xianjie Zheng
// Email: xzheng97@wisc.edu
// CS Login: xianjie
//
/////////////////////////// OTHER SOURCES OF HELP //////////////////////////////
// fully acknowledge and credit all sources of help,
// other than Instructors and TAs.
//
// Persons: NULL
//
// Online sources: NULL
//
//////////////////////////// 80 columns wide ///////////////////////////////////
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "buildgraph.h"
#include "buildrepr.h"
#include "Textparsing.h"
#include "linkedlist.h"
/*
* Function that connect node to a graph
*
* @param
* head: the nodelist for the program
* inputarray: the argument that user want to run
*/
struct LinklistNode* buildgraph(struct LinklistNode* head, char * inputarray){
struct node* newnode = NULL;
int findflag = 0;
struct node* targetnode = NULL;
for (int i = 0; i < getsize(&head); i++){
targetnode = (struct node*)getnodedata(&head, i);
for (int j = 0; j < getsize(&targetnode->dependlist); j++){
for (int k = 0 ; k < getsize(&head); k++){
// When the children find in targetnode
if(strcmp((char *)getnodedata(&targetnode->dependlist, j), (char *)(((struct node*)getnodedata(&head, k))->target)) == 0){
findflag = 1;
append(&(((struct node*)getnodedata(&head,k))->parent), getnodedata(&head, i));
append(&targetnode->children, getnodedata(&head, k));
}
}
// When the children does not list in targetnode
if(findflag == 0){
newnode = (struct node*) calloc(1, sizeof(struct node));
newnode->target = (char *)getnodedata(&targetnode->dependlist, j);
//append this newnode's parent
append(&newnode->parent, (void*)getnodedata(&head, i));
append(&targetnode->children,(void*)newnode);
append(&head, newnode);
}
findflag = 0;
}
}
struct LinklistNode* exeorder = (struct LinklistNode*)calloc(1, sizeof(struct LinklistNode ));
// Check if there's any cycle in the graph
if(inputarray == NULL){
if(detectcycle((struct node*)(getnodedata(&head,0)), exeorder, NULL)) {
fprintf(stderr,"ERROR: cycle detected!\n");
exit(EXIT_FAILURE);
}
}
else{
for (int i = 0; i < getsize(&head); i++){
if(strcmp((char*)(((struct node*)getnodedata(&head,i))->target) , inputarray)==0){
if(detectcycle((struct node*)(getnodedata(&head,i)), exeorder, NULL)) {
fprintf(stderr,"ERROR: cycle detected!\n");
exit(EXIT_FAILURE);
}
break;
}
}
}
// return the exectuion oreder of the node
return exeorder;
}
/*
* Function that check if there's any cycle in the program
*
* @param
* head: the nodelist for the program
* order: the order of execution
* path: temparary path of execution
*/
int detectcycle(struct node* head, struct LinklistNode* order, struct LinklistNode* path) {
// Check if children exist
if(head->children == NULL) {
append(&order, (void*)head);
return 0;
}
// Check if the path contains the node
if(contains(&path, head)){
for(int d = getsize(&path)-1; d >=0; d--){
free(getnode(&path, d));
}
return 1;
}
// Add to the path
append(&path,(void*)head);
// Recutsion to check cycle
for (int i = 0; i < getsize(&(head->children)); i++) {
if(((struct node*)getnodedata(&head->children,i))->visited == 1) {
continue;
}
else {
if(!detectcycle((struct node*)getnodedata(&head->children,i), order, path)) {
((struct node*)getnodedata(&head->children,i))->visited = 1;
}
else {
return 1;
}
}
}
append(&order, (void*)head);
removelastnode(&path);
return 0;
}
/*
* Function that check if the node is reappear in the path
*
* @param
* headref: the node linkedlist
* linkednode: the node we want to check
*/
int contains(struct LinklistNode** headref, struct node* linkednode)
{
for(int i = 0; i < getsize(headref); i++){
if(strcmp((char*)((struct node*)getnodedata(headref, i))->target,(char*)(linkednode->target)) == 0 ) {
return 1;
}
}
return 0;
}
/*
* Function that set revisted into 0
*
* @param
* head: the nodelist for the program
*/
void resetvisited(struct LinklistNode* head) {
struct node* root;
for (int i = 0; i < getsize(&head); i++){
root = (struct node*)getnodedata(&head, i);
root->visited = 0;
}
}