Skip to content

Commit 1fe4cca

Browse files
authored
Add files via upload
1 parent 998c7cf commit 1fe4cca

File tree

3 files changed

+301
-0
lines changed

3 files changed

+301
-0
lines changed

Assignment2/DisjointSets.java

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
//created by Xingya Ren
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
7+
/****************************
8+
*
9+
* COMP251 template file
10+
*
11+
* Assignment 2, Question 1
12+
*
13+
*****************************/
14+
15+
16+
public class DisjointSets {
17+
18+
private int[] par;
19+
private int[] rank;
20+
21+
/* contructor: creates a partition of n elements. */
22+
/* Each element is in a separate disjoint set */
23+
DisjointSets(int n) {
24+
if (n>0) {
25+
par = new int[n];
26+
rank = new int[n];
27+
for (int i=0; i<this.par.length; i++) {
28+
par[i] = i;
29+
}
30+
}
31+
}
32+
33+
public String toString(){
34+
int pari,countsets=0;
35+
String output = "";
36+
String[] setstrings = new String[this.par.length];
37+
/* build string for each set */
38+
for (int i=0; i<this.par.length; i++) {
39+
pari = find(i);
40+
if (setstrings[pari]==null) {
41+
setstrings[pari] = String.valueOf(i);
42+
countsets+=1;
43+
} else {
44+
setstrings[pari] += "," + i;
45+
}
46+
}
47+
/* print strings */
48+
output = countsets + " set(s):\n";
49+
for (int i=0; i<this.par.length; i++) {
50+
if (setstrings[i] != null) {
51+
output += i + " : " + setstrings[i] + "\n";
52+
}
53+
}
54+
return output;
55+
}
56+
57+
/* find resentative of element i */
58+
public int find(int i) {
59+
60+
/* Fill this method (The statement return 0 is here only to compile) */
61+
if(par[i]==i){
62+
//i's parent is itself --> self-loop
63+
return i;
64+
}else {
65+
int result = find(par[i]);
66+
par[i] = result;
67+
return result; //return the rep
68+
}
69+
}
70+
71+
/* merge sets containing elements i and j */
72+
public int union(int i, int j) {
73+
74+
/* Fill this method (The statement return 0 is here only to compile) */
75+
//get the ranks of i and j
76+
int iRep = this.find(i);
77+
int jRep = this.find(j);
78+
//if elements are in the same set, we do nothing and return the rep
79+
if(iRep==jRep) {
80+
return iRep;
81+
}
82+
//get the ranks
83+
int iRank = 0;
84+
int jRank = 0;
85+
iRank = rank[iRep];
86+
jRank = rank[jRep];
87+
//compare the ranks to see who should be the new parent
88+
if(iRank == jRank) {
89+
//merge i into j
90+
this.par[iRep] = jRep;
91+
rank[jRep]++; //increment the rank by one
92+
return jRep;
93+
}else if(iRank < jRank) {
94+
//merge i into j
95+
this.par[iRep] = jRep;
96+
return jRep;
97+
}else {
98+
//merge j into i
99+
this.par[jRep] = iRep;
100+
return iRep;
101+
}
102+
}
103+
104+
public static void main(String[] args) {
105+
106+
DisjointSets myset = new DisjointSets(6);
107+
System.out.println(myset);
108+
System.out.println("-> Union 2 and 3");
109+
myset.union(2,3);
110+
System.out.println(myset);
111+
System.out.println("-> Union 2 and 3");
112+
myset.union(2,3);
113+
System.out.println(myset);
114+
System.out.println("-> Union 2 and 1");
115+
myset.union(2,1);
116+
System.out.println(myset);
117+
System.out.println("-> Union 4 and 5");
118+
myset.union(4,5);
119+
System.out.println(myset);
120+
System.out.println("-> Union 3 and 1");
121+
myset.union(3,1);
122+
System.out.println(myset);
123+
System.out.println("-> Union 2 and 4");
124+
myset.union(2,4);
125+
System.out.println(myset);
126+
127+
}
128+
129+
}

