-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
130 lines (109 loc) · 3.56 KB
/
code
File metadata and controls
130 lines (109 loc) · 3.56 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
// The MAX_LENGTH should be defined according to the maximum length of the lines in the input file:
#define MAX_LENGTH 200
// Load the Adjacency list file:
FILE * Input_file;
Input_file=fopen("in7.txt","r"); // PLease change the input file name here
// Initiallization:
char Row_str[MAX_LENGTH]; // Each row of the input file
char *pch;
int Num_vert=0; //Number of vertices
int i=0, j=0, k=0;
int Head_node[MAX_LENGTH][MAX_LENGTH]; // Head_node[i][j]= The head node of the j-th edge of node i
int Edge_Charge[MAX_LENGTH][MAX_LENGTH];
for (i=0;i<MAX_LENGTH;i++){
for (j=0;j<MAX_LENGTH;j++){
Head_node[i][j]=0;
Edge_Charge[i][j]=0;
}
}
int Node_Charge[MAX_LENGTH];
int indegree[MAX_LENGTH];
for (k=0;k<MAX_LENGTH;k++){
indegree[k]=0;
Node_Charge[k]=0;}
int Num_edges=0;
int Vertex;
int Topological_Order[MAX_LENGTH];
i=0;j=0;
// Load the input file to an array
//printf("The Adjacency list is:\n\n");
while(fgets(Row_str, MAX_LENGTH, Input_file)!=NULL){
puts(Row_str);
Node_Charge[Num_vert]++;
Num_vert++;
pch=strtok(Row_str," ");
while (pch!=NULL){
Vertex=atoi(pch);
if (Vertex!=0){
// Find head of each edge(=the node that the edge enters it) and initiallizing in-degree array
if (j!=0){
Head_node[i][j-1]=Vertex;
Num_edges++;
indegree[Vertex-1]++;
Edge_Charge[i][Vertex-1]+=2; // 1 charge per reading edge, 1 charge for counting in-degree
}
}
pch = strtok( NULL, " " );
j++;}
i++;j=0;}
printf("\n_______________________________________________\n");
printf("\n\nTotal number of Vertices = %d and Edges=%d\n",Num_vert,Num_edges);
printf("\n_______________________________________________\n");
//TOPOLOGICAL SORTING:
int order=0;
int check_cycle=1;
i=0;k=1;
while (i<Num_vert){
if (indegree[i]==0){
Node_Charge[i]+=2; // 2 charges for pop and push from in-degree to topological order
Topological_Order[order]=i+1;
order++;
indegree[i]=-1;
k++;
for(j=0;j<Num_vert;j++){
if ((Head_node[i][j])>0){
indegree[(Head_node[i][j])-1]--;
Edge_Charge[i][(Head_node[i][j])-1]++;} // modify in-degree by deleting related edges, and charging them
}
i=0;
check_cycle=0;
}
else
i++;check_cycle=1;
}
if (check_cycle==1 && k<Num_vert){
printf("This graph has a cycle!\n");
return 1;
}
printf("\n");
printf("\nTopological Order:\n");
for (i=0;i<Num_vert;i++)
fprintf(stdout,"%d ",Topological_Order[i]);
printf("\n_______________________________________________\n");
printf("\n");
int Total_Node_Charge=0;
for (i=0;i<Num_vert;i++){
fprintf(stdout,"Vertex %d Charge = %d\n",i+1,Node_Charge[i]);
Total_Node_Charge+=Node_Charge[i];
}
printf("\nTotal number of operations charged to all vertices is :%d\n",Total_Node_Charge);
printf("\n_______________________________________________\n");
int Total_Edge_Charge=0;
for (i=0;i<Num_vert;i++){
for(j=0;j<Num_vert;j++){
if (Edge_Charge[i][j] != 0)
fprintf(stdout,"The Charge of Edge from Vertex %d to Vertex %d = %d\n",i+1,j+1,Edge_Charge[i][j]);
Total_Edge_Charge+=Edge_Charge[i][j];}}
printf("\nTotal number of operations charged to edges is : %d\n",Total_Edge_Charge);
printf("\n_______________________________________________\n");
int Total_operations=Total_Edge_Charge+Total_Node_Charge;
printf("\nTotal number of operations is : %d\n",Total_operations);
printf("\n_______________________________________________\n");
fclose(Input_file);
return 0;
}