-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.java
220 lines (195 loc) · 6.99 KB
/
main.java
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package dsa;
import java.util.Scanner;
import javax.sound.sampled.SourceDataLine;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Timetable_project
{
ArrayList<Node> arr=new ArrayList<Node>(); //adjacency list that stores the connected graph of all subjects
static class Node //each node has a subname (subject name) associated with it and a next node that it points to
{
String subname;
Node next;
Node(String subname)
{
this.subname=subname;
next=null;
}
Node()
{
subname=null;
next=null;
}
}
static Scanner src=new Scanner(System.in);
public void create() //creates the graph from given csv file
{
String csvFile = "E:/project/dsa/courses.csv"; //file path for the csv file
readFile reader = new readFile(); //create an object of class readFile
ArrayList<List<String>> data=reader.readFile1(csvFile); //store csv data as an arraylist of list
int nyear = data.size(); //number of batches
System.out.println("Arraylist of courses and subjects");
System.out.println(data.toString()); //print arraylist of lists
System.out.println();
for(int i=0;i<nyear;i++) //creating a list for each unique subjects
{
for(int j=1;j<data.get(i).size();j++)
{
int flag=0;
for(int k=0;k<arr.size();k++)
{
if((arr.get(k).subname).equals(data.get(i).get(j)))
{
flag=1;
break;
}
}
if(flag==0)
{
Node new_node=new Node(data.get(i).get(j));
arr.add(new_node);
}
}
}
System.out.println("Unique subjects (vertices) : ");
for(int p=0;p<arr.size();p++) //prints all unique subjects (vertices of our graph)
{
System.out.print(arr.get(p).subname+" ");
}
System.out.println();
for(int i=0;i<data.size();i++)//edges are added between all subjects for the same batch
{
for(int j=1;j<data.get(i).size();j++)
{
for(int k=j+1;k<data.get(i).size();k++)
{
insert(data.get(i).get(j),data.get(i).get(k));
insert(data.get(i).get(k),data.get(i).get(j));
}
}
}
}
public void insert(String u,String v) //passing two subjects that have an edge between them
{
Node temp=new Node(v);
for(int a=0;a<arr.size();a++) //find index of source subject
{
if(arr.get(a).subname.equals(u))
{
int flag=0;
Node ptr=arr.get(a);
while(ptr.next!=null)
{
if(ptr.subname.equals(v)) //find index of destination subject
{
flag=1;
break;
}
ptr=ptr.next;
}
if(flag==0)
ptr.next=temp;
}
}
}
public void display() //To display the adjancency list
{
System.out.println();
System.out.println("The subject list: ");
for(int h=0;h<arr.size();h++) //for traversal of all the subjects present in the list
{
System.out.print(arr.get(h).subname);
Node temp=arr.get(h).next;
while(temp!=null)
{
System.out.print(" --> "+temp.subname); //Displaying the list of adjacent vertices for each subject
temp=temp.next;
}
System.out.println(" ");
}
System.out.println();
}
public void colour()
{
int s=arr.size();
int res[]=new int[s];
char ava[]=new char[s];
for(int i=0;i<s;i++)//initializing the res array to -1
{
res[i]=-1;
}
for(int i=0;i<s;i++)//initializing the ava array to 'T'
{
ava[i]='T';
}
res[0]=0;
for(int k=1;k<s;k++)
{
Node temp=arr.get(k).next;
while(temp!=null)// Assigning colours to different nodes considering no to adjacent vertices can have the same colour
{
for(int p=0;p<s;p++)
{
if(arr.get(p).subname.equals(temp.subname))
{
if(res[p]!=(-1))//If the colour is already assigned to a node then it can't be assigned to its adjacent nodes
//Hence marking the colours which are not available in ava array
{
ava[res[p]]='F';
}
break;
}
}
temp=temp.next;
}
for(int q=0;q<s;q++)
{
if(ava[q]=='T')//The first available colour in the array will be assigned to to the node
{
res[k]=q;
break;
}
}
for(int r=0;r<s;r++)//Once again initializing the whole array 'T'
{
ava[r]='T';
}
}
System.out.println("resulting array with colours: ");//Printing the colours asssigned to different nodes in an array
for(int t=0;t<s;t++)
{
System.out.print(res[t]+" ");
}
System.out.println(" ");
System.out.println();
System.out.println("Timetable : ");
int count=0;//Stores the number of subjects printed
for(int t=0;t<s;t++)//To print all the sub whose exams can be conducted in the same slot
{
System.out.print("SLOT "+(t+1)+" : ");
for(int h=0;h<s;h++)
{
if(res[h]==t)//printing all the subjects where node colour is 't'
{
System.out.print(arr.get(h).subname+" ");
count++;
}
}
System.out.println(" ");
if(count==arr.size()) //When all the subjects are displayed it breaks out of the loop
break;
}
}
public static void main(String[] args) //main
{
Timetable_project t=new Timetable_project();
t.create();
t.display();
t.colour();
}
}