Assignment2/HW_Sched.java

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
//created by Xingya Ren
2+
3+
import java.util.*;
4+
5+
class Assignment implements Comparator<Assignment>{
6+
int number;
7+
int weight;
8+
int deadline;
9+
10+
11+
protected Assignment() {
12+
}
13+
14+
protected Assignment(int number, int weight, int deadline) {
15+
this.number = number;
16+
this.weight = weight;
17+
this.deadline = deadline;
18+
}
19+
20+
21+
22+
/**
23+
* This method is used to sort to compare assignment objects for sorting.
24+
* Return -1 if a1 > a2
25+
* Return 1 if a1 < a2
26+
* Return 0 if a1 = a2
27+
*/
28+
@Override
29+
public int compare(Assignment a1, Assignment a2) {
30+
// TODO Implement this
31+
//sort by weight in descending order
32+
//if there is a tie-->put the one that has larger deadline before
33+
if(a1.weight < a2.weight) {
34+
return 1;
35+
}else if(a1.weight > a2.weight) {
36+
return -1;
37+
}else {
38+
//two have the same weights
39+
//the one with larger deadline comes first
40+
if(a1.deadline < a2.deadline) {
41+
return 1;
42+
}else {
43+
return -1;
44+
}
45+
}
46+
}
47+
}
48+
49+
public class HW_Sched {
50+
ArrayList<Assignment> Assignments = new ArrayList<Assignment>();
51+
int m;
52+
int lastDeadline = 0;
53+
54+
protected HW_Sched(int[] weights, int[] deadlines, int size) {
55+
for (int i=0; i<size; i++) {
56+
Assignment homework = new Assignment(i, weights[i], deadlines[i]);
57+
this.Assignments.add(homework);
58+
if (homework.deadline > lastDeadline) {
59+
lastDeadline = homework.deadline;
60+
}
61+
}
62+
m =size;
63+
}
64+
65+
66+
/**
67+
*
68+
* @return Array where output[i] corresponds to the assignment
69+
* that will be done at time i.
70+
*/
71+
public int[] SelectAssignments() {
72+
//TODO Implement this
73+
74+
//Sort assignments
75+
//Order will depend on how compare function is implemented
76+
Collections.sort(Assignments, new Assignment());
77+
78+
// If schedule[i] has a value -1, it indicates that the
79+
// i'th timeslot in the schedule is empty
80+
int[] homeworkPlan = new int[lastDeadline];
81+
for (int i=0; i < homeworkPlan.length; ++i) {
82+
homeworkPlan[i] = -1;
83+
}
84+
//STUDENT CODE STARTS HERE
85+
//add assign to the array
86+
//if the next one should come before, then swap(?)
87+
88+
//loop through the sorted list
89+
//get the largest deadline
90+
//check if that corresponding slot is occupied
91+
//if it is -->go to the previous slot
92+
//if not --> assign the assignment
93+
for(int i=1; i<=Assignments.size(); i++) {
94+
int theDdl = Assignments.get(i-1).deadline;
95+
if(homeworkPlan[theDdl-1] == -1) {
96+
//there is currently no assignment assiged to this slot
97+
homeworkPlan[theDdl-1] = Assignments.get(i-1).number;
98+
}else {
99+
//go to the previous slot
100+
for(int j=theDdl-2; j>=0; j--) {
101+
if (homeworkPlan[j]==-1) {
102+
homeworkPlan[j] = Assignments.get(i-1).number;
103+
break;
104+
}
105+
}
106+
}
107+
}
108+
return homeworkPlan;
109+
}
110+
}
111+
112+
113+
114+

Assignment2/Kruskal.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//created by Xingya Ren
2+
3+
import java.util.*;
4+
5+
public class Kruskal{
6+
7+
public static WGraph kruskal(WGraph g){
8+
9+
/* Fill this method (The statement return null is here only to compile) */
10+
//takes a graph as input and returns a new graph object that is the MST of the given graph
11+
//sort the list of nodes in ascending weights
12+
WGraph minSpanTree = new WGraph();
13+
DisjointSets setForTheGraph = new DisjointSets(g.getNbNodes()); //because we can't do union on a graph we turn it into a set
14+
//loop through the list of sorted edges
15+
for(Edge e: g.listOfEdgesSorted()) {
16+
if(IsSafe(setForTheGraph, e)) {
17+
//is safe to add --> add it
18+
minSpanTree.addEdge(e);
19+
setForTheGraph.union(e.nodes[0], e.nodes[1]);
20+
}
21+
}
22+
return minSpanTree;
23+
}
24+
25+
public static Boolean IsSafe(DisjointSets p, Edge e){
26+
27+
/* Fill this method (The statement return 0 is here only to compile) */
28+
//nodes p; edge e
29+
//returns true if it is safe to add the edge e to this graph
30+
//false if not
31+
//Using DisjointSets from the previous question
32+
//DisjointSets has: int find(int i) which returns the representative
33+
// int union(int i, int j) which merges two sets and return the rep
34+
35+
//sort the nodes in ascending order
36+
//an array list that holds the sorted edges
37+
int node1 = e.nodes[0];
38+
int node2 = e.nodes[1];
39+
int node1Rep, node2Rep;
40+
node1Rep = p.find(node1);
41+
node2Rep = p.find(node2);
42+
if(node1Rep == node2Rep) {
43+
//in the same set --> not safe
44+
return false;
45+
}else{
46+
return true; //different rep's --> different sets
47+
}
48+
}
49+
50+
public static void main(String[] args){
51+
52+
String file = args[0];
53+
WGraph g = new WGraph(file);
54+
WGraph t = kruskal(g);
55+
System.out.println(t);
56+
57+
}
58+
}

0 commit comments

Comments
 (0